diff --git a/src/action/client.h b/src/action/client.h index b12f8cef..6a180c8c 100644 --- a/src/action/client.h +++ b/src/action/client.h @@ -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); diff --git a/src/common/util.c b/src/common/util.c index 8e562b19..fb5cb93c 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -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); } \ No newline at end of file diff --git a/src/common/util.h b/src/common/util.h index c7f83f2b..52b509a7 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -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); \ No newline at end of file +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); \ No newline at end of file diff --git a/src/dispatch/bind_define.h b/src/dispatch/bind_define.h index b0b04e1d..6215b962 100644 --- a/src/dispatch/bind_define.h +++ b/src/dispatch/bind_define.h @@ -2070,13 +2070,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); diff --git a/src/layout/scroll.h b/src/layout/scroll.h index dbdfe919..6d290607 100644 --- a/src/layout/scroll.h +++ b/src/layout/scroll.h @@ -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); } } } diff --git a/src/mango.c b/src/mango.c index 679fa9d3..47aa9497 100644 --- a/src/mango.c +++ b/src/mango.c @@ -1290,8 +1290,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); } @@ -1376,11 +1375,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; @@ -1871,11 +1868,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); @@ -1911,11 +1904,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); } @@ -2384,8 +2373,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; } @@ -2412,11 +2400,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); } } @@ -4657,6 +4643,8 @@ void init_client_properties(Client *c) { 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