[Xfce4-commits] <libxfce4ui:jeromeg/keyboard-shortcuts-rework> Simplify computing of shortcut name in the dialog.

Jérôme Guelfucci noreply at xfce.org
Sun May 22 14:08:02 CEST 2011


Updating branch refs/heads/jeromeg/keyboard-shortcuts-rework
         to 899aaa97827f263c877faab3f8d0686fd9784b66 (commit)
       from 50d432cc506c231f033e2e466403e0f57695219e (commit)

commit 899aaa97827f263c877faab3f8d0686fd9784b66
Author: Jérôme Guelfucci <jeromeg at xfce.org>
Date:   Fri Apr 22 09:16:51 2011 +0200

    Simplify computing of shortcut name in the dialog.
    
    Instead of handling the key-pressed event with XKB code, we now use
    gdk/gtk functions directly which simplifies things a lot.
    
    This needs testing to ensure that there is no regression and that
    shortcuts with Numlock work.

 libxfce4kbd-private/xfce-shortcut-dialog.c |  108 +++++----------------------
 1 files changed, 20 insertions(+), 88 deletions(-)

diff --git a/libxfce4kbd-private/xfce-shortcut-dialog.c b/libxfce4kbd-private/xfce-shortcut-dialog.c
index 8049467..5f1b058 100644
--- a/libxfce4kbd-private/xfce-shortcut-dialog.c
+++ b/libxfce4kbd-private/xfce-shortcut-dialog.c
@@ -43,9 +43,6 @@ static gboolean xfce_shortcut_dialog_key_pressed      (XfceShortcutDialog      *
                                                        GdkEventKey             *event);
 static gboolean xfce_shortcut_dialog_key_released     (XfceShortcutDialog      *dialog,
                                                        GdkEventKey             *event);
-static gchar   *xfce_shortcut_dialog_shortcut_name    (XfceShortcutDialog      *dialog,
-                                                       guint                    keyval,
-                                                       guint                    modifiers);
 
 
 
@@ -312,13 +309,29 @@ static gboolean
 xfce_shortcut_dialog_key_pressed (XfceShortcutDialog *dialog,
                                   GdkEventKey        *event)
 {
-  gchar *text;
-  gchar *shortcut;
+  GdkKeymap       *keymap;
+  GdkModifierType  consumed, modifiers;
+  guint            keyval, mod_mask;
+  gchar           *text;
+  gchar           *shortcut;
 
   g_free (dialog->shortcut);
 
-  /* Determine and remember the current shortcut */
-  dialog->shortcut = xfce_shortcut_dialog_shortcut_name (dialog, event->keyval, event->state);
+  /* Get the keyboard state */
+  mod_mask = gtk_accelerator_get_default_mod_mask ();
+  keymap = gdk_keymap_get_default ();
+  modifiers = event->state;
+
+  gdk_keymap_translate_keyboard_state (keymap, event->hardware_keycode,
+                                       modifiers, 0,
+                                       &keyval, NULL, NULL, &consumed);
+
+  /* Get the modifiers */
+  modifiers &= ~consumed;
+  modifiers &= mod_mask;
+
+  /* Get and store the pressed shortcut */
+  dialog->shortcut = gtk_accelerator_name (keyval, modifiers);
 
   shortcut = g_markup_escape_text (dialog->shortcut, -1);
   text = g_strdup_printf ("<span size='large'><b>%s</b></span>", shortcut);
@@ -362,87 +375,6 @@ xfce_shortcut_dialog_key_released (XfceShortcutDialog *dialog,
 
 
 
-static gchar *
-xfce_shortcut_dialog_shortcut_name (XfceShortcutDialog *dialog,
-                                    guint               keyval,
-                                    guint               modifiers)
-{
-  XModifierKeymap *modmap;
-  Display         *display;
-  const KeySym    *keysyms;
-  KeyCode          keycode;
-  KeySym          *keymap;
-  gint             keysyms_per_keycode = 0;
-  gint             min_keycode = 0;
-  gint             max_keycode = 0;
-  gint             mask;
-  gint             i;
-  gint             j;
-
-  g_return_val_if_fail (XFCE_IS_SHORTCUT_DIALOG (dialog), NULL);
-
-  display = GDK_DISPLAY_XDISPLAY (gdk_display_get_default ());
-
-  gdk_error_trap_push ();
-
-  XDisplayKeycodes (display, &min_keycode, &max_keycode);
-
-  keymap = XGetKeyboardMapping (display, min_keycode, max_keycode - min_keycode + 1, &keysyms_per_keycode);
-
-  if (G_LIKELY (keymap != NULL))
-    {
-      modmap = XGetModifierMapping (display);
-
-      if (G_LIKELY (modmap != NULL))
-        {
-          for (i = 0; i < 8 * modmap->max_keypermod; ++i)
-            {
-              keycode = modmap->modifiermap[i];
-
-              if (keycode == 0 || keycode < min_keycode || keycode > max_keycode)
-                continue;
-
-              keysyms = keymap + (keycode - min_keycode) * keysyms_per_keycode;
-              mask = 1 << (i / modmap->max_keypermod);
-
-              for (j = 0; j < keysyms_per_keycode; ++j)
-                {
-                  if (keysyms[j] == GDK_Super_L || keysyms[j] == GDK_Super_R)
-                    modifiers &= ~mask;
-
-#if 0
-                  if (keysyms[j] == GDK_Meta_L || keysyms[j] == GDK_Meta_R)
-                    modifiers &= ~mask;
-#endif
-
-                  if (keysyms[j] == GDK_Hyper_L || keysyms[j] == GDK_Hyper_R)
-                    modifiers &= ~mask;
-
-                  if (keysyms[j] == GDK_Scroll_Lock)
-                    modifiers &= ~mask;
-
-                  if (keysyms[j] == GDK_Num_Lock)
-                    modifiers &= ~mask;
-
-                  if (keysyms[j] == GDK_Caps_Lock)
-                    modifiers &= ~mask;
-                }
-            }
-
-          XFreeModifiermap (modmap);
-        }
-
-      XFree (keymap);
-    }
-
-  gdk_flush ();
-  gdk_error_trap_pop ();
-
-  return gtk_accelerator_name (keyval, modifiers);
-}
-
-
-
 const gchar*
 xfce_shortcut_dialog_get_shortcut (XfceShortcutDialog *dialog)
 {



More information about the Xfce4-commits mailing list