From afa431547b696a82375412a289993a8d0eebdabd Mon Sep 17 00:00:00 2001 From: Olivier Fourdan Date: Wed, 16 Dec 2020 16:51:24 +0100 Subject: [PATCH] core: Make sure focus_default_window() worked The function focus_default_window() optionally takes a MetaWindow argument denoting a window that should not be focused. That function calls focus_ancestor_or_top_window() which in turn calls meta_window_focus() to pass focus to another window. However meta_window_focus() gives no guarantee that the given window will end up being the one focused, and can fail in various and creative ways. If that fails, we could possibly end up with the focus window being the one to avoid, while the caller assumes focus was changed, going as far as asserting that fact like meta_window_unmanage() does. As a result, mutter may abort simply because meta_window_focus() failed to set focus on the expected window. To avoid that issue, check that the focus did not end up on the window that we explicitly did not want, and if that's the case, simply fallback to the default focus window. Closes: https://gitlab.gnome.org/GNOME/mutter/-/issues/862 Part-of: --- src/core/workspace.c | 59 +++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/src/core/workspace.c b/src/core/workspace.c index 1dd8daf22..89f6d369f 100644 --- a/src/core/workspace.c +++ b/src/core/workspace.c @@ -1357,6 +1357,30 @@ find_focusable_ancestor (MetaWindow *window, return TRUE; } +static gboolean +try_to_set_focus_and_check (MetaWindow *window, + MetaWindow *not_this_one, + uint32_t timestamp) +{ + meta_window_focus (window, timestamp); + + /* meta_window_focus() does not guarantee that focus will end up + * where we expect, it can fail for various reasons, better check + * it did not actually changed or even left focus to the window we + * explicitly want to avoid. + */ + if (not_this_one && + meta_display_get_focus_window (window->display) == not_this_one) + { + meta_warning ("Failed to focus window %s while avoiding %s", + window->desc, not_this_one->desc); + + return FALSE; + } + + return TRUE; +} + /* Focus ancestor of not_this_one if there is one */ static void focus_ancestor_or_top_window (MetaWorkspace *workspace, @@ -1390,13 +1414,14 @@ focus_ancestor_or_top_window (MetaWorkspace *workspace, "Focusing %s, ancestor of %s", ancestor->desc, not_this_one->desc); - meta_window_focus (ancestor, timestamp); + if (try_to_set_focus_and_check (ancestor, not_this_one, timestamp)) + { + /* Also raise the window if in click-to-focus */ + if (meta_prefs_get_focus_mode () == G_DESKTOP_FOCUS_MODE_CLICK) + meta_window_raise (ancestor); - /* Also raise the window if in click-to-focus */ - if (meta_prefs_get_focus_mode () == G_DESKTOP_FOCUS_MODE_CLICK) - meta_window_raise (ancestor); - - return; + return; + } } } @@ -1408,19 +1433,19 @@ focus_ancestor_or_top_window (MetaWorkspace *workspace, { meta_topic (META_DEBUG_FOCUS, "Focusing workspace MRU window %s", window->desc); + if (try_to_set_focus_and_check (window, not_this_one, timestamp)) + { + /* Also raise the window if in click-to-focus */ + if (meta_prefs_get_focus_mode () == G_DESKTOP_FOCUS_MODE_CLICK) + meta_window_raise (window); - meta_window_focus (window, timestamp); + return; + } + } - /* Also raise the window if in click-to-focus */ - if (meta_prefs_get_focus_mode () == G_DESKTOP_FOCUS_MODE_CLICK) - meta_window_raise (window); - } - else - { - meta_topic (META_DEBUG_FOCUS, - "No MRU window to focus found; focusing no_focus_window."); - meta_display_unset_input_focus (workspace->display, timestamp); - } + meta_topic (META_DEBUG_FOCUS, + "No MRU window to focus found; focusing no_focus_window."); + meta_display_unset_input_focus (workspace->display, timestamp); } /**