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);
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') &&
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') &&
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') &&
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') &&
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') &&
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') &&
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') &&
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') &&
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') &&
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') &&
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') &&
179
* Parses a string representing a virtual accelerator. The format
180
* looks like "<Control>a" or "<Shift><Alt>F1" or
181
* "<Release>z" (the last one is for key release). The parser
182
* is fairly liberal and allows lower or upper case, and also
183
* abbreviations such as "<Ctl>" and "<Ctrl>".
185
* If the parse fails, @accelerator_key and @accelerator_mods will
186
* be set to 0 (zero) and %FALSE will be returned. If the string contains
187
* only modifiers, @accelerator_key will be set to 0 but %TRUE will be
190
* The virtual vs. concrete accelerator distinction is a relic of
191
* how the X Window System works; there are modifiers Mod2-Mod5 that
192
* can represent various keyboard keys (numlock, meta, hyper, etc.),
193
* the virtual modifier represents the keyboard key, the concrete
194
* modifier the actual Mod2-Mod5 bits in the key press event.
195
* @param accelerator: string representing an accelerator
196
* @param accelerator_key: return location for accelerator keyval
197
* @param accelerator_mods: return location for accelerator modifier mask
198
* @Returns: %TRUE on success.
201
egg_accelerator_parse_virtual (const gchar *accelerator,
202
guint *accelerator_key,
203
EggVirtualModifierType *accelerator_mods)
206
GdkModifierType mods;
211
*accelerator_key = 0;
212
if (accelerator_mods)
213
*accelerator_mods = 0;
215
g_return_val_if_fail (accelerator != NULL, FALSE);
221
len = strlen (accelerator);
224
if (*accelerator == '<')
226
if (len >= 9 && is_release (accelerator))
230
mods |= EGG_VIRTUAL_RELEASE_MASK;
232
else if (len >= 9 && is_control (accelerator))
236
mods |= EGG_VIRTUAL_CONTROL_MASK;
238
else if (len >= 7 && is_shift (accelerator))
242
mods |= EGG_VIRTUAL_SHIFT_MASK;
244
else if (len >= 6 && is_shft (accelerator))
248
mods |= EGG_VIRTUAL_SHIFT_MASK;
250
else if (len >= 6 && is_ctrl (accelerator))
254
mods |= EGG_VIRTUAL_CONTROL_MASK;
256
else if (len >= 6 && is_modx (accelerator))
258
static const guint mod_vals[] = {
259
EGG_VIRTUAL_ALT_MASK, EGG_VIRTUAL_MOD2_MASK, EGG_VIRTUAL_MOD3_MASK,
260
EGG_VIRTUAL_MOD4_MASK, EGG_VIRTUAL_MOD5_MASK
265
mods |= mod_vals[*accelerator - '1'];
268
else if (len >= 5 && is_ctl (accelerator))
272
mods |= EGG_VIRTUAL_CONTROL_MASK;
274
else if (len >= 5 && is_alt (accelerator))
278
mods |= EGG_VIRTUAL_ALT_MASK;
280
else if (len >= 6 && is_meta (accelerator))
284
mods |= EGG_VIRTUAL_META_MASK;
286
else if (len >= 7 && is_hyper (accelerator))
290
mods |= EGG_VIRTUAL_HYPER_MASK;
292
else if (len >= 7 && is_super (accelerator))
296
mods |= EGG_VIRTUAL_SUPER_MASK;
302
last_ch = *accelerator;
303
while (last_ch && last_ch != '>')
305
last_ch = *accelerator;
313
keyval = gdk_keyval_from_name (accelerator);
324
*accelerator_key = gdk_keyval_to_lower (keyval);
325
if (accelerator_mods)
326
*accelerator_mods = mods;
333
* Converts an accelerator keyval and modifier mask
334
* into a string parseable by egg_accelerator_parse_virtual().
335
* For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
336
* this function returns "<Control>q".
338
* The caller of this function must free the returned string.
339
* @param accelerator_key: accelerator keyval
340
* @param accelerator_mods: accelerator modifier mask
341
* @returns: a newly-allocated accelerator name
344
egg_virtual_accelerator_name (guint accelerator_key,
345
EggVirtualModifierType accelerator_mods)
347
static const gchar text_release[] = "<Release>";
348
static const gchar text_shift[] = "<Shift>";
349
static const gchar text_control[] = "<Control>";
350
static const gchar text_mod1[] = "<Alt>";
351
static const gchar text_mod2[] = "<Mod2>";
352
static const gchar text_mod3[] = "<Mod3>";
353
static const gchar text_mod4[] = "<Mod4>";
354
static const gchar text_mod5[] = "<Mod5>";
355
static const gchar text_meta[] = "<Meta>";
356
static const gchar text_super[] = "<Super>";
357
static const gchar text_hyper[] = "<Hyper>";
362
accelerator_mods &= EGG_VIRTUAL_MODIFIER_MASK;
364
keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
369
if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
370
l += sizeof (text_release) - 1;
371
if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
372
l += sizeof (text_shift) - 1;
373
if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
374
l += sizeof (text_control) - 1;
375
if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
376
l += sizeof (text_mod1) - 1;
377
if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
378
l += sizeof (text_mod2) - 1;
379
if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
380
l += sizeof (text_mod3) - 1;
381
if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
382
l += sizeof (text_mod4) - 1;
383
if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
384
l += sizeof (text_mod5) - 1;
385
if (accelerator_mods & EGG_VIRTUAL_META_MASK)
386
l += sizeof (text_meta) - 1;
387
if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
388
l += sizeof (text_hyper) - 1;
389
if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
390
l += sizeof (text_super) - 1;
391
l += strlen (keyval_name);
393
accelerator = g_new (gchar, l + 1);
397
if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
399
strcpy (accelerator + l, text_release);
400
l += sizeof (text_release) - 1;
402
if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
404
strcpy (accelerator + l, text_shift);
405
l += sizeof (text_shift) - 1;
407
if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
409
strcpy (accelerator + l, text_control);
410
l += sizeof (text_control) - 1;
412
if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
414
strcpy (accelerator + l, text_mod1);
415
l += sizeof (text_mod1) - 1;
417
if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
419
strcpy (accelerator + l, text_mod2);
420
l += sizeof (text_mod2) - 1;
422
if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
424
strcpy (accelerator + l, text_mod3);
425
l += sizeof (text_mod3) - 1;
427
if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
429
strcpy (accelerator + l, text_mod4);
430
l += sizeof (text_mod4) - 1;
432
if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
434
strcpy (accelerator + l, text_mod5);
435
l += sizeof (text_mod5) - 1;
437
if (accelerator_mods & EGG_VIRTUAL_META_MASK)
439
strcpy (accelerator + l, text_meta);
440
l += sizeof (text_meta) - 1;
442
if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
444
strcpy (accelerator + l, text_hyper);
445
l += sizeof (text_hyper) - 1;
447
if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
449
strcpy (accelerator + l, text_super);
450
l += sizeof (text_super) - 1;
453
strcpy (accelerator + l, keyval_name);
459
egg_keymap_resolve_virtual_modifiers (GdkKeymap *keymap,
460
EggVirtualModifierType virtual_mods,
461
GdkModifierType *concrete_mods)
463
GdkModifierType concrete;
465
const EggModmap *modmap;
467
g_return_if_fail (GDK_IS_KEYMAP (keymap));
468
g_return_if_fail (concrete_mods != NULL);
470
modmap = egg_keymap_get_modmap (keymap);
472
/* Not so sure about this algorithm. */
476
while (i < EGG_MODMAP_ENTRY_LAST)
478
if (modmap->mapping[i] & virtual_mods)
479
concrete |= (1 << i);
484
*concrete_mods = concrete;
488
egg_keymap_virtualize_modifiers (GdkKeymap *keymap,
489
GdkModifierType concrete_mods,
490
EggVirtualModifierType *virtual_mods)
492
GdkModifierType virtual;
494
const EggModmap *modmap;
496
g_return_if_fail (GDK_IS_KEYMAP (keymap));
497
g_return_if_fail (virtual_mods != NULL);
499
modmap = egg_keymap_get_modmap (keymap);
501
/* Not so sure about this algorithm. */
505
while (i < EGG_MODMAP_ENTRY_LAST)
507
if ((1 << i) & concrete_mods)
509
EggVirtualModifierType cleaned;
511
cleaned = modmap->mapping[i] & ~(EGG_VIRTUAL_MOD2_MASK |
512
EGG_VIRTUAL_MOD3_MASK |
513
EGG_VIRTUAL_MOD4_MASK |
514
EGG_VIRTUAL_MOD5_MASK);
522
/* Rather than dropping mod2->mod5 if not bound,
523
* go ahead and use the concrete names
525
virtual |= modmap->mapping[i];
532
*virtual_mods = virtual;
536
reload_modmap (GdkKeymap *keymap,
539
XModifierKeymap *xmodmap;
543
/* FIXME multihead */
544
xmodmap = XGetModifierMapping (gdk_x11_get_default_xdisplay ());
546
memset (modmap->mapping, 0, sizeof (modmap->mapping));
548
/* there are 8 modifiers, and the first 3 are shift, shift lock,
551
map_size = 8 * xmodmap->max_keypermod;
552
i = 3 * xmodmap->max_keypermod;
555
/* get the key code at this point in the map,
556
* see if its keysym is one we're interested in
558
int keycode = xmodmap->modifiermap[i];
563
EggVirtualModifierType mask;
569
gdk_keymap_get_entries_for_keycode (keymap,
571
&keys, &keyvals, &n_entries);
575
while (j < n_entries)
577
if (keyvals[j] == GDK_Num_Lock)
578
mask |= EGG_VIRTUAL_NUM_LOCK_MASK;
579
else if (keyvals[j] == GDK_Scroll_Lock)
580
mask |= EGG_VIRTUAL_SCROLL_LOCK_MASK;
581
else if (keyvals[j] == GDK_Meta_L ||
582
keyvals[j] == GDK_Meta_R)
583
mask |= EGG_VIRTUAL_META_MASK;
584
else if (keyvals[j] == GDK_Hyper_L ||
585
keyvals[j] == GDK_Hyper_R)
586
mask |= EGG_VIRTUAL_HYPER_MASK;
587
else if (keyvals[j] == GDK_Super_L ||
588
keyvals[j] == GDK_Super_R)
589
mask |= EGG_VIRTUAL_SUPER_MASK;
590
else if (keyvals[j] == GDK_Mode_switch)
591
mask |= EGG_VIRTUAL_MODE_SWITCH_MASK;
596
/* Mod1Mask is 1 << 3 for example, i.e. the
597
* fourth modifier, i / keyspermod is the modifier
600
modmap->mapping[i/xmodmap->max_keypermod] |= mask;
608
/* Add in the not-really-virtual fixed entries */
609
modmap->mapping[EGG_MODMAP_ENTRY_SHIFT] |= EGG_VIRTUAL_SHIFT_MASK;
610
modmap->mapping[EGG_MODMAP_ENTRY_CONTROL] |= EGG_VIRTUAL_CONTROL_MASK;
611
modmap->mapping[EGG_MODMAP_ENTRY_LOCK] |= EGG_VIRTUAL_LOCK_MASK;
612
modmap->mapping[EGG_MODMAP_ENTRY_MOD1] |= EGG_VIRTUAL_ALT_MASK;
613
modmap->mapping[EGG_MODMAP_ENTRY_MOD2] |= EGG_VIRTUAL_MOD2_MASK;
614
modmap->mapping[EGG_MODMAP_ENTRY_MOD3] |= EGG_VIRTUAL_MOD3_MASK;
615
modmap->mapping[EGG_MODMAP_ENTRY_MOD4] |= EGG_VIRTUAL_MOD4_MASK;
616
modmap->mapping[EGG_MODMAP_ENTRY_MOD5] |= EGG_VIRTUAL_MOD5_MASK;
618
XFreeModifiermap (xmodmap);
622
egg_keymap_get_modmap (GdkKeymap *keymap)
626
/* This is all a hack, much simpler when we can just
627
* modify GDK directly.
630
modmap = g_object_get_data (G_OBJECT (keymap),
635
modmap = g_new0 (EggModmap, 1);
637
/* FIXME modify keymap change events with an event filter
638
* and force a reload if we get one
641
reload_modmap (keymap, modmap);
643
g_object_set_data_full (G_OBJECT (keymap),
649
g_assert (modmap != NULL);
656
int *egg_keystring_to_keysyms (gchar *accelerator, int *iNbKeys)
658
int i = 0, iNbKeyMax = 10; // on limite a 10, c'est bien assez.
659
gint *pKeySyms = g_new0 (int, iNbKeyMax);
663
GdkModifierType mods;
671
len = strlen (accelerator);
675
if (*accelerator == '<')
677
if (len >= 9 && is_release (accelerator))
681
cKeyName = "Release";
683
else if (len >= 9 && is_control (accelerator))
687
cKeyName = "Control_L";
689
else if (len >= 7 && is_shift (accelerator))
693
cKeyName = "Shift_L";
695
else if (len >= 6 && is_shft (accelerator))
699
cKeyName = "Shift_L";
701
else if (len >= 6 && is_ctrl (accelerator))
705
cKeyName = "Control_L";
707
else if (len >= 6 && is_modx (accelerator))
714
else if (len >= 5 && is_ctl (accelerator))
718
cKeyName = "Control_L";
720
else if (len >= 5 && is_alt (accelerator))
726
else if (len >= 6 && is_meta (accelerator))
732
else if (len >= 7 && is_hyper (accelerator))
736
cKeyName = "Hyper_L"; /// ?
738
else if (len >= 7 && is_super (accelerator))
742
cKeyName = "Super_L";
748
last_ch = *accelerator;
749
while (last_ch && last_ch != '>')
751
last_ch = *accelerator;
757
else // c'est une simple touche.
759
cKeyName = accelerator;
765
if (cKeyName != NULL)
767
//g_print ("cKeyName : %s\n", cKeyName);
768
int keysym = XStringToKeysym (cKeyName);
769
if (keysym == NoSymbol)
771
fprintf (stderr, "no such key name '%s'", cKeyName);
775
pKeySyms[i++] = keysym;