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"
25 #include <gdk/gdkkeysyms.h>
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))
44 EggVirtualModifierType mapping[EGG_MODMAP_ENTRY_LAST];
48 const EggModmap* egg_keymap_get_modmap (GdkKeymap *keymap);
51 static inline gboolean
52 is_alt (const gchar *string)
54 return ((string[0] == '<') &&
55 (string[1] == 'a' || string[1] == 'A') &&
56 (string[2] == 'l' || string[2] == 'L') &&
57 (string[3] == 't' || string[3] == 'T') &&
61 static inline gboolean
62 is_ctl (const gchar *string)
64 return ((string[0] == '<') &&
65 (string[1] == 'c' || string[1] == 'C') &&
66 (string[2] == 't' || string[2] == 'T') &&
67 (string[3] == 'l' || string[3] == 'L') &&
71 static inline gboolean
72 is_modx (const gchar *string)
74 return ((string[0] == '<') &&
75 (string[1] == 'm' || string[1] == 'M') &&
76 (string[2] == 'o' || string[2] == 'O') &&
77 (string[3] == 'd' || string[3] == 'D') &&
78 (string[4] >= '1' && string[4] <= '5') &&
82 static inline gboolean
83 is_ctrl (const gchar *string)
85 return ((string[0] == '<') &&
86 (string[1] == 'c' || string[1] == 'C') &&
87 (string[2] == 't' || string[2] == 'T') &&
88 (string[3] == 'r' || string[3] == 'R') &&
89 (string[4] == 'l' || string[4] == 'L') &&
93 static inline gboolean
94 is_shft (const gchar *string)
96 return ((string[0] == '<') &&
97 (string[1] == 's' || string[1] == 'S') &&
98 (string[2] == 'h' || string[2] == 'H') &&
99 (string[3] == 'f' || string[3] == 'F') &&
100 (string[4] == 't' || string[4] == 'T') &&
104 static inline gboolean
105 is_shift (const gchar *string)
107 return ((string[0] == '<') &&
108 (string[1] == 's' || string[1] == 'S') &&
109 (string[2] == 'h' || string[2] == 'H') &&
110 (string[3] == 'i' || string[3] == 'I') &&
111 (string[4] == 'f' || string[4] == 'F') &&
112 (string[5] == 't' || string[5] == 'T') &&
116 static inline gboolean
117 is_control (const gchar *string)
119 return ((string[0] == '<') &&
120 (string[1] == 'c' || string[1] == 'C') &&
121 (string[2] == 'o' || string[2] == 'O') &&
122 (string[3] == 'n' || string[3] == 'N') &&
123 (string[4] == 't' || string[4] == 'T') &&
124 (string[5] == 'r' || string[5] == 'R') &&
125 (string[6] == 'o' || string[6] == 'O') &&
126 (string[7] == 'l' || string[7] == 'L') &&
130 static inline gboolean
131 is_release (const gchar *string)
133 return ((string[0] == '<') &&
134 (string[1] == 'r' || string[1] == 'R') &&
135 (string[2] == 'e' || string[2] == 'E') &&
136 (string[3] == 'l' || string[3] == 'L') &&
137 (string[4] == 'e' || string[4] == 'E') &&
138 (string[5] == 'a' || string[5] == 'A') &&
139 (string[6] == 's' || string[6] == 'S') &&
140 (string[7] == 'e' || string[7] == 'E') &&
144 static inline gboolean
145 is_meta (const gchar *string)
147 return ((string[0] == '<') &&
148 (string[1] == 'm' || string[1] == 'M') &&
149 (string[2] == 'e' || string[2] == 'E') &&
150 (string[3] == 't' || string[3] == 'T') &&
151 (string[4] == 'a' || string[4] == 'A') &&
155 static inline gboolean
156 is_super (const gchar *string)
158 return ((string[0] == '<') &&
159 (string[1] == 's' || string[1] == 'S') &&
160 (string[2] == 'u' || string[2] == 'U') &&
161 (string[3] == 'p' || string[3] == 'P') &&
162 (string[4] == 'e' || string[4] == 'E') &&
163 (string[5] == 'r' || string[5] == 'R') &&
167 static inline gboolean
168 is_hyper (const gchar *string)
170 return ((string[0] == '<') &&
171 (string[1] == 'h' || string[1] == 'H') &&
172 (string[2] == 'y' || string[2] == 'Y') &&
173 (string[3] == 'p' || string[3] == 'P') &&
174 (string[4] == 'e' || string[4] == 'E') &&
175 (string[5] == 'r' || string[5] == 'R') &&
180 * egg_accelerator_parse_virtual:
181 * @accelerator: string representing an accelerator
182 * @accelerator_key: return location for accelerator keyval
183 * @accelerator_mods: return location for accelerator modifier mask
185 * Parses a string representing a virtual accelerator. The format
186 * looks like "<Control>a" or "<Shift><Alt>F1" or
187 * "<Release>z" (the last one is for key release). The parser
188 * is fairly liberal and allows lower or upper case, and also
189 * abbreviations such as "<Ctl>" and "<Ctrl>".
191 * If the parse fails, @accelerator_key and @accelerator_mods will
192 * be set to 0 (zero) and %FALSE will be returned. If the string contains
193 * only modifiers, @accelerator_key will be set to 0 but %TRUE will be
196 * The virtual vs. concrete accelerator distinction is a relic of
197 * how the X Window System works; there are modifiers Mod2-Mod5 that
198 * can represent various keyboard keys (numlock, meta, hyper, etc.),
199 * the virtual modifier represents the keyboard key, the concrete
200 * modifier the actual Mod2-Mod5 bits in the key press event.
202 * Returns: %TRUE on success.
205 egg_accelerator_parse_virtual (const gchar *accelerator,
206 guint *accelerator_key,
207 EggVirtualModifierType *accelerator_mods)
210 GdkModifierType mods;
215 *accelerator_key = 0;
216 if (accelerator_mods)
217 *accelerator_mods = 0;
219 g_return_val_if_fail (accelerator != NULL, FALSE);
225 len = strlen (accelerator);
228 if (*accelerator == '<')
230 if (len >= 9 && is_release (accelerator))
234 mods |= EGG_VIRTUAL_RELEASE_MASK;
236 else if (len >= 9 && is_control (accelerator))
240 mods |= EGG_VIRTUAL_CONTROL_MASK;
242 else if (len >= 7 && is_shift (accelerator))
246 mods |= EGG_VIRTUAL_SHIFT_MASK;
248 else if (len >= 6 && is_shft (accelerator))
252 mods |= EGG_VIRTUAL_SHIFT_MASK;
254 else if (len >= 6 && is_ctrl (accelerator))
258 mods |= EGG_VIRTUAL_CONTROL_MASK;
260 else if (len >= 6 && is_modx (accelerator))
262 static const guint mod_vals[] = {
263 EGG_VIRTUAL_ALT_MASK, EGG_VIRTUAL_MOD2_MASK, EGG_VIRTUAL_MOD3_MASK,
264 EGG_VIRTUAL_MOD4_MASK, EGG_VIRTUAL_MOD5_MASK
269 mods |= mod_vals[*accelerator - '1'];
272 else if (len >= 5 && is_ctl (accelerator))
276 mods |= EGG_VIRTUAL_CONTROL_MASK;
278 else if (len >= 5 && is_alt (accelerator))
282 mods |= EGG_VIRTUAL_ALT_MASK;
284 else if (len >= 6 && is_meta (accelerator))
288 mods |= EGG_VIRTUAL_META_MASK;
290 else if (len >= 7 && is_hyper (accelerator))
294 mods |= EGG_VIRTUAL_HYPER_MASK;
296 else if (len >= 7 && is_super (accelerator))
300 mods |= EGG_VIRTUAL_SUPER_MASK;
306 last_ch = *accelerator;
307 while (last_ch && last_ch != '>')
309 last_ch = *accelerator;
317 keyval = gdk_keyval_from_name (accelerator);
328 *accelerator_key = gdk_keyval_to_lower (keyval);
329 if (accelerator_mods)
330 *accelerator_mods = mods;
337 * egg_virtual_accelerator_name:
338 * @accelerator_key: accelerator keyval
339 * @accelerator_mods: accelerator modifier mask
340 * @returns: a newly-allocated accelerator name
342 * Converts an accelerator keyval and modifier mask
343 * into a string parseable by egg_accelerator_parse_virtual().
344 * For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
345 * this function returns "<Control>q".
347 * The caller of this function must free the returned string.
350 egg_virtual_accelerator_name (guint accelerator_key,
351 EggVirtualModifierType accelerator_mods)
353 static const gchar text_release[] = "<Release>";
354 static const gchar text_shift[] = "<Shift>";
355 static const gchar text_control[] = "<Control>";
356 static const gchar text_mod1[] = "<Alt>";
357 static const gchar text_mod2[] = "<Mod2>";
358 static const gchar text_mod3[] = "<Mod3>";
359 static const gchar text_mod4[] = "<Mod4>";
360 static const gchar text_mod5[] = "<Mod5>";
361 static const gchar text_meta[] = "<Meta>";
362 static const gchar text_super[] = "<Super>";
363 static const gchar text_hyper[] = "<Hyper>";
368 accelerator_mods &= EGG_VIRTUAL_MODIFIER_MASK;
370 keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
375 if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
376 l += sizeof (text_release) - 1;
377 if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
378 l += sizeof (text_shift) - 1;
379 if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
380 l += sizeof (text_control) - 1;
381 if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
382 l += sizeof (text_mod1) - 1;
383 if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
384 l += sizeof (text_mod2) - 1;
385 if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
386 l += sizeof (text_mod3) - 1;
387 if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
388 l += sizeof (text_mod4) - 1;
389 if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
390 l += sizeof (text_mod5) - 1;
391 if (accelerator_mods & EGG_VIRTUAL_META_MASK)
392 l += sizeof (text_meta) - 1;
393 if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
394 l += sizeof (text_hyper) - 1;
395 if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
396 l += sizeof (text_super) - 1;
397 l += strlen (keyval_name);
399 accelerator = g_new (gchar, l + 1);
403 if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
405 strcpy (accelerator + l, text_release);
406 l += sizeof (text_release) - 1;
408 if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
410 strcpy (accelerator + l, text_shift);
411 l += sizeof (text_shift) - 1;
413 if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
415 strcpy (accelerator + l, text_control);
416 l += sizeof (text_control) - 1;
418 if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
420 strcpy (accelerator + l, text_mod1);
421 l += sizeof (text_mod1) - 1;
423 if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
425 strcpy (accelerator + l, text_mod2);
426 l += sizeof (text_mod2) - 1;
428 if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
430 strcpy (accelerator + l, text_mod3);
431 l += sizeof (text_mod3) - 1;
433 if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
435 strcpy (accelerator + l, text_mod4);
436 l += sizeof (text_mod4) - 1;
438 if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
440 strcpy (accelerator + l, text_mod5);
441 l += sizeof (text_mod5) - 1;
443 if (accelerator_mods & EGG_VIRTUAL_META_MASK)
445 strcpy (accelerator + l, text_meta);
446 l += sizeof (text_meta) - 1;
448 if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
450 strcpy (accelerator + l, text_hyper);
451 l += sizeof (text_hyper) - 1;
453 if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
455 strcpy (accelerator + l, text_super);
456 l += sizeof (text_super) - 1;
459 strcpy (accelerator + l, keyval_name);
466 egg_keymap_resolve_virtual_modifiers (GdkKeymap *keymap,
467 GdkModifierType virtual_mods,
468 GdkModifierType *concrete_mods)
470 GdkModifierType concrete;
472 const EggModmap *modmap;
474 g_return_if_fail (GDK_IS_KEYMAP (keymap));
475 g_return_if_fail (concrete_mods != NULL);
477 modmap = egg_keymap_get_modmap (keymap);
479 /* Not so sure about this algorithm. */
483 while (i < EGG_MODMAP_ENTRY_LAST)
485 if (modmap->mapping[i] & virtual_mods)
486 concrete |= (1 << i);
491 *concrete_mods = concrete;
496 egg_keymap_virtualize_modifiers (GdkKeymap *keymap,
497 GdkModifierType concrete_mods,
498 EggVirtualModifierType *virtual_mods)
500 GdkModifierType virtual;
502 const EggModmap *modmap;
504 g_return_if_fail (GDK_IS_KEYMAP (keymap));
505 g_return_if_fail (virtual_mods != NULL);
507 modmap = egg_keymap_get_modmap (keymap);
509 /* Not so sure about this algorithm. */
513 while (i < EGG_MODMAP_ENTRY_LAST)
515 if ((1 << i) & concrete_mods)
517 EggVirtualModifierType cleaned;
519 cleaned = modmap->mapping[i] & ~(EGG_VIRTUAL_MOD2_MASK |
520 EGG_VIRTUAL_MOD3_MASK |
521 EGG_VIRTUAL_MOD4_MASK |
522 EGG_VIRTUAL_MOD5_MASK);
530 /* Rather than dropping mod2->mod5 if not bound,
531 * go ahead and use the concrete names
533 virtual |= modmap->mapping[i];
540 *virtual_mods = virtual;
545 reload_modmap (GdkKeymap *keymap,
548 XModifierKeymap *xmodmap;
552 /* FIXME multihead */
553 xmodmap = XGetModifierMapping (gdk_x11_get_default_xdisplay ());
555 memset (modmap->mapping, 0, sizeof (modmap->mapping));
557 /* there are 8 modifiers, and the first 3 are shift, shift lock,
560 map_size = 8 * xmodmap->max_keypermod;
561 i = 3 * xmodmap->max_keypermod;
564 /* get the key code at this point in the map,
565 * see if its keysym is one we're interested in
567 int keycode = xmodmap->modifiermap[i];
572 EggVirtualModifierType mask;
578 gdk_keymap_get_entries_for_keycode (keymap,
580 &keys, &keyvals, &n_entries);
584 while (j < n_entries)
586 if (keyvals[j] == GDK_Num_Lock)
587 mask |= EGG_VIRTUAL_NUM_LOCK_MASK;
588 else if (keyvals[j] == GDK_Scroll_Lock)
589 mask |= EGG_VIRTUAL_SCROLL_LOCK_MASK;
590 else if (keyvals[j] == GDK_Meta_L ||
591 keyvals[j] == GDK_Meta_R)
592 mask |= EGG_VIRTUAL_META_MASK;
593 else if (keyvals[j] == GDK_Hyper_L ||
594 keyvals[j] == GDK_Hyper_R)
595 mask |= EGG_VIRTUAL_HYPER_MASK;
596 else if (keyvals[j] == GDK_Super_L ||
597 keyvals[j] == GDK_Super_R)
598 mask |= EGG_VIRTUAL_SUPER_MASK;
599 else if (keyvals[j] == GDK_Mode_switch)
600 mask |= EGG_VIRTUAL_MODE_SWITCH_MASK;
605 /* Mod1Mask is 1 << 3 for example, i.e. the
606 * fourth modifier, i / keyspermod is the modifier
609 modmap->mapping[i/xmodmap->max_keypermod] |= mask;
617 /* Add in the not-really-virtual fixed entries */
618 modmap->mapping[EGG_MODMAP_ENTRY_SHIFT] |= EGG_VIRTUAL_SHIFT_MASK;
619 modmap->mapping[EGG_MODMAP_ENTRY_CONTROL] |= EGG_VIRTUAL_CONTROL_MASK;
620 modmap->mapping[EGG_MODMAP_ENTRY_LOCK] |= EGG_VIRTUAL_LOCK_MASK;
621 modmap->mapping[EGG_MODMAP_ENTRY_MOD1] |= EGG_VIRTUAL_ALT_MASK;
622 modmap->mapping[EGG_MODMAP_ENTRY_MOD2] |= EGG_VIRTUAL_MOD2_MASK;
623 modmap->mapping[EGG_MODMAP_ENTRY_MOD3] |= EGG_VIRTUAL_MOD3_MASK;
624 modmap->mapping[EGG_MODMAP_ENTRY_MOD4] |= EGG_VIRTUAL_MOD4_MASK;
625 modmap->mapping[EGG_MODMAP_ENTRY_MOD5] |= EGG_VIRTUAL_MOD5_MASK;
627 XFreeModifiermap (xmodmap);
631 egg_keymap_get_modmap (GdkKeymap *keymap)
635 /* This is all a hack, much simpler when we can just
636 * modify GDK directly.
639 modmap = g_object_get_data (G_OBJECT (keymap),
644 modmap = g_new0 (EggModmap, 1);
646 /* FIXME modify keymap change events with an event filter
647 * and force a reload if we get one
650 reload_modmap (keymap, modmap);
652 g_object_set_data_full (G_OBJECT (keymap),
658 g_assert (modmap != NULL);