[gnome-flashback] common: add gf_keybindings_grab_iso_group



commit 7601be343db6a97b755eb8fe110f040cd85c810a
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Sun Dec 1 00:19:41 2019 +0200

    common: add gf_keybindings_grab_iso_group
    
    https://gitlab.gnome.org/GNOME/gnome-flashback/issues/24

 gnome-flashback/libcommon/gf-keybindings.c         | 214 ++++++---------------
 gnome-flashback/libcommon/gf-keybindings.h         |  21 +-
 .../libinput-sources/gf-input-source-manager.c     |  39 +++-
 gnome-flashback/libshell/flashback-shell.c         |   3 +-
 4 files changed, 113 insertions(+), 164 deletions(-)
---
diff --git a/gnome-flashback/libcommon/gf-keybindings.c b/gnome-flashback/libcommon/gf-keybindings.c
index f743a9b..911fc9a 100644
--- a/gnome-flashback/libcommon/gf-keybindings.c
+++ b/gnome-flashback/libcommon/gf-keybindings.c
@@ -34,12 +34,9 @@ struct _GfKeybindings
   GObject     parent;
 
   GHashTable *keybindings;
-  GHashTable *iso_next_group_keybindings;
+  GHashTable *iso_group_keybindings;
 
-  gboolean    iso_next_group;
-
-  GSettings  *settings;
-  gchar      *iso_next_group_option;
+  gchar      *iso_group;
 
   Display    *xdisplay;
   Window      xwindow;
@@ -77,17 +74,6 @@ enum
 
 static guint signals[LAST_SIGNAL] = { 0 };
 
-enum
-{
-  PROP_0,
-
-  PROP_ISO_NEXT_GROUP,
-
-  LAST_PROP
-};
-
-static GParamSpec *properties[LAST_PROP] = { NULL };
-
 G_DEFINE_TYPE (GfKeybindings, gf_keybindings, G_TYPE_OBJECT)
 
 static gboolean
@@ -313,110 +299,110 @@ get_keycodes_for_keysym (GfKeybindings *keybindings,
 }
 
 static void
-reload_iso_next_group_keybindings (GfKeybindings *keybindings)
+reload_iso_group_keybindings (GfKeybindings *keybindings)
 {
   gint *keycodes;
   gint n_keycodes;
   gint i;
 
-  g_hash_table_remove_all (keybindings->iso_next_group_keybindings);
+  g_hash_table_remove_all (keybindings->iso_group_keybindings);
 
-  if (keybindings->iso_next_group_option == NULL)
+  if (keybindings->iso_group == NULL)
     return;
 
   n_keycodes = get_keycodes_for_keysym (keybindings, XK_ISO_Next_Group,
                                         &keycodes);
 
-  if (g_strcmp0 (keybindings->iso_next_group_option, "toggle") == 0 ||
-      g_strcmp0 (keybindings->iso_next_group_option, "lalt_toggle") == 0 ||
-      g_strcmp0 (keybindings->iso_next_group_option, "lwin_toggle") == 0 ||
-      g_strcmp0 (keybindings->iso_next_group_option, "rwin_toggle") == 0 ||
-      g_strcmp0 (keybindings->iso_next_group_option, "lshift_toggle") == 0 ||
-      g_strcmp0 (keybindings->iso_next_group_option, "rshift_toggle") == 0 ||
-      g_strcmp0 (keybindings->iso_next_group_option, "lctrl_toggle") == 0 ||
-      g_strcmp0 (keybindings->iso_next_group_option, "rctrl_toggle") == 0 ||
-      g_strcmp0 (keybindings->iso_next_group_option, "sclk_toggle") == 0 ||
-      g_strcmp0 (keybindings->iso_next_group_option, "menu_toggle") == 0 ||
-      g_strcmp0 (keybindings->iso_next_group_option, "caps_toggle") == 0)
+  if (g_strcmp0 (keybindings->iso_group, "toggle") == 0 ||
+      g_strcmp0 (keybindings->iso_group, "lalt_toggle") == 0 ||
+      g_strcmp0 (keybindings->iso_group, "lwin_toggle") == 0 ||
+      g_strcmp0 (keybindings->iso_group, "rwin_toggle") == 0 ||
+      g_strcmp0 (keybindings->iso_group, "lshift_toggle") == 0 ||
+      g_strcmp0 (keybindings->iso_group, "rshift_toggle") == 0 ||
+      g_strcmp0 (keybindings->iso_group, "lctrl_toggle") == 0 ||
+      g_strcmp0 (keybindings->iso_group, "rctrl_toggle") == 0 ||
+      g_strcmp0 (keybindings->iso_group, "sclk_toggle") == 0 ||
+      g_strcmp0 (keybindings->iso_group, "menu_toggle") == 0 ||
+      g_strcmp0 (keybindings->iso_group, "caps_toggle") == 0)
     {
       for (i = 0; i < n_keycodes; i++)
         {
           Keybinding *keybinding;
 
           keybinding = keybinding_new (NULL, 0, 0, keycodes[i], 0, 0);
-          g_hash_table_insert (keybindings->iso_next_group_keybindings,
+          g_hash_table_insert (keybindings->iso_group_keybindings,
                                GINT_TO_POINTER (i), keybinding);
         }
     }
-  else if (g_strcmp0 (keybindings->iso_next_group_option, "shift_caps_toggle") == 0 ||
-           g_strcmp0 (keybindings->iso_next_group_option, "shifts_toggle") == 0)
+  else if (g_strcmp0 (keybindings->iso_group, "shift_caps_toggle") == 0 ||
+           g_strcmp0 (keybindings->iso_group, "shifts_toggle") == 0)
     {
       for (i = 0; i < n_keycodes; i++)
         {
           Keybinding *keybinding;
 
           keybinding = keybinding_new (NULL, 0, 0, keycodes[i], ShiftMask, 0);
-          g_hash_table_insert (keybindings->iso_next_group_keybindings,
+          g_hash_table_insert (keybindings->iso_group_keybindings,
                                GINT_TO_POINTER (i), keybinding);
         }
     }
-  else if (g_strcmp0 (keybindings->iso_next_group_option, "alt_caps_toggle") == 0 ||
-           g_strcmp0 (keybindings->iso_next_group_option, "alt_space_toggle") == 0)
+  else if (g_strcmp0 (keybindings->iso_group, "alt_caps_toggle") == 0 ||
+           g_strcmp0 (keybindings->iso_group, "alt_space_toggle") == 0)
     {
       for (i = 0; i < n_keycodes; i++)
         {
           Keybinding *keybinding;
 
           keybinding = keybinding_new (NULL, 0, 0, keycodes[i], Mod1Mask, 0);
-          g_hash_table_insert (keybindings->iso_next_group_keybindings,
+          g_hash_table_insert (keybindings->iso_group_keybindings,
                                GINT_TO_POINTER (i), keybinding);
         }
     }
-  else if (g_strcmp0 (keybindings->iso_next_group_option, "ctrl_shift_toggle") == 0 ||
-           g_strcmp0 (keybindings->iso_next_group_option, "lctrl_lshift_toggle") == 0 ||
-           g_strcmp0 (keybindings->iso_next_group_option, "rctrl_rshift_toggle") == 0)
+  else if (g_strcmp0 (keybindings->iso_group, "ctrl_shift_toggle") == 0 ||
+           g_strcmp0 (keybindings->iso_group, "lctrl_lshift_toggle") == 0 ||
+           g_strcmp0 (keybindings->iso_group, "rctrl_rshift_toggle") == 0)
     {
       for (i = 0; i < n_keycodes; i++)
         {
           Keybinding *keybinding;
 
           keybinding = keybinding_new (NULL, 0, 0, keycodes[i], ShiftMask, 0);
-          g_hash_table_insert (keybindings->iso_next_group_keybindings,
+          g_hash_table_insert (keybindings->iso_group_keybindings,
                                GINT_TO_POINTER (i), keybinding);
 
           keybinding = keybinding_new (NULL, 0, 0, keycodes[i], ControlMask, 0);
-          g_hash_table_insert (keybindings->iso_next_group_keybindings,
+          g_hash_table_insert (keybindings->iso_group_keybindings,
                                GINT_TO_POINTER (i + n_keycodes), keybinding);
         }
     }
-  else if (g_strcmp0 (keybindings->iso_next_group_option, "ctrl_alt_toggle") == 0)
+  else if (g_strcmp0 (keybindings->iso_group, "ctrl_alt_toggle") == 0)
     {
       for (i = 0; i < n_keycodes; i++)
         {
           Keybinding *keybinding;
 
           keybinding = keybinding_new (NULL, 0, 0, keycodes[i], Mod1Mask, 0);
-          g_hash_table_insert (keybindings->iso_next_group_keybindings,
+          g_hash_table_insert (keybindings->iso_group_keybindings,
                                GINT_TO_POINTER (i), keybinding);
 
           keybinding = keybinding_new (NULL, 0, 0, keycodes[i], ControlMask, 0);
-          g_hash_table_insert (keybindings->iso_next_group_keybindings,
+          g_hash_table_insert (keybindings->iso_group_keybindings,
                                GINT_TO_POINTER (i + n_keycodes), keybinding);
         }
     }
-  else if (g_strcmp0 (keybindings->iso_next_group_option, "alt_shift_toggle") == 0 ||
-           g_strcmp0 (keybindings->iso_next_group_option, "lalt_lshift_toggle") == 0)
+  else if (g_strcmp0 (keybindings->iso_group, "alt_shift_toggle") == 0 ||
+           g_strcmp0 (keybindings->iso_group, "lalt_lshift_toggle") == 0)
     {
       for (i = 0; i < n_keycodes; i++)
         {
           Keybinding *keybinding;
 
           keybinding = keybinding_new (NULL, 0, 0, keycodes[i], Mod1Mask, 0);
-          g_hash_table_insert (keybindings->iso_next_group_keybindings,
+          g_hash_table_insert (keybindings->iso_group_keybindings,
                                GINT_TO_POINTER (i), keybinding);
 
           keybinding = keybinding_new (NULL, 0, 0, keycodes[i], ShiftMask, 0);
-          g_hash_table_insert (keybindings->iso_next_group_keybindings,
+          g_hash_table_insert (keybindings->iso_group_keybindings,
                                GINT_TO_POINTER (i + n_keycodes), keybinding);
         }
     }
@@ -430,7 +416,7 @@ ungrab_keybindings (GfKeybindings *keybindings)
   GHashTableIter iter;
   gpointer value;
 
-  g_hash_table_iter_init (&iter, keybindings->iso_next_group_keybindings);
+  g_hash_table_iter_init (&iter, keybindings->iso_group_keybindings);
   while (g_hash_table_iter_next (&iter, NULL, &value))
     {
       Keybinding *keybinding;
@@ -488,7 +474,7 @@ reload_keybindings (GfKeybindings *keybindings)
   GHashTableIter iter;
   gpointer value;
 
-  reload_iso_next_group_keybindings (keybindings);
+  reload_iso_group_keybindings (keybindings);
 
   g_hash_table_iter_init (&iter, keybindings->keybindings);
   while (g_hash_table_iter_next (&iter, NULL, &value))
@@ -531,7 +517,7 @@ regrab_keybindings (GfKeybindings *keybindings)
   GHashTableIter iter;
   gpointer value;
 
-  g_hash_table_iter_init (&iter, keybindings->iso_next_group_keybindings);
+  g_hash_table_iter_init (&iter, keybindings->iso_group_keybindings);
   while (g_hash_table_iter_next (&iter, NULL, &value))
     {
       Keybinding *keybinding;
@@ -553,8 +539,8 @@ regrab_keybindings (GfKeybindings *keybindings)
 }
 
 static gboolean
-process_iso_next_group (GfKeybindings *keybindings,
-                        XEvent        *event)
+process_iso_group (GfKeybindings *keybindings,
+                   XEvent        *event)
 {
   gboolean processed;
   guint state;
@@ -566,7 +552,7 @@ process_iso_next_group (GfKeybindings *keybindings,
 
   processed = FALSE;
   state = event->xkey.state & 0xff & ~(keybindings->ignored_mask);
-  values = g_hash_table_get_values (keybindings->iso_next_group_keybindings);
+  values = g_hash_table_get_values (keybindings->iso_group_keybindings);
 
   for (l = values; l; l = l->next)
     {
@@ -671,7 +657,7 @@ filter_func (GdkXEvent *xevent,
   if (ev->type != KeyPress && ev->type != KeyRelease)
     return GDK_FILTER_CONTINUE;
 
-  if (process_iso_next_group (keybindings, ev))
+  if (process_iso_group (keybindings, ev))
     return GDK_FILTER_REMOVE;
 
   XAllowEvents (keybindings->xdisplay, AsyncKeyboard, ev->xkey.time);
@@ -690,61 +676,6 @@ get_next_action (void)
   return ++action;
 }
 
-static void
-xkb_options_changed_cb (GSettings *settings,
-                        gchar     *key,
-                        gpointer   user_data)
-{
-  GfKeybindings *keybindings;
-  gchar **xkb_options;
-  gchar **p;
-  gchar *option;
-
-  keybindings = GF_KEYBINDINGS (user_data);
-  xkb_options = g_settings_get_strv (settings, KEY_XKB_OPTIONS);
-  option = NULL;
-
-  for (p = xkb_options; p && *p; ++p)
-    {
-      if (g_str_has_prefix (*p, "grp:"))
-        {
-          option = (*p + 4);
-          break;
-        }
-    }
-
-  if (g_strcmp0 (option, keybindings->iso_next_group_option) == 0)
-    {
-      g_strfreev (xkb_options);
-      return;
-    }
-
-  g_free (keybindings->iso_next_group_option);
-  keybindings->iso_next_group_option = g_strdup (option);
-  g_strfreev (xkb_options);
-
-  reload_iso_next_group_keybindings (keybindings);
-}
-
-static void
-gf_keybindings_constructed (GObject *object)
-{
-  GfKeybindings *keybindings;
-
-  keybindings = GF_KEYBINDINGS (object);
-
-  G_OBJECT_CLASS (gf_keybindings_parent_class)->constructed (object);
-
-  if (keybindings->iso_next_group)
-    {
-      keybindings->settings = g_settings_new (DESKTOP_INPUT_SOURCES_SCHEMA);
-
-      g_signal_connect (keybindings->settings, "changed::" KEY_XKB_OPTIONS,
-                        G_CALLBACK (xkb_options_changed_cb), keybindings);
-      xkb_options_changed_cb (keybindings->settings, NULL, keybindings);
-    }
-}
-
 static void
 gf_keybindings_dispose (GObject *object)
 {
@@ -758,14 +689,12 @@ gf_keybindings_dispose (GObject *object)
       keybindings->keybindings = NULL;
     }
 
-  if (keybindings->iso_next_group_keybindings != NULL)
+  if (keybindings->iso_group_keybindings != NULL)
     {
-      g_hash_table_destroy (keybindings->iso_next_group_keybindings);
-      keybindings->iso_next_group_keybindings = NULL;
+      g_hash_table_destroy (keybindings->iso_group_keybindings);
+      keybindings->iso_group_keybindings = NULL;
     }
 
-  g_clear_object (&keybindings->settings);
-
   G_OBJECT_CLASS (gf_keybindings_parent_class)->dispose (object);
 }
 
@@ -778,33 +707,11 @@ gf_keybindings_finalize (GObject *object)
 
   gdk_window_remove_filter (NULL, filter_func, keybindings);
 
-  g_free (keybindings->iso_next_group_option);
+  g_free (keybindings->iso_group);
 
   G_OBJECT_CLASS (gf_keybindings_parent_class)->finalize (object);
 }
 
-static void
-gf_keybindings_set_property (GObject      *object,
-                             guint         prop_id,
-                             const GValue *value,
-                             GParamSpec   *pspec)
-{
-  GfKeybindings *keybindings;
-
-  keybindings = GF_KEYBINDINGS (object);
-
-  switch (prop_id)
-    {
-      case PROP_ISO_NEXT_GROUP:
-        keybindings->iso_next_group = g_value_get_boolean (value);
-        break;
-
-      default:
-        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-        break;
-    }
-}
-
 static void
 gf_keybindings_class_init (GfKeybindingsClass *keybindings_class)
 {
@@ -812,10 +719,8 @@ gf_keybindings_class_init (GfKeybindingsClass *keybindings_class)
 
   object_class = G_OBJECT_CLASS (keybindings_class);
 
-  object_class->constructed = gf_keybindings_constructed;
   object_class->dispose = gf_keybindings_dispose;
   object_class->finalize = gf_keybindings_finalize;
-  object_class->set_property = gf_keybindings_set_property;
 
   /**
    * GfKeybindings::accelerator-activated:
@@ -843,13 +748,6 @@ gf_keybindings_class_init (GfKeybindingsClass *keybindings_class)
                   G_TYPE_FROM_CLASS (keybindings_class), G_SIGNAL_RUN_LAST,
                   0, g_signal_accumulator_first_wins, NULL, NULL,
                   G_TYPE_BOOLEAN, 1, GF_TYPE_KEYBINDING_TYPE);
-
-  properties[PROP_ISO_NEXT_GROUP] =
-    g_param_spec_boolean ("iso-next-group", "iso-next-group", "iso-next-group",
-                          FALSE, G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE |
-                          G_PARAM_STATIC_STRINGS);
-
-  g_object_class_install_properties (object_class, LAST_PROP, properties);
 }
 
 static void
@@ -862,8 +760,8 @@ gf_keybindings_init (GfKeybindings *keybindings)
 
   keybindings->keybindings = g_hash_table_new_full (NULL, NULL, NULL,
                                                     keybinding_free);
-  keybindings->iso_next_group_keybindings = g_hash_table_new_full (NULL, NULL, NULL,
-                                                                   keybinding_free);
+  keybindings->iso_group_keybindings = g_hash_table_new_full (NULL, NULL, NULL,
+                                                              keybinding_free);
 
   display = gdk_display_get_default ();
   xkb_major = XkbMajorVersion;
@@ -902,13 +800,25 @@ gf_keybindings_init (GfKeybindings *keybindings)
  * Returns: (transfer full): a newly created #GfKeybindings.
  */
 GfKeybindings *
-gf_keybindings_new (gboolean iso_next_group)
+gf_keybindings_new (void)
 {
   return g_object_new (GF_TYPE_KEYBINDINGS,
-                       "iso-next-group", iso_next_group,
                        NULL);
 }
 
+void
+gf_keybindings_grab_iso_group (GfKeybindings *keybindings,
+                               const gchar   *iso_group)
+{
+  if (g_strcmp0 (iso_group, keybindings->iso_group) == 0)
+    return;
+
+  g_free (keybindings->iso_group);
+  keybindings->iso_group = g_strdup (iso_group);
+
+  reload_iso_group_keybindings (keybindings);
+}
+
 /**
  * gf_keybindings_grab:
  * @keybindings: a #GfKeybindings
diff --git a/gnome-flashback/libcommon/gf-keybindings.h b/gnome-flashback/libcommon/gf-keybindings.h
index 0d31b7d..836b77d 100644
--- a/gnome-flashback/libcommon/gf-keybindings.h
+++ b/gnome-flashback/libcommon/gf-keybindings.h
@@ -31,19 +31,22 @@ typedef enum
 #define GF_TYPE_KEYBINDINGS gf_keybindings_get_type ()
 G_DECLARE_FINAL_TYPE (GfKeybindings, gf_keybindings, GF, KEYBINDINGS, GObject)
 
-GfKeybindings   *gf_keybindings_new           (gboolean       iso_next_group);
+GfKeybindings   *gf_keybindings_new            (void);
 
-guint            gf_keybindings_grab          (GfKeybindings *keybindings,
-                                               const gchar   *accelerator);
+void             gf_keybindings_grab_iso_group (GfKeybindings *keybindings,
+                                                const gchar   *iso_group);
 
-gboolean         gf_keybindings_ungrab        (GfKeybindings *keybindings,
-                                               guint          action);
+guint            gf_keybindings_grab           (GfKeybindings *keybindings,
+                                                const gchar   *accelerator);
 
-guint            gf_keybindings_get_keyval    (GfKeybindings *keybindings,
-                                               guint          action);
+gboolean         gf_keybindings_ungrab         (GfKeybindings *keybindings,
+                                                guint          action);
 
-GdkModifierType  gf_keybindings_get_modifiers (GfKeybindings *keybindings,
-                                               guint          action);
+guint            gf_keybindings_get_keyval     (GfKeybindings *keybindings,
+                                                guint          action);
+
+GdkModifierType  gf_keybindings_get_modifiers  (GfKeybindings *keybindings,
+                                                guint          action);
 
 G_END_DECLS
 
diff --git a/gnome-flashback/libinput-sources/gf-input-source-manager.c 
b/gnome-flashback/libinput-sources/gf-input-source-manager.c
index d78b17f..39bb0ea 100644
--- a/gnome-flashback/libinput-sources/gf-input-source-manager.c
+++ b/gnome-flashback/libinput-sources/gf-input-source-manager.c
@@ -595,11 +595,34 @@ accelerator_activated_cb (GfKeybindings *keybindings,
   gtk_widget_show (manager->popup);
 }
 
+static char *
+get_iso_group_from_xkb_options (gchar **xkb_options)
+{
+  gchar **p;
+  char *option;
+
+  option = NULL;
+
+  for (p = xkb_options; p && *p; ++p)
+    {
+      if (!g_str_has_prefix (*p, "grp:"))
+        continue;
+
+      option = g_strdup (*p + 4);
+      break;
+    }
+
+  return option;
+}
+
 static void
 keybindings_init (GfInputSourceManager *manager)
 {
+  gchar **options;
+  char *iso_group;
+
   manager->wm_keybindings = g_settings_new (DESKTOP_WM_KEYBINDINGS_SCHEMA);
-  manager->keybindings = gf_keybindings_new (TRUE);
+  manager->keybindings = gf_keybindings_new ();
 
   g_signal_connect (manager->wm_keybindings,
                     "changed::" KEY_SWITCH_INPUT_SOURCE,
@@ -611,6 +634,13 @@ keybindings_init (GfInputSourceManager *manager)
                     G_CALLBACK (switch_input_backward_changed_cb),
                     manager);
 
+  options = gf_input_source_settings_get_xkb_options (manager->settings);
+  iso_group = get_iso_group_from_xkb_options (options);
+  g_strfreev (options);
+
+  gf_keybindings_grab_iso_group (manager->keybindings, iso_group);
+  g_free (iso_group);
+
   g_signal_connect (manager->keybindings, "accelerator-activated",
                     G_CALLBACK (accelerator_activated_cb), manager);
   g_signal_connect (manager->keybindings, "modifiers-accelerator-activated",
@@ -1191,11 +1221,16 @@ xkb_options_changed_cb (GfInputSourceSettings *settings,
 {
   GfInputSourceManager *manager;
   gchar **options;
+  char *iso_group;
 
   manager = GF_INPUT_SOURCE_MANAGER (user_data);
 
   options = gf_input_source_settings_get_xkb_options (manager->settings);
 
+  iso_group = get_iso_group_from_xkb_options (options);
+  gf_keybindings_grab_iso_group (manager->keybindings, iso_group);
+  g_free (iso_group);
+
   gf_keyboard_manager_set_xkb_options (manager->keyboard_manager, options);
   g_strfreev (options);
 
@@ -1569,8 +1604,8 @@ gf_input_source_manager_init (GfInputSourceManager *manager)
 {
   manager->keyboard_manager = gf_keyboard_manager_new ();
 
-  keybindings_init (manager);
   input_source_settings_init (manager);
+  keybindings_init (manager);
 }
 
 GfInputSourceManager *
diff --git a/gnome-flashback/libshell/flashback-shell.c b/gnome-flashback/libshell/flashback-shell.c
index 7ebf37c..f50e6b1 100644
--- a/gnome-flashback/libshell/flashback-shell.c
+++ b/gnome-flashback/libshell/flashback-shell.c
@@ -539,7 +539,8 @@ flashback_shell_init (FlashbackShell *shell)
   shell->grabbed_accelerators = g_hash_table_new_full (NULL, NULL, NULL, g_free);
   shell->grabbers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
 
-  shell->keybindings = gf_keybindings_new (FALSE);
+  shell->keybindings = gf_keybindings_new ();
+
   g_signal_connect (shell->keybindings, "accelerator-activated",
                     G_CALLBACK (accelerator_activated), shell);
 


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