From 3d3e453157ec8c1b107fac2bca5c46681c6cb0ad Mon Sep 17 00:00:00 2001 From: Benjamin Berg Date: Sun, 24 Aug 2008 14:31:46 +0200 Subject: [PATCH] Change the API of the keygrabber so only one gdk_sync is neccessary. (ticket #7999) --- src/sugar/_sugarext.defs | 6 ++--- src/sugar/_sugarext.override | 45 +++++++++++++++++++++++++++++++++++ src/sugar/sugar-key-grabber.c | 39 +++++++++++++++++------------- src/sugar/sugar-key-grabber.h | 4 ++-- 4 files changed, 73 insertions(+), 21 deletions(-) diff --git a/src/sugar/_sugarext.defs b/src/sugar/_sugarext.defs index 6e741dca..87d629f6 100644 --- a/src/sugar/_sugarext.defs +++ b/src/sugar/_sugarext.defs @@ -102,12 +102,12 @@ (return-type "GType") ) -(define-method grab +(define-method grab_keys (of-object "SugarKeyGrabber") - (c-name "sugar_key_grabber_grab") + (c-name "sugar_key_grabber_grab_keys") (return-type "none") (parameters - '("const-char*" "key") + '("const-char*[]" "keys") ) ) diff --git a/src/sugar/_sugarext.override b/src/sugar/_sugarext.override index dc54de02..b9936004 100644 --- a/src/sugar/_sugarext.override +++ b/src/sugar/_sugarext.override @@ -33,3 +33,48 @@ ignore-glob *_get_type _* %% +override sugar_key_grabber_grab_keys kwargs +static PyObject * +_wrap_sugar_key_grabber_grab_keys(PyGObject *self, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = { "key", NULL }; + PyObject *py_keys; + char **keys; + int i, len; + + if (!PyArg_ParseTupleAndKeywords(args,kwargs, + "O:SugarKeyGrabber.grab_keys", + kwlist, &py_keys)) + return NULL; + + if (!PySequence_Check(py_keys) || (len = PySequence_Size(py_keys)) < 0) { + PyErr_SetString(PyExc_ValueError, + "keys should be a sequence of strings"); + return NULL; + } + + keys = g_new(char*, len + 1); + for (i = 0; i < len; i++) { + PyObject *item = PySequence_GetItem(py_keys, i); + if (!item) { + g_free(keys); + return NULL; + } + if (!PyString_Check(item)) { + PyErr_SetString(PyExc_TypeError, "key must be a string"); + g_free(keys); + Py_DECREF(item); + return NULL; + } + keys[i] = PyString_AsString(item); + Py_DECREF(item); + } + keys[len] = NULL; + + sugar_key_grabber_grab_keys (SUGAR_KEY_GRABBER(self->obj), (const char**) keys); + + Py_INCREF(Py_None); + return Py_None; +} + +%% diff --git a/src/sugar/sugar-key-grabber.c b/src/sugar/sugar-key-grabber.c index 8e18c864..89b743fc 100644 --- a/src/sugar/sugar-key-grabber.c +++ b/src/sugar/sugar-key-grabber.c @@ -155,21 +155,15 @@ sugar_key_grabber_init(SugarKeyGrabber *grabber) * gnome-control-center/gnome-settings-daemon/gnome-settings-multimedia-keys.c */ -static gboolean +static void grab_key_real (Key *key, GdkWindow *root, gboolean grab, int result) { - gdk_error_trap_push (); if (grab) XGrabKey (GDK_DISPLAY(), key->keycode, (result | key->state), GDK_WINDOW_XID (root), True, GrabModeAsync, GrabModeAsync); else XUngrabKey(GDK_DISPLAY(), key->keycode, (result | key->state), GDK_WINDOW_XID (root)); - gdk_flush (); - - gdk_error_trap_pop (); - - return TRUE; } #define N_BITS 32 @@ -198,24 +192,37 @@ grab_key (SugarKeyGrabber *grabber, Key *key, gboolean grab) result |= (1<root, grab, result) == FALSE) - return; + grab_key_real (key, grabber->root, grab, result); } } + void -sugar_key_grabber_grab(SugarKeyGrabber *grabber, const char *key) +sugar_key_grabber_grab_keys(SugarKeyGrabber *grabber, const char **keys) { + char **cur = keys; + char *key; Key *keyinfo; - keyinfo = g_new0 (Key, 1); - keyinfo->key = g_strdup(key); - egg_accelerator_parse_virtual (key, &keyinfo->keysym, - &keyinfo->keycode, &keyinfo->state); + gdk_error_trap_push(); - grab_key(grabber, keyinfo, TRUE); + while (*cur != NULL) { + key = *cur; + cur += 1; + + keyinfo = g_new0 (Key, 1); + keyinfo->key = g_strdup(key); - grabber->keys = g_list_append(grabber->keys, keyinfo); + egg_accelerator_parse_virtual (key, &keyinfo->keysym, + &keyinfo->keycode, &keyinfo->state); + + grab_key(grabber, keyinfo, TRUE); + + grabber->keys = g_list_append(grabber->keys, keyinfo); + } + + gdk_flush(); + gdk_error_trap_push(); } gboolean diff --git a/src/sugar/sugar-key-grabber.h b/src/sugar/sugar-key-grabber.h index f131ee3f..ab028705 100644 --- a/src/sugar/sugar-key-grabber.h +++ b/src/sugar/sugar-key-grabber.h @@ -55,8 +55,8 @@ struct _SugarKeyGrabberClass { }; GType sugar_key_grabber_get_type (void); -void sugar_key_grabber_grab (SugarKeyGrabber *grabber, - const char *key); +void sugar_key_grabber_grab_keys (SugarKeyGrabber *grabber, + const char **keys); char *sugar_key_grabber_get_key (SugarKeyGrabber *grabber, guint keycode, guint state);