diff --git a/src/Makefile-tests.am b/src/Makefile-tests.am index e35ad9104..7cfae2371 100644 --- a/src/Makefile-tests.am +++ b/src/Makefile-tests.am @@ -18,6 +18,7 @@ dist_stacking_DATA = \ $(srcdir)/closed-transient-no-input-no-take-focus-parent.metatest \ $(srcdir)/closed-transient-no-input-no-take-focus-parents.metatest \ $(srcdir)/closed-transient-no-input-parent.metatest \ + $(srcdir)/stacking/closed-transient-no-input-parent-delayed-focus-default-cancelled.metatest \ $(srcdir)/tests/stacking/minimized.metatest \ $(srcdir)/tests/stacking/mixed-windows.metatest \ $(srcdir)/tests/stacking/set-parent.metatest \ diff --git a/src/tests/stacking/closed-transient-no-input-parent-delayed-focus-default-cancelled.metatest b/src/tests/stacking/closed-transient-no-input-parent-delayed-focus-default-cancelled.metatest new file mode 100644 index 000000000..38897e388 --- /dev/null +++ b/src/tests/stacking/closed-transient-no-input-parent-delayed-focus-default-cancelled.metatest @@ -0,0 +1,36 @@ +new_client 2 x11 +create 2/1 +show 2/1 + +new_client 1 x11 +create 1/1 +show 1/1 + +create 1/2 csd +set_parent 1/2 1 +accept_focus 1/2 false +show 1/2 + +create 1/3 csd +set_parent 1/3 2 +show 1/3 + +wait +assert_focused 1/3 +assert_stacking 2/1 1/1 1/2 1/3 + +destroy 1/3 +sleep 10 + +assert_focused none +assert_stacking 2/1 1/1 1/2 + +activate 2/1 +wait + +assert_focused 2/1 +assert_stacking 1/1 1/2 2/1 + +sleep 250 +assert_focused 2/1 +assert_stacking 1/1 1/2 2/1 diff --git a/src/tests/stacking/closed-transient-no-input-parent.metatest b/src/tests/stacking/closed-transient-no-input-parent.metatest index 4cadb2350..e0f1dc1e2 100644 --- a/src/tests/stacking/closed-transient-no-input-parent.metatest +++ b/src/tests/stacking/closed-transient-no-input-parent.metatest @@ -1,3 +1,7 @@ +new_client 2 x11 +create 2/1 +show 2/1 + new_client 1 x11 create 1/1 show 1/1 @@ -12,9 +16,15 @@ set_parent 1/3 2 show 1/3 wait -assert_stacking 1/1 1/2 1/3 +assert_focused 1/3 +assert_stacking 2/1 1/1 1/2 1/3 destroy 1/3 +dispatch -wait -assert_stacking 1/1 1/2 +assert_focused none +assert_stacking 2/1 1/1 1/2 + +sleep 250 +assert_focused 1/1 +assert_stacking 2/1 1/1 1/2 diff --git a/src/x11/window-x11.c b/src/x11/window-x11.c index 4b27892ac..d86d35f61 100644 --- a/src/x11/window-x11.c +++ b/src/x11/window-x11.c @@ -734,6 +734,66 @@ request_take_focus (MetaWindow *window, send_icccm_message (window, display->x11_display->atom_WM_TAKE_FOCUS, timestamp); } +typedef struct +{ + MetaWindow *window; + guint32 timestamp; + guint timeout_id; + gulong unmanaged_id; + gulong focused_changed_id; +} MetaWindowX11DelayedFocusData; + +static void +meta_window_x11_delayed_focus_data_free (MetaWindowX11DelayedFocusData *data) +{ + g_signal_handler_disconnect (data->window, data->unmanaged_id); + g_signal_handler_disconnect (data->window->display, data->focused_changed_id); + + if (data->timeout_id) + g_source_remove (data->timeout_id); + + g_free (data); +} + +static gboolean +focus_window_delayed_timeout (gpointer user_data) +{ + MetaWindowX11DelayedFocusData *data = user_data; + MetaWindow *window = data->window; + guint32 timestamp = data->timestamp; + + data->timeout_id = 0; + meta_window_x11_delayed_focus_data_free (data); + + meta_window_focus (window, timestamp); + + return G_SOURCE_REMOVE; +} + +static void +meta_window_x11_maybe_focus_delayed (MetaWindow *window, + guint32 timestamp) +{ + MetaWindowX11DelayedFocusData *data; + + data = g_new0 (MetaWindowX11DelayedFocusData, 1); + data->window = window; + data->timestamp = timestamp; + + data->unmanaged_id = + g_signal_connect_swapped (window, "unmanaged", + G_CALLBACK (meta_window_x11_delayed_focus_data_free), + data); + + data->focused_changed_id = + g_signal_connect_swapped (window->display, "notify::focus-window", + G_CALLBACK (meta_window_x11_delayed_focus_data_free), + data); + + data->timeout_id = g_timeout_add (TAKE_FOCUS_FALLBACK_DELAY_MS, + focus_window_delayed_timeout, data); +} + static void meta_window_x11_focus (MetaWindow *window, guint32 timestamp) @@ -783,13 +843,43 @@ meta_window_x11_focus (MetaWindow *window, * Normally, we want to just leave the focus undisturbed until * the window responds to WM_TAKE_FOCUS, but if we're unmanaging * the current focus window we *need* to move the focus away, so - * we focus the no_focus_window now (and set - * display->focus_window to that) before sending WM_TAKE_FOCUS. + * we focus the no focus window before sending WM_TAKE_FOCUS, + * and eventually the default focus windwo excluding this one, + * if meanwhile we don't get any focus request. */ if (window->display->focus_window != NULL && window->display->focus_window->unmanaging) - meta_x11_display_focus_the_no_focus_window (window->display->x11_display, - timestamp); + { + MetaX11Display *x11_display = window->display->x11_display; + MetaWindow *focus_window = window; + MetaWorkspace *workspace = window->workspace; + MetaStack *stack = workspace->display->stack; + + while (TRUE) + { + focus_window = meta_stack_get_default_focus_window (stack, + workspace, + focus_window); + if (!focus_window) + break; + + if (focus_window->unmanaging) + continue; + + if (focus_window->input) + break; + + if (focus_window->shaded && focus_window->frame) + break; + } + + meta_x11_display_focus_the_no_focus_window (x11_display, + timestamp); + + if (focus_window) + meta_window_x11_maybe_focus_delayed (focus_window, + timestamp); + } } request_take_focus (window, timestamp);