opt: Ensure that the client remains in the linked list until it is destroyed

This commit is contained in:
DreamMaoMao 2026-06-29 09:58:13 +08:00
parent 634ca7a51c
commit 22ecd84225
6 changed files with 54 additions and 41 deletions

View file

@ -46,8 +46,8 @@ static void finish_exchange_arrange_and_focus(Client *c1, Client *c2,
} else {
arrange(c1->mon, false, false);
}
wl_list_remove(&c2->flink);
wl_list_insert(&c1->flink, &c2->flink);
wl_list_safe_reinsert_next(&c1->flink, &c2->flink);
if (config.warpcursor)
warp_cursor(c1);

View file

@ -205,4 +205,30 @@ void wl_list_swap(struct wl_list *l1, struct wl_list *l2) {
tmp2_prev->next = l1;
tmp2_next->prev = l1;
}
}
void wl_list_safe_reinsert_prev(struct wl_list *l1, struct wl_list *l2) {
if (!l1 || !l2)
return;
if (l1 == l2)
return;
if (l1->prev == l2)
return;
wl_list_remove(l2);
wl_list_init(l2);
wl_list_insert(l1->prev, l2);
}
void wl_list_safe_reinsert_next(struct wl_list *l1, struct wl_list *l2) {
if (!l1 || !l2)
return;
if (l1 == l2)
return;
if (l1->next == l2)
return;
wl_list_remove(l2);
wl_list_init(l2);
wl_list_insert(l1, l2);
}

View file

@ -12,4 +12,6 @@ char *join_strings(char *arr[], const char *sep);
char *join_strings_with_suffix(char *arr[], const char *suffix,
const char *sep);
char *string_printf(const char *fmt, ...);
void wl_list_swap(struct wl_list *l1, struct wl_list *l2);
void wl_list_swap(struct wl_list *l1, struct wl_list *l2);
void wl_list_safe_reinsert_prev(struct wl_list *l1, struct wl_list *l2);
void wl_list_safe_reinsert_next(struct wl_list *l1, struct wl_list *l2);

View file

@ -2071,13 +2071,11 @@ int32_t scroller_apply_stack(Client *c, Client *target_client,
if (direction == LEFT || direction == UP) {
if (c != stack_head) {
wl_list_remove(&c->link);
wl_list_insert(stack_head->link.prev, &c->link);
wl_list_safe_reinsert_prev(&stack_head->link, &c->link);
}
} else if (direction == RIGHT || direction == DOWN) {
if (c != stack_tail) {
wl_list_remove(&c->link);
wl_list_insert(&stack_tail->link, &c->link);
wl_list_safe_reinsert_next(&stack_head->link, &c->link);
}
}
sync_scroller_state_to_clients(m, tag);

View file

@ -785,16 +785,14 @@ void scroller_insert_stack(Client *c, Client *target_client,
if (tnode->prev_in_stack)
tnode->prev_in_stack->next_in_stack = newnode;
tnode->prev_in_stack = newnode;
wl_list_remove(&c->link);
wl_list_insert(tnode->client->link.prev, &c->link);
wl_list_safe_reinsert_prev(&tnode->client->link, &c->link);
} else {
newnode->prev_in_stack = tnode;
newnode->next_in_stack = tnode->next_in_stack;
if (tnode->next_in_stack)
tnode->next_in_stack->prev_in_stack = newnode;
tnode->next_in_stack = newnode;
wl_list_remove(&c->link);
wl_list_insert(&tnode->client->link, &c->link);
wl_list_safe_reinsert_next(&tnode->client->link, &c->link);
}
/* 处理堆叠头部的全屏/最大化状态*/
@ -833,13 +831,11 @@ void scroller_drop_tile(Client *c, Client *closest, int vertical) {
return;
} else if (closest->drop_direction == UP) {
if (c != stack_head) {
wl_list_remove(&c->link);
wl_list_insert(stack_head->link.prev, &c->link);
wl_list_safe_reinsert_prev(&stack_head->link, &c->link);
}
} else if (closest->drop_direction == DOWN) {
if (c != stack_tail) {
wl_list_remove(&c->link);
wl_list_insert(&stack_tail->link, &c->link);
wl_list_safe_reinsert_next(&stack_head->link, &c->link);
}
}
} else {
@ -853,13 +849,11 @@ void scroller_drop_tile(Client *c, Client *closest, int vertical) {
return;
} else if (closest->drop_direction == LEFT) {
if (c != stack_head) {
wl_list_remove(&c->link);
wl_list_insert(stack_head->link.prev, &c->link);
wl_list_safe_reinsert_prev(&stack_head->link, &c->link);
}
} else if (closest->drop_direction == RIGHT) {
if (c != stack_tail) {
wl_list_remove(&c->link);
wl_list_insert(&stack_tail->link, &c->link);
wl_list_safe_reinsert_next(&stack_head->link, &c->link);
}
}
}

View file

@ -1284,8 +1284,7 @@ void show_scratchpad(Client *c) {
}
c->oldtags = c->mon->tagset[c->mon->seltags];
wl_list_remove(&c->link); // 从原来位置移除
wl_list_insert(clients.prev->next, &c->link); // 插入开头
wl_list_safe_reinsert_next(&clients, &c->link);
show_hide_client(c);
setborder_color(c);
}
@ -1368,11 +1367,9 @@ void client_replace(Client *c, Client *w, bool isgroupaction) {
wlr_scene_node_set_enabled(&w->group_bar->scene_buffer->node, false);
}
wl_list_remove(&c->link);
wl_list_insert(&w->link, &c->link);
wl_list_safe_reinsert_next(&w->link, &c->link);
wl_list_remove(&c->flink);
wl_list_insert(&w->flink, &c->flink);
wl_list_safe_reinsert_prev(&w->flink, &c->flink);
w->is_logic_hide = true;
c->is_logic_hide = false;
@ -1860,11 +1857,7 @@ void applyrules(Client *c) {
(!c->istagsilent || !newtags || newtags & mon->tagset[mon->seltags]);
if (!should_init_get_focus) {
if (c->flink.prev && c->flink.next && c->flink.prev != &c->flink) {
wl_list_remove(&c->flink);
wl_list_init(&c->flink);
}
wl_list_insert(fstack.prev, &c->flink);
wl_list_safe_reinsert_prev(&fstack, &c->flink);
}
setmon(c, mon, newtags, should_init_get_focus);
@ -1900,11 +1893,7 @@ void applyrules(Client *c) {
}
if (c->isfloating && !c->iscustompos && !c->isnamedscratchpad) {
if (c->link.prev && c->link.next && c->link.prev != &c->link) {
wl_list_remove(&c->link);
wl_list_init(&c->link);
}
wl_list_insert(clients.prev, &c->link);
wl_list_safe_reinsert_prev(&clients, &c->link);
set_float_malposition(c);
}
@ -2373,8 +2362,7 @@ void place_drag_tile_client(Client *c) {
if (closest->drop_direction == UNDIR) {
setfloating(c, 0);
wl_list_remove(&c->link);
wl_list_insert(closest->link.prev, &c->link);
wl_list_safe_reinsert_prev(&closest->link, &c->link);
arrange(closest->mon, false, false);
return;
}
@ -2401,11 +2389,9 @@ void place_drag_tile_client(Client *c) {
}
if (closest->drop_direction == LEFT || closest->drop_direction == UP) {
wl_list_remove(&c->link);
wl_list_insert(closest->link.prev, &c->link);
wl_list_safe_reinsert_prev(&closest->link, &c->link);
} else {
wl_list_remove(&c->link);
wl_list_insert(&closest->link, &c->link);
wl_list_safe_reinsert_next(&closest->link, &c->link);
}
}
@ -4538,6 +4524,13 @@ void init_client_properties(Client *c) {
sizeof(c->opacity_animation.current_border_color));
c->opacity_animation.initial_opacity = c->unfocused_opacity;
c->opacity_animation.current_opacity = c->unfocused_opacity;
c->animation.tagining = false;
c->animation.running = false;
c->animation.overining = false;
c->animation.tagouting = false;
c->animation.tagouted = false;
wl_list_init(&c->link);
wl_list_init(&c->flink);
}
void // old fix to 0.5