~ubuntu-branches/ubuntu/lucid/tomboy/lucid-proposed

« back to all changes in this revision

Viewing changes to libtomboy/tomboykeybinder.c

Tags: upstream-0.3.1
ImportĀ upstreamĀ versionĀ 0.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
#include <gdk/gdk.h>
 
4
#include <gdk/gdkwindow.h>
 
5
#include <gdk/gdkx.h>
 
6
#include <X11/Xlib.h>
 
7
 
 
8
#include "eggaccelerators.h"
 
9
#include "tomboykeybinder.h"
 
10
 
 
11
static GSList *bindings = NULL;
 
12
 
 
13
/* Uncomment the next line to print a debug trace. */
 
14
/* #define DEBUG */
 
15
 
 
16
#ifdef DEBUG
 
17
#  define TRACE(x) x
 
18
#else
 
19
#  define TRACE(x) do {} while (FALSE);
 
20
#endif
 
21
 
 
22
typedef struct _Binding {
 
23
        TomboyBindkeyHandler  handler;
 
24
        gpointer              user_data;
 
25
        char                 *keystring;
 
26
        uint                  keycode;
 
27
        uint                  modifiers;
 
28
} Binding;
 
29
 
 
30
static gboolean 
 
31
do_grab_key (Binding *binding)
 
32
{
 
33
        GdkKeymap *keymap = gdk_keymap_get_default ();
 
34
        GdkWindow *rootwin = gdk_get_default_root_window ();
 
35
 
 
36
        EggVirtualModifierType virtual_mods = 0;
 
37
        guint keysym = 0;
 
38
 
 
39
        if (keymap == NULL || rootwin == NULL)
 
40
                return FALSE;
 
41
 
 
42
        if (!egg_accelerator_parse_virtual (binding->keystring, 
 
43
                                            &keysym, 
 
44
                                            &virtual_mods))
 
45
                return FALSE;
 
46
 
 
47
        TRACE (g_print ("Got accel %d, %d\n", keysym, virtual_mods));
 
48
 
 
49
        binding->keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (rootwin), 
 
50
                                             keysym);
 
51
        if (binding->keycode == 0)
 
52
                return FALSE;
 
53
 
 
54
        TRACE (g_print ("Got keycode %d\n", binding->keycode));
 
55
 
 
56
        egg_keymap_resolve_virtual_modifiers (keymap,
 
57
                                              virtual_mods,
 
58
                                              &binding->modifiers);
 
59
 
 
60
        TRACE (g_print ("Got modmask %d\n", binding->modifiers));
 
61
 
 
62
        XGrabKey (GDK_WINDOW_XDISPLAY (rootwin),
 
63
                  binding->keycode,
 
64
                  binding->modifiers,
 
65
                  GDK_WINDOW_XWINDOW (rootwin),
 
66
                  True,
 
67
                  GrabModeAsync, 
 
68
                  GrabModeAsync);
 
69
 
 
70
        return TRUE;
 
71
}
 
72
 
 
73
static gboolean 
 
74
do_ungrab_key (Binding *binding)
 
75
{
 
76
        GdkWindow *rootwin = gdk_get_default_root_window ();
 
77
 
 
78
        TRACE (g_print ("Removing grab for '%s'\n", binding->keystring));
 
79
 
 
80
        XUngrabKey (GDK_WINDOW_XDISPLAY (rootwin),
 
81
                    binding->keycode,
 
82
                    binding->modifiers,
 
83
                    GDK_WINDOW_XWINDOW (rootwin));
 
84
 
 
85
        return TRUE;
 
86
}
 
87
 
 
88
static GdkFilterReturn
 
89
filter_func (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
 
90
{
 
91
        GdkFilterReturn return_val = GDK_FILTER_CONTINUE;
 
92
        XEvent *xevent = (XEvent *) gdk_xevent;
 
93
        GSList *iter;
 
94
 
 
95
        TRACE (g_print ("Got Event! %d, %d\n", xevent->type, event->type));
 
96
 
 
97
        switch (xevent->type) {
 
98
        case KeyPress:
 
99
                TRACE (g_print ("Got KeyPress! keycode: %d, modifiers: %d\n", 
 
100
                                xevent->xkey.keycode, 
 
101
                                xevent->xkey.state));
 
102
 
 
103
                for (iter = bindings; iter != NULL; iter = iter->next) {
 
104
                        Binding *binding = (Binding *) iter->data;
 
105
 
 
106
                        if (binding->keycode == xevent->xkey.keycode &&
 
107
                            binding->modifiers == xevent->xkey.state) {
 
108
 
 
109
                                TRACE (g_print ("Calling handler for '%s'...\n", 
 
110
                                                binding->keystring));
 
111
 
 
112
                                (binding->handler) (binding->keystring, 
 
113
                                                    binding->user_data);
 
114
                        }
 
115
                }
 
116
                break;
 
117
        case KeyRelease:
 
118
                TRACE (g_print ("Got KeyRelease! \n"));
 
119
                break;
 
120
        }
 
121
 
 
122
        return return_val;
 
123
}
 
124
 
 
125
void 
 
126
keymap_changed (GdkKeymap *map)
 
127
{
 
128
        GSList *iter;
 
129
 
 
130
        TRACE (g_print ("Keymap changed! Regrabbing keys..."));
 
131
 
 
132
        for (iter = bindings; iter != NULL; iter = iter->next) {
 
133
                Binding *binding = (Binding *) iter->data;
 
134
 
 
135
                do_ungrab_key (binding);
 
136
                do_grab_key (binding);
 
137
        }
 
138
}
 
139
 
 
140
void 
 
141
tomboy_keybinder_init (void)
 
142
{
 
143
        GdkKeymap *keymap = gdk_keymap_get_default ();
 
144
        GdkWindow *rootwin = gdk_get_default_root_window ();
 
145
 
 
146
        gdk_window_add_filter (rootwin, 
 
147
                               filter_func, 
 
148
                               NULL);
 
149
 
 
150
        g_signal_connect (keymap, 
 
151
                          "keys_changed",
 
152
                          G_CALLBACK (keymap_changed),
 
153
                          NULL);
 
154
}
 
155
 
 
156
void 
 
157
tomboy_keybinder_bind (const char           *keystring,
 
158
                       TomboyBindkeyHandler  handler,
 
159
                       gpointer              user_data)
 
160
{
 
161
        Binding *binding;
 
162
        gboolean success;
 
163
 
 
164
        binding = g_new0 (Binding, 1);
 
165
        binding->keystring = g_strdup (keystring);
 
166
        binding->handler = handler;
 
167
        binding->user_data = user_data;
 
168
 
 
169
        /* Sets the binding's keycode and modifiers */
 
170
        success = do_grab_key (binding);
 
171
 
 
172
        if (success) {
 
173
                bindings = g_slist_prepend (bindings, binding);
 
174
        } else {
 
175
                g_free (binding->keystring);
 
176
                g_free (binding);
 
177
        }
 
178
}
 
179
 
 
180
void
 
181
tomboy_keybinder_unbind (const char           *keystring, 
 
182
                         TomboyBindkeyHandler  handler)
 
183
{
 
184
        GSList *iter;
 
185
 
 
186
        for (iter = bindings; iter != NULL; iter = iter->next) {
 
187
                Binding *binding = (Binding *) iter->data;
 
188
 
 
189
                if (strcmp (keystring, binding->keystring) != 0 ||
 
190
                    handler != binding->handler) 
 
191
                        continue;
 
192
 
 
193
                do_ungrab_key (binding);
 
194
 
 
195
                bindings = g_slist_remove (bindings, binding);
 
196
 
 
197
                g_free (binding->keystring);
 
198
                g_free (binding);
 
199
                break;
 
200
        }
 
201
}
 
202
 
 
203
/* 
 
204
 * From eggcellrenderkeys.c.
 
205
 */
 
206
gboolean
 
207
tomboy_keybinder_is_modifier (guint keycode)
 
208
{
 
209
        gint i;
 
210
        gint map_size;
 
211
        XModifierKeymap *mod_keymap;
 
212
        gboolean retval = FALSE;
 
213
 
 
214
        mod_keymap = XGetModifierMapping (gdk_display);
 
215
 
 
216
        map_size = 8 * mod_keymap->max_keypermod;
 
217
 
 
218
        i = 0;
 
219
        while (i < map_size) {
 
220
                if (keycode == mod_keymap->modifiermap[i]) {
 
221
                        retval = TRUE;
 
222
                        break;
 
223
                }
 
224
                ++i;
 
225
        }
 
226
 
 
227
        XFreeModifiermap (mod_keymap);
 
228
 
 
229
        return retval;
 
230
}