Debian Codegraph @

src/wm/tree.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3
4 /// In my ideal world, the compositor shouldn't be concerned with the X window tree. It
5 /// should only need to care about the toplevel windows. However, because we support
6 /// window rules based on window properties, which can be set on any descendant of a
7 /// toplevel, we need to keep track of the entire window tree.
8 ///
9 /// For descendants of a toplevel window, what we actually care about is what's called a
10 /// "client" window. A client window is a window with the `WM_STATE` property set, in
11 /// theory and descendants of a toplevel can gain/lose this property at any time. So we
12 /// setup a minimal structure for every single window to keep track of this. And once
13 /// a window becomes a client window, it will have our full attention and have all of its
14 /// information stored in the toplevel `struct managed_win`.
15
16 #include <uthash.h>
17 #include <xcb/xproto.h>
18
19 #include "log.h"
20 #include "utils/list.h"
21 #include "utils/misc.h"
22
23 #include "wm.h"
24 #include "wm_internal.h"
25
26 struct wm_tree_change_list {
27 struct wm_tree_change item;
28 struct list_node siblings;
29 };
30
31 void wm_tree_reap_zombie(struct wm_tree_node *zombie) {
32 BUG_ON(!zombie->is_zombie);
33 list_remove(&zombie->siblings);
34 free(zombie);
35 }
36
37 /// Enqueue a tree change.
38 static void wm_tree_enqueue_change(struct wm_tree *tree, struct wm_tree_change change) {
39 struct wm_tree_change_list *change_list;
40 if (!list_is_empty(&tree->free_changes)) {
41 change_list = list_entry(tree->free_changes.next,
42 struct wm_tree_change_list, siblings);
43 list_remove(&change_list->siblings);
44 } else {
45 change_list = cmalloc(struct wm_tree_change_list);
46 }
47
48 change_list->item = change;
49 list_insert_before(&tree->changes, &change_list->siblings);
50 }
51
52 /// Enqueue a `WM_TREE_CHANGE_TOPLEVEL_KILLED` change for a toplevel window. If there are
53 /// any `WM_TREE_CHANGE_TOPLEVEL_NEW` changes in the queue for the same toplevel, they
54 /// will be cancelled out.
55 ///
56 /// @return true if this change is cancelled out by a previous change, false otherwise.
57 static bool wm_tree_enqueue_toplevel_killed(struct wm_tree *tree, wm_treeid toplevel,
58 struct wm_tree_node *zombie) {
59 // A gone toplevel will cancel out a previous
60 // `WM_TREE_CHANGE_TOPLEVEL_NEW` change in the queue.
61 bool found = false;
62 list_foreach_safe(struct wm_tree_change_list, i, &tree->changes, siblings) {
63 if (!wm_treeid_eq(i->item.toplevel, toplevel)) {
64 continue;
65 }
66 if (i->item.type == WM_TREE_CHANGE_TOPLEVEL_NEW) {
67 list_remove(&i->siblings);
68 list_insert_after(&tree->free_changes, &i->siblings);
69 found = true;
70 } else if (found) {
71 // We also need to delete all other changes
72 // related to this toplevel in between the new and
73 // gone changes.
74 list_remove(&i->siblings);
75 list_insert_after(&tree->free_changes, &i->siblings);
76 } else if (i->item.type == WM_TREE_CHANGE_CLIENT) {
77 // Need to update client changes, so they points to the
78 // zombie instead of the old toplevel node, since the old
79 // toplevel node could be freed before tree changes are
80 // processed.
81 i->item.client.toplevel = zombie;
82 }
83 }
84 if (found) {
85 wm_tree_reap_zombie(zombie);
86 return true;
87 }
88
89 wm_tree_enqueue_change(tree, (struct wm_tree_change){
90 .toplevel = toplevel,
91 .type = WM_TREE_CHANGE_TOPLEVEL_KILLED,
92 .killed = zombie,
93 });
94 return false;
95 }
96
97 static void wm_tree_enqueue_client_change(struct wm_tree *tree, struct wm_tree_node *toplevel,
98 wm_treeid old_client, wm_treeid new_client) {
99 // A client change can coalesce with a previous client change.
100 list_foreach_safe(struct wm_tree_change_list, i, &tree->changes, siblings) {
101 if (!wm_treeid_eq(i->item.toplevel, toplevel->id) ||
102 i->item.type != WM_TREE_CHANGE_CLIENT) {
103 continue;
104 }
105
106 if (!wm_treeid_eq(i->item.client.new_, old_client)) {
107 log_warn("Inconsistent client change for toplevel "
108 "%#010x. Missing changes from %#010x to %#010x. "
109 "Possible bug.",
110 toplevel->id.x, i->item.client.new_.x, old_client.x);
111 }
112
113 i->item.client.new_ = new_client;
114 if (wm_treeid_eq(i->item.client.old, new_client)) {
115 list_remove(&i->siblings);
116 list_insert_after(&tree->free_changes, &i->siblings);
117 }
118 return;
119 }
120 wm_tree_enqueue_change(tree, (struct wm_tree_change){
121 .toplevel = toplevel->id,
122 .type = WM_TREE_CHANGE_CLIENT,
123 .client =
124 {
125 .toplevel = toplevel,
126 .old = old_client,
127 .new_ = new_client,
128 },
129 });
130 }
131
132 static void wm_tree_enqueue_toplevel_new(struct wm_tree *tree, struct wm_tree_node *toplevel) {
133 // We don't let a `WM_TREE_CHANGE_TOPLEVEL_NEW` cancel out a previous
134 // `WM_TREE_CHANGE_TOPLEVEL_KILLED`, because the new toplevel would be a different
135 // window reusing the same ID. So we need to go through the proper destruction
136 // process for the previous toplevel. Changes are not commutative (naturally).
137 wm_tree_enqueue_change(tree, (struct wm_tree_change){
138 .toplevel = toplevel->id,
139 .type = WM_TREE_CHANGE_TOPLEVEL_NEW,
140 .new_ = toplevel,
141 });
142 }
143
144 static void wm_tree_enqueue_toplevel_restacked(struct wm_tree *tree) {
145 list_foreach(struct wm_tree_change_list, i, &tree->changes, siblings) {
146 if (i->item.type == WM_TREE_CHANGE_TOPLEVEL_RESTACKED ||
147 i->item.type == WM_TREE_CHANGE_TOPLEVEL_NEW ||
148 i->item.type == WM_TREE_CHANGE_TOPLEVEL_KILLED) {
149 // Only need to keep one
150 // `WM_TREE_CHANGE_TOPLEVEL_RESTACKED` change, and order
151 // doesn't matter.
152 return;
153 }
154 }
155 wm_tree_enqueue_change(tree, (struct wm_tree_change){
156 .type = WM_TREE_CHANGE_TOPLEVEL_RESTACKED,
157 });
158 }
159
160 /// Dequeue the oldest change from the change queue. If the queue is empty, a change with
161 /// `toplevel` set to `XCB_NONE` will be returned.
162 struct wm_tree_change wm_tree_dequeue_change(struct wm_tree *tree) {
163 if (list_is_empty(&tree->changes)) {
164 return (struct wm_tree_change){.type = WM_TREE_CHANGE_NONE};
165 }
166
167 auto change = list_entry(tree->changes.next, struct wm_tree_change_list, siblings);
168 list_remove(&change->siblings);
169 list_insert_after(&tree->free_changes, &change->siblings);
170 return change->item;
171 }
172
173 /// Return the next node in the subtree after `node` in a pre-order traversal. Returns
174 /// NULL if `node` is the last node in the traversal.
175 struct wm_tree_node *wm_tree_next(struct wm_tree_node *node, struct wm_tree_node *subroot) {
176 if (node == NULL) {
177 return NULL;
178 }
179
180 if (!list_is_empty(&node->children)) {
181 // Descend if there are children
182 return list_entry(node->children.next, struct wm_tree_node, siblings);
183 }
184
185 while (node != subroot && node->siblings.next == &node->parent->children) {
186 // If the current node has no more children, go back to the
187 // parent.
188 node = node->parent;
189 }
190 if (node == subroot) {
191 // We've gone past the topmost node for our search, stop.
192 return NULL;
193 }
194 return list_entry(node->siblings.next, struct wm_tree_node, siblings);
195 }
196
197 /// Find a client window under a toplevel window. If there are multiple windows with
198 /// `WM_STATE` set under the toplevel window, we will return an arbitrary one.
199 struct wm_tree_node *attr_pure wm_tree_find_client(struct wm_tree_node *subroot) {
200 if (subroot->has_wm_state) {
201 log_debug("Toplevel %#010x has WM_STATE set, weird. Using itself as its "
202 "client window.",
203 subroot->id.x);
204 return subroot;
205 }
206
207 BUG_ON(subroot->parent == NULL); // Trying to find client window on the
208 // root window
209
210 for (auto curr = subroot; curr != NULL; curr = wm_tree_next(curr, subroot)) {
211 if (curr->has_wm_state) {
212 return curr;
213 }
214 }
215
216 return NULL;
217 }
218
219 struct wm_tree_node *wm_tree_find(const struct wm_tree *tree, xcb_window_t id) {
220 struct wm_tree_node *node = NULL;
221 HASH_FIND_INT(tree->nodes, &id, node);
222 return node;
223 }
224
225 struct wm_tree_node *
226 wm_tree_find_toplevel_for(const struct wm_tree *tree, struct wm_tree_node *node) {
227 BUG_ON_NULL(node);
228 BUG_ON_NULL(node->parent); // Trying to find toplevel for the root
229 // window
230
231 struct wm_tree_node *toplevel;
232 for (auto curr = node; curr->parent != NULL; curr = curr->parent) {
233 toplevel = curr;
234 }
235 return toplevel->parent == tree->root ? toplevel : NULL;
236 }
237
238 /// Change whether a tree node has the `WM_STATE` property set.
239 /// `destroyed` indicate whether `node` is about to be destroyed, in which case, the `old`
240 /// field of the change event will be set to NULL.
241 void wm_tree_set_wm_state(struct wm_tree *tree, struct wm_tree_node *node, bool has_wm_state) {
242 BUG_ON(node == NULL);
243
244 if (node->has_wm_state == has_wm_state) {
245 log_debug("WM_STATE unchanged call (window %#010x, WM_STATE %d).",
246 node->id.x, has_wm_state);
247 return;
248 }
249
250 node->has_wm_state = has_wm_state;
251 BUG_ON(node->parent == NULL); // Trying to set WM_STATE on the root window
252
253 struct wm_tree_node *toplevel = wm_tree_find_toplevel_for(tree, node);
254 if (toplevel == NULL) {
255 return;
256 }
257
258 if (toplevel == node) {
259 log_debug("Setting WM_STATE on a toplevel window %#010x, weird.", node->id.x);
260 }
261
262 if (!has_wm_state && toplevel->client_window == node) {
263 auto new_client = wm_tree_find_client(toplevel);
264 toplevel->client_window = new_client;
265 wm_tree_enqueue_client_change(
266 tree, toplevel, node->id,
267 new_client != NULL ? new_client->id : WM_TREEID_NONE);
268 } else if (has_wm_state && toplevel->client_window == NULL) {
269 toplevel->client_window = node;
270 wm_tree_enqueue_client_change(tree, toplevel, WM_TREEID_NONE, node->id);
271 } else if (has_wm_state) {
272 // If the toplevel window already has a client window, we won't
273 // try to usurp it.
274 log_debug("Toplevel window %#010x already has a client window "
275 "%#010x, ignoring new client window %#010x. I don't "
276 "like your window manager.",
277 toplevel->id.x, toplevel->client_window->id.x, node->id.x);
278 }
279 }
280
281 struct wm_tree_node *wm_tree_new_window(struct wm_tree *tree, xcb_window_t id) {
282 auto node = ccalloc(1, struct wm_tree_node);
283 node->id.x = id;
284 node->id.gen = tree->gen++;
285 node->has_wm_state = false;
286 node->receiving_events = false;
287 node->is_zombie = false;
288 node->visited = false;
289 node->leader = id;
290 list_init_head(&node->children);
291 return node;
292 }
293
294 void wm_tree_add_window(struct wm_tree *tree, struct wm_tree_node *node) {
295 HASH_ADD_INT(tree->nodes, id.x, node);
296 }
297
298 static void
299 wm_tree_refresh_client_and_queue_change(struct wm_tree *tree, struct wm_tree_node *toplevel) {
300 BUG_ON_NULL(toplevel);
301 BUG_ON_NULL(toplevel->parent);
302 BUG_ON(toplevel->parent->parent != NULL);
303 auto new_client = wm_tree_find_client(toplevel);
304 if (new_client != toplevel->client_window) {
305 wm_treeid old_client_id = WM_TREEID_NONE, new_client_id = WM_TREEID_NONE;
306 if (toplevel->client_window != NULL) {
307 old_client_id = toplevel->client_window->id;
308 }
309 if (new_client != NULL) {
310 new_client_id = new_client->id;
311 }
312 log_debug("Toplevel window %#010x had client window %#010x, now has "
313 "%#010x.",
314 toplevel->id.x, old_client_id.x, new_client_id.x);
315 toplevel->client_window = new_client;
316 wm_tree_enqueue_client_change(tree, toplevel, old_client_id, new_client_id);
317 }
318 }
319
320 struct wm_tree_node *wm_tree_detach(struct wm_tree *tree, struct wm_tree_node *subroot) {
321 BUG_ON(subroot == NULL);
322 BUG_ON(subroot->parent == NULL); // Trying to detach the root window?!
323
324 auto toplevel = wm_tree_find_toplevel_for(tree, subroot);
325 struct wm_tree_node *zombie = NULL;
326 if (toplevel != subroot) {
327 list_remove(&subroot->siblings);
328 if (toplevel != NULL) {
329 wm_tree_refresh_client_and_queue_change(tree, toplevel);
330 }
331 } else {
332 // Detached a toplevel, create a zombie for it
333 log_debug("Detaching toplevel window %#010x.", subroot->id.x);
334 zombie = ccalloc(1, struct wm_tree_node);
335 zombie->parent = subroot->parent;
336 zombie->id = subroot->id;
337 zombie->is_zombie = true;
338 list_init_head(&zombie->children);
339 list_replace(&subroot->siblings, &zombie->siblings);
340 if (wm_tree_enqueue_toplevel_killed(tree, subroot->id, zombie)) {
341 zombie = NULL;
342 }
343
344 // Gen bump must happen after enqueuing the change, because otherwise the
345 // kill change won't cancel out a previous new change because the IDs will
346 // be different.
347 subroot->id.gen = tree->gen++;
348 subroot->client_window = NULL;
349 }
350 subroot->parent = NULL;
351 return zombie;
352 }
353
354 void wm_tree_attach(struct wm_tree *tree, struct wm_tree_node *child,
355 struct wm_tree_node *parent) {
356 BUG_ON(child->parent != NULL); // Trying to attach a window that's already
357 // attached
358 child->parent = parent;
359 if (parent == NULL) {
360 BUG_ON(tree->root != NULL); // Trying to create a second root
361 // window
362 tree->root = child;
363 return;
364 }
365
366 list_insert_after(&parent->children, &child->siblings);
367
368 auto toplevel = wm_tree_find_toplevel_for(tree, child);
369 if (child == toplevel) {
370 wm_tree_enqueue_toplevel_new(tree, child);
371 }
372 if (toplevel != NULL) {
373 wm_tree_refresh_client_and_queue_change(tree, toplevel);
374 }
375 }
376
377 void wm_tree_move_to_end(struct wm_tree *tree, struct wm_tree_node *node, bool to_bottom) {
378 BUG_ON(node == NULL);
379 BUG_ON(node->parent == NULL); // Trying to move the root window
380
381 if ((node->parent->children.next == &node->siblings && !to_bottom) ||
382 (node->parent->children.prev == &node->siblings && to_bottom)) {
383 // Already at the target position
384 return;
385 }
386 list_remove(&node->siblings);
387 if (to_bottom) {
388 list_insert_before(&node->parent->children, &node->siblings);
389 } else {
390 list_insert_after(&node->parent->children, &node->siblings);
391 }
392 if (node->parent == tree->root) {
393 wm_tree_enqueue_toplevel_restacked(tree);
394 }
395 }
396
397 /// Move `node` to above `other` in their parent's child window stack.
398 void wm_tree_move_to_above(struct wm_tree *tree, struct wm_tree_node *node,
399 struct wm_tree_node *other) {
400 BUG_ON(node == NULL);
401 BUG_ON(node->parent == NULL); // Trying to move the root window
402 BUG_ON(other == NULL);
403 BUG_ON(node->parent != other->parent);
404
405 if (node->siblings.next == &other->siblings) {
406 // Already above `other`
407 return;
408 }
409
410 list_remove(&node->siblings);
411 list_insert_before(&other->siblings, &node->siblings);
412 if (node->parent == tree->root) {
413 wm_tree_enqueue_toplevel_restacked(tree);
414 }
415 }
416
417 void wm_tree_clear(struct wm_tree *tree) {
418 struct wm_tree_node *cur, *tmp;
419 HASH_ITER(hh, tree->nodes, cur, tmp) {
420 HASH_DEL(tree->nodes, cur);
421 free(cur);
422 }
423 list_foreach_safe(struct wm_tree_change_list, i, &tree->changes, siblings) {
424 list_remove(&i->siblings);
425 free(i);
426 }
427 list_foreach_safe(struct wm_tree_change_list, i, &tree->free_changes, siblings) {
428 list_remove(&i->siblings);
429 free(i);
430 }
431 }
432
433 TEST_CASE(tree_manipulation) {
434 struct wm_tree tree;
435 wm_tree_init(&tree);
436
437 wm_tree_add_window(&tree, wm_tree_new_window(&tree, 1));
438 auto root = wm_tree_find(&tree, 1);
439 TEST_NOTEQUAL(root, NULL);
440 TEST_EQUAL(root->parent, NULL);
441
442 tree.root = root;
443
444 auto change = wm_tree_dequeue_change(&tree);
445 TEST_EQUAL(change.type, WM_TREE_CHANGE_NONE);
446
447 auto node2 = wm_tree_new_window(&tree, 2);
448 wm_tree_add_window(&tree, node2);
449 wm_tree_attach(&tree, node2, root);
450 TEST_NOTEQUAL(node2, NULL);
451 TEST_EQUAL(node2, wm_tree_find(&tree, 2));
452 TEST_EQUAL(node2->parent, root);
453
454 change = wm_tree_dequeue_change(&tree);
455 TEST_EQUAL(change.toplevel.x, 2);
456 TEST_EQUAL(change.type, WM_TREE_CHANGE_TOPLEVEL_NEW);
457 TEST_TRUE(wm_treeid_eq(node2->id, change.toplevel));
458
459 auto node3 = wm_tree_new_window(&tree, 3);
460 wm_tree_add_window(&tree, node3);
461 wm_tree_attach(&tree, node3, root);
462
463 change = wm_tree_dequeue_change(&tree);
464 TEST_EQUAL(change.toplevel.x, 3);
465 TEST_EQUAL(change.type, WM_TREE_CHANGE_TOPLEVEL_NEW);
466
467 auto zombie = wm_tree_detach(&tree, node2);
468 wm_tree_attach(&tree, node2, node3);
469 TEST_EQUAL(node2->parent, node3);
470 TEST_EQUAL(node3->children.next, &node2->siblings);
471
472 // node2 is now a child of node3, so it's no longer a toplevel
473 change = wm_tree_dequeue_change(&tree);
474 TEST_EQUAL(change.toplevel.x, 2);
475 TEST_EQUAL(change.type, WM_TREE_CHANGE_TOPLEVEL_KILLED);
476 TEST_EQUAL(change.killed, zombie);
477 wm_tree_reap_zombie(change.killed);
478
479 wm_tree_set_wm_state(&tree, node2, true);
480 change = wm_tree_dequeue_change(&tree);
481 TEST_EQUAL(change.toplevel.x, 3);
482 TEST_EQUAL(change.type, WM_TREE_CHANGE_CLIENT);
483 TEST_TRUE(wm_treeid_eq(change.client.old, WM_TREEID_NONE));
484 TEST_EQUAL(change.client.new_.x, 2);
485
486 auto node4 = wm_tree_new_window(&tree, 4);
487 wm_tree_add_window(&tree, node4);
488 wm_tree_attach(&tree, node4, node3);
489 change = wm_tree_dequeue_change(&tree);
490 TEST_EQUAL(change.type, WM_TREE_CHANGE_NONE);
491
492 wm_tree_set_wm_state(&tree, node4, true);
493 change = wm_tree_dequeue_change(&tree);
494 // node3 already has node2 as its client window, so the new one should be ignored.
495 TEST_EQUAL(change.type, WM_TREE_CHANGE_NONE);
496
497 TEST_EQUAL(wm_tree_detach(&tree, node2), NULL);
498 HASH_DEL(tree.nodes, node2);
499 free(node2);
500 change = wm_tree_dequeue_change(&tree);
501 TEST_EQUAL(change.toplevel.x, 3);
502 TEST_EQUAL(change.type, WM_TREE_CHANGE_CLIENT);
503 TEST_EQUAL(change.client.old.x, 2);
504 TEST_EQUAL(change.client.new_.x, 4);
505
506 // Test window ID reuse
507 TEST_EQUAL(wm_tree_detach(&tree, node4), NULL);
508 HASH_DEL(tree.nodes, node4);
509 free(node4);
510 node4 = wm_tree_new_window(&tree, 4);
511 wm_tree_add_window(&tree, node4);
512 wm_tree_attach(&tree, node4, node3);
513 wm_tree_set_wm_state(&tree, node4, true);
514
515 change = wm_tree_dequeue_change(&tree);
516 TEST_EQUAL(change.toplevel.x, 3);
517 TEST_EQUAL(change.type, WM_TREE_CHANGE_CLIENT);
518 TEST_EQUAL(change.client.old.x, 4);
519 TEST_EQUAL(change.client.new_.x, 4);
520
521 auto node5 = wm_tree_new_window(&tree, 5);
522 wm_tree_add_window(&tree, node5);
523 wm_tree_attach(&tree, node5, root);
524 TEST_EQUAL(wm_tree_detach(&tree, node5), NULL);
525 HASH_DEL(tree.nodes, node5);
526 free(node5);
527 change = wm_tree_dequeue_change(&tree);
528 TEST_EQUAL(change.type, WM_TREE_CHANGE_NONE); // Changes cancelled out
529
530 wm_tree_clear(&tree);
531 }