4
#include <gdk/gdkwindow.h>
8
#include "eggaccelerators.h"
9
#include "tomboykeybinder.h"
11
static GSList *bindings = NULL;
13
/* Uncomment the next line to print a debug trace. */
19
# define TRACE(x) do {} while (FALSE);
22
typedef struct _Binding {
23
TomboyBindkeyHandler handler;
31
do_grab_key (Binding *binding)
33
GdkKeymap *keymap = gdk_keymap_get_default ();
34
GdkWindow *rootwin = gdk_get_default_root_window ();
36
EggVirtualModifierType virtual_mods = 0;
39
if (keymap == NULL || rootwin == NULL)
42
if (!egg_accelerator_parse_virtual (binding->keystring,
47
TRACE (g_print ("Got accel %d, %d\n", keysym, virtual_mods));
49
binding->keycode = XKeysymToKeycode (GDK_WINDOW_XDISPLAY (rootwin),
51
if (binding->keycode == 0)
54
TRACE (g_print ("Got keycode %d\n", binding->keycode));
56
egg_keymap_resolve_virtual_modifiers (keymap,
60
TRACE (g_print ("Got modmask %d\n", binding->modifiers));
62
XGrabKey (GDK_WINDOW_XDISPLAY (rootwin),
65
GDK_WINDOW_XWINDOW (rootwin),
74
do_ungrab_key (Binding *binding)
76
GdkWindow *rootwin = gdk_get_default_root_window ();
78
TRACE (g_print ("Removing grab for '%s'\n", binding->keystring));
80
XUngrabKey (GDK_WINDOW_XDISPLAY (rootwin),
83
GDK_WINDOW_XWINDOW (rootwin));
88
static GdkFilterReturn
89
filter_func (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
91
GdkFilterReturn return_val = GDK_FILTER_CONTINUE;
92
XEvent *xevent = (XEvent *) gdk_xevent;
95
TRACE (g_print ("Got Event! %d, %d\n", xevent->type, event->type));
97
switch (xevent->type) {
99
TRACE (g_print ("Got KeyPress! keycode: %d, modifiers: %d\n",
100
xevent->xkey.keycode,
101
xevent->xkey.state));
103
for (iter = bindings; iter != NULL; iter = iter->next) {
104
Binding *binding = (Binding *) iter->data;
106
if (binding->keycode == xevent->xkey.keycode &&
107
binding->modifiers == xevent->xkey.state) {
109
TRACE (g_print ("Calling handler for '%s'...\n",
110
binding->keystring));
112
(binding->handler) (binding->keystring,
118
TRACE (g_print ("Got KeyRelease! \n"));
126
keymap_changed (GdkKeymap *map)
130
TRACE (g_print ("Keymap changed! Regrabbing keys..."));
132
for (iter = bindings; iter != NULL; iter = iter->next) {
133
Binding *binding = (Binding *) iter->data;
135
do_ungrab_key (binding);
136
do_grab_key (binding);
141
tomboy_keybinder_init (void)
143
GdkKeymap *keymap = gdk_keymap_get_default ();
144
GdkWindow *rootwin = gdk_get_default_root_window ();
146
gdk_window_add_filter (rootwin,
150
g_signal_connect (keymap,
152
G_CALLBACK (keymap_changed),
157
tomboy_keybinder_bind (const char *keystring,
158
TomboyBindkeyHandler handler,
164
binding = g_new0 (Binding, 1);
165
binding->keystring = g_strdup (keystring);
166
binding->handler = handler;
167
binding->user_data = user_data;
169
/* Sets the binding's keycode and modifiers */
170
success = do_grab_key (binding);
173
bindings = g_slist_prepend (bindings, binding);
175
g_free (binding->keystring);
181
tomboy_keybinder_unbind (const char *keystring,
182
TomboyBindkeyHandler handler)
186
for (iter = bindings; iter != NULL; iter = iter->next) {
187
Binding *binding = (Binding *) iter->data;
189
if (strcmp (keystring, binding->keystring) != 0 ||
190
handler != binding->handler)
193
do_ungrab_key (binding);
195
bindings = g_slist_remove (bindings, binding);
197
g_free (binding->keystring);
204
* From eggcellrenderkeys.c.
207
tomboy_keybinder_is_modifier (guint keycode)
211
XModifierKeymap *mod_keymap;
212
gboolean retval = FALSE;
214
mod_keymap = XGetModifierMapping (gdk_display);
216
map_size = 8 * mod_keymap->max_keypermod;
219
while (i < map_size) {
220
if (keycode == mod_keymap->modifiermap[i]) {
227
XFreeModifiermap (mod_keymap);