[Spice-devel] [PATCH 2/2] use Xkb to get keyboard modifier mask

Christophe Fergeau cfergeau at redhat.com
Tue Aug 16 05:53:48 PDT 2011


To be able to enable/disable keyboard leds on X11, we need to query
the X server for which mask correspond to which led (NumLock,
CapsLock). So far this was done using XKeysymToKeycode and iterating
over X modifier mapping.
Xkb provides XkbKeysymToModifiers for this purpose, and since
we're using Xkb anyway, it makes more sense to use it.

At some point, on my Fedora 15 box, XKeysymToKeycode was returning
NoSymbol for CapsLock and NumLock leading to spicec not being able
to change the keyboard leds when qemu tells it to. However, I couldn't
reproduce this when I tried again :-/
---
 client/x11/platform.cpp |   25 ++-----------------------
 1 files changed, 2 insertions(+), 23 deletions(-)

diff --git a/client/x11/platform.cpp b/client/x11/platform.cpp
index 2b2dfa8..670912c 100644
--- a/client/x11/platform.cpp
+++ b/client/x11/platform.cpp
@@ -2959,27 +2959,6 @@ static void init_xfixes()
         XFixesQueryVersion(x_display, &major, &minor) && major >= 1;
 }
 
-static unsigned int get_modifier_mask(KeySym modifier)
-{
-    int mask = 0;
-    int i;
-
-    XModifierKeymap* map = XGetModifierMapping(x_display);
-    KeyCode keycode = XKeysymToKeycode(x_display, modifier);
-    if (keycode == NoSymbol) {
-        XFreeModifiermap(map);
-        return 0;
-    }
-
-    for (i = 0; i < 8; i++) {
-        if (map->modifiermap[map->max_keypermod * i] == keycode) {
-            mask = 1 << i;
-        }
-    }
-    XFreeModifiermap(map);
-    return mask;
-}
-
 static void init_kbd()
 {
     int xkb_major = XkbMajorVersion;
@@ -2992,8 +2971,8 @@ static void init_kbd()
         !XkbQueryExtension(x_display, &opcode, &event, &error, &xkb_major, &xkb_minor)) {
         return;
     }
-    caps_lock_mask = get_modifier_mask(XK_Caps_Lock);
-    num_lock_mask = get_modifier_mask(XK_Num_Lock);
+    caps_lock_mask = XkbKeysymToModifiers(x_display, XK_Caps_Lock);
+    num_lock_mask = XkbKeysymToModifiers(x_display, XK_Num_Lock);
 }
 
 static void init_XIM()
-- 
1.7.6



More information about the Spice-devel mailing list