[mutter/wip/carlosg/input-thread: 92/96] backends/native: Shuffle xkb_keymap creation




commit 7512474b495c01fec121ad7f61bdbc2669a94331
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Aug 12 12:47:07 2020 +0200

    backends/native: Shuffle xkb_keymap creation
    
    Push it a little bit down to the MetaSeatNative. As both the UI thread
    and the input thread are interested in dealing with the xkb_keymap and
    it is not meant to be used in different threads, keep 2 separate copies
    around.
    
    The keyboard map will always be set from the UI thread, so the xkb_keymap
    owned by the MetaSeatNative (owned by the UI thread) can be considered
    canonical.
    
    https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1403

 src/backends/native/meta-backend-native.c | 22 +++-----------
 src/backends/native/meta-keymap-native.c  | 17 -----------
 src/backends/native/meta-seat-impl.c      | 43 +++++++-------------------
 src/backends/native/meta-seat-impl.h      |  4 ---
 src/backends/native/meta-seat-native.c    | 50 +++++++++++++++++++++++++++----
 src/backends/native/meta-seat-native.h    |  8 +++--
 6 files changed, 65 insertions(+), 79 deletions(-)
---
diff --git a/src/backends/native/meta-backend-native.c b/src/backends/native/meta-backend-native.c
index 2cb75f8aaa..1e7153ebf9 100644
--- a/src/backends/native/meta-backend-native.c
+++ b/src/backends/native/meta-backend-native.c
@@ -187,12 +187,12 @@ meta_backend_native_post_init (MetaBackend *backend)
   MetaSeatNative *seat =
     META_SEAT_NATIVE (clutter_backend_get_default_seat (clutter_backend));
 
-  META_BACKEND_CLASS (meta_backend_native_parent_class)->post_init (backend);
-
   native->input_settings = meta_seat_impl_get_input_settings (seat->impl);
   g_signal_connect_object (native->input_settings, "kbd-a11y-changed",
                            G_CALLBACK (kbd_a11y_changed_cb), backend, 0);
 
+  META_BACKEND_CLASS (meta_backend_native_parent_class)->post_init (backend);
+
   if (meta_settings_is_experimental_feature_enabled (settings,
                                                      META_EXPERIMENTAL_FEATURE_RT_SCHEDULER))
     {
@@ -286,27 +286,13 @@ meta_backend_native_set_keymap (MetaBackend *backend,
                                 const char  *options)
 {
   ClutterBackend *clutter_backend = meta_backend_get_clutter_backend (backend);
-  struct xkb_rule_names names;
-  struct xkb_keymap *keymap;
-  struct xkb_context *context;
   ClutterSeat *seat;
 
-  names.rules = DEFAULT_XKB_RULES_FILE;
-  names.model = DEFAULT_XKB_MODEL;
-  names.layout = layouts;
-  names.variant = variants;
-  names.options = options;
-
-  context = meta_create_xkb_context ();
-  keymap = xkb_keymap_new_from_names (context, &names, XKB_KEYMAP_COMPILE_NO_FLAGS);
-  xkb_context_unref (context);
-
   seat = clutter_backend_get_default_seat (clutter_backend);
-  meta_seat_native_set_keyboard_map (META_SEAT_NATIVE (seat), keymap);
+  meta_seat_native_set_keyboard_map (META_SEAT_NATIVE (seat),
+                                     layouts, variants, options);
 
   meta_backend_notify_keymap_changed (backend);
-
-  xkb_keymap_unref (keymap);
 }
 
 static struct xkb_keymap *
diff --git a/src/backends/native/meta-keymap-native.c b/src/backends/native/meta-keymap-native.c
index e0b09a13bd..62f293c84c 100644
--- a/src/backends/native/meta-keymap-native.c
+++ b/src/backends/native/meta-keymap-native.c
@@ -25,10 +25,6 @@
 #include "backends/native/meta-keymap-native.h"
 #include "backends/native/meta-seat-native.h"
 
-static const char *option_xkb_layout = "us";
-static const char *option_xkb_variant = "";
-static const char *option_xkb_options = "";
-
 typedef struct _MetaKeymapNative MetaKeymapNative;
 
 struct _MetaKeymapNative
@@ -91,19 +87,6 @@ meta_keymap_native_class_init (MetaKeymapNativeClass *klass)
 static void
 meta_keymap_native_init (MetaKeymapNative *keymap)
 {
-  struct xkb_context *ctx;
-  struct xkb_rule_names names;
-
-  names.rules = "evdev";
-  names.model = "pc105";
-  names.layout = option_xkb_layout;
-  names.variant = option_xkb_variant;
-  names.options = option_xkb_options;
-
-  ctx = meta_create_xkb_context ();
-  g_assert (ctx);
-  keymap->keymap = xkb_keymap_new_from_names (ctx, &names, 0);
-  xkb_context_unref (ctx);
 }
 
 void
diff --git a/src/backends/native/meta-seat-impl.c b/src/backends/native/meta-seat-impl.c
index 6f1ddc8594..8f13560207 100644
--- a/src/backends/native/meta-seat-impl.c
+++ b/src/backends/native/meta-seat-impl.c
@@ -2837,19 +2837,23 @@ meta_seat_impl_set_device_callbacks (MetaOpenDeviceCallback  open_callback,
 void
 meta_seat_impl_update_xkb_state (MetaSeatImpl *seat)
 {
-  xkb_mod_mask_t latched_mods;
-  xkb_mod_mask_t locked_mods;
+  xkb_mod_mask_t latched_mods = 0;
+  xkb_mod_mask_t locked_mods = 0;
   struct xkb_keymap *xkb_keymap;
 
   g_rw_lock_writer_lock (&seat->state_lock);
 
   xkb_keymap = meta_keymap_native_get_keyboard_map (seat->keymap);
 
-  latched_mods = xkb_state_serialize_mods (seat->xkb,
-                                           XKB_STATE_MODS_LATCHED);
-  locked_mods = xkb_state_serialize_mods (seat->xkb,
-                                          XKB_STATE_MODS_LOCKED);
-  xkb_state_unref (seat->xkb);
+  if (seat->xkb)
+    {
+      latched_mods = xkb_state_serialize_mods (seat->xkb,
+                                               XKB_STATE_MODS_LATCHED);
+      locked_mods = xkb_state_serialize_mods (seat->xkb,
+                                              XKB_STATE_MODS_LOCKED);
+      xkb_state_unref (seat->xkb);
+    }
+
   seat->xkb = xkb_state_new (xkb_keymap);
 
   xkb_state_update_mask (seat->xkb,
@@ -2992,22 +2996,6 @@ meta_seat_impl_set_keyboard_map (MetaSeatImpl      *seat,
   meta_seat_impl_update_xkb_state (seat);
 }
 
-/**
- * meta_seat_impl_get_keyboard_map: (skip)
- * @seat: the #ClutterSeat created by the evdev backend
- *
- * Retrieves the #xkb_keymap in use by the evdev backend.
- *
- * Return value: the #xkb_keymap.
- */
-struct xkb_keymap *
-meta_seat_impl_get_keyboard_map (MetaSeatImpl *seat)
-{
-  g_return_val_if_fail (META_IS_SEAT_IMPL (seat), NULL);
-
-  return xkb_state_get_keymap (seat->xkb);
-}
-
 /**
  * meta_seat_impl_set_keyboard_layout_index: (skip)
  * @seat: the #ClutterSeat created by the evdev backend
@@ -3042,15 +3030,6 @@ meta_seat_impl_set_keyboard_layout_index (MetaSeatImpl       *seat,
   g_rw_lock_writer_unlock (&seat->state_lock);
 }
 
-/**
- * meta_seat_impl_get_keyboard_layout_index: (skip)
- */
-xkb_layout_index_t
-meta_seat_impl_get_keyboard_layout_index (MetaSeatImpl *seat)
-{
-  return seat->layout_idx;
-}
-
 /**
  * meta_seat_impl_set_keyboard_numlock: (skip)
  * @seat: the #ClutterSeat created by the evdev backend
diff --git a/src/backends/native/meta-seat-impl.h b/src/backends/native/meta-seat-impl.h
index 0fba91fce9..d458bea70d 100644
--- a/src/backends/native/meta-seat-impl.h
+++ b/src/backends/native/meta-seat-impl.h
@@ -218,13 +218,9 @@ struct xkb_state * meta_seat_impl_get_xkb_state (MetaSeatImpl *seat);
 void               meta_seat_impl_set_keyboard_map   (MetaSeatImpl      *seat,
                                                       struct xkb_keymap *keymap);
 
-struct xkb_keymap * meta_seat_impl_get_keyboard_map (MetaSeatImpl *seat);
-
 void meta_seat_impl_set_keyboard_layout_index (MetaSeatImpl       *seat,
                                                xkb_layout_index_t  idx);
 
-xkb_layout_index_t meta_seat_impl_get_keyboard_layout_index (MetaSeatImpl *seat);
-
 void meta_seat_impl_set_keyboard_numlock (MetaSeatImpl *seat,
                                           gboolean      numlock_state);
 
diff --git a/src/backends/native/meta-seat-native.c b/src/backends/native/meta-seat-native.c
index 37df1e13e2..b781cf426f 100644
--- a/src/backends/native/meta-seat-native.c
+++ b/src/backends/native/meta-seat-native.c
@@ -35,6 +35,7 @@
 #include <math.h>
 
 #include "backends/meta-cursor-tracker-private.h"
+#include "backends/meta-keymap-utils.h"
 #include "backends/native/meta-barrier-native.h"
 #include "backends/native/meta-event-native.h"
 #include "backends/native/meta-input-device-native.h"
@@ -176,6 +177,8 @@ meta_seat_native_constructed (GObject *object)
   seat->kms_cursor_renderer =
     meta_kms_cursor_renderer_new (meta_get_backend ());
 
+  meta_seat_native_set_keyboard_map (seat, "us", "", "");
+
   if (G_OBJECT_CLASS (meta_seat_native_parent_class)->constructed)
     G_OBJECT_CLASS (meta_seat_native_parent_class)->constructed (object);
 }
@@ -226,6 +229,8 @@ meta_seat_native_finalize (GObject *object)
   MetaSeatNative *seat = META_SEAT_NATIVE (object);
   GList *iter;
 
+  if (seat->xkb_keymap)
+    xkb_keymap_unref (seat->xkb_keymap);
   g_clear_object (&seat->core_pointer);
   g_clear_object (&seat->core_keyboard);
   g_clear_object (&seat->impl);
@@ -480,6 +485,28 @@ meta_seat_native_reclaim_devices (MetaSeatNative *seat)
   seat->released = FALSE;
 }
 
+static struct xkb_keymap *
+create_keymap (const char *layouts,
+               const char *variants,
+               const char *options)
+{
+  struct xkb_rule_names names;
+  struct xkb_keymap *keymap;
+  struct xkb_context *context;
+
+  names.rules = DEFAULT_XKB_RULES_FILE;
+  names.model = DEFAULT_XKB_MODEL;
+  names.layout = layouts;
+  names.variant = variants;
+  names.options = options;
+
+  context = meta_create_xkb_context ();
+  keymap = xkb_keymap_new_from_names (context, &names, XKB_KEYMAP_COMPILE_NO_FLAGS);
+  xkb_context_unref (context);
+
+  return keymap;
+}
+
 /**
  * meta_seat_native_set_keyboard_map: (skip)
  * @seat: the #ClutterSeat created by the evdev backend
@@ -491,12 +518,22 @@ meta_seat_native_reclaim_devices (MetaSeatNative *seat)
  * is pressed when calling this function.
  */
 void
-meta_seat_native_set_keyboard_map (MetaSeatNative    *seat,
-                                   struct xkb_keymap *xkb_keymap)
+meta_seat_native_set_keyboard_map (MetaSeatNative *seat,
+                                   const char     *layouts,
+                                   const char     *variants,
+                                   const char     *options)
 {
-  g_return_if_fail (META_IS_SEAT_NATIVE (seat));
+  struct xkb_keymap *keymap, *impl_keymap;
+
+  keymap = create_keymap (layouts, variants, options);
+  impl_keymap = create_keymap (layouts, variants, options);
+
+  if (seat->xkb_keymap)
+    xkb_keymap_unref (seat->xkb_keymap);
+  seat->xkb_keymap = keymap;
 
-  meta_seat_impl_set_keyboard_map (seat->impl, xkb_keymap);
+  meta_seat_impl_set_keyboard_map (seat->impl, impl_keymap);
+  xkb_keymap_unref (impl_keymap);
 }
 
 /**
@@ -512,7 +549,7 @@ meta_seat_native_get_keyboard_map (MetaSeatNative *seat)
 {
   g_return_val_if_fail (META_IS_SEAT_NATIVE (seat), NULL);
 
-  return meta_seat_impl_get_keyboard_map (seat->impl);
+  return seat->xkb_keymap;
 }
 
 /**
@@ -528,6 +565,7 @@ meta_seat_native_set_keyboard_layout_index (MetaSeatNative     *seat,
 {
   g_return_if_fail (META_IS_SEAT_NATIVE (seat));
 
+  seat->xkb_layout_index = idx;
   meta_seat_impl_set_keyboard_layout_index (seat->impl, idx);
 }
 
@@ -537,7 +575,7 @@ meta_seat_native_set_keyboard_layout_index (MetaSeatNative     *seat,
 xkb_layout_index_t
 meta_seat_native_get_keyboard_layout_index (MetaSeatNative *seat)
 {
-  return meta_seat_impl_get_keyboard_layout_index (seat->impl);
+  return seat->xkb_layout_index;
 }
 
 /**
diff --git a/src/backends/native/meta-seat-native.h b/src/backends/native/meta-seat-native.h
index ac42bcbfb5..fffd0c81c7 100644
--- a/src/backends/native/meta-seat-native.h
+++ b/src/backends/native/meta-seat-native.h
@@ -46,6 +46,8 @@ struct _MetaSeatNative
   char *seat_id;
 
   GList *devices;
+  struct xkb_keymap *xkb_keymap;
+  xkb_layout_index_t xkb_layout_index;
 
   ClutterInputDevice *core_pointer;
   ClutterInputDevice *core_keyboard;
@@ -91,8 +93,10 @@ void  meta_seat_native_set_device_callbacks (MetaOpenDeviceCallback  open_callba
 void  meta_seat_native_release_devices (MetaSeatNative *seat);
 void  meta_seat_native_reclaim_devices (MetaSeatNative *seat);
 
-void               meta_seat_native_set_keyboard_map   (MetaSeatNative    *seat,
-                                                        struct xkb_keymap *keymap);
+void meta_seat_native_set_keyboard_map (MetaSeatNative *seat,
+                                        const char     *layouts,
+                                        const char     *variants,
+                                        const char     *options);
 
 struct xkb_keymap * meta_seat_native_get_keyboard_map (MetaSeatNative *seat);
 


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