summaryrefslogtreecommitdiff
path: root/gnome-settings-daemon/gnome-settings-keybindings.c
blob: a6c9ce8e4577c8172051567b72b15d82c7603c56 (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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include <config.h>

#include <string.h>
#include <X11/keysym.h>
#include <glib/gi18n.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include "gnome-settings-daemon.h"
#include "gnome-settings-keybindings.h"
#include "eggaccelerators.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)

#define GCONF_BINDING_DIR "/desktop/gnome/keybindings"

typedef struct {
  guint keysym;
  guint state;
  guint keycode;
} Key;

typedef struct {
  char *binding_str;
  char *action;
  char *gconf_key;
  Key   key;
  Key   previous_key;
} Binding;
  
static GSList *binding_list = NULL;
static GSList *screens = NULL;

static GSList *
get_screens_list (void)
{
  GdkDisplay *display = gdk_display_get_default();
  GSList *list = NULL;
  int i;

  if (gdk_display_get_n_screens (display) == 1) {
    list = g_slist_append (list, gdk_screen_get_default ());
  } else {
    for (i = 0; i < gdk_display_get_n_screens (display); i++) {
      GdkScreen *screen;

      screen = gdk_display_get_screen (display, i);
      if (screen != NULL) {
        list = g_slist_append (list, screen);
      }
    }
  }

  return list;
}

extern char **environ;

static char *
screen_exec_display_string (GdkScreen *screen)
{
  GString    *str;
  const char *old_display;
  char       *retval;
  char       *p;

  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);

  old_display = gdk_display_get_name (gdk_screen_get_display (screen));

  str = g_string_new ("DISPLAY=");
  g_string_append (str, old_display);

  p = strrchr (str->str, '.');
  if (p && p >  strchr (str->str, ':'))
          g_string_truncate (str, p - str->str);

  g_string_append_printf (str, ".%d", gdk_screen_get_number (screen));

  retval = str->str;

  g_string_free (str, FALSE);

  return retval;
}

/**
 * get_exec_environment:
 *
 * Description: Modifies the current program environment to
 * ensure that $DISPLAY is set such that a launched application
 * inheriting this environment would appear on screen.
 *
 * Returns: a newly-allocated %NULL-terminated array of strings or
 * %NULL on error. Use g_strfreev() to free it.
 *
 * mainly ripped from egg_screen_exec_display_string in
 * gnome-panel/egg-screen-exec.c
 **/
char **
get_exec_environment (XEvent *xevent)
{
  char **retval = NULL;
  int    i;
  int    display_index = -1;

  GdkScreen  *screen = NULL;

  GdkWindow *window = gdk_xid_table_lookup (xevent->xkey.root);

  if (window)
    screen = gdk_drawable_get_screen (GDK_DRAWABLE (window));

  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);

  for (i = 0; environ [i]; i++)
    if (!strncmp (environ [i], "DISPLAY", 7))
      display_index = i;

  if (display_index == -1)
    display_index = i++;

  retval = g_new (char *, i + 1);

  for (i = 0; environ [i]; i++)
    if (i == display_index)
      retval [i] = screen_exec_display_string (screen);
    else
      retval [i] = g_strdup (environ [i]);

  retval [i] = NULL;

  return retval;
}


static gint 
compare_bindings (gconstpointer a, gconstpointer b)
{
  Binding *key_a = (Binding*) a;
  char *key_b = (char*) b;

  return strcmp (key_b, key_a->gconf_key);
}

static gboolean
parse_binding (Binding *binding)
{
  g_return_val_if_fail (binding != NULL, FALSE);

  binding->key.keysym = 0;
  binding->key.state = 0;

  if (binding->binding_str == NULL ||
      binding->binding_str[0] == '\0' ||
      strcmp (binding->binding_str, "Disabled") == 0)
          return FALSE;

  if (egg_accelerator_parse_virtual (binding->binding_str, &binding->key.keysym, &binding->key.keycode, &binding->key.state) == FALSE)
          return FALSE;

  return TRUE;
}

gboolean 
bindings_get_entry (char *subdir)
{
  GConfValue *value;
  Binding *new_binding;
  GSList *tmp_elem = NULL, *list = NULL, *li;
  char *gconf_key;
  char *action = NULL;
  char *key = NULL;
  GConfClient *client = gconf_client_get_default();
  
  g_return_val_if_fail (subdir != NULL, FALSE);
  
  /* value = gconf_entry_get_value (entry); */
  gconf_key = g_path_get_basename (subdir);

  if (!gconf_key)
    return FALSE;

  /* Get entries for this binding */
  list = gconf_client_all_entries (client, subdir, NULL);
  g_object_unref (client);

  for (li = list; li != NULL; li = li->next)
    {
      GConfEntry *entry = li->data;
      char *key_name = g_path_get_basename (gconf_entry_get_key (entry));
      if (strcmp (key_name, "action") == 0)
	{
	  if (!action)
	    {
	      value = gconf_entry_get_value (entry);
	      if (value->type != GCONF_VALUE_STRING)
		return FALSE;
	      action = g_strdup (gconf_value_get_string (value));
	    }
	  else
	    g_warning (_("Key Binding (%s) has its action defined multiple times\n"),
		       gconf_key);
	}
      if (strcmp (key_name, "binding") == 0)
	{
	  if (!key)
	    {
	      value = gconf_entry_get_value (entry);
	      if (value->type != GCONF_VALUE_STRING)
		return FALSE;
	      key = g_strdup (gconf_value_get_string (value));
	    }
	  else
	    g_warning (_("Key Binding (%s) has its binding defined multiple times\n"),
		       gconf_key);
	}
    }
  if (!action || !key)
    { 
      g_warning (_("Key Binding (%s) is incomplete\n"), gconf_key);
      return FALSE;
    }
    
  tmp_elem = g_slist_find_custom (binding_list, gconf_key,
				  compare_bindings);

  if (!tmp_elem)
    new_binding = g_new0 (Binding, 1);
  else
    {
      new_binding = (Binding*) tmp_elem->data;
      g_free (new_binding->binding_str);
      g_free (new_binding->action);
    }
  
  new_binding->binding_str = key;
  new_binding->action = action;
  new_binding->gconf_key = gconf_key;

  new_binding->previous_key.keysym = new_binding->key.keysym;
  new_binding->previous_key.state = new_binding->key.state;
  new_binding->previous_key.keycode = new_binding->key.keycode;

  if (parse_binding (new_binding))
      binding_list = g_slist_append (binding_list, new_binding);
  else
    {
      g_warning (_("Key Binding (%s) is invalid\n"), gconf_key);
      g_free (new_binding->binding_str);
      g_free (new_binding->action);
      return FALSE;
    }
  return TRUE;
}

static gboolean 
key_already_used (Binding *binding)
{
  GSList *li;
  
  for (li = binding_list; li != NULL; li = li->next)
    {
      Binding *tmp_binding =  (Binding*) li->data;

      if (tmp_binding != binding &&  tmp_binding->key.keycode == binding->key.keycode &&
	  tmp_binding->key.state == binding->key.state)
	return TRUE;
    }
  return FALSE;
}

static void
grab_key (GdkWindow *root, Key *key, int result, gboolean grab)
{
  gdk_error_trap_push ();
  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));
  gdk_flush ();
  if (gdk_error_trap_pop ()) {
    g_warning (_("It seems that another application already has"
                 " access to key '%d'."), key->keycode);
  }
}

/* inspired from all_combinations from gnome-panel/gnome-panel/global-keys.c */
#define N_BITS 32
static void
do_grab (gboolean grab,
	 Key *key)
{
	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++) {
		GSList *l;
		int j, result = 0;

		for (j = 0; j < bits_set_cnt; j++) {
			if (i & (1<<j))
				result |= (1<<indexes[j]);
		}

		for (l = screens; l ; l = l->next) {
                  GdkScreen *screen = l->data;
                  grab_key (gdk_screen_get_root_window (screen), key, result,
			    grab);
		}
	}
}

void
binding_register_keys (void)
{
  GSList *li;
  
  gdk_error_trap_push();

  /* Now check for changes and grab new key if not already used */
  for (li = binding_list ; li != NULL; li = li->next)
    {
      Binding *binding = (Binding *) li->data;
      
      if (binding->previous_key.keycode != binding->key.keycode || 
	  binding->previous_key.state != binding->key.state)
        {
          /* Ungrab key if it changed and not clashing with previously set binding */
          if (!key_already_used (binding))
            {
              if (binding->previous_key.keycode)
		do_grab (FALSE, &binding->previous_key);
	      do_grab (TRUE, &binding->key);

	      binding->previous_key.keysym = binding->key.keysym;
	      binding->previous_key.state = binding->key.state;
	      binding->previous_key.keycode = binding->key.keycode;
            }
          else
            g_warning (_("Key Binding (%s) is already in use\n"), binding->binding_str);
        }
    }
  gdk_flush ();
  gdk_error_trap_pop();
}

static void
bindings_callback (GConfEntry *entry)
{
  /* ensure we get binding dir not a sub component */
  gchar** key_elems = g_strsplit (gconf_entry_get_key (entry), "/", 15);
  gchar* binding_entry  = g_strdup_printf ("/%s/%s/%s/%s", key_elems[1], 
					   key_elems[2], key_elems[3], 
					   key_elems[4]);					 
  g_strfreev (key_elems);
  
  bindings_get_entry (binding_entry);
  g_free (binding_entry);
  
  binding_register_keys ();
}

GdkFilterReturn
keybindings_filter (GdkXEvent *gdk_xevent,
		    GdkEvent *event,
		    gpointer data)
{
  XEvent *xevent = (XEvent *)gdk_xevent;
  guint keycode, state;
  GSList *li;

  if(xevent->type != KeyPress)
          return GDK_FILTER_CONTINUE;

  keycode = xevent->xkey.keycode;
  state = xevent->xkey.state;
  
  for (li = binding_list; li != NULL; li = li->next)
    {
      Binding *binding = (Binding*) li->data;
      
      if (keycode == binding->key.keycode &&
          (state & USED_MODS) == binding->key.state)
        {
          GError* error = NULL;
	  gboolean retval;
	  gchar **argv = NULL;
	  gchar **envp = NULL;

	  g_return_val_if_fail (binding->action != NULL, GDK_FILTER_CONTINUE);

	  if (!g_shell_parse_argv (binding->action,
				   NULL, &argv,
				   &error))
	    return GDK_FILTER_CONTINUE;

	  envp = get_exec_environment (xevent);

	  
	  retval = g_spawn_async (NULL,
				  argv,
				  envp,
				  G_SPAWN_SEARCH_PATH,
				  NULL,
				  NULL,
				  NULL,
				  &error);
	  g_strfreev (argv);
	  g_strfreev (envp);

          if (!retval)
	    {
	      GtkWidget *dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_WARNING,
							  GTK_BUTTONS_CLOSE,
							  _("Error while trying to run (%s)\n"\
							  "which is linked to the key (%s)"),
							  binding->action,
							  binding->binding_str);
	      	g_signal_connect (dialog, "response",
				  G_CALLBACK (gtk_widget_destroy),
				  NULL);
		gtk_widget_show (dialog);
	    }
          return GDK_FILTER_REMOVE;
        }
    }
  return GDK_FILTER_CONTINUE;
}

void
gnome_settings_keybindings_init (GConfClient *client)
{
  GdkDisplay *dpy = gdk_display_get_default ();
  GdkScreen *screen;
  int screen_num = gdk_display_get_n_screens (dpy);
  int i;

  gnome_settings_daemon_register_callback (GCONF_BINDING_DIR, bindings_callback);
  
  gdk_window_add_filter (gdk_get_default_root_window (),
			 keybindings_filter,
			 NULL);
  for (i = 0; i < screen_num; i++)
    {
      screen = gdk_display_get_screen (dpy, i);
      gdk_window_add_filter (gdk_screen_get_root_window (screen),
			     keybindings_filter, NULL);
    }
}

void
gnome_settings_keybindings_load (GConfClient *client)
{
  GSList *list, *li;

  list = gconf_client_all_dirs (client, GCONF_BINDING_DIR, NULL);
  screens = get_screens_list ();

  for (li = list; li != NULL; li = li->next)
    {
      char *subdir = li->data;
      li->data = NULL;
      bindings_get_entry (subdir);
    }
  binding_register_keys ();
}