[mutter/bilelmoussaoui/keybindings: 1/4] keybindings: Move grab/freeze functions per backend




commit 2a2f8473aa552f6bf00ee51f7376bd99da400c84
Author: Bilal Elmoussaoui <belmouss redhat com>
Date:   Mon Jun 20 10:24:49 2022 +0200

    keybindings: Move grab/freeze functions per backend
    
    By making those functions virtual functions of MetaBackend
    and have the BackendNative implementation no op

 src/backends/meta-backend-private.h | 13 +++++
 src/backends/x11/meta-backend-x11.c | 91 +++++++++++++++++++++++++++++++++++
 src/core/keybindings.c              | 94 +++----------------------------------
 3 files changed, 111 insertions(+), 87 deletions(-)
---
diff --git a/src/backends/meta-backend-private.h b/src/backends/meta-backend-private.h
index e9e94e044d..dbeb03749c 100644
--- a/src/backends/meta-backend-private.h
+++ b/src/backends/meta-backend-private.h
@@ -82,6 +82,19 @@ struct _MetaBackendClass
                               int          device_id,
                               uint32_t     timestamp);
 
+  void (* freeze_keyboard) (MetaBackend *backend,
+                            uint32_t     timestamp);
+
+  void (* unfreeze_keyboard) (MetaBackend *backend,
+                              uint32_t     timestamp);
+
+  gboolean (* grab_keyboard ) (MetaBackend *backend,
+                               MetaWindow  *window,
+                               uint32_t    timestamp);
+
+  void (* ungrab_keyboard) (MetaBackend *backend,
+                            uint32_t     timestamp);
+
   void (* finish_touch_sequence) (MetaBackend          *backend,
                                   ClutterEventSequence *sequence,
                                   MetaSequenceState     state);
diff --git a/src/backends/x11/meta-backend-x11.c b/src/backends/x11/meta-backend-x11.c
index 969b64dddb..ecb017fe97 100644
--- a/src/backends/x11/meta-backend-x11.c
+++ b/src/backends/x11/meta-backend-x11.c
@@ -508,6 +508,47 @@ on_kbd_a11y_changed (MetaInputSettings   *input_settings,
   meta_seat_x11_apply_kbd_a11y_settings (seat, a11y_settings);
 }
 
+static gboolean
+grab_keyboard (Window  xwindow,
+               guint32 timestamp,
+               int     grab_mode)
+{
+  int grab_status;
+
+  unsigned char mask_bits[XIMaskLen (XI_LASTEVENT)] = { 0 };
+  XIEventMask mask = { XIAllMasterDevices, sizeof (mask_bits), mask_bits };
+
+  XISetMask (mask.mask, XI_KeyPress);
+  XISetMask (mask.mask, XI_KeyRelease);
+
+  /* Grab the keyboard, so we get key releases and all key
+   * presses
+   */
+
+  MetaBackendX11 *backend = META_BACKEND_X11 (meta_get_backend ());
+  Display *xdisplay = meta_backend_x11_get_xdisplay (backend);
+
+  /* Strictly, we only need to set grab_mode on the keyboard device
+   * while the pointer should always be XIGrabModeAsync. Unfortunately
+   * there is a bug in the X server, only fixed (link below) in 1.15,
+   * which swaps these arguments for keyboard devices. As such, we set
+   * both the device and the paired device mode which works around
+   * that bug and also works on fixed X servers.
+   *
+   * http://cgit.freedesktop.org/xorg/xserver/commit/?id=9003399708936481083424b4ff8f18a16b88b7b3
+   */
+  grab_status = XIGrabDevice (xdisplay,
+                              META_VIRTUAL_CORE_KEYBOARD_ID,
+                              xwindow,
+                              timestamp,
+                              None,
+                              grab_mode, grab_mode,
+                              False, /* owner_events */
+                              &mask);
+
+  return (grab_status == Success);
+}
+
 static void
 meta_backend_x11_post_init (MetaBackend *backend)
 {
@@ -685,6 +726,52 @@ meta_backend_x11_ungrab_device (MetaBackend *backend,
   return (ret == Success);
 }
 
+static void
+meta_backend_x11_freeze_keyboard (MetaBackend *backend,
+                                  uint32_t     timestamp)
+{
+  Window window = meta_backend_x11_get_xwindow (META_BACKEND_X11 (backend));
+  grab_keyboard (window, timestamp, XIGrabModeSync);
+}
+
+static void
+meta_backend_x11_unfreeze_keyboard (MetaBackend *backend,
+                                    uint32_t     timestamp)
+{
+  Display *xdisplay = meta_backend_x11_get_xdisplay (META_BACKEND_X11 (backend));
+
+  XIAllowEvents (xdisplay, META_VIRTUAL_CORE_KEYBOARD_ID,
+                 XIAsyncDevice, timestamp);
+  /* We shouldn't need to unfreeze the pointer device here, however we
+   * have to, due to the workaround we do in grab_keyboard().
+   */
+  XIAllowEvents (xdisplay, META_VIRTUAL_CORE_POINTER_ID,
+                 XIAsyncDevice, timestamp);
+}
+
+static gboolean
+meta_backend_x11_grab_keyboard (MetaBackend *backend,
+                                MetaWindow  *window,
+                                uint32_t     timestamp)
+{
+    Window grabwindow;
+
+    grabwindow = meta_window_x11_get_toplevel_xwindow (window);
+
+    meta_topic (META_DEBUG_KEYBINDINGS,
+                "Grabbing all keys on window %s", window->desc);
+    return grab_keyboard (grabwindow, timestamp, XIGrabModeAsync)
+}
+
+static void
+meta_backend_x11_ungrab_keyboard (MetaBackend *backend,
+                                  uint32_t     timestamp)
+{
+  Display *xdisplay = meta_backend_x11_get_xdisplay (META_BACKEND_X11 (backend));
+
+  XIUngrabDevice (xdisplay, META_VIRTUAL_CORE_KEYBOARD_ID, timestamp);
+}
+
 static void
 meta_backend_x11_finish_touch_sequence (MetaBackend          *backend,
                                         ClutterEventSequence *sequence,
@@ -914,6 +1001,10 @@ meta_backend_x11_class_init (MetaBackendX11Class *klass)
   backend_class->post_init = meta_backend_x11_post_init;
   backend_class->grab_device = meta_backend_x11_grab_device;
   backend_class->ungrab_device = meta_backend_x11_ungrab_device;
+  backend_class->freeze_keyboard = meta_backend_x11_freeze_keyboard;
+  backend_class->unfreeze_keyboard = meta_backend_x11_unfreeze_keyboard;
+  backend_class->grab_keyboard = meta_backend_x11_grab_keyboard;
+  backend_class->ungrab_keyboard = meta_backend_x11_ungrab_keyboard;
   backend_class->finish_touch_sequence = meta_backend_x11_finish_touch_sequence;
   backend_class->get_current_logical_monitor = meta_backend_x11_get_current_logical_monitor;
   backend_class->get_keymap = meta_backend_x11_get_keymap;
diff --git a/src/core/keybindings.c b/src/core/keybindings.c
index 975e68897a..9d2ec2d327 100644
--- a/src/core/keybindings.c
+++ b/src/core/keybindings.c
@@ -1752,68 +1752,12 @@ meta_display_ungrab_accelerator (MetaDisplay *display,
   return TRUE;
 }
 
-static gboolean
-grab_keyboard (Window  xwindow,
-               guint32 timestamp,
-               int     grab_mode)
-{
-  int grab_status;
-
-  unsigned char mask_bits[XIMaskLen (XI_LASTEVENT)] = { 0 };
-  XIEventMask mask = { XIAllMasterDevices, sizeof (mask_bits), mask_bits };
-
-  XISetMask (mask.mask, XI_KeyPress);
-  XISetMask (mask.mask, XI_KeyRelease);
-
-  if (meta_is_wayland_compositor ())
-    return TRUE;
-
-  /* Grab the keyboard, so we get key releases and all key
-   * presses
-   */
-
-  MetaBackendX11 *backend = META_BACKEND_X11 (meta_get_backend ());
-  Display *xdisplay = meta_backend_x11_get_xdisplay (backend);
-
-  /* Strictly, we only need to set grab_mode on the keyboard device
-   * while the pointer should always be XIGrabModeAsync. Unfortunately
-   * there is a bug in the X server, only fixed (link below) in 1.15,
-   * which swaps these arguments for keyboard devices. As such, we set
-   * both the device and the paired device mode which works around
-   * that bug and also works on fixed X servers.
-   *
-   * http://cgit.freedesktop.org/xorg/xserver/commit/?id=9003399708936481083424b4ff8f18a16b88b7b3
-   */
-  grab_status = XIGrabDevice (xdisplay,
-                              META_VIRTUAL_CORE_KEYBOARD_ID,
-                              xwindow,
-                              timestamp,
-                              None,
-                              grab_mode, grab_mode,
-                              False, /* owner_events */
-                              &mask);
-
-  return (grab_status == Success);
-}
-
-static void
-ungrab_keyboard (guint32 timestamp)
-{
-  if (meta_is_wayland_compositor ())
-    return;
-
-  MetaBackendX11 *backend = META_BACKEND_X11 (meta_get_backend ());
-  Display *xdisplay = meta_backend_x11_get_xdisplay (backend);
-
-  XIUngrabDevice (xdisplay, META_VIRTUAL_CORE_KEYBOARD_ID, timestamp);
-}
-
 gboolean
 meta_window_grab_all_keys (MetaWindow  *window,
                            guint32      timestamp)
 {
-  Window grabwindow;
   gboolean retval = TRUE;
+  MetaBackend *backend = meta_get_backend ();
 
   if (window->all_keys_grabbed)
     return FALSE;
@@ -1829,14 +1773,7 @@ meta_window_grab_all_keys (MetaWindow  *window,
               window->desc);
   meta_window_focus (window, timestamp);
 
-  if (!meta_is_wayland_compositor ())
-    {
-      grabwindow = meta_window_x11_get_toplevel_xwindow (window);
-
-      meta_topic (META_DEBUG_KEYBINDINGS,
-                  "Grabbing all keys on window %s", window->desc);
-      retval = grab_keyboard (grabwindow, timestamp, XIGrabModeAsync);
-    }
+  retval = META_BACKEND_GET_CLASS (backend)->grab_keyboard (backend, window, timestamp);
   if (retval)
     {
       window->keys_grabbed = FALSE;
@@ -1853,8 +1790,7 @@ meta_window_ungrab_all_keys (MetaWindow *window,
 {
   if (window->all_keys_grabbed)
     {
-      if (!meta_is_wayland_compositor())
-        ungrab_keyboard (timestamp);
+      meta_display_ungrab_keyboard (window->display, timestamp);
 
       window->grab_on_frame = FALSE;
       window->all_keys_grabbed = FALSE;
@@ -1869,37 +1805,21 @@ void
 meta_display_freeze_keyboard (MetaDisplay *display, guint32 timestamp)
 {
   MetaBackend *backend = meta_get_backend ();
-
-  if (!META_IS_BACKEND_X11 (backend))
-    return;
-
-  Window window = meta_backend_x11_get_xwindow (META_BACKEND_X11 (backend));
-  grab_keyboard (window, timestamp, XIGrabModeSync);
+  META_BACKEND_GET_CLASS (backend)->freeze_keyboard (backend, timestamp);
 }
 
 void
 meta_display_ungrab_keyboard (MetaDisplay *display, guint32 timestamp)
 {
-  ungrab_keyboard (timestamp);
+  MetaBackend *backend = meta_get_backend ();
+  META_BACKEND_GET_CLASS (backend)->ungrab_keyboard (backend, timestamp);
 }
 
 void
 meta_display_unfreeze_keyboard (MetaDisplay *display, guint32 timestamp)
 {
   MetaBackend *backend = meta_get_backend ();
-
-  if (!META_IS_BACKEND_X11 (backend))
-    return;
-
-  Display *xdisplay = meta_backend_x11_get_xdisplay (META_BACKEND_X11 (backend));
-
-  XIAllowEvents (xdisplay, META_VIRTUAL_CORE_KEYBOARD_ID,
-                 XIAsyncDevice, timestamp);
-  /* We shouldn't need to unfreeze the pointer device here, however we
-   * have to, due to the workaround we do in grab_keyboard().
-   */
-  XIAllowEvents (xdisplay, META_VIRTUAL_CORE_POINTER_ID,
-                 XIAsyncDevice, timestamp);
+  META_BACKEND_GET_CLASS (backend)->unfreeze_keyboard (backend, timestamp);
 }
 
 static gboolean


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