gnome-control-center r8608 - trunk/capplets/keybindings



Author: jensg
Date: Sat Mar 29 11:48:06 2008
New Revision: 8608
URL: http://svn.gnome.org/viewvc/gnome-control-center?rev=8608&view=rev

Log:
2008-03-29  Jens Granseuer  <jensgr gmx net>

	* eggaccelerators.c: (egg_accelerator_parse_virtual),
	(egg_virtual_accelerator_name), (egg_virtual_accelerator_label),
	(egg_keymap_resolve_virtual_modifiers),
	(egg_keymap_virtualize_modifiers), (reload_modmap),
	(egg_keymap_get_modmap):
	* eggaccelerators.h: fix mismatched modifier mapping between egg
	and GTK (so that e.g. <Super> works) and replace some custom
	functionality with stock GTK


Modified:
   trunk/capplets/keybindings/ChangeLog
   trunk/capplets/keybindings/eggaccelerators.c
   trunk/capplets/keybindings/eggaccelerators.h

Modified: trunk/capplets/keybindings/eggaccelerators.c
==============================================================================
--- trunk/capplets/keybindings/eggaccelerators.c	(original)
+++ trunk/capplets/keybindings/eggaccelerators.c	Sat Mar 29 11:48:06 2008
@@ -206,7 +206,7 @@
  * can represent various keyboard keys (numlock, meta, hyper, etc.),
  * the virtual modifier represents the keyboard key, the concrete
  * modifier the actual Mod2-Mod5 bits in the key press event.
- * 
+ *
  * Returns: %TRUE on success.
  */
 gboolean
@@ -219,7 +219,7 @@
   GdkModifierType mods;
   gint len;
   gboolean bad_keyval;
-  
+
   if (accelerator_key)
     *accelerator_key = 0;
   if (accelerator_mods)
@@ -230,7 +230,7 @@
   g_return_val_if_fail (accelerator != NULL, FALSE);
 
   bad_keyval = FALSE;
-  
+
   keyval = 0;
   mods = 0;
   len = strlen (accelerator);
@@ -313,7 +313,7 @@
 	  else
 	    {
 	      gchar last_ch;
-	      
+
 	      last_ch = *accelerator;
 	      while (last_ch && last_ch != '>')
 		{
@@ -329,7 +329,7 @@
 
           if (keyval == 0)
 	    {
-	      /* If keyval is 0, than maybe it's a keycode.  Check for 0x## */
+	      /* If keyval is 0, then maybe it's a keycode.  Check for 0x## */
 	      if (len >= 4 && is_keycode (accelerator))
 		{
 		  char keystring[5];
@@ -359,7 +359,7 @@
           len -= len;
 	}
     }
-  
+
   if (accelerator_key)
     *accelerator_key = gdk_keyval_to_lower (keyval);
   if (accelerator_mods)
@@ -373,7 +373,7 @@
  * @accelerator_key:  accelerator keyval
  * @accelerator_mods: accelerator modifier mask
  * @returns:          a newly-allocated accelerator name
- * 
+ *
  * Converts an accelerator keyval and modifier mask
  * into a string parseable by egg_accelerator_parse_virtual().
  * For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
@@ -386,124 +386,21 @@
 			      guint		     keycode,
                               EggVirtualModifierType accelerator_mods)
 {
-  static const gchar text_release[] = "<Release>";
-  static const gchar text_shift[] = "<Shift>";
-  static const gchar text_control[] = "<Control>";
-  static const gchar text_mod1[] = "<Alt>";
-  static const gchar text_mod2[] = "<Mod2>";
-  static const gchar text_mod3[] = "<Mod3>";
-  static const gchar text_mod4[] = "<Mod4>";
-  static const gchar text_mod5[] = "<Mod5>";
-  static const gchar text_meta[] = "<Meta>";
-  static const gchar text_super[] = "<Super>";
-  static const gchar text_hyper[] = "<Hyper>";
-  guint l;
-  gchar *keyval_name, *str = NULL;
-  gchar *accelerator;
+  gchar *gtk_name;
+  GdkModifierType gdkmods = 0;
 
-  accelerator_mods &= EGG_VIRTUAL_MODIFIER_MASK;
+  egg_keymap_resolve_virtual_modifiers (NULL, accelerator_mods, &gdkmods);
+  gtk_name = gtk_accelerator_name (accelerator_key, gdkmods);
 
   if (!accelerator_key)
     {
-      str = g_strdup_printf ("0x%02x", keycode);
-      keyval_name = str;
-    }
-  else
-    {
-      keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
-      if (!keyval_name)
-        keyval_name = "";
-    }
-
-  l = 0;
-  if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
-    l += sizeof (text_release) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
-    l += sizeof (text_shift) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
-    l += sizeof (text_control) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
-    l += sizeof (text_mod1) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
-    l += sizeof (text_mod2) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
-    l += sizeof (text_mod3) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
-    l += sizeof (text_mod4) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
-    l += sizeof (text_mod5) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_META_MASK)
-    l += sizeof (text_meta) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
-    l += sizeof (text_hyper) - 1;
-  if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
-    l += sizeof (text_super) - 1;
-  l += strlen (keyval_name);
-
-  accelerator = g_new (gchar, l + 1);
-
-  l = 0;
-  accelerator[l] = 0;
-  if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
-    {
-      strcpy (accelerator + l, text_release);
-      l += sizeof (text_release) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
-    {
-      strcpy (accelerator + l, text_shift);
-      l += sizeof (text_shift) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
-    {
-      strcpy (accelerator + l, text_control);
-      l += sizeof (text_control) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
-    {
-      strcpy (accelerator + l, text_mod1);
-      l += sizeof (text_mod1) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
-    {
-      strcpy (accelerator + l, text_mod2);
-      l += sizeof (text_mod2) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
-    {
-      strcpy (accelerator + l, text_mod3);
-      l += sizeof (text_mod3) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
-    {
-      strcpy (accelerator + l, text_mod4);
-      l += sizeof (text_mod4) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
-    {
-      strcpy (accelerator + l, text_mod5);
-      l += sizeof (text_mod5) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_META_MASK)
-    {
-      strcpy (accelerator + l, text_meta);
-      l += sizeof (text_meta) - 1;
+	gchar *name;
+	name = g_strdup_printf ("%s0x%02x", gtk_name, keycode);
+	g_free (gtk_name);
+	return name;
     }
-  if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
-    {
-      strcpy (accelerator + l, text_hyper);
-      l += sizeof (text_hyper) - 1;
-    }
-  if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
-    {
-      strcpy (accelerator + l, text_super);
-      l += sizeof (text_super) - 1;
-    }
-  
-  strcpy (accelerator + l, keyval_name);
-  g_free (str);
 
-  return accelerator;
+  return gtk_name;
 }
 
 /**
@@ -511,7 +408,7 @@
  * @accelerator_key:  accelerator keyval
  * @accelerator_mods: accelerator modifier mask
  * @returns:          a newly-allocated accelerator label
- * 
+ *
  * Converts an accelerator keyval and modifier mask
  * into a (possibly translated) string that can be displayed to
  * a user.
@@ -526,10 +423,9 @@
 			       EggVirtualModifierType accelerator_mods)
 {
   gchar *gtk_label;
-  GdkModifierType gdkmods;
+  GdkModifierType gdkmods = 0;
 
-  egg_keymap_resolve_virtual_modifiers (gdk_keymap_get_default (),
-					accelerator_mods, &gdkmods);
+  egg_keymap_resolve_virtual_modifiers (NULL, accelerator_mods, &gdkmods);
   gtk_label = gtk_accelerator_get_label (accelerator_key, gdkmods);
 
   if (!accelerator_key)
@@ -552,21 +448,18 @@
   int i;
   const EggModmap *modmap;
 
-  g_return_if_fail (GDK_IS_KEYMAP (keymap));
   g_return_if_fail (concrete_mods != NULL);
-  
+  g_return_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap));
+
   modmap = egg_keymap_get_modmap (keymap);
-  
+
   /* Not so sure about this algorithm. */
-  
+
   concrete = 0;
-  i = 0;
-  while (i < EGG_MODMAP_ENTRY_LAST)
+  for (i = 0; i < EGG_MODMAP_ENTRY_LAST; ++i)
     {
       if (modmap->mapping[i] & virtual_mods)
-        concrete |= (1 << i);
-
-      ++i;
+        concrete |= MODMAP_ENTRY_TO_MODIFIER (i);
     }
 
   *concrete_mods = concrete;
@@ -580,27 +473,26 @@
   GdkModifierType virtual;
   int i;
   const EggModmap *modmap;
-  
-  g_return_if_fail (GDK_IS_KEYMAP (keymap));
+
   g_return_if_fail (virtual_mods != NULL);
+  g_return_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap));
 
   modmap = egg_keymap_get_modmap (keymap);
-  
+
   /* Not so sure about this algorithm. */
-  
+
   virtual = 0;
-  i = 0;
-  while (i < EGG_MODMAP_ENTRY_LAST)
+  for (i = 0; i < EGG_MODMAP_ENTRY_LAST; ++i)
     {
-      if ((1 << i) & concrete_mods)
+      if (MODMAP_ENTRY_TO_MODIFIER (i) & concrete_mods)
         {
           EggVirtualModifierType cleaned;
-          
+
           cleaned = modmap->mapping[i] & ~(EGG_VIRTUAL_MOD2_MASK |
                                            EGG_VIRTUAL_MOD3_MASK |
                                            EGG_VIRTUAL_MOD4_MASK |
                                            EGG_VIRTUAL_MOD5_MASK);
-          
+
           if (cleaned != 0)
             {
               virtual |= cleaned;
@@ -613,10 +505,8 @@
               virtual |= modmap->mapping[i];
             }
         }
-      
-      ++i;
     }
-  
+
   *virtual_mods = virtual;
 }
 
@@ -632,13 +522,12 @@
   xmodmap = XGetModifierMapping (gdk_x11_get_default_xdisplay ());
 
   memset (modmap->mapping, 0, sizeof (modmap->mapping));
-  
-  /* there are 8 modifiers, and the first 3 are shift, shift lock,
-   * and control
+
+  /* there are 8 modifiers in the order shift, shift lock,
+   * control, mod1-5 with up to max_keypermod bindings each
    */
   map_size = 8 * xmodmap->max_keypermod;
-  i = 3 * xmodmap->max_keypermod;
-  while (i < map_size)
+  for (i = 3 * xmodmap->max_keypermod; i < map_size; ++i)
     {
       /* get the key code at this point in the map,
        * see if its keysym is one we're interested in
@@ -649,7 +538,7 @@
       int n_entries;
       int j;
       EggVirtualModifierType mask;
-      
+
       keys = NULL;
       keyvals = NULL;
       n_entries = 0;
@@ -657,11 +546,10 @@
       gdk_keymap_get_entries_for_keycode (keymap,
                                           keycode,
                                           &keys, &keyvals, &n_entries);
-      
+
       mask = 0;
-      j = 0;
-      while (j < n_entries)
-        {          
+      for (j = 0; j < n_entries; ++j)
+        {
           if (keyvals[j] == GDK_Num_Lock)
             mask |= EGG_VIRTUAL_NUM_LOCK_MASK;
           else if (keyvals[j] == GDK_Scroll_Lock)
@@ -677,20 +565,16 @@
             mask |= EGG_VIRTUAL_SUPER_MASK;
           else if (keyvals[j] == GDK_Mode_switch)
             mask |= EGG_VIRTUAL_MODE_SWITCH_MASK;
-          
-          ++j;
         }
 
       /* Mod1Mask is 1 << 3 for example, i.e. the
        * fourth modifier, i / keyspermod is the modifier
        * index
-       */      
+       */
       modmap->mapping[i/xmodmap->max_keypermod] |= mask;
-      
+
       g_free (keyvals);
-      g_free (keys);      
-      
-      ++i;
+      g_free (keys);
     }
 
   /* Add in the not-really-virtual fixed entries */
@@ -702,7 +586,7 @@
   modmap->mapping[EGG_MODMAP_ENTRY_MOD3] |= EGG_VIRTUAL_MOD3_MASK;
   modmap->mapping[EGG_MODMAP_ENTRY_MOD4] |= EGG_VIRTUAL_MOD4_MASK;
   modmap->mapping[EGG_MODMAP_ENTRY_MOD5] |= EGG_VIRTUAL_MOD5_MASK;
-  
+
   XFreeModifiermap (xmodmap);
 }
 
@@ -711,12 +595,14 @@
 {
   EggModmap *modmap;
 
+  if (keymap == NULL)
+    keymap = gdk_keymap_get_default ();
+
   /* This is all a hack, much simpler when we can just
    * modify GDK directly.
    */
-  
-  modmap = g_object_get_data (G_OBJECT (keymap),
-                              "egg-modmap");
+
+  modmap = g_object_get_data (G_OBJECT (keymap), "egg-modmap");
 
   if (modmap == NULL)
     {
@@ -725,9 +611,9 @@
       /* FIXME modify keymap change events with an event filter
        * and force a reload if we get one
        */
-      
+
       reload_modmap (keymap, modmap);
-      
+
       g_object_set_data_full (G_OBJECT (keymap),
                               "egg-modmap",
                               modmap,
@@ -735,6 +621,6 @@
     }
 
   g_assert (modmap != NULL);
-  
+
   return modmap;
 }

Modified: trunk/capplets/keybindings/eggaccelerators.h
==============================================================================
--- trunk/capplets/keybindings/eggaccelerators.h	(original)
+++ trunk/capplets/keybindings/eggaccelerators.h	Sat Mar 29 11:48:06 2008
@@ -36,7 +36,7 @@
   EGG_VIRTUAL_CONTROL_MASK  = 1 << 2,
 
   EGG_VIRTUAL_ALT_MASK      = 1 << 3, /* fixed as Mod1 */
-  
+
   EGG_VIRTUAL_MOD2_MASK	    = 1 << 4,
   EGG_VIRTUAL_MOD3_MASK	    = 1 << 5,
   EGG_VIRTUAL_MOD4_MASK	    = 1 << 6,
@@ -50,21 +50,23 @@
   GDK_BUTTON5_MASK  = 1 << 12,
   /* 13, 14 are used by Xkb for the keyboard group */
 #endif
-  
-  EGG_VIRTUAL_META_MASK = 1 << 24,
-  EGG_VIRTUAL_SUPER_MASK = 1 << 25,
-  EGG_VIRTUAL_HYPER_MASK = 1 << 26,
-  EGG_VIRTUAL_MODE_SWITCH_MASK = 1 << 27, 
-  EGG_VIRTUAL_NUM_LOCK_MASK = 1 << 28,
-  EGG_VIRTUAL_SCROLL_LOCK_MASK = 1 << 29,
+
+  EGG_VIRTUAL_MODE_SWITCH_MASK = 1 << 23,
+  EGG_VIRTUAL_NUM_LOCK_MASK = 1 << 24,
+  EGG_VIRTUAL_SCROLL_LOCK_MASK = 1 << 25,
+
+  /* Also in GdkModifierType */
+  EGG_VIRTUAL_SUPER_MASK = 1 << 26,
+  EGG_VIRTUAL_HYPER_MASK = 1 << 27,
+  EGG_VIRTUAL_META_MASK = 1 << 28,
 
   /* Also in GdkModifierType */
   EGG_VIRTUAL_RELEASE_MASK  = 1 << 30,
 
   /*     28-31 24-27 20-23 16-19 12-15 8-11 4-7 0-3
-   *       7     f     0     0     0    0    f   f
-   */  
-  EGG_VIRTUAL_MODIFIER_MASK = 0x7f0000ff
+   *       5     f     8     0     0    0    f   f
+   */
+  EGG_VIRTUAL_MODIFIER_MASK = 0x5f8000ff
 
 } EggVirtualModifierType;
 



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