Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/sugar-key-grabber.c
blob: 89b743fc902ee5f3934abdf17986152547860899 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * Copyright (C) 2006-2007, Red Hat, Inc.
 *
 * 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.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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.
 */

#include <X11/X.h>
#include <gdk/gdkscreen.h>
#include <gdk/gdkx.h>
#include <gdk/gdk.h>

#include "sugar-key-grabber.h"
#include "eggaccelerators.h"
#include "sugar-marshal.h"

/* 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)
/* 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)

enum {
	KEY_PRESSED,
	KEY_RELEASED,
	N_SIGNALS
};

typedef struct {
  char *key;
  guint keysym;
  guint state;
  guint keycode;
} Key;

G_DEFINE_TYPE(SugarKeyGrabber, sugar_key_grabber, G_TYPE_OBJECT)

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;
	}
}

static void
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,
                         sugar_marshal_BOOLEAN__UINT_UINT,
                         G_TYPE_BOOLEAN, 2,
                         G_TYPE_UINT,
                         G_TYPE_UINT);
	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,
                         sugar_marshal_BOOLEAN__UINT_UINT,
                         G_TYPE_BOOLEAN, 2,
                         G_TYPE_UINT,
                         G_TYPE_UINT);
}

char *
sugar_key_grabber_get_key(SugarKeyGrabber *grabber, guint keycode, guint state)
{
	GList *l;

	for (l = grabber->keys; l != NULL; l = l->next) {
		Key *keyinfo = (Key *)l->data;
		if ((keyinfo->keycode == keycode) &&
			((state & USED_MODS) == keyinfo->state)) {
			return g_strdup(keyinfo->key);
		}
	}

	return NULL;
}

static GdkFilterReturn
filter_events(GdkXEvent *xevent, GdkEvent *event, gpointer data)
{
	SugarKeyGrabber *grabber = (SugarKeyGrabber *)data;
	XEvent *xev = (XEvent *)xevent;

	if (xev->type == KeyRelease) {
		int return_value;
		g_signal_emit (grabber, signals[KEY_RELEASED], 0, xev->xkey.keycode,
					   xev->xkey.state, &return_value);
		if(return_value)
			return GDK_FILTER_REMOVE;
	}

	if (xev->type == KeyPress) {
		int return_value;
		g_signal_emit (grabber, signals[KEY_PRESSED], 0, xev->xkey.keycode,
					   xev->xkey.state, &return_value);
		if(return_value)
			return GDK_FILTER_REMOVE;
	}

	return GDK_FILTER_CONTINUE;
}

static void
sugar_key_grabber_init(SugarKeyGrabber *grabber)
{
	GdkScreen *screen;

	screen = gdk_screen_get_default();
	grabber->root = gdk_screen_get_root_window(screen);
	grabber->keys = NULL;

	gdk_window_add_filter(grabber->root, filter_events, grabber);
}

/* grab_key and grab_key_real are from 
 * gnome-control-center/gnome-settings-daemon/gnome-settings-multimedia-keys.c
 */

static void
grab_key_real (Key *key, GdkWindow *root, gboolean grab, int result)
{
        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));
}

#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;
        guint mask_to_traverse = IGNORED_MODS & ~ key->state;

        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]);
                }

                grab_key_real (key, grabber->root, grab, result);
        }
}


void
sugar_key_grabber_grab_keys(SugarKeyGrabber *grabber, const char **keys)
{
    char **cur = keys;
    char *key;
	Key *keyinfo;

    gdk_error_trap_push();

    while (*cur != NULL) {
        key = *cur;
        cur += 1;
        
        keyinfo = g_new0 (Key, 1);
        keyinfo->key = g_strdup(key);

        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
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;

	xdisplay = gdk_x11_drawable_get_xdisplay(GDK_DRAWABLE (grabber->root));

	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;
		}
	}
	
	XFreeModifiermap (modmap);
	
	return is_modifier;
}