From 7e7c2b0cef9c54f2aa1ca987963c97ee38d2fcc8 Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Thu, 15 Feb 2024 00:07:31 +0000 Subject: [PATCH] win: store focused state in struct managed_win So we don't need the whole session_t just to check if a window is focused. Signed-off-by: Yuxuan Shui --- src/c2.c | 4 +--- src/dbus.c | 4 ++-- src/win.c | 27 +++++++++++++++------------ src/win.h | 4 +++- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/c2.c b/src/c2.c index daee18f7..614c456c 100644 --- a/src/c2.c +++ b/src/c2.c @@ -1383,9 +1383,7 @@ static inline void c2_match_once_leaf(session_t *ps, const struct managed_win *w case C2_L_PFULLSCREEN: predef_target = w->is_fullscreen; break; case C2_L_POVREDIR: predef_target = w->a.override_redirect; break; case C2_L_PARGB: predef_target = win_has_alpha(w); break; - case C2_L_PFOCUSED: - predef_target = win_is_focused_raw(ps, w); - break; + case C2_L_PFOCUSED: predef_target = win_is_focused_raw(w); break; case C2_L_PWMWIN: predef_target = w->wmwin; break; case C2_L_PBSHAPED: predef_target = w->bounding_shaped; break; case C2_L_PROUNDED: predef_target = w->rounded_corners; break; diff --git a/src/dbus.c b/src/dbus.c index 861d424e..f16695b7 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -906,7 +906,7 @@ cdbus_process_window_property_get(session_t *ps, DBusMessage *msg, cdbus_window_ } if (!strcmp("RawFocused", target)) { cdbus_reply(ps, msg, cdbus_append_bool_variant, - (bool[]){win_is_focused_raw(ps, w)}); + (bool[]){win_is_focused_raw(w)}); return true; } @@ -976,7 +976,7 @@ static bool cdbus_process_win_get(session_t *ps, DBusMessage *msg) { cdbus_m_win_get_do(wmwin, cdbus_reply_bool); cdbus_m_win_get_do(leader, cdbus_reply_wid); if (!strcmp("focused_raw", target)) { - cdbus_reply_bool(ps, msg, win_is_focused_raw(ps, w)); + cdbus_reply_bool(ps, msg, win_is_focused_raw(w)); return true; } cdbus_m_win_get_do(fade_force, cdbus_reply_enum); diff --git a/src/win.c b/src/win.c index 76f085d4..a9660227 100644 --- a/src/win.c +++ b/src/win.c @@ -139,10 +139,10 @@ static inline bool attr_pure win_is_real_visible(const struct managed_win *w) { * Update focused state of a window. */ static void win_update_focused(session_t *ps, struct managed_win *w) { - if (UNSET != w->focused_force) { + if (w->focused_force != UNSET) { w->focused = w->focused_force; } else { - w->focused = win_is_focused_raw(ps, w); + w->focused = win_is_focused_raw(w); // Use wintype_focus, and treat WM windows and override-redirected // windows specially @@ -210,7 +210,7 @@ static inline bool group_is_focused(session_t *ps, xcb_window_t leader) { continue; } auto mw = (struct managed_win *)w; - if (win_get_leader(ps, mw) == leader && win_is_focused_raw(ps, mw)) { + if (win_get_leader(ps, mw) == leader && win_is_focused_raw(mw)) { return true; } } @@ -843,7 +843,7 @@ double win_calc_opacity_target(session_t *ps, const struct managed_win *w) { } else { // Respect active_opacity only when the window is physically // focused - if (win_is_focused_raw(ps, w)) { + if (win_is_focused_raw(w)) { opacity = ps->o.active_opacity; } else if (!w->focused) { // Respect inactive_opacity in some cases @@ -1792,7 +1792,7 @@ static inline void win_set_leader(session_t *ps, struct managed_win *w, xcb_wind // 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); - if (win_is_focused_raw(ps, w) && cache_leader_old != cache_leader) { + if (win_is_focused_raw(w) && cache_leader_old != cache_leader) { ps->active_leader = cache_leader; group_on_factor_change(ps, cache_leader_old); @@ -1905,7 +1905,7 @@ static void win_on_focus_change(session_t *ps, struct managed_win *w) { xcb_window_t leader = win_get_leader(ps, w); // If the window gets focused, replace the old active_leader - if (win_is_focused_raw(ps, w) && leader != ps->active_leader) { + if (win_is_focused_raw(w) && leader != ps->active_leader) { xcb_window_t active_leader_old = ps->active_leader; ps->active_leader = leader; @@ -1914,7 +1914,7 @@ static void win_on_focus_change(session_t *ps, struct managed_win *w) { group_on_factor_change(ps, leader); } // If the group get unfocused, remove it from active_leader - else if (!win_is_focused_raw(ps, w) && leader && + else if (!win_is_focused_raw(w) && leader && leader == ps->active_leader && !group_is_focused(ps, leader)) { ps->active_leader = XCB_NONE; group_on_factor_change(ps, leader); @@ -1927,7 +1927,7 @@ static void win_on_focus_change(session_t *ps, struct managed_win *w) { #ifdef CONFIG_DBUS // Send D-Bus signal if (ps->o.dbus) { - if (win_is_focused_raw(ps, w)) { + if (win_is_focused_raw(w)) { cdbus_ev_win_focusin(ps, &w->base); } else { cdbus_ev_win_focusout(ps, &w->base); @@ -1945,15 +1945,18 @@ void win_set_focused(session_t *ps, struct managed_win *w) { return; } - if (win_is_focused_raw(ps, w)) { + if (w->is_ewmh_focused) { + assert(ps->active_win == w); return; } auto old_active_win = ps->active_win; ps->active_win = w; - assert(win_is_focused_raw(ps, w)); + w->is_ewmh_focused = true; if (old_active_win) { + assert(old_active_win->is_ewmh_focused); + old_active_win->is_ewmh_focused = false; win_on_focus_change(ps, old_active_win); } win_on_focus_change(ps, w); @@ -2866,8 +2869,8 @@ 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 session_t *ps, const struct managed_win *w) { - return w->a.map_state == XCB_MAP_STATE_VIEWABLE && ps->active_win == w; +bool win_is_focused_raw(const struct managed_win *w) { + return w->a.map_state == XCB_MAP_STATE_VIEWABLE && w->is_ewmh_focused; } // Find the managed window immediately below `i` in the window stack diff --git a/src/win.h b/src/win.h index f98cb9a8..93f10b15 100644 --- a/src/win.h +++ b/src/win.h @@ -205,6 +205,8 @@ struct managed_win { /// `is_ewmh_fullscreen`, or the windows spatial relation with the /// root window. Which one is used is determined by user configuration. bool is_fullscreen; + /// Whether the window is the EWMH active window. + bool is_ewmh_focused; // Opacity-related members /// Current window opacity. @@ -435,7 +437,7 @@ struct managed_win *find_managed_window_or_parent(session_t *ps, xcb_window_t wi /** * Check if a window is focused, without using any focus rules or forced focus settings */ -bool attr_pure win_is_focused_raw(const session_t *ps, const struct managed_win *w); +bool attr_pure win_is_focused_raw(const struct managed_win *w); /// check if window has ARGB visual bool attr_pure win_has_alpha(const struct managed_win *w);