~ubuntu-branches/ubuntu/oneiric/cairo-dock/oneiric

« back to all changes in this revision

Viewing changes to src/cairo-dock-keybinder.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthieu Baerts (matttbe)
  • Date: 2010-08-09 23:26:12 UTC
  • mto: (18.1.1 cairo-dock) (19.1.1 cairo-dock)
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: james.westby@ubuntu.com-20100809232612-pocdxliaxjdetm37
Tags: upstream-2.2.0~0beta4
ImportĀ upstreamĀ versionĀ 2.2.0~0beta4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
** cairo-dock-keybinder.c
3
 
** Login : <ctaf42@localhost.localdomain>
4
 
** Started on  Thu Jan 31 03:57:17 2008 Cedric GESTES
5
 
** $Id$
6
 
**
7
 
** Author(s)
8
 
**  - Cedric GESTES <ctaf42@gmail.com>
9
 
**  - Havoc Pennington
10
 
**  - Tim Janik
11
 
**
12
 
** Copyright (C) 2008 Cedric GESTES
13
 
** This program is free software; you can redistribute it and/or modify
14
 
** it under the terms of the GNU General Public License as published by
15
 
** the Free Software Foundation; either version 3 of the License, or
16
 
** (at your option) any later version.
17
 
**
18
 
** This program is distributed in the hope that it will be useful,
19
 
** but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 
** GNU General Public License for more details.
22
 
**
23
 
** You should have received a copy of the GNU General Public License
24
 
** along with this program; if not, write to the Free Software
25
 
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
 
*
27
 
* imported from tomboy_keybinder.c
28
 
*/
29
 
 
30
 
#include <string.h>
31
 
#include <sys/types.h>
32
 
#include <gdk/gdk.h>
33
 
#include <gdk/gdkwindow.h>
34
 
#include <gdk/gdkx.h>
35
 
#include <X11/Xlib.h>
36
 
#ifdef HAVE_XEXTEND
37
 
#include <X11/extensions/XTest.h>
38
 
#endif
39
 
 
40
 
#include "eggaccelerators.h"
41
 
#include "cairo-dock-log.h"
42
 
#include "cairo-dock-X-utilities.h"
43
 
#include "cairo-dock-keybinder.h"
44
 
 
45
 
typedef unsigned int uint;
46
 
typedef struct _Binding {
47
 
        CDBindkeyHandler        handler;
48
 
        gpointer              user_data;
49
 
        char                 *keystring;
50
 
        uint                  keycode;
51
 
        uint                  modifiers;
52
 
} Binding;
53
 
 
54
 
static GSList *bindings = NULL;
55
 
static guint32 last_event_time = 0;
56
 
static gboolean processing_event = FALSE;
57
 
 
58
 
static guint num_lock_mask=0, caps_lock_mask=0, scroll_lock_mask=0;
59
 
 
60
 
static void
61
 
lookup_ignorable_modifiers (GdkKeymap *keymap)
62
 
{
63
 
        egg_keymap_resolve_virtual_modifiers (keymap,
64
 
                                              EGG_VIRTUAL_LOCK_MASK,
65
 
                                              &caps_lock_mask);
66
 
 
67
 
        egg_keymap_resolve_virtual_modifiers (keymap,
68
 
                                              EGG_VIRTUAL_NUM_LOCK_MASK,
69
 
                                              &num_lock_mask);
70
 
 
71
 
        egg_keymap_resolve_virtual_modifiers (keymap,
72
 
                                              EGG_VIRTUAL_SCROLL_LOCK_MASK,
73
 
                                              &scroll_lock_mask);
74
 
}
75
 
 
76
 
static void
77
 
grab_ungrab_with_ignorable_modifiers (GdkWindow *rootwin,
78
 
                                      Binding   *binding,
79
 
                                      gboolean   grab)
80
 
{
81
 
        guint mod_masks [] = {
82
 
                0, /* modifier only */
83
 
                num_lock_mask,
84
 
                caps_lock_mask,
85
 
                scroll_lock_mask,
86
 
                num_lock_mask  | caps_lock_mask,
87
 
                num_lock_mask  | scroll_lock_mask,
88
 
                caps_lock_mask | scroll_lock_mask,
89
 
                num_lock_mask  | caps_lock_mask | scroll_lock_mask,
90
 
        };
91
 
        
92
 
        guint i;
93
 
        for (i = 0; i < G_N_ELEMENTS (mod_masks); i++) {
94
 
                if (grab) {
95
 
                        XGrabKey (GDK_WINDOW_XDISPLAY (rootwin),
96
 
                                  binding->keycode,
97
 
                                  binding->modifiers | mod_masks [i],
98
 
                                  GDK_WINDOW_XWINDOW (rootwin),
99
 
                                  False,
100
 
                                  GrabModeAsync,
101
 
                                  GrabModeAsync);
102
 
                } else {
103
 
                        XUngrabKey (GDK_WINDOW_XDISPLAY (rootwin),
104
 
                                    binding->keycode,
105
 
                                    binding->modifiers | mod_masks [i],
106
 
                                    GDK_WINDOW_XWINDOW (rootwin));
107
 
                }
108
 
        }
109
 
}
110
 
 
111
 
static gboolean
112
 
do_grab_key (Binding *binding)
113
 
{
114
 
        GdkKeymap *keymap = gdk_keymap_get_default ();
115
 
        GdkWindow *rootwin = gdk_get_default_root_window ();
116
 
 
117
 
        EggVirtualModifierType virtual_mods = 0;
118
 
        guint keysym = 0;
119
 
 
120
 
        if (keymap == NULL || rootwin == NULL)
121
 
                return FALSE;
122
 
 
123
 
        if (!egg_accelerator_parse_virtual (binding->keystring,
124
 
                                            &keysym,
125
 
                                            &virtual_mods))
126
 
                return FALSE;
127
 
 
128
 
        cd_debug ("Got accel %d, %d", keysym, virtual_mods);
129
 
 
130
 
        binding->keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (rootwin),
131
 
                                             keysym);
132
 
        if (binding->keycode == 0)
133
 
                return FALSE;
134
 
 
135
 
        cd_debug ("Got keycode %d", binding->keycode);
136
 
 
137
 
        egg_keymap_resolve_virtual_modifiers (keymap,
138
 
                                              virtual_mods,
139
 
                                              &binding->modifiers);
140
 
 
141
 
        cd_debug ("Got modmask %d", binding->modifiers);
142
 
 
143
 
        gdk_error_trap_push ();
144
 
 
145
 
        grab_ungrab_with_ignorable_modifiers (rootwin,
146
 
                                              binding,
147
 
                                              TRUE /* grab */);
148
 
 
149
 
        gdk_flush ();
150
 
 
151
 
        if (gdk_error_trap_pop ()) {
152
 
           g_warning ("Binding '%s' failed!", binding->keystring);
153
 
           return FALSE;
154
 
        }
155
 
 
156
 
        return TRUE;
157
 
}
158
 
 
159
 
static gboolean
160
 
do_ungrab_key (Binding *binding)
161
 
{
162
 
        GdkWindow *rootwin = gdk_get_default_root_window ();
163
 
 
164
 
        cd_debug ("Removing grab for '%s'", binding->keystring);
165
 
 
166
 
        grab_ungrab_with_ignorable_modifiers (rootwin,
167
 
                                              binding,
168
 
                                              FALSE /* ungrab */);
169
 
 
170
 
        return TRUE;
171
 
}
172
 
 
173
 
static GdkFilterReturn
174
 
filter_func (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
175
 
{
176
 
        GdkFilterReturn return_val = GDK_FILTER_CONTINUE;
177
 
        XEvent *xevent = (XEvent *) gdk_xevent;
178
 
        guint event_mods;
179
 
        GSList *iter;
180
 
 
181
 
        cd_debug ("Got Event! %d, %d", xevent->type, event->type);
182
 
 
183
 
        switch (xevent->type) {
184
 
        case KeyPress:
185
 
                cd_debug ("Got KeyPress! keycode: %d, modifiers: %d",
186
 
                                xevent->xkey.keycode,
187
 
                                xevent->xkey.state);
188
 
 
189
 
                /*
190
 
                 * Set the last event time for use when showing
191
 
                 * windows to avoid anti-focus-stealing code.
192
 
                 */
193
 
                processing_event = TRUE;
194
 
                last_event_time = xevent->xkey.time;
195
 
 
196
 
                event_mods = xevent->xkey.state & ~(num_lock_mask  |
197
 
                                                    caps_lock_mask |
198
 
                                                    scroll_lock_mask);
199
 
 
200
 
                for (iter = bindings; iter != NULL; iter = iter->next) {
201
 
                        Binding *binding = (Binding *) iter->data;
202
 
 
203
 
                        if (binding->keycode == xevent->xkey.keycode &&
204
 
                            binding->modifiers == event_mods) {
205
 
 
206
 
                                cd_debug ("Calling handler for '%s'...",
207
 
                                                binding->keystring);
208
 
 
209
 
                                (binding->handler) (binding->keystring,
210
 
                                                    binding->user_data);
211
 
                        }
212
 
                }
213
 
 
214
 
                processing_event = FALSE;
215
 
                break;
216
 
        case KeyRelease:
217
 
                cd_debug ("Got KeyRelease! ");
218
 
                break;
219
 
        }
220
 
 
221
 
        return return_val;
222
 
}
223
 
 
224
 
static void
225
 
keymap_changed (GdkKeymap *map)
226
 
{
227
 
        GdkKeymap *keymap = gdk_keymap_get_default ();
228
 
        GSList *iter;
229
 
 
230
 
        cd_debug ("Keymap changed! Regrabbing keys...");
231
 
 
232
 
        for (iter = bindings; iter != NULL; iter = iter->next) {
233
 
                Binding *binding = (Binding *) iter->data;
234
 
                do_ungrab_key (binding);
235
 
        }
236
 
 
237
 
        lookup_ignorable_modifiers (keymap);
238
 
 
239
 
        for (iter = bindings; iter != NULL; iter = iter->next) {
240
 
                Binding *binding = (Binding *) iter->data;
241
 
                do_grab_key (binding);
242
 
        }
243
 
}
244
 
 
245
 
void
246
 
cd_keybinder_init (void)
247
 
{
248
 
        GdkKeymap *keymap = gdk_keymap_get_default ();
249
 
        GdkWindow *rootwin = gdk_get_default_root_window ();
250
 
 
251
 
        lookup_ignorable_modifiers (keymap);
252
 
 
253
 
        gdk_window_add_filter (rootwin,
254
 
                filter_func,
255
 
                NULL);
256
 
 
257
 
        g_signal_connect (keymap,
258
 
                "keys_changed",
259
 
                G_CALLBACK (keymap_changed),
260
 
                NULL);
261
 
}
262
 
 
263
 
void
264
 
cd_keybinder_stop (void)
265
 
{
266
 
        GdkKeymap *keymap = gdk_keymap_get_default ();
267
 
        GdkWindow *rootwin = gdk_get_default_root_window ();
268
 
 
269
 
        num_lock_mask=0, caps_lock_mask=0, scroll_lock_mask=0;
270
 
 
271
 
        g_signal_handlers_disconnect_by_func (keymap,
272
 
                G_CALLBACK (keymap_changed),
273
 
                NULL);
274
 
        
275
 
        gdk_window_remove_filter (rootwin,
276
 
                filter_func,
277
 
                NULL);
278
 
}
279
 
 
280
 
 
281
 
gboolean
282
 
cd_keybinder_bind (const char           *keystring,
283
 
                       CDBindkeyHandler  handler,
284
 
                       gpointer              user_data)
285
 
{
286
 
        Binding *binding;
287
 
        gboolean success;
288
 
 
289
 
        cd_debug ("%s (%s)", __func__, keystring);
290
 
        if (!keystring)
291
 
                return FALSE;
292
 
        binding = g_new0 (Binding, 1);
293
 
        binding->keystring = g_strdup (keystring);
294
 
        binding->handler = handler;
295
 
        binding->user_data = user_data;
296
 
 
297
 
        /* Sets the binding's keycode and modifiers */
298
 
        success = do_grab_key (binding);
299
 
 
300
 
        if (success) {
301
 
                bindings = g_slist_prepend (bindings, binding);
302
 
        } else {
303
 
                cd_warning ("couldnt bind %s", keystring);
304
 
                g_free (binding->keystring);
305
 
                g_free (binding);
306
 
        }
307
 
 
308
 
        return success;
309
 
}
310
 
 
311
 
void
312
 
cd_keybinder_unbind (const char           *keystring,
313
 
                         CDBindkeyHandler  handler)
314
 
{
315
 
        cd_debug ("%s (%s)", __func__, keystring);
316
 
        GSList *iter;
317
 
 
318
 
        if (!keystring)
319
 
          return;
320
 
        for (iter = bindings; iter != NULL; iter = iter->next) {
321
 
                Binding *binding = (Binding *) iter->data;
322
 
 
323
 
                if (strcmp (keystring, binding->keystring) != 0 ||
324
 
                    handler != binding->handler)
325
 
                        continue;
326
 
 
327
 
                do_ungrab_key (binding);
328
 
 
329
 
                bindings = g_slist_remove (bindings, binding);
330
 
                
331
 
                g_print (" --- remove key binding '%s'\n", binding->keystring);
332
 
                g_free (binding->keystring);
333
 
                g_free (binding);
334
 
                break;
335
 
        }
336
 
}
337
 
 
338
 
/*
339
 
 * From eggcellrenderkeys.c.
340
 
 */
341
 
gboolean
342
 
cd_keybinder_is_modifier (guint keycode)
343
 
{
344
 
        gint i;
345
 
        gint map_size;
346
 
        XModifierKeymap *mod_keymap;
347
 
        gboolean retval = FALSE;
348
 
 
349
 
        mod_keymap = XGetModifierMapping (gdk_display);
350
 
 
351
 
        map_size = 8 * mod_keymap->max_keypermod;
352
 
 
353
 
        i = 0;
354
 
        while (i < map_size) {
355
 
                if (keycode == mod_keymap->modifiermap[i]) {
356
 
                        retval = TRUE;
357
 
                        break;
358
 
                }
359
 
                ++i;
360
 
        }
361
 
 
362
 
        XFreeModifiermap (mod_keymap);
363
 
 
364
 
        return retval;
365
 
}
366
 
 
367
 
guint32
368
 
cd_keybinder_get_current_event_time (void)
369
 
{
370
 
        if (processing_event)
371
 
                return last_event_time;
372
 
        else
373
 
                return GDK_CURRENT_TIME;
374
 
}
375
 
 
376
 
 
377
 
gboolean cairo_dock_simulate_key_sequence (gchar *cKeyString)  // the idea was taken from xdo.
378
 
{
379
 
#ifdef HAVE_XEXTEND
380
 
        g_return_val_if_fail (cairo_dock_xtest_is_available (), FALSE);
381
 
        g_return_val_if_fail (cKeyString != NULL, FALSE);
382
 
        cd_message ("%s (%s)", __func__, cKeyString);
383
 
        
384
 
        int iNbKeys = 0;
385
 
        int *pKeySyms = egg_keystring_to_keysyms (cKeyString, &iNbKeys);
386
 
 
387
 
        int i;
388
 
        int keycode;
389
 
        Display *dpy = cairo_dock_get_Xdisplay ();
390
 
        for (i = 0; i < iNbKeys; i ++)
391
 
        {
392
 
                keycode = XKeysymToKeycode (dpy, pKeySyms[i]);
393
 
                XTestFakeKeyEvent (dpy, keycode, TRUE, CurrentTime);  // TRUE <=> presse.
394
 
        }
395
 
        
396
 
        for (i = iNbKeys-1; i >=0; i --)
397
 
        {
398
 
                keycode = XKeysymToKeycode (dpy, pKeySyms[i]);
399
 
                XTestFakeKeyEvent (dpy, keycode, FALSE, CurrentTime);  // TRUE <=> presse.
400
 
        }
401
 
        
402
 
        XFlush (dpy);
403
 
        
404
 
        return TRUE;
405
 
#else
406
 
        cd_warning ("The dock was not compiled with the support of XTest.");
407
 
        return FALSE;
408
 
#endif
409
 
}
410
 
 
411