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"
26
#include <gdk/gdkkeysyms.h>
27
#include <gtk/gtkaccelgroup.h>
31
EGG_MODMAP_ENTRY_SHIFT = 0,
32
EGG_MODMAP_ENTRY_LOCK = 1,
33
EGG_MODMAP_ENTRY_CONTROL = 2,
34
EGG_MODMAP_ENTRY_MOD1 = 3,
35
EGG_MODMAP_ENTRY_MOD2 = 4,
36
EGG_MODMAP_ENTRY_MOD3 = 5,
37
EGG_MODMAP_ENTRY_MOD4 = 6,
38
EGG_MODMAP_ENTRY_MOD5 = 7,
39
EGG_MODMAP_ENTRY_LAST = 8
42
#define MODMAP_ENTRY_TO_MODIFIER(x) (1 << (x))
46
EggVirtualModifierType mapping[EGG_MODMAP_ENTRY_LAST];
50
const EggModmap* egg_keymap_get_modmap (GdkKeymap *keymap);
52
static inline gboolean
53
is_alt (const gchar *string)
55
return ((string[0] == '<') &&
56
(string[1] == 'a' || string[1] == 'A') &&
57
(string[2] == 'l' || string[2] == 'L') &&
58
(string[3] == 't' || string[3] == 'T') &&
62
static inline gboolean
63
is_ctl (const gchar *string)
65
return ((string[0] == '<') &&
66
(string[1] == 'c' || string[1] == 'C') &&
67
(string[2] == 't' || string[2] == 'T') &&
68
(string[3] == 'l' || string[3] == 'L') &&
72
static inline gboolean
73
is_modx (const gchar *string)
75
return ((string[0] == '<') &&
76
(string[1] == 'm' || string[1] == 'M') &&
77
(string[2] == 'o' || string[2] == 'O') &&
78
(string[3] == 'd' || string[3] == 'D') &&
79
(string[4] >= '1' && string[4] <= '5') &&
83
static inline gboolean
84
is_ctrl (const gchar *string)
86
return ((string[0] == '<') &&
87
(string[1] == 'c' || string[1] == 'C') &&
88
(string[2] == 't' || string[2] == 'T') &&
89
(string[3] == 'r' || string[3] == 'R') &&
90
(string[4] == 'l' || string[4] == 'L') &&
94
static inline gboolean
95
is_shft (const gchar *string)
97
return ((string[0] == '<') &&
98
(string[1] == 's' || string[1] == 'S') &&
99
(string[2] == 'h' || string[2] == 'H') &&
100
(string[3] == 'f' || string[3] == 'F') &&
101
(string[4] == 't' || string[4] == 'T') &&
105
static inline gboolean
106
is_shift (const gchar *string)
108
return ((string[0] == '<') &&
109
(string[1] == 's' || string[1] == 'S') &&
110
(string[2] == 'h' || string[2] == 'H') &&
111
(string[3] == 'i' || string[3] == 'I') &&
112
(string[4] == 'f' || string[4] == 'F') &&
113
(string[5] == 't' || string[5] == 'T') &&
117
static inline gboolean
118
is_control (const gchar *string)
120
return ((string[0] == '<') &&
121
(string[1] == 'c' || string[1] == 'C') &&
122
(string[2] == 'o' || string[2] == 'O') &&
123
(string[3] == 'n' || string[3] == 'N') &&
124
(string[4] == 't' || string[4] == 'T') &&
125
(string[5] == 'r' || string[5] == 'R') &&
126
(string[6] == 'o' || string[6] == 'O') &&
127
(string[7] == 'l' || string[7] == 'L') &&
131
static inline gboolean
132
is_release (const gchar *string)
134
return ((string[0] == '<') &&
135
(string[1] == 'r' || string[1] == 'R') &&
136
(string[2] == 'e' || string[2] == 'E') &&
137
(string[3] == 'l' || string[3] == 'L') &&
138
(string[4] == 'e' || string[4] == 'E') &&
139
(string[5] == 'a' || string[5] == 'A') &&
140
(string[6] == 's' || string[6] == 'S') &&
141
(string[7] == 'e' || string[7] == 'E') &&
145
static inline gboolean
146
is_meta (const gchar *string)
148
return ((string[0] == '<') &&
149
(string[1] == 'm' || string[1] == 'M') &&
150
(string[2] == 'e' || string[2] == 'E') &&
151
(string[3] == 't' || string[3] == 'T') &&
152
(string[4] == 'a' || string[4] == 'A') &&
156
static inline gboolean
157
is_super (const gchar *string)
159
return ((string[0] == '<') &&
160
(string[1] == 's' || string[1] == 'S') &&
161
(string[2] == 'u' || string[2] == 'U') &&
162
(string[3] == 'p' || string[3] == 'P') &&
163
(string[4] == 'e' || string[4] == 'E') &&
164
(string[5] == 'r' || string[5] == 'R') &&
168
static inline gboolean
169
is_hyper (const gchar *string)
171
return ((string[0] == '<') &&
172
(string[1] == 'h' || string[1] == 'H') &&
173
(string[2] == 'y' || string[2] == 'Y') &&
174
(string[3] == 'p' || string[3] == 'P') &&
175
(string[4] == 'e' || string[4] == 'E') &&
176
(string[5] == 'r' || string[5] == 'R') &&
180
is_primary (const gchar *string)
182
return ((string[0] == '<') &&
183
(string[1] == 'p' || string[1] == 'P') &&
184
(string[2] == 'r' || string[2] == 'R') &&
185
(string[3] == 'i' || string[3] == 'I') &&
186
(string[4] == 'm' || string[4] == 'M') &&
187
(string[5] == 'a' || string[5] == 'A') &&
188
(string[6] == 'r' || string[6] == 'R') &&
189
(string[7] == 'y' || string[7] == 'Y') &&
193
static inline gboolean
194
is_keycode (const gchar *string)
196
return ((string[0] == '0') &&
201
* egg_accelerator_parse_virtual:
202
* @accelerator: string representing an accelerator
203
* @accelerator_key: return location for accelerator keyval
204
* @accelerator_mods: return location for accelerator modifier mask
206
* Parses a string representing a virtual accelerator. The format
207
* looks like "<Control>a" or "<Shift><Alt>F1" or
208
* "<Release>z" (the last one is for key release). The parser
209
* is fairly liberal and allows lower or upper case, and also
210
* abbreviations such as "<Ctl>" and "<Ctrl>".
212
* If the parse fails, @accelerator_key and @accelerator_mods will
213
* be set to 0 (zero) and %FALSE will be returned. If the string contains
214
* only modifiers, @accelerator_key will be set to 0 but %TRUE will be
217
* The virtual vs. concrete accelerator distinction is a relic of
218
* how the X Window System works; there are modifiers Mod2-Mod5 that
219
* can represent various keyboard keys (numlock, meta, hyper, etc.),
220
* the virtual modifier represents the keyboard key, the concrete
221
* modifier the actual Mod2-Mod5 bits in the key press event.
223
* Returns: %TRUE on success.
226
egg_accelerator_parse_virtual (const gchar *accelerator,
227
guint *accelerator_key,
229
EggVirtualModifierType *accelerator_mods)
232
GdkModifierType mods;
237
*accelerator_key = 0;
238
if (accelerator_mods)
239
*accelerator_mods = 0;
243
g_return_val_if_fail (accelerator != NULL, FALSE);
249
len = strlen (accelerator);
252
if (*accelerator == '<')
254
if (len >= 9 && is_release (accelerator))
258
mods |= EGG_VIRTUAL_RELEASE_MASK;
260
else if (len >= 9 && is_control (accelerator))
264
mods |= EGG_VIRTUAL_CONTROL_MASK;
266
else if (len >= 7 && is_shift (accelerator))
270
mods |= EGG_VIRTUAL_SHIFT_MASK;
272
else if (len >= 6 && is_shft (accelerator))
276
mods |= EGG_VIRTUAL_SHIFT_MASK;
278
else if (len >= 6 && is_ctrl (accelerator))
282
mods |= EGG_VIRTUAL_CONTROL_MASK;
284
else if (len >= 6 && is_modx (accelerator))
286
static const guint mod_vals[] = {
287
EGG_VIRTUAL_ALT_MASK, EGG_VIRTUAL_MOD2_MASK, EGG_VIRTUAL_MOD3_MASK,
288
EGG_VIRTUAL_MOD4_MASK, EGG_VIRTUAL_MOD5_MASK
293
mods |= mod_vals[*accelerator - '1'];
296
else if (len >= 5 && is_ctl (accelerator))
300
mods |= EGG_VIRTUAL_CONTROL_MASK;
302
else if (len >= 5 && is_alt (accelerator))
306
mods |= EGG_VIRTUAL_ALT_MASK;
308
else if (len >= 6 && is_meta (accelerator))
312
mods |= EGG_VIRTUAL_META_MASK;
314
else if (len >= 7 && is_hyper (accelerator))
318
mods |= EGG_VIRTUAL_HYPER_MASK;
320
else if (len >= 7 && is_super (accelerator))
324
mods |= EGG_VIRTUAL_SUPER_MASK;
326
else if (len >= 9 && is_primary (accelerator))
330
mods |= EGG_VIRTUAL_CONTROL_MASK;
336
last_ch = *accelerator;
337
while (last_ch && last_ch != '>')
339
last_ch = *accelerator;
347
keyval = gdk_keyval_from_name (accelerator);
351
/* If keyval is 0, than maybe it's a keycode. Check for 0x## */
352
if (len >= 4 && is_keycode (accelerator))
358
memcpy (keystring, accelerator, 4);
359
keystring [4] = '\000';
361
tmp_keycode = strtol (keystring, &endptr, 16);
363
if (endptr == NULL || *endptr != '\000')
369
*keycode = tmp_keycode;
370
/* 0x00 is an invalid keycode too. */
383
*accelerator_key = gdk_keyval_to_lower (keyval);
384
if (accelerator_mods)
385
*accelerator_mods = mods;
391
* egg_virtual_accelerator_name:
392
* @accelerator_key: accelerator keyval
393
* @accelerator_mods: accelerator modifier mask
394
* @returns: a newly-allocated accelerator name
396
* Converts an accelerator keyval and modifier mask
397
* into a string parseable by egg_accelerator_parse_virtual().
398
* For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
399
* this function returns "<Control>q".
401
* The caller of this function must free the returned string.
404
egg_virtual_accelerator_name (guint accelerator_key,
406
EggVirtualModifierType accelerator_mods)
408
static const gchar text_release[] = "<Release>";
409
static const gchar text_shift[] = "<Shift>";
410
static const gchar text_control[] = "<Control>";
411
static const gchar text_mod1[] = "<Alt>";
412
static const gchar text_mod2[] = "<Mod2>";
413
static const gchar text_mod3[] = "<Mod3>";
414
static const gchar text_mod4[] = "<Mod4>";
415
static const gchar text_mod5[] = "<Mod5>";
416
static const gchar text_meta[] = "<Meta>";
417
static const gchar text_super[] = "<Super>";
418
static const gchar text_hyper[] = "<Hyper>";
420
gchar *keyval_name, *str = NULL;
423
accelerator_mods &= EGG_VIRTUAL_MODIFIER_MASK;
425
if (!accelerator_key)
427
str = g_strdup_printf ("0x%02x", keycode);
432
keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
438
if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
439
l += sizeof (text_release) - 1;
440
if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
441
l += sizeof (text_shift) - 1;
442
if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
443
l += sizeof (text_control) - 1;
444
if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
445
l += sizeof (text_mod1) - 1;
446
if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
447
l += sizeof (text_mod2) - 1;
448
if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
449
l += sizeof (text_mod3) - 1;
450
if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
451
l += sizeof (text_mod4) - 1;
452
if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
453
l += sizeof (text_mod5) - 1;
454
if (accelerator_mods & EGG_VIRTUAL_META_MASK)
455
l += sizeof (text_meta) - 1;
456
if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
457
l += sizeof (text_hyper) - 1;
458
if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
459
l += sizeof (text_super) - 1;
460
l += strlen (keyval_name);
462
accelerator = g_new (gchar, l + 1);
466
if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
468
strcpy (accelerator + l, text_release);
469
l += sizeof (text_release) - 1;
471
if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
473
strcpy (accelerator + l, text_shift);
474
l += sizeof (text_shift) - 1;
476
if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
478
strcpy (accelerator + l, text_control);
479
l += sizeof (text_control) - 1;
481
if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
483
strcpy (accelerator + l, text_mod1);
484
l += sizeof (text_mod1) - 1;
486
if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
488
strcpy (accelerator + l, text_mod2);
489
l += sizeof (text_mod2) - 1;
491
if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
493
strcpy (accelerator + l, text_mod3);
494
l += sizeof (text_mod3) - 1;
496
if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
498
strcpy (accelerator + l, text_mod4);
499
l += sizeof (text_mod4) - 1;
501
if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
503
strcpy (accelerator + l, text_mod5);
504
l += sizeof (text_mod5) - 1;
506
if (accelerator_mods & EGG_VIRTUAL_META_MASK)
508
strcpy (accelerator + l, text_meta);
509
l += sizeof (text_meta) - 1;
511
if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
513
strcpy (accelerator + l, text_hyper);
514
l += sizeof (text_hyper) - 1;
516
if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
518
strcpy (accelerator + l, text_super);
519
l += sizeof (text_super) - 1;
522
strcpy (accelerator + l, keyval_name);
529
* egg_virtual_accelerator_label:
530
* @accelerator_key: accelerator keyval
531
* @accelerator_mods: accelerator modifier mask
532
* @returns: a newly-allocated accelerator label
534
* Converts an accelerator keyval and modifier mask
535
* into a (possibly translated) string that can be displayed to
537
* For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
538
* and you use a German locale, this function returns "Strg+Q".
540
* The caller of this function must free the returned string.
543
egg_virtual_accelerator_label (guint accelerator_key,
545
EggVirtualModifierType accelerator_mods)
548
GdkModifierType gdkmods;
550
egg_keymap_resolve_virtual_modifiers (gdk_keymap_get_default (),
551
accelerator_mods, &gdkmods);
552
gtk_label = gtk_accelerator_get_label (accelerator_key, gdkmods);
554
if (!accelerator_key)
557
label = g_strdup_printf ("%s0x%02x", gtk_label, keycode);
566
egg_keymap_resolve_virtual_modifiers (GdkKeymap *keymap,
567
EggVirtualModifierType virtual_mods,
568
GdkModifierType *concrete_mods)
570
GdkModifierType concrete;
572
const EggModmap *modmap;
574
g_return_if_fail (GDK_IS_KEYMAP (keymap));
575
g_return_if_fail (concrete_mods != NULL);
577
modmap = egg_keymap_get_modmap (keymap);
579
/* Not so sure about this algorithm. */
583
while (i < EGG_MODMAP_ENTRY_LAST)
585
if (modmap->mapping[i] & virtual_mods)
586
concrete |= (1 << i);
591
*concrete_mods = concrete;
595
egg_keymap_virtualize_modifiers (GdkKeymap *keymap,
596
GdkModifierType concrete_mods,
597
EggVirtualModifierType *virtual_mods)
599
GdkModifierType virtual;
601
const EggModmap *modmap;
603
g_return_if_fail (GDK_IS_KEYMAP (keymap));
604
g_return_if_fail (virtual_mods != NULL);
606
modmap = egg_keymap_get_modmap (keymap);
608
/* Not so sure about this algorithm. */
612
while (i < EGG_MODMAP_ENTRY_LAST)
614
if ((1 << i) & concrete_mods)
616
EggVirtualModifierType cleaned;
618
cleaned = modmap->mapping[i] & ~(EGG_VIRTUAL_MOD2_MASK |
619
EGG_VIRTUAL_MOD3_MASK |
620
EGG_VIRTUAL_MOD4_MASK |
621
EGG_VIRTUAL_MOD5_MASK);
629
/* Rather than dropping mod2->mod5 if not bound,
630
* go ahead and use the concrete names
632
virtual |= modmap->mapping[i];
639
*virtual_mods = virtual;
643
reload_modmap (GdkKeymap *keymap,
646
XModifierKeymap *xmodmap;
650
/* FIXME multihead */
651
xmodmap = XGetModifierMapping (gdk_x11_get_default_xdisplay ());
653
memset (modmap->mapping, 0, sizeof (modmap->mapping));
655
/* there are 8 modifiers, and the first 3 are shift, shift lock,
658
map_size = 8 * xmodmap->max_keypermod;
659
i = 3 * xmodmap->max_keypermod;
662
/* get the key code at this point in the map,
663
* see if its keysym is one we're interested in
665
int keycode = xmodmap->modifiermap[i];
670
EggVirtualModifierType mask;
676
gdk_keymap_get_entries_for_keycode (keymap,
678
&keys, &keyvals, &n_entries);
682
while (j < n_entries)
684
if (keyvals[j] == GDK_Num_Lock)
685
mask |= EGG_VIRTUAL_NUM_LOCK_MASK;
686
else if (keyvals[j] == GDK_Scroll_Lock)
687
mask |= EGG_VIRTUAL_SCROLL_LOCK_MASK;
688
else if (keyvals[j] == GDK_Meta_L ||
689
keyvals[j] == GDK_Meta_R)
690
mask |= EGG_VIRTUAL_META_MASK;
691
else if (keyvals[j] == GDK_Hyper_L ||
692
keyvals[j] == GDK_Hyper_R)
693
mask |= EGG_VIRTUAL_HYPER_MASK;
694
else if (keyvals[j] == GDK_Super_L ||
695
keyvals[j] == GDK_Super_R)
696
mask |= EGG_VIRTUAL_SUPER_MASK;
697
else if (keyvals[j] == GDK_Mode_switch)
698
mask |= EGG_VIRTUAL_MODE_SWITCH_MASK;
703
/* Mod1Mask is 1 << 3 for example, i.e. the
704
* fourth modifier, i / keyspermod is the modifier
707
modmap->mapping[i/xmodmap->max_keypermod] |= mask;
715
/* Add in the not-really-virtual fixed entries */
716
modmap->mapping[EGG_MODMAP_ENTRY_SHIFT] |= EGG_VIRTUAL_SHIFT_MASK;
717
modmap->mapping[EGG_MODMAP_ENTRY_CONTROL] |= EGG_VIRTUAL_CONTROL_MASK;
718
modmap->mapping[EGG_MODMAP_ENTRY_LOCK] |= EGG_VIRTUAL_LOCK_MASK;
719
modmap->mapping[EGG_MODMAP_ENTRY_MOD1] |= EGG_VIRTUAL_ALT_MASK;
720
modmap->mapping[EGG_MODMAP_ENTRY_MOD2] |= EGG_VIRTUAL_MOD2_MASK;
721
modmap->mapping[EGG_MODMAP_ENTRY_MOD3] |= EGG_VIRTUAL_MOD3_MASK;
722
modmap->mapping[EGG_MODMAP_ENTRY_MOD4] |= EGG_VIRTUAL_MOD4_MASK;
723
modmap->mapping[EGG_MODMAP_ENTRY_MOD5] |= EGG_VIRTUAL_MOD5_MASK;
725
XFreeModifiermap (xmodmap);
729
egg_keymap_get_modmap (GdkKeymap *keymap)
733
/* This is all a hack, much simpler when we can just
734
* modify GDK directly.
737
modmap = g_object_get_data (G_OBJECT (keymap),
742
modmap = g_new0 (EggModmap, 1);
744
/* FIXME modify keymap change events with an event filter
745
* and force a reload if we get one
748
reload_modmap (keymap, modmap);
750
g_object_set_data_full (G_OBJECT (keymap),
756
g_assert (modmap != NULL);