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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/* tomboykeybinder.c
* Copyright (C) 2008 Novell
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
*/
#include <string.h>
#include <gdk/gdk.h>
#include <gdk/gdkwindow.h>
#include <gdk/gdkx.h>
#include <X11/Xlib.h>
#include "eggaccelerators.h"
#include "tomboykeybinder.h"
/* Uncomment the next line to print a debug trace. */
/* #define DEBUG */
#ifdef DEBUG
# define TRACE(x) x
#else
# define TRACE(x) do {} while (FALSE);
#endif
typedef struct _Binding {
TomboyBindkeyHandler handler;
gpointer user_data;
char *keystring;
uint keycode;
uint modifiers;
} Binding;
static GSList *bindings = NULL;
static guint32 last_event_time = 0;
static gboolean processing_event = FALSE;
static guint num_lock_mask, caps_lock_mask, scroll_lock_mask;
static void
lookup_ignorable_modifiers (GdkKeymap *keymap)
{
egg_keymap_resolve_virtual_modifiers (keymap,
EGG_VIRTUAL_LOCK_MASK,
&caps_lock_mask);
egg_keymap_resolve_virtual_modifiers (keymap,
EGG_VIRTUAL_NUM_LOCK_MASK,
&num_lock_mask);
egg_keymap_resolve_virtual_modifiers (keymap,
EGG_VIRTUAL_SCROLL_LOCK_MASK,
&scroll_lock_mask);
}
static void
grab_ungrab_with_ignorable_modifiers (GdkWindow *rootwin,
Binding *binding,
gboolean grab)
{
guint mod_masks [] = {
0, /* modifier only */
num_lock_mask,
caps_lock_mask,
scroll_lock_mask,
num_lock_mask | caps_lock_mask,
num_lock_mask | scroll_lock_mask,
caps_lock_mask | scroll_lock_mask,
num_lock_mask | caps_lock_mask | scroll_lock_mask,
};
int i;
for (i = 0; i < G_N_ELEMENTS (mod_masks); i++) {
if (grab) {
XGrabKey (GDK_WINDOW_XDISPLAY (rootwin),
binding->keycode,
binding->modifiers | mod_masks [i],
GDK_WINDOW_XID (rootwin),
False,
GrabModeAsync,
GrabModeAsync);
} else {
XUngrabKey (GDK_WINDOW_XDISPLAY (rootwin),
binding->keycode,
binding->modifiers | mod_masks [i],
GDK_WINDOW_XID (rootwin));
}
}
}
static gboolean
do_grab_key (Binding *binding)
{
GdkKeymap *keymap = gdk_keymap_get_default ();
GdkWindow *rootwin = gdk_get_default_root_window ();
EggVirtualModifierType virtual_mods = 0;
guint keysym = 0;
if (keymap == NULL || rootwin == NULL)
return FALSE;
if (!egg_accelerator_parse_virtual (binding->keystring,
&keysym,
&virtual_mods))
return FALSE;
TRACE (g_print ("Got accel %d, %d\n", keysym, virtual_mods));
binding->keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (rootwin),
keysym);
if (binding->keycode == 0)
return FALSE;
TRACE (g_print ("Got keycode %d\n", binding->keycode));
egg_keymap_resolve_virtual_modifiers (keymap,
virtual_mods,
&binding->modifiers);
TRACE (g_print ("Got modmask %d\n", binding->modifiers));
gdk_error_trap_push ();
grab_ungrab_with_ignorable_modifiers (rootwin,
binding,
TRUE /* grab */);
gdk_flush ();
if (gdk_error_trap_pop ()) {
g_warning ("Binding '%s' failed!\n", binding->keystring);
return FALSE;
}
return TRUE;
}
static gboolean
do_ungrab_key (Binding *binding)
{
GdkWindow *rootwin = gdk_get_default_root_window ();
TRACE (g_print ("Removing grab for '%s'\n", binding->keystring));
grab_ungrab_with_ignorable_modifiers (rootwin,
binding,
FALSE /* ungrab */);
return TRUE;
}
static GdkFilterReturn
filter_func (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
{
GdkFilterReturn return_val = GDK_FILTER_CONTINUE;
XEvent *xevent = (XEvent *) gdk_xevent;
guint event_mods;
GSList *iter;
TRACE (g_print ("Got Event! %d, %d\n", xevent->type, event->type));
switch (xevent->type) {
case KeyPress:
TRACE (g_print ("Got KeyPress! keycode: %d, modifiers: %d\n",
xevent->xkey.keycode,
xevent->xkey.state));
/*
* Set the last event time for use when showing
* windows to avoid anti-focus-stealing code.
*/
processing_event = TRUE;
last_event_time = xevent->xkey.time;
event_mods = xevent->xkey.state & ~(num_lock_mask |
caps_lock_mask |
scroll_lock_mask);
for (iter = bindings; iter != NULL; iter = iter->next) {
Binding *binding = (Binding *) iter->data;
if (binding->keycode == xevent->xkey.keycode &&
binding->modifiers == event_mods) {
TRACE (g_print ("Calling handler for '%s'...\n",
binding->keystring));
(binding->handler) (binding->keystring,
binding->user_data);
}
}
processing_event = FALSE;
break;
case KeyRelease:
TRACE (g_print ("Got KeyRelease! \n"));
break;
}
return return_val;
}
static void
keymap_changed (GdkKeymap *map)
{
GdkKeymap *keymap = gdk_keymap_get_default ();
GSList *iter;
TRACE (g_print ("Keymap changed! Regrabbing keys..."));
for (iter = bindings; iter != NULL; iter = iter->next) {
Binding *binding = (Binding *) iter->data;
do_ungrab_key (binding);
}
lookup_ignorable_modifiers (keymap);
for (iter = bindings; iter != NULL; iter = iter->next) {
Binding *binding = (Binding *) iter->data;
do_grab_key (binding);
}
}
void
tomboy_keybinder_init (void)
{
GdkKeymap *keymap = gdk_keymap_get_default ();
GdkWindow *rootwin = gdk_get_default_root_window ();
lookup_ignorable_modifiers (keymap);
gdk_window_add_filter (rootwin,
filter_func,
NULL);
g_signal_connect (keymap,
"keys_changed",
G_CALLBACK (keymap_changed),
NULL);
}
void
tomboy_keybinder_bind (const char *keystring,
TomboyBindkeyHandler handler,
gpointer user_data)
{
Binding *binding;
gboolean success;
binding = g_new0 (Binding, 1);
binding->keystring = g_strdup (keystring);
binding->handler = handler;
binding->user_data = user_data;
/* Sets the binding's keycode and modifiers */
success = do_grab_key (binding);
if (success) {
bindings = g_slist_prepend (bindings, binding);
} else {
g_free (binding->keystring);
g_free (binding);
}
}
void
tomboy_keybinder_unbind (const char *keystring,
TomboyBindkeyHandler handler)
{
GSList *iter;
for (iter = bindings; iter != NULL; iter = iter->next) {
Binding *binding = (Binding *) iter->data;
if (strcmp (keystring, binding->keystring) != 0 ||
handler != binding->handler)
continue;
do_ungrab_key (binding);
bindings = g_slist_remove (bindings, binding);
g_free (binding->keystring);
g_free (binding);
break;
}
}
/*
* From eggcellrenderkeys.c.
*/
gboolean
tomboy_keybinder_is_modifier (guint keycode)
{
gint i;
gint map_size;
XModifierKeymap *mod_keymap;
gboolean retval = FALSE;
mod_keymap = XGetModifierMapping (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()));
map_size = 8 * mod_keymap->max_keypermod;
i = 0;
while (i < map_size) {
if (keycode == mod_keymap->modifiermap[i]) {
retval = TRUE;
break;
}
++i;
}
XFreeModifiermap (mod_keymap);
return retval;
}
guint32
tomboy_keybinder_get_current_event_time (void)
{
if (processing_event)
return last_event_time;
else
return GDK_CURRENT_TIME;
}
|