2006-04-25 Hendrik Brandt <heb@gnome-de.org>
[beagle.git] / glue / eggaccelerators.c
blobca63e7815024d8da42219b1fc772df15a2978080
1 /* eggaccelerators.c
2 * Copyright (C) 2002 Red Hat, Inc.; Copyright 1998, 2001 Tim Janik
3 * Developed by Havoc Pennington, Tim Janik
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include "eggaccelerators.h"
23 #include <string.h>
24 #include <gdk/gdkx.h>
25 #include <gdk/gdkkeysyms.h>
27 enum
29 EGG_MODMAP_ENTRY_SHIFT = 0,
30 EGG_MODMAP_ENTRY_LOCK = 1,
31 EGG_MODMAP_ENTRY_CONTROL = 2,
32 EGG_MODMAP_ENTRY_MOD1 = 3,
33 EGG_MODMAP_ENTRY_MOD2 = 4,
34 EGG_MODMAP_ENTRY_MOD3 = 5,
35 EGG_MODMAP_ENTRY_MOD4 = 6,
36 EGG_MODMAP_ENTRY_MOD5 = 7,
37 EGG_MODMAP_ENTRY_LAST = 8
40 #define MODMAP_ENTRY_TO_MODIFIER(x) (1 << (x))
42 typedef struct
44 EggVirtualModifierType mapping[EGG_MODMAP_ENTRY_LAST];
46 } EggModmap;
48 const EggModmap* egg_keymap_get_modmap (GdkKeymap *keymap);
50 static inline gboolean
51 is_alt (const gchar *string)
53 return ((string[0] == '<') &&
54 (string[1] == 'a' || string[1] == 'A') &&
55 (string[2] == 'l' || string[2] == 'L') &&
56 (string[3] == 't' || string[3] == 'T') &&
57 (string[4] == '>'));
60 static inline gboolean
61 is_ctl (const gchar *string)
63 return ((string[0] == '<') &&
64 (string[1] == 'c' || string[1] == 'C') &&
65 (string[2] == 't' || string[2] == 'T') &&
66 (string[3] == 'l' || string[3] == 'L') &&
67 (string[4] == '>'));
70 static inline gboolean
71 is_modx (const gchar *string)
73 return ((string[0] == '<') &&
74 (string[1] == 'm' || string[1] == 'M') &&
75 (string[2] == 'o' || string[2] == 'O') &&
76 (string[3] == 'd' || string[3] == 'D') &&
77 (string[4] >= '1' && string[4] <= '5') &&
78 (string[5] == '>'));
81 static inline gboolean
82 is_ctrl (const gchar *string)
84 return ((string[0] == '<') &&
85 (string[1] == 'c' || string[1] == 'C') &&
86 (string[2] == 't' || string[2] == 'T') &&
87 (string[3] == 'r' || string[3] == 'R') &&
88 (string[4] == 'l' || string[4] == 'L') &&
89 (string[5] == '>'));
92 static inline gboolean
93 is_shft (const gchar *string)
95 return ((string[0] == '<') &&
96 (string[1] == 's' || string[1] == 'S') &&
97 (string[2] == 'h' || string[2] == 'H') &&
98 (string[3] == 'f' || string[3] == 'F') &&
99 (string[4] == 't' || string[4] == 'T') &&
100 (string[5] == '>'));
103 static inline gboolean
104 is_shift (const gchar *string)
106 return ((string[0] == '<') &&
107 (string[1] == 's' || string[1] == 'S') &&
108 (string[2] == 'h' || string[2] == 'H') &&
109 (string[3] == 'i' || string[3] == 'I') &&
110 (string[4] == 'f' || string[4] == 'F') &&
111 (string[5] == 't' || string[5] == 'T') &&
112 (string[6] == '>'));
115 static inline gboolean
116 is_control (const gchar *string)
118 return ((string[0] == '<') &&
119 (string[1] == 'c' || string[1] == 'C') &&
120 (string[2] == 'o' || string[2] == 'O') &&
121 (string[3] == 'n' || string[3] == 'N') &&
122 (string[4] == 't' || string[4] == 'T') &&
123 (string[5] == 'r' || string[5] == 'R') &&
124 (string[6] == 'o' || string[6] == 'O') &&
125 (string[7] == 'l' || string[7] == 'L') &&
126 (string[8] == '>'));
129 static inline gboolean
130 is_release (const gchar *string)
132 return ((string[0] == '<') &&
133 (string[1] == 'r' || string[1] == 'R') &&
134 (string[2] == 'e' || string[2] == 'E') &&
135 (string[3] == 'l' || string[3] == 'L') &&
136 (string[4] == 'e' || string[4] == 'E') &&
137 (string[5] == 'a' || string[5] == 'A') &&
138 (string[6] == 's' || string[6] == 'S') &&
139 (string[7] == 'e' || string[7] == 'E') &&
140 (string[8] == '>'));
143 static inline gboolean
144 is_meta (const gchar *string)
146 return ((string[0] == '<') &&
147 (string[1] == 'm' || string[1] == 'M') &&
148 (string[2] == 'e' || string[2] == 'E') &&
149 (string[3] == 't' || string[3] == 'T') &&
150 (string[4] == 'a' || string[4] == 'A') &&
151 (string[5] == '>'));
154 static inline gboolean
155 is_super (const gchar *string)
157 return ((string[0] == '<') &&
158 (string[1] == 's' || string[1] == 'S') &&
159 (string[2] == 'u' || string[2] == 'U') &&
160 (string[3] == 'p' || string[3] == 'P') &&
161 (string[4] == 'e' || string[4] == 'E') &&
162 (string[5] == 'r' || string[5] == 'R') &&
163 (string[6] == '>'));
166 static inline gboolean
167 is_hyper (const gchar *string)
169 return ((string[0] == '<') &&
170 (string[1] == 'h' || string[1] == 'H') &&
171 (string[2] == 'y' || string[2] == 'Y') &&
172 (string[3] == 'p' || string[3] == 'P') &&
173 (string[4] == 'e' || string[4] == 'E') &&
174 (string[5] == 'r' || string[5] == 'R') &&
175 (string[6] == '>'));
179 * egg_accelerator_parse_virtual:
180 * @accelerator: string representing an accelerator
181 * @accelerator_key: return location for accelerator keyval
182 * @accelerator_mods: return location for accelerator modifier mask
184 * Parses a string representing a virtual accelerator. The format
185 * looks like "&lt;Control&gt;a" or "&lt;Shift&gt;&lt;Alt&gt;F1" or
186 * "&lt;Release&gt;z" (the last one is for key release). The parser
187 * is fairly liberal and allows lower or upper case, and also
188 * abbreviations such as "&lt;Ctl&gt;" and "&lt;Ctrl&gt;".
190 * If the parse fails, @accelerator_key and @accelerator_mods will
191 * be set to 0 (zero) and %FALSE will be returned. If the string contains
192 * only modifiers, @accelerator_key will be set to 0 but %TRUE will be
193 * returned.
195 * The virtual vs. concrete accelerator distinction is a relic of
196 * how the X Window System works; there are modifiers Mod2-Mod5 that
197 * can represent various keyboard keys (numlock, meta, hyper, etc.),
198 * the virtual modifier represents the keyboard key, the concrete
199 * modifier the actual Mod2-Mod5 bits in the key press event.
201 * Returns: %TRUE on success.
203 gboolean
204 egg_accelerator_parse_virtual (const gchar *accelerator,
205 guint *accelerator_key,
206 EggVirtualModifierType *accelerator_mods)
208 guint keyval;
209 GdkModifierType mods;
210 gint len;
211 gboolean bad_keyval;
213 if (accelerator_key)
214 *accelerator_key = 0;
215 if (accelerator_mods)
216 *accelerator_mods = 0;
218 g_return_val_if_fail (accelerator != NULL, FALSE);
220 bad_keyval = FALSE;
222 keyval = 0;
223 mods = 0;
224 len = strlen (accelerator);
225 while (len)
227 if (*accelerator == '<')
229 if (len >= 9 && is_release (accelerator))
231 accelerator += 9;
232 len -= 9;
233 mods |= EGG_VIRTUAL_RELEASE_MASK;
235 else if (len >= 9 && is_control (accelerator))
237 accelerator += 9;
238 len -= 9;
239 mods |= EGG_VIRTUAL_CONTROL_MASK;
241 else if (len >= 7 && is_shift (accelerator))
243 accelerator += 7;
244 len -= 7;
245 mods |= EGG_VIRTUAL_SHIFT_MASK;
247 else if (len >= 6 && is_shft (accelerator))
249 accelerator += 6;
250 len -= 6;
251 mods |= EGG_VIRTUAL_SHIFT_MASK;
253 else if (len >= 6 && is_ctrl (accelerator))
255 accelerator += 6;
256 len -= 6;
257 mods |= EGG_VIRTUAL_CONTROL_MASK;
259 else if (len >= 6 && is_modx (accelerator))
261 static const guint mod_vals[] = {
262 EGG_VIRTUAL_ALT_MASK, EGG_VIRTUAL_MOD2_MASK, EGG_VIRTUAL_MOD3_MASK,
263 EGG_VIRTUAL_MOD4_MASK, EGG_VIRTUAL_MOD5_MASK
266 len -= 6;
267 accelerator += 4;
268 mods |= mod_vals[*accelerator - '1'];
269 accelerator += 2;
271 else if (len >= 5 && is_ctl (accelerator))
273 accelerator += 5;
274 len -= 5;
275 mods |= EGG_VIRTUAL_CONTROL_MASK;
277 else if (len >= 5 && is_alt (accelerator))
279 accelerator += 5;
280 len -= 5;
281 mods |= EGG_VIRTUAL_ALT_MASK;
283 else if (len >= 6 && is_meta (accelerator))
285 accelerator += 6;
286 len -= 6;
287 mods |= EGG_VIRTUAL_META_MASK;
289 else if (len >= 7 && is_hyper (accelerator))
291 accelerator += 7;
292 len -= 7;
293 mods |= EGG_VIRTUAL_HYPER_MASK;
295 else if (len >= 7 && is_super (accelerator))
297 accelerator += 7;
298 len -= 7;
299 mods |= EGG_VIRTUAL_SUPER_MASK;
301 else
303 gchar last_ch;
305 last_ch = *accelerator;
306 while (last_ch && last_ch != '>')
308 last_ch = *accelerator;
309 accelerator += 1;
310 len -= 1;
314 else
316 keyval = gdk_keyval_from_name (accelerator);
318 if (keyval == 0)
319 bad_keyval = TRUE;
321 accelerator += len;
322 len -= len;
326 if (accelerator_key)
327 *accelerator_key = gdk_keyval_to_lower (keyval);
328 if (accelerator_mods)
329 *accelerator_mods = mods;
331 return !bad_keyval;
336 * egg_virtual_accelerator_name:
337 * @accelerator_key: accelerator keyval
338 * @accelerator_mods: accelerator modifier mask
339 * @returns: a newly-allocated accelerator name
341 * Converts an accelerator keyval and modifier mask
342 * into a string parseable by egg_accelerator_parse_virtual().
343 * For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
344 * this function returns "&lt;Control&gt;q".
346 * The caller of this function must free the returned string.
348 gchar*
349 egg_virtual_accelerator_name (guint accelerator_key,
350 EggVirtualModifierType accelerator_mods)
352 static const gchar text_release[] = "<Release>";
353 static const gchar text_shift[] = "<Shift>";
354 static const gchar text_control[] = "<Control>";
355 static const gchar text_mod1[] = "<Alt>";
356 static const gchar text_mod2[] = "<Mod2>";
357 static const gchar text_mod3[] = "<Mod3>";
358 static const gchar text_mod4[] = "<Mod4>";
359 static const gchar text_mod5[] = "<Mod5>";
360 static const gchar text_meta[] = "<Meta>";
361 static const gchar text_super[] = "<Super>";
362 static const gchar text_hyper[] = "<Hyper>";
363 guint l;
364 gchar *keyval_name;
365 gchar *accelerator;
367 accelerator_mods &= EGG_VIRTUAL_MODIFIER_MASK;
369 keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
370 if (!keyval_name)
371 keyval_name = "";
373 l = 0;
374 if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
375 l += sizeof (text_release) - 1;
376 if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
377 l += sizeof (text_shift) - 1;
378 if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
379 l += sizeof (text_control) - 1;
380 if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
381 l += sizeof (text_mod1) - 1;
382 if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
383 l += sizeof (text_mod2) - 1;
384 if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
385 l += sizeof (text_mod3) - 1;
386 if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
387 l += sizeof (text_mod4) - 1;
388 if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
389 l += sizeof (text_mod5) - 1;
390 if (accelerator_mods & EGG_VIRTUAL_META_MASK)
391 l += sizeof (text_meta) - 1;
392 if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
393 l += sizeof (text_hyper) - 1;
394 if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
395 l += sizeof (text_super) - 1;
396 l += strlen (keyval_name);
398 accelerator = g_new (gchar, l + 1);
400 l = 0;
401 accelerator[l] = 0;
402 if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
404 strcpy (accelerator + l, text_release);
405 l += sizeof (text_release) - 1;
407 if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
409 strcpy (accelerator + l, text_shift);
410 l += sizeof (text_shift) - 1;
412 if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
414 strcpy (accelerator + l, text_control);
415 l += sizeof (text_control) - 1;
417 if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
419 strcpy (accelerator + l, text_mod1);
420 l += sizeof (text_mod1) - 1;
422 if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
424 strcpy (accelerator + l, text_mod2);
425 l += sizeof (text_mod2) - 1;
427 if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
429 strcpy (accelerator + l, text_mod3);
430 l += sizeof (text_mod3) - 1;
432 if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
434 strcpy (accelerator + l, text_mod4);
435 l += sizeof (text_mod4) - 1;
437 if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
439 strcpy (accelerator + l, text_mod5);
440 l += sizeof (text_mod5) - 1;
442 if (accelerator_mods & EGG_VIRTUAL_META_MASK)
444 strcpy (accelerator + l, text_meta);
445 l += sizeof (text_meta) - 1;
447 if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
449 strcpy (accelerator + l, text_hyper);
450 l += sizeof (text_hyper) - 1;
452 if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
454 strcpy (accelerator + l, text_super);
455 l += sizeof (text_super) - 1;
458 strcpy (accelerator + l, keyval_name);
460 return accelerator;
463 void
464 egg_keymap_resolve_virtual_modifiers (GdkKeymap *keymap,
465 EggVirtualModifierType virtual_mods,
466 GdkModifierType *concrete_mods)
468 GdkModifierType concrete;
469 int i;
470 const EggModmap *modmap;
472 g_return_if_fail (GDK_IS_KEYMAP (keymap));
473 g_return_if_fail (concrete_mods != NULL);
475 modmap = egg_keymap_get_modmap (keymap);
477 /* Not so sure about this algorithm. */
479 concrete = 0;
480 i = 0;
481 while (i < EGG_MODMAP_ENTRY_LAST)
483 if (modmap->mapping[i] & virtual_mods)
484 concrete |= (1 << i);
486 ++i;
489 *concrete_mods = concrete;
492 void
493 egg_keymap_virtualize_modifiers (GdkKeymap *keymap,
494 GdkModifierType concrete_mods,
495 EggVirtualModifierType *virtual_mods)
497 GdkModifierType virtual;
498 int i;
499 const EggModmap *modmap;
501 g_return_if_fail (GDK_IS_KEYMAP (keymap));
502 g_return_if_fail (virtual_mods != NULL);
504 modmap = egg_keymap_get_modmap (keymap);
506 /* Not so sure about this algorithm. */
508 virtual = 0;
509 i = 0;
510 while (i < EGG_MODMAP_ENTRY_LAST)
512 if ((1 << i) & concrete_mods)
514 EggVirtualModifierType cleaned;
516 cleaned = modmap->mapping[i] & ~(EGG_VIRTUAL_MOD2_MASK |
517 EGG_VIRTUAL_MOD3_MASK |
518 EGG_VIRTUAL_MOD4_MASK |
519 EGG_VIRTUAL_MOD5_MASK);
521 if (cleaned != 0)
523 virtual |= cleaned;
525 else
527 /* Rather than dropping mod2->mod5 if not bound,
528 * go ahead and use the concrete names
530 virtual |= modmap->mapping[i];
534 ++i;
537 *virtual_mods = virtual;
540 static void
541 reload_modmap (GdkKeymap *keymap,
542 EggModmap *modmap)
544 XModifierKeymap *xmodmap;
545 int map_size;
546 int i;
548 /* FIXME multihead */
549 xmodmap = XGetModifierMapping (gdk_x11_get_default_xdisplay ());
551 memset (modmap->mapping, 0, sizeof (modmap->mapping));
553 /* there are 8 modifiers, and the first 3 are shift, shift lock,
554 * and control
556 map_size = 8 * xmodmap->max_keypermod;
557 i = 3 * xmodmap->max_keypermod;
558 while (i < map_size)
560 /* get the key code at this point in the map,
561 * see if its keysym is one we're interested in
563 int keycode = xmodmap->modifiermap[i];
564 GdkKeymapKey *keys;
565 guint *keyvals;
566 int n_entries;
567 int j;
568 EggVirtualModifierType mask;
570 keys = NULL;
571 keyvals = NULL;
572 n_entries = 0;
574 gdk_keymap_get_entries_for_keycode (keymap,
575 keycode,
576 &keys, &keyvals, &n_entries);
578 mask = 0;
579 j = 0;
580 while (j < n_entries)
582 if (keyvals[j] == GDK_Num_Lock)
583 mask |= EGG_VIRTUAL_NUM_LOCK_MASK;
584 else if (keyvals[j] == GDK_Scroll_Lock)
585 mask |= EGG_VIRTUAL_SCROLL_LOCK_MASK;
586 else if (keyvals[j] == GDK_Meta_L ||
587 keyvals[j] == GDK_Meta_R)
588 mask |= EGG_VIRTUAL_META_MASK;
589 else if (keyvals[j] == GDK_Hyper_L ||
590 keyvals[j] == GDK_Hyper_R)
591 mask |= EGG_VIRTUAL_HYPER_MASK;
592 else if (keyvals[j] == GDK_Super_L ||
593 keyvals[j] == GDK_Super_R)
594 mask |= EGG_VIRTUAL_SUPER_MASK;
595 else if (keyvals[j] == GDK_Mode_switch)
596 mask |= EGG_VIRTUAL_MODE_SWITCH_MASK;
598 ++j;
601 /* Mod1Mask is 1 << 3 for example, i.e. the
602 * fourth modifier, i / keyspermod is the modifier
603 * index
605 modmap->mapping[i/xmodmap->max_keypermod] |= mask;
607 g_free (keyvals);
608 g_free (keys);
610 ++i;
613 /* Add in the not-really-virtual fixed entries */
614 modmap->mapping[EGG_MODMAP_ENTRY_SHIFT] |= EGG_VIRTUAL_SHIFT_MASK;
615 modmap->mapping[EGG_MODMAP_ENTRY_CONTROL] |= EGG_VIRTUAL_CONTROL_MASK;
616 modmap->mapping[EGG_MODMAP_ENTRY_LOCK] |= EGG_VIRTUAL_LOCK_MASK;
617 modmap->mapping[EGG_MODMAP_ENTRY_MOD1] |= EGG_VIRTUAL_ALT_MASK;
618 modmap->mapping[EGG_MODMAP_ENTRY_MOD2] |= EGG_VIRTUAL_MOD2_MASK;
619 modmap->mapping[EGG_MODMAP_ENTRY_MOD3] |= EGG_VIRTUAL_MOD3_MASK;
620 modmap->mapping[EGG_MODMAP_ENTRY_MOD4] |= EGG_VIRTUAL_MOD4_MASK;
621 modmap->mapping[EGG_MODMAP_ENTRY_MOD5] |= EGG_VIRTUAL_MOD5_MASK;
623 XFreeModifiermap (xmodmap);
626 const EggModmap*
627 egg_keymap_get_modmap (GdkKeymap *keymap)
629 EggModmap *modmap;
631 /* This is all a hack, much simpler when we can just
632 * modify GDK directly.
635 modmap = g_object_get_data (G_OBJECT (keymap),
636 "egg-modmap");
638 if (modmap == NULL)
640 modmap = g_new0 (EggModmap, 1);
642 /* FIXME modify keymap change events with an event filter
643 * and force a reload if we get one
646 reload_modmap (keymap, modmap);
648 g_object_set_data_full (G_OBJECT (keymap),
649 "egg-modmap",
650 modmap,
651 g_free);
654 g_assert (modmap != NULL);
656 return modmap;