[mutter/bilelmoussaoui/keybindings: 122/124] keybindings: Move grab/freeze functions per backend




commit 6a2c652c65203816c97058aa1b0b3494ec53d8f4
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/meta-backend.c         | 34 ++++++++++++++
 src/backends/x11/meta-backend-x11.c | 93 ++++++++++++++++++++++++++++++++++++
 src/core/keybindings.c              | 94 +++----------------------------------
 4 files changed, 147 insertions(+), 87 deletions(-)
---
diff --git a/src/backends/meta-backend-private.h b/src/backends/meta-backend-private.h
index ce703dfa37..ef0336afe7 100644
--- a/src/backends/meta-backend-private.h
+++ b/src/backends/meta-backend-private.h
@@ -84,6 +84,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/meta-backend.c b/src/backends/meta-backend.c
index 856b58e879..ed9716833f 100644
--- a/src/backends/meta-backend.c
+++ b/src/backends/meta-backend.c
@@ -638,6 +638,36 @@ meta_backend_real_is_headless (MetaBackend *backend)
   return FALSE;
 }
 
+static void
+meta_backend_real_freeze_keyboard (MetaBackend *backend,
+                                   uint32_t     timestamp)
+{
+  /* Do nothing */
+}
+
+static void
+meta_backend_real_unfreeze_keyboard (MetaBackend *backend,
+                                     uint32_t     timestamp)
+{
+  /* Do nothing */
+}
+
+static gboolean
+meta_backend_real_grab_keyboard (MetaBackend *backend,
+                                 MetaWindow  *window,
+                                 uint32_t     timestamp)
+{
+  /* Do nothing */
+  return FALSE;
+}
+
+static void
+meta_backend_real_ungrab_keyboard (MetaBackend *backend,
+                                   uint32_t     timestamp)
+{
+  /* Do nothing */
+}
+
 gboolean
 meta_backend_is_lid_closed (MetaBackend *backend)
 {
@@ -874,6 +904,10 @@ meta_backend_class_init (MetaBackendClass *klass)
   klass->is_lid_closed = meta_backend_real_is_lid_closed;
   klass->create_cursor_tracker = meta_backend_real_create_cursor_tracker;
   klass->is_headless = meta_backend_real_is_headless;
+  klass->freeze_keyboard = meta_backend_real_freeze_keyboard;
+  klass->unfreeze_keyboard = meta_backend_real_unfreeze_keyboard;
+  klass->grab_keyboard = meta_backend_real_grab_keyboard;
+  klass->ungrab_keyboard = meta_backend_real_ungrab_keyboard;
 
   obj_props[PROP_CONTEXT] =
     g_param_spec_object ("context",
diff --git a/src/backends/x11/meta-backend-x11.c b/src/backends/x11/meta-backend-x11.c
index da14cd73a2..7cdf21b6b9 100644
--- a/src/backends/x11/meta-backend-x11.c
+++ b/src/backends/x11/meta-backend-x11.c
@@ -55,8 +55,10 @@
 #include "clutter/clutter.h"
 #include "compositor/compositor-private.h"
 #include "core/display-private.h"
+#include "core/window-private.h"
 #include "meta/meta-cursor-tracker.h"
 #include "meta/util.h"
+#include "x11/window-x11.h"
 
 struct _MetaBackendX11Private
 {
@@ -523,6 +525,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)
 {
@@ -681,6 +724,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,
@@ -957,6 +1046,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]