[Xcb-commit] keysyms

Julien Danjou jdanjou at kemper.freedesktop.org
Sat Apr 11 10:04:04 PDT 2009


 keysyms/keysyms.c     |   53 +++++++++++++++++++++++++-------------------------
 keysyms/xcb_keysyms.h |   15 ++++++++++++--
 2 files changed, 40 insertions(+), 28 deletions(-)

New commits:
commit 3a11bcb5adb53035981b8910f7edcade845b49a5
Author: Julien Danjou <julien at danjou.info>
Date:   Sat Apr 11 19:02:55 2009 +0200

    keysyms: fix xcb_key_symbols_get_keycode() API
    
    xcb_key_symbols_get_keycode() returned a keycode.
    That's wrong, since actually a keysym can have multiple keycode. And we
    usually need all this keycodes to correctly grab a keysym.
    
    Signed-off-by: Julien Danjou <julien at danjou.info>

diff --git a/keysyms/keysyms.c b/keysyms/keysyms.c
index d1f1d52..c3e7f2e 100644
--- a/keysyms/keysyms.c
+++ b/keysyms/keysyms.c
@@ -235,34 +235,35 @@ xcb_keysym_t xcb_key_symbols_get_keysym (xcb_key_symbols_t *syms,
   return keysyms[col];
 }
 
-
-xcb_keycode_t
-xcb_key_symbols_get_keycode (xcb_key_symbols_t *syms,
-			 xcb_keysym_t      keysym)
+xcb_keycode_t *
+xcb_key_symbols_get_keycode(xcb_key_symbols_t *syms,
+                            xcb_keysym_t      keysym)
 {
-  xcb_keysym_t    ks;
-  xcb_keycode_t   keycode_null = { XCB_NO_SYMBOL };
-  int          i, j;
-
-  if (!syms)
-    return keycode_null;
-  
-  xcb_key_symbols_get_reply (syms, NULL);
-
-  for (j = 0; j < syms->u.reply->keysyms_per_keycode; j++)
-    {
-      for (i = xcb_get_setup (syms->c)->min_keycode; i <= xcb_get_setup (syms->c)->max_keycode; i++)
-	{
-	  xcb_keycode_t keycode;
-	  
-	  keycode = i;
-	  ks = xcb_key_symbols_get_keysym (syms, keycode, j);
-	  if (ks == keysym)
-	    return keycode;
-	}
-    }
+  xcb_keysym_t ks;
+  int j, nresult = 0;
+  xcb_keycode_t i, min, max, *result = NULL;
+
+  if(syms)
+  {
+      xcb_key_symbols_get_reply (syms, NULL);
+      min = xcb_get_setup(syms->c)->min_keycode;
+      max = xcb_get_setup(syms->c)->max_keycode;
+
+      for(j = 0; j < syms->u.reply->keysyms_per_keycode; j++)
+          for(i = min; i && i <= max; i++)
+          {
+              ks = xcb_key_symbols_get_keysym(syms, i, j);
+              if(ks == keysym)
+              {
+                  nresult++;
+                  result = realloc(result, sizeof(xcb_keycode_t) * (nresult + 1));
+                  result[nresult - 1] = i;
+                  result[nresult] = XCB_NO_SYMBOL;
+              }
+          }
+  }
   
-  return keycode_null;
+  return result;
 }
 
 xcb_keysym_t
diff --git a/keysyms/xcb_keysyms.h b/keysyms/xcb_keysyms.h
index 1202982..9d34a50 100644
--- a/keysyms/xcb_keysyms.h
+++ b/keysyms/xcb_keysyms.h
@@ -19,8 +19,19 @@ xcb_keysym_t      xcb_key_symbols_get_keysym    (xcb_key_symbols_t         *syms
 					  xcb_keycode_t             keycode,
 					  int                    col);
 
-xcb_keycode_t     xcb_key_symbols_get_keycode   (xcb_key_symbols_t         *syms,
-					  xcb_keysym_t              keysym);
+/**
+ * @brief Get the keycodes attached to a keysyms.
+ * There can be several value, so what is returned is an array of keycode
+ * terminated by XCB_NO_SYMBOL. You are responsible to free it.
+ * Be aware that this function can be slow. It will convert all
+ * combinations of all available keycodes to keysyms to find the ones that
+ * match.
+ * @param syms Key symbols.
+ * @param keysym The keysym to look for.
+ * @return A XCB_NO_SYMBOL terminated array of keycode, or NULL if nothing is found.
+ */
+xcb_keycode_t * xcb_key_symbols_get_keycode(xcb_key_symbols_t *syms,
+                                            xcb_keysym_t keysym);
 
 xcb_keysym_t      xcb_key_press_lookup_keysym   (xcb_key_symbols_t         *syms,
 					  xcb_key_press_event_t      *event,


More information about the xcb-commit mailing list