| 1 | // SPDX-License-Identifier: MPL-2.0 |
| 2 | // Copyright (c) Yuxuan Shui |
| 3 | |
| 4 | /// Yeah... We have our own window manager inside the compositor. As a compositor, we do |
| 5 | /// need to do a little bit of what a window manager does, to correctly render windows. |
| 6 | /// But our window manager is a lot less sophisticated than a average window manager. We |
| 7 | /// only keep track of a list of top-level windows, and the order they are stacked. |
| 8 | /// But OTOH doing window managing here is also somewhat more challenging. As we are not |
| 9 | /// a window manager, we don't actually know what window is an application window, what |
| 10 | /// is not. We have to rely on the real window manager playing nice and following the |
| 11 | /// ICCCM and EWMH standards. |
| 12 | |
| 13 | #pragma once |
| 14 | |
| 15 | #include <assert.h> |
| 16 | #include <stdalign.h> |
| 17 | |
| 18 | #include <uthash.h> |
| 19 | #include <xcb/xproto.h> |
| 20 | |
| 21 | #include <picom/types.h> |
| 22 | |
| 23 | #include "compiler.h" |
| 24 | |
| 25 | struct wm; |
| 26 | struct win; |
| 27 | struct list_node; |
| 28 | struct x_connection; |
| 29 | |
| 30 | /// Direct children of a toplevel. |
| 31 | struct subwin { |
| 32 | xcb_window_t id; |
| 33 | xcb_window_t toplevel; |
| 34 | enum tristate has_wm_state; |
| 35 | UT_hash_handle hh; |
| 36 | }; |
| 37 | |
| 38 | enum wm_tree_change_type { |
| 39 | /// The client window of a toplevel changed |
| 40 | WM_TREE_CHANGE_CLIENT, |
| 41 | /// A toplevel window is killed on the X server side |
| 42 | /// A zombie will be left in its place. |
| 43 | WM_TREE_CHANGE_TOPLEVEL_KILLED, |
| 44 | /// A new toplevel window appeared |
| 45 | WM_TREE_CHANGE_TOPLEVEL_NEW, |
| 46 | |
| 47 | // TODO(yshui): This is a stop-gap measure to make sure we invalidate `reg_ignore` |
| 48 | // of windows. Once we get rid of `reg_ignore`, which is only used by the legacy |
| 49 | // backends, this event should be removed. |
| 50 | // |
| 51 | // (`reg_ignore` is the cached cumulative opaque region of all windows above a |
| 52 | // window in the stacking order. If it actually is performance critical, we |
| 53 | // can probably cache it more cleanly in renderer layout.) |
| 54 | |
| 55 | /// The stacking order of toplevel windows changed. Note, toplevel gone/new |
| 56 | /// changes also imply a restack. |
| 57 | WM_TREE_CHANGE_TOPLEVEL_RESTACKED, |
| 58 | |
| 59 | /// Nothing changed |
| 60 | WM_TREE_CHANGE_NONE, |
| 61 | }; |
| 62 | |
| 63 | typedef struct wm_treeid { |
| 64 | /// The generation of the window ID. This is used to detect if the window ID is |
| 65 | /// reused. Inherited from the wm_tree at cr |
| 66 | alignas(8) uint64_t gen; |
| 67 | /// The X window ID. |
| 68 | xcb_window_t x; |
| 69 | |
| 70 | /// Explicit padding |
| 71 | char padding[4]; |
| 72 | } wm_treeid; |
| 73 | |
| 74 | static const wm_treeid WM_TREEID_NONE = {.gen = 0, .x = XCB_NONE}; |
| 75 | |
| 76 | static_assert(sizeof(wm_treeid) == 16, "wm_treeid size is not 16 bytes"); |
| 77 | static_assert(alignof(wm_treeid) == 8, "wm_treeid alignment is not 8 bytes"); |
| 78 | |
| 79 | struct wm_change { |
| 80 | enum wm_tree_change_type type; |
| 81 | /// The toplevel window this change is about. For |
| 82 | /// `WM_TREE_CHANGE_TOPLEVEL_KILLED`, this is the zombie window left in place of |
| 83 | /// the killed toplevel. For `WM_TREE_CHANGE_TOPLEVEL_RESTACKED`, this is NULL. |
| 84 | struct wm_ref *toplevel; |
| 85 | struct { |
| 86 | wm_treeid old; |
| 87 | wm_treeid new_; |
| 88 | } client; |
| 89 | }; |
| 90 | |
| 91 | /// Reference to a window in the `struct wm`. Most of wm operations operate on wm_refs. If |
| 92 | /// the referenced window is managed, a `struct window` can be retrieved by |
| 93 | /// `wm_ref_deref`. |
| 94 | struct wm_ref; |
| 95 | struct atom; |
| 96 | |
| 97 | static inline bool wm_treeid_eq(wm_treeid a, wm_treeid b) { |
| 98 | return a.gen == b.gen && a.x == b.x; |
| 99 | } |
| 100 | |
| 101 | /// Create a new window management object. |
| 102 | /// Caller is expected to first call `wm_import_start` with the root window after |
| 103 | /// creating the object. Otherwise many operations will fail or crash. |
| 104 | struct wm *wm_new(void); |
| 105 | void wm_free(struct wm *wm); |
| 106 | |
| 107 | /// The current focused toplevel, based on information from X server. |
| 108 | struct wm_ref *wm_focused_win(struct wm *wm); |
| 109 | /// The current cached focused leader. This is only update to date after a call to |
| 110 | /// `wm_refresh_leaders`, and before any changes to the wm tree. Otherwise this could |
| 111 | /// return a dangling pointer. |
| 112 | const struct wm_ref *wm_focused_leader(struct wm *wm); |
| 113 | |
| 114 | // Note: `wm` keeps track of 2 lists of windows. One is the window stack, which includes |
| 115 | // all windows that might need to be rendered, which means it would include destroyed |
| 116 | // windows in case they have close animation. This list is accessed by `wm_stack_*` series |
| 117 | // of functions. The other is a hash table of windows, which does not include destroyed |
| 118 | // windows. This list is accessed by `wm_find_*`, `wm_foreach`, and `wm_num_windows`. |
| 119 | // Adding a window to the window stack also automatically adds it to the hash table. |
| 120 | |
| 121 | /// Find a window in the hash table from window id. |
| 122 | struct wm_ref *attr_pure wm_find(const struct wm *wm, xcb_window_t id); |
| 123 | // Find the WM frame of a client window. `id` is the client window id. |
| 124 | struct wm_ref *attr_pure wm_find_by_client(const struct wm *wm, xcb_window_t client); |
| 125 | /// Find the toplevel of a window by going up the window tree. |
| 126 | struct wm_ref *attr_pure wm_ref_toplevel_of(const struct wm *wm, struct wm_ref *cursor); |
| 127 | /// Return the client window of a window. Must be called with a cursor to a toplevel. |
| 128 | /// Returns NULL if there is no client window. |
| 129 | struct wm_ref *attr_pure wm_ref_client_of(struct wm_ref *cursor); |
| 130 | /// Find the next window in the window stack. Returns NULL if `cursor` is the last window. |
| 131 | struct wm_ref *attr_pure wm_ref_below(const struct wm_ref *cursor); |
| 132 | struct wm_ref *attr_pure wm_ref_above(const struct wm_ref *cursor); |
| 133 | struct wm_ref *attr_pure wm_root_ref(const struct wm *wm); |
| 134 | |
| 135 | struct wm_ref *attr_pure wm_ref_topmost_child(const struct wm_ref *cursor); |
| 136 | struct wm_ref *attr_pure wm_ref_bottommost_child(const struct wm_ref *cursor); |
| 137 | |
| 138 | /// Move window `w` so it's right above `below`, if `below` is 0, `w` is moved |
| 139 | /// to the bottom of the stack |
| 140 | void wm_stack_move_to_above(struct wm *wm, struct wm_ref *cursor, xcb_window_t below); |
| 141 | /// Move window `w` to the top of the stack. |
| 142 | void wm_stack_move_to_end(struct wm *wm, struct wm_ref *cursor, bool to_bottom); |
| 143 | |
| 144 | struct win *attr_pure wm_ref_deref(const struct wm_ref *cursor); |
| 145 | xcb_window_t attr_pure wm_ref_win_id(const struct wm_ref *cursor); |
| 146 | wm_treeid attr_pure wm_ref_treeid(const struct wm_ref *cursor); |
| 147 | /// Assign a window to a cursor. The cursor must not already have a window assigned. |
| 148 | void wm_ref_set(struct wm_ref *cursor, struct win *w); |
| 149 | /// Mark `cursor` as the focused window. |
| 150 | void wm_ref_set_focused(struct wm *wm, struct wm_ref *cursor); |
| 151 | void wm_ref_set_leader(struct wm *wm, struct wm_ref *cursor, xcb_window_t leader); |
| 152 | /// Get the current cached group leader of a window. This is only update to date after a |
| 153 | /// call to `wm_refresh_leaders`. |
| 154 | const struct wm_ref *wm_ref_leader(const struct wm_ref *cursor); |
| 155 | bool attr_pure wm_ref_is_zombie(const struct wm_ref *cursor); |
| 156 | |
| 157 | /// Recalculate all cached leaders, and update `wm_focused_leader`. |
| 158 | void wm_refresh_leaders(struct wm *wm); |
| 159 | |
| 160 | /// Destroy a window. Children of this window should already have been destroyed. This |
| 161 | /// will cause a `WM_TREE_CHANGE_TOPLEVEL_KILLED` event to be generated, and a zombie |
| 162 | /// window to be placed where the window was. |
| 163 | void wm_destroy(struct wm *wm, xcb_window_t wid); |
| 164 | /// Remove a zombie window from the window tree. |
| 165 | void wm_reap_zombie(struct wm_ref *zombie); |
| 166 | void wm_reparent(struct wm *wm, struct x_connection *c, struct atom *atoms, |
| 167 | xcb_window_t wid, xcb_window_t parent); |
| 168 | /// Disconnect `child` from its `parent`. If `new_parent_known` is true, the new parent |
| 169 | /// is a fully imported window in our tree. Otherwise, the new parent is either unknown, |
| 170 | /// or in the process of being imported. |
| 171 | void wm_disconnect(struct wm *wm, xcb_window_t child, xcb_window_t parent, |
| 172 | xcb_window_t new_parent); |
| 173 | void wm_set_has_wm_state(struct wm *wm, struct wm_ref *cursor, bool has_wm_state); |
| 174 | |
| 175 | /// Start the import process for `wid`. |
| 176 | /// |
| 177 | /// This function sets up event masks for `wid`, and start an async query tree request on |
| 178 | /// it. When the query tree request is completed, `wm_handle_query_tree_reply` will be |
| 179 | /// called to actually insert the window into the window tree. |
| 180 | /// `wm_handle_query_tree_reply` will also in turn start the import process for all |
| 181 | /// children of `wid`. |
| 182 | /// |
| 183 | /// The reason for this two step process is because we want to catch all windows ever |
| 184 | /// created on X server's side. It's not enough to just set up event masks and wait for |
| 185 | /// events. Because at the point in time we set up the event mask, some child windows |
| 186 | /// could have already been created. It would have been nice if there is a way to listen |
| 187 | /// for events on the whole window tree, for all present and future windows. But alas, the |
| 188 | /// X11 protocol is lacking in this area. |
| 189 | /// |
| 190 | /// The best thing we can do is set up the event mask to catch all future events, and then |
| 191 | /// query the current state. But there are complications with this approach, too. Because |
| 192 | /// there is no way to atomically do these two things in one go, things could happen |
| 193 | /// between these two steps, for which we will receive events. Some of these are easy to |
| 194 | /// deal with, e.g. if a window is created, we will get an event for that, and later we |
| 195 | /// will see that window again in the query tree reply. These are easy to ignore. Some are |
| 196 | /// more complex. Because there could be some child windows we are not aware of. We could |
| 197 | /// get events for windows that we don't know about. We try our best to ignore those |
| 198 | /// events. |
| 199 | /// |
| 200 | /// Another problem with this is, the usual way we send a request then process the reply |
| 201 | /// is synchronous. i.e. with `xcb_<request>_reply(xcb_<request>(...))`. As previously |
| 202 | /// mentioned, we might receive events before the reply. And in this case we would process |
| 203 | /// the reply _before_ any of those events! This might be benign, but it is difficult to |
| 204 | /// audit all the possible cases and events to make sure this always work. One example is, |
| 205 | /// imagine a window A is being imported, and another window B, which is already in the |
| 206 | /// tree got reparented to A. We would think B appeared in two places if we processed |
| 207 | /// query tree reply before the reparent event. For that, we introduce the concept of |
| 208 | /// "async requests". Replies to these requests are received and processed like other X |
| 209 | /// events. With that, we don't need to worry about the order of events and replies. |
| 210 | /// |
| 211 | /// (Now you have a glimpse of how much X11 sucks.) |
| 212 | void wm_import_start(struct wm *wm, struct x_connection *c, struct atom *atoms, |
| 213 | xcb_window_t wid, struct wm_ref *parent); |
| 214 | |
| 215 | /// Check if there are tree change events |
| 216 | bool wm_has_tree_changes(const struct wm *wm); |
| 217 | |
| 218 | struct wm_change wm_dequeue_change(struct wm *wm); |
| 219 | |
| 220 | /// Whether the window tree should be in a consistent state. When the window tree is |
| 221 | /// consistent, we should not be receiving X events that refer to windows that we don't |
| 222 | /// know about. And we also should not have any orphaned windows in the tree. |
| 223 | bool wm_is_consistent(const struct wm *wm); |
| 224 | |
| 225 | // Unit testing helpers |
| 226 | |
| 227 | struct wm_ref *wm_new_mock_window(struct wm *wm, xcb_window_t wid); |
| 228 | void wm_free_mock_window(struct wm *wm, struct wm_ref *cursor); |