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>
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
static inline gboolean
181
is_primary (const gchar *string)
183
return ((string[0] == '<') &&
184
(string[1] == 'p' || string[1] == 'P') &&
185
(string[2] == 'r' || string[2] == 'R') &&
186
(string[3] == 'i' || string[3] == 'I') &&
187
(string[4] == 'm' || string[4] == 'M') &&
188
(string[5] == 'a' || string[5] == 'A') &&
189
(string[6] == 'r' || string[6] == 'R') &&
190
(string[7] == 'y' || string[7] == 'Y') &&
194
static inline gboolean
195
is_keycode (const gchar *string)
197
return ((string[0] == '0') &&
202
* egg_accelerator_parse_virtual:
203
* @accelerator: string representing an accelerator
204
* @accelerator_key: return location for accelerator keyval
205
* @accelerator_mods: return location for accelerator modifier mask
207
* Parses a string representing a virtual accelerator. The format
208
* looks like "<Control>a" or "<Shift><Alt>F1" or
209
* "<Release>z" (the last one is for key release). The parser
210
* is fairly liberal and allows lower or upper case, and also
211
* abbreviations such as "<Ctl>" and "<Ctrl>".
213
* If the parse fails, @accelerator_key and @accelerator_mods will
214
* be set to 0 (zero) and %FALSE will be returned. If the string contains
215
* only modifiers, @accelerator_key will be set to 0 but %TRUE will be
218
* The virtual vs. concrete accelerator distinction is a relic of
219
* how the X Window System works; there are modifiers Mod2-Mod5 that
220
* can represent various keyboard keys (numlock, meta, hyper, etc.),
221
* the virtual modifier represents the keyboard key, the concrete
222
* modifier the actual Mod2-Mod5 bits in the key press event.
224
* Returns: %TRUE on success.
227
egg_accelerator_parse_virtual (const gchar *accelerator,
228
guint *accelerator_key,
230
EggVirtualModifierType *accelerator_mods)
233
GdkModifierType mods;
238
*accelerator_key = 0;
239
if (accelerator_mods)
240
*accelerator_mods = 0;
244
g_return_val_if_fail (accelerator != NULL, FALSE);
250
len = strlen (accelerator);
253
if (*accelerator == '<')
255
if (len >= 9 && is_release (accelerator))
259
mods |= EGG_VIRTUAL_RELEASE_MASK;
261
else if (len >= 9 && is_control (accelerator))
265
mods |= EGG_VIRTUAL_CONTROL_MASK;
267
else if (len >= 7 && is_shift (accelerator))
271
mods |= EGG_VIRTUAL_SHIFT_MASK;
273
else if (len >= 6 && is_shft (accelerator))
277
mods |= EGG_VIRTUAL_SHIFT_MASK;
279
else if (len >= 6 && is_ctrl (accelerator))
283
mods |= EGG_VIRTUAL_CONTROL_MASK;
285
else if (len >= 6 && is_modx (accelerator))
287
static const guint mod_vals[] = {
288
EGG_VIRTUAL_ALT_MASK, EGG_VIRTUAL_MOD2_MASK, EGG_VIRTUAL_MOD3_MASK,
289
EGG_VIRTUAL_MOD4_MASK, EGG_VIRTUAL_MOD5_MASK
294
mods |= mod_vals[*accelerator - '1'];
297
else if (len >= 5 && is_ctl (accelerator))
301
mods |= EGG_VIRTUAL_CONTROL_MASK;
303
else if (len >= 5 && is_alt (accelerator))
307
mods |= EGG_VIRTUAL_ALT_MASK;
309
else if (len >= 6 && is_meta (accelerator))
313
mods |= EGG_VIRTUAL_META_MASK;
315
else if (len >= 7 && is_hyper (accelerator))
319
mods |= EGG_VIRTUAL_HYPER_MASK;
321
else if (len >= 7 && is_super (accelerator))
325
mods |= EGG_VIRTUAL_SUPER_MASK;
327
else if (len >= 9 && is_primary (accelerator))
331
mods |= EGG_VIRTUAL_CONTROL_MASK;
337
last_ch = *accelerator;
338
while (last_ch && last_ch != '>')
340
last_ch = *accelerator;
348
keyval = gdk_keyval_from_name (accelerator);
352
/* If keyval is 0, then maybe it's a keycode. Check for 0x## */
353
if (len >= 4 && is_keycode (accelerator))
359
memcpy (keystring, accelerator, 4);
360
keystring [4] = '\000';
362
tmp_keycode = strtol (keystring, &endptr, 16);
364
if (endptr == NULL || *endptr != '\000')
368
else if (keycode != NULL)
370
*keycode = tmp_keycode;
371
/* 0x00 is an invalid keycode too. */
377
else if (keycode != NULL)
379
*keycode = XKeysymToKeycode (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), keyval);
390
*accelerator_key = gdk_keyval_to_lower (keyval);
391
if (accelerator_mods)
392
*accelerator_mods = mods;
398
* egg_virtual_accelerator_name:
399
* @accelerator_key: accelerator keyval
400
* @accelerator_mods: accelerator modifier mask
401
* @returns: a newly-allocated accelerator name
403
* Converts an accelerator keyval and modifier mask
404
* into a string parseable by egg_accelerator_parse_virtual().
405
* For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
406
* this function returns "<Control>q".
408
* The caller of this function must free the returned string.
411
egg_virtual_accelerator_name (guint accelerator_key,
413
EggVirtualModifierType accelerator_mods)
416
GdkModifierType gdkmods = 0;
418
egg_keymap_resolve_virtual_modifiers (NULL, accelerator_mods, &gdkmods);
419
gtk_name = gtk_accelerator_name (accelerator_key, gdkmods);
421
if (!accelerator_key)
424
name = g_strdup_printf ("%s0x%02x", gtk_name, keycode);
433
* egg_virtual_accelerator_label:
434
* @accelerator_key: accelerator keyval
435
* @accelerator_mods: accelerator modifier mask
436
* @returns: a newly-allocated accelerator label
438
* Converts an accelerator keyval and modifier mask
439
* into a (possibly translated) string that can be displayed to
441
* For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
442
* and you use a German locale, this function returns "Strg+Q".
444
* The caller of this function must free the returned string.
447
egg_virtual_accelerator_label (guint accelerator_key,
449
EggVirtualModifierType accelerator_mods)
452
GdkModifierType gdkmods = 0;
454
egg_keymap_resolve_virtual_modifiers (NULL, accelerator_mods, &gdkmods);
455
gtk_label = gtk_accelerator_get_label (accelerator_key, gdkmods);
457
if (!accelerator_key)
460
label = g_strdup_printf ("%s0x%02x", gtk_label, keycode);
469
egg_keymap_resolve_virtual_modifiers (GdkKeymap *keymap,
470
EggVirtualModifierType virtual_mods,
471
GdkModifierType *concrete_mods)
473
GdkModifierType concrete;
475
const EggModmap *modmap;
477
g_return_if_fail (concrete_mods != NULL);
478
g_return_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap));
480
modmap = egg_keymap_get_modmap (keymap);
482
/* Not so sure about this algorithm. */
485
for (i = 0; i < EGG_MODMAP_ENTRY_LAST; ++i)
487
if (modmap->mapping[i] & virtual_mods)
488
concrete |= MODMAP_ENTRY_TO_MODIFIER (i);
491
*concrete_mods = concrete;
495
egg_keymap_virtualize_modifiers (GdkKeymap *keymap,
496
GdkModifierType concrete_mods,
497
EggVirtualModifierType *virtual_mods)
499
GdkModifierType virtual;
501
const EggModmap *modmap;
503
g_return_if_fail (virtual_mods != NULL);
504
g_return_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap));
506
modmap = egg_keymap_get_modmap (keymap);
508
/* Not so sure about this algorithm. */
511
for (i = 0; i < EGG_MODMAP_ENTRY_LAST; ++i)
513
if (MODMAP_ENTRY_TO_MODIFIER (i) & concrete_mods)
515
EggVirtualModifierType cleaned;
517
cleaned = modmap->mapping[i] & ~(EGG_VIRTUAL_MOD2_MASK |
518
EGG_VIRTUAL_MOD3_MASK |
519
EGG_VIRTUAL_MOD4_MASK |
520
EGG_VIRTUAL_MOD5_MASK);
528
/* Rather than dropping mod2->mod5 if not bound,
529
* go ahead and use the concrete names
531
virtual |= modmap->mapping[i];
536
*virtual_mods = virtual;
540
reload_modmap (GdkKeymap *keymap,
543
XModifierKeymap *xmodmap;
547
/* FIXME multihead */
548
xmodmap = XGetModifierMapping (gdk_x11_get_default_xdisplay ());
550
memset (modmap->mapping, 0, sizeof (modmap->mapping));
552
/* there are 8 modifiers in the order shift, shift lock,
553
* control, mod1-5 with up to max_keypermod bindings each
555
map_size = 8 * xmodmap->max_keypermod;
556
for (i = 3 * xmodmap->max_keypermod; i < map_size; ++i)
558
/* get the key code at this point in the map,
559
* see if its keysym is one we're interested in
561
int keycode = xmodmap->modifiermap[i];
566
EggVirtualModifierType mask;
572
gdk_keymap_get_entries_for_keycode (keymap,
574
&keys, &keyvals, &n_entries);
577
for (j = 0; j < n_entries; ++j)
579
if (keyvals[j] == GDK_KEY_Num_Lock)
580
mask |= EGG_VIRTUAL_NUM_LOCK_MASK;
581
else if (keyvals[j] == GDK_KEY_Scroll_Lock)
582
mask |= EGG_VIRTUAL_SCROLL_LOCK_MASK;
583
else if (keyvals[j] == GDK_KEY_Meta_L ||
584
keyvals[j] == GDK_KEY_Meta_R)
585
mask |= EGG_VIRTUAL_META_MASK;
586
else if (keyvals[j] == GDK_KEY_Hyper_L ||
587
keyvals[j] == GDK_KEY_Hyper_R)
588
mask |= EGG_VIRTUAL_HYPER_MASK;
589
else if (keyvals[j] == GDK_KEY_Super_L ||
590
keyvals[j] == GDK_KEY_Super_R)
591
mask |= EGG_VIRTUAL_SUPER_MASK;
592
else if (keyvals[j] == GDK_KEY_Mode_switch)
593
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;
606
/* Add in the not-really-virtual fixed entries */
607
modmap->mapping[EGG_MODMAP_ENTRY_SHIFT] |= EGG_VIRTUAL_SHIFT_MASK;
608
modmap->mapping[EGG_MODMAP_ENTRY_CONTROL] |= EGG_VIRTUAL_CONTROL_MASK;
609
modmap->mapping[EGG_MODMAP_ENTRY_LOCK] |= EGG_VIRTUAL_LOCK_MASK;
610
modmap->mapping[EGG_MODMAP_ENTRY_MOD1] |= EGG_VIRTUAL_ALT_MASK;
611
modmap->mapping[EGG_MODMAP_ENTRY_MOD2] |= EGG_VIRTUAL_MOD2_MASK;
612
modmap->mapping[EGG_MODMAP_ENTRY_MOD3] |= EGG_VIRTUAL_MOD3_MASK;
613
modmap->mapping[EGG_MODMAP_ENTRY_MOD4] |= EGG_VIRTUAL_MOD4_MASK;
614
modmap->mapping[EGG_MODMAP_ENTRY_MOD5] |= EGG_VIRTUAL_MOD5_MASK;
616
XFreeModifiermap (xmodmap);
620
egg_keymap_get_modmap (GdkKeymap *keymap)
625
keymap = gdk_keymap_get_default ();
627
/* This is all a hack, much simpler when we can just
628
* modify GDK directly.
631
modmap = g_object_get_data (G_OBJECT (keymap), "egg-modmap");
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);