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_keycode (const gchar *string)
183
return ((string[0] == '0') &&
188
* egg_accelerator_parse_virtual:
189
* @accelerator: string representing an accelerator
190
* @accelerator_key: return location for accelerator keyval
191
* @accelerator_mods: return location for accelerator modifier mask
193
* Parses a string representing a virtual accelerator. The format
194
* looks like "<Control>a" or "<Shift><Alt>F1" or
195
* "<Release>z" (the last one is for key release). The parser
196
* is fairly liberal and allows lower or upper case, and also
197
* abbreviations such as "<Ctl>" and "<Ctrl>".
199
* If the parse fails, @accelerator_key and @accelerator_mods will
200
* be set to 0 (zero) and %FALSE will be returned. If the string contains
201
* only modifiers, @accelerator_key will be set to 0 but %TRUE will be
204
* The virtual vs. concrete accelerator distinction is a relic of
205
* how the X Window System works; there are modifiers Mod2-Mod5 that
206
* can represent various keyboard keys (numlock, meta, hyper, etc.),
207
* the virtual modifier represents the keyboard key, the concrete
208
* modifier the actual Mod2-Mod5 bits in the key press event.
210
* Returns: %TRUE on success.
213
egg_accelerator_parse_virtual (const gchar *accelerator,
214
guint *accelerator_key,
216
EggVirtualModifierType *accelerator_mods)
219
GdkModifierType mods;
224
*accelerator_key = 0;
225
if (accelerator_mods)
226
*accelerator_mods = 0;
230
g_return_val_if_fail (accelerator != NULL, FALSE);
236
len = strlen (accelerator);
239
if (*accelerator == '<')
241
if (len >= 9 && is_release (accelerator))
245
mods |= EGG_VIRTUAL_RELEASE_MASK;
247
else if (len >= 9 && is_control (accelerator))
251
mods |= EGG_VIRTUAL_CONTROL_MASK;
253
else if (len >= 7 && is_shift (accelerator))
257
mods |= EGG_VIRTUAL_SHIFT_MASK;
259
else if (len >= 6 && is_shft (accelerator))
263
mods |= EGG_VIRTUAL_SHIFT_MASK;
265
else if (len >= 6 && is_ctrl (accelerator))
269
mods |= EGG_VIRTUAL_CONTROL_MASK;
271
else if (len >= 6 && is_modx (accelerator))
273
static const guint mod_vals[] = {
274
EGG_VIRTUAL_ALT_MASK, EGG_VIRTUAL_MOD2_MASK, EGG_VIRTUAL_MOD3_MASK,
275
EGG_VIRTUAL_MOD4_MASK, EGG_VIRTUAL_MOD5_MASK
280
mods |= mod_vals[*accelerator - '1'];
283
else if (len >= 5 && is_ctl (accelerator))
287
mods |= EGG_VIRTUAL_CONTROL_MASK;
289
else if (len >= 5 && is_alt (accelerator))
293
mods |= EGG_VIRTUAL_ALT_MASK;
295
else if (len >= 6 && is_meta (accelerator))
299
mods |= EGG_VIRTUAL_META_MASK;
301
else if (len >= 7 && is_hyper (accelerator))
305
mods |= EGG_VIRTUAL_HYPER_MASK;
307
else if (len >= 7 && is_super (accelerator))
311
mods |= EGG_VIRTUAL_SUPER_MASK;
317
last_ch = *accelerator;
318
while (last_ch && last_ch != '>')
320
last_ch = *accelerator;
328
keyval = gdk_keyval_from_name (accelerator);
332
/* If keyval is 0, then maybe it's a keycode. Check for 0x## */
333
if (len >= 4 && is_keycode (accelerator))
339
memcpy (keystring, accelerator, 4);
340
keystring [4] = '\000';
342
tmp_keycode = strtol (keystring, &endptr, 16);
344
if (endptr == NULL || *endptr != '\000')
348
else if (keycode != NULL)
350
*keycode = tmp_keycode;
351
/* 0x00 is an invalid keycode too. */
357
else if (keycode != NULL)
359
*keycode = XKeysymToKeycode (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), keyval);
370
*accelerator_key = gdk_keyval_to_lower (keyval);
371
if (accelerator_mods)
372
*accelerator_mods = mods;
378
* egg_virtual_accelerator_name:
379
* @accelerator_key: accelerator keyval
380
* @accelerator_mods: accelerator modifier mask
381
* @returns: a newly-allocated accelerator name
383
* Converts an accelerator keyval and modifier mask
384
* into a string parseable by egg_accelerator_parse_virtual().
385
* For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
386
* this function returns "<Control>q".
388
* The caller of this function must free the returned string.
391
egg_virtual_accelerator_name (guint accelerator_key,
393
EggVirtualModifierType accelerator_mods)
396
GdkModifierType gdkmods = 0;
398
egg_keymap_resolve_virtual_modifiers (NULL, accelerator_mods, &gdkmods);
399
gtk_name = gtk_accelerator_name (accelerator_key, gdkmods);
401
if (!accelerator_key)
404
name = g_strdup_printf ("%s0x%02x", gtk_name, keycode);
413
* egg_virtual_accelerator_label:
414
* @accelerator_key: accelerator keyval
415
* @accelerator_mods: accelerator modifier mask
416
* @returns: a newly-allocated accelerator label
418
* Converts an accelerator keyval and modifier mask
419
* into a (possibly translated) string that can be displayed to
421
* For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
422
* and you use a German locale, this function returns "Strg+Q".
424
* The caller of this function must free the returned string.
427
egg_virtual_accelerator_label (guint accelerator_key,
429
EggVirtualModifierType accelerator_mods)
432
GdkModifierType gdkmods = 0;
434
egg_keymap_resolve_virtual_modifiers (NULL, accelerator_mods, &gdkmods);
435
gtk_label = gtk_accelerator_get_label (accelerator_key, gdkmods);
437
if (!accelerator_key)
440
label = g_strdup_printf ("%s0x%02x", gtk_label, keycode);
449
egg_keymap_resolve_virtual_modifiers (GdkKeymap *keymap,
450
EggVirtualModifierType virtual_mods,
451
GdkModifierType *concrete_mods)
453
GdkModifierType concrete;
455
const EggModmap *modmap;
457
g_return_if_fail (concrete_mods != NULL);
458
g_return_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap));
460
modmap = egg_keymap_get_modmap (keymap);
462
/* Not so sure about this algorithm. */
465
for (i = 0; i < EGG_MODMAP_ENTRY_LAST; ++i)
467
if (modmap->mapping[i] & virtual_mods)
468
concrete |= MODMAP_ENTRY_TO_MODIFIER (i);
471
*concrete_mods = concrete;
475
egg_keymap_virtualize_modifiers (GdkKeymap *keymap,
476
GdkModifierType concrete_mods,
477
EggVirtualModifierType *virtual_mods)
479
GdkModifierType virtual;
481
const EggModmap *modmap;
483
g_return_if_fail (virtual_mods != NULL);
484
g_return_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap));
486
modmap = egg_keymap_get_modmap (keymap);
488
/* Not so sure about this algorithm. */
491
for (i = 0; i < EGG_MODMAP_ENTRY_LAST; ++i)
493
if (MODMAP_ENTRY_TO_MODIFIER (i) & concrete_mods)
495
EggVirtualModifierType cleaned;
497
cleaned = modmap->mapping[i] & ~(EGG_VIRTUAL_MOD2_MASK |
498
EGG_VIRTUAL_MOD3_MASK |
499
EGG_VIRTUAL_MOD4_MASK |
500
EGG_VIRTUAL_MOD5_MASK);
508
/* Rather than dropping mod2->mod5 if not bound,
509
* go ahead and use the concrete names
511
virtual |= modmap->mapping[i];
516
*virtual_mods = virtual;
520
reload_modmap (GdkKeymap *keymap,
523
XModifierKeymap *xmodmap;
527
/* FIXME multihead */
528
xmodmap = XGetModifierMapping (gdk_x11_get_default_xdisplay ());
530
memset (modmap->mapping, 0, sizeof (modmap->mapping));
532
/* there are 8 modifiers in the order shift, shift lock,
533
* control, mod1-5 with up to max_keypermod bindings each
535
map_size = 8 * xmodmap->max_keypermod;
536
for (i = 3 * xmodmap->max_keypermod; i < map_size; ++i)
538
/* get the key code at this point in the map,
539
* see if its keysym is one we're interested in
541
int keycode = xmodmap->modifiermap[i];
546
EggVirtualModifierType mask;
552
gdk_keymap_get_entries_for_keycode (keymap,
554
&keys, &keyvals, &n_entries);
557
for (j = 0; j < n_entries; ++j)
559
if (keyvals[j] == GDK_KEY_Num_Lock)
560
mask |= EGG_VIRTUAL_NUM_LOCK_MASK;
561
else if (keyvals[j] == GDK_KEY_Scroll_Lock)
562
mask |= EGG_VIRTUAL_SCROLL_LOCK_MASK;
563
else if (keyvals[j] == GDK_KEY_Meta_L ||
564
keyvals[j] == GDK_KEY_Meta_R)
565
mask |= EGG_VIRTUAL_META_MASK;
566
else if (keyvals[j] == GDK_KEY_Hyper_L ||
567
keyvals[j] == GDK_KEY_Hyper_R)
568
mask |= EGG_VIRTUAL_HYPER_MASK;
569
else if (keyvals[j] == GDK_KEY_Super_L ||
570
keyvals[j] == GDK_KEY_Super_R)
571
mask |= EGG_VIRTUAL_SUPER_MASK;
572
else if (keyvals[j] == GDK_KEY_Mode_switch)
573
mask |= EGG_VIRTUAL_MODE_SWITCH_MASK;
576
/* Mod1Mask is 1 << 3 for example, i.e. the
577
* fourth modifier, i / keyspermod is the modifier
580
modmap->mapping[i/xmodmap->max_keypermod] |= mask;
586
/* Add in the not-really-virtual fixed entries */
587
modmap->mapping[EGG_MODMAP_ENTRY_SHIFT] |= EGG_VIRTUAL_SHIFT_MASK;
588
modmap->mapping[EGG_MODMAP_ENTRY_CONTROL] |= EGG_VIRTUAL_CONTROL_MASK;
589
modmap->mapping[EGG_MODMAP_ENTRY_LOCK] |= EGG_VIRTUAL_LOCK_MASK;
590
modmap->mapping[EGG_MODMAP_ENTRY_MOD1] |= EGG_VIRTUAL_ALT_MASK;
591
modmap->mapping[EGG_MODMAP_ENTRY_MOD2] |= EGG_VIRTUAL_MOD2_MASK;
592
modmap->mapping[EGG_MODMAP_ENTRY_MOD3] |= EGG_VIRTUAL_MOD3_MASK;
593
modmap->mapping[EGG_MODMAP_ENTRY_MOD4] |= EGG_VIRTUAL_MOD4_MASK;
594
modmap->mapping[EGG_MODMAP_ENTRY_MOD5] |= EGG_VIRTUAL_MOD5_MASK;
596
XFreeModifiermap (xmodmap);
600
egg_keymap_get_modmap (GdkKeymap *keymap)
605
keymap = gdk_keymap_get_default ();
607
/* This is all a hack, much simpler when we can just
608
* modify GDK directly.
611
modmap = g_object_get_data (G_OBJECT (keymap), "egg-modmap");
615
modmap = g_new0 (EggModmap, 1);
617
/* FIXME modify keymap change events with an event filter
618
* and force a reload if we get one
621
reload_modmap (keymap, modmap);
623
g_object_set_data_full (G_OBJECT (keymap),
629
g_assert (modmap != NULL);