[metacity] display: ensure that we ignore our own focus events for focus predictions



commit 66a4cb4a49072c44888b8112cb12468c4c7bedae
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Sat Sep 8 18:24:42 2018 +0300

    display: ensure that we ignore our own focus events for focus predictions
    
    When we set the input focus, we first set the predicted window,
    and then try to process focus events. But as FocusOut on the
    existing window comes before FocusIn on the new window, we'll
    see the focus out on the old window and think the focus is going
    to nothing, which makes metacity think the prediction failed.
    
    Fix this by making sure that we ignore focus window changes of our
    own cause when updating the focus window field, by ignoring all
    focus events that have a serial the same as the focus request or
    lower. Note that if metacity doens't make any requests after the
    focus request, this could be racy, as another client could steal
    the focus, but metacity would ignore it as the serial was the same.
    Bump the serial by making a dummy ChangeProperty request to a
    metactiy-controlled window in this case.
    
    Based on mutter commit:
    https://gitlab.gnome.org/GNOME/mutter/commit/7fdfbad6d495ede1632588e528801443846e5f6d

 src/core/atomnames.h |  2 ++
 src/core/display.c   | 59 +++++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 46 insertions(+), 15 deletions(-)
---
diff --git a/src/core/atomnames.h b/src/core/atomnames.h
index 6e142196..bab2630b 100644
--- a/src/core/atomnames.h
+++ b/src/core/atomnames.h
@@ -62,6 +62,8 @@ item(_GTK_FRAME_EXTENTS)
 item(_GNOME_PANEL_ACTION)
 item(_GNOME_PANEL_ACTION_MAIN_MENU)
 item(_GNOME_PANEL_ACTION_RUN_DIALOG)
+item(_METACITY_TIMESTAMP_PING)
+item(_METACITY_FOCUS_SET)
 item(_METACITY_SENTINEL)
 item(_METACITY_VERSION)
 item(WM_CLIENT_MACHINE)
diff --git a/src/core/display.c b/src/core/display.c
index 7f0698e4..f4338da7 100644
--- a/src/core/display.c
+++ b/src/core/display.c
@@ -1210,6 +1210,17 @@ meta_display_get_current_time (MetaDisplay *display)
   return display->current_time;
 }
 
+static Bool
+find_timestamp_predicate (Display  *xdisplay,
+                          XEvent   *ev,
+                          XPointer  arg)
+{
+  MetaDisplay *display = (MetaDisplay *) arg;
+
+  return (ev->type == PropertyNotify &&
+          ev->xproperty.atom == display->atom__METACITY_TIMESTAMP_PING);
+}
+
 /* Get a timestamp, even if it means a roundtrip */
 guint32
 meta_display_get_current_time_roundtrip (MetaDisplay *display)
@@ -1221,17 +1232,13 @@ meta_display_get_current_time_roundtrip (MetaDisplay *display)
     {
       XEvent property_event;
 
-      /* Using the property XA_PRIMARY because it's safe; nothing
-       * would use it as a property. The type doesn't matter.
-       */
-      XChangeProperty (display->xdisplay,
-                       display->timestamp_pinging_window,
-                       XA_PRIMARY, XA_STRING, 8,
-                       PropModeAppend, NULL, 0);
-      XWindowEvent (display->xdisplay,
-                    display->timestamp_pinging_window,
-                    PropertyChangeMask,
-                    &property_event);
+      XChangeProperty (display->xdisplay, display->timestamp_pinging_window,
+                       display->atom__METACITY_TIMESTAMP_PING,
+                       XA_STRING, 8, PropModeAppend, NULL, 0);
+      XIfEvent (display->xdisplay,
+                &property_event,
+                find_timestamp_predicate,
+                (XPointer) display);
       timestamp = property_event.xproperty.time;
     }
 
@@ -1485,6 +1492,7 @@ request_xserver_input_focus_change (MetaDisplay *display,
                                     guint32      timestamp)
 {
   MetaWindow *meta_window;
+  gulong serial;
 
   if (timestamp_too_old (display, &timestamp))
     return;
@@ -1492,14 +1500,35 @@ request_xserver_input_focus_change (MetaDisplay *display,
   meta_window = meta_display_lookup_x_window (display, xwindow);
 
   meta_error_trap_push (display);
-  update_focus_window (display,
-                       meta_window,
-                       XNextRequest (display->xdisplay));
+
+  /* In order for mutter to know that the focus request succeeded, we track
+   * the serial of the "focus request" we made, but if we take the serial
+   * of the XSetInputFocus request, then there's no way to determine the
+   * difference between focus events as a result of the SetInputFocus and
+   * focus events that other clients send around the same time. Ensure that
+   * we know which is which by making two requests that the server will
+   * process at the same time.
+   */
+  XGrabServer (display->xdisplay);
+
+  serial = XNextRequest (display->xdisplay);
 
   XSetInputFocus (display->xdisplay,
                   xwindow,
                   RevertToPointerRoot,
                   timestamp);
+
+  XChangeProperty (display->xdisplay, display->timestamp_pinging_window,
+                   display->atom__METACITY_FOCUS_SET,
+                   XA_STRING, 8, PropModeAppend, NULL, 0);
+
+  XUngrabServer (display->xdisplay);
+  XFlush (display->xdisplay);
+
+  update_focus_window (display,
+                       meta_window,
+                       serial);
+
   meta_error_trap_pop (display);
 
   display->last_focus_time = timestamp;
@@ -1608,7 +1637,7 @@ handle_window_focus_event (MetaDisplay *display,
   else
     g_return_if_reached ();
 
-  if (display->server_focus_serial >= display->focus_serial)
+  if (display->server_focus_serial > display->focus_serial)
     {
       update_focus_window (display, focus_window,
                            display->server_focus_serial);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]