Debian Codegraph @

src/wm/wm.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui
3
4 #include <stddef.h>
5 #include <uthash.h>
6 #include <xcb/xproto.h>
7
8 #include "common.h"
9 #include "log.h"
10 #include "utils/list.h"
11 #include "x.h"
12
13 #include "win.h"
14 #include "wm.h"
15 #include "wm_internal.h"
16
17 struct wm_query_tree_request {
18 struct x_async_request_base base;
19 struct wm *wm;
20 struct atom *atoms;
21 xcb_window_t wid;
22 };
23
24 struct wm_get_property_request {
25 struct x_async_request_base base;
26 struct wm *wm;
27 xcb_window_t wid;
28 };
29
30 struct wm {
31 /// The toplevel currently being focused. Concretely this is the toplevel window
32 /// that contains the window currently considered by the X server to be focused.
33 struct wm_tree_node *focused_win;
34 struct wm_tree tree;
35
36 /// This is a virtual root for all "orphaned" windows. A window is orphaned
37 /// if it is not reachable from the root node. This can only be non-empty if
38 /// the tree is not consistent, i.e. there are pending async query tree requests.
39 ///
40 /// Note an orphaned window cannot be a toplevel. This is trivially true because
41 /// a toplevel has the root window as its parent, and once the root window is
42 /// created its list of children is always up to date.
43 struct wm_tree_node orphan_root;
44
45 /// Number of pending async imports. This tracks the async event mask setups and
46 /// query tree queries. We also have async get property requests, but they are not
47 /// tracked because they don't affect the tree structure.
48 unsigned n_pending_imports;
49
50 /// Whether cached window leaders should be recalculated. Following tree changes
51 /// will trigger a leader refresh:
52 /// - A toplevel is added. This is because a window leader might be set before
53 /// we
54 /// have imported the target window into our tree.
55 /// - A toplevel is removed.
56 /// - The leader property of any toplevel is changed. A window's leader changing
57 /// affects
58 /// all of its descendants in the leadership tree (this is different from the
59 /// window tree). But keeping track of the leadership tree is too much work,
60 /// so we just refresh all cached leaders.
61 bool needs_leader_refresh;
62 };
63
64 // TODO(yshui): this is a bit weird and I am not decided on it yet myself. Maybe we can
65 // expose `wm_tree_node` directly. But maybe we want to bundle some additional data with
66 // it. Anyway, this is probably easy to get rid of if we want to.
67 /// A wrapper of `wm_tree_node`. This points to the `siblings` `struct list_node` in a
68 /// `struct wm_tree_node`.
69 struct wm_ref {
70 struct list_node inner;
71 };
72 static_assert(offsetof(struct wm_ref, inner) == 0, "wm_cursor should be usable as a "
73 "wm_tree_node");
74 static_assert(alignof(struct wm_ref) == alignof(struct list_node),
75 "wm_cursor should have the same alignment as wm_tree_node");
76
77 static inline const struct wm_tree_node *to_tree_node(const struct wm_ref *cursor) {
78 return cursor != NULL ? list_entry(&cursor->inner, struct wm_tree_node, siblings)
79 : NULL;
80 }
81
82 static inline struct wm_tree_node *to_tree_node_mut(struct wm_ref *cursor) {
83 return cursor != NULL ? list_entry(&cursor->inner, struct wm_tree_node, siblings)
84 : NULL;
85 }
86
87 xcb_window_t wm_ref_win_id(const struct wm_ref *cursor) {
88 return to_tree_node(cursor)->id.x;
89 }
90
91 wm_treeid wm_ref_treeid(const struct wm_ref *cursor) {
92 return to_tree_node(cursor)->id;
93 }
94
95 struct win *wm_ref_deref(const struct wm_ref *cursor) {
96 auto node = to_tree_node(cursor);
97 if (node->parent == NULL) {
98 log_error("Trying to dereference a root node. Expect malfunction.");
99 return NULL;
100 }
101 if (node->parent->parent != NULL) {
102 // Don't return the client window if this is not a toplevel node. This
103 // saves us from needing to clear `->win` when a window is reparented.
104 return NULL;
105 }
106 return node->win;
107 }
108
109 void wm_ref_set(struct wm_ref *cursor, struct win *w) {
110 to_tree_node_mut(cursor)->win = w;
111 }
112
113 void wm_ref_set_focused(struct wm *wm, struct wm_ref *cursor) {
114 auto node = to_tree_node_mut(cursor);
115 if (wm->focused_win == node) {
116 return;
117 }
118
119 log_debug("Focused window changed from %#010x to %#010x",
120 wm->focused_win ? wm->focused_win->id.x : 0, node ? node->id.x : 0);
121 wm->focused_win = node;
122 }
123
124 void wm_ref_set_leader(struct wm *wm, struct wm_ref *cursor, xcb_window_t leader) {
125 // This function only changes `leader`, not the cached `leader_final`. So this has
126 // no impact on the returned node of `wm_focused_leader()` - it will be updated
127 // along with all `leader_final`s in `wm_refresh_leaders`.
128 struct wm_tree_node *node =
129 wm_tree_find_toplevel_for(&wm->tree, to_tree_node_mut(cursor));
130 if (node->leader == leader) {
131 return;
132 }
133
134 wm->needs_leader_refresh = true;
135 node->leader = leader;
136 }
137
138 struct wm_ref *wm_focused_win(struct wm *wm) {
139 return wm->focused_win ? (struct wm_ref *)&wm->focused_win->siblings : NULL;
140 }
141
142 const struct wm_ref *wm_focused_leader(struct wm *wm) {
143 return wm->focused_win != NULL
144 ? (struct wm_ref *)&wm->focused_win->leader_final->siblings
145 : NULL;
146 }
147
148 const struct wm_ref *wm_ref_leader(const struct wm_ref *cursor) {
149 auto node = to_tree_node(cursor);
150 return (struct wm_ref *)&node->leader_final->siblings;
151 }
152
153 bool wm_ref_is_zombie(const struct wm_ref *cursor) {
154 return to_tree_node(cursor)->is_zombie;
155 }
156
157 struct wm_ref *wm_ref_below(const struct wm_ref *cursor) {
158 return &to_tree_node(cursor)->parent->children != cursor->inner.next
159 ? (struct wm_ref *)cursor->inner.next
160 : NULL;
161 }
162
163 struct wm_ref *wm_ref_above(const struct wm_ref *cursor) {
164 return &to_tree_node(cursor)->parent->children != cursor->inner.prev
165 ? (struct wm_ref *)cursor->inner.prev
166 : NULL;
167 }
168
169 struct wm_ref *wm_root_ref(const struct wm *wm) {
170 return (struct wm_ref *)&wm->tree.root->siblings;
171 }
172
173 struct wm_ref *wm_ref_topmost_child(const struct wm_ref *cursor) {
174 auto node = to_tree_node(cursor);
175 return !list_is_empty(&node->children) ? (struct wm_ref *)node->children.next : NULL;
176 }
177
178 struct wm_ref *wm_ref_bottommost_child(const struct wm_ref *cursor) {
179 auto node = to_tree_node(cursor);
180 return !list_is_empty(&node->children) ? (struct wm_ref *)node->children.prev : NULL;
181 }
182
183 struct wm_ref *wm_find(const struct wm *wm, xcb_window_t id) {
184 auto node = wm_tree_find(&wm->tree, id);
185 return node != NULL ? (struct wm_ref *)&node->siblings : NULL;
186 }
187
188 struct wm_ref *wm_find_by_client(const struct wm *wm, xcb_window_t client) {
189 auto node = wm_tree_find(&wm->tree, client);
190 if (node == NULL || node->parent == NULL) {
191 // If `client` is the root window, we return NULL too, technically
192 // the root window doesn't have a client window.
193 return NULL;
194 }
195 auto toplevel = wm_tree_find_toplevel_for(&wm->tree, node);
196 return toplevel != NULL ? (struct wm_ref *)&toplevel->siblings : NULL;
197 }
198
199 struct wm_ref *wm_ref_toplevel_of(const struct wm *wm, struct wm_ref *cursor) {
200 auto toplevel = wm_tree_find_toplevel_for(&wm->tree, to_tree_node_mut(cursor));
201 return toplevel != NULL ? (struct wm_ref *)&toplevel->siblings : NULL;
202 }
203
204 struct wm_ref *wm_ref_client_of(struct wm_ref *cursor) {
205 auto client = to_tree_node(cursor)->client_window;
206 return client != NULL ? (struct wm_ref *)&client->siblings : NULL;
207 }
208
209 struct wm_ref *wm_stack_end(struct wm *wm) {
210 return (struct wm_ref *)&wm->tree.root->children;
211 }
212
213 static struct wm_tree_node *wm_find_leader(struct wm *wm, struct wm_tree_node *node) {
214 if (node->leader_final != NULL) {
215 if (node->visited) {
216 log_warn("Window %#010x is part of a cycle in the leadership "
217 "tree",
218 node->id.x);
219 }
220 return node->leader_final;
221 }
222
223 node->leader_final = node;
224 if (node->leader != node->id.x) {
225 auto leader_node = wm_tree_find(&wm->tree, node->leader);
226 if (leader_node == NULL) {
227 return node->leader_final;
228 }
229 leader_node = wm_tree_find_toplevel_for(&wm->tree, leader_node);
230 if (leader_node == NULL) {
231 log_debug("Cannot find toplevel for leader %#010x of window "
232 "%#010x. tree consistency: %d",
233 node->leader, node->id.x, wm_is_consistent(wm));
234 wm->needs_leader_refresh = true;
235 return node->leader_final;
236 }
237 node->visited = true;
238 node->leader_final = wm_find_leader(wm, leader_node);
239 node->visited = false;
240 }
241 return node->leader_final;
242 }
243
244 void wm_refresh_leaders(struct wm *wm) {
245 if (!wm->needs_leader_refresh) {
246 return;
247 }
248 log_debug("Refreshing window leaders, tree consistency: %d", wm_is_consistent(wm));
249 wm->needs_leader_refresh = false;
250 list_foreach(struct wm_tree_node, i, &wm->tree.root->children, siblings) {
251 if (i->is_zombie) {
252 // Don't change anything about a zombie window.
253 continue;
254 }
255 i->leader_final = NULL;
256 }
257 list_foreach(struct wm_tree_node, i, &wm->tree.root->children, siblings) {
258 if (i->is_zombie) {
259 continue;
260 }
261 wm_find_leader(wm, i);
262 BUG_ON_NULL(i->leader_final);
263 log_verbose("Window %#010x has leader %#010x", i->id.x,
264 i->leader_final->id.x);
265 }
266 if (wm->needs_leader_refresh) {
267 log_debug("Leaders not fully resolved, will try again later.");
268 }
269 }
270
271 /// Move window `w` so it's right above `below`, if `below` is XCB_NONE, `w` is moved
272 /// to the bottom of the stack
273 void wm_stack_move_to_above(struct wm *wm, struct wm_ref *cursor, xcb_window_t below) {
274 BUG_ON_NULL(cursor);
275 auto node = to_tree_node_mut(cursor);
276 if (node->parent == &wm->orphan_root) {
277 // If this window is orphaned, moving it around its siblings is
278 // meaningless. Same below.
279 log_debug("Ignoring restack request for orphaned window %#010x", node->id.x);
280 return;
281 }
282 if (below == XCB_NONE) {
283 // `below` being XCB_NONE means the window is put at the bottom.
284 wm_tree_move_to_end(&wm->tree, node, /*to_bottom=*/true);
285 return;
286 }
287
288 auto below_node = wm_tree_find(&wm->tree, below);
289 if (below_node == NULL) {
290 log_error("Trying to restack window %#010x, but its sibling window "
291 "%#010x is not in our tree. Expect malfunction.",
292 node->id.x, below);
293 assert(false);
294 return;
295 }
296 wm_tree_move_to_above(&wm->tree, node, below_node);
297 }
298
299 void wm_stack_move_to_end(struct wm *wm, struct wm_ref *cursor, bool to_bottom) {
300 auto node = to_tree_node_mut(cursor);
301 if (node->parent == &wm->orphan_root) {
302 return;
303 }
304 wm_tree_move_to_end(&wm->tree, node, to_bottom);
305 }
306
307 struct wm *wm_new(void) {
308 auto wm = ccalloc(1, struct wm);
309 wm_tree_init(&wm->tree);
310 list_init_head(&wm->orphan_root.children);
311 wm->n_pending_imports = 0;
312 return wm;
313 }
314
315 void wm_free(struct wm *wm) {
316 // Free all `struct win`s associated with tree nodes, this leaves dangling
317 // pointers, but we are freeing the tree nodes immediately after, so everything
318 // is fine (TM).
319 if (wm->tree.root != NULL) {
320 wm_stack_foreach_safe(wm, i, next) {
321 auto w = wm_ref_deref(i);
322 auto tree_node = to_tree_node_mut(i);
323 free(w);
324
325 if (tree_node->is_zombie) {
326 // This mainly happens on `session_destroy`, e.g. when
327 // there's ongoing animations.
328 log_debug("Leftover zombie node for window %#010x",
329 tree_node->id.x);
330 wm_tree_reap_zombie(tree_node);
331 }
332 }
333 }
334 wm_tree_clear(&wm->tree);
335 assert(wm_is_consistent(wm));
336 assert(list_is_empty(&wm->orphan_root.children));
337
338 free(wm);
339 }
340
341 /// Move `from->win` to `to->win`, update `win->tree_ref`.
342 static void wm_move_win(struct wm_tree_node *from, struct wm_tree_node *to) {
343 if (from->win != NULL) {
344 from->win->tree_ref = (struct wm_ref *)&to->siblings;
345 }
346 to->win = from->win;
347 from->win = NULL;
348 }
349
350 static void wm_detach_inner(struct wm *wm, struct wm_tree_node *child) {
351 auto zombie = wm_tree_detach(&wm->tree, child);
352 assert(zombie != NULL || child->win == NULL);
353 if (zombie != NULL) {
354 wm_move_win(child, zombie);
355 }
356 }
357
358 /// Disconnect `child` from `parent`. This is called when a window is removed from its
359 /// parent's children list. This can be a result of the window being destroyed, or
360 /// reparented to another window. This `child` is attached to the orphan root.
361 void wm_disconnect(struct wm *wm, xcb_window_t child, xcb_window_t parent,
362 xcb_window_t new_parent) {
363 auto parent_node = wm_tree_find(&wm->tree, parent);
364 auto new_parent_node = wm_tree_find(&wm->tree, new_parent);
365 BUG_ON_NULL(parent_node);
366
367 auto child_node = wm_tree_find(&wm->tree, child);
368 if (child_node == NULL) {
369 // X sends DestroyNotify from child first, then from parent. So if the
370 // child was imported, we will get here. This is the normal case.
371 //
372 // This also happens if the child is created then immediately destroyed,
373 // before we get the CreateNotify event for it. In
374 // `wm_import_start_no_flush` we will ignore this window because setting
375 // the event mask will fail. Then once we get the DestroyNotify for it
376 // from the parent, we get here.
377 log_debug("Child window %#010x of %#010x is not in our tree, ignoring",
378 child, parent);
379 return;
380 }
381
382 // If child_node->parent is not parent_node, then the parent must be in the
383 // process of being queried. In that case the child_node must be orphaned.
384 BUG_ON(child_node->parent != parent_node &&
385 (parent_node->tree_queried || child_node->parent != &wm->orphan_root));
386
387 wm_detach_inner(wm, child_node);
388 if ((new_parent_node != NULL && new_parent_node->receiving_events) ||
389 child_node->receiving_events) {
390 // We need to be sure we will be able to keep track of all orphaned
391 // windows to know none of them will be destroyed without us knowing. If
392 // we have already set up event mask for the new parent, we will be
393 // getting a reparent event from the new parent, which means we will have
394 // an eye on `child` at all times, so that's fine. If we have already set
395 // up event mask on the child itself, that's even better.
396 log_debug("Disconnecting window %#010x from window %#010x", child, parent);
397 wm_tree_attach(&wm->tree, child_node, &wm->orphan_root);
398 } else {
399 // Otherwise, we might potentially lose track of this window, so we have
400 // to destroy it.
401 log_debug("Destroy window %#010x because we can't keep track of it", child);
402 HASH_DEL(wm->tree.nodes, child_node);
403 free(child_node);
404 }
405 }
406
407 void wm_destroy(struct wm *wm, xcb_window_t wid) {
408 struct wm_tree_node *node = wm_tree_find(&wm->tree, wid);
409 BUG_ON_NULL(node);
410
411 log_debug("Destroying window %#010x", wid);
412
413 if (!list_is_empty(&node->children)) {
414 log_error("Window %#010x is destroyed but it still has children. "
415 "Orphaning them.",
416 wid);
417 list_foreach(struct wm_tree_node, i, &node->children, siblings) {
418 log_error(" Child: %#010x", i->id.x);
419 }
420 list_splice(&node->children, &wm->orphan_root.children);
421 }
422
423 if (node == wm->focused_win) {
424 wm->focused_win = NULL;
425 }
426
427 wm_detach_inner(wm, node);
428
429 HASH_DEL(wm->tree.nodes, node);
430 free(node);
431 }
432
433 void wm_reap_zombie(struct wm_ref *zombie) {
434 wm_tree_reap_zombie(to_tree_node_mut(zombie));
435 }
436
437 struct wm_wid_or_node {
438 union {
439 xcb_window_t wid;
440 struct wm_tree_node *node;
441 };
442 bool is_wid;
443 };
444
445 /// Start the import process of `wid`. If `new` is not NULL, it means the window is
446 /// reusing the same window ID as a previously destroyed window, and that destroyed window
447 /// is in our orphan tree. In this case, we revive the orphaned window instead of creating
448 /// a new one.
449 static void wm_import_start_inner(struct wm *wm, struct x_connection *c, struct atom *atoms,
450 xcb_window_t wid, struct wm_tree_node *parent);
451
452 static void wm_reparent_inner(struct wm *wm, struct x_connection *c, struct atom *atoms,
453 xcb_window_t wid, struct wm_tree_node *new_parent) {
454 BUG_ON_NULL(new_parent);
455 auto window = wm_tree_find(&wm->tree, wid);
456
457 /// If a previously unseen window is reparented to a window that has been fully
458 /// imported, we must treat it as a newly created window. Because it will not be
459 /// included in a query tree reply, so we must initiate its import process
460 /// explicitly.
461 if (window == NULL) {
462 if (new_parent->tree_queried) {
463 // Contract: we just checked `wid` is not in the tree.
464 wm_import_start_inner(wm, c, atoms, wid, new_parent);
465 }
466 return;
467 }
468
469 if (window->parent == new_parent) {
470 // Reparent to the same parent moves the window to the top of the
471 // stack
472 BUG_ON(!new_parent->tree_queried);
473 wm_tree_move_to_end(&wm->tree, window, false);
474 return;
475 }
476
477 BUG_ON(window->parent != &wm->orphan_root);
478
479 // Attaching `window` to `new_parent` will change the children list of
480 // `new_parent`, if there is a pending query tree request for `new_parent`, doing
481 // so will create an overlap. In other words, `window` will appear in the query
482 // tree reply too. Generally speaking, we want to keep a node's children list
483 // empty while there is a pending query tree request for it. (Imagine sending the
484 // query tree "locks" the children list until the reply is processed). Same logic
485 // applies to `wm_import_start`.
486 //
487 // Alternatively if the new parent isn't in our tree yet, we orphan the window
488 // too. Or if we have an orphaned window indicating the new parent was reusing
489 // a destroyed window's ID, then we know we will re-query the new parent later
490 // when we encounter it in a query tree reply, so we orphan the window in this
491 // case as well.
492 if (!new_parent->tree_queried) {
493 log_debug("Window %#010x is attached to window %#010x that is currently "
494 "been queried, orphaning.",
495 window->id.x, new_parent->id.x);
496 return;
497 }
498
499 log_debug("Reparented window %#010x to window %#010x", window->id.x,
500 new_parent->id.x);
501 BUG_ON(wm_tree_detach(&wm->tree, window) != NULL);
502 wm_tree_attach(&wm->tree, window, new_parent);
503 }
504
505 void wm_reparent(struct wm *wm, struct x_connection *c, struct atom *atoms,
506 xcb_window_t wid, xcb_window_t parent) {
507 return wm_reparent_inner(wm, c, atoms, wid, wm_tree_find(&wm->tree, parent));
508 }
509
510 void wm_set_has_wm_state(struct wm *wm, struct wm_ref *cursor, bool has_wm_state) {
511 wm_tree_set_wm_state(&wm->tree, to_tree_node_mut(cursor), has_wm_state);
512 }
513
514 static const xcb_event_mask_t WM_IMPORT_EV_MASK = XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
515 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
516 XCB_EVENT_MASK_PROPERTY_CHANGE;
517
518 static void
519 wm_handle_query_tree_reply(struct x_connection *c, struct x_async_request_base *base,
520 const xcb_raw_generic_event_t *reply_or_error) {
521 auto req = (struct wm_query_tree_request *)base;
522 auto atoms = req->atoms;
523 auto wm = req->wm;
524 auto node = wm_tree_find(&wm->tree, req->wid);
525 free(req);
526
527 wm->n_pending_imports--;
528
529 if (reply_or_error == NULL) {
530 // The program is quitting... If this happens when wm is in an
531 // inconsistent state, there could be errors.
532 // TODO(yshui): fix this
533 return;
534 }
535
536 if (node == NULL) {
537 // If the node was previously destroyed, then it means a newly created
538 // window has reused its window ID. We should ignore the query tree reply,
539 // because we haven't setup event mask for this window yet, we won't be
540 // able to stay up-to-date with this window, and would have to re-query in
541 // `wm_import_start_no_flush` anyway. And we don't have a node to attach
542 // the children to anyway.
543 if (reply_or_error->response_type != 0) {
544 log_debug("Ignoring query tree reply for window not in our "
545 "tree.");
546 BUG_ON(wm_is_consistent(wm));
547 }
548 return;
549 }
550
551 if (!node->receiving_events) {
552 log_debug("Window ID %#010x is destroyed then reused, and hasn't had "
553 "event mask set yet.",
554 node->id.x);
555 return;
556 }
557
558 if (reply_or_error->response_type == 0) {
559 // This is an error, most likely the window is gone when we tried
560 // to query it.
561 // A window we are tracking died without us knowing, this should
562 // be impossible.
563 xcb_generic_error_t *err = (xcb_generic_error_t *)reply_or_error;
564 log_error("Query tree request for window %#010x failed with error %s.",
565 node == NULL ? 0 : node->id.x, x_strerror(c, err));
566 BUG_ON(false);
567 }
568
569 if (node->tree_queried) {
570 log_debug("Window %#010x has already been queried.", node->id.x);
571 return;
572 }
573
574 node->tree_queried = true;
575
576 auto reply = (const xcb_query_tree_reply_t *)reply_or_error;
577 log_debug("Finished querying tree for window %#010x", node->id.x);
578
579 auto children = xcb_query_tree_children(reply);
580 log_debug("Window %#010x has %d children", node->id.x,
581 xcb_query_tree_children_length(reply));
582 for (int i = 0; i < xcb_query_tree_children_length(reply); i++) {
583 auto child = children[i];
584 // wm_reparent handles both the case where child is new, and the case
585 // where the child is an known orphan.
586 wm_reparent_inner(wm, c, atoms, child, node);
587 }
588 }
589
590 static void
591 wm_handle_get_wm_state_reply(struct x_connection *c, struct x_async_request_base *base,
592 const xcb_raw_generic_event_t *reply_or_error) {
593 auto req = (struct wm_get_property_request *)base;
594 if (reply_or_error == NULL) {
595 free(req);
596 return;
597 }
598
599 // We guarantee that if a query tree request is pending, its corresponding
600 // window tree node won't be reaped. But we don't guarantee the same for
601 // get property requests. So we need to search the node by window ID again.
602
603 if (reply_or_error->response_type == 0) {
604 // This is an error, most likely the window is gone when we tried
605 // to query it. (Note the tree node might have been freed at this
606 // point if the query tree request also failed earlier.)
607 xcb_generic_error_t *err = (xcb_generic_error_t *)reply_or_error;
608 log_debug("Get WM_STATE request for window %#010x failed with "
609 "error %s",
610 req->wid, x_strerror(c, err));
611 free(req);
612 return;
613 }
614
615 auto node = wm_tree_find(&req->wm->tree, req->wid);
616 if (node == NULL) {
617 // An in-flight query tree request will keep node alive, but if the query
618 // tree request is completed, this node will be allowed to be destroyed.
619 // So if we received a DestroyNotify for `node` in between the reply to
620 // query tree and the reply to get property, `node` will be NULL here.
621 free(req);
622 return;
623 }
624 auto reply = (const xcb_get_property_reply_t *)reply_or_error;
625 wm_tree_set_wm_state(&req->wm->tree, node, reply->type != XCB_NONE);
626 free(req);
627 }
628
629 struct wm_set_event_mask_request {
630 struct x_async_request_base base;
631 struct wm *wm;
632 struct atom *atoms;
633 xcb_window_t wid;
634 };
635
636 static void
637 wm_handle_set_event_mask_reply(struct x_connection *c, struct x_async_request_base *base,
638 const xcb_raw_generic_event_t *reply_or_error) {
639 auto req = (struct wm_set_event_mask_request *)base;
640 auto wm = req->wm;
641 auto atoms = req->atoms;
642 auto wid = req->wid;
643 free(req);
644
645 if (reply_or_error == NULL) {
646 goto end_import;
647 }
648 if (reply_or_error->response_type == 0) {
649 log_debug("Failed to set event mask for window %#010x: %s, ignoring this "
650 "window.",
651 wid, x_strerror(c, (const xcb_generic_error_t *)reply_or_error));
652 goto end_import;
653 }
654
655 auto node = wm_tree_find(&wm->tree, wid);
656 if (node == NULL) {
657 // The window initiated this request is gone, but a window with this wid
658 // does exist - we just haven't gotten the event for its creation yet. We
659 // create a placeholder for it.
660 node = wm_tree_new_window(&wm->tree, wid);
661 wm_tree_add_window(&wm->tree, node);
662 wm_tree_attach(&wm->tree, node, &wm->orphan_root);
663 }
664
665 if (node->receiving_events) {
666 // This means another set event mask request was already completed before
667 // us. We don't need to do anything.
668 log_debug("Event mask already set for window %#010x", wid);
669 goto end_import;
670 }
671
672 log_debug("Event mask set for window %#010x, sending query tree.", wid);
673 node->receiving_events = true;
674
675 {
676 auto req2 = ccalloc(1, struct wm_query_tree_request);
677 req2->base.callback = wm_handle_query_tree_reply;
678 req2->wid = node->id.x;
679 req2->wm = wm;
680 req2->atoms = atoms;
681 x_async_query_tree(c, node->id.x, &req2->base);
682 }
683
684 // (It's OK to resend the get property request even if one is already in-flight,
685 // unlike query tree.)
686 {
687 auto req2 = ccalloc(1, struct wm_get_property_request);
688 req2->base.callback = wm_handle_get_wm_state_reply;
689 req2->wm = wm;
690 req2->wid = node->id.x;
691 x_async_get_property(c, node->id.x, atoms->aWM_STATE, XCB_ATOM_ANY, 0, 2,
692 &req2->base);
693 }
694 return;
695
696 end_import:
697 wm->n_pending_imports--;
698 }
699
700 /// Create a window for `wid`. Send query tree and get property requests, for this new
701 /// window. Caller must guarantee `wid` isn't already in the tree.
702 ///
703 /// Note this function does not flush the X connection.
704 static void wm_import_start_inner(struct wm *wm, struct x_connection *c, struct atom *atoms,
705 xcb_window_t wid, struct wm_tree_node *parent) {
706 // We need to create a tree node immediate before we even know if it still exists.
707 // Because otherwise we have nothing to keep track of its stacking order.
708 auto new = wm_tree_new_window(&wm->tree, wid);
709 wm_tree_add_window(&wm->tree, new);
710 wm_tree_attach(&wm->tree, new, parent);
711
712 log_debug("Starting import process for window %#010x", new->id.x);
713
714 auto req = ccalloc(1, struct wm_set_event_mask_request);
715 req->base.callback = wm_handle_set_event_mask_reply;
716 req->wid = wid;
717 req->atoms = atoms;
718 req->wm = wm;
719 x_async_change_window_attributes(
720 c, wid, XCB_CW_EVENT_MASK, (const uint32_t[]){WM_IMPORT_EV_MASK}, &req->base);
721
722 wm->n_pending_imports++;
723 }
724
725 void wm_import_start(struct wm *wm, struct x_connection *c, struct atom *atoms,
726 xcb_window_t wid, struct wm_ref *parent) {
727 struct wm_tree_node *parent_node = parent != NULL ? to_tree_node_mut(parent) : NULL;
728 if (parent_node != NULL && !parent_node->tree_queried) {
729 // Parent node is currently being queried, we can't attach the new window
730 // to it as that will change its children list.
731 return;
732 }
733 // Contract: how do we know `wid` is not in the tree? First of all, if `wid` is in
734 // the tree, it must be an orphan node, otherwise that would mean the same `wid`
735 // is children of two different windows. Notice a node is added to the orphan root
736 // only after we have confirmed its existence by setting up its event mask. Also
737 // notice replies to event mask setup requests are processed in order. So if we
738 // haven't received a CreateNotify before a window enters the orphan tree, we will
739 // _never_ get a CreateNotify for it.
740 //
741 // We get to here by receiving a CreateNotify for `wid`, therefore `wid` cannot be
742 // in the orphan tree.
743 wm_import_start_inner(wm, c, atoms, wid, parent_node);
744 }
745
746 bool wm_is_consistent(const struct wm *wm) {
747 return wm->n_pending_imports == 0 && list_is_empty(&wm->orphan_root.children);
748 }
749
750 bool wm_has_tree_changes(const struct wm *wm) {
751 return !list_is_empty(&wm->tree.changes);
752 }
753
754 struct wm_change wm_dequeue_change(struct wm *wm) {
755 auto tree_change = wm_tree_dequeue_change(&wm->tree);
756 struct wm_change ret = {
757 .type = tree_change.type,
758 .toplevel = NULL,
759 };
760 switch (tree_change.type) {
761 case WM_TREE_CHANGE_CLIENT:
762 ret.client.old = tree_change.client.old;
763 ret.client.new_ = tree_change.client.new_;
764 ret.toplevel = (struct wm_ref *)&tree_change.client.toplevel->siblings;
765 break;
766 case WM_TREE_CHANGE_TOPLEVEL_KILLED:
767 ret.toplevel = (struct wm_ref *)&tree_change.killed->siblings;
768 wm->needs_leader_refresh = true;
769 break;
770 case WM_TREE_CHANGE_TOPLEVEL_NEW:
771 ret.toplevel = (struct wm_ref *)&tree_change.new_->siblings;
772 wm->needs_leader_refresh = true;
773 break;
774 default: break;
775 }
776 return ret;
777 }
778
779 struct wm_ref *wm_new_mock_window(struct wm *wm, xcb_window_t wid) {
780 auto node = wm_tree_new_window(&wm->tree, wid);
781 return (struct wm_ref *)&node->siblings;
782 }
783 void wm_free_mock_window(struct wm * /*wm*/, struct wm_ref *cursor) {
784 free(to_tree_node_mut(cursor));
785 }