[Sugar-devel] [PATCH sugar-toolkit-gtk3] Key grabber: make grab_keys introspectable

Simon Schampijer simon at schampijer.de
Wed Aug 22 06:45:32 EDT 2012


To the grab_keys function we pass a list of strings the key
grabber should listen for. In order to make this
introspectable we need to annotate the function correctly. I
followed the instructions for passing arrays [1] and have
chosen the type of the array data looking at [2].

A simple example on how to test this is at [3]. The 'a'
keystroke is grabbed by the key grabber.

Signed-off-by: Simon Schampijer <simon at laptop.org>

[1] https://live.gnome.org/PyGObject/IntrospectionPorting#Passing_arrays
[2] https://live.gnome.org/GObjectIntrospection/Annotations#Default_Basic_Types
[3] http://dev.laptop.org/~erikos/touch/test_keyhandler.py
---
 src/sugar3/sugar-key-grabber.c | 41 ++++++++++++++++++++++++-----------------
 src/sugar3/sugar-key-grabber.h |  3 ++-
 2 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/src/sugar3/sugar-key-grabber.c b/src/sugar3/sugar-key-grabber.c
index 0810edc..74a0a95 100644
--- a/src/sugar3/sugar-key-grabber.c
+++ b/src/sugar3/sugar-key-grabber.c
@@ -199,34 +199,41 @@ grab_key (SugarKeyGrabber *grabber, Key *key, gboolean grab)
         }
 }
 
-
+/**
+ * sugar_key_grabber_grab_keys:
+ * @grabber: a #SugarKeyGrabber
+ * @keys: (array length=n_elements) (element-type utf8): array of
+ *     keys the grabber will listen to
+ * @n_elements: number of elements in @keys.
+ *
+ * Pass to the key grabber the keys it should listen to.
+ **/
 void
-sugar_key_grabber_grab_keys(SugarKeyGrabber *grabber, const char **keys)
+sugar_key_grabber_grab_keys(SugarKeyGrabber *grabber,
+			    const gchar  *keys[],
+			    gint          n_elements)
 {
-    const char **cur = keys;
+    gint i;
     const char *key;
     Key *keyinfo = NULL;
-    int min_keycodes, max_keycodes;
+    gint min_keycodes, max_keycodes;
 
     XDisplayKeycodes(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
                      &min_keycodes, &max_keycodes);
 
-    while (*cur != NULL) {
-        key = *cur;
-        cur += 1;
-
-        keyinfo = g_new0 (Key, 1);
-        keyinfo->key = g_strdup(key);
+    for (i = 0; i < n_elements; i++){
+	keyinfo = g_new0 (Key, 1);
+	keyinfo->key = g_strdup(keys[i]);
 
-        if (!egg_accelerator_parse_virtual (key, &keyinfo->keysym,
+        if (!egg_accelerator_parse_virtual (keys[i], &keyinfo->keysym,
                                             &keyinfo->keycode,
                                             &keyinfo->state)) {
-            g_warning ("Invalid key specified: %s", key);
+            g_warning ("Invalid key specified: %s", keys[i]);
             continue;
         }
 
         if (keyinfo->keycode < min_keycodes || keyinfo->keycode > max_keycodes) {
-            g_warning ("Keycode out of bounds: %d for key %s", keyinfo->keycode, key);
+            g_warning ("Keycode out of bounds: %d for key %s", keyinfo->keycode, keys[i]);
             continue;
         }
 
@@ -239,12 +246,13 @@ sugar_key_grabber_grab_keys(SugarKeyGrabber *grabber, const char **keys)
         if(!error_code)
             grabber->keys = g_list_append(grabber->keys, keyinfo);
         else if(error_code == BadAccess)
-            g_warning ("Grab failed, another application may already have access to key '%s'", key);
+            g_warning ("Grab failed, another application may already have access to key '%s'", keys[i]);
         else if(error_code == BadValue)
             g_warning ("Grab failed, invalid key %s specified. keysym: %u keycode: %u state: %u",
-                       key, keyinfo->keysym, keyinfo->keycode, keyinfo->state);
+                       keys[i], keyinfo->keysym, keyinfo->keycode, keyinfo->state);
         else
-            g_warning ("Grab failed for key '%s' for unknown reason '%d'", key, error_code);
+            g_warning ("Grab failed for key '%s' for unknown reason '%d'", keys[i], error_code);
+
     }
 }
 
@@ -285,4 +293,3 @@ sugar_key_grabber_is_modifier(SugarKeyGrabber *grabber, guint keycode, guint mas
 
 	return is_modifier;
 }
-
diff --git a/src/sugar3/sugar-key-grabber.h b/src/sugar3/sugar-key-grabber.h
index 9abc887..2afebd4 100644
--- a/src/sugar3/sugar-key-grabber.h
+++ b/src/sugar3/sugar-key-grabber.h
@@ -56,7 +56,8 @@ struct _SugarKeyGrabberClass {
 
 GType	 sugar_key_grabber_get_type	(void);
 void     sugar_key_grabber_grab_keys (SugarKeyGrabber *grabber,
-									  const char     **keys);
+				      const gchar     *keys[],
+				      gint          n_elements);
 char    *sugar_key_grabber_get_key  (SugarKeyGrabber *grabber,
 									 guint            keycode,
 									 guint            state);
-- 
1.7.11.4



More information about the Sugar-devel mailing list