win: remove find_managed_window_or_parent

It has only one caller left, inline it. The only legitimate use of
ancestral search - looking for the focused window.

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2024-03-26 13:41:25 +00:00
parent 65428cada2
commit 3f99ac8abf
No known key found for this signature in database
GPG Key ID: D3A4405BE6CC17F4
3 changed files with 40 additions and 63 deletions

View File

@ -533,24 +533,53 @@ static void recheck_focus(session_t *ps) {
// Determine the currently focused window so we can apply appropriate
// opacity on it
xcb_window_t wid = XCB_NONE;
xcb_get_input_focus_reply_t *reply =
xcb_get_input_focus_reply(ps->c.c, xcb_get_input_focus(ps->c.c), NULL);
xcb_generic_error_t *e = NULL;
auto reply = xcb_get_input_focus_reply(ps->c.c, xcb_get_input_focus(ps->c.c), &e);
if (reply == NULL) {
// Not able to get input focus means very not good things...
log_error_x_error(e, "Failed to get focused window.");
free(e);
return;
}
xcb_window_t wid = reply->focus;
free(reply);
if (reply) {
wid = reply->focus;
free(reply);
if (wid == XCB_NONE || wid == XCB_INPUT_FOCUS_POINTER_ROOT ||
wid == ps->c.screen_info->root) {
// Focus is not on a toplevel.
return;
}
auto w = find_managed_window_or_parent(ps, wid);
// 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;
}
log_trace("%#010" PRIx32 " (%#010lx \"%s\") focused.", wid,
(w ? w->base.id : XCB_NONE), (w ? w->name : NULL));
auto parent = tree->parent;
free(tree);
if (parent == ps->c.screen_info->root) {
break;
}
wid = parent;
}
auto w = find_managed_win(ps, wid);
// And we set the focus state here
if (w) {
log_debug("%#010" PRIx32 " (%#010" PRIx32 " \"%s\") focused.", wid,
w->base.id, w->name);
win_set_focused(ps, w);
return;
} else {
log_warn("Focus window %#010" PRIx32 " not found.", wid);
}
}
@ -1697,14 +1726,7 @@ static void handle_pending_updates(EV_P_ struct session *ps) {
// Process window flags
refresh_windows(ps);
{
auto r = xcb_get_input_focus_reply(
ps->c.c, xcb_get_input_focus(ps->c.c), NULL);
if (!ps->active_win || (r && r->focus != ps->active_win->base.id)) {
recheck_focus(ps);
}
free(r);
}
recheck_focus(ps);
// Process window flags (stale images)
refresh_images(ps);

View File

@ -2640,43 +2640,6 @@ struct managed_win *find_toplevel(session_t *ps, xcb_window_t id) {
return NULL;
}
/**
* Find a managed window that is, or is a parent of `wid`.
*
* @param ps current session
* @param wid window ID
* @return struct _win object of the found window, NULL if not found
*/
struct managed_win *find_managed_window_or_parent(session_t *ps, xcb_window_t wid) {
// TODO(yshui) this should probably be an "update tree", then
// find_toplevel. current approach is a bit more "racy", as the server
// state might be ahead of our state
struct win *w = NULL;
// We traverse through its ancestors to find out the frame
// Using find_win here because if we found a unmanaged window we know
// about, we can stop early.
while (wid && wid != ps->c.screen_info->root && !(w = find_win(ps, wid))) {
// 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.
auto reply =
xcb_query_tree_reply(ps->c.c, xcb_query_tree(ps->c.c, wid), NULL);
if (reply == NULL) {
break;
}
wid = reply->parent;
free(reply);
}
if (w == NULL || !w->managed) {
return NULL;
}
return (struct managed_win *)w;
}
/// Set flags on a window. Some sanity checks are performed
void win_set_flags(struct managed_win *w, uint64_t flags) {
log_debug("Set flags %" PRIu64 " to window %#010x (%s)", flags, w->base.id, w->name);

View File

@ -421,14 +421,6 @@ void win_skip_fading(struct managed_win *w);
struct managed_win *find_managed_win(session_t *ps, xcb_window_t id);
struct win *find_win(session_t *ps, xcb_window_t id);
struct managed_win *find_toplevel(session_t *ps, xcb_window_t id);
/**
* Find a managed window that is, or is a parent of `wid`.
*
* @param ps current session
* @param wid window ID
* @return struct _win object of the found window, NULL if not found
*/
struct managed_win *find_managed_window_or_parent(session_t *ps, xcb_window_t wid);
/**
* Check if a window is focused, without using any focus rules or forced focus settings