diff --git a/include/picom/backend.h b/include/picom/backend.h index 74e6f1e4af..e087894f1b 100644 --- a/include/picom/backend.h +++ b/include/picom/backend.h @@ -31,7 +31,7 @@ struct xvisual_info { }; typedef struct session session_t; -struct managed_win; +struct win; struct ev_loop; struct backend_operations; diff --git a/src/c2.c b/src/c2.c index a883a284f8..b8891b7006 100644 --- a/src/c2.c +++ b/src/c2.c @@ -314,7 +314,7 @@ static inline c2_ptr_t c2h_comb_tree(c2_b_op_t op, c2_ptr_t p1, c2_ptr_t p2) { static inline int c2h_b_opp(c2_b_op_t op) { switch (op) { case C2_B_OAND: return 2; - case C2_B_OOR: return 1; + case C2_B_OOR: case C2_B_OXOR: return 1; default: break; } @@ -441,8 +441,12 @@ TEST_CASE(c2_parse) { size_t len = c2_condition_to_str(cond->ptr, str, sizeof(str)); TEST_STREQUAL3(str, "name = \"xterm\"", len); - struct managed_win test_win = { + struct wm *wm = wm_new(); + wm_import_incomplete(wm, 1, XCB_NONE); + + struct win test_win = { .name = "xterm", + .tree_ref = wm_find(wm, 1), }; TEST_TRUE(c2_match(state, &test_win, cond, NULL)); c2_list_postprocess(state, NULL, cond); @@ -563,6 +567,8 @@ TEST_CASE(c2_parse) { len = c2_condition_to_str(cond->ptr, str, sizeof(str)); TEST_STREQUAL3(str, rule, len); c2_list_free(&cond, NULL); + + wm_free(wm); } #define c2_error(format, ...) \ @@ -1562,8 +1568,9 @@ static inline bool c2_int_op(const c2_l_t *leaf, int64_t target) { unreachable(); } -static bool c2_match_once_leaf_int(const struct managed_win *w, const c2_l_t *leaf) { - const xcb_window_t wid = (leaf->target_on_client ? w->client_win : w->base.id); +static bool c2_match_once_leaf_int(const struct win *w, const c2_l_t *leaf) { + auto const client_win = win_client_id(w, /*fallback_to_self=*/true); + const xcb_window_t wid = (leaf->target_on_client ? client_win : win_id(w)); // Get the value if (leaf->predef != C2_L_PUNDEFINED) { @@ -1586,7 +1593,7 @@ static bool c2_match_once_leaf_int(const struct managed_win *w, const c2_l_t *le case C2_L_PWMWIN: predef_target = win_is_wmwin(w); break; case C2_L_PBSHAPED: predef_target = w->bounding_shaped; break; case C2_L_PROUNDED: predef_target = w->rounded_corners; break; - case C2_L_PCLIENT: predef_target = w->client_win; break; + case C2_L_PCLIENT: predef_target = client_win; break; case C2_L_PLEADER: predef_target = w->leader; break; case C2_L_POVREDIR: // When user wants to check override-redirect, they almost always @@ -1594,9 +1601,8 @@ static bool c2_match_once_leaf_int(const struct managed_win *w, const c2_l_t *le // don't track the override-redirect state of the client window // directly, however we can assume if a window has a window // manager frame around it, it's not override-redirect. - predef_target = - w->a.override_redirect && (w->client_win == w->base.id || - w->client_win == XCB_WINDOW_NONE); + predef_target = w->a.override_redirect && + wm_ref_client_of(w->tree_ref) == NULL; break; default: unreachable(); } @@ -1608,7 +1614,7 @@ static bool c2_match_once_leaf_int(const struct managed_win *w, const c2_l_t *le assert(!values->needs_update); if (!values->valid) { log_verbose("Property %s not found on window %#010x (%s)", leaf->tgt, - w->client_win, w->name); + client_win, w->name); return false; } @@ -1670,8 +1676,8 @@ static bool c2_string_op(const c2_l_t *leaf, const char *target) { unreachable(); } -static bool c2_match_once_leaf_string(struct atom *atoms, const struct managed_win *w, - const c2_l_t *leaf) { +static bool +c2_match_once_leaf_string(struct atom *atoms, const struct win *w, const c2_l_t *leaf) { // A predefined target const char *predef_target = NULL; @@ -1696,8 +1702,8 @@ static bool c2_match_once_leaf_string(struct atom *atoms, const struct managed_w auto values = &w->c2_state.values[leaf->target_id]; assert(!values->needs_update); if (!values->valid) { - log_verbose("Property %s not found on window %#010x (%s)", leaf->tgt, - w->client_win, w->name); + log_verbose("Property %s not found on window %#010x, client %#010x (%s)", + leaf->tgt, win_id(w), win_client_id(w, false), w->name); return false; } @@ -1752,10 +1758,11 @@ static bool c2_match_once_leaf_string(struct atom *atoms, const struct managed_w * For internal use. */ static inline bool -c2_match_once_leaf(struct c2_state *state, const struct managed_win *w, const c2_l_t *leaf) { +c2_match_once_leaf(struct c2_state *state, const struct win *w, const c2_l_t *leaf) { assert(leaf); - const xcb_window_t wid = (leaf->target_on_client ? w->client_win : w->base.id); + const xcb_window_t wid = + (leaf->target_on_client ? win_client_id(w, /*fallback_to_self=*/true) : win_id(w)); // Return if wid is missing if (leaf->predef == C2_L_PUNDEFINED && !wid) { @@ -1790,8 +1797,7 @@ c2_match_once_leaf(struct c2_state *state, const struct managed_win *w, const c2 * * @return true if matched, false otherwise. */ -static bool -c2_match_once(struct c2_state *state, const struct managed_win *w, const c2_ptr_t cond) { +static bool c2_match_once(struct c2_state *state, const struct win *w, const c2_ptr_t cond) { bool result = false; if (cond.isbranch) { @@ -1802,8 +1808,8 @@ c2_match_once(struct c2_state *state, const struct managed_win *w, const c2_ptr_ return false; } - log_verbose("Matching window %#010x (%s) against condition %s", - w->base.id, w->name, c2_condition_to_str2(cond)); + log_verbose("Matching window %#010x (%s) against condition %s", win_id(w), + w->name, c2_condition_to_str2(cond)); switch (pb->op) { case C2_B_OAND: @@ -1821,7 +1827,7 @@ c2_match_once(struct c2_state *state, const struct managed_win *w, const c2_ptr_ default: unreachable(); } - log_debug("(%#010x): branch: result = %d, pattern = %s", w->base.id, + log_debug("(%#010x): branch: result = %d, pattern = %s", win_id(w), result, c2_condition_to_str2(cond)); } else { // A leaf @@ -1833,9 +1839,9 @@ c2_match_once(struct c2_state *state, const struct managed_win *w, const c2_ptr_ result = c2_match_once_leaf(state, w, pleaf); - log_debug("(%#010x): leaf: result = %d, client = %#010x, " - "pattern = %s", - w->base.id, result, w->client_win, c2_condition_to_str2(cond)); + log_debug("(%#010x): leaf: result = %d, client = %#010x, pattern = %s", + win_id(w), result, win_client_id(w, false), + c2_condition_to_str2(cond)); } // Postprocess the result @@ -1853,8 +1859,8 @@ c2_match_once(struct c2_state *state, const struct managed_win *w, const c2_ptr_ * @param pdata a place to return the data * @return true if matched, false otherwise. */ -bool c2_match(struct c2_state *state, const struct managed_win *w, - const c2_lptr_t *condlst, void **pdata) { +bool c2_match(struct c2_state *state, const struct win *w, const c2_lptr_t *condlst, + void **pdata) { // Then go through the whole linked list for (; condlst; condlst = condlst->next) { if (c2_match_once(state, w, condlst->ptr)) { @@ -1869,8 +1875,8 @@ bool c2_match(struct c2_state *state, const struct managed_win *w, } /// Match a window against the first condition in a condition linked list. -bool c2_match_one(struct c2_state *state, const struct managed_win *w, - const c2_lptr_t *condlst, void **pdata) { +bool c2_match_one(struct c2_state *state, const struct win *w, const c2_lptr_t *condlst, + void **pdata) { if (!condlst) { return false; } diff --git a/src/c2.h b/src/c2.h index 328a3c4c78..2dcd3e712d 100644 --- a/src/c2.h +++ b/src/c2.h @@ -18,7 +18,7 @@ struct c2_window_state { struct c2_property_value *values; }; struct atom; -struct managed_win; +struct win; typedef void (*c2_userdata_free)(void *); c2_lptr_t *c2_parse(c2_lptr_t **pcondlst, const char *pattern, void *data); @@ -50,10 +50,10 @@ void c2_window_state_update(struct c2_state *state, struct c2_window_state *wind xcb_connection_t *c, xcb_window_t client_win, xcb_window_t frame_win); -bool c2_match(struct c2_state *state, const struct managed_win *w, - const c2_lptr_t *condlst, void **pdata); -bool c2_match_one(struct c2_state *state, const struct managed_win *w, - const c2_lptr_t *condlst, void **pdata); +bool c2_match(struct c2_state *state, const struct win *w, const c2_lptr_t *condlst, + void **pdata); +bool c2_match_one(struct c2_state *state, const struct win *w, const c2_lptr_t *condlst, + void **pdata); bool c2_list_postprocess(struct c2_state *state, xcb_connection_t *c, c2_lptr_t *list); typedef bool (*c2_list_foreach_cb_t)(const c2_lptr_t *cond, void *data); diff --git a/src/dbus.c b/src/dbus.c index 4c09b08390..dac97f4227 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -20,7 +20,6 @@ #include "compiler.h" #include "config.h" #include "log.h" -#include "utils/list.h" #include "utils/misc.h" #include "utils/str.h" #include "wm/defs.h" @@ -464,24 +463,8 @@ static bool cdbus_append_string_variant(DBusMessage *msg, const char *data) { return true; } -static int cdbus_append_wids_callback(struct win *w, void *data) { - DBusMessageIter *iter = data; - cdbus_window_t wid = w->id; - if (!dbus_message_iter_append_basic(iter, CDBUS_TYPE_WINDOW, &wid)) { - return 1; - } - return 0; -} - /// Append all window IDs in the window list of a session to a D-Bus message static bool cdbus_append_wids(DBusMessage *msg, session_t *ps) { - // Get the number of wids we are to include - unsigned count = wm_num_windows(ps->wm); - if (!count) { - // Nothing to append - return true; - } - DBusMessageIter it, subit; dbus_message_iter_init_append(msg, &it); if (!dbus_message_iter_open_container(&it, DBUS_TYPE_ARRAY, @@ -490,12 +473,27 @@ static bool cdbus_append_wids(DBusMessage *msg, session_t *ps) { return false; } - auto result = wm_foreach(ps->wm, cdbus_append_wids_callback, &subit); + bool failed = false; + wm_stack_foreach(ps->wm, cursor) { + if (wm_ref_is_zombie(cursor)) { + continue; + } + auto w = wm_ref_deref(cursor); + if (w == NULL) { + continue; + } + + auto wid = win_id(w); + if (!dbus_message_iter_append_basic(&subit, CDBUS_TYPE_WINDOW, &wid)) { + failed = true; + break; + } + } if (!dbus_message_iter_close_container(&it, &subit)) { log_error("Failed to close container."); return false; } - if (result != 0) { + if (failed) { log_error("Failed to append argument."); return false; } @@ -581,14 +579,20 @@ cdbus_process_window_property_get(session_t *ps, DBusMessage *msg, cdbus_window_ return DBUS_HANDLER_RESULT_HANDLED; } - auto w = wm_find_managed(ps->wm, wid); - - if (!w) { + auto cursor = wm_find(ps->wm, wid); + if (cursor == NULL) { log_debug("Window %#010x not found.", wid); dbus_set_error(e, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid); return DBUS_HANDLER_RESULT_HANDLED; } + auto w = wm_ref_deref(cursor); + if (w == NULL) { + log_debug("Window %#010x is not managed.", wid); + dbus_set_error(e, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid); + return DBUS_HANDLER_RESULT_HANDLED; + } + #define append(tgt, type, expr) \ if (!strcmp(#tgt, target)) { \ if (!cdbus_append_##type(reply, expr)) { \ @@ -599,19 +603,18 @@ cdbus_process_window_property_get(session_t *ps, DBusMessage *msg, cdbus_window_ #define append_win_property(name, member, type) append(name, type, w->member) append(Mapped, bool_variant, w->state == WSTATE_MAPPED); - append(Id, wid_variant, w->base.id); + append(Id, wid_variant, win_id(w)); append(Type, string_variant, WINTYPES[w->window_type].name); append(RawFocused, bool_variant, win_is_focused_raw(w)); - append_win_property(ClientWin, client_win, wid_variant); + append(ClientWin, wid_variant, win_client_id(w, /*fallback_to_self=*/true)); append_win_property(Leader, leader, wid_variant); append_win_property(Name, name, string_variant); if (!strcmp("Next", target)) { cdbus_window_t next_id = 0; - if (!list_node_is_last(wm_stack_end(ps->wm), &w->base.stack_neighbour)) { - next_id = list_entry(w->base.stack_neighbour.next, struct win, - stack_neighbour) - ->id; + auto below = wm_ref_below(cursor); + if (below != NULL) { + next_id = wm_ref_win_id(below); } if (!cdbus_append_wid_variant(reply, next_id)) { return DBUS_HANDLER_RESULT_NEED_MEMORY; @@ -668,14 +671,20 @@ cdbus_process_win_get(session_t *ps, DBusMessage *msg, DBusMessage *reply, DBusE return DBUS_HANDLER_RESULT_HANDLED; } - auto w = wm_find_managed(ps->wm, wid); - - if (!w) { + auto cursor = wm_find(ps->wm, wid); + if (cursor == NULL) { log_debug("Window %#010x not found.", wid); dbus_set_error(err, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid); return DBUS_HANDLER_RESULT_HANDLED; } + auto w = wm_ref_deref(cursor); + if (w == NULL) { + log_debug("Window %#010x is not managed.", wid); + dbus_set_error(err, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid); + return DBUS_HANDLER_RESULT_HANDLED; + } + #define append(tgt, type, expr) \ if (strcmp(#tgt, target) == 0) { \ if (!cdbus_append_##type(reply, expr)) { \ @@ -686,18 +695,16 @@ cdbus_process_win_get(session_t *ps, DBusMessage *msg, DBusMessage *reply, DBusE #define append_win_property(tgt, type) append(tgt, type, w->tgt) if (!strcmp("next", target)) { - xcb_window_t next_id = - list_node_is_last(wm_stack_end(ps->wm), &w->base.stack_neighbour) - ? 0 - : list_entry(w->base.stack_neighbour.next, struct win, stack_neighbour) - ->id; + auto below = wm_ref_below(cursor); + xcb_window_t next_id = below ? wm_ref_win_id(below) : XCB_NONE; if (!cdbus_append_wid(reply, next_id)) { return DBUS_HANDLER_RESULT_NEED_MEMORY; } return DBUS_HANDLER_RESULT_HANDLED; } - append(id, boolean, w->base.id); + append(id, wid, win_id(w)); + append(client_win, wid, win_client_id(w, /*fallback_to_self=*/true)); append(map_state, boolean, w->a.map_state); append(wmwin, boolean, win_is_wmwin(w)); append(focused_raw, boolean, win_is_focused_raw(w)); @@ -708,7 +715,6 @@ cdbus_process_win_get(session_t *ps, DBusMessage *msg, DBusMessage *reply, DBusE append_win_property(mode, enum); append_win_property(opacity, double); - append_win_property(client_win, wid); append_win_property(ever_damaged, boolean); append_win_property(window_type, enum); append_win_property(leader, wid); @@ -753,13 +759,19 @@ cdbus_process_win_set(session_t *ps, DBusMessage *msg, DBusMessage *reply, DBusE return DBUS_HANDLER_RESULT_HANDLED; } - auto w = wm_find_managed(ps->wm, wid); - - if (!w) { + auto cursor = wm_find(ps->wm, wid); + if (cursor == NULL) { log_debug("Window %#010x not found.", wid); dbus_set_error(err, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid); return DBUS_HANDLER_RESULT_HANDLED; } + + auto w = wm_ref_deref(cursor); + if (w == NULL) { + log_debug("Window %#010x is not managed.", wid); + dbus_set_error(err, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid); + return DBUS_HANDLER_RESULT_HANDLED; + } cdbus_enum_t val = UNSET; if (!cdbus_msg_get_arg(msg, 2, CDBUS_TYPE_ENUM, &val)) { dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL); @@ -812,13 +824,13 @@ cdbus_process_find_win(session_t *ps, DBusMessage *msg, DBusMessage *reply, DBus } auto w = wm_find_by_client(ps->wm, client); if (w) { - wid = w->base.id; + wid = wm_ref_win_id(w); } } else if (!strcmp("focused", target)) { // Find focused window auto active_win = wm_active_win(ps->wm); if (active_win && active_win->state != WSTATE_UNMAPPED) { - wid = active_win->base.id; + wid = win_id(active_win); } } else { log_debug(CDBUS_ERROR_BADTGT_S, target); @@ -1072,21 +1084,6 @@ static DBusHandlerResult cdbus_process_introspect(DBusMessage *reply) { } ///@} -static int cdbus_process_windows_root_introspect_callback(struct win *w, void *data) { - char **introspect = data; - if (!w->managed) { - return 0; - } - char *tmp = NULL; - if (asprintf(&tmp, "\n", w->id) < 0) { - log_fatal("Failed to allocate memory."); - return 1; - } - mstrextend(introspect, tmp); - free(tmp); - return 0; -} - /** * Process an D-Bus Introspect request, for /windows. */ @@ -1109,8 +1106,18 @@ cdbus_process_windows_root_introspect(session_t *ps, DBusMessage *reply) { scoped_charp introspect = NULL; mstrextend(&introspect, str_introspect); - if (wm_foreach(ps->wm, cdbus_process_windows_root_introspect_callback, &introspect)) { - return DBUS_HANDLER_RESULT_NEED_MEMORY; + wm_stack_foreach(ps->wm, cursor) { + auto w = wm_ref_deref(cursor); + if (w == NULL) { + continue; + } + char *tmp = NULL; + if (asprintf(&tmp, "\n", win_id(w)) < 0) { + log_fatal("Failed to allocate memory."); + return DBUS_HANDLER_RESULT_NEED_MEMORY; + } + mstrextend(&introspect, tmp); + free(tmp); } mstrextend(&introspect, ""); if (!cdbus_append_string(reply, introspect)) { @@ -1424,41 +1431,41 @@ static bool cdbus_signal_wid(struct cdbus_data *cd, const char *interface, ///@{ void cdbus_ev_win_added(struct cdbus_data *cd, struct win *w) { if (cd->dbus_conn) { - cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_added", w->id); - cdbus_signal_wid(cd, PICOM_COMPOSITOR_INTERFACE, "WinAdded", w->id); + cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_added", win_id(w)); + cdbus_signal_wid(cd, PICOM_COMPOSITOR_INTERFACE, "WinAdded", win_id(w)); } } void cdbus_ev_win_destroyed(struct cdbus_data *cd, struct win *w) { if (cd->dbus_conn) { - cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_destroyed", w->id); - cdbus_signal_wid(cd, PICOM_COMPOSITOR_INTERFACE, "WinDestroyed", w->id); + cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_destroyed", win_id(w)); + cdbus_signal_wid(cd, PICOM_COMPOSITOR_INTERFACE, "WinDestroyed", win_id(w)); } } void cdbus_ev_win_mapped(struct cdbus_data *cd, struct win *w) { if (cd->dbus_conn) { - cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_mapped", w->id); - cdbus_signal_wid(cd, PICOM_COMPOSITOR_INTERFACE, "WinMapped", w->id); + cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_mapped", win_id(w)); + cdbus_signal_wid(cd, PICOM_COMPOSITOR_INTERFACE, "WinMapped", win_id(w)); } } void cdbus_ev_win_unmapped(struct cdbus_data *cd, struct win *w) { if (cd->dbus_conn) { - cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_unmapped", w->id); - cdbus_signal_wid(cd, PICOM_COMPOSITOR_INTERFACE, "WinUnmapped", w->id); + cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_unmapped", win_id(w)); + cdbus_signal_wid(cd, PICOM_COMPOSITOR_INTERFACE, "WinUnmapped", win_id(w)); } } void cdbus_ev_win_focusout(struct cdbus_data *cd, struct win *w) { if (cd->dbus_conn) { - cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_focusout", w->id); + cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_focusout", win_id(w)); } } void cdbus_ev_win_focusin(struct cdbus_data *cd, struct win *w) { if (cd->dbus_conn) { - cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_focusin", w->id); + cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_focusin", win_id(w)); } } //!@} diff --git a/src/event.c b/src/event.c index 1642156fa4..b55f7a1e2d 100644 --- a/src/event.c +++ b/src/event.c @@ -74,11 +74,12 @@ static inline const char *ev_window_name(session_t *ps, xcb_window_t wid) { } else if (ps->overlay == wid) { name = "(Overlay)"; } else { - auto w = wm_find_managed(ps->wm, wid); - if (!w) { - w = wm_find_by_client(ps->wm, wid); + auto cursor = wm_find(ps->wm, wid); + if (!cursor || !wm_ref_deref(cursor)) { + cursor = wm_find_by_client(ps->wm, wid); } + auto w = cursor ? wm_ref_deref(cursor) : NULL; if (w && w->name) { name = w->name; } @@ -203,151 +204,130 @@ static inline void ev_focus_out(session_t *ps, xcb_focus_out_event_t *ev) { } static inline void ev_create_notify(session_t *ps, xcb_create_notify_event_t *ev) { - if (ev->parent == ps->c.screen_info->root) { - wm_stack_add_top(ps->wm, ev->window); - ps->pending_updates = true; - return; - } - - auto w = wm_find_managed(ps->wm, ev->parent); - if (w == NULL) { - // The parent window is not a toplevel window, we don't care about it. - // This can happen if a toplevel is reparented somewhere, and more events - // were generated from it before we can unsubscribe. - return; - } - // A direct child of a toplevel, subscribe to property changes so we can - // know if WM_STATE is set on this window. - wm_subwin_add_and_subscribe(ps->wm, &ps->c, ev->window, ev->parent); - if (w->client_win == XCB_NONE || w->client_win == w->base.id) { - win_set_flags(w, WIN_FLAGS_CLIENT_STALE); - ps->pending_updates = true; - } + wm_import_incomplete(ps->wm, ev->window, ev->parent); } /// Handle configure event of a regular window static void configure_win(session_t *ps, xcb_configure_notify_event_t *ce) { - auto w = wm_find(ps->wm, ce->window); + auto cursor = wm_find(ps->wm, ce->window); + auto below = wm_find(ps->wm, ce->above_sibling); - if (!w) { + if (!cursor) { + log_error("Configure event received for unknown window %#010x", ce->window); return; } - wm_stack_move_above(ps->wm, w, ce->above_sibling); + if (below == NULL && ce->above_sibling != XCB_NONE) { + log_error("Configure event received for window %#010x, but its sibling " + "window %#010x is not in our tree. Expect malfunction.", + ce->window, ce->above_sibling); + } else if (below != NULL) { + wm_stack_move_to_above(ps->wm, cursor, below); + } else { + // above_sibling being XCB_NONE means the window is put at the bottom. + wm_stack_move_to_end(ps->wm, cursor, true); + } - if (!w->managed) { + auto w = wm_ref_deref(cursor); + if (!w) { return; } - auto mw = (struct managed_win *)w; - add_damage_from_win(ps, mw); + add_damage_from_win(ps, w); // We check against pending_g here, because there might have been multiple // configure notifies in this cycle, or the window could receive multiple updates // while it's unmapped. - bool position_changed = mw->pending_g.x != ce->x || mw->pending_g.y != ce->y; - bool size_changed = mw->pending_g.width != ce->width || - mw->pending_g.height != ce->height || - mw->pending_g.border_width != ce->border_width; + bool position_changed = w->pending_g.x != ce->x || w->pending_g.y != ce->y; + bool size_changed = w->pending_g.width != ce->width || + w->pending_g.height != ce->height || + w->pending_g.border_width != ce->border_width; if (position_changed || size_changed) { // Queue pending updates - win_set_flags(mw, WIN_FLAGS_FACTOR_CHANGED); + win_set_flags(w, WIN_FLAGS_FACTOR_CHANGED); // TODO(yshui) don't set pending_updates if the window is not // visible/mapped ps->pending_updates = true; // At least one of the following if's is true if (position_changed) { - log_trace("Window position changed, %dx%d -> %dx%d", mw->g.x, - mw->g.y, ce->x, ce->y); - mw->pending_g.x = ce->x; - mw->pending_g.y = ce->y; - win_set_flags(mw, WIN_FLAGS_POSITION_STALE); + log_trace("Window position changed, %dx%d -> %dx%d", w->g.x, + w->g.y, ce->x, ce->y); + w->pending_g.x = ce->x; + w->pending_g.y = ce->y; + win_set_flags(w, WIN_FLAGS_POSITION_STALE); } if (size_changed) { - log_trace("Window size changed, %dx%d -> %dx%d", mw->g.width, - mw->g.height, ce->width, ce->height); - mw->pending_g.width = ce->width; - mw->pending_g.height = ce->height; - mw->pending_g.border_width = ce->border_width; - win_set_flags(mw, WIN_FLAGS_SIZE_STALE); + log_trace("Window size changed, %dx%d -> %dx%d", w->g.width, + w->g.height, ce->width, ce->height); + w->pending_g.width = ce->width; + w->pending_g.height = ce->height; + w->pending_g.border_width = ce->border_width; + win_set_flags(w, WIN_FLAGS_SIZE_STALE); } // Recalculate which monitor this window is on - win_update_monitor(&ps->monitors, mw); + win_update_monitor(&ps->monitors, w); } // override_redirect flag cannot be changed after window creation, as far // as I know, so there's no point to re-match windows here. - mw->a.override_redirect = ce->override_redirect; + w->a.override_redirect = ce->override_redirect; } static inline void ev_configure_notify(session_t *ps, xcb_configure_notify_event_t *ev) { - log_debug("{ send_event: %d, id: %#010x, above: %#010x, override_redirect: %d }", + log_debug("{ event: %#010x, id: %#010x, above: %#010x, override_redirect: %d }", ev->event, ev->window, ev->above_sibling, ev->override_redirect); + + if (ps->overlay && ev->window == ps->overlay) { + return; + } + if (ev->window == ps->c.screen_info->root) { set_root_flags(ps, ROOT_FLAGS_CONFIGURED); - } else { + } else if (!wm_is_wid_masked(ps->wm, ev->event)) { configure_win(ps, ev); } } static inline void ev_destroy_notify(session_t *ps, xcb_destroy_notify_event_t *ev) { - auto subwin = wm_subwin_find(ps->wm, ev->window); - if (subwin) { - wm_subwin_remove(ps->wm, subwin); - } - - auto w = wm_find(ps->wm, ev->window); - auto mw = wm_find_by_client(ps->wm, ev->window); - if (mw && mw->client_win == mw->base.id) { - // We only want _real_ frame window - assert(&mw->base == w); - mw = NULL; - } - - // A window can't be a client window and a top-level window at the same time, - // so only one of `w` and `mw` can be non-NULL - assert(w == NULL || mw == NULL); - - if (w != NULL) { - destroy_win_start(ps, w); - if (!w->managed) { - // If the window wasn't managed, we can release it immediately - destroy_win_finish(ps, w); - } - return; - } - if (mw != NULL) { - win_unmark_client(mw); - win_set_flags(mw, WIN_FLAGS_CLIENT_STALE); - ps->pending_updates = true; - return; - } - log_debug("Received a destroy notify from an unknown window, %#010x", ev->window); + log_debug("{ event: %#010x, id: %#010x }", ev->event, ev->window); + wm_destroy(ps->wm, ev->window); } static inline void ev_map_notify(session_t *ps, xcb_map_notify_event_t *ev) { // Unmap overlay window if it got mapped but we are currently not // in redirected state. - if (ps->overlay && ev->window == ps->overlay && !ps->redirected) { - log_debug("Overlay is mapped while we are not redirected"); - auto e = xcb_request_check( - ps->c.c, xcb_unmap_window_checked(ps->c.c, ps->overlay)); - if (e) { - log_error("Failed to unmap the overlay window"); - free(e); + if (ps->overlay && ev->window == ps->overlay) { + if (!ps->redirected) { + log_debug("Overlay is mapped while we are not redirected"); + auto succeeded = + XCB_AWAIT_VOID(xcb_unmap_window, ps->c.c, ps->overlay); + if (!succeeded) { + log_error("Failed to unmap the overlay window"); + } } // We don't track the overlay window, so we can return return; } - auto w = wm_find_managed(ps->wm, ev->window); - if (!w) { + if (wm_is_wid_masked(ps->wm, ev->event)) { + return; + } + + auto cursor = wm_find(ps->wm, ev->window); + if (cursor == NULL) { + log_error("Map event received for unknown window %#010x, overlay is " + "%#010x", + ev->window, ps->overlay); return; } + auto w = wm_ref_deref(cursor); + if (w == NULL) { + return; + } win_set_flags(w, WIN_FLAGS_MAPPED); // We set `ever_damaged` to false here, instead of in `map_win_start`, // because we might receive damage events before that function is called @@ -363,142 +343,51 @@ static inline void ev_map_notify(session_t *ps, xcb_map_notify_event_t *ev) { } static inline void ev_unmap_notify(session_t *ps, xcb_unmap_notify_event_t *ev) { - auto w = wm_find_managed(ps->wm, ev->window); - if (w) { - unmap_win_start(w); - } -} - -static inline void ev_reparent_notify(session_t *ps, xcb_reparent_notify_event_t *ev) { - log_debug("Window %#010x has new parent: %#010x, override_redirect: %d", - ev->window, ev->parent, ev->override_redirect); - - auto old_toplevel = wm_find_by_client(ps->wm, ev->window); - auto old_w = wm_find(ps->wm, ev->window); - auto old_subwin = wm_subwin_find(ps->wm, ev->window); - auto new_toplevel = wm_find_managed(ps->wm, ev->parent); - xcb_window_t old_parent = XCB_NONE; - if (old_subwin) { - old_parent = old_subwin->toplevel; - } else if (old_w) { - old_parent = ps->c.screen_info->root; - } - if (old_toplevel && old_toplevel->client_win == old_toplevel->base.id) { - // We only want _real_ frame window, meaning old_toplevel must be - // a parent of `ev->window`. - old_toplevel = NULL; - } - // A window can't be a toplevel and a subwin at the same time - assert(old_w == NULL || old_subwin == NULL); - - log_debug("old toplevel: %p, old subwin: %p, new toplevel: %p, old parent: " - "%#010x, new parent: %#010x, root window: %#010x", - old_w, old_subwin, new_toplevel, old_parent, ev->parent, - ps->c.screen_info->root); - - if (old_w == NULL && old_subwin == NULL && new_toplevel == NULL && - ev->parent != ps->c.screen_info->root) { - // The window is neither a toplevel nor a subwin, and the new parent is - // neither a root nor a toplevel, we don't care about this window. - // This can happen if a window is reparented to somewhere irrelevant, but - // more events from it are generated before we can unsubscribe. + if (ps->overlay && ev->window == ps->overlay) { return; } - if (ev->parent == old_parent) { - // Parent unchanged, but if the parent is root, we need to move the window - // to the top of the window stack - if (old_w) { - // root -> root reparent, we just need to move it to the top - log_debug("Restack %#010x (%s) to top", old_w->id, - win_get_name_if_managed(old_w)); - wm_stack_move_to_top(ps->wm, old_w); - if (old_w->managed) { - add_damage_from_win(ps, win_as_managed(old_w)); - } - } + if (wm_is_wid_masked(ps->wm, ev->event)) { return; } - if (old_toplevel) { - assert(old_subwin != NULL); - assert(old_subwin->toplevel == old_toplevel->base.id); - win_unmark_client(old_toplevel); - win_set_flags(old_toplevel, WIN_FLAGS_CLIENT_STALE); - ps->pending_updates = true; + auto cursor = wm_find(ps->wm, ev->window); + if (cursor == NULL) { + log_error("Unmap event received for unknown window %#010x", ev->window); + return; } - - if (old_w) { - // A toplevel is reparented, so it is no longer a toplevel. We need to - // destroy the existing toplevel. - if (old_w->managed) { - auto mw = (struct managed_win *)old_w; - // Usually, damage for unmapped windows are added in - // `paint_preprocess`, when a window was painted before and isn't - // anymore. But since we are reparenting the window here, we would - // lose track of the `to_paint` information. So we just add damage - // here. - if (mw->to_paint) { - add_damage_from_win(ps, mw); - } - // Emulating what X server does: a destroyed - // window is always unmapped first. - if (mw->state == WSTATE_MAPPED) { - unmap_win_start(mw); - } - - // If an animation is running, the best we could do is stopping - // it. - free(mw->running_animation); - mw->running_animation = NULL; - } - destroy_win_start(ps, old_w); - destroy_win_finish(ps, old_w); - } - - // We need to guarantee a subwin exists iff it has a valid toplevel. - auto new_subwin = old_subwin; - if (new_subwin != NULL && new_toplevel == NULL) { - wm_subwin_remove_and_unsubscribe(ps->wm, &ps->c, new_subwin); - new_subwin = NULL; - } else if (new_subwin == NULL && new_toplevel != NULL) { - new_subwin = - wm_subwin_add_and_subscribe(ps->wm, &ps->c, ev->window, ev->parent); - } - if (new_subwin) { - new_subwin->toplevel = new_toplevel->base.id; - if (new_toplevel->client_win == XCB_NONE || - new_toplevel->client_win == new_toplevel->base.id) { - win_set_flags(new_toplevel, WIN_FLAGS_CLIENT_STALE); - ps->pending_updates = true; - } + auto w = wm_ref_deref(cursor); + if (w != NULL) { + unmap_win_start(w); } +} - if (ev->parent == ps->c.screen_info->root) { - // New parent is root, add a toplevel; - assert(old_w == NULL); - assert(new_toplevel == NULL); - wm_stack_add_top(ps->wm, ev->window); - ps->pending_updates = true; - } +static inline void ev_reparent_notify(session_t *ps, xcb_reparent_notify_event_t *ev) { + log_debug("Window %#010x has new parent: %#010x, override_redirect: %d, " + "send_event: %#010x", + ev->window, ev->parent, ev->override_redirect, ev->event); + wm_reparent(ps->wm, ev->window, ev->parent); } static inline void ev_circulate_notify(session_t *ps, xcb_circulate_notify_event_t *ev) { - auto w = wm_find(ps->wm, ev->window); - - if (!w) { + if (wm_is_wid_masked(ps->wm, ev->event)) { return; } - log_debug("Moving window %#010x (%s) to the %s", w->id, - win_get_name_if_managed(w), ev->place == PlaceOnTop ? "top" : "bottom"); - if (ev->place == PlaceOnTop) { - wm_stack_move_to_top(ps->wm, w); - } else { - wm_stack_move_to_bottom(ps->wm, w); + auto cursor = wm_find(ps->wm, ev->window); + + if (cursor == NULL) { + log_error("Circulate event received for unknown window %#010x", ev->window); + return; } - if (w->managed) { - add_damage_from_win(ps, win_as_managed(w)); + + log_debug("Moving window %#010x (%s) to the %s", ev->window, + ev_window_name(ps, ev->window), ev->place == PlaceOnTop ? "top" : "bottom"); + wm_stack_move_to_end(ps->wm, cursor, ev->place == XCB_PLACE_ON_BOTTOM); + + auto w = wm_ref_deref(cursor); + if (w != NULL) { + add_damage_from_win(ps, w); } } @@ -529,50 +418,6 @@ static inline void ev_expose(session_t *ps, xcb_expose_event_t *ev) { } } -static inline void ev_subwin_wm_state_changed(session_t *ps, xcb_property_notify_event_t *ev) { - auto subwin = wm_subwin_find(ps->wm, ev->window); - if (!subwin) { - // We only care if a direct child of a toplevel gained/lost WM_STATE - return; - } - - enum tristate old_has_wm_state = subwin->has_wm_state; - subwin->has_wm_state = ev->state == XCB_PROPERTY_DELETE ? TRI_FALSE : TRI_TRUE; - if (old_has_wm_state == subwin->has_wm_state) { - if (subwin->has_wm_state == TRI_FALSE) { - log_warn("Child window %#010x of window %#010x lost WM_STATE a " - "second time?", - ev->window, subwin->toplevel); - } - return; - } - - auto toplevel = wm_find(ps->wm, subwin->toplevel); - BUG_ON(toplevel == NULL); - if (!toplevel->managed) { - return; - } - - auto managed = (struct managed_win *)toplevel; - if (managed->client_win == subwin->id) { - // 1. This window is the client window of its toplevel, and now it lost - // its WM_STATE (implies it must have it before) - assert(subwin->has_wm_state == TRI_FALSE); - win_set_flags(managed, WIN_FLAGS_CLIENT_STALE); - } else if (subwin->has_wm_state == TRI_TRUE) { - // 2. This window is not the client window of its toplevel, and - // now it gained WM_STATE - if (managed->client_win != XCB_NONE && managed->client_win != toplevel->id) { - log_warn("Toplevel %#010x already has a client window %#010x, " - "but another of its child windows %#010x gained " - "WM_STATE.", - toplevel->id, managed->client_win, subwin->id); - } else { - win_set_flags(managed, WIN_FLAGS_CLIENT_STALE); - } - } -} - static inline void ev_property_notify(session_t *ps, xcb_property_notify_event_t *ev) { if (unlikely(log_get_level_tls() <= LOG_LEVEL_TRACE)) { // Print out changed atom @@ -604,10 +449,31 @@ static inline void ev_property_notify(session_t *ps, xcb_property_notify_event_t return; } + if (wm_is_wid_masked(ps->wm, ev->window)) { + return; + } + ps->pending_updates = true; - auto w = wm_find_by_client(ps->wm, ev->window); + auto cursor = wm_find(ps->wm, ev->window); + if (cursor == NULL) { + log_error("Property notify received for unknown window %#010x", ev->window); + return; + } + + auto toplevel_cursor = wm_ref_toplevel_of(cursor); if (ev->atom == ps->atoms->aWM_STATE) { - ev_subwin_wm_state_changed(ps, ev); + log_debug("WM_STATE changed for window %#010x (%s): %s", ev->window, + ev_window_name(ps, ev->window), + ev->state == XCB_PROPERTY_DELETE ? "deleted" : "set"); + wm_set_has_wm_state(ps->wm, cursor, ev->state != XCB_PROPERTY_DELETE); + } + + // We only care if the property is set on the toplevel itself, or on its + // client window if it has one. WM_STATE is an exception, it is handled + // always because it is what determines if a window is a client window. + auto client_cursor = wm_ref_client_of(toplevel_cursor) ?: toplevel_cursor; + if (cursor != client_cursor && cursor != toplevel_cursor) { + return; } if (ev->atom == ps->atoms->a_NET_WM_BYPASS_COMPOSITOR) { @@ -615,41 +481,34 @@ static inline void ev_property_notify(session_t *ps, xcb_property_notify_event_t queue_redraw(ps); } - if (w) { - win_set_property_stale(w, ev->atom); + auto toplevel = wm_ref_deref(toplevel_cursor); + if (toplevel) { + win_set_property_stale(toplevel, ev->atom); } - if (ev->atom == ps->atoms->a_NET_WM_WINDOW_OPACITY) { + if (ev->atom == ps->atoms->a_NET_WM_WINDOW_OPACITY && toplevel != NULL) { // We already handle if this is set on the client window, check // if this is set on the frame window as well. // TODO(yshui) do we really need this? - auto toplevel = wm_find_managed(ps->wm, ev->window); - if (toplevel) { - win_set_property_stale(toplevel, ev->atom); - } + win_set_property_stale(toplevel, ev->atom); } // Check for other atoms we are tracking if (c2_state_is_property_tracked(ps->c2_state, ev->atom)) { - bool change_is_on_client = false; - w = wm_find_managed(ps->wm, ev->window); - if (!w) { - w = wm_find_by_client(ps->wm, ev->window); - change_is_on_client = true; - } - if (w) { - c2_window_state_mark_dirty(ps->c2_state, &w->c2_state, ev->atom, - change_is_on_client); + bool change_is_on_client = cursor == client_cursor; + if (toplevel) { + c2_window_state_mark_dirty(ps->c2_state, &toplevel->c2_state, + ev->atom, change_is_on_client); // Set FACTOR_CHANGED so rules based on properties will be // re-evaluated. // Don't need to set property stale here, since that only // concerns properties we explicitly check. - win_set_flags(w, WIN_FLAGS_FACTOR_CHANGED); + win_set_flags(toplevel, WIN_FLAGS_FACTOR_CHANGED); } } } -static inline void repair_win(session_t *ps, struct managed_win *w) { +static inline void repair_win(session_t *ps, struct win *w) { // Only mapped window can receive damages assert(w->state == WSTATE_MAPPED || win_check_flags_all(w, WIN_FLAGS_MAPPED)); @@ -674,8 +533,8 @@ static inline void repair_win(session_t *ps, struct managed_win *w) { free(e); } win_extents(w, &parts); - log_debug("Window %#010x (%s) has been damaged the first time", - w->base.id, w->name); + log_debug("Window %#010x (%s) has been damaged the first time", win_id(w), + w->name); } else { auto cookie = xcb_damage_subtract(ps->c.c, w->damage, XCB_NONE, ps->damage_ring.x_region); @@ -687,7 +546,7 @@ static inline void repair_win(session_t *ps, struct managed_win *w) { w->g.y + w->g.border_width); } - log_trace("Mark window %#010x (%s) as having received damage", w->base.id, w->name); + log_trace("Mark window %#010x (%s) as having received damage", win_id(w), w->name); w->ever_damaged = true; w->pixmap_damaged = true; @@ -714,24 +573,29 @@ static inline void repair_win(session_t *ps, struct managed_win *w) { } static inline void ev_damage_notify(session_t *ps, xcb_damage_notify_event_t *de) { - /* - if (ps->root == de->drawable) { - root_damaged(); - return; - } */ - - auto w = wm_find_managed(ps->wm, de->drawable); + auto cursor = wm_find(ps->wm, de->drawable); - if (!w) { + if (cursor == NULL) { + log_error("Damage notify received for unknown window %#010x", de->drawable); return; } - repair_win(ps, w); + auto w = wm_ref_deref(cursor); + if (w != NULL) { + repair_win(ps, w); + } } static inline void ev_shape_notify(session_t *ps, xcb_shape_notify_event_t *ev) { - auto w = wm_find_managed(ps->wm, ev->affected_window); - if (!w || w->a.map_state == XCB_MAP_STATE_UNMAPPED) { + auto cursor = wm_find(ps->wm, ev->affected_window); + if (cursor == NULL) { + log_error("Shape notify received for unknown window %#010x", + ev->affected_window); + return; + } + + auto w = wm_ref_deref(cursor); + if (w == NULL || w->a.map_state == XCB_MAP_STATE_UNMAPPED) { return; } diff --git a/src/inspect.c b/src/inspect.c index 860da38b5d..4e8d357991 100644 --- a/src/inspect.c +++ b/src/inspect.c @@ -23,22 +23,30 @@ #include "wm/win.h" #include "x.h" -static struct managed_win * +static struct win * setup_window(struct x_connection *c, struct atom *atoms, struct options *options, - struct c2_state *state, xcb_window_t target) { + struct wm *wm, struct c2_state *state, xcb_window_t target) { // Pretend we are the compositor, and build up the window state - struct managed_win *w = ccalloc(1, struct managed_win); + auto cursor = wm_find(wm, target); + if (cursor == NULL) { + log_fatal("Could not find window %#010x", target); + wm_free(wm); + return NULL; + } + + auto toplevel = wm_ref_toplevel_of(cursor); + struct win *w = ccalloc(1, struct win); w->state = WSTATE_MAPPED; - w->base.id = target; - w->client_win = win_get_client_window(c, NULL, atoms, w); + w->tree_ref = toplevel; win_update_wintype(c, atoms, w); - win_update_frame_extents(c, atoms, w, w->client_win, options->frame_opacity); + win_update_frame_extents(c, atoms, w, win_client_id(w, /*fallback_to_self=*/true), + options->frame_opacity); // TODO(yshui) get leader win_update_name(c, atoms, w); win_update_class(c, atoms, w); win_update_role(c, atoms, w); - auto geometry_reply = XCB_AWAIT(xcb_get_geometry, c->c, w->base.id); + auto geometry_reply = XCB_AWAIT(xcb_get_geometry, c->c, win_id(w)); w->g = (struct win_geometry){ .x = geometry_reply->x, .y = geometry_reply->y, @@ -68,17 +76,18 @@ setup_window(struct x_connection *c, struct atom *atoms, struct options *options free(reply); } } - if (wid == w->base.id || wid == w->client_win) { + if (wid == win_id(w) || wid == win_client_id(w, /*fallback_to_self=*/false)) { w->focused = true; } - auto attributes_reply = XCB_AWAIT(xcb_get_window_attributes, c->c, w->base.id); + auto attributes_reply = XCB_AWAIT(xcb_get_window_attributes, c->c, win_id(w)); w->a = *attributes_reply; w->pictfmt = x_get_pictform_for_visual(c, w->a.visual); free(attributes_reply); c2_window_state_init(state, &w->c2_state); - c2_window_state_update(state, &w->c2_state, c->c, w->client_win, w->base.id); + c2_window_state_update(state, &w->c2_state, c->c, + win_client_id(w, /*fallback_to_self=*/true), win_id(w)); return w; } @@ -140,7 +149,7 @@ xcb_window_t select_window(struct x_connection *c) { struct c2_match_state { struct c2_state *state; - struct managed_win *w; + struct win *w; bool print_value; }; @@ -188,9 +197,14 @@ int inspect_main(int argc, char **argv, const char *config_file) { auto state = c2_state_new(atoms); options_postprocess_c2_lists(state, &c, &options); + struct wm *wm = wm_new(); + + wm_import_incomplete(wm, c.screen_info->root, XCB_NONE); + wm_complete_import(wm, &c, atoms); + auto target = select_window(&c); log_info("Target window: %#x", target); - auto w = setup_window(&c, atoms, &options, state, target); + auto w = setup_window(&c, atoms, &options, wm, state, target); struct c2_match_state match_state = { .state = state, .w = w, @@ -252,6 +266,8 @@ int inspect_main(int argc, char **argv, const char *config_file) { c2_window_state_destroy(state, &w->c2_state); free(w); + wm_free(wm); + log_deinit_tls(); c2_state_free(state); destroy_atoms(atoms); diff --git a/src/opengl.c b/src/opengl.c index e80b209984..a1b9b4266d 100644 --- a/src/opengl.c +++ b/src/opengl.c @@ -233,8 +233,11 @@ void glx_destroy(session_t *ps) { } // Free all GLX resources of windows - win_stack_foreach_managed(w, wm_stack_end(ps->wm)) { - free_win_res_glx(ps, w); + wm_stack_foreach(ps->wm, cursor) { + auto w = wm_ref_deref(cursor); + if (w != NULL) { + free_win_res_glx(ps, w); + } } // Free GLSL shaders/programs @@ -1160,9 +1163,9 @@ void glx_read_border_pixel(int root_height, int root_width, int x, int y, int wi gl_check_err(); } -bool glx_round_corners_dst(session_t *ps, struct managed_win *w, - const glx_texture_t *ptex, int dx, int dy, int width, - int height, float z, float cr, const region_t *reg_tgt) { +bool glx_round_corners_dst(session_t *ps, struct win *w, const glx_texture_t *ptex, + int dx, int dy, int width, int height, float z, float cr, + const region_t *reg_tgt) { assert(ps->psglx->round_passes->prog); bool ret = false; @@ -1533,7 +1536,7 @@ bool glx_render(session_t *ps, const glx_texture_t *ptex, int x, int y, int dx, /** * Free GLX part of win. */ -void free_win_res_glx(session_t *ps, struct managed_win *w) { +void free_win_res_glx(session_t *ps, struct win *w) { free_paint_glx(ps, &w->paint); free_paint_glx(ps, &w->shadow_paint); free_glx_bc(ps, &w->glx_blur_cache); diff --git a/src/opengl.h b/src/opengl.h index a10d960d42..60e1fa32b3 100644 --- a/src/opengl.h +++ b/src/opengl.h @@ -122,9 +122,9 @@ void glx_set_clip(session_t *ps, const region_t *reg); bool glx_blur_dst(session_t *ps, int dx, int dy, int width, int height, float z, GLfloat factor_center, const region_t *reg_tgt, glx_blur_cache_t *pbc); -bool glx_round_corners_dst(session_t *ps, struct managed_win *w, - const glx_texture_t *ptex, int dx, int dy, int width, - int height, float z, float cr, const region_t *reg_tgt); +bool glx_round_corners_dst(session_t *ps, struct win *w, const glx_texture_t *ptex, + int dx, int dy, int width, int height, float z, float cr, + const region_t *reg_tgt); GLuint glx_create_shader(GLenum shader_type, const char *shader_str); @@ -224,4 +224,4 @@ static inline void free_paint_glx(session_t *ps, paint_t *ppaint) { /** * Free GLX part of win. */ -void free_win_res_glx(session_t *ps, struct managed_win *w); +void free_win_res_glx(session_t *ps, struct win *w); diff --git a/src/picom.c b/src/picom.c index ebd62d377c..b1f94b6d47 100644 --- a/src/picom.c +++ b/src/picom.c @@ -460,7 +460,8 @@ void update_ewmh_active_win(session_t *ps) { // Search for the window xcb_window_t wid = wid_get_prop_window(&ps->c, ps->c.screen_info->root, ps->atoms->a_NET_ACTIVE_WINDOW); - auto w = wm_find_by_client(ps->wm, wid); + auto cursor = wm_find_by_client(ps->wm, wid); + auto w = cursor ? wm_ref_deref(cursor) : NULL; // Mark the window focused. No need to unfocus the previous one. if (w) { @@ -501,36 +502,20 @@ static void recheck_focus(session_t *ps) { return; } - // Trace upwards until we reach the toplevel containing the focus window. - while (true) { - auto tree = xcb_query_tree_reply(ps->c.c, xcb_query_tree(ps->c.c, wid), &e); - if (tree == NULL) { - // xcb_query_tree probably fails if you run picom when X is - // somehow initializing (like add it in .xinitrc). In this case - // just leave it alone. - log_error_x_error(e, "Failed to query window tree."); - free(e); - return; - } - - auto parent = tree->parent; - free(tree); - - if (parent == ps->c.screen_info->root) { - break; - } - wid = parent; + auto cursor = wm_find(ps->wm, wid); + if (cursor == NULL) { + log_error("Window %#010x not found in window tree.", wid); + return; } - auto w = wm_find_managed(ps->wm, wid); + cursor = wm_ref_toplevel_of(cursor); // And we set the focus state here + auto w = wm_ref_deref(cursor); if (w) { log_debug("%#010" PRIx32 " (%#010" PRIx32 " \"%s\") focused.", wid, - w->base.id, w->name); + win_id(w), w->name); win_set_focused(ps, w); - } else { - log_warn("Focus window %#010" PRIx32 " not found.", wid); } } @@ -543,7 +528,11 @@ static void rebuild_screen_reg(session_t *ps) { /// Free up all the images and deinit the backend static void destroy_backend(session_t *ps) { - win_stack_foreach_managed_safe(w, wm_stack_end(ps->wm)) { + wm_stack_foreach_safe(ps->wm, cursor, next_cursor) { + auto w = wm_ref_deref(cursor); + if (w == NULL) { + continue; + } // An unmapped window shouldn't have a pixmap, unless it has animation // running. (`w->previous.state != w->state` means there might be // animation but we haven't had a chance to start it because @@ -569,7 +558,7 @@ static void destroy_backend(session_t *ps) { free_paint(ps, &w->paint); if (w->state == WSTATE_DESTROYED) { - destroy_win_finish(ps, &w->base); + win_destroy_finish(ps, w); } } @@ -640,20 +629,6 @@ static bool initialize_blur(session_t *ps) { return ps->backend_blur_context != NULL; } -static int mark_pixmap_stale(struct win *w, void *data) { - struct session *ps = data; - if (!w->managed) { - return 0; - } - auto mw = win_as_managed(w); - assert(mw->state != WSTATE_DESTROYED); - // We need to reacquire image - log_debug("Marking window %#010x (%s) for update after redirection", w->id, mw->name); - win_set_flags(mw, WIN_FLAGS_PIXMAP_STALE); - ps->pending_updates = true; - return 0; -} - /// Init the backend and bind all the window pixmap to backend images static bool initialize_backend(session_t *ps) { if (!ps->o.use_legacy_backends) { @@ -701,9 +676,18 @@ static bool initialize_backend(session_t *ps) { } } - // wm_stack shouldn't include window that's not iterated by wm_foreach at - // this moment. Since there cannot be any fading windows. - wm_foreach(ps->wm, mark_pixmap_stale, ps); + wm_stack_foreach(ps->wm, cursor) { + auto w = wm_ref_deref(cursor); + if (w != NULL) { + assert(w->state != WSTATE_DESTROYED); + // We need to reacquire image + log_debug("Marking window %#010x (%s) for update after " + "redirection", + win_id(w), w->name); + win_set_flags(w, WIN_FLAGS_PIXMAP_STALE); + ps->pending_updates = true; + } + } ps->renderer = renderer_new(ps->backend_data, ps->o.shadow_radius, (struct color){.alpha = ps->o.shadow_opacity, .red = ps->o.shadow_red, @@ -725,6 +709,18 @@ static bool initialize_backend(session_t *ps) { return false; } +static inline void invalidate_reg_ignore(session_t *ps) { + // Invalidate reg_ignore from the top + wm_stack_foreach(ps->wm, cursor) { + auto top_w = wm_ref_deref(cursor); + if (top_w != NULL) { + rc_region_unref(&top_w->reg_ignore); + top_w->reg_ignore_valid = false; + break; + } + } +} + /// Handle configure event of the root window static void configure_root(session_t *ps) { // TODO(yshui) re-initializing backend should be done outside of the @@ -760,19 +756,16 @@ static void configure_root(session_t *ps) { free(r); rebuild_screen_reg(ps); - - // Invalidate reg_ignore from the top - auto top_w = wm_stack_next_managed(ps->wm, wm_stack_end(ps->wm)); - if (top_w) { - rc_region_unref(&top_w->reg_ignore); - top_w->reg_ignore_valid = false; - } + invalidate_reg_ignore(ps); // Whether a window is fullscreen depends on the new screen // size. So we need to refresh the fullscreen state of all // windows. - win_stack_foreach_managed(w, wm_stack_end(ps->wm)) { - win_update_is_fullscreen(ps, w); + wm_stack_foreach(ps->wm, cursor) { + auto w = wm_ref_deref(cursor); + if (w != NULL) { + win_update_is_fullscreen(ps, w); + } } if (ps->redirected) { @@ -829,17 +822,22 @@ static void handle_root_flags(session_t *ps) { * * @return whether the operation succeeded */ -static bool paint_preprocess(session_t *ps, bool *animation, struct managed_win **out_bottom) { +static bool paint_preprocess(session_t *ps, bool *animation, struct win **out_bottom) { // XXX need better, more general name for `fade_running`. It really // means if fade is still ongoing after the current frame is rendered - struct managed_win *bottom = NULL; + struct win *bottom = NULL; *animation = false; *out_bottom = NULL; // First, let's process fading, and animated shaders // TODO(yshui) check if a window is fully obscured, and if we don't need to // process fading or animation for it. - win_stack_foreach_managed_safe(w, wm_stack_end(ps->wm)) { + wm_stack_foreach_safe(ps->wm, cursor, tmp) { + auto w = wm_ref_deref(cursor); + if (w == NULL) { + continue; + } + const winmode_t mode_old = w->mode; const bool was_painted = w->to_paint; @@ -887,8 +885,13 @@ static bool paint_preprocess(session_t *ps, bool *animation, struct managed_win // Track whether it's the highest window to paint bool is_highest = true; bool reg_ignore_valid = true; - win_stack_foreach_managed_safe(w, wm_stack_end(ps->wm)) { + wm_stack_foreach_safe(ps->wm, cursor, next_cursor) { __label__ skip_window; + auto w = wm_ref_deref(cursor); + if (w == NULL) { + continue; + } + bool to_paint = true; // w->to_paint remembers whether this window is painted last time const bool was_painted = w->to_paint; @@ -902,7 +905,7 @@ static bool paint_preprocess(session_t *ps, bool *animation, struct managed_win // log_trace("%d %d %s", w->a.map_state, w->ever_damaged, w->name); log_trace("Checking whether window %#010x (%s) should be painted", - w->base.id, w->name); + win_id(w), w->name); // Give up if it's not damaged or invisible, or it's unmapped and its // pixmap is gone (for example due to a ConfigureNotify), or when it's @@ -912,8 +915,8 @@ static bool paint_preprocess(session_t *ps, bool *animation, struct managed_win log_trace("|- is unmapped"); to_paint = false; } else if (unlikely(ps->debug_window != XCB_NONE) && - (w->base.id == ps->debug_window || - w->client_win == ps->debug_window)) { + (win_id(w) == ps->debug_window || + win_client_id(w, /*fallback_to_self=*/false) == ps->debug_window)) { log_trace("|- is the debug window"); to_paint = false; } else if (!w->ever_damaged) { @@ -954,7 +957,7 @@ static bool paint_preprocess(session_t *ps, bool *animation, struct managed_win } log_trace("|- will be painted"); - log_verbose("Window %#010x (%s) will be painted", w->base.id, w->name); + log_verbose("Window %#010x (%s) will be painted", win_id(w), w->name); // Generate ignore region for painting to reduce GPU load if (!w->reg_ignore) { @@ -1007,11 +1010,6 @@ static bool paint_preprocess(session_t *ps, bool *animation, struct managed_win } w->prev_trans = bottom; - if (bottom) { - w->stacking_rank = bottom->stacking_rank + 1; - } else { - w->stacking_rank = 0; - } bottom = w; // If the screen is not redirected and the window has redir_ignore set, @@ -1027,7 +1025,7 @@ static bool paint_preprocess(session_t *ps, bool *animation, struct managed_win if (w->state == WSTATE_DESTROYED && w->running_animation == NULL) { // the window should be destroyed because it was destroyed // by X server and now its animations are finished - destroy_win_finish(ps, &w->base); + win_destroy_finish(ps, w); w = NULL; } @@ -1572,42 +1570,71 @@ static void handle_queued_x_events(EV_P attr_unused, ev_prepare *w, int revents } static void handle_new_windows(session_t *ps) { - list_foreach_safe(struct win, w, wm_stack_end(ps->wm), stack_neighbour) { - if (w->is_new) { - auto new_w = maybe_allocate_managed_win(ps, w); - if (new_w == w) { - continue; - } - - assert(new_w->managed); - wm_stack_replace(ps->wm, w, new_w); + wm_complete_import(ps->wm, &ps->c, ps->atoms); - auto mw = (struct managed_win *)new_w; - if (mw->a.map_state == XCB_MAP_STATE_VIEWABLE) { - win_set_flags(mw, WIN_FLAGS_MAPPED); - - // This window might be damaged before we called fill_win - // and created the damage handle. And there is no way for - // us to find out. So just blindly mark it damaged - mw->ever_damaged = true; + // Check tree changes first, because later property updates need accurate + // client window information + struct win *w = NULL; + while (true) { + auto wm_change = wm_dequeue_change(ps->wm); + if (wm_change.type == WM_TREE_CHANGE_NONE) { + break; + } + switch (wm_change.type) { + case WM_TREE_CHANGE_TOPLEVEL_NEW: + w = win_maybe_allocate(ps, wm_change.toplevel); + if (w != NULL && w->a.map_state == XCB_MAP_STATE_VIEWABLE) { + win_map_start(w); } - // Send D-Bus signal - if (ps->o.dbus) { - cdbus_ev_win_added(session_get_cdbus(ps), new_w); + break; + case WM_TREE_CHANGE_TOPLEVEL_KILLED: + w = wm_ref_deref(wm_change.toplevel); + if (w != NULL) { + // Pointing the window tree_ref to the zombie. + w->tree_ref = wm_change.toplevel; + win_destroy_start(ps, w); + } else { + // This window is not managed, no point keeping the zombie + // around. + wm_reap_zombie(wm_change.toplevel); + } + break; + case WM_TREE_CHANGE_CLIENT: + log_debug("Client window for window %#010x changed from " + "%#010x to %#010x", + wm_ref_win_id(wm_change.toplevel), + wm_change.client.old.x, wm_change.client.new_.x); + w = wm_ref_deref(wm_change.toplevel); + if (w != NULL) { + win_set_flags(w, WIN_FLAGS_CLIENT_STALE); + } else { + log_debug("An unmanaged window %#010x has a new client " + "%#010x", + wm_ref_win_id(wm_change.toplevel), + wm_change.client.new_.x); } + break; + case WM_TREE_CHANGE_TOPLEVEL_RESTACKED: invalidate_reg_ignore(ps); break; + default: unreachable(); } } } static void refresh_windows(session_t *ps) { - win_stack_foreach_managed(w, wm_stack_end(ps->wm)) { - win_process_update_flags(ps, w); + wm_stack_foreach(ps->wm, cursor) { + auto w = wm_ref_deref(cursor); + if (w != NULL) { + win_process_update_flags(ps, w); + } } } static void refresh_images(session_t *ps) { - win_stack_foreach_managed(w, wm_stack_end(ps->wm)) { - win_process_image_flags(ps, w); + wm_stack_foreach(ps->wm, cursor) { + auto w = wm_ref_deref(cursor); + if (w != NULL) { + win_process_image_flags(ps, w); + } } } @@ -1633,7 +1660,8 @@ static void handle_pending_updates(EV_P_ struct session *ps, double delta_t) { // Catching up with X server handle_queued_x_events(EV_A, &ps->event_check, 0); - if (ps->pending_updates) { + if (ps->pending_updates || wm_has_incomplete_imports(ps->wm) || + wm_has_tree_changes(ps->wm)) { log_debug("Delayed handling of events, entering critical section"); // Process new windows, and maybe allocate struct managed_win for them handle_new_windows(ps); @@ -1663,17 +1691,19 @@ static void handle_pending_updates(EV_P_ struct session *ps, double delta_t) { ps->pending_updates = false; log_trace("Exited critical section"); - win_stack_foreach_managed_safe(w, wm_stack_end(ps->wm)) { + wm_stack_foreach_safe(ps->wm, cursor, tmp) { + auto w = wm_ref_deref(cursor); + BUG_ON(w != NULL && w->tree_ref != cursor); // Window might be freed by this function, if it's destroyed and its // animation finished - if (win_process_animation_and_state_change(ps, w, delta_t)) { + if (w != NULL && win_process_animation_and_state_change(ps, w, delta_t)) { free(w->running_animation); w->running_animation = NULL; w->in_openclose = false; if (w->state == WSTATE_UNMAPPED) { unmap_win_finish(ps, w); } else if (w->state == WSTATE_DESTROYED) { - destroy_win_finish(ps, &w->base); + win_destroy_finish(ps, w); } } } @@ -1738,23 +1768,34 @@ static void draw_callback_impl(EV_P_ session_t *ps, int revents attr_unused) { // // TODO(yshui) I think maybe we don't need this anymore, since now we // immediate acquire pixmap right after `map_win_start`. - win_stack_foreach_managed_safe(w, wm_stack_end(ps->wm)) { + wm_stack_foreach_safe(ps->wm, cursor, tmp) { + auto w = wm_ref_deref(cursor); + if (w == NULL) { + continue; + } free(w->running_animation); w->running_animation = NULL; if (w->state == WSTATE_DESTROYED) { - destroy_win_finish(ps, &w->base); + win_destroy_finish(ps, w); } } } if (ps->o.benchmark) { if (ps->o.benchmark_wid) { - auto w = wm_find_managed(ps->wm, ps->o.benchmark_wid); - if (!w) { + auto w = wm_find(ps->wm, ps->o.benchmark_wid); + if (w == NULL) { log_fatal("Couldn't find specified benchmark window."); exit(1); } - add_damage_from_win(ps, w); + w = wm_ref_toplevel_of(w); + + auto win = wm_ref_deref(w); + if (win != NULL) { + add_damage_from_win(ps, win); + } else { + force_repaint(ps); + } } else { force_repaint(ps); } @@ -1765,7 +1806,7 @@ static void draw_callback_impl(EV_P_ session_t *ps, int revents attr_unused) { * screen should be redirected. */ bool animation = false; bool was_redirected = ps->redirected; - struct managed_win *bottom = NULL; + struct win *bottom = NULL; if (!paint_preprocess(ps, &animation, &bottom)) { log_fatal("Pre-render preparation has failed, exiting..."); exit(1); @@ -2473,6 +2514,9 @@ static session_t *session_init(int argc, char **argv, Display *dpy, #endif } + ps->wm = wm_new(); + wm_import_incomplete(ps->wm, ps->c.screen_info->root, XCB_NONE); + e = xcb_request_check(ps->c.c, xcb_grab_server_checked(ps->c.c)); if (e) { log_fatal_x_error(e, "Failed to grab X server"); @@ -2488,8 +2532,7 @@ static session_t *session_init(int argc, char **argv, Display *dpy, // think there still could be race condition that mandates discarding the events. x_discard_events(&ps->c); - xcb_query_tree_reply_t *query_tree_reply = xcb_query_tree_reply( - ps->c.c, xcb_query_tree(ps->c.c, ps->c.screen_info->root), NULL); + wm_complete_import(ps->wm, &ps->c, ps->atoms); e = xcb_request_check(ps->c.c, xcb_ungrab_server_checked(ps->c.c)); if (e) { @@ -2500,23 +2543,9 @@ static session_t *session_init(int argc, char **argv, Display *dpy, ps->server_grabbed = false; - ps->wm = wm_new(); - if (query_tree_reply) { - xcb_window_t *children; - int nchildren; - - children = xcb_query_tree_children(query_tree_reply); - nchildren = xcb_query_tree_children_length(query_tree_reply); - - for (int i = 0; i < nchildren; i++) { - wm_stack_add_above(ps->wm, children[i], i ? children[i - 1] : XCB_NONE); - } - free(query_tree_reply); - } - log_debug("Initial stack:"); - list_foreach(struct win, w, wm_stack_end(ps->wm), stack_neighbour) { - log_debug("%#010x", w->id); + wm_stack_foreach(ps->wm, i) { + log_debug(" %#010x", wm_ref_win_id(i)); } ps->command_builder = command_builder_new(); @@ -2569,8 +2598,11 @@ static void session_destroy(session_t *ps) { } #endif - win_stack_foreach_managed_safe(w, wm_stack_end(ps->wm)) { - free_win_res(ps, w); + wm_stack_foreach(ps->wm, cursor) { + auto w = wm_ref_deref(cursor); + if (w != NULL) { + free_win_res(ps, w); + } } // Free blacklists @@ -2677,7 +2709,7 @@ static void session_destroy(session_t *ps) { ev_signal_stop(ps->loop, &ps->usr1_signal); ev_signal_stop(ps->loop, &ps->int_signal); - wm_free(ps->wm, &ps->c); + wm_free(ps->wm); free_x_connection(&ps->c); } diff --git a/src/render.c b/src/render.c index e2719ccf19..990ed7c459 100644 --- a/src/render.c +++ b/src/render.c @@ -345,7 +345,7 @@ void render(session_t *ps, int x, int y, int dx, int dy, int wid, int hei, int f } static inline void -paint_region(session_t *ps, const struct managed_win *w, int x, int y, int wid, int hei, +paint_region(session_t *ps, const struct win *w, int x, int y, int wid, int hei, double opacity, const region_t *reg_paint, xcb_render_picture_t pict) { const int dx = (w ? w->g.x : 0) + x; const int dy = (w ? w->g.y : 0) + y; @@ -392,19 +392,19 @@ static inline bool paint_isvalid(session_t *ps, const paint_t *ppaint) { /** * Paint a window itself and dim it if asked. */ -void paint_one(session_t *ps, struct managed_win *w, const region_t *reg_paint) { +void paint_one(session_t *ps, struct win *w, const region_t *reg_paint) { // Fetch Pixmap if (!w->paint.pixmap) { w->paint.pixmap = x_new_id(&ps->c); set_ignore_cookie(&ps->c, xcb_composite_name_window_pixmap( - ps->c.c, w->base.id, w->paint.pixmap)); + ps->c.c, win_id(w), w->paint.pixmap)); } xcb_drawable_t draw = w->paint.pixmap; if (!draw) { log_error("Failed to get pixmap from window %#010x (%s), window won't be " "visible", - w->base.id, w->name); + win_id(w), w->name); return; } @@ -424,12 +424,12 @@ void paint_one(session_t *ps, struct managed_win *w, const region_t *reg_paint) // causing the jittering issue M4he reported in #7. if (!paint_bind_tex(ps, &w->paint, 0, 0, false, 0, w->a.visual, (!ps->o.glx_no_rebind_pixmap && w->pixmap_damaged))) { - log_error("Failed to bind texture for window %#010x.", w->base.id); + log_error("Failed to bind texture for window %#010x.", win_id(w)); } w->pixmap_damaged = false; if (!paint_isvalid(ps, &w->paint)) { - log_error("Window %#010x is missing painting data.", w->base.id); + log_error("Window %#010x is missing painting data.", win_id(w)); return; } @@ -681,7 +681,7 @@ static void paint_root(session_t *ps, const region_t *reg_paint) { /** * Generate shadow Picture for a window. */ -static bool win_build_shadow(session_t *ps, struct managed_win *w, double opacity) { +static bool win_build_shadow(session_t *ps, struct win *w, double opacity) { const int width = w->widthb; const int height = w->heightb; // log_trace("(): building shadow for %s %d %d", w->name, width, height); @@ -761,13 +761,12 @@ static bool win_build_shadow(session_t *ps, struct managed_win *w, double opacit /** * Paint the shadow of a window. */ -static inline void -win_paint_shadow(session_t *ps, struct managed_win *w, region_t *reg_paint) { +static inline void win_paint_shadow(session_t *ps, struct win *w, region_t *reg_paint) { // Bind shadow pixmap to GLX texture if needed paint_bind_tex(ps, &w->shadow_paint, 0, 0, false, 32, 0, false); if (!paint_isvalid(ps, &w->shadow_paint)) { - log_error("Window %#010x is missing shadow data.", w->base.id); + log_error("Window %#010x is missing shadow data.", win_id(w)); return; } @@ -897,7 +896,7 @@ xr_blur_dst(session_t *ps, xcb_render_picture_t tgt_buffer, int16_t x, int16_t y * Blur the background of a window. */ static inline void -win_blur_background(session_t *ps, struct managed_win *w, xcb_render_picture_t tgt_buffer, +win_blur_background(session_t *ps, struct win *w, xcb_render_picture_t tgt_buffer, const region_t *reg_paint) { const int16_t x = w->g.x; const int16_t y = w->g.y; @@ -1001,7 +1000,7 @@ win_blur_background(session_t *ps, struct managed_win *w, xcb_render_picture_t t /// paint all windows /// region = ?? /// region_real = the damage region -void paint_all(session_t *ps, struct managed_win *t) { +void paint_all(session_t *ps, struct win *t) { if (ps->o.xrender_sync_fence || (ps->drivers & DRIVER_NVIDIA)) { if (ps->xsync_exists && !x_fence_sync(&ps->c, ps->sync_fence)) { log_error("x_fence_sync failed, xrender-sync-fence will be " diff --git a/src/render.h b/src/render.h index 62258f0edb..69564f848a 100644 --- a/src/render.h +++ b/src/render.h @@ -14,7 +14,7 @@ typedef struct _glx_texture glx_texture_t; typedef struct glx_prog_main glx_prog_main_t; typedef struct session session_t; -struct managed_win; +struct win; typedef struct paint { xcb_pixmap_t pixmap; @@ -35,9 +35,9 @@ void render(session_t *ps, int x, int y, int dx, int dy, int w, int h, int fullw int fullh, double opacity, bool argb, bool neg, int cr, xcb_render_picture_t pict, glx_texture_t *ptex, const region_t *reg_paint, const glx_prog_main_t *pprogram, clip_t *clip); -void paint_one(session_t *ps, struct managed_win *w, const region_t *reg_paint); +void paint_one(session_t *ps, struct win *w, const region_t *reg_paint); -void paint_all(session_t *ps, struct managed_win *const t); +void paint_all(session_t *ps, struct win *const t); void free_paint(session_t *ps, paint_t *ppaint); void free_root_tile(session_t *ps); diff --git a/src/renderer/command_builder.c b/src/renderer/command_builder.c index b1fe0d3ba8..ae3250c453 100644 --- a/src/renderer/command_builder.c +++ b/src/renderer/command_builder.c @@ -110,7 +110,7 @@ command_for_shadow(struct layer *layer, struct backend_command *cmd, layer->shadow.origin.x, layer->shadow.origin.y, (unsigned)shadow_size_scaled.width, (unsigned)shadow_size_scaled.height); - log_trace("Calculate shadow for %#010x (%s)", w->base.id, w->name); + log_trace("Calculate shadow for %#010x (%s)", win_id(w), w->name); log_region(TRACE, &cmd->target_mask); if (!wintype_options[w->window_type].full_shadow) { // We need to not draw under the window diff --git a/src/renderer/damage.c b/src/renderer/damage.c index e154685cd4..e5986a6888 100644 --- a/src/renderer/damage.c +++ b/src/renderer/damage.c @@ -8,14 +8,6 @@ #include "damage.h" -static inline bool attr_unused layer_key_eq(const struct layer_key *a, - const struct layer_key *b) { - if (!a->generation || !b->generation) { - return false; - } - return a->window == b->window && a->generation == b->generation; -} - /// Compare two layers that contain the same window, return if they are the "same". Same /// means these two layers are render in the same way at the same position, with the only /// possible differences being the contents inside the window. @@ -164,7 +156,7 @@ void layout_manager_damage(struct layout_manager *lm, unsigned buffer_age, auto layout = layout_manager_layout(lm, l); dynarr_foreach(layout->layers, layer) { log_trace("\t%#010x %dx%d+%dx%d (prev %d, next %d)", - layer->key.window, layer->window.size.width, + layer->key.x, layer->window.size.width, layer->window.size.height, layer->window.origin.x, layer->window.origin.y, layer->prev_rank, layer->next_rank); @@ -265,9 +257,9 @@ void layout_manager_damage(struct layout_manager *lm, unsigned buffer_age, break; } - assert(layer_key_eq(&past_layer->key, &curr_layer->key)); - log_trace("%#010x == %#010x %s", past_layer->key.window, - curr_layer->key.window, curr_layer->win->name); + assert(wm_treeid_eq(past_layer->key, curr_layer->key)); + log_trace("%#010x == %#010x %s", past_layer->key.x, curr_layer->key.x, + curr_layer->win->name); if (!layer_compare(past_layer, past_layer_cmd, curr_layer, curr_layer_cmd)) { region_union_render_layer(damage, curr_layer, curr_layer_cmd); diff --git a/src/renderer/layout.c b/src/renderer/layout.c index 7ee8567df5..f40953129e 100644 --- a/src/renderer/layout.c +++ b/src/renderer/layout.c @@ -18,7 +18,7 @@ #include "layout.h" struct layer_index { UT_hash_handle hh; - struct layer_key key; + wm_treeid key; unsigned index; struct list_node free_list; }; @@ -39,7 +39,7 @@ struct layout_manager { /// Compute layout of a layer from a window. Returns false if the window is not /// visible / should not be rendered. `out_layer` is modified either way. -static bool layer_from_window(struct layer *out_layer, struct managed_win *w, ivec2 size) { +static bool layer_from_window(struct layer *out_layer, struct win *w, ivec2 size) { bool to_paint = false; if (!w->ever_damaged || w->paint_excluded) { goto out; @@ -108,8 +108,7 @@ static bool layer_from_window(struct layer *out_layer, struct managed_win *w, iv out_layer->is_clipping = w->transparent_clipping; out_layer->next_rank = -1; out_layer->prev_rank = -1; - out_layer->key = - (struct layer_key){.window = w->base.id, .generation = w->base.generation}; + out_layer->key = wm_ref_treeid(w->tree_ref); out_layer->win = w; to_paint = true; @@ -181,20 +180,17 @@ void layout_manager_append_layout(struct layout_manager *lm, struct wm *wm, auto layout = &lm->layouts[lm->current]; command_builder_command_list_free(layout->commands); layout->root_image_generation = root_pixmap_generation; - - unsigned nlayers = wm_stack_num_managed_windows(wm); - dynarr_resize(layout->layers, nlayers, layer_init, layer_deinit); layout->size = size; unsigned rank = 0; struct layer_index *index, *next_index; - for (struct list_node *cursor = wm_stack_end(wm)->prev; - cursor != wm_stack_end(wm); cursor = cursor->prev) { - auto w = list_entry(cursor, struct win, stack_neighbour); - if (!w->managed) { + wm_stack_foreach_rev(wm, cursor) { + auto w = wm_ref_deref(cursor); + if (w == NULL) { continue; } - if (!layer_from_window(&layout->layers[rank], (struct managed_win *)w, size)) { + dynarr_resize(layout->layers, rank + 1, layer_init, layer_deinit); + if (!layer_from_window(&layout->layers[rank], (struct win *)w, size)) { continue; } @@ -205,9 +201,8 @@ void layout_manager_append_layout(struct layout_manager *lm, struct wm *wm, layout->layers[rank].prev_rank = (int)index->index; } rank++; - assert(rank <= nlayers); } - dynarr_resize(layout->layers, rank, layer_init, layer_deinit); + dynarr_truncate(layout->layers, rank, layer_deinit); // Update indices. If a layer exist in both prev_layout and current layout, // we could update the index using next_rank; if a layer no longer exist in diff --git a/src/renderer/layout.h b/src/renderer/layout.h index 25e60503f6..448ffd7b1b 100644 --- a/src/renderer/layout.h +++ b/src/renderer/layout.h @@ -9,26 +9,15 @@ #include #include "region.h" - -struct layer_key { - /// Window generation, (see `struct wm::generation` for explanation of what a - /// generation is) - uint64_t generation; - /// Window ID - xcb_window_t window; - uint32_t pad; // explicit padding because this will be used as hash table - // key -}; - -static_assert(sizeof(struct layer_key) == 16, "layer_key has implicit padding"); +#include "wm/wm.h" /// A layer to be rendered in a render layout struct layer { /// Window that will be rendered in this layer - struct layer_key key; + wm_treeid key; /// The window, this is only valid for the current layout. Once /// a frame has passed, windows could have been freed. - struct managed_win *win; + struct win *win; /// Damaged region of this layer, in screen coordinates region_t damaged; /// Window rectangle in screen coordinates, before it's scaled. diff --git a/src/renderer/renderer.c b/src/renderer/renderer.c index 54dfa44069..445e35b9ca 100644 --- a/src/renderer/renderer.c +++ b/src/renderer/renderer.c @@ -182,7 +182,7 @@ renderer_set_root_size(struct renderer *r, struct backend_base *backend, ivec2 r } static bool -renderer_bind_mask(struct renderer *r, struct backend_base *backend, struct managed_win *w) { +renderer_bind_mask(struct renderer *r, struct backend_base *backend, struct win *w) { ivec2 size = {.width = w->widthb, .height = w->heightb}; bool succeeded = false; auto image = backend->ops.new_image(backend, BACKEND_IMAGE_FORMAT_MASK, size); @@ -346,8 +346,8 @@ image_handle renderer_shadow_from_mask(struct renderer *r, struct backend_base * return shadow_image; } -static bool renderer_bind_shadow(struct renderer *r, struct backend_base *backend, - struct managed_win *w) { +static bool +renderer_bind_shadow(struct renderer *r, struct backend_base *backend, struct win *w) { if (backend->ops.quirks(backend) & BACKEND_QUIRK_SLOW_BLUR) { xcb_pixmap_t shadow = XCB_NONE; xcb_render_picture_t pict = XCB_NONE; @@ -398,7 +398,7 @@ static bool renderer_prepare_commands(struct renderer *r, struct backend_base *b assert(layer->number_of_commands > 0); layer_end = cmd + layer->number_of_commands; log_trace("Prepare commands for layer %#010x @ %#010x (%s)", - layer->win->base.id, layer->win->client_win, + win_id(layer->win), win_client_id(layer->win, false), layer->win->name); } @@ -522,8 +522,8 @@ bool renderer_render(struct renderer *r, struct backend_base *backend, layer += 1; layer_end += layer->number_of_commands; log_trace("Layer for window %#010x @ %#010x (%s)", - layer->win->base.id, layer->win->client_win, - layer->win->name); + win_id(layer->win), + win_client_id(layer->win, false), layer->win->name); } log_backend_command(TRACE, *i); } diff --git a/src/utils/dynarr.h b/src/utils/dynarr.h index 62150d5878..4c9f700512 100644 --- a/src/utils/dynarr.h +++ b/src/utils/dynarr.h @@ -170,8 +170,8 @@ static inline void dynarr_remove_swap_impl(size_t size, void *arr, size_t idx) { #define dynarr_foreach_rev(arr, i) \ for (typeof(arr)(i) = dynarr_end(arr) - 1; (i) >= (arr); (i)--) -/// Find the index of an element in the array by using trivial comparison, returns -1 if -/// not found. +/// Find the index of the first appearance of an element in the array by using trivial +/// comparison, returns -1 if not found. #define dynarr_find_pod(arr, needle) \ ({ \ ptrdiff_t dynarr_find_ret = -1; \ diff --git a/src/utils/misc.h b/src/utils/misc.h index 92750fcf94..1769bdc5cb 100644 --- a/src/utils/misc.h +++ b/src/utils/misc.h @@ -64,6 +64,8 @@ safe_isinf(double a) { abort(); \ } \ } while (0) +/// Abort the program if `expr` is NULL. This is NOT disabled in release builds. +#define BUG_ON_NULL(expr) BUG_ON((expr) == NULL); #define CHECK_EXPR(...) ((void)0) /// Same as assert, but evaluates the expression even in release builds #define CHECK(expr) \ diff --git a/src/wm/meson.build b/src/wm/meson.build index 7813e193e4..1fb66fa707 100644 --- a/src/wm/meson.build +++ b/src/wm/meson.build @@ -1 +1 @@ -srcs += [ files('win.c', 'wm.c') ] +srcs += [ files('win.c', 'wm.c', 'tree.c') ] diff --git a/src/wm/tree.c b/src/wm/tree.c new file mode 100644 index 0000000000..3c4c493234 --- /dev/null +++ b/src/wm/tree.c @@ -0,0 +1,518 @@ +// SPDX-License-Identifier: MPL-2.0 +// Copyright (c) Yuxuan Shui + +/// In my ideal world, the compositor shouldn't be concerned with the X window tree. It +/// should only need to care about the toplevel windows. However, because we support +/// window rules based on window properties, which can be set on any descendant of a +/// toplevel, we need to keep track of the entire window tree. +/// +/// For descendants of a toplevel window, what we actually care about is what's called a +/// "client" window. A client window is a window with the `WM_STATE` property set, in +/// theory and descendants of a toplevel can gain/lose this property at any time. So we +/// setup a minimal structure for every single window to keep track of this. And once +/// a window becomes a client window, it will have our full attention and have all of its +/// information stored in the toplevel `struct managed_win`. + +#include +#include + +#include "log.h" +#include "utils/list.h" +#include "utils/misc.h" + +#include "wm.h" +#include "wm_internal.h" + +struct wm_tree_change_list { + struct wm_tree_change item; + struct list_node siblings; +}; + +void wm_tree_reap_zombie(struct wm_tree_node *zombie) { + BUG_ON(!zombie->is_zombie); + list_remove(&zombie->siblings); + free(zombie); +} + +static void wm_tree_enqueue_change(struct wm_tree *tree, struct wm_tree_change change) { + if (change.type == WM_TREE_CHANGE_TOPLEVEL_KILLED) { + // A gone toplevel will cancel out a previous + // `WM_TREE_CHANGE_TOPLEVEL_NEW` change in the queue. + bool found = false; + list_foreach_safe(struct wm_tree_change_list, i, &tree->changes, siblings) { + if (i->item.type == WM_TREE_CHANGE_TOPLEVEL_NEW && + wm_treeid_eq(i->item.toplevel, change.toplevel)) { + list_remove(&i->siblings); + list_insert_after(&tree->free_changes, &i->siblings); + found = true; + } else if (wm_treeid_eq(i->item.toplevel, change.toplevel) && found) { + // We also need to delete all other changes related to + // this toplevel in between the new and gone changes. + list_remove(&i->siblings); + list_insert_after(&tree->free_changes, &i->siblings); + } + } + if (found) { + wm_tree_reap_zombie(change.killed); + return; + } + } else if (change.type == WM_TREE_CHANGE_CLIENT) { + // A client change can coalesce with a previous client change. + list_foreach_safe(struct wm_tree_change_list, i, &tree->changes, siblings) { + if (!wm_treeid_eq(i->item.toplevel, change.toplevel) || + i->item.type != WM_TREE_CHANGE_CLIENT) { + continue; + } + + if (!wm_treeid_eq(i->item.client.new_, change.client.old)) { + log_warn("Inconsistent client change for toplevel " + "%#010x. Missing changes from %#010x to %#010x. " + "Possible bug.", + change.toplevel.x, i->item.client.new_.x, + change.client.old.x); + } + + i->item.client.new_ = change.client.new_; + if (wm_treeid_eq(i->item.client.old, change.client.new_)) { + list_remove(&i->siblings); + list_insert_after(&tree->free_changes, &i->siblings); + } + return; + } + } else if (change.type == WM_TREE_CHANGE_TOPLEVEL_RESTACKED) { + list_foreach(struct wm_tree_change_list, i, &tree->changes, siblings) { + if (i->item.type == WM_TREE_CHANGE_TOPLEVEL_RESTACKED || + i->item.type == WM_TREE_CHANGE_TOPLEVEL_NEW || + i->item.type == WM_TREE_CHANGE_TOPLEVEL_KILLED) { + // Only need to keep one + // `WM_TREE_CHANGE_TOPLEVEL_RESTACKED` change, and order + // doesn't matter. + return; + } + } + } + + // We don't let a `WM_TREE_CHANGE_TOPLEVEL_NEW` cancel out a previous + // `WM_TREE_CHANGE_TOPLEVEL_GONE`, because the new toplevel would be a different + // window reusing the same ID. So we need to go through the proper destruction + // process for the previous toplevel. Changes are not commutative (naturally). + + struct wm_tree_change_list *change_list; + if (!list_is_empty(&tree->free_changes)) { + change_list = list_entry(tree->free_changes.next, + struct wm_tree_change_list, siblings); + list_remove(&change_list->siblings); + } else { + change_list = cmalloc(struct wm_tree_change_list); + } + + change_list->item = change; + list_insert_before(&tree->changes, &change_list->siblings); +} + +/// Dequeue the oldest change from the change queue. If the queue is empty, a change with +/// `toplevel` set to `XCB_NONE` will be returned. +struct wm_tree_change wm_tree_dequeue_change(struct wm_tree *tree) { + if (list_is_empty(&tree->changes)) { + return (struct wm_tree_change){.type = WM_TREE_CHANGE_NONE}; + } + + auto change = list_entry(tree->changes.next, struct wm_tree_change_list, siblings); + list_remove(&change->siblings); + list_insert_after(&tree->free_changes, &change->siblings); + return change->item; +} + +/// Return the next node in the subtree after `node` in a pre-order traversal. Returns +/// NULL if `node` is the last node in the traversal. +struct wm_tree_node *wm_tree_next(struct wm_tree_node *node, struct wm_tree_node *subroot) { + if (!list_is_empty(&node->children)) { + // Descend if there are children + return list_entry(node->children.next, struct wm_tree_node, siblings); + } + + while (node != subroot && node->siblings.next == &node->parent->children) { + // If the current node has no more children, go back to the + // parent. + node = node->parent; + } + if (node == subroot) { + // We've gone past the topmost node for our search, stop. + return NULL; + } + return list_entry(node->siblings.next, struct wm_tree_node, siblings); +} + +/// Find a client window under a toplevel window. If there are multiple windows with +/// `WM_STATE` set under the toplevel window, we will return an arbitrary one. +static struct wm_tree_node *attr_pure wm_tree_find_client(struct wm_tree_node *subroot) { + if (subroot->has_wm_state) { + log_debug("Toplevel %#010x has WM_STATE set, weird. Using itself as its " + "client window.", + subroot->id.x); + return subroot; + } + + BUG_ON(subroot->parent == NULL); // Trying to find client window on the + // root window + + for (auto curr = subroot; curr != NULL; curr = wm_tree_next(curr, subroot)) { + if (curr->has_wm_state) { + return curr; + } + } + + return NULL; +} + +struct wm_tree_node *wm_tree_find(const struct wm_tree *tree, xcb_window_t id) { + struct wm_tree_node *node = NULL; + HASH_FIND_INT(tree->nodes, &id, node); + return node; +} + +struct wm_tree_node *wm_tree_find_toplevel_for(struct wm_tree_node *node) { + BUG_ON_NULL(node); + BUG_ON_NULL(node->parent); // Trying to find toplevel for the root + // window + + struct wm_tree_node *toplevel; + for (auto curr = node; curr->parent != NULL; curr = curr->parent) { + toplevel = curr; + } + return toplevel; +} + +/// Change whether a tree node has the `WM_STATE` property set. +/// `destroyed` indicate whether `node` is about to be destroyed, in which case, the `old` +/// field of the change event will be set to NULL. +void wm_tree_set_wm_state(struct wm_tree *tree, struct wm_tree_node *node, bool has_wm_state) { + BUG_ON(node == NULL); + + if (node->has_wm_state == has_wm_state) { + log_debug("WM_STATE unchanged call (window %#010x, WM_STATE %d).", + node->id.x, has_wm_state); + return; + } + + node->has_wm_state = has_wm_state; + BUG_ON(node->parent == NULL); // Trying to set WM_STATE on the root window + + struct wm_tree_node *toplevel; + for (auto cur = node; cur->parent != NULL; cur = cur->parent) { + toplevel = cur; + } + + if (toplevel == node) { + log_debug("Setting WM_STATE on a toplevel window %#010x, weird.", node->id.x); + return; + } + + struct wm_tree_change change = { + .toplevel = toplevel->id, + .type = WM_TREE_CHANGE_CLIENT, + .client = {.toplevel = toplevel}, + }; + if (!has_wm_state && toplevel->client_window == node) { + auto new_client = wm_tree_find_client(toplevel); + toplevel->client_window = new_client; + change.client.old = node->id; + change.client.new_ = new_client != NULL ? new_client->id : WM_TREEID_NONE; + wm_tree_enqueue_change(tree, change); + } else if (has_wm_state && toplevel->client_window == NULL) { + toplevel->client_window = node; + change.client.old = WM_TREEID_NONE; + change.client.new_ = node->id; + wm_tree_enqueue_change(tree, change); + } else if (has_wm_state) { + // If the toplevel window already has a client window, we won't + // try to usurp it. + log_debug("Toplevel window %#010x already has a client window " + "%#010x, ignoring new client window %#010x. I don't " + "like your window manager.", + toplevel->id.x, toplevel->client_window->id.x, node->id.x); + } +} + +struct wm_tree_node * +wm_tree_new_window(struct wm_tree *tree, xcb_window_t id, struct wm_tree_node *parent) { + auto node = ccalloc(1, struct wm_tree_node); + node->id.x = id; + node->id.gen = tree->gen++; + node->has_wm_state = false; + list_init_head(&node->children); + + BUG_ON(parent == NULL && tree->nodes != NULL); // Trying to create a second + // root window + HASH_ADD_INT(tree->nodes, id.x, node); + + node->parent = parent; + if (parent != NULL) { + list_insert_after(&parent->children, &node->siblings); + if (parent->parent == NULL) { + // Parent is root, this is a new toplevel window + wm_tree_enqueue_change(tree, (struct wm_tree_change){ + .toplevel = node->id, + .type = WM_TREE_CHANGE_TOPLEVEL_NEW, + .new_ = node, + }); + } + } + return node; +} + +static void +wm_tree_refresh_client_and_queue_change(struct wm_tree *tree, struct wm_tree_node *toplevel) { + BUG_ON_NULL(toplevel); + BUG_ON_NULL(toplevel->parent); + BUG_ON(toplevel->parent->parent != NULL); + auto new_client = wm_tree_find_client(toplevel); + if (new_client != toplevel->client_window) { + struct wm_tree_change change = {.toplevel = toplevel->id, + .type = WM_TREE_CHANGE_CLIENT, + .client = {.toplevel = toplevel, + .old = WM_TREEID_NONE, + .new_ = WM_TREEID_NONE}}; + if (toplevel->client_window != NULL) { + change.client.old = toplevel->client_window->id; + } + if (new_client != NULL) { + change.client.new_ = new_client->id; + } + toplevel->client_window = new_client; + wm_tree_enqueue_change(tree, change); + } +} + +void wm_tree_detach(struct wm_tree *tree, struct wm_tree_node *subroot) { + BUG_ON(subroot == NULL); + BUG_ON(subroot->parent == NULL); // Trying to detach the root window?! + + auto toplevel = wm_tree_find_toplevel_for(subroot); + if (toplevel != subroot) { + list_remove(&subroot->siblings); + wm_tree_refresh_client_and_queue_change(tree, toplevel); + } else { + // Detached a toplevel, create a zombie for it + auto zombie = ccalloc(1, struct wm_tree_node); + zombie->parent = subroot->parent; + zombie->id = subroot->id; + zombie->is_zombie = true; + zombie->win = subroot->win; + list_init_head(&zombie->children); + list_replace(&subroot->siblings, &zombie->siblings); + wm_tree_enqueue_change(tree, (struct wm_tree_change){ + .toplevel = subroot->id, + .type = WM_TREE_CHANGE_TOPLEVEL_KILLED, + .killed = zombie, + }); + } +} + +void wm_tree_destroy_window(struct wm_tree *tree, struct wm_tree_node *node) { + BUG_ON(node == NULL); + BUG_ON(node->parent == NULL); // Trying to destroy the root window?! + + if (node->has_wm_state) { + wm_tree_set_wm_state(tree, node, false); + } + + HASH_DEL(tree->nodes, node); + + if (!list_is_empty(&node->children)) { + log_error("Window %#010x is destroyed, but it still has children. Expect " + "malfunction.", + node->id.x); + list_foreach_safe(struct wm_tree_node, i, &node->children, siblings) { + log_error(" Child window %#010x", i->id.x); + wm_tree_destroy_window(tree, i); + } + } + + if (node->parent->parent == NULL) { + node->is_zombie = true; + wm_tree_enqueue_change(tree, (struct wm_tree_change){ + .toplevel = node->id, + .type = WM_TREE_CHANGE_TOPLEVEL_KILLED, + .killed = node, + }); + } else { + list_remove(&node->siblings); + free(node); + } +} + +void wm_tree_move_to_end(struct wm_tree *tree, struct wm_tree_node *node, bool to_bottom) { + BUG_ON(node == NULL); + BUG_ON(node->parent == NULL); // Trying to move the root window + + if ((node->parent->children.next == &node->siblings && !to_bottom) || + (node->parent->children.prev == &node->siblings && to_bottom)) { + // Already at the target position + return; + } + list_remove(&node->siblings); + if (to_bottom) { + list_insert_before(&node->parent->children, &node->siblings); + } else { + list_insert_after(&node->parent->children, &node->siblings); + } + if (node->parent->parent == NULL) { + wm_tree_enqueue_change(tree, (struct wm_tree_change){ + .type = WM_TREE_CHANGE_TOPLEVEL_RESTACKED, + }); + } +} + +/// Move `node` to above `other` in their parent's child window stack. +void wm_tree_move_to_above(struct wm_tree *tree, struct wm_tree_node *node, + struct wm_tree_node *other) { + BUG_ON(node == NULL); + BUG_ON(node->parent == NULL); // Trying to move the root window + BUG_ON(other == NULL); + BUG_ON(node->parent != other->parent); + + if (node->siblings.next == &other->siblings) { + // Already above `other` + return; + } + + list_remove(&node->siblings); + list_insert_before(&other->siblings, &node->siblings); + if (node->parent->parent == NULL) { + wm_tree_enqueue_change(tree, (struct wm_tree_change){ + .type = WM_TREE_CHANGE_TOPLEVEL_RESTACKED, + }); + } +} + +void wm_tree_reparent(struct wm_tree *tree, struct wm_tree_node *node, + struct wm_tree_node *new_parent) { + BUG_ON(node == NULL); + BUG_ON(new_parent == NULL); // Trying make `node` a root window + + if (node->parent == new_parent) { + // Reparent to the same parent moves the window to the top of the stack + wm_tree_move_to_end(tree, node, false); + return; + } + + wm_tree_detach(tree, node); + + // Reparented window always becomes the topmost child of the new parent + list_insert_after(&new_parent->children, &node->siblings); + node->parent = new_parent; + + auto toplevel = wm_tree_find_toplevel_for(node); + if (node == toplevel) { + // This node could have a stale `->win` if it was a toplevel at + // some point in the past. + node->win = NULL; + wm_tree_enqueue_change(tree, (struct wm_tree_change){ + .toplevel = node->id, + .type = WM_TREE_CHANGE_TOPLEVEL_NEW, + .new_ = node, + }); + } else { + wm_tree_refresh_client_and_queue_change(tree, toplevel); + } +} + +void wm_tree_clear(struct wm_tree *tree) { + struct wm_tree_node *cur, *tmp; + HASH_ITER(hh, tree->nodes, cur, tmp) { + HASH_DEL(tree->nodes, cur); + free(cur); + } + list_foreach_safe(struct wm_tree_change_list, i, &tree->changes, siblings) { + list_remove(&i->siblings); + free(i); + } + list_foreach_safe(struct wm_tree_change_list, i, &tree->free_changes, siblings) { + list_remove(&i->siblings); + free(i); + } +} + +TEST_CASE(tree_manipulation) { + struct wm_tree tree; + wm_tree_init(&tree); + + wm_tree_new_window(&tree, 1, NULL); + auto root = wm_tree_find(&tree, 1); + assert(root != NULL); + assert(root->parent == NULL); + + auto change = wm_tree_dequeue_change(&tree); + assert(change.type == WM_TREE_CHANGE_NONE); + + wm_tree_new_window(&tree, 2, root); + auto node2 = wm_tree_find(&tree, 2); + assert(node2 != NULL); + assert(node2->parent == root); + + change = wm_tree_dequeue_change(&tree); + assert(change.toplevel.x == 2); + assert(change.type == WM_TREE_CHANGE_TOPLEVEL_NEW); + assert(wm_treeid_eq(node2->id, change.toplevel)); + + wm_tree_new_window(&tree, 3, root); + auto node3 = wm_tree_find(&tree, 3); + assert(node3 != NULL); + + change = wm_tree_dequeue_change(&tree); + assert(change.toplevel.x == 3); + assert(change.type == WM_TREE_CHANGE_TOPLEVEL_NEW); + + wm_tree_reparent(&tree, node2, node3); + assert(node2->parent == node3); + assert(node3->children.next == &node2->siblings); + + // node2 is now a child of node3, so it's no longer a toplevel + change = wm_tree_dequeue_change(&tree); + assert(change.toplevel.x == 2); + assert(change.type == WM_TREE_CHANGE_TOPLEVEL_KILLED); + wm_tree_reap_zombie(change.killed); + + wm_tree_set_wm_state(&tree, node2, true); + change = wm_tree_dequeue_change(&tree); + assert(change.toplevel.x == 3); + assert(change.type == WM_TREE_CHANGE_CLIENT); + assert(wm_treeid_eq(change.client.old, WM_TREEID_NONE)); + assert(change.client.new_.x == 2); + + wm_tree_new_window(&tree, 4, node3); + auto node4 = wm_tree_find(&tree, 4); + change = wm_tree_dequeue_change(&tree); + assert(change.type == WM_TREE_CHANGE_NONE); + + wm_tree_set_wm_state(&tree, node4, true); + change = wm_tree_dequeue_change(&tree); + // node3 already has node2 as its client window, so the new one should be ignored. + assert(change.type == WM_TREE_CHANGE_NONE); + + wm_tree_destroy_window(&tree, node2); + change = wm_tree_dequeue_change(&tree); + assert(change.toplevel.x == 3); + assert(change.type == WM_TREE_CHANGE_CLIENT); + assert(change.client.old.x == 2); + assert(change.client.new_.x == 4); + + // Test window ID reuse + wm_tree_destroy_window(&tree, node4); + node4 = wm_tree_new_window(&tree, 4, node3); + wm_tree_set_wm_state(&tree, node4, true); + + change = wm_tree_dequeue_change(&tree); + assert(change.toplevel.x == 3); + assert(change.type == WM_TREE_CHANGE_CLIENT); + assert(change.client.old.x == 4); + assert(change.client.new_.x == 4); + + auto node5 = wm_tree_new_window(&tree, 5, root); + wm_tree_destroy_window(&tree, node5); + change = wm_tree_dequeue_change(&tree); + assert(change.type == WM_TREE_CHANGE_NONE); // Changes cancelled out + + wm_tree_clear(&tree); +} diff --git a/src/wm/win.c b/src/wm/win.c index 88cab3e05f..cb7c152399 100644 --- a/src/wm/win.c +++ b/src/wm/win.c @@ -36,11 +36,6 @@ #include "win.h" -// TODO(yshui) Make more window states internal -struct managed_win_internal { - struct managed_win base; -}; - #define OPAQUE (0xffffffff) static const int WIN_GET_LEADER_MAX_RECURSION = 20; static const int ROUNDED_PIXELS = 1; @@ -74,25 +69,23 @@ static const double ROUNDED_PERCENT = 0.05; * Reread opacity property of a window. */ static void win_update_opacity_prop(struct x_connection *c, struct atom *atoms, - struct managed_win *w, bool detect_client_opacity); -static void win_update_prop_shadow_raw(struct x_connection *c, struct atom *atoms, - struct managed_win *w); -static bool -win_update_prop_shadow(struct x_connection *c, struct atom *atoms, struct managed_win *w); + struct win *w, bool detect_client_opacity); +static void +win_update_prop_shadow_raw(struct x_connection *c, struct atom *atoms, struct win *w); +static bool win_update_prop_shadow(struct x_connection *c, struct atom *atoms, struct win *w); /** * Update leader of a window. */ static xcb_window_t win_get_leader_property(struct x_connection *c, struct atom *atoms, xcb_window_t wid, bool detect_transient, bool detect_client_leader); -static void win_mark_client(session_t *ps, struct managed_win *w, xcb_window_t client); /// Generate a "no corners" region function, from a function that returns the /// region via a region_t pointer argument. Corners of the window will be removed from /// the returned region. /// Function signature has to be (win *, region_t *) #define gen_without_corners(fun) \ - void fun##_without_corners(const struct managed_win *w, region_t *res) { \ + void fun##_without_corners(const struct win *w, region_t *res) { \ fun(w, res); \ win_region_remove_corners_local(w, res); \ } @@ -101,28 +94,28 @@ static void win_mark_client(session_t *ps, struct managed_win *w, xcb_window_t c /// region via a region_t pointer argument. /// Function signature has to be (win *) #define gen_by_val(fun) \ - region_t fun##_by_val(const struct managed_win *w) { \ + region_t fun##_by_val(const struct win *w) { \ region_t ret; \ pixman_region32_init(&ret); \ fun(w, &ret); \ return ret; \ } -static xcb_window_t win_get_leader_raw(session_t *ps, struct managed_win *w, int recursions); +static struct wm_ref *win_get_leader_raw(session_t *ps, struct win *w, int recursions); /** * Get the leader of a window. * * This function updates w->cache_leader if necessary. */ -static inline xcb_window_t win_get_leader(session_t *ps, struct managed_win *w) { +static inline struct wm_ref *win_get_leader(session_t *ps, struct win *w) { return win_get_leader_raw(ps, w, 0); } /** * Update focused state of a window. */ -static void win_update_focused(session_t *ps, struct managed_win *w) { +static void win_update_focused(session_t *ps, struct win *w) { if (w->focused_force != UNSET) { w->focused = w->focused_force; } else { @@ -133,7 +126,8 @@ static void win_update_focused(session_t *ps, struct managed_win *w) { // windows specially if (ps->o.wintype_option[w->window_type].focus || (ps->o.mark_wmwin_focused && is_wmwin) || - (ps->o.mark_ovredir_focused && w->base.id == w->client_win && !is_wmwin) || + (ps->o.mark_ovredir_focused && + wm_ref_client_of(w->tree_ref) == NULL && !is_wmwin) || (w->a.map_state == XCB_MAP_STATE_VIEWABLE && c2_match(ps->c2_state, w, ps->o.focus_blacklist, NULL))) { w->focused = true; @@ -154,45 +148,28 @@ struct group_callback_data { xcb_window_t leader; }; -static inline int group_on_factor_change_callback(struct win *w, void *data_) { - struct group_callback_data *data = data_; - if (!w->managed) { - return 0; - } - auto mw = (struct managed_win *)w; - if (data->leader == win_get_leader(data->ps, mw)) { - win_on_factor_change(data->ps, mw); - } - return 0; -} - /** * Run win_on_factor_change() on all windows with the same leader window. * * @param leader leader window ID */ -static inline void group_on_factor_change(session_t *ps, xcb_window_t leader) { +static inline void group_on_factor_change(session_t *ps, struct wm_ref *leader) { if (!leader) { return; } - struct group_callback_data data = { - .ps = ps, - .leader = leader, - }; - wm_foreach(ps->wm, group_on_factor_change_callback, &data); -} - -static inline int group_is_focused_callback(struct win *w, void *data_) { - struct group_callback_data *data = data_; - if (!w->managed) { - return 0; - } - auto mw = (struct managed_win *)w; - if (data->leader == win_get_leader(data->ps, mw) && win_is_focused_raw(mw)) { - return 1; + wm_stack_foreach(ps->wm, cursor) { + if (wm_ref_is_zombie(cursor)) { + continue; + } + auto w = wm_ref_deref(cursor); + if (w == NULL) { + continue; + } + if (leader == win_get_leader(ps, w)) { + win_on_factor_change(ps, w); + } } - return 0; } /** @@ -201,35 +178,49 @@ static inline int group_is_focused_callback(struct win *w, void *data_) { * @param leader leader window ID * @return true if the window group is focused, false otherwise */ -static inline bool group_is_focused(session_t *ps, xcb_window_t leader) { +static inline bool group_is_focused(session_t *ps, struct wm_ref *leader) { if (!leader) { return false; } - struct group_callback_data data = { - .ps = ps, - .leader = leader, - }; - return wm_foreach(ps->wm, group_is_focused_callback, &data); + wm_stack_foreach(ps->wm, cursor) { + if (wm_ref_is_zombie(cursor)) { + continue; + } + auto w = wm_ref_deref(cursor); + if (w == NULL) { + continue; + } + if (leader == win_get_leader(ps, w) && win_is_focused_raw(w)) { + return true; + } + } + return false; } /** * Set leader of a window. */ -static inline void win_set_leader(session_t *ps, struct managed_win *w, xcb_window_t nleader) { - xcb_window_t cache_leader_old = win_get_leader(ps, w); +static inline void win_set_leader(session_t *ps, struct win *w, xcb_window_t nleader) { + auto cache_leader_old = win_get_leader(ps, w); w->leader = nleader; // Forcefully do this to deal with the case when a child window // gets mapped before parent, or when the window is a waypoint - win_stack_foreach_managed(i, wm_stack_end(ps->wm)) { - i->cache_leader = XCB_NONE; + wm_stack_foreach(ps->wm, cursor) { + if (wm_ref_is_zombie(cursor)) { + continue; + } + auto i = wm_ref_deref(cursor); + if (i != NULL) { + i->cache_leader = XCB_NONE; + } } // Update the old and new window group and active_leader if the // window could affect their state. - xcb_window_t cache_leader = win_get_leader(ps, w); + auto cache_leader = win_get_leader(ps, w); if (win_is_focused_raw(w) && cache_leader_old != cache_leader) { wm_set_active_leader(ps->wm, cache_leader); @@ -241,7 +232,7 @@ static inline void win_set_leader(session_t *ps, struct managed_win *w, xcb_wind /** * Get a rectangular region a window occupies, excluding shadow. */ -static void win_get_region_local(const struct managed_win *w, region_t *res) { +static void win_get_region_local(const struct win *w, region_t *res) { assert(w->widthb >= 0 && w->heightb >= 0); pixman_region32_fini(res); pixman_region32_init_rect(res, 0, 0, (uint)w->widthb, (uint)w->heightb); @@ -250,7 +241,7 @@ static void win_get_region_local(const struct managed_win *w, region_t *res) { /** * Get a rectangular region a window occupies, excluding frame and shadow. */ -void win_get_region_noframe_local(const struct managed_win *w, region_t *res) { +void win_get_region_noframe_local(const struct win *w, region_t *res) { const margin_t extents = win_calc_frame_extents(w); int x = extents.left; @@ -268,7 +259,7 @@ void win_get_region_noframe_local(const struct managed_win *w, region_t *res) { gen_without_corners(win_get_region_noframe_local); -void win_get_region_frame_local(const struct managed_win *w, region_t *res) { +void win_get_region_frame_local(const struct win *w, region_t *res) { const margin_t extents = win_calc_frame_extents(w); auto outer_width = w->widthb; auto outer_height = w->heightb; @@ -303,7 +294,7 @@ gen_by_val(win_get_region_frame_local); * @param ps current session * @param w struct _win element representing the window */ -void add_damage_from_win(session_t *ps, const struct managed_win *w) { +void add_damage_from_win(session_t *ps, const struct win *w) { // XXX there was a cached extents region, investigate // if that's better @@ -317,8 +308,8 @@ void add_damage_from_win(session_t *ps, const struct managed_win *w) { } /// Release the images attached to this window -static inline void win_release_pixmap(backend_t *base, struct managed_win *w) { - log_debug("Releasing pixmap of window %#010x (%s)", w->base.id, w->name); +static inline void win_release_pixmap(backend_t *base, struct win *w) { + log_debug("Releasing pixmap of window %#010x (%s)", win_id(w), w->name); assert(w->win_image); if (w->win_image) { xcb_pixmap_t pixmap = XCB_NONE; @@ -331,8 +322,8 @@ static inline void win_release_pixmap(backend_t *base, struct managed_win *w) { } } } -static inline void win_release_shadow(backend_t *base, struct managed_win *w) { - log_debug("Releasing shadow of window %#010x (%s)", w->base.id, w->name); +static inline void win_release_shadow(backend_t *base, struct win *w) { + log_debug("Releasing shadow of window %#010x (%s)", win_id(w), w->name); if (w->shadow_image) { assert(w->shadow); xcb_pixmap_t pixmap = XCB_NONE; @@ -344,7 +335,7 @@ static inline void win_release_shadow(backend_t *base, struct managed_win *w) { } } -static inline void win_release_mask(backend_t *base, struct managed_win *w) { +static inline void win_release_mask(backend_t *base, struct win *w) { if (w->mask_image) { xcb_pixmap_t pixmap = XCB_NONE; pixmap = base->ops.release_image(base, w->mask_image); @@ -355,18 +346,18 @@ static inline void win_release_mask(backend_t *base, struct managed_win *w) { } } -static inline bool win_bind_pixmap(struct backend_base *b, struct managed_win *w) { +static inline bool win_bind_pixmap(struct backend_base *b, struct win *w) { assert(!w->win_image); auto pixmap = x_new_id(b->c); auto e = xcb_request_check( - b->c->c, xcb_composite_name_window_pixmap_checked(b->c->c, w->base.id, pixmap)); + b->c->c, xcb_composite_name_window_pixmap_checked(b->c->c, win_id(w), pixmap)); if (e) { - log_error("Failed to get named pixmap for window %#010x(%s)", w->base.id, + log_error("Failed to get named pixmap for window %#010x(%s)", win_id(w), w->name); free(e); return false; } - log_debug("New named pixmap for %#010x (%s) : %#010x", w->base.id, w->name, pixmap); + log_debug("New named pixmap for %#010x (%s) : %#010x", win_id(w), w->name, pixmap); w->win_image = b->ops.bind_pixmap(b, pixmap, x_get_visual_info(b->c, w->a.visual)); if (!w->win_image) { log_error("Failed to bind pixmap"); @@ -379,7 +370,7 @@ static inline bool win_bind_pixmap(struct backend_base *b, struct managed_win *w return true; } -void win_release_images(struct backend_base *backend, struct managed_win *w) { +void win_release_images(struct backend_base *backend, struct win *w) { // We don't want to decide what we should do if the image we want to // release is stale (do we clear the stale flags or not?) But if we are // not releasing any images anyway, we don't care about the stale flags. @@ -395,10 +386,10 @@ void win_release_images(struct backend_base *backend, struct managed_win *w) { /// Returns true if the `prop` property is stale, as well as clears the stale /// flag. -static bool win_fetch_and_unset_property_stale(struct managed_win *w, xcb_atom_t prop); +static bool win_fetch_and_unset_property_stale(struct win *w, xcb_atom_t prop); /// Returns true if any of the properties are stale, as well as clear all the /// stale flags. -static void win_clear_all_properties_stale(struct managed_win *w); +static void win_clear_all_properties_stale(struct win *w); // TODO(yshui) make WIN_FLAGS_FACTOR_CHANGED more fine-grained, or find a better // alternative @@ -406,7 +397,7 @@ static void win_clear_all_properties_stale(struct managed_win *w); /// Fetch new window properties from the X server, and run appropriate updates. /// Might set WIN_FLAGS_FACTOR_CHANGED -static void win_update_properties(session_t *ps, struct managed_win *w) { +static void win_update_properties(session_t *ps, struct win *w) { // we cannot receive property change when window has been destroyed assert(w->state != WSTATE_DESTROYED); @@ -421,8 +412,8 @@ static void win_update_properties(session_t *ps, struct managed_win *w) { } if (win_fetch_and_unset_property_stale(w, ps->atoms->a_NET_FRAME_EXTENTS)) { - win_update_frame_extents(&ps->c, ps->atoms, w, w->client_win, - ps->o.frame_opacity); + auto client_win = win_client_id(w, /*fallback_to_self=*/false); + win_update_frame_extents(&ps->c, ps->atoms, w, client_win, ps->o.frame_opacity); add_damage_from_win(ps, w); } @@ -459,7 +450,8 @@ static void win_update_properties(session_t *ps, struct managed_win *w) { if (win_fetch_and_unset_property_stale(w, ps->atoms->aWM_CLIENT_LEADER) || win_fetch_and_unset_property_stale(w, ps->atoms->aWM_TRANSIENT_FOR)) { - auto new_leader = win_get_leader_property(&ps->c, ps->atoms, w->client_win, + auto client_win = win_client_id(w, /*fallback_to_self=*/true); + auto new_leader = win_get_leader_property(&ps->c, ps->atoms, client_win, ps->o.detect_transient, ps->o.detect_client_leader); if (w->leader != new_leader) { @@ -471,15 +463,14 @@ static void win_update_properties(session_t *ps, struct managed_win *w) { win_clear_all_properties_stale(w); } -static void map_win_start(struct managed_win *w); /// Handle non-image flags. This phase might set IMAGES_STALE flags -void win_process_update_flags(session_t *ps, struct managed_win *w) { +void win_process_update_flags(session_t *ps, struct win *w) { log_trace("Processing flags for window %#010x (%s), was rendered: %d, flags: " "%#" PRIx64, - w->base.id, w->name, w->to_paint, w->flags); + win_id(w), w->name, w->to_paint, w->flags); if (win_check_flags_all(w, WIN_FLAGS_MAPPED)) { - map_win_start(w); + win_map_start(w); win_clear_flags(w, WIN_FLAGS_MAPPED); } @@ -489,21 +480,12 @@ void win_process_update_flags(session_t *ps, struct managed_win *w) { return; } - // Check client first, because later property updates need accurate client - // window information + bool damaged = false; if (win_check_flags_all(w, WIN_FLAGS_CLIENT_STALE)) { - log_debug("Rechecking client window for %#010x (%s)", w->base.id, w->name); - auto client_win = win_get_client_window(&ps->c, ps->wm, ps->atoms, w); - if (w->client_win && w->client_win != client_win) { - win_unmark_client(w); - } - log_debug("New client window for %#010x (%s): %#010x", w->base.id, - w->name, client_win); - win_mark_client(ps, w, client_win); + win_on_client_update(ps, w); win_clear_flags(w, WIN_FLAGS_CLIENT_STALE); } - bool damaged = false; if (win_check_flags_any(w, WIN_FLAGS_SIZE_STALE | WIN_FLAGS_POSITION_STALE)) { // For damage calculation purposes, we don't care if the window // is mapped in X server, we only care if we rendered it last @@ -578,7 +560,7 @@ void win_process_update_flags(session_t *ps, struct managed_win *w) { } } -void win_process_image_flags(session_t *ps, struct managed_win *w) { +void win_process_image_flags(session_t *ps, struct win *w) { // Assert that the MAPPED flag is already handled. assert(!win_check_flags_all(w, WIN_FLAGS_MAPPED)); @@ -623,7 +605,7 @@ void win_process_image_flags(session_t *ps, struct managed_win *w) { * Check if a window has rounded corners. * XXX This is really dumb */ -static bool attr_pure win_has_rounded_corners(const struct managed_win *w) { +static bool attr_pure win_has_rounded_corners(const struct win *w) { if (!w->bounding_shaped) { return false; } @@ -656,22 +638,16 @@ static bool attr_pure win_has_rounded_corners(const struct managed_win *w) { return false; } -int win_update_name(struct x_connection *c, struct atom *atoms, struct managed_win *w) { +int win_update_name(struct x_connection *c, struct atom *atoms, struct win *w) { char **strlst = NULL; int nstr = 0; + auto client_win = win_client_id(w, /*fallback_to_self=*/true); - if (!w->client_win) { - return 0; - } - - if (!(wid_get_text_prop(c, atoms, w->client_win, atoms->a_NET_WM_NAME, &strlst, &nstr))) { - log_debug("(%#010x): _NET_WM_NAME unset, falling back to " - "WM_NAME.", - w->client_win); + if (!(wid_get_text_prop(c, atoms, client_win, atoms->a_NET_WM_NAME, &strlst, &nstr))) { + log_debug("(%#010x): _NET_WM_NAME unset, falling back to WM_NAME.", client_win); - if (!wid_get_text_prop(c, atoms, w->client_win, atoms->aWM_NAME, &strlst, - &nstr)) { - log_debug("Unsetting window name for %#010x", w->client_win); + if (!wid_get_text_prop(c, atoms, client_win, atoms->aWM_NAME, &strlst, &nstr)) { + log_debug("Unsetting window name for %#010x", client_win); free(w->name); w->name = NULL; return -1; @@ -687,17 +663,17 @@ int win_update_name(struct x_connection *c, struct atom *atoms, struct managed_w free(strlst); - log_debug("(%#010x): client = %#010x, name = \"%s\", " - "ret = %d", - w->base.id, w->client_win, w->name, ret); + log_debug("(%#010x): client = %#010x, name = \"%s\", ret = %d", win_id(w), + client_win, w->name, ret); return ret; } -int win_update_role(struct x_connection *c, struct atom *atoms, struct managed_win *w) { +int win_update_role(struct x_connection *c, struct atom *atoms, struct win *w) { char **strlst = NULL; int nstr = 0; + auto client_win = win_client_id(w, /*fallback_to_self=*/true); - if (!wid_get_text_prop(c, atoms, w->client_win, atoms->aWM_WINDOW_ROLE, &strlst, &nstr)) { + if (!wid_get_text_prop(c, atoms, client_win, atoms->aWM_WINDOW_ROLE, &strlst, &nstr)) { return -1; } @@ -710,9 +686,8 @@ int win_update_role(struct x_connection *c, struct atom *atoms, struct managed_w free(strlst); - log_trace("(%#010x): client = %#010x, role = \"%s\", " - "ret = %d", - w->base.id, w->client_win, w->role, ret); + log_trace("(%#010x): client = %#010x, role = \"%s\", ret = %d", win_id(w), + client_win, w->role, ret); return ret; } @@ -768,19 +743,19 @@ static bool wid_get_opacity_prop(struct x_connection *c, struct atom *atoms, } // XXX should distinguish between frame has alpha and window body has alpha -bool win_has_alpha(const struct managed_win *w) { +bool win_has_alpha(const struct win *w) { return w->pictfmt && w->pictfmt->type == XCB_RENDER_PICT_TYPE_DIRECT && w->pictfmt->direct.alpha_mask; } -bool win_client_has_alpha(const struct managed_win *w) { +bool win_client_has_alpha(const struct win *w) { return w->client_pictfmt && w->client_pictfmt->type == XCB_RENDER_PICT_TYPE_DIRECT && w->client_pictfmt->direct.alpha_mask; } -winmode_t win_calc_mode_raw(const struct managed_win *w) { +winmode_t win_calc_mode_raw(const struct win *w) { if (win_has_alpha(w)) { - if (w->client_win == XCB_NONE) { + if (wm_ref_client_of(w->tree_ref) == NULL) { // This is a window not managed by the WM, and it has // alpha, so it's transparent. No need to check WM frame. return WMODE_TRANS; @@ -808,7 +783,7 @@ winmode_t win_calc_mode_raw(const struct managed_win *w) { return WMODE_SOLID; } -winmode_t win_calc_mode(const struct managed_win *w) { +winmode_t win_calc_mode(const struct win *w) { if (win_animatable_get(w, WIN_SCRIPT_OPACITY) < 1.0) { return WMODE_TRANS; } @@ -829,7 +804,7 @@ winmode_t win_calc_mode(const struct managed_win *w) { * * @return target opacity */ -static double win_calc_opacity_target(session_t *ps, const struct managed_win *w) { +static double win_calc_opacity_target(session_t *ps, const struct win *w) { double opacity = 1; if (w->state == WSTATE_UNMAPPED || w->state == WSTATE_DESTROYED) { @@ -864,7 +839,7 @@ static double win_calc_opacity_target(session_t *ps, const struct managed_win *w /// Finish the unmapping of a window (e.g. after fading has finished). /// Doesn't free `w` -void unmap_win_finish(session_t *ps, struct managed_win *w) { +void unmap_win_finish(session_t *ps, struct win *w) { w->reg_ignore_valid = false; // We are in unmap_win, this window definitely was viewable @@ -892,7 +867,7 @@ void unmap_win_finish(session_t *ps, struct managed_win *w) { /** * Determine whether a window is to be dimmed. */ -bool win_should_dim(session_t *ps, const struct managed_win *w) { +bool win_should_dim(session_t *ps, const struct win *w) { // Make sure we do nothing if the window is unmapped / being destroyed if (w->state == WSTATE_UNMAPPED) { return false; @@ -909,10 +884,9 @@ bool win_should_dim(session_t *ps, const struct managed_win *w) { * * The property must be set on the outermost window, usually the WM frame. */ -void win_update_prop_shadow_raw(struct x_connection *c, struct atom *atoms, - struct managed_win *w) { +void win_update_prop_shadow_raw(struct x_connection *c, struct atom *atoms, struct win *w) { winprop_t prop = - x_get_prop(c, w->base.id, atoms->a_COMPTON_SHADOW, 1, XCB_ATOM_CARDINAL, 32); + x_get_prop(c, win_id(w), atoms->a_COMPTON_SHADOW, 1, XCB_ATOM_CARDINAL, 32); if (!prop.nitems) { w->prop_shadow = -1; @@ -923,12 +897,12 @@ void win_update_prop_shadow_raw(struct x_connection *c, struct atom *atoms, free_winprop(&prop); } -static void win_set_shadow(session_t *ps, struct managed_win *w, bool shadow_new) { +static void win_set_shadow(session_t *ps, struct win *w, bool shadow_new) { if (w->shadow == shadow_new) { return; } - log_debug("Updating shadow property of window %#010x (%s) to %d", w->base.id, + log_debug("Updating shadow property of window %#010x (%s) to %d", win_id(w), w->name, shadow_new); // We don't handle property updates of non-visible windows until they are @@ -974,8 +948,8 @@ static void win_set_shadow(session_t *ps, struct managed_win *w, bool shadow_new * Determine if a window should have shadow, and update things depending * on shadow state. */ -static void win_determine_shadow(session_t *ps, struct managed_win *w) { - log_debug("Determining shadow of window %#010x (%s)", w->base.id, w->name); +static void win_determine_shadow(session_t *ps, struct win *w) { + log_debug("Determining shadow of window %#010x (%s)", win_id(w), w->name); bool shadow_new = w->shadow; if (w->shadow_force != UNSET) { @@ -1005,8 +979,7 @@ static void win_determine_shadow(session_t *ps, struct managed_win *w) { * Reread _COMPTON_SHADOW property from a window and update related * things. */ -static bool -win_update_prop_shadow(struct x_connection *c, struct atom *atoms, struct managed_win *w) { +static bool win_update_prop_shadow(struct x_connection *c, struct atom *atoms, struct win *w) { long long attr_shadow_old = w->prop_shadow; win_update_prop_shadow_raw(c, atoms, w); return w->prop_shadow != attr_shadow_old; @@ -1016,8 +989,9 @@ win_update_prop_shadow(struct x_connection *c, struct atom *atoms, struct manage * Update window EWMH fullscreen state. */ bool win_update_prop_fullscreen(struct x_connection *c, const struct atom *atoms, - struct managed_win *w) { - auto prop = x_get_prop(c, w->client_win, atoms->a_NET_WM_STATE, 12, XCB_ATOM_ATOM, 0); + struct win *w) { + auto prop = x_get_prop(c, win_client_id(w, /*fallback_to_self=*/true), + atoms->a_NET_WM_STATE, 12, XCB_ATOM_ATOM, 0); bool is_fullscreen = false; for (uint32_t i = 0; i < prop.nitems; i++) { if (prop.atom[i] == atoms->a_NET_WM_STATE_FULLSCREEN) { @@ -1032,13 +1006,13 @@ bool win_update_prop_fullscreen(struct x_connection *c, const struct atom *atoms return changed; } -static void win_determine_clip_shadow_above(session_t *ps, struct managed_win *w) { +static void win_determine_clip_shadow_above(session_t *ps, struct win *w) { bool should_crop = (ps->o.wintype_option[w->window_type].clip_shadow_above || c2_match(ps->c2_state, w, ps->o.shadow_clip_list, NULL)); w->clip_shadow_above = should_crop; } -static void win_set_invert_color(session_t *ps, struct managed_win *w, bool invert_color_new) { +static void win_set_invert_color(session_t *ps, struct win *w, bool invert_color_new) { if (w->invert_color == invert_color_new) { return; } @@ -1051,7 +1025,7 @@ static void win_set_invert_color(session_t *ps, struct managed_win *w, bool inve /** * Determine if a window should have color inverted. */ -static void win_determine_invert_color(session_t *ps, struct managed_win *w) { +static void win_determine_invert_color(session_t *ps, struct win *w) { bool invert_color_new = w->invert_color; if (UNSET != w->invert_color_force) { @@ -1066,7 +1040,7 @@ static void win_determine_invert_color(session_t *ps, struct managed_win *w) { /** * Set w->invert_color_force of a window. */ -void win_set_invert_color_force(session_t *ps, struct managed_win *w, switch_t val) { +void win_set_invert_color_force(session_t *ps, struct win *w, switch_t val) { if (val != w->invert_color_force) { w->invert_color_force = val; win_determine_invert_color(ps, w); @@ -1079,14 +1053,14 @@ void win_set_invert_color_force(session_t *ps, struct managed_win *w, switch_t v * * Doesn't affect fading already in progress */ -void win_set_fade_force(struct managed_win *w, switch_t val) { +void win_set_fade_force(struct win *w, switch_t val) { w->fade_force = val; } /** * Set w->focused_force of a window. */ -void win_set_focused_force(session_t *ps, struct managed_win *w, switch_t val) { +void win_set_focused_force(session_t *ps, struct win *w, switch_t val) { if (val != w->focused_force) { w->focused_force = val; win_on_factor_change(ps, w); @@ -1097,7 +1071,7 @@ void win_set_focused_force(session_t *ps, struct managed_win *w, switch_t val) { /** * Set w->shadow_force of a window. */ -void win_set_shadow_force(session_t *ps, struct managed_win *w, switch_t val) { +void win_set_shadow_force(session_t *ps, struct win *w, switch_t val) { if (val != w->shadow_force) { w->shadow_force = val; win_determine_shadow(ps, w); @@ -1105,8 +1079,7 @@ void win_set_shadow_force(session_t *ps, struct managed_win *w, switch_t val) { } } -static void -win_set_blur_background(session_t *ps, struct managed_win *w, bool blur_background_new) { +static void win_set_blur_background(session_t *ps, struct win *w, bool blur_background_new) { if (w->blur_background == blur_background_new) { return; } @@ -1119,8 +1092,7 @@ win_set_blur_background(session_t *ps, struct managed_win *w, bool blur_backgrou add_damage_from_win(ps, w); } -static void -win_set_fg_shader(session_t *ps, struct managed_win *w, struct shader_info *shader_new) { +static void win_set_fg_shader(session_t *ps, struct win *w, struct shader_info *shader_new) { if (w->fg_shader == shader_new) { return; } @@ -1135,8 +1107,8 @@ win_set_fg_shader(session_t *ps, struct managed_win *w, struct shader_info *shad /** * Determine if a window should have background blurred. */ -static void win_determine_blur_background(session_t *ps, struct managed_win *w) { - log_debug("Determining blur-background of window %#010x (%s)", w->base.id, w->name); +static void win_determine_blur_background(session_t *ps, struct win *w) { + log_debug("Determining blur-background of window %#010x (%s)", win_id(w), w->name); if (w->a.map_state != XCB_MAP_STATE_VIEWABLE) { return; } @@ -1159,7 +1131,7 @@ static void win_determine_blur_background(session_t *ps, struct managed_win *w) /** * Determine if a window should have rounded corners. */ -static void win_determine_rounded_corners(session_t *ps, struct managed_win *w) { +static void win_determine_rounded_corners(session_t *ps, struct win *w) { void *radius_override = NULL; if (c2_match(ps->c2_state, w, ps->o.corner_radius_rules, &radius_override)) { log_debug("Matched corner rule! %d", w->corner_radius); @@ -1176,7 +1148,7 @@ static void win_determine_rounded_corners(session_t *ps, struct managed_win *w) ((w && w->is_fullscreen) || c2_match(ps->c2_state, w, ps->o.rounded_corners_blacklist, NULL))) { w->corner_radius = 0; - log_debug("Not rounding corners for window %#010x", w->base.id); + log_debug("Not rounding corners for window %#010x", win_id(w)); } else { if (radius_override) { w->corner_radius = (int)(long)radius_override; @@ -1184,7 +1156,7 @@ static void win_determine_rounded_corners(session_t *ps, struct managed_win *w) w->corner_radius = ps->o.corner_radius; } - log_debug("Rounding corners for window %#010x", w->base.id); + log_debug("Rounding corners for window %#010x", win_id(w)); // Initialize the border color to an invalid value w->border_col[0] = w->border_col[1] = w->border_col[2] = w->border_col[3] = -1.0F; @@ -1194,7 +1166,7 @@ static void win_determine_rounded_corners(session_t *ps, struct managed_win *w) /** * Determine custom window shader to use for a window. */ -static void win_determine_fg_shader(session_t *ps, struct managed_win *w) { +static void win_determine_fg_shader(session_t *ps, struct win *w) { if (w->a.map_state != XCB_MAP_STATE_VIEWABLE) { return; } @@ -1216,7 +1188,7 @@ static void win_determine_fg_shader(session_t *ps, struct managed_win *w) { /** * Update window opacity according to opacity rules. */ -void win_update_opacity_rule(session_t *ps, struct managed_win *w) { +void win_update_opacity_rule(session_t *ps, struct win *w) { if (w->a.map_state != XCB_MAP_STATE_VIEWABLE) { return; } @@ -1238,9 +1210,10 @@ void win_update_opacity_rule(session_t *ps, struct managed_win *w) { * * TODO(yshui) need better name */ -void win_on_factor_change(session_t *ps, struct managed_win *w) { - log_debug("Window %#010x (%s) factor change", w->base.id, w->name); - c2_window_state_update(ps->c2_state, &w->c2_state, ps->c.c, w->client_win, w->base.id); +void win_on_factor_change(session_t *ps, struct win *w) { + auto wid = win_client_id(w, /*fallback_to_self=*/true); + log_debug("Window %#010x, client %#010x (%s) factor change", win_id(w), wid, w->name); + c2_window_state_update(ps->c2_state, &w->c2_state, ps->c.c, wid, win_id(w)); // Focus and is_fullscreen needs to be updated first, as other rules might depend // on the focused state of the window win_update_focused(ps, w); @@ -1271,7 +1244,8 @@ void win_on_factor_change(session_t *ps, struct managed_win *w) { w->reg_ignore_valid = false; if (ps->debug_window != XCB_NONE && - (w->base.id == ps->debug_window || w->client_win == ps->debug_window)) { + (win_id(w) == ps->debug_window || + (win_client_id(w, /*fallback_to_self=*/false) == ps->debug_window))) { w->paint_excluded = true; } } @@ -1279,9 +1253,9 @@ void win_on_factor_change(session_t *ps, struct managed_win *w) { /** * Update cache data in struct _win that depends on window size. */ -void win_on_win_size_change(struct managed_win *w, int shadow_offset_x, - int shadow_offset_y, int shadow_radius) { - log_trace("Window %#010x (%s) size changed, was %dx%d, now %dx%d", w->base.id, +void win_on_win_size_change(struct win *w, int shadow_offset_x, int shadow_offset_y, + int shadow_radius) { + log_trace("Window %#010x (%s) size changed, was %dx%d, now %dx%d", win_id(w), w->name, w->widthb, w->heightb, w->g.width + w->g.border_width * 2, w->g.height + w->g.border_width * 2); @@ -1300,36 +1274,38 @@ void win_on_win_size_change(struct managed_win *w, int shadow_offset_x, /** * Update window type. */ -bool win_update_wintype(struct x_connection *c, struct atom *atoms, struct managed_win *w) { +bool win_update_wintype(struct x_connection *c, struct atom *atoms, struct win *w) { const wintype_t wtype_old = w->window_type; + auto wid = win_client_id(w, /*fallback_to_self=*/true); // Detect window type here - w->window_type = wid_get_prop_wintype(c, atoms, w->client_win); + w->window_type = wid_get_prop_wintype(c, atoms, wid); // Conform to EWMH standard, if _NET_WM_WINDOW_TYPE is not present, take // override-redirect windows or windows without WM_TRANSIENT_FOR as // _NET_WM_WINDOW_TYPE_NORMAL, otherwise as _NET_WM_WINDOW_TYPE_DIALOG. if (WINTYPE_UNKNOWN == w->window_type) { if (w->a.override_redirect || - !wid_has_prop(c->c, w->client_win, atoms->aWM_TRANSIENT_FOR)) { + !wid_has_prop(c->c, wid, atoms->aWM_TRANSIENT_FOR)) { w->window_type = WINTYPE_NORMAL; } else { w->window_type = WINTYPE_DIALOG; } } + log_debug("Window (%#010x) has type %s", win_id(w), WINTYPES[w->window_type].name); + return w->window_type != wtype_old; } /** - * Mark a window as the client window of another. + * Update window after its client window changed. * * @param ps current session * @param w struct _win of the parent window - * @param client window ID of the client window */ -static void win_mark_client(session_t *ps, struct managed_win *w, xcb_window_t client) { - w->client_win = client; +void win_on_client_update(session_t *ps, struct win *w) { + auto client_win = wm_ref_client_of(w->tree_ref); // If the window isn't mapped yet, stop here, as the function will be // called in map_win() @@ -1339,12 +1315,13 @@ static void win_mark_client(session_t *ps, struct managed_win *w, xcb_window_t c win_update_wintype(&ps->c, ps->atoms, w); + xcb_window_t client_win_id = client_win ? wm_ref_win_id(client_win) : XCB_NONE; // Get frame widths. The window is in damaged area already. - win_update_frame_extents(&ps->c, ps->atoms, w, client, ps->o.frame_opacity); + win_update_frame_extents(&ps->c, ps->atoms, w, client_win_id, ps->o.frame_opacity); // Get window group if (ps->o.track_leader) { - auto new_leader = win_get_leader_property(&ps->c, ps->atoms, w->client_win, + auto new_leader = win_get_leader_property(&ps->c, ps->atoms, client_win_id, ps->o.detect_transient, ps->o.detect_client_leader); if (w->leader != new_leader) { @@ -1360,11 +1337,8 @@ static void win_mark_client(session_t *ps, struct managed_win *w, xcb_window_t c // Update everything related to conditions win_on_factor_change(ps, w); - xcb_generic_error_t *e = NULL; - auto r = xcb_get_window_attributes_reply( - ps->c.c, xcb_get_window_attributes(ps->c.c, w->client_win), &e); + auto r = XCB_AWAIT(xcb_get_window_attributes, ps->c.c, client_win_id); if (!r) { - log_error_x_error(e, "Failed to get client window attributes"); return; } @@ -1372,91 +1346,17 @@ static void win_mark_client(session_t *ps, struct managed_win *w, xcb_window_t c free(r); } -/** - * Unmark current client window of a window. - * - * @param ps current session - * @param w struct _win of the parent window - */ -void win_unmark_client(struct managed_win *w) { - xcb_window_t client = w->client_win; - log_debug("Detaching client window %#010x from frame %#010x (%s)", client, - w->base.id, w->name); - w->client_win = XCB_NONE; -} - -/** - * Look for the client window of a particular window. - */ -static xcb_window_t -find_client_win(struct x_connection *c, struct wm *wm, struct atom *atoms, xcb_window_t w) { - xcb_query_tree_reply_t *reply = - xcb_query_tree_reply(c->c, xcb_query_tree(c->c, w), NULL); - if (!reply) { - return XCB_NONE; - } - - xcb_window_t *children = xcb_query_tree_children(reply); - int nchildren = xcb_query_tree_children_length(reply); - xcb_window_t ret = XCB_NONE; - - for (int i = 0; i < nchildren; ++i) { - auto subwin = wm ? wm_subwin_find(wm, children[i]) : NULL; - bool has_wm_state; - assert(subwin != NULL || wm == NULL); - if (!subwin || subwin->has_wm_state == TRI_UNKNOWN) { - has_wm_state = wid_has_prop(c->c, children[i], atoms->aWM_STATE); - if (subwin) { - subwin->has_wm_state = has_wm_state ? TRI_TRUE : TRI_FALSE; - } - } else { - has_wm_state = subwin->has_wm_state == TRI_TRUE; - } - if (has_wm_state) { - ret = children[i]; - break; - } - } - - free(reply); - return ret; -} - -/** - * Get client window of a window. - * - * @param ps current session - * @param w struct _win of the parent window - */ -xcb_window_t win_get_client_window(struct x_connection *c, struct wm *wm, - struct atom *atoms, const struct managed_win *w) { - // Always recursively look for a window with WM_STATE, as Fluxbox - // sets override-redirect flags on all frame windows. - xcb_window_t cw = find_client_win(c, wm, atoms, w->base.id); - if (cw) { - log_debug("(%#010x): client %#010x", w->base.id, cw); - } else { - // Set a window's client window to itself if we couldn't find a - // client window - cw = w->base.id; - log_debug("(%#010x): client self (%s)", w->base.id, - (w->a.override_redirect ? "override-redirected" : "wmwin")); - } - - return cw; -} - #ifdef CONFIG_OPENGL -void free_win_res_glx(session_t *ps, struct managed_win *w); +void free_win_res_glx(session_t *ps, struct win *w); #else -static inline void free_win_res_glx(session_t * /*ps*/, struct managed_win * /*w*/) { +static inline void free_win_res_glx(session_t * /*ps*/, struct win * /*w*/) { } #endif /** * Free all resources in a struct _win. */ -void free_win_res(session_t *ps, struct managed_win *w) { +void free_win_res(session_t *ps, struct win *w) { // No need to call backend release_image here because // finish_unmap_win should've done that for us. // XXX unless we are called by session_destroy @@ -1483,31 +1383,16 @@ void free_win_res(session_t *ps, struct managed_win *w) { c2_window_state_destroy(ps->c2_state, &w->c2_state); } -/// Query the Xorg for information about window `win`, and return that -/// information in a new managed_win object. However, if the window does -/// not need to be managed, the original `win` object is returned. -struct win *attr_ret_nonnull maybe_allocate_managed_win(session_t *ps, struct win *w) { - static const struct managed_win win_def = { - // No need to initialize. (or, you can think that - // they are initialized right here). - // The following ones are updated during paint or paint preprocess - .to_paint = false, +/// Query the Xorg for information about window `win`, and assign a window to `cursor` if +/// this window should be managed. +struct win *win_maybe_allocate(session_t *ps, struct wm_ref *cursor) { + static const struct win win_def = { .frame_opacity = 1.0, - .dim = false, - .invert_color = false, - .blur_background = false, - .reg_ignore = NULL, - // The following ones are updated for other reasons - .pixmap_damaged = false, // updated by damage events - .state = WSTATE_UNMAPPED, // updated by window state changes - .in_openclose = true, // set to false after first map is done, - // true here because window is just created - .reg_ignore_valid = false, // set to true when damaged + .in_openclose = true, // set to false after first map is done, + // true here because window is just created .flags = WIN_FLAGS_PIXMAP_NONE, // updated by // property/attributes/etc // change - .stale_props = NULL, - .stale_props_capacity = 0, // Runtime variables, updated by dbus .fade_force = UNSET, @@ -1515,86 +1400,30 @@ struct win *attr_ret_nonnull maybe_allocate_managed_win(session_t *ps, struct wi .focused_force = UNSET, .invert_color_force = UNSET, - // Initialized in this function - .a = {0}, - .pictfmt = NULL, - .client_pictfmt = NULL, - .widthb = 0, - .heightb = 0, - .shadow_dx = 0, - .shadow_dy = 0, - .shadow_width = 0, - .shadow_height = 0, - .damage = XCB_NONE, - - // Not initialized until mapped, this variables - // have no meaning or have no use until the window - // is mapped - .win_image = NULL, - .shadow_image = NULL, - .mask_image = NULL, - .prev_trans = NULL, - .shadow = false, - .clip_shadow_above = false, - .fg_shader = NULL, .randr_monitor = -1, .mode = WMODE_TRANS, - .ever_damaged = false, - .client_win = XCB_NONE, .leader = XCB_NONE, .cache_leader = XCB_NONE, .window_type = WINTYPE_UNKNOWN, - .focused = false, - .has_opacity_prop = false, .opacity_prop = OPAQUE, - .opacity_is_set = false, .opacity_set = 1, - .opacity = 0, - .frame_extents = MARGIN_INIT, // in win_mark_client - .bounding_shaped = false, - .bounding_shape = {0}, - .rounded_corners = false, - .paint_excluded = false, - .fade_excluded = false, - .transparent_clipping = false, - .unredir_if_possible_excluded = false, + .frame_extents = MARGIN_INIT, .prop_shadow = -1, - // following 4 are set in win_mark_client - .name = NULL, - .class_instance = NULL, - .class_general = NULL, - .role = NULL, - // Initialized during paint .paint = PAINT_INIT, .shadow_paint = PAINT_INIT, - - .corner_radius = 0, }; - assert(!w->destroyed); - assert(w->is_new); - - w->is_new = false; - // Reject overlay window - if (w->id == ps->overlay) { + if (wm_ref_win_id(cursor) == ps->overlay) { // Would anyone reparent windows to the overlay window? Doing this // just in case. - return w; - } - - auto duplicated_win = wm_find_managed(ps->wm, w->id); - if (duplicated_win) { - log_debug("Window %#010x (recorded name: %s) added multiple " - "times", - w->id, duplicated_win->name); - return &duplicated_win->base; + return NULL; } - log_debug("Managing window %#010x", w->id); - xcb_get_window_attributes_cookie_t acookie = - xcb_get_window_attributes(ps->c.c, w->id); + xcb_window_t wid = wm_ref_win_id(cursor); + log_debug("Managing window %#010x", wid); + xcb_get_window_attributes_cookie_t acookie = xcb_get_window_attributes(ps->c.c, wid); xcb_get_window_attributes_reply_t *a = xcb_get_window_attributes_reply(ps->c.c, acookie, NULL); if (!a || a->map_state == XCB_MAP_STATE_UNVIEWABLE) { @@ -1604,27 +1433,23 @@ struct win *attr_ret_nonnull maybe_allocate_managed_win(session_t *ps, struct wi // BTW, we don't care about Input Only windows, except for // stacking proposes, so we need to keep track of them still. free(a); - return w; + return NULL; } if (a->_class == XCB_WINDOW_CLASS_INPUT_ONLY) { // No need to manage this window, but we still keep it on the // window stack - w->managed = false; free(a); - return w; + return NULL; } // Allocate and initialize the new win structure - auto new_internal = cmalloc(struct managed_win_internal); - auto new = (struct managed_win *)new_internal; + auto new = cmalloc(struct win); // Fill structure // We only need to initialize the part that are not initialized // by map_win *new = win_def; - new->base = *w; - new->base.managed = true; new->a = *a; new->shadow_opacity = ps->o.shadow_opacity; pixman_region32_init(&new->bounding_shape); @@ -1632,12 +1457,12 @@ struct win *attr_ret_nonnull maybe_allocate_managed_win(session_t *ps, struct wi free(a); xcb_generic_error_t *e; - auto g = xcb_get_geometry_reply(ps->c.c, xcb_get_geometry(ps->c.c, w->id), &e); + auto g = xcb_get_geometry_reply(ps->c.c, xcb_get_geometry(ps->c.c, wid), &e); if (!g) { - log_error_x_error(e, "Failed to get geometry of window %#010x", w->id); + log_error_x_error(e, "Failed to get geometry of window %#010x", wid); free(e); free(new); - return w; + return NULL; } new->pending_g = (struct win_geometry){ .x = g->x, @@ -1652,13 +1477,13 @@ struct win *attr_ret_nonnull maybe_allocate_managed_win(session_t *ps, struct wi // Create Damage for window (if not Input Only) new->damage = x_new_id(&ps->c); e = xcb_request_check( - ps->c.c, xcb_damage_create_checked(ps->c.c, new->damage, w->id, + ps->c.c, xcb_damage_create_checked(ps->c.c, new->damage, wid, XCB_DAMAGE_REPORT_LEVEL_NON_EMPTY)); if (e) { log_error_x_error(e, "Failed to create damage"); free(e); free(new); - return w; + return NULL; } // Set window event mask @@ -1667,33 +1492,22 @@ struct win *attr_ret_nonnull maybe_allocate_managed_win(session_t *ps, struct wi if (!ps->o.use_ewmh_active_win) { frame_event_mask |= XCB_EVENT_MASK_FOCUS_CHANGE; } - xcb_change_window_attributes(ps->c.c, new->base.id, XCB_CW_EVENT_MASK, + xcb_change_window_attributes(ps->c.c, wid, XCB_CW_EVENT_MASK, (const uint32_t[]){frame_event_mask}); - // Add existing subwins of this window - auto tree_reply = - xcb_query_tree_reply(ps->c.c, xcb_query_tree(ps->c.c, new->base.id), NULL); - if (tree_reply) { - auto children = xcb_query_tree_children(tree_reply); - for (int i = 0; i < xcb_query_tree_children_length(tree_reply); i++) { - wm_subwin_add_and_subscribe(ps->wm, &ps->c, children[i], new->base.id); - } - free(tree_reply); - } - // Get notification when the shape of a window changes if (ps->shape_exists) { - xcb_shape_select_input(ps->c.c, new->base.id, 1); + xcb_shape_select_input(ps->c.c, wid, 1); } new->pictfmt = x_get_pictform_for_visual(&ps->c, new->a.visual); new->client_pictfmt = NULL; + new->tree_ref = cursor; // Set all the stale flags on this new window, so it's properties will get // updated when it's mapped - win_set_flags(new, WIN_FLAGS_CLIENT_STALE | WIN_FLAGS_SIZE_STALE | - WIN_FLAGS_POSITION_STALE | WIN_FLAGS_PROPERTY_STALE | - WIN_FLAGS_FACTOR_CHANGED); + win_set_flags(new, WIN_FLAGS_SIZE_STALE | WIN_FLAGS_POSITION_STALE | + WIN_FLAGS_PROPERTY_STALE | WIN_FLAGS_FACTOR_CHANGED); xcb_atom_t init_stale_props[] = { ps->atoms->a_NET_WM_WINDOW_TYPE, ps->atoms->a_NET_WM_WINDOW_OPACITY, ps->atoms->a_NET_FRAME_EXTENTS, ps->atoms->aWM_NAME, @@ -1706,7 +1520,9 @@ struct win *attr_ret_nonnull maybe_allocate_managed_win(session_t *ps, struct wi c2_window_state_init(ps->c2_state, &new->c2_state); pixman_region32_init(&new->damaged); - return &new->base; + wm_ref_set(cursor, new); + + return new; } /** @@ -1733,18 +1549,23 @@ win_get_leader_property(struct x_connection *c, struct atom *atoms, xcb_window_t /** * Internal function of win_get_leader(). */ -static xcb_window_t win_get_leader_raw(session_t *ps, struct managed_win *w, int recursions) { +static struct wm_ref *win_get_leader_raw(session_t *ps, struct win *w, int recursions) { // Rebuild the cache if needed - if (!w->cache_leader && (w->client_win || w->leader)) { - // Leader defaults to client window - if (!(w->cache_leader = w->leader)) { - w->cache_leader = w->client_win; + auto client_win = wm_ref_client_of(w->tree_ref); + if (w->cache_leader == NULL && (client_win != NULL || w->leader)) { + // Leader defaults to client window, or to the window itself if + // it doesn't have a client window + w->cache_leader = wm_find(ps->wm, w->leader); + if (!w->cache_leader) { + w->cache_leader = client_win ?: w->tree_ref; } // If the leader of this window isn't itself, look for its // ancestors - if (w->cache_leader && w->cache_leader != w->client_win) { - auto wp = wm_find_by_client(ps->wm, w->cache_leader); + if (w->cache_leader && w->cache_leader != client_win && + w->cache_leader != w->tree_ref) { + auto parent = wm_ref_toplevel_of(w->cache_leader); + auto wp = parent ? wm_ref_deref(parent) : NULL; if (wp) { // Dead loop? if (recursions > WIN_GET_LEADER_MAX_RECURSION) { @@ -1763,14 +1584,10 @@ static xcb_window_t win_get_leader_raw(session_t *ps, struct managed_win *w, int * Retrieve the WM_CLASS of a window and update its * win structure. */ -bool win_update_class(struct x_connection *c, struct atom *atoms, struct managed_win *w) { +bool win_update_class(struct x_connection *c, struct atom *atoms, struct win *w) { char **strlst = NULL; int nstr = 0; - - // Can't do anything if there's no client window - if (!w->client_win) { - return false; - } + auto client_win = win_client_id(w, /*fallback_to_self=*/true); // Free and reset old strings free(w->class_instance); @@ -1779,7 +1596,7 @@ bool win_update_class(struct x_connection *c, struct atom *atoms, struct managed w->class_general = NULL; // Retrieve the property string list - if (!wid_get_text_prop(c, atoms, w->client_win, atoms->aWM_CLASS, &strlst, &nstr)) { + if (!wid_get_text_prop(c, atoms, client_win, atoms->aWM_CLASS, &strlst, &nstr)) { return false; } @@ -1792,9 +1609,8 @@ bool win_update_class(struct x_connection *c, struct atom *atoms, struct managed free(strlst); - log_trace("(%#010x): client = %#010x, " - "instance = \"%s\", general = \"%s\"", - w->base.id, w->client_win, w->class_instance, w->class_general); + log_trace("(%#010x): client = %#010x, instance = \"%s\", general = \"%s\"", + win_id(w), client_win, w->class_instance, w->class_general); return true; } @@ -1802,15 +1618,15 @@ bool win_update_class(struct x_connection *c, struct atom *atoms, struct managed /** * Handle window focus change. */ -static void win_on_focus_change(session_t *ps, struct managed_win *w) { +static void win_on_focus_change(session_t *ps, struct win *w) { // If window grouping detection is enabled if (ps->o.track_leader) { - xcb_window_t leader = win_get_leader(ps, w); + auto leader = win_get_leader(ps, w); // If the window gets focused, replace the old active_leader auto active_leader = wm_active_leader(ps->wm); if (win_is_focused_raw(w) && leader != active_leader) { - xcb_window_t active_leader_old = active_leader; + auto active_leader_old = active_leader; wm_set_active_leader(ps->wm, leader); @@ -1831,9 +1647,9 @@ static void win_on_focus_change(session_t *ps, struct managed_win *w) { // Send D-Bus signal if (ps->o.dbus) { if (win_is_focused_raw(w)) { - cdbus_ev_win_focusin(session_get_cdbus(ps), &w->base); + cdbus_ev_win_focusin(session_get_cdbus(ps), w); } else { - cdbus_ev_win_focusout(session_get_cdbus(ps), &w->base); + cdbus_ev_win_focusout(session_get_cdbus(ps), w); } } } @@ -1841,7 +1657,7 @@ static void win_on_focus_change(session_t *ps, struct managed_win *w) { /** * Set real focused state of a window. */ -void win_set_focused(session_t *ps, struct managed_win *w) { +void win_set_focused(session_t *ps, struct win *w) { // Unmapped windows will have their focused state reset on map if (w->a.map_state != XCB_MAP_STATE_VIEWABLE) { return; @@ -1870,7 +1686,7 @@ void win_set_focused(session_t *ps, struct managed_win *w) { * Note w->shadow and shadow geometry must be correct before calling this * function. */ -void win_extents(const struct managed_win *w, region_t *res) { +void win_extents(const struct win *w, region_t *res) { pixman_region32_clear(res); pixman_region32_union_rect(res, res, w->g.x, w->g.y, (uint)w->widthb, (uint)w->heightb); @@ -1889,8 +1705,8 @@ gen_by_val(win_extents); * * Mark the window shape as updated */ -void win_update_bounding_shape(struct x_connection *c, struct managed_win *w, - bool shape_exists, bool detect_rounded_corners) { +void win_update_bounding_shape(struct x_connection *c, struct win *w, bool shape_exists, + bool detect_rounded_corners) { // We don't handle property updates of non-visible windows until they are // mapped. assert(w->state == WSTATE_MAPPED); @@ -1900,7 +1716,7 @@ void win_update_bounding_shape(struct x_connection *c, struct managed_win *w, win_get_region_local(w, &w->bounding_shape); if (shape_exists) { - w->bounding_shaped = win_bounding_shaped(c, w->base.id); + w->bounding_shaped = win_bounding_shaped(c, win_id(w)); } // Only request for a bounding region if the window is shaped @@ -1912,7 +1728,7 @@ void win_update_bounding_shape(struct x_connection *c, struct managed_win *w, */ xcb_shape_get_rectangles_reply_t *r = xcb_shape_get_rectangles_reply( - c->c, xcb_shape_get_rectangles(c->c, w->base.id, XCB_SHAPE_SK_BOUNDING), + c->c, xcb_shape_get_rectangles(c->c, win_id(w), XCB_SHAPE_SK_BOUNDING), NULL); if (!r) { @@ -1952,33 +1768,37 @@ void win_update_bounding_shape(struct x_connection *c, struct managed_win *w, /** * Reread opacity property of a window. */ -void win_update_opacity_prop(struct x_connection *c, struct atom *atoms, - struct managed_win *w, bool detect_client_opacity) { +void win_update_opacity_prop(struct x_connection *c, struct atom *atoms, struct win *w, + bool detect_client_opacity) { // get frame opacity first w->has_opacity_prop = - wid_get_opacity_prop(c, atoms, w->base.id, OPAQUE, &w->opacity_prop); + wid_get_opacity_prop(c, atoms, win_id(w), OPAQUE, &w->opacity_prop); if (w->has_opacity_prop) { // opacity found return; } - if (detect_client_opacity && w->client_win && w->base.id == w->client_win) { - // checking client opacity not allowed + auto client_win = wm_ref_client_of(w->tree_ref); + if (!detect_client_opacity || client_win == NULL) { return; } // get client opacity - w->has_opacity_prop = - wid_get_opacity_prop(c, atoms, w->client_win, OPAQUE, &w->opacity_prop); + w->has_opacity_prop = wid_get_opacity_prop(c, atoms, wm_ref_win_id(client_win), + OPAQUE, &w->opacity_prop); } /** * Retrieve frame extents from a window. */ -void win_update_frame_extents(struct x_connection *c, struct atom *atoms, - struct managed_win *w, xcb_window_t client, - double frame_opacity) { +void win_update_frame_extents(struct x_connection *c, struct atom *atoms, struct win *w, + xcb_window_t client, double frame_opacity) { + if (client == XCB_NONE) { + w->frame_extents = (margin_t){0}; + return; + } + winprop_t prop = x_get_prop(c, client, atoms->a_NET_FRAME_EXTENTS, 4L, XCB_ATOM_CARDINAL, 32); @@ -2012,18 +1832,19 @@ void win_update_frame_extents(struct x_connection *c, struct atom *atoms, } } - log_trace("(%#010x): %d, %d, %d, %d", w->base.id, w->frame_extents.left, + log_trace("(%#010x): %d, %d, %d, %d", win_id(w), w->frame_extents.left, w->frame_extents.right, w->frame_extents.top, w->frame_extents.bottom); free_winprop(&prop); } -bool win_is_region_ignore_valid(session_t *ps, const struct managed_win *w) { - win_stack_foreach_managed(i, wm_stack_end(ps->wm)) { +bool win_is_region_ignore_valid(session_t *ps, const struct win *w) { + wm_stack_foreach(ps->wm, cursor) { + auto i = wm_ref_deref(cursor); if (i == w) { break; } - if (!i->reg_ignore_valid) { + if (i != NULL && !i->reg_ignore_valid) { return false; } } @@ -2032,131 +1853,90 @@ bool win_is_region_ignore_valid(session_t *ps, const struct managed_win *w) { /// Finish the destruction of a window (e.g. after fading has finished). /// Frees `w` -void destroy_win_finish(session_t *ps, struct win *w) { - log_debug("Trying to finish destroying (%#010x)", w->id); - - auto next_w = wm_stack_next_managed(ps->wm, &w->stack_neighbour); - list_remove(&w->stack_neighbour); - - if (w->managed) { - auto mw = (struct managed_win *)w; - unmap_win_finish(ps, mw); - - // Unmapping might preserve the shadow image, so free it here - win_release_shadow(ps->backend_data, mw); - win_release_mask(ps->backend_data, mw); - - // Invalidate reg_ignore of windows below this one - // TODO(yshui) what if next_w is not mapped?? - /* TODO(yshui) seriously figure out how reg_ignore behaves. - * I think if `w` is unmapped, and destroyed after - * paint happened at least once, w->reg_ignore_valid would - * be true, and there is no need to invalid w->next->reg_ignore - * when w is destroyed. */ - if (next_w) { - rc_region_unref(&next_w->reg_ignore); - next_w->reg_ignore_valid = false; - } - - if (mw == wm_active_win(ps->wm)) { - // Usually, the window cannot be the focused at - // destruction. FocusOut should be generated before the - // window is destroyed. We do this check just to be - // completely sure we don't have dangling references. - log_debug("window %#010x (%s) is destroyed while being " - "focused", - w->id, mw->name); - wm_set_active_win(ps->wm, NULL); - } - - free_win_res(ps, mw); - - // Drop w from all prev_trans to avoid accessing freed memory in - // repair_win() - // TODO(yshui) there can only be one prev_trans pointing to w - win_stack_foreach_managed(w2, wm_stack_end(ps->wm)) { - if (mw == w2->prev_trans) { - w2->prev_trans = NULL; - } +void win_destroy_finish(session_t *ps, struct win *w) { + log_debug("Trying to finish destroying (%#010x)", win_id(w)); + + unmap_win_finish(ps, w); + + // Unmapping might preserve the shadow image, so free it here + win_release_shadow(ps->backend_data, w); + win_release_mask(ps->backend_data, w); + + if (w == wm_active_win(ps->wm)) { + // Usually, the window cannot be the focused at + // destruction. FocusOut should be generated before the + // window is destroyed. We do this check just to be + // completely sure we don't have dangling references. + log_debug("window %#010x (%s) is destroyed while being " + "focused", + win_id(w), w->name); + wm_set_active_win(ps->wm, NULL); + } + + free_win_res(ps, w); + + // Drop w from all prev_trans to avoid accessing freed memory in + // repair_win() + // TODO(yshui) there can only be one prev_trans pointing to w + wm_stack_foreach(ps->wm, cursor) { + auto w2 = wm_ref_deref(cursor); + if (w2 != NULL && w == w2->prev_trans) { + w2->prev_trans = NULL; } } + wm_reap_zombie(w->tree_ref); free(w); } /// Start destroying a window. Windows cannot always be destroyed immediately /// because of fading and such. -void destroy_win_start(session_t *ps, struct win *w) { - assert(w); - - // A toplevel window is destroyed, all of its children lose their - // subwin status. - wm_subwin_remove_and_unsubscribe_for_toplevel(ps->wm, &ps->c, w->id); - - auto mw = (struct managed_win *)w; - log_debug("Destroying %#010x \"%s\", managed = %d", w->id, - (w->managed ? mw->name : NULL), w->managed); - - // Delete destroyed window from the hash table, even though the window - // might still be rendered for a while. We need to make sure future window - // with the same window id won't confuse us. Keep the window in the window - // stack if it's managed and mapped, since we might still need to render - // it (e.g. fading out). Window will be removed from the stack when it - // finishes destroying. - wm_remove(ps->wm, w); - - if (w->managed) { - if (mw->state != WSTATE_UNMAPPED) { - // Only UNMAPPED state has window resources freed, - // otherwise we need to call unmap_win_finish to free - // them. - log_warn("Did X server not unmap window %#010x before destroying " - "it?", - w->id); - } - // Clear IMAGES_STALE flags since the window is destroyed: Clear - // PIXMAP_STALE as there is no pixmap available anymore, so STALE - // doesn't make sense. - // XXX Clear SHADOW_STALE as setting/clearing flags on a destroyed - // window doesn't work leading to an inconsistent state where the - // shadow is refreshed but the flags are stuck in STALE. Do this - // before changing the window state to destroying - win_clear_flags(mw, WIN_FLAGS_PIXMAP_STALE); - - // If size/shape/position information is stale, - // win_process_update_flags will update them and add the new - // window extents to damage. Since the window has been destroyed, - // we cannot get the complete information at this point, so we - // just add what we currently have to the damage. - if (win_check_flags_any(mw, WIN_FLAGS_SIZE_STALE | WIN_FLAGS_POSITION_STALE)) { - add_damage_from_win(ps, mw); - } - - if (win_check_flags_all(mw, WIN_FLAGS_CLIENT_STALE)) { - mw->client_win = mw->base.id; - log_debug("(%#010x): client self (%s)", mw->base.id, - (mw->a.override_redirect ? "override-redirected" : "wmwin")); - } +void win_destroy_start(session_t *ps, struct win *w) { + BUG_ON(w == NULL); + log_debug("Destroying %#010x (%s)", win_id(w), w->name); + + if (w->state != WSTATE_UNMAPPED) { + // Only UNMAPPED state has window resources freed, + // otherwise we need to call unmap_win_finish to free + // them. + log_warn("Did X server not unmap window %#010x before destroying " + "it?", + win_id(w)); + } + // Clear IMAGES_STALE flags since the window is destroyed: Clear + // PIXMAP_STALE as there is no pixmap available anymore, so STALE + // doesn't make sense. + // XXX Clear SHADOW_STALE as setting/clearing flags on a destroyed + // window doesn't work leading to an inconsistent state where the + // shadow is refreshed but the flags are stuck in STALE. Do this + // before changing the window state to destroying + win_clear_flags(w, WIN_FLAGS_PIXMAP_STALE); + + // If size/shape/position information is stale, + // win_process_update_flags will update them and add the new + // window extents to damage. Since the window has been destroyed, + // we cannot get the complete information at this point, so we + // just add what we currently have to the damage. + if (win_check_flags_any(w, WIN_FLAGS_SIZE_STALE | WIN_FLAGS_POSITION_STALE)) { + add_damage_from_win(ps, w); + } - // Clear some flags about stale window information. Because now - // the window is destroyed, we can't update them anyway. - win_clear_flags(mw, WIN_FLAGS_SIZE_STALE | WIN_FLAGS_POSITION_STALE | - WIN_FLAGS_PROPERTY_STALE | - WIN_FLAGS_FACTOR_CHANGED | WIN_FLAGS_CLIENT_STALE); + // Clear some flags about stale window information. Because now + // the window is destroyed, we can't update them anyway. + win_clear_flags(w, WIN_FLAGS_SIZE_STALE | WIN_FLAGS_POSITION_STALE | + WIN_FLAGS_PROPERTY_STALE | WIN_FLAGS_FACTOR_CHANGED); - // Update state flags of a managed window - mw->state = WSTATE_DESTROYED; - mw->a.map_state = XCB_MAP_STATE_UNMAPPED; - mw->in_openclose = true; - } + // Update state flags of a managed window + w->state = WSTATE_DESTROYED; + w->a.map_state = XCB_MAP_STATE_UNMAPPED; + w->in_openclose = true; } -void unmap_win_start(struct managed_win *w) { +void unmap_win_start(struct win *w) { assert(w); - assert(w->base.managed); assert(w->a._class != XCB_WINDOW_CLASS_INPUT_ONLY); - log_debug("Unmapping %#010x \"%s\"", w->base.id, w->name); + log_debug("Unmapping %#010x (%s)", win_id(w), w->name); assert(w->state != WSTATE_DESTROYED); @@ -2175,8 +1955,7 @@ void unmap_win_start(struct managed_win *w) { w->state = WSTATE_UNMAPPED; } -struct win_script_context -win_script_context_prepare(struct session *ps, struct managed_win *w) { +struct win_script_context win_script_context_prepare(struct session *ps, struct win *w) { auto monitor = (w->randr_monitor >= 0 && w->randr_monitor < ps->monitors.count) ? *pixman_region32_extents(&ps->monitors.regions[w->randr_monitor]) @@ -2197,7 +1976,7 @@ win_script_context_prepare(struct session *ps, struct managed_win *w) { return ret; } -double win_animatable_get(const struct managed_win *w, enum win_script_output output) { +double win_animatable_get(const struct win *w, enum win_script_output output) { if (w->running_animation && w->running_animation_outputs[output] >= 0) { return w->running_animation->memory[w->running_animation_outputs[output]]; } @@ -2223,8 +2002,7 @@ double win_animatable_get(const struct managed_win *w, enum win_script_output ou #define WSTATE_PAIR(a, b) ((int)(a) * NUM_OF_WSTATES + (int)(b)) -bool win_process_animation_and_state_change(struct session *ps, struct managed_win *w, - double delta_t) { +bool win_process_animation_and_state_change(struct session *ps, struct win *w, double delta_t) { // If the window hasn't ever been damaged yet, it won't be rendered in this frame. // And if it is also unmapped/destroyed, it won't receive damage events. In this // case we can skip its animation. For mapped windows, we need to provisionally @@ -2246,7 +2024,7 @@ bool win_process_animation_and_state_change(struct session *ps, struct managed_w if (w->running_animation == NULL) { return false; } - log_verbose("Advance animation for %#010x (%s) %f seconds", w->base.id, + log_verbose("Advance animation for %#010x (%s) %f seconds", win_id(w), w->name, delta_t); if (!script_instance_is_finished(w->running_animation)) { w->running_animation->elapsed += delta_t; @@ -2281,13 +2059,13 @@ bool win_process_animation_and_state_change(struct session *ps, struct managed_w if (ps->o.dbus) { switch (w->state) { case WSTATE_UNMAPPED: - cdbus_ev_win_unmapped(session_get_cdbus(ps), &w->base); + cdbus_ev_win_unmapped(session_get_cdbus(ps), w); break; case WSTATE_MAPPED: - cdbus_ev_win_mapped(session_get_cdbus(ps), &w->base); + cdbus_ev_win_mapped(session_get_cdbus(ps), w); break; case WSTATE_DESTROYED: - cdbus_ev_win_destroyed(session_get_cdbus(ps), &w->base); + cdbus_ev_win_destroyed(session_get_cdbus(ps), w); break; } } @@ -2328,7 +2106,7 @@ bool win_process_animation_and_state_change(struct session *ps, struct managed_w (w->running_animation_suppressions & (1 << trigger)) != 0) { log_debug("Not starting animation %s for window %#010x (%s) because it " "is being suppressed.", - animation_trigger_names[trigger], w->base.id, w->name); + animation_trigger_names[trigger], win_id(w), w->name); goto advance_animation; } @@ -2337,7 +2115,7 @@ bool win_process_animation_and_state_change(struct session *ps, struct managed_w } log_debug("Starting animation %s for window %#010x (%s)", - animation_trigger_names[trigger], w->base.id, w->name); + animation_trigger_names[trigger], win_id(w), w->name); auto new_animation = script_instance_new(ps->o.animations[trigger].script); if (w->running_animation) { @@ -2355,7 +2133,7 @@ bool win_process_animation_and_state_change(struct session *ps, struct managed_w // TODO(absolutelynothelix): rename to x_update_win_(randr_?)monitor and move to // the x.c. -void win_update_monitor(struct x_monitors *monitors, struct managed_win *mw) { +void win_update_monitor(struct x_monitors *monitors, struct win *mw) { mw->randr_monitor = -1; for (int i = 0; i < monitors->count; i++) { auto e = pixman_region32_extents(&monitors->regions[i]); @@ -2364,18 +2142,18 @@ void win_update_monitor(struct x_monitors *monitors, struct managed_win *mw) { mw->randr_monitor = i; log_debug("Window %#010x (%s), %dx%d+%dx%d, is entirely on the " "monitor %d (%dx%d+%dx%d)", - mw->base.id, mw->name, mw->g.x, mw->g.y, mw->widthb, + win_id(mw), mw->name, mw->g.x, mw->g.y, mw->widthb, mw->heightb, i, e->x1, e->y1, e->x2 - e->x1, e->y2 - e->y1); return; } } log_debug("Window %#010x (%s), %dx%d+%dx%d, is not entirely on any monitor", - mw->base.id, mw->name, mw->g.x, mw->g.y, mw->widthb, mw->heightb); + win_id(mw), mw->name, mw->g.x, mw->g.y, mw->widthb, mw->heightb); } /// Start the mapping of a window. We cannot map immediately since we might need to fade /// the window in. -static void map_win_start(struct managed_win *w) { +void win_map_start(struct win *w) { assert(w); // Don't care about window mapping if it's an InputOnly window @@ -2384,7 +2162,7 @@ static void map_win_start(struct managed_win *w) { return; } - log_debug("Mapping (%#010x \"%s\"), old state %d", w->base.id, w->name, w->state); + log_debug("Mapping (%#010x \"%s\"), old state %d", win_id(w), w->name, w->state); assert(w->state != WSTATE_DESTROYED); if (w->state == WSTATE_MAPPED) { @@ -2406,17 +2184,15 @@ static void map_win_start(struct managed_win *w) { // Update window mode here to check for ARGB windows w->mode = win_calc_mode(w); - log_debug("Window (%#010x) has type %s", w->base.id, WINTYPES[w->window_type].name); - w->state = WSTATE_MAPPED; - win_set_flags(w, WIN_FLAGS_PIXMAP_STALE); + win_set_flags(w, WIN_FLAGS_PIXMAP_STALE | WIN_FLAGS_CLIENT_STALE); } /// Set flags on a window. Some sanity checks are performed -void win_set_flags(struct managed_win *w, uint64_t flags) { - log_verbose("Set flags %" PRIu64 " to window %#010x (%s)", flags, w->base.id, w->name); +void win_set_flags(struct win *w, uint64_t flags) { + log_verbose("Set flags %" PRIu64 " to window %#010x (%s)", flags, win_id(w), w->name); if (unlikely(w->state == WSTATE_DESTROYED)) { - log_error("Flags set on a destroyed window %#010x (%s)", w->base.id, w->name); + log_error("Flags set on a destroyed window %#010x (%s)", win_id(w), w->name); return; } @@ -2424,19 +2200,19 @@ void win_set_flags(struct managed_win *w, uint64_t flags) { } /// Clear flags on a window. Some sanity checks are performed -void win_clear_flags(struct managed_win *w, uint64_t flags) { - log_verbose("Clear flags %" PRIu64 " from window %#010x (%s)", flags, w->base.id, +void win_clear_flags(struct win *w, uint64_t flags) { + log_verbose("Clear flags %" PRIu64 " from window %#010x (%s)", flags, win_id(w), w->name); if (unlikely(w->state == WSTATE_DESTROYED)) { log_warn("Flags %" PRIu64 " cleared on a destroyed window %#010x (%s)", - flags, w->base.id, w->name); + flags, win_id(w), w->name); return; } w->flags = w->flags & (~flags); } -void win_set_properties_stale(struct managed_win *w, const xcb_atom_t *props, int nprops) { +void win_set_properties_stale(struct win *w, const xcb_atom_t *props, int nprops) { auto const bits_per_element = sizeof(*w->stale_props) * 8; size_t new_capacity = w->stale_props_capacity; @@ -2466,12 +2242,12 @@ void win_set_properties_stale(struct managed_win *w, const xcb_atom_t *props, in win_set_flags(w, WIN_FLAGS_PROPERTY_STALE); } -static void win_clear_all_properties_stale(struct managed_win *w) { +static void win_clear_all_properties_stale(struct win *w) { memset(w->stale_props, 0, w->stale_props_capacity * sizeof(*w->stale_props)); win_clear_flags(w, WIN_FLAGS_PROPERTY_STALE); } -static bool win_fetch_and_unset_property_stale(struct managed_win *w, xcb_atom_t prop) { +static bool win_fetch_and_unset_property_stale(struct win *w, xcb_atom_t prop) { auto const bits_per_element = sizeof(*w->stale_props) * 8; if (prop >= w->stale_props_capacity * bits_per_element) { return false; @@ -2483,11 +2259,11 @@ static bool win_fetch_and_unset_property_stale(struct managed_win *w, xcb_atom_t return ret; } -bool win_check_flags_any(struct managed_win *w, uint64_t flags) { +bool win_check_flags_any(struct win *w, uint64_t flags) { return (w->flags & flags) != 0; } -bool win_check_flags_all(struct managed_win *w, uint64_t flags) { +bool win_check_flags_all(struct win *w, uint64_t flags) { return (w->flags & flags) == flags; } @@ -2496,7 +2272,7 @@ bool win_check_flags_all(struct managed_win *w, uint64_t flags) { * * It's not using w->border_size for performance measures. */ -void win_update_is_fullscreen(const session_t *ps, struct managed_win *w) { +void win_update_is_fullscreen(const session_t *ps, struct win *w) { if (!ps->o.no_ewmh_fullscreen && w->is_ewmh_fullscreen) { w->is_fullscreen = true; return; @@ -2512,11 +2288,12 @@ void win_update_is_fullscreen(const session_t *ps, struct managed_win *w) { * * TODO(yshui) cache this property */ -bool win_is_bypassing_compositor(const session_t *ps, const struct managed_win *w) { +bool win_is_bypassing_compositor(const session_t *ps, const struct win *w) { bool ret = false; + auto wid = win_client_id(w, /*fallback_to_self=*/true); - auto prop = x_get_prop(&ps->c, w->client_win, ps->atoms->a_NET_WM_BYPASS_COMPOSITOR, - 1L, XCB_ATOM_CARDINAL, 32); + auto prop = x_get_prop(&ps->c, wid, ps->atoms->a_NET_WM_BYPASS_COMPOSITOR, 1L, + XCB_ATOM_CARDINAL, 32); if (prop.nitems && *prop.c32 == 1) { ret = true; @@ -2530,6 +2307,6 @@ bool win_is_bypassing_compositor(const session_t *ps, const struct managed_win * * Check if a window is focused, without using any focus rules or forced focus * settings */ -bool win_is_focused_raw(const struct managed_win *w) { +bool win_is_focused_raw(const struct win *w) { return w->a.map_state == XCB_MAP_STATE_VIEWABLE && w->is_ewmh_focused; } diff --git a/src/wm/win.h b/src/wm/win.h index 284e40c7a9..d82a0fb90b 100644 --- a/src/wm/win.h +++ b/src/wm/win.h @@ -20,20 +20,26 @@ #include "transition/script.h" #include "utils/list.h" #include "utils/misc.h" +#include "wm/wm.h" #include "x.h" #include "xcb/xproto.h" struct backend_base; typedef struct session session_t; typedef struct _glx_texture glx_texture_t; +struct wm_cursor; -#define win_stack_foreach_managed(w, win_stack) \ - list_foreach(struct managed_win, w, win_stack, \ - base.stack_neighbour) if ((w)->base.managed) +#define wm_stack_foreach(wm, i) \ + for (struct wm_ref * (i) = wm_ref_topmost_child(wm_root_ref(wm)); (i); \ + (i) = wm_ref_below(i)) +#define wm_stack_foreach_rev(wm, i) \ + for (struct wm_ref * (i) = wm_ref_bottommost_child(wm_root_ref(wm)); (i); \ + (i) = wm_ref_above(i)) -#define win_stack_foreach_managed_safe(w, win_stack) \ - list_foreach_safe(struct managed_win, w, win_stack, \ - base.stack_neighbour) if ((w)->base.managed) +#define wm_stack_foreach_safe(wm, i, next_i) \ + for (struct wm_ref * (i) = wm_ref_topmost_child(wm_root_ref(wm)), \ + *(next_i) = (i) != NULL ? wm_ref_below(i) : NULL; \ + (i); (i) = (next_i), (next_i) = (i) != NULL ? wm_ref_below(i) : NULL) #ifdef CONFIG_OPENGL // FIXME this type should be in opengl.h @@ -73,25 +79,6 @@ struct window_stack_entry { * considered part of the window. */ -/// Structure representing a top-level managed window. -struct win { - UT_hash_handle hh; - struct list_node stack_neighbour; - /// ID of the top-level frame window. - xcb_window_t id; - /// Generation of the window. - /// (see `struct wm::generation` for explanation of what a generation is) - uint64_t generation; - /// Whether the window is destroyed from Xorg's perspective - bool destroyed : 1; - /// True if we just received CreateNotify, and haven't queried X for any info - /// about the window - bool is_new : 1; - /// True if this window is managed, i.e. this struct is actually a `managed_win`. - /// Always false if `is_new` is true. - bool managed : 1; -}; - struct win_geometry { int16_t x; int16_t y; @@ -106,22 +93,20 @@ struct win_state_change { winstate_t state; }; -struct managed_win { - struct win base; +struct win { + /// Reference back to the position of this window inside the window tree. + struct wm_ref *tree_ref; /// backend data attached to this window. Only available when /// `state` is not UNMAPPED image_handle win_image; image_handle shadow_image; image_handle mask_image; + // TODO(yshui) only used by legacy backends, remove. /// Pointer to the next higher window to paint. - struct managed_win *prev_trans; - /// Number of windows above this window - int stacking_rank; + struct win *prev_trans; // TODO(yshui) rethink reg_ignore // Core members - /// The "mapped state" of this window, doesn't necessary - /// match X mapped state, because of fading. winstate_t state; /// Window attributes. xcb_get_window_attributes_reply_t a; @@ -184,14 +169,12 @@ struct managed_win { bool in_openclose; // Client window related members - /// ID of the top-level client window of the window. - xcb_window_t client_win; /// Type of the window. wintype_t window_type; /// Leader window ID of the window. xcb_window_t leader; - /// Cached topmost window ID of the window. - xcb_window_t cache_leader; + /// Cached topmost window ID of the leader window. + struct wm_ref *cache_leader; // Focus-related members /// Whether the window is to be considered focused. @@ -357,47 +340,46 @@ static const struct script_output_info win_script_outputs[] = { /// section. Returns true if the window had an animation running and it has just finished, /// or if the window's states just changed and there is no animation defined for this /// state change. -bool win_process_animation_and_state_change(struct session *ps, struct managed_win *w, - double delta_t); -double win_animatable_get(const struct managed_win *w, enum win_script_output output); -void win_process_update_flags(session_t *ps, struct managed_win *w); -void win_process_image_flags(session_t *ps, struct managed_win *w); +bool win_process_animation_and_state_change(struct session *ps, struct win *w, double delta_t); +double win_animatable_get(const struct win *w, enum win_script_output output); +void win_process_update_flags(session_t *ps, struct win *w); +void win_process_image_flags(session_t *ps, struct win *w); /// Start the unmap of a window. We cannot unmap immediately since we might need to fade /// the window out. -void unmap_win_start(struct managed_win *); -void unmap_win_finish(session_t *ps, struct managed_win *w); +void unmap_win_start(struct win *); +void unmap_win_finish(session_t *ps, struct win *w); /// Start the destroying of a window. Windows cannot always be destroyed immediately /// because of fading and such. -void destroy_win_start(session_t *ps, struct win *w); - +void win_destroy_start(session_t *ps, struct win *w); +void win_map_start(struct win *w); /// Release images bound with a window, set the *_NONE flags on the window. Only to be /// used when de-initializing the backend outside of win.c -void win_release_images(struct backend_base *base, struct managed_win *w); -winmode_t attr_pure win_calc_mode_raw(const struct managed_win *w); +void win_release_images(struct backend_base *base, struct win *w); +winmode_t attr_pure win_calc_mode_raw(const struct win *w); // TODO(yshui) `win_calc_mode` is only used by legacy backends -winmode_t attr_pure win_calc_mode(const struct managed_win *w); -void win_set_shadow_force(session_t *ps, struct managed_win *w, switch_t val); -void win_set_fade_force(struct managed_win *w, switch_t val); -void win_set_focused_force(session_t *ps, struct managed_win *w, switch_t val); -void win_set_invert_color_force(session_t *ps, struct managed_win *w, switch_t val); +winmode_t attr_pure win_calc_mode(const struct win *w); +void win_set_shadow_force(session_t *ps, struct win *w, switch_t val); +void win_set_fade_force(struct win *w, switch_t val); +void win_set_focused_force(session_t *ps, struct win *w, switch_t val); +void win_set_invert_color_force(session_t *ps, struct win *w, switch_t val); /** * Set real focused state of a window. */ -void win_set_focused(session_t *ps, struct managed_win *w); -void win_on_factor_change(session_t *ps, struct managed_win *w); -void win_unmark_client(struct managed_win *w); +void win_set_focused(session_t *ps, struct win *w); +void win_on_factor_change(session_t *ps, struct win *w); +void win_on_client_update(session_t *ps, struct win *w); -bool attr_pure win_should_dim(session_t *ps, const struct managed_win *w); +bool attr_pure win_should_dim(session_t *ps, const struct win *w); -void win_update_monitor(struct x_monitors *monitors, struct managed_win *mw); +void win_update_monitor(struct x_monitors *monitors, struct win *mw); /// Recheck if a window is fullscreen -void win_update_is_fullscreen(const session_t *ps, struct managed_win *w); +void win_update_is_fullscreen(const session_t *ps, struct win *w); /** * Check if a window has BYPASS_COMPOSITOR property set */ -bool win_is_bypassing_compositor(const session_t *ps, const struct managed_win *w); +bool win_is_bypassing_compositor(const session_t *ps, const struct win *w); /** * Get a rectangular region in global coordinates a window (and possibly * its shadow) occupies. @@ -405,114 +387,112 @@ bool win_is_bypassing_compositor(const session_t *ps, const struct managed_win * * Note w->shadow and shadow geometry must be correct before calling this * function. */ -void win_extents(const struct managed_win *w, region_t *res); -region_t win_extents_by_val(const struct managed_win *w); +void win_extents(const struct win *w, region_t *res); +region_t win_extents_by_val(const struct win *w); /** * Add a window to damaged area. * * @param ps current session * @param w struct _win element representing the window */ -void add_damage_from_win(session_t *ps, const struct managed_win *w); +void add_damage_from_win(session_t *ps, const struct win *w); /** * Get a rectangular region a window occupies, excluding frame and shadow. * * Return region in global coordinates. */ -void win_get_region_noframe_local(const struct managed_win *w, region_t *); -void win_get_region_noframe_local_without_corners(const struct managed_win *w, region_t *); +void win_get_region_noframe_local(const struct win *w, region_t *); +void win_get_region_noframe_local_without_corners(const struct win *w, region_t *); /// Get the region for the frame of the window -void win_get_region_frame_local(const struct managed_win *w, region_t *res); +void win_get_region_frame_local(const struct win *w, region_t *res); /// Get the region for the frame of the window, by value -region_t win_get_region_frame_local_by_val(const struct managed_win *w); +region_t win_get_region_frame_local_by_val(const struct win *w); /// Query the Xorg for information about window `win` /// `win` pointer might become invalid after this function returns -struct win *attr_ret_nonnull maybe_allocate_managed_win(session_t *ps, struct win *win); +struct win *win_maybe_allocate(session_t *ps, struct wm_ref *cursor); /** * Release a destroyed window that is no longer needed. */ -void destroy_win_finish(session_t *ps, struct win *w); +void win_destroy_finish(session_t *ps, struct win *w); /** * Check if a window is focused, without using any focus rules or forced focus settings */ -bool attr_pure win_is_focused_raw(const struct managed_win *w); +bool attr_pure win_is_focused_raw(const struct win *w); /// check if window has ARGB visual -bool attr_pure win_has_alpha(const struct managed_win *w); +bool attr_pure win_has_alpha(const struct win *w); /// Whether it looks like a WM window. We consider a window WM window if /// it does not have a decedent with WM_STATE and it is not override- /// redirected itself. -static inline bool attr_pure win_is_wmwin(const struct managed_win *w) { - return w->base.id == w->client_win && !w->a.override_redirect; +static inline bool attr_pure win_is_wmwin(const struct win *w) { + return wm_ref_client_of(w->tree_ref) == NULL && !w->a.override_redirect; } -static inline struct managed_win *win_as_managed(struct win *w) { - BUG_ON(!w->managed); - return (struct managed_win *)w; +static inline xcb_window_t win_id(const struct win *w) { + return wm_ref_win_id(w->tree_ref); } -static inline const char *win_get_name_if_managed(const struct win *w) { - if (!w->managed) { - return "(unmanaged)"; +/// Returns the client window of a window. If a client window does not exist, returns the +/// window itself when `fallback_to_self` is true, otherwise returns XCB_NONE. +static inline xcb_window_t win_client_id(const struct win *w, bool fallback_to_self) { + auto client_win = wm_ref_client_of(w->tree_ref); + if (client_win == NULL) { + return fallback_to_self ? win_id(w) : XCB_NONE; } - auto mw = (struct managed_win *)w; - return mw->name; + return wm_ref_win_id(client_win); } /// check if reg_ignore_valid is true for all windows above us -bool attr_pure win_is_region_ignore_valid(session_t *ps, const struct managed_win *w); +bool attr_pure win_is_region_ignore_valid(session_t *ps, const struct win *w); /// Whether a given window is mapped on the X server side -bool win_is_mapped_in_x(const struct managed_win *w); +bool win_is_mapped_in_x(const struct win *w); /// Set flags on a window. Some sanity checks are performed -void win_set_flags(struct managed_win *w, uint64_t flags); +void win_set_flags(struct win *w, uint64_t flags); /// Clear flags on a window. Some sanity checks are performed -void win_clear_flags(struct managed_win *w, uint64_t flags); +void win_clear_flags(struct win *w, uint64_t flags); /// Returns true if any of the flags in `flags` is set -bool win_check_flags_any(struct managed_win *w, uint64_t flags); +bool win_check_flags_any(struct win *w, uint64_t flags); /// Returns true if all of the flags in `flags` are set -bool win_check_flags_all(struct managed_win *w, uint64_t flags); +bool win_check_flags_all(struct win *w, uint64_t flags); /// Mark properties as stale for a window -void win_set_properties_stale(struct managed_win *w, const xcb_atom_t *prop, int nprops); +void win_set_properties_stale(struct win *w, const xcb_atom_t *prop, int nprops); -xcb_window_t win_get_client_window(struct x_connection *c, struct wm *wm, - struct atom *atoms, const struct managed_win *w); -bool win_update_wintype(struct x_connection *c, struct atom *atoms, struct managed_win *w); +bool win_update_wintype(struct x_connection *c, struct atom *atoms, struct win *w); /** * Retrieve frame extents from a window. */ -void win_update_frame_extents(struct x_connection *c, struct atom *atoms, - struct managed_win *w, xcb_window_t client, - double frame_opacity); +void win_update_frame_extents(struct x_connection *c, struct atom *atoms, struct win *w, + xcb_window_t client, double frame_opacity); /** * Retrieve the WM_CLASS of a window and update its * win structure. */ -bool win_update_class(struct x_connection *c, struct atom *atoms, struct managed_win *w); -int win_update_role(struct x_connection *c, struct atom *atoms, struct managed_win *w); -int win_update_name(struct x_connection *c, struct atom *atoms, struct managed_win *w); -void win_on_win_size_change(struct managed_win *w, int shadow_offset_x, - int shadow_offset_y, int shadow_radius); -void win_update_bounding_shape(struct x_connection *c, struct managed_win *w, - bool shape_exists, bool detect_rounded_corners); +bool win_update_class(struct x_connection *c, struct atom *atoms, struct win *w); +int win_update_role(struct x_connection *c, struct atom *atoms, struct win *w); +int win_update_name(struct x_connection *c, struct atom *atoms, struct win *w); +void win_on_win_size_change(struct win *w, int shadow_offset_x, int shadow_offset_y, + int shadow_radius); +void win_update_bounding_shape(struct x_connection *c, struct win *w, bool shape_exists, + bool detect_rounded_corners); bool win_update_prop_fullscreen(struct x_connection *c, const struct atom *atoms, - struct managed_win *w); + struct win *w); -static inline attr_unused void win_set_property_stale(struct managed_win *w, xcb_atom_t prop) { +static inline attr_unused void win_set_property_stale(struct win *w, xcb_atom_t prop) { return win_set_properties_stale(w, (xcb_atom_t[]){prop}, 1); } /// Free all resources in a struct win -void free_win_res(session_t *ps, struct managed_win *w); +void free_win_res(session_t *ps, struct win *w); /// Remove the corners of window `w` from region `res`. `origin` is the top-left corner of /// `w` in `res`'s coordinate system. static inline void -win_region_remove_corners(const struct managed_win *w, ivec2 origin, region_t *res) { +win_region_remove_corners(const struct win *w, ivec2 origin, region_t *res) { static const int corner_index[][2] = { {0, 0}, {0, 1}, @@ -535,12 +515,11 @@ win_region_remove_corners(const struct managed_win *w, ivec2 origin, region_t *r } /// Like `win_region_remove_corners`, but `origin` is (0, 0). -static inline void -win_region_remove_corners_local(const struct managed_win *w, region_t *res) { +static inline void win_region_remove_corners_local(const struct win *w, region_t *res) { win_region_remove_corners(w, (ivec2){0, 0}, res); } -static inline region_t attr_unused win_get_bounding_shape_global_by_val(struct managed_win *w) { +static inline region_t attr_unused win_get_bounding_shape_global_by_val(struct win *w) { region_t ret; pixman_region32_init(&ret); pixman_region32_copy(&ret, &w->bounding_shape); @@ -548,8 +527,7 @@ static inline region_t attr_unused win_get_bounding_shape_global_by_val(struct m return ret; } -static inline region_t -win_get_bounding_shape_global_without_corners_by_val(struct managed_win *w) { +static inline region_t win_get_bounding_shape_global_without_corners_by_val(struct win *w) { region_t ret; pixman_region32_init(&ret); pixman_region32_copy(&ret, &w->bounding_shape); @@ -562,7 +540,7 @@ win_get_bounding_shape_global_without_corners_by_val(struct managed_win *w) { * Calculate the extents of the frame of the given window based on EWMH * _NET_FRAME_EXTENTS and the X window border width. */ -static inline margin_t attr_pure attr_unused win_calc_frame_extents(const struct managed_win *w) { +static inline margin_t attr_pure attr_unused win_calc_frame_extents(const struct win *w) { margin_t result = w->frame_extents; result.top = max2(result.top, w->g.border_width); result.left = max2(result.left, w->g.border_width); @@ -574,7 +552,7 @@ static inline margin_t attr_pure attr_unused win_calc_frame_extents(const struct /** * Check whether a window has WM frames. */ -static inline bool attr_pure attr_unused win_has_frame(const struct managed_win *w) { +static inline bool attr_pure attr_unused win_has_frame(const struct win *w) { return w->g.border_width || w->frame_extents.top || w->frame_extents.left || w->frame_extents.right || w->frame_extents.bottom; } diff --git a/src/wm/wm.c b/src/wm/wm.c index 935cb6ee3a..413df84d89 100644 --- a/src/wm/wm.c +++ b/src/wm/wm.c @@ -1,366 +1,429 @@ // SPDX-License-Identifier: MPL-2.0 // Copyright (c) Yuxuan Shui +#include #include #include +#include "common.h" #include "log.h" +#include "utils/dynarr.h" #include "utils/list.h" -#include "utils/uthash_extra.h" #include "x.h" #include "win.h" #include "wm.h" +#include "wm_internal.h" struct wm { - /// Current window generation, start from 1. 0 is reserved for using as - /// an invalid generation number. - /// - /// Because X server recycles window IDs, `id` along - /// is not enough to uniquely identify a window. This generation number is - /// incremented every time a window is destroyed, so that if a window ID is - /// reused, its generation number will be different from before. - /// Unless, of course, if the generation number overflows, but since we are - /// using a uint64_t here, that won't happen for a very long time. Still, - /// it is recommended that you restart the compositor at least once before - /// the Universe collapse back on itself. - uint64_t generation; - /// A hash table of all windows. - struct win *windows; - /// Windows in their stacking order - struct list_node window_stack; /// Pointer to win of current active window. Used by /// EWMH _NET_ACTIVE_WINDOW focus detection. In theory, /// it's more reliable to store the window ID directly here, just in /// case the WM does something extraordinary, but caching the pointer /// means another layer of complexity. - struct managed_win *active_win; + struct win *active_win; /// Window ID of leader window of currently active window. Used for /// subsidiary window detection. - xcb_window_t active_leader; - struct subwin *subwins; + struct wm_tree_node *active_leader; + struct wm_tree tree; + + struct wm_tree_node *root; + + /// Incomplete imports. See `wm_import_incomplete` for an explanation. + /// This is a dynarr. + struct wm_tree_node **incompletes; + /// Tree nodes that we have chosen to forget, but we might still receive some + /// events from, we keep them here to ignore those events. + struct wm_tree_node **masked; +}; + +// TODO(yshui): this is a bit weird and I am not decided on it yet myself. Maybe we can +// expose `wm_tree_node` directly. But maybe we want to bundle some additional data with +// it. Anyway, this is probably easy to get rid of if we want to. +/// A wrapper of `wm_tree_node`. This points to the `siblings` `struct list_node` in a +/// `struct wm_tree_node`. +struct wm_ref { + struct list_node inner; }; +static_assert(offsetof(struct wm_ref, inner) == 0, "wm_cursor should be usable as a " + "wm_tree_node"); +static_assert(alignof(struct wm_ref) == alignof(struct list_node), + "wm_cursor should have the same alignment as wm_tree_node"); + +static inline const struct wm_tree_node *to_tree_node(const struct wm_ref *cursor) { + return cursor != NULL ? list_entry(&cursor->inner, struct wm_tree_node, siblings) + : NULL; +} + +static inline struct wm_tree_node *to_tree_node_mut(struct wm_ref *cursor) { + return cursor != NULL ? list_entry(&cursor->inner, struct wm_tree_node, siblings) + : NULL; +} -unsigned int wm_get_window_count(struct wm *wm) { - unsigned int count = 0; - HASH_ITER2(wm->windows, w) { - assert(!w->destroyed); - ++count; +xcb_window_t wm_ref_win_id(const struct wm_ref *cursor) { + return to_tree_node(cursor)->id.x; +} + +wm_treeid wm_ref_treeid(const struct wm_ref *cursor) { + return to_tree_node(cursor)->id; +} + +struct win *wm_ref_deref(const struct wm_ref *cursor) { + auto node = to_tree_node(cursor); + if (node->parent == NULL) { + log_error("Trying to dereference a root node. Expect malfunction."); + return NULL; } - return count; + if (node->parent->parent != NULL) { + // Don't return the client window if this is not a toplevel node. This + // saves us from needing to clear `->win` when a window is reparented. + return NULL; + } + return node->win; +} + +void wm_ref_set(struct wm_ref *cursor, struct win *w) { + to_tree_node_mut(cursor)->win = w; +} + +static ptrdiff_t wm_find_masked(struct wm *wm, xcb_window_t wid) { + dynarr_foreach(wm->masked, m) { + if ((*m)->id.x == wid) { + return m - wm->masked; + } + } + return -1; +} + +bool wm_is_wid_masked(struct wm *wm, xcb_window_t wid) { + return wm_find_masked(wm, wid) != -1; } -struct managed_win *wm_active_win(struct wm *wm) { +struct win *wm_active_win(struct wm *wm) { return wm->active_win; } -void wm_set_active_win(struct wm *wm, struct managed_win *w) { +void wm_set_active_win(struct wm *wm, struct win *w) { wm->active_win = w; } -xcb_window_t wm_active_leader(struct wm *wm) { - return wm->active_leader; +struct wm_ref *wm_active_leader(struct wm *wm) { + return wm->active_leader != NULL ? (struct wm_ref *)&wm->active_leader->siblings : NULL; } -void wm_set_active_leader(struct wm *wm, xcb_window_t leader) { - wm->active_leader = leader; +void wm_set_active_leader(struct wm *wm, struct wm_ref *leader) { + wm->active_leader = to_tree_node_mut(leader); } -struct win *wm_stack_next(struct wm *wm, struct list_node *cursor) { - if (!list_node_is_last(&wm->window_stack, cursor)) { - return list_entry(cursor->next, struct win, stack_neighbour); - } - return NULL; +bool wm_ref_is_zombie(const struct wm_ref *cursor) { + return to_tree_node(cursor)->is_zombie; } -// Find the managed window immediately below `i` in the window stack -struct managed_win * -wm_stack_next_managed(const struct wm *wm, const struct list_node *cursor) { - while (!list_node_is_last(&wm->window_stack, cursor)) { - auto next = list_entry(cursor->next, struct win, stack_neighbour); - if (next->managed) { - return (struct managed_win *)next; - } - cursor = &next->stack_neighbour; - } - return NULL; +struct wm_ref *wm_ref_below(const struct wm_ref *cursor) { + return &to_tree_node(cursor)->parent->children != cursor->inner.next + ? (struct wm_ref *)cursor->inner.next + : NULL; } -/// Find a managed window from window id in window linked list of the session. -struct win *wm_find(struct wm *wm, xcb_window_t id) { - if (!id) { - return NULL; - } +struct wm_ref *wm_ref_above(const struct wm_ref *cursor) { + return &to_tree_node(cursor)->parent->children != cursor->inner.prev + ? (struct wm_ref *)cursor->inner.prev + : NULL; +} - struct win *w = NULL; - HASH_FIND_INT(wm->windows, &id, w); - assert(w == NULL || !w->destroyed); - return w; +struct wm_ref *wm_root_ref(const struct wm *wm) { + return (struct wm_ref *)&wm->root->siblings; } -void wm_remove(struct wm *wm, struct win *w) { - wm->generation++; - HASH_DEL(wm->windows, w); +struct wm_ref *wm_ref_topmost_child(const struct wm_ref *cursor) { + auto node = to_tree_node(cursor); + return !list_is_empty(&node->children) ? (struct wm_ref *)node->children.next : NULL; } -int wm_foreach(struct wm *wm, int (*func)(struct win *, void *), void *data) { - HASH_ITER2(wm->windows, w) { - assert(!w->destroyed); - int ret = func(w, data); - if (ret) { - return ret; - } - } - return 0; -} - -void wm_stack_replace(struct wm *wm, struct win *old, struct win *new_) { - list_replace(&old->stack_neighbour, &new_->stack_neighbour); - struct win *replaced = NULL; - HASH_REPLACE_INT(wm->windows, id, new_, replaced); - assert(replaced == old); - free(old); -} - -/// Insert a new window after list_node `prev` -/// New window will be in unmapped state -static struct win * -wm_stack_insert_after(struct wm *wm, xcb_window_t id, struct list_node *prev) { - log_debug("Adding window %#010x", id); - struct win *old_w = NULL; - HASH_FIND_INT(wm->windows, &id, old_w); - assert(old_w == NULL); - - auto new_w = cmalloc(struct win); - list_insert_after(prev, &new_w->stack_neighbour); - new_w->id = id; - new_w->managed = false; - new_w->is_new = true; - new_w->destroyed = false; - new_w->generation = wm->generation; - - HASH_ADD_INT(wm->windows, id, new_w); - return new_w; -} - -struct win *wm_stack_add_top(struct wm *wm, xcb_window_t id) { - return wm_stack_insert_after(wm, id, &wm->window_stack); -} - -struct win *wm_stack_add_above(struct wm *wm, xcb_window_t id, xcb_window_t below) { - struct win *w = NULL; - HASH_FIND_INT(wm->windows, &below, w); - if (!w) { - if (!list_is_empty(&wm->window_stack)) { - // `below` window is not found even if the window stack is - // not empty - return NULL; - } - return wm_stack_add_top(wm, id); - } - // we found something from the hash table, so if the stack is - // empty, we are in an inconsistent state. - assert(!list_is_empty(&wm->window_stack)); - return wm_stack_insert_after(wm, id, w->stack_neighbour.prev); +struct wm_ref *wm_ref_bottommost_child(const struct wm_ref *cursor) { + auto node = to_tree_node(cursor); + return !list_is_empty(&node->children) ? (struct wm_ref *)node->children.prev : NULL; } -/// Move window `w` so it's before `next` in the list -static inline void wm_stack_move_before(struct wm *wm, struct win *w, struct list_node *next) { - struct managed_win *mw = NULL; - if (w->managed) { - mw = (struct managed_win *)w; - } +struct wm_ref *wm_find(const struct wm *wm, xcb_window_t id) { + auto node = wm_tree_find(&wm->tree, id); + return node != NULL ? (struct wm_ref *)&node->siblings : NULL; +} - if (mw) { - // This invalidates all reg_ignore below the new stack position of - // `w` - mw->reg_ignore_valid = false; - rc_region_unref(&mw->reg_ignore); - - // This invalidates all reg_ignore below the old stack position of - // `w` - auto next_w = wm_stack_next_managed(wm, &w->stack_neighbour); - if (next_w) { - next_w->reg_ignore_valid = false; - rc_region_unref(&next_w->reg_ignore); - } - } +struct wm_ref *wm_find_by_client(const struct wm *wm, xcb_window_t client) { + auto node = wm_tree_find(&wm->tree, client); + return node != NULL ? (struct wm_ref *)&wm_tree_find_toplevel_for(node)->siblings + : NULL; +} - list_move_before(&w->stack_neighbour, next); +struct wm_ref *wm_ref_toplevel_of(struct wm_ref *cursor) { + return (struct wm_ref *)&wm_tree_find_toplevel_for(to_tree_node_mut(cursor))->siblings; +} -#ifdef DEBUG_RESTACK - log_trace("Window stack modified. Current stack:"); - for (auto c = &wm->window_stack; c; c = c->next) { - const char *desc = ""; - if (c->state == WSTATE_DESTROYING) { - desc = "(D) "; - } - log_trace("%#010x \"%s\" %s", c->id, c->name, desc); - } -#endif +struct wm_ref *wm_ref_client_of(struct wm_ref *cursor) { + auto client = to_tree_node(cursor)->client_window; + return client != NULL ? (struct wm_ref *)&client->siblings : NULL; +} + +void wm_remove(struct wm *wm, struct wm_ref *w) { + wm_tree_destroy_window(&wm->tree, (struct wm_tree_node *)w); } -struct list_node *wm_stack_end(struct wm *wm) { - return &wm->window_stack; +struct wm_ref *wm_stack_end(struct wm *wm) { + return (struct wm_ref *)&wm->root->children; } /// Move window `w` so it's right above `below`, if `below` is 0, `w` is moved /// to the bottom of the stack -void wm_stack_move_above(struct wm *wm, struct win *w, xcb_window_t below) { - xcb_window_t old_below; +void wm_stack_move_to_above(struct wm *wm, struct wm_ref *cursor, struct wm_ref *below) { + wm_tree_move_to_above(&wm->tree, to_tree_node_mut(cursor), to_tree_node_mut(below)); +} - if (!list_node_is_last(&wm->window_stack, &w->stack_neighbour)) { - old_below = list_next_entry(w, stack_neighbour)->id; - } else { - old_below = XCB_NONE; - } - log_debug("Restack %#010x (%s), old_below: %#010x, new_below: %#010x", w->id, - win_get_name_if_managed(w), old_below, below); - - if (old_below != below) { - struct list_node *new_next; - if (!below) { - new_next = &wm->window_stack; - } else { - struct win *tmp_w = NULL; - HASH_FIND_INT(wm->windows, &below, tmp_w); - - if (!tmp_w) { - log_error("Failed to found new below window %#010x.", below); - return; - } +void wm_stack_move_to_end(struct wm *wm, struct wm_ref *cursor, bool to_bottom) { + wm_tree_move_to_end(&wm->tree, to_tree_node_mut(cursor), to_bottom); +} - new_next = &tmp_w->stack_neighbour; +struct wm *wm_new(void) { + auto wm = ccalloc(1, struct wm); + wm_tree_init(&wm->tree); + wm->incompletes = dynarr_new(struct wm_tree_node *, 4); + wm->masked = dynarr_new(struct wm_tree_node *, 8); + return wm; +} + +void wm_free(struct wm *wm) { + // Free all `struct win`s associated with tree nodes, this leaves dangling + // pointers, but we are freeing the tree nodes immediately after, so everything + // is fine (TM). + wm_stack_foreach_safe(wm, i, next) { + auto w = wm_ref_deref(i); + auto tree_node = to_tree_node_mut(i); + free(w); + + if (tree_node->is_zombie) { + // This mainly happens on `session_destroy`, e.g. when there's + // ongoing animations. + log_debug("Leftover zombie node for window %#010x", tree_node->id.x); + wm_tree_reap_zombie(tree_node); } - wm_stack_move_before(wm, w, new_next); } + wm_tree_clear(&wm->tree); + dynarr_free_pod(wm->incompletes); + dynarr_free_pod(wm->masked); + + free(wm); } -void wm_stack_move_to_top(struct wm *wm, struct win *w) { - if (&w->stack_neighbour == wm->window_stack.next) { - // already at top +void wm_destroy(struct wm *wm, xcb_window_t wid) { + auto masked = wm_find_masked(wm, wid); + if (masked != -1) { + free(wm->masked[masked]); + dynarr_remove_swap(wm->masked, (size_t)masked); return; } - wm_stack_move_before(wm, w, wm->window_stack.next); -} -unsigned wm_stack_num_managed_windows(const struct wm *wm) { - unsigned count = 0; - list_foreach(struct win, w, &wm->window_stack, stack_neighbour) { - if (w->managed) { - count += 1; - } + struct wm_tree_node *node = wm_tree_find(&wm->tree, wid); + if (!node) { + log_error("Trying to destroy window %#010x, but it's not in the tree. " + "Expect malfunction.", + wid); + return; } - return count; -} -struct managed_win *wm_find_by_client(struct wm *wm, xcb_window_t client) { - if (!client) { - return NULL; + auto index = dynarr_find_pod(wm->incompletes, node); + if (index != -1) { + dynarr_remove_swap(wm->incompletes, (size_t)index); } - HASH_ITER2(wm->windows, w) { - assert(!w->destroyed); - if (!w->managed) { - continue; - } + wm_tree_destroy_window(&wm->tree, node); +} - auto mw = (struct managed_win *)w; - if (mw->client_win == client) { - return mw; +void wm_reap_zombie(struct wm_ref *zombie) { + wm_tree_reap_zombie(to_tree_node_mut(zombie)); +} + +void wm_reparent(struct wm *wm, xcb_window_t wid, xcb_window_t parent) { + auto window = wm_tree_find(&wm->tree, wid); + auto new_parent = wm_tree_find(&wm->tree, parent); + // We delete the window here if parent is not found, or if the parent is + // an incomplete import. We will rediscover this window later in + // `wm_complete_import`. Keeping it around will only confuse us. + bool should_forget = + new_parent == NULL || dynarr_find_pod(wm->incompletes, new_parent) != -1; + if (window == NULL) { + log_debug("Reparenting window %#010x which is not in our tree. Assuming " + "it came from fog of war.", + wid); + if (!should_forget) { + wm_import_incomplete(wm, wid, parent); + } + return; + } + if (should_forget) { + log_debug("Window %#010x reparented to window %#010x which is " + "%s, forgetting and masking instead.", + window->id.x, parent, + new_parent == NULL ? "not in our tree" : "an incomplete import"); + + wm_tree_detach(&wm->tree, window); + for (auto curr = window; curr != NULL;) { + auto next = wm_tree_next(curr, window); + HASH_DEL(wm->tree.nodes, curr); + auto incomplete_index = dynarr_find_pod(wm->incompletes, curr); + if (incomplete_index != -1) { + dynarr_remove_swap(wm->incompletes, (size_t)incomplete_index); + // Incomplete imports cannot have children. + assert(list_is_empty(&curr->children)); + + // Incomplete imports won't have event masks set, so we + // don't need to mask them. + free(curr); + } else { + dynarr_push(wm->masked, curr); + } + curr = next; } + return; } - return NULL; + wm_tree_reparent(&wm->tree, window, new_parent); } -struct managed_win *wm_find_managed(struct wm *wm, xcb_window_t id) { - struct win *w = wm_find(wm, id); - if (!w || !w->managed) { - return NULL; - } - - auto mw = (struct managed_win *)w; - assert(mw->state != WSTATE_DESTROYED); - return mw; +void wm_set_has_wm_state(struct wm *wm, struct wm_ref *cursor, bool has_wm_state) { + wm_tree_set_wm_state(&wm->tree, to_tree_node_mut(cursor), has_wm_state); } -unsigned wm_num_windows(const struct wm *wm) { - return HASH_COUNT(wm->windows); +void wm_import_incomplete(struct wm *wm, xcb_window_t wid, xcb_window_t parent) { + auto masked = wm_find_masked(wm, wid); + if (masked != -1) { + // A new window created with the same wid means the window we chose to + // forget has been deleted (without us knowing), and its ID was then + // reused. + free(wm->masked[masked]); + dynarr_remove_swap(wm->masked, (size_t)masked); + } + auto parent_cursor = NULL; + if (parent != XCB_NONE) { + parent_cursor = wm_tree_find(&wm->tree, parent); + if (parent_cursor == NULL) { + log_error("Importing window %#010x, but its parent %#010x is not " + "in our tree, ignoring. Expect malfunction.", + wid, parent); + return; + } + } + log_debug("Importing window %#010x with parent %#010x", wid, parent); + auto new = wm_tree_new_window(&wm->tree, wid, parent_cursor); + dynarr_push(wm->incompletes, new); + if (parent == XCB_NONE) { + BUG_ON(wm->root != NULL); // Can't have more than one root + wm->root = new; + } +} +static const xcb_event_mask_t WM_IMPORT_EV_MASK = + XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE; +static void wm_complete_import_single(struct wm *wm, struct x_connection *c, + struct atom *atoms, struct wm_tree_node *node) { + log_debug("Finishing importing window %#010x with parent %#010lx.", node->id.x, + node->parent != NULL ? node->parent->id.x : XCB_NONE); + set_cant_fail_cookie( + c, xcb_change_window_attributes(c->c, node->id.x, XCB_CW_EVENT_MASK, + (const uint32_t[]){WM_IMPORT_EV_MASK})); + if (wid_has_prop(c->c, node->id.x, atoms->aWM_STATE)) { + wm_tree_set_wm_state(&wm->tree, node, true); + } } -struct subwin *wm_subwin_add_and_subscribe(struct wm *wm, struct x_connection *c, - xcb_window_t id, xcb_window_t parent) { - struct subwin *subwin = NULL; - HASH_FIND_INT(wm->subwins, &id, subwin); - BUG_ON(subwin != NULL); +static void wm_complete_import_subtree(struct wm *wm, struct x_connection *c, + struct atom *atoms, struct wm_tree_node *subroot) { + wm_complete_import_single(wm, c, atoms, subroot); - subwin = ccalloc(1, struct subwin); - subwin->id = id; - subwin->toplevel = parent; - HASH_ADD_INT(wm->subwins, id, subwin); + for (auto curr = subroot; curr != NULL; curr = wm_tree_next(curr, subroot)) { + // Surprise! This newly imported window might already have children. + // Although we haven't setup SubstructureNotify for it yet, it's still + // possible for another window to be reparented to it. - log_debug("Allocated subwin %p for window %#010x, toplevel %#010x, total: %d", - subwin, id, parent, HASH_COUNT(wm->subwins)); - XCB_AWAIT_VOID(xcb_change_window_attributes, c->c, id, XCB_CW_EVENT_MASK, - (const uint32_t[]){XCB_EVENT_MASK_PROPERTY_CHANGE}); - return subwin; -} + xcb_query_tree_reply_t *tree = XCB_AWAIT(xcb_query_tree, c->c, curr->id.x); + if (!tree) { + log_error("Disappearing window subtree rooted at %#010x. Expect " + "malfunction.", + curr->id.x); + continue; + } -struct subwin *wm_subwin_find(struct wm *wm, xcb_window_t id) { - struct subwin *subwin = NULL; - HASH_FIND_INT(wm->subwins, &id, subwin); - return subwin; + auto children = xcb_query_tree_children(tree); + auto children_len = xcb_query_tree_children_length(tree); + for (int i = 0; i < children_len; i++) { + // `children` goes from bottom to top, and `wm_tree_new_window` + // puts the new window at the top of the stacking order, which + // means the windows will naturally be in the correct stacking + // order. + auto existing = wm_tree_find(&wm->tree, children[i]); + if (existing != NULL) { + // This should never happen: we haven't subscribed to + // child creation events yet, and any window reparented to + // an incomplete is deleted. report an error and try to + // recover. + auto index = dynarr_find_pod(wm->incompletes, existing); + if (index != -1) { + dynarr_remove_swap(wm->incompletes, (size_t)index); + } + log_error("Window %#010x already exists in the tree, but " + "it appeared again as a child of window " + "%#010x. Deleting the old one, but expect " + "malfunction.", + children[i], curr->id.x); + wm_tree_destroy_window(&wm->tree, existing); + } + existing = wm_tree_new_window(&wm->tree, children[i], curr); + wm_complete_import_single(wm, c, atoms, existing); + } + free(tree); + } } -void wm_subwin_remove(struct wm *wm, struct subwin *subwin) { - log_debug("Freeing subwin %p for window %#010x, toplevel %#010x", subwin, - subwin->id, subwin->toplevel); - HASH_DEL(wm->subwins, subwin); - free(subwin); -} +void wm_complete_import(struct wm *wm, struct x_connection *c, struct atom *atoms) { + // Unveil the fog of war + dynarr_foreach(wm->masked, m) { + free(*m); + } + dynarr_clear_pod(wm->masked); -void wm_subwin_remove_and_unsubscribe(struct wm *wm, struct x_connection *c, - struct subwin *subwin) { - log_debug("Freeing subwin %p for window %#010x", subwin, subwin->id); - XCB_AWAIT_VOID(xcb_change_window_attributes, c->c, subwin->id, XCB_CW_EVENT_MASK, - (const uint32_t[]){0}); - wm_subwin_remove(wm, subwin); -} -void wm_subwin_remove_and_unsubscribe_for_toplevel(struct wm *wm, struct x_connection *c, - xcb_window_t toplevel) { - struct subwin *subwin, *next_subwin; - HASH_ITER(hh, wm->subwins, subwin, next_subwin) { - if (subwin->toplevel == toplevel) { - wm_subwin_remove_and_unsubscribe(wm, c, subwin); - } + while (!dynarr_is_empty(wm->incompletes)) { + auto i = dynarr_pop(wm->incompletes); + // This function modifies `wm->incompletes`, so we can't use + // `dynarr_foreach`. + wm_complete_import_subtree(wm, c, atoms, i); } } -struct wm *wm_new(void) { - auto wm = ccalloc(1, struct wm); - list_init_head(&wm->window_stack); - wm->generation = 1; - return wm; +bool wm_has_incomplete_imports(const struct wm *wm) { + return !dynarr_is_empty(wm->incompletes); } -void wm_free(struct wm *wm, struct x_connection *c) { - list_foreach_safe(struct win, w, &wm->window_stack, stack_neighbour) { - if (w->managed) { - XCB_AWAIT_VOID(xcb_change_window_attributes, c->c, w->id, - XCB_CW_EVENT_MASK, (const uint32_t[]){0}); - } - if (!w->destroyed) { - HASH_DEL(wm->windows, w); - } - free(w); - } - list_init_head(&wm->window_stack); +bool wm_has_tree_changes(const struct wm *wm) { + return !list_is_empty(&wm->tree.changes); +} - struct subwin *subwin, *next_subwin; - HASH_ITER(hh, wm->subwins, subwin, next_subwin) { - wm_subwin_remove_and_unsubscribe(wm, c, subwin); +struct wm_change wm_dequeue_change(struct wm *wm) { + auto tree_change = wm_tree_dequeue_change(&wm->tree); + struct wm_change ret = { + .type = tree_change.type, + .toplevel = NULL, + }; + switch (tree_change.type) { + case WM_TREE_CHANGE_CLIENT: + ret.client.old = tree_change.client.old; + ret.client.new_ = tree_change.client.new_; + ret.toplevel = (struct wm_ref *)&tree_change.client.toplevel->siblings; + break; + case WM_TREE_CHANGE_TOPLEVEL_KILLED: + ret.toplevel = (struct wm_ref *)&tree_change.killed->siblings; + break; + case WM_TREE_CHANGE_TOPLEVEL_NEW: + ret.toplevel = (struct wm_ref *)&tree_change.new_->siblings; + break; + default: break; } - - free(wm); + return ret; } diff --git a/src/wm/wm.h b/src/wm/wm.h index 13db1ce7af..ededf7355b 100644 --- a/src/wm/wm.h +++ b/src/wm/wm.h @@ -12,6 +12,9 @@ #pragma once +#include +#include + #include #include @@ -20,7 +23,7 @@ #include "compiler.h" struct wm; -struct managed_win; +struct win; struct list_node; struct x_connection; @@ -32,70 +35,175 @@ struct subwin { UT_hash_handle hh; }; +enum wm_tree_change_type { + /// The client window of a toplevel changed + WM_TREE_CHANGE_CLIENT, + /// A toplevel window is killed on the X server side + /// A zombie will be left in its place. + WM_TREE_CHANGE_TOPLEVEL_KILLED, + /// A new toplevel window appeared + WM_TREE_CHANGE_TOPLEVEL_NEW, + + // TODO(yshui): This is a stop-gap measure to make sure we invalidate `reg_ignore` + // of windows. Once we get rid of `reg_ignore`, which is only used by the legacy + // backends, this event should be removed. + // + // (`reg_ignore` is the cached cumulative opaque region of all windows above a + // window in the stacking order. If it actually is performance critical, we + // can probably cache it more cleanly in renderer layout.) + + /// The stacking order of toplevel windows changed. Note, toplevel gone/new + /// changes also imply a restack. + WM_TREE_CHANGE_TOPLEVEL_RESTACKED, + + /// Nothing changed + WM_TREE_CHANGE_NONE, +}; + +typedef struct wm_treeid { + /// The generation of the window ID. This is used to detect if the window ID is + /// reused. Inherited from the wm_tree at cr + uint64_t gen; + /// The X window ID. + xcb_window_t x; + + /// Explicit padding + char padding[4]; +} wm_treeid; + +static const wm_treeid WM_TREEID_NONE = {.gen = 0, .x = XCB_NONE}; + +static_assert(sizeof(wm_treeid) == 16, "wm_treeid size is not 16 bytes"); +static_assert(alignof(wm_treeid) == 8, "wm_treeid alignment is not 8 bytes"); + +struct wm_change { + enum wm_tree_change_type type; + /// The toplevel window this change is about. For + /// `WM_TREE_CHANGE_TOPLEVEL_KILLED`, this is the zombie window left in place of + /// the killed toplevel. For `WM_TREE_CHANGE_TOPLEVEL_RESTACKED`, this is NULL. + struct wm_ref *toplevel; + struct { + wm_treeid old; + wm_treeid new_; + } client; +}; + +/// Reference to a window in the `struct wm`. Most of wm operations operate on wm_refs. If +/// the referenced window is managed, a `struct window` can be retrieved by +/// `wm_ref_deref`. +struct wm_ref; +struct atom; + +static inline bool wm_treeid_eq(wm_treeid a, wm_treeid b) { + return a.gen == b.gen && a.x == b.x; +} + struct wm *wm_new(void); -void wm_free(struct wm *wm, struct x_connection *c); +void wm_free(struct wm *wm); -struct managed_win *wm_active_win(struct wm *wm); -void wm_set_active_win(struct wm *wm, struct managed_win *w); -xcb_window_t wm_active_leader(struct wm *wm); -void wm_set_active_leader(struct wm *wm, xcb_window_t leader); +struct win *wm_active_win(struct wm *wm); +void wm_set_active_win(struct wm *wm, struct win *w); +struct wm_ref *wm_active_leader(struct wm *wm); +void wm_set_active_leader(struct wm *wm, struct wm_ref *leader); // Note: `wm` keeps track of 2 lists of windows. One is the window stack, which includes // all windows that might need to be rendered, which means it would include destroyed -// windows in case they need to be faded out. This list is accessed by `wm_stack_*` series +// windows in case they have close animation. This list is accessed by `wm_stack_*` series // of functions. The other is a hash table of windows, which does not include destroyed // windows. This list is accessed by `wm_find_*`, `wm_foreach`, and `wm_num_windows`. // Adding a window to the window stack also automatically adds it to the hash table. /// Find a window in the hash table from window id. -struct win *wm_find(struct wm *wm, xcb_window_t id); +struct wm_ref *attr_pure wm_find(const struct wm *wm, xcb_window_t id); /// Remove a window from the hash table. -void wm_remove(struct wm *wm, struct win *w); -/// Find a managed window from window id in window linked list of the session. -struct managed_win *wm_find_managed(struct wm *wm, xcb_window_t id); +void wm_remove(struct wm *wm, struct wm_ref *w); // Find the WM frame of a client window. `id` is the client window id. -struct managed_win *wm_find_by_client(struct wm *wm, xcb_window_t client); -/// Call `func` on each toplevel window. `func` should return 0 if the iteration -/// should continue. If it returns anything else, the iteration will stop and the -/// return value will be returned from `wm_foreach`. If the iteration finishes -/// naturally, 0 will be returned. -int wm_foreach(struct wm *wm, int (*func)(struct win *, void *), void *data); -/// Returns the number of windows in the hash table. -unsigned attr_const wm_num_windows(const struct wm *wm); - -/// Returns the cursor past the last window in the stack (the `end`). The window stack is -/// a cyclic linked list, so the next element after `end` is the first element. The `end` -/// itself does not point to a valid window. The address of `end` is stable as long as -/// the `struct wm` itself is not freed. -struct list_node *attr_const wm_stack_end(struct wm *wm); -/// Insert a new win entry at the top of the stack -struct win *wm_stack_add_top(struct wm *wm, xcb_window_t id); -/// Insert a new window above window with id `below`, if there is no window, add -/// to top New window will be in unmapped state -struct win *wm_stack_add_above(struct wm *wm, xcb_window_t id, xcb_window_t below); -// Find the managed window immediately below `i` in the window stack -struct managed_win *attr_pure wm_stack_next_managed(const struct wm *wm, - const struct list_node *cursor); +struct wm_ref *attr_pure wm_find_by_client(const struct wm *wm, xcb_window_t client); +/// Find the toplevel of a window by going up the window tree. +struct wm_ref *attr_pure wm_ref_toplevel_of(struct wm_ref *cursor); +/// Return the client window of a window. Must be called with a cursor to a toplevel. +/// Returns NULL if there is no client window. +struct wm_ref *attr_pure wm_ref_client_of(struct wm_ref *cursor); +/// Find the next window in the window stack. Returns NULL if `cursor` is the last window. +struct wm_ref *attr_pure wm_ref_below(const struct wm_ref *cursor); +struct wm_ref *attr_pure wm_ref_above(const struct wm_ref *cursor); +struct wm_ref *attr_pure wm_root_ref(const struct wm *wm); + +struct wm_ref *attr_pure wm_ref_topmost_child(const struct wm_ref *cursor); +struct wm_ref *attr_pure wm_ref_bottommost_child(const struct wm_ref *cursor); + /// Move window `w` so it's right above `below`, if `below` is 0, `w` is moved /// to the bottom of the stack -void wm_stack_move_above(struct wm *wm, struct win *w, xcb_window_t below); -/// Move window `w` to the bottom of the stack. -static inline void wm_stack_move_to_bottom(struct wm *wm, struct win *w) { - wm_stack_move_above(wm, w, 0); -} +void wm_stack_move_to_above(struct wm *wm, struct wm_ref *cursor, struct wm_ref *below); /// Move window `w` to the top of the stack. -void wm_stack_move_to_top(struct wm *wm, struct win *w); -/// Replace window `old` with `new_` in the stack, also replace the window in the hash -/// table. `old` will be freed. -void wm_stack_replace(struct wm *wm, struct win *old, struct win *new_); -unsigned attr_const wm_stack_num_managed_windows(const struct wm *wm); - -struct subwin *wm_subwin_add_and_subscribe(struct wm *wm, struct x_connection *c, - xcb_window_t id, xcb_window_t parent); -struct subwin *wm_subwin_find(struct wm *wm, xcb_window_t id); -void wm_subwin_remove(struct wm *wm, struct subwin *subwin); -void wm_subwin_remove_and_unsubscribe(struct wm *wm, struct x_connection *c, - struct subwin *subwin); -/// Remove all subwins associated with a toplevel window -void wm_subwin_remove_and_unsubscribe_for_toplevel(struct wm *wm, struct x_connection *c, - xcb_window_t toplevel); +void wm_stack_move_to_end(struct wm *wm, struct wm_ref *cursor, bool to_bottom); + +struct win *attr_pure wm_ref_deref(const struct wm_ref *cursor); +xcb_window_t attr_pure wm_ref_win_id(const struct wm_ref *cursor); +wm_treeid attr_pure wm_ref_treeid(const struct wm_ref *cursor); +/// Assign a window to a cursor. The cursor must not already have a window assigned. +void wm_ref_set(struct wm_ref *cursor, struct win *w); +bool attr_pure wm_ref_is_zombie(const struct wm_ref *cursor); + +/// Destroy a window. Children of this window should already have been destroyed. This +/// will cause a `WM_TREE_CHANGE_TOPLEVEL_KILLED` event to be generated, and a zombie +/// window to be placed where the window was. +void wm_destroy(struct wm *wm, xcb_window_t wid); +/// Remove a zombie window from the window tree. +void wm_reap_zombie(struct wm_ref *zombie); +void wm_reparent(struct wm *wm, xcb_window_t wid, xcb_window_t parent); +void wm_set_has_wm_state(struct wm *wm, struct wm_ref *cursor, bool has_wm_state); + +/// Create a tree node for `wid`, with `parent` as its parent. The parent must already +/// be in the window tree. This function creates a placeholder tree node, without +/// contacting the X server, thus can be called outside of the X critical section. The +/// expectation is that the caller will later call `wm_complete_import` inside the +/// X critical section to complete the import. +/// +/// ## NOTE +/// +/// The reason for this complicated dance is because we want to catch all windows ever +/// created on X server's side. For a newly created windows, we will setup a +/// SubstructureNotify event mask to catch any new windows created under it. But between +/// the time we received the creation event and the time we setup the event mask, if any +/// windows were created under the new window, we will miss them. Therefore we have to +/// scan the new windows in X critical section so they won't change as we scan. +/// +/// On the other hand, we can't push everything to the X critical section, because +/// updating the window stack requires knowledge of all windows in the stack. Think +/// ConfigureNotify, if we don't know about the `above_sibling` window, we don't know +/// where to put the window. So we need to create an incomplete import first. +/// +/// But wait, this is actually way more involved. Because we choose to not set up event +/// masks for incomplete imports (we can also choose otherwise, but that also has its own +/// set of problems), there is a whole subtree of windows we don't know about. And those +/// windows might involve in reparent events. To handle that, we essentially put "fog of +/// war" under any incomplete imports, anything reparented into the fog is lost, and will +/// be rediscovered later during subtree scans. If a window is reparented out of the fog, +/// then it's treated as if a brand new window was created. +/// +/// But wait again, there's more. We can delete "lost" windows on our side and unset event +/// masks, but again because this is racy, we might still receive some events for those +/// windows. So we have to keep a list of "lost" windows, and correctly ignore events sent +/// for them. (And since we are actively ignoring events from these windows, we might as +/// well not unset their event masks, saves us a trip to the X server). +/// +/// (Now you have a glimpse of how much X11 sucks.) +void wm_import_incomplete(struct wm *wm, xcb_window_t wid, xcb_window_t parent); + +/// Check if there are any incomplete imports in the window tree. +bool wm_has_incomplete_imports(const struct wm *wm); + +/// Check if there are tree change events +bool wm_has_tree_changes(const struct wm *wm); + +/// Complete the previous incomplete imports by querying the X server. This function will +/// recursively import all children of previously created placeholders, and add them to +/// the window tree. This function must be called from the X critical section. This +/// function also subscribes to SubstructureNotify events for all newly imported windows, +/// as well as for the (now completed) placeholder windows. +void wm_complete_import(struct wm *wm, struct x_connection *c, struct atom *atoms); + +bool wm_is_wid_masked(struct wm *wm, xcb_window_t wid); + +struct wm_change wm_dequeue_change(struct wm *wm); diff --git a/src/wm/wm_internal.h b/src/wm/wm_internal.h new file mode 100644 index 0000000000..9e33808a9b --- /dev/null +++ b/src/wm/wm_internal.h @@ -0,0 +1,109 @@ +#pragma once + +#include +#include + +#include +#include + +#include "utils/list.h" + +#include "wm.h" + +struct wm_tree { + /// The generation of the wm tree. This number is incremented every time a new + /// window is created. + /// + /// Because X server recycles window IDs, X ID alone is not enough to uniquely + /// identify a window. This generation number is incremented every time a window + /// is created, so even if a window ID is reused, its generation number is + /// guaranteed to be different from before. Unless, of course, the generation + /// number overflows, but since we are using a uint64_t here, that won't happen + /// for a very long time. Still, it is recommended that you restart the compositor + /// at least once before the Universe collapse back on itself. + uint64_t gen; + /// wm tree nodes indexed by their X window ID. + struct wm_tree_node *nodes; + + struct list_node changes; + struct list_node free_changes; +}; + +struct wm_tree_node { + UT_hash_handle hh; + + struct wm_tree_node *parent; + struct win *win; + + struct list_node siblings; + struct list_node children; + + wm_treeid id; + /// The client window. Only a toplevel can have a client window. + struct wm_tree_node *client_window; + + bool has_wm_state : 1; + /// Whether this window exists only on our side. A zombie window is a toplevel + /// that has been destroyed or reparented (i.e. no long a toplevel) on the X + /// server side, but is kept on our side for things like animations. A zombie + /// window cannot be found in the wm_tree hash table. + bool is_zombie : 1; +}; + +/// Describe a change of a toplevel's client window. +/// A `XCB_NONE` in either `old_client` or `new_client` means a missing client window. +/// i.e. if `old_client` is `XCB_NONE`, it means the toplevel window did not have a client +/// window before the change, and if `new_client` is `XCB_NONE`, it means the toplevel +/// window lost its client window after the change. +struct wm_tree_change { + wm_treeid toplevel; + union { + /// Information for `WM_TREE_CHANGE_CLIENT`. + struct { + struct wm_tree_node *toplevel; + /// The old and new client windows. + wm_treeid old, new_; + } client; + /// Information for `WM_TREE_CHANGE_TOPLEVEL_KILLED`. + /// The zombie window left in place of the killed toplevel. + struct wm_tree_node *killed; + struct wm_tree_node *new_; + }; + + enum wm_tree_change_type type; +}; + +/// Free all tree nodes and changes, without generating any change events. Used when +/// shutting down. +void wm_tree_clear(struct wm_tree *tree); +struct wm_tree_node *attr_pure wm_tree_find(const struct wm_tree *tree, xcb_window_t id); +struct wm_tree_node *attr_pure wm_tree_find_toplevel_for(struct wm_tree_node *node); +struct wm_tree_node *attr_pure wm_tree_next(struct wm_tree_node *node, + struct wm_tree_node *subroot); +/// Create a new window node in the tree, with X window ID `id`, and parent `parent`. If +/// `parent` is NULL, the new node will be the root window. Only one root window is +/// permitted, and the root window cannot be destroyed once created, until +/// `wm_tree_clear` is called. If `parent` is not NULL, the new node will be put at the +/// top of the stacking order among its siblings. +struct wm_tree_node * +wm_tree_new_window(struct wm_tree *tree, xcb_window_t id, struct wm_tree_node *parent); +void wm_tree_destroy_window(struct wm_tree *tree, struct wm_tree_node *node); +/// Detach the subtree rooted at `subroot` from `tree`. The subtree root is removed from +/// its parent, and the disconnected tree nodes won't be able to be found via +/// `wm_tree_find`. Relevant events will be generated. +void wm_tree_detach(struct wm_tree *tree, struct wm_tree_node *subroot); +void wm_tree_reparent(struct wm_tree *tree, struct wm_tree_node *node, + struct wm_tree_node *new_parent); +void wm_tree_move_to_above(struct wm_tree *tree, struct wm_tree_node *node, + struct wm_tree_node *other); +/// Move `node` to the top or the bottom of its parent's child window stack. +void wm_tree_move_to_end(struct wm_tree *tree, struct wm_tree_node *node, bool to_bottom); +struct wm_tree_change wm_tree_dequeue_change(struct wm_tree *tree); +void wm_tree_reap_zombie(struct wm_tree_node *zombie); +void wm_tree_set_wm_state(struct wm_tree *tree, struct wm_tree_node *node, bool has_wm_state); + +static inline void wm_tree_init(struct wm_tree *tree) { + tree->nodes = NULL; + list_init_head(&tree->changes); + list_init_head(&tree->free_changes); +}