| 1 | #pragma once |
| 2 | |
| 3 | #include <stdalign.h> |
| 4 | |
| 5 | #include <uthash.h> |
| 6 | #include <xcb/xproto.h> |
| 7 | |
| 8 | #include "utils/list.h" |
| 9 | |
| 10 | #include "wm.h" |
| 11 | |
| 12 | struct wm_tree { |
| 13 | /// The generation of the wm tree. This number is incremented every time a new |
| 14 | /// window is created. |
| 15 | /// |
| 16 | /// Because X server recycles window IDs, X ID alone is not enough to uniquely |
| 17 | /// identify a window. This generation number is incremented every time a window |
| 18 | /// is created, so even if a window ID is reused, its generation number is |
| 19 | /// guaranteed to be different from before. Unless, of course, the generation |
| 20 | /// number overflows, but since we are using a uint64_t here, that won't happen |
| 21 | /// for a very long time. Still, it is recommended that you restart the compositor |
| 22 | /// at least once before the Universe collapse back on itself. |
| 23 | uint64_t gen; |
| 24 | /// wm tree nodes indexed by their X window ID. |
| 25 | struct wm_tree_node *nodes; |
| 26 | struct wm_tree_node *root; |
| 27 | |
| 28 | struct list_node changes; |
| 29 | struct list_node free_changes; |
| 30 | }; |
| 31 | |
| 32 | struct wm_query_tree_request; |
| 33 | |
| 34 | struct wm_tree_node { |
| 35 | UT_hash_handle hh; |
| 36 | |
| 37 | struct list_node siblings; |
| 38 | struct list_node children; |
| 39 | |
| 40 | wm_treeid id; |
| 41 | |
| 42 | struct win *win; |
| 43 | struct wm_tree_node *parent; |
| 44 | |
| 45 | /// The client window. Only a toplevel can have a client window. |
| 46 | struct wm_tree_node *client_window; |
| 47 | |
| 48 | /// The leader of the window group. |
| 49 | /// `leader` is the immediate leader of the window, while `leader_final` is the |
| 50 | /// "final" leader, i.e. the last leader if you follow the leader chain. |
| 51 | /// `leader` is a direct property coming from the X server, while `leader_final` |
| 52 | /// is calculated. `leader_final` is calculated by `wm_refresh_leaders` if `struct |
| 53 | /// wm::need_leader_refresh` is true. |
| 54 | /// |
| 55 | /// Note we cannot store pointer to tree node for `leader`. Because leader update |
| 56 | /// and window destruction are not atomic, e.g. when a window is destroyed, some |
| 57 | /// window's leader may still point to the destroyed window. This also means X |
| 58 | /// leader is inherently racy w.r.t. window ID reuse. Leader tracking really is |
| 59 | /// just best effort. |
| 60 | struct wm_tree_node *leader_final; |
| 61 | xcb_window_t leader; |
| 62 | |
| 63 | bool has_wm_state : 1; |
| 64 | /// Whether this window exists only on our side. A zombie window is a toplevel |
| 65 | /// that has been destroyed or reparented (i.e. no long a toplevel) on the X |
| 66 | /// server side, but is kept on our side for things like animations. A zombie |
| 67 | /// window cannot be found in the wm_tree hash table. |
| 68 | bool is_zombie : 1; |
| 69 | bool visited : 1; |
| 70 | /// Whether we have set up event masks on this window. This means we can reliably |
| 71 | /// detect if the window is destroyed. |
| 72 | bool receiving_events : 1; |
| 73 | /// If the initial query tree request has completed. This means the children list |
| 74 | /// of this window is complete w.r.t. the event stream. |
| 75 | bool tree_queried : 1; |
| 76 | }; |
| 77 | |
| 78 | /// Describe a change of a toplevel's client window. |
| 79 | /// A `XCB_NONE` in either `old_client` or `new_client` means a missing client window. |
| 80 | /// i.e. if `old_client` is `XCB_NONE`, it means the toplevel window did not have a client |
| 81 | /// window before the change, and if `new_client` is `XCB_NONE`, it means the toplevel |
| 82 | /// window lost its client window after the change. |
| 83 | struct wm_tree_change { |
| 84 | wm_treeid toplevel; |
| 85 | union { |
| 86 | /// Information for `WM_TREE_CHANGE_CLIENT`. |
| 87 | struct { |
| 88 | struct wm_tree_node *toplevel; |
| 89 | /// The old and new client windows. |
| 90 | wm_treeid old, new_; |
| 91 | } client; |
| 92 | /// Information for `WM_TREE_CHANGE_TOPLEVEL_KILLED`. |
| 93 | /// The zombie window left in place of the killed toplevel. |
| 94 | struct wm_tree_node *killed; |
| 95 | struct wm_tree_node *new_; |
| 96 | }; |
| 97 | |
| 98 | enum wm_tree_change_type type; |
| 99 | }; |
| 100 | |
| 101 | /// Free all tree nodes and changes, without generating any change events. Used when |
| 102 | /// shutting down. |
| 103 | void wm_tree_clear(struct wm_tree *tree); |
| 104 | struct wm_tree_node *attr_pure wm_tree_find(const struct wm_tree *tree, xcb_window_t id); |
| 105 | /// Find the toplevel that is an ancestor of `node` or `node` itself. Returns NULL if |
| 106 | /// `node` is part of an orphaned subtree. |
| 107 | struct wm_tree_node *attr_pure wm_tree_find_toplevel_for(const struct wm_tree *tree, |
| 108 | struct wm_tree_node *node); |
| 109 | struct wm_tree_node *attr_pure wm_tree_next(struct wm_tree_node *node, |
| 110 | struct wm_tree_node *subroot); |
| 111 | /// Create a new window node in the tree, with X window ID `id`, and parent `parent`. If |
| 112 | /// `parent` is NULL, the new node will be the root window. Only one root window is |
| 113 | /// permitted, and the root window cannot be destroyed once created, until |
| 114 | /// `wm_tree_clear` is called. If `parent` is not NULL, the new node will be put at the |
| 115 | /// top of the stacking order among its siblings. |
| 116 | struct wm_tree_node *wm_tree_new_window(struct wm_tree *tree, xcb_window_t id); |
| 117 | void wm_tree_add_window(struct wm_tree *tree, struct wm_tree_node *node); |
| 118 | void wm_tree_destroy_window(struct wm_tree *tree, struct wm_tree_node *node); |
| 119 | /// Detach the subtree rooted at `subroot` from `tree`. The subtree root is removed from |
| 120 | /// its parent, and the disconnected tree nodes won't be able to be found via |
| 121 | /// `wm_tree_find`. Relevant events will be generated. |
| 122 | /// |
| 123 | /// Returns the zombie tree node if one is created, or NULL. |
| 124 | struct wm_tree_node *must_use wm_tree_detach(struct wm_tree *tree, struct wm_tree_node *subroot); |
| 125 | /// Attach `node` to `parent`. `node` becomes the topmost child of `parent`. If `parent` |
| 126 | /// is NULL, `node` becomes the root window. |
| 127 | void wm_tree_attach(struct wm_tree *tree, struct wm_tree_node *child, |
| 128 | struct wm_tree_node *parent); |
| 129 | void wm_tree_move_to_above(struct wm_tree *tree, struct wm_tree_node *node, |
| 130 | struct wm_tree_node *other); |
| 131 | /// Move `node` to the top or the bottom of its parent's child window stack. |
| 132 | void wm_tree_move_to_end(struct wm_tree *tree, struct wm_tree_node *node, bool to_bottom); |
| 133 | struct wm_tree_change wm_tree_dequeue_change(struct wm_tree *tree); |
| 134 | void wm_tree_reap_zombie(struct wm_tree_node *zombie); |
| 135 | void wm_tree_set_wm_state(struct wm_tree *tree, struct wm_tree_node *node, bool has_wm_state); |
| 136 | struct wm_tree_node *attr_pure wm_tree_find_client(struct wm_tree_node *subroot); |
| 137 | |
| 138 | static inline void wm_tree_init(struct wm_tree *tree) { |
| 139 | tree->nodes = NULL; |
| 140 | tree->gen = 1; |
| 141 | list_init_head(&tree->changes); |
| 142 | list_init_head(&tree->free_changes); |
| 143 | } |