2007-06-24 14:57:57 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006-2007, Red Hat, Inc.
|
2006-08-25 11:19:27 +02:00
|
|
|
*
|
2007-06-24 14:57:57 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
2006-08-25 11:19:27 +02:00
|
|
|
*
|
2007-06-24 14:57:57 +02:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-08-25 11:19:27 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2007-06-24 14:57:57 +02:00
|
|
|
* Lesser General Public License for more details.
|
2006-08-25 11:19:27 +02:00
|
|
|
*
|
2007-06-24 14:57:57 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
2006-08-25 11:19:27 +02:00
|
|
|
*/
|
|
|
|
|
2006-08-25 12:28:52 +02:00
|
|
|
#include <X11/X.h>
|
2011-11-15 22:56:24 +01:00
|
|
|
#include <X11/Xlib.h>
|
2012-12-13 15:20:29 +01:00
|
|
|
#include <X11/extensions/XInput2.h>
|
2006-08-25 14:03:48 +02:00
|
|
|
#include <gdk/gdk.h>
|
2011-11-15 22:56:24 +01:00
|
|
|
#include <gdk/gdkx.h>
|
2006-08-25 12:28:52 +02:00
|
|
|
|
2006-08-25 11:19:27 +02:00
|
|
|
#include "sugar-key-grabber.h"
|
2006-08-25 14:03:48 +02:00
|
|
|
#include "eggaccelerators.h"
|
2007-06-20 15:22:12 +02:00
|
|
|
#include "sugar-marshal.h"
|
2006-08-25 11:19:27 +02:00
|
|
|
|
2006-08-25 12:28:52 +02:00
|
|
|
/* we exclude shift, GDK_CONTROL_MASK and GDK_MOD1_MASK since we know what
|
|
|
|
these modifiers mean
|
|
|
|
these are the mods whose combinations are bound by the keygrabbing code */
|
|
|
|
#define IGNORED_MODS (0x2000 /*Xkb modifier*/ | GDK_LOCK_MASK | \
|
|
|
|
GDK_MOD2_MASK | GDK_MOD3_MASK | GDK_MOD4_MASK | GDK_MOD5_MASK)
|
2006-08-25 14:03:48 +02:00
|
|
|
/* these are the ones we actually use for global keys, we always only check
|
|
|
|
* for these set */
|
|
|
|
#define USED_MODS (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK)
|
2006-08-25 12:28:52 +02:00
|
|
|
|
2006-08-25 14:03:48 +02:00
|
|
|
enum {
|
|
|
|
KEY_PRESSED,
|
2006-09-21 14:08:10 +02:00
|
|
|
KEY_RELEASED,
|
2006-08-25 14:03:48 +02:00
|
|
|
N_SIGNALS
|
2006-08-25 11:19:27 +02:00
|
|
|
};
|
|
|
|
|
2006-08-25 14:03:48 +02:00
|
|
|
typedef struct {
|
|
|
|
char *key;
|
|
|
|
guint keysym;
|
|
|
|
guint state;
|
|
|
|
guint keycode;
|
|
|
|
} Key;
|
|
|
|
|
2006-08-25 11:19:27 +02:00
|
|
|
G_DEFINE_TYPE(SugarKeyGrabber, sugar_key_grabber, G_TYPE_OBJECT)
|
|
|
|
|
2006-08-25 14:03:48 +02:00
|
|
|
static guint signals[N_SIGNALS];
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_key_info(Key *key_info)
|
|
|
|
{
|
|
|
|
g_free(key_info->key);
|
|
|
|
g_free(key_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sugar_key_grabber_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
SugarKeyGrabber *grabber = SUGAR_KEY_GRABBER(object);
|
|
|
|
|
|
|
|
if (grabber->keys) {
|
|
|
|
g_list_foreach(grabber->keys, (GFunc)free_key_info, NULL);
|
|
|
|
g_list_free(grabber->keys);
|
|
|
|
grabber->keys = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-25 11:19:27 +02:00
|
|
|
static void
|
2006-08-25 14:03:48 +02:00
|
|
|
sugar_key_grabber_class_init(SugarKeyGrabberClass *grabber_class)
|
|
|
|
{
|
|
|
|
GObjectClass *g_object_class = G_OBJECT_CLASS (grabber_class);
|
|
|
|
|
|
|
|
g_object_class->dispose = sugar_key_grabber_dispose;
|
|
|
|
|
|
|
|
signals[KEY_PRESSED] = g_signal_new ("key-pressed",
|
|
|
|
G_TYPE_FROM_CLASS (grabber_class),
|
|
|
|
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
|
|
|
|
G_STRUCT_OFFSET (SugarKeyGrabberClass, key_pressed),
|
|
|
|
NULL, NULL,
|
2009-07-12 14:11:41 +02:00
|
|
|
sugar_marshal_BOOLEAN__UINT_UINT_UINT,
|
|
|
|
G_TYPE_BOOLEAN, 3,
|
|
|
|
G_TYPE_UINT,
|
2007-03-17 14:30:23 +01:00
|
|
|
G_TYPE_UINT,
|
|
|
|
G_TYPE_UINT);
|
2006-09-21 14:08:10 +02:00
|
|
|
signals[KEY_RELEASED] = g_signal_new ("key-released",
|
|
|
|
G_TYPE_FROM_CLASS (grabber_class),
|
|
|
|
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
|
|
|
|
G_STRUCT_OFFSET (SugarKeyGrabberClass, key_released),
|
|
|
|
NULL, NULL,
|
2009-07-12 14:11:41 +02:00
|
|
|
sugar_marshal_BOOLEAN__UINT_UINT_UINT,
|
|
|
|
G_TYPE_BOOLEAN, 3,
|
|
|
|
G_TYPE_UINT,
|
2007-03-17 14:30:23 +01:00
|
|
|
G_TYPE_UINT,
|
|
|
|
G_TYPE_UINT);
|
2006-09-21 14:08:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-17 14:30:23 +01:00
|
|
|
char *
|
|
|
|
sugar_key_grabber_get_key(SugarKeyGrabber *grabber, guint keycode, guint state)
|
2006-09-21 14:08:10 +02:00
|
|
|
{
|
|
|
|
GList *l;
|
2007-03-07 22:23:11 +01:00
|
|
|
|
2006-09-21 14:08:10 +02:00
|
|
|
for (l = grabber->keys; l != NULL; l = l->next) {
|
|
|
|
Key *keyinfo = (Key *)l->data;
|
2007-03-17 14:30:23 +01:00
|
|
|
if ((keyinfo->keycode == keycode) &&
|
|
|
|
((state & USED_MODS) == keyinfo->state)) {
|
|
|
|
return g_strdup(keyinfo->key);
|
2006-09-21 14:08:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2006-08-25 11:19:27 +02:00
|
|
|
}
|
|
|
|
|
2006-08-25 12:28:52 +02:00
|
|
|
static GdkFilterReturn
|
|
|
|
filter_events(GdkXEvent *xevent, GdkEvent *event, gpointer data)
|
|
|
|
{
|
2006-08-25 14:03:48 +02:00
|
|
|
SugarKeyGrabber *grabber = (SugarKeyGrabber *)data;
|
2006-08-25 12:28:52 +02:00
|
|
|
XEvent *xev = (XEvent *)xevent;
|
|
|
|
|
2006-09-21 14:08:10 +02:00
|
|
|
if (xev->type == KeyRelease) {
|
2007-03-17 14:30:23 +01:00
|
|
|
int return_value;
|
|
|
|
g_signal_emit (grabber, signals[KEY_RELEASED], 0, xev->xkey.keycode,
|
2009-07-12 14:11:41 +02:00
|
|
|
xev->xkey.state, xev->xkey.time, &return_value);
|
2007-03-17 14:30:23 +01:00
|
|
|
if(return_value)
|
|
|
|
return GDK_FILTER_REMOVE;
|
2006-09-21 14:08:10 +02:00
|
|
|
}
|
|
|
|
|
2006-08-25 14:03:48 +02:00
|
|
|
if (xev->type == KeyPress) {
|
2007-03-17 14:30:23 +01:00
|
|
|
int return_value;
|
|
|
|
g_signal_emit (grabber, signals[KEY_PRESSED], 0, xev->xkey.keycode,
|
2009-07-12 14:11:41 +02:00
|
|
|
xev->xkey.state, xev->xkey.time, &return_value);
|
2007-03-17 14:30:23 +01:00
|
|
|
if(return_value)
|
|
|
|
return GDK_FILTER_REMOVE;
|
2006-08-25 14:03:48 +02:00
|
|
|
}
|
2006-08-25 12:28:52 +02:00
|
|
|
|
2012-12-13 15:20:29 +01:00
|
|
|
if (xev->type == GenericEvent) {
|
|
|
|
XIDeviceEvent *ev;
|
|
|
|
int return_value = FALSE;
|
|
|
|
|
|
|
|
ev = (XIDeviceEvent *) ((XGenericEventCookie *) xev)->data;
|
|
|
|
|
|
|
|
if (ev->evtype == XI_KeyPress) {
|
|
|
|
g_signal_emit (grabber, signals[KEY_PRESSED], 0,
|
|
|
|
ev->detail, ev->mods.effective, ev->time, &return_value);
|
|
|
|
} else if (ev->evtype == XI_KeyRelease) {
|
|
|
|
g_signal_emit (grabber, signals[KEY_RELEASED], 0,
|
|
|
|
ev->detail, ev->mods.effective, ev->time, &return_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (return_value)
|
|
|
|
return GDK_FILTER_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-25 14:03:48 +02:00
|
|
|
return GDK_FILTER_CONTINUE;
|
2006-08-25 12:28:52 +02:00
|
|
|
}
|
|
|
|
|
2006-08-25 11:19:27 +02:00
|
|
|
static void
|
2006-08-25 12:28:52 +02:00
|
|
|
sugar_key_grabber_init(SugarKeyGrabber *grabber)
|
|
|
|
{
|
|
|
|
GdkScreen *screen;
|
|
|
|
|
|
|
|
screen = gdk_screen_get_default();
|
|
|
|
grabber->root = gdk_screen_get_root_window(screen);
|
2006-08-25 14:03:48 +02:00
|
|
|
grabber->keys = NULL;
|
2006-08-25 12:28:52 +02:00
|
|
|
|
|
|
|
gdk_window_add_filter(grabber->root, filter_events, grabber);
|
|
|
|
}
|
|
|
|
|
2009-08-25 19:55:48 +02:00
|
|
|
/* grab_key and grab_key_real are from
|
2006-08-25 14:03:48 +02:00
|
|
|
* gnome-control-center/gnome-settings-daemon/gnome-settings-multimedia-keys.c
|
|
|
|
*/
|
2006-08-25 12:28:52 +02:00
|
|
|
|
2008-08-24 14:31:46 +02:00
|
|
|
static void
|
2006-08-25 14:03:48 +02:00
|
|
|
grab_key_real (Key *key, GdkWindow *root, gboolean grab, int result)
|
2006-08-25 11:19:27 +02:00
|
|
|
{
|
2011-11-15 22:56:24 +01:00
|
|
|
Display *display = GDK_DISPLAY_XDISPLAY(gdk_display_get_default ());
|
2006-08-25 14:03:48 +02:00
|
|
|
if (grab)
|
2011-11-15 22:56:24 +01:00
|
|
|
XGrabKey (display, key->keycode, (result | key->state),
|
2006-08-25 14:03:48 +02:00
|
|
|
GDK_WINDOW_XID (root), True, GrabModeAsync, GrabModeAsync);
|
|
|
|
else
|
2011-11-15 22:56:24 +01:00
|
|
|
XUngrabKey(display, key->keycode, (result | key->state),
|
2006-08-25 14:03:48 +02:00
|
|
|
GDK_WINDOW_XID (root));
|
|
|
|
}
|
2006-08-25 12:28:52 +02:00
|
|
|
|
2006-08-25 14:03:48 +02:00
|
|
|
#define N_BITS 32
|
|
|
|
static void
|
|
|
|
grab_key (SugarKeyGrabber *grabber, Key *key, gboolean grab)
|
|
|
|
{
|
|
|
|
int indexes[N_BITS];/*indexes of bits we need to flip*/
|
|
|
|
int i, bit, bits_set_cnt;
|
|
|
|
int uppervalue;
|
2009-02-26 13:01:58 +01:00
|
|
|
guint mask_to_traverse = IGNORED_MODS & ~key->state & GDK_MODIFIER_MASK;
|
2006-08-25 14:03:48 +02:00
|
|
|
|
|
|
|
bit = 0;
|
|
|
|
for (i = 0; i < N_BITS; i++) {
|
|
|
|
if (mask_to_traverse & (1<<i))
|
|
|
|
indexes[bit++]=i;
|
|
|
|
}
|
|
|
|
|
|
|
|
bits_set_cnt = bit;
|
|
|
|
|
|
|
|
uppervalue = 1<<bits_set_cnt;
|
|
|
|
for (i = 0; i < uppervalue; i++) {
|
|
|
|
int j, result = 0;
|
|
|
|
|
|
|
|
for (j = 0; j < bits_set_cnt; j++) {
|
|
|
|
if (i & (1<<j))
|
|
|
|
result |= (1<<indexes[j]);
|
|
|
|
}
|
|
|
|
|
2008-08-24 14:31:46 +02:00
|
|
|
grab_key_real (key, grabber->root, grab, result);
|
2006-08-25 14:03:48 +02:00
|
|
|
}
|
2006-08-25 12:28:52 +02:00
|
|
|
}
|
|
|
|
|
2012-08-22 12:34:25 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
**/
|
2006-08-25 12:28:52 +02:00
|
|
|
void
|
2012-08-22 12:34:25 +02:00
|
|
|
sugar_key_grabber_grab_keys(SugarKeyGrabber *grabber,
|
|
|
|
const gchar *keys[],
|
|
|
|
gint n_elements)
|
2006-08-25 12:28:52 +02:00
|
|
|
{
|
2012-08-22 12:34:25 +02:00
|
|
|
gint i;
|
2009-02-26 13:01:58 +01:00
|
|
|
const char *key;
|
|
|
|
Key *keyinfo = NULL;
|
2012-08-22 12:34:25 +02:00
|
|
|
gint min_keycodes, max_keycodes;
|
2006-08-25 12:28:52 +02:00
|
|
|
|
2009-02-26 13:01:58 +01:00
|
|
|
XDisplayKeycodes(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
|
|
|
|
&min_keycodes, &max_keycodes);
|
2008-08-24 14:31:46 +02:00
|
|
|
|
2012-08-22 12:34:25 +02:00
|
|
|
for (i = 0; i < n_elements; i++){
|
|
|
|
keyinfo = g_new0 (Key, 1);
|
|
|
|
keyinfo->key = g_strdup(keys[i]);
|
2008-08-24 14:31:46 +02:00
|
|
|
|
2012-08-22 12:34:25 +02:00
|
|
|
if (!egg_accelerator_parse_virtual (keys[i], &keyinfo->keysym,
|
2009-02-26 13:01:58 +01:00
|
|
|
&keyinfo->keycode,
|
|
|
|
&keyinfo->state)) {
|
2012-08-22 12:34:25 +02:00
|
|
|
g_warning ("Invalid key specified: %s", keys[i]);
|
2009-02-26 13:01:58 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keyinfo->keycode < min_keycodes || keyinfo->keycode > max_keycodes) {
|
2012-08-22 12:34:25 +02:00
|
|
|
g_warning ("Keycode out of bounds: %d for key %s", keyinfo->keycode, keys[i]);
|
2009-02-26 13:01:58 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
gdk_error_trap_push();
|
2008-08-24 14:31:46 +02:00
|
|
|
|
|
|
|
grab_key(grabber, keyinfo, TRUE);
|
2006-08-25 12:28:52 +02:00
|
|
|
|
2009-02-26 13:01:58 +01:00
|
|
|
gdk_flush();
|
|
|
|
gint error_code = gdk_error_trap_pop ();
|
|
|
|
if(!error_code)
|
|
|
|
grabber->keys = g_list_append(grabber->keys, keyinfo);
|
|
|
|
else if(error_code == BadAccess)
|
2012-08-22 12:34:25 +02:00
|
|
|
g_warning ("Grab failed, another application may already have access to key '%s'", keys[i]);
|
2009-02-26 13:01:58 +01:00
|
|
|
else if(error_code == BadValue)
|
|
|
|
g_warning ("Grab failed, invalid key %s specified. keysym: %u keycode: %u state: %u",
|
2012-08-22 12:34:25 +02:00
|
|
|
keys[i], keyinfo->keysym, keyinfo->keycode, keyinfo->state);
|
2009-02-26 13:01:58 +01:00
|
|
|
else
|
2012-08-22 12:34:25 +02:00
|
|
|
g_warning ("Grab failed for key '%s' for unknown reason '%d'", keys[i], error_code);
|
|
|
|
|
2008-08-24 14:31:46 +02:00
|
|
|
}
|
2006-08-25 11:19:27 +02:00
|
|
|
}
|
2008-06-20 14:46:09 +02:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
sugar_key_grabber_is_modifier(SugarKeyGrabber *grabber, guint keycode, guint mask)
|
|
|
|
{
|
|
|
|
Display *xdisplay;
|
|
|
|
XModifierKeymap *modmap;
|
|
|
|
gint start, end, i, mod_index;
|
|
|
|
gboolean is_modifier = FALSE;
|
|
|
|
|
2011-11-15 22:56:24 +01:00
|
|
|
xdisplay = GDK_DISPLAY_XDISPLAY(gdk_display_get_default ());
|
2008-06-20 14:46:09 +02:00
|
|
|
|
|
|
|
modmap = XGetModifierMapping(xdisplay);
|
|
|
|
|
|
|
|
if (mask != -1) {
|
|
|
|
mod_index = 0;
|
|
|
|
mask = mask >> 1;
|
|
|
|
while (mask != 0) {
|
|
|
|
mask = mask >> 1;
|
|
|
|
mod_index += 1;
|
|
|
|
}
|
|
|
|
start = mod_index * modmap->max_keypermod;
|
|
|
|
end = (mod_index + 1) * modmap->max_keypermod;
|
|
|
|
} else {
|
|
|
|
start = 0;
|
|
|
|
end = 8 * modmap->max_keypermod;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = start; i < end; i++) {
|
|
|
|
if (keycode == modmap->modifiermap[i]) {
|
|
|
|
is_modifier = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2008-06-20 14:46:09 +02:00
|
|
|
XFreeModifiermap (modmap);
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2008-06-20 14:46:09 +02:00
|
|
|
return is_modifier;
|
|
|
|
}
|