1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
39 #include "gdkprivate-x11.h"
40 #include "gdkinternals.h"
41 #include "gdkdisplay-x11.h"
42 #include "gdkkeysyms.h"
46 #include <X11/XKBlib.h>
48 /* OSF-4.0 is apparently missing this macro
50 # ifndef XkbKeySymEntry
51 # define XkbKeySymEntry(d,k,sl,g) \
52 (XkbKeySym(d,k,((XkbKeyGroupsWidth(d,k)*(g))+(sl))))
56 typedef struct _GdkKeymapX11 GdkKeymapX11;
58 #define GDK_TYPE_KEYMAP_X11 (gdk_keymap_x11_get_type ())
59 #define GDK_KEYMAP_X11(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_KEYMAP_X11, GdkKeymapX11))
60 #define GDK_IS_KEYMAP_X11(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_KEYMAP_X11))
62 typedef struct _DirectionCacheEntry DirectionCacheEntry;
64 struct _DirectionCacheEntry
68 PangoDirection direction;
73 GdkKeymap parent_instance;
78 gint keysyms_per_keycode;
79 XModifierKeymap* mod_keymap;
81 GdkModifierType group_switch_mask;
82 GdkModifierType num_lock_mask;
83 GdkModifierType modmap[8];
85 PangoDirection current_direction;
86 gboolean have_direction;
91 /* We cache the directions */
92 Atom current_group_atom;
93 guint current_cache_serial;
94 /* A cache of size four should be more than enough, people usually
95 * have two groups around, and the xkb limit is four. It still
96 * works correct for more than four groups. It's just the
99 DirectionCacheEntry group_direction_cache[4];
103 #define KEYMAP_USE_XKB(keymap) GDK_DISPLAY_X11 ((keymap)->display)->use_xkb
104 #define KEYMAP_XDISPLAY(keymap) GDK_DISPLAY_XDISPLAY ((keymap)->display)
106 static GType gdk_keymap_x11_get_type (void);
107 static void gdk_keymap_x11_init (GdkKeymapX11 *keymap);
110 gdk_keymap_x11_get_type (void)
112 static GType object_type = 0;
116 static const GTypeInfo object_info =
118 sizeof (GdkKeymapClass),
119 (GBaseInitFunc) NULL,
120 (GBaseFinalizeFunc) NULL,
121 (GClassInitFunc) NULL,
122 NULL, /* class_finalize */
123 NULL, /* class_data */
124 sizeof (GdkKeymapX11),
126 (GInstanceInitFunc) gdk_keymap_x11_init,
129 object_type = g_type_register_static (GDK_TYPE_KEYMAP,
130 g_intern_static_string ("GdkKeymapX11"),
138 gdk_keymap_x11_init (GdkKeymapX11 *keymap)
140 keymap->min_keycode = 0;
141 keymap->max_keycode = 0;
143 keymap->keymap = NULL;
144 keymap->keysyms_per_keycode = 0;
145 keymap->mod_keymap = NULL;
147 keymap->num_lock_mask = 0;
148 keymap->sun_keypad = FALSE;
149 keymap->group_switch_mask = 0;
150 keymap->lock_keysym = GDK_Caps_Lock;
151 keymap->have_direction = FALSE;
152 keymap->current_serial = 0;
155 keymap->xkb_desc = NULL;
156 keymap->current_group_atom = 0;
157 keymap->current_cache_serial = 0;
163 update_keyrange (GdkKeymapX11 *keymap_x11)
165 if (keymap_x11->max_keycode == 0)
166 XDisplayKeycodes (KEYMAP_XDISPLAY (GDK_KEYMAP (keymap_x11)),
167 &keymap_x11->min_keycode, &keymap_x11->max_keycode);
173 update_modmap (Display *display,
174 GdkKeymapX11 *keymap_x11)
179 GdkModifierType mask;
181 { "Meta", 0, GDK_META_MASK },
182 { "Super", 0, GDK_SUPER_MASK },
183 { "Hyper", 0, GDK_HYPER_MASK },
190 for (i = 0; vmods[i].name; i++)
191 vmods[i].atom = XInternAtom (display, vmods[i].name, FALSE);
193 for (i = 0; i < 8; i++)
194 keymap_x11->modmap[i] = 1 << i;
196 for (i = 0; i < XkbNumVirtualMods; i++)
198 for (j = 0; vmods[j].atom; j++)
200 if (keymap_x11->xkb_desc->names->vmods[i] == vmods[j].atom)
202 for (k = 0; k < 8; k++)
204 if (keymap_x11->xkb_desc->server->vmods[i] & (1 << k))
205 keymap_x11->modmap[k] |= vmods[j].mask;
213 get_xkb (GdkKeymapX11 *keymap_x11)
215 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_KEYMAP (keymap_x11)->display);
216 Display *xdisplay = display_x11->xdisplay;
218 update_keyrange (keymap_x11);
220 if (keymap_x11->xkb_desc == NULL)
222 keymap_x11->xkb_desc = XkbGetMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask | XkbVirtualModsMask, XkbUseCoreKbd);
223 if (keymap_x11->xkb_desc == NULL)
224 g_error ("Failed to get keymap");
226 XkbGetNames (xdisplay, XkbGroupNamesMask | XkbVirtualModNamesMask, keymap_x11->xkb_desc);
228 update_modmap (xdisplay, keymap_x11);
230 else if (keymap_x11->current_serial != display_x11->keymap_serial)
232 XkbGetUpdatedMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask | XkbVirtualModsMask,
233 keymap_x11->xkb_desc);
234 XkbGetNames (xdisplay, XkbGroupNamesMask | XkbVirtualModNamesMask, keymap_x11->xkb_desc);
236 update_modmap (xdisplay, keymap_x11);
239 keymap_x11->current_serial = display_x11->keymap_serial;
241 return keymap_x11->xkb_desc;
243 #endif /* HAVE_XKB */
245 /* Whether we were able to turn on detectable-autorepeat using
246 * XkbSetDetectableAutorepeat. If FALSE, we'll fall back
247 * to checking the next event with XPending().
251 * gdk_keymap_get_for_display:
252 * @display: the #GdkDisplay.
253 * @returns: the #GdkKeymap attached to @display.
255 * Returns the #GdkKeymap attached to @display.
260 gdk_keymap_get_for_display (GdkDisplay *display)
262 GdkDisplayX11 *display_x11;
263 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
264 display_x11 = GDK_DISPLAY_X11 (display);
266 if (!display_x11->keymap)
267 display_x11->keymap = g_object_new (gdk_keymap_x11_get_type (), NULL);
269 display_x11->keymap->display = display;
271 return display_x11->keymap;
274 /* Find the index of the group/level pair within the keysyms for a key.
275 * We round up the number of keysyms per keycode to the next even number,
276 * otherwise we lose a whole group of keys
278 #define KEYSYM_INDEX(keymap_impl, group, level) \
279 (2 * ((group) % (gint)((keymap_impl->keysyms_per_keycode + 1) / 2)) + (level))
280 #define KEYSYM_IS_KEYPAD(s) (((s) >= 0xff80 && (s) <= 0xffbd) || \
281 ((s) >= 0x11000000 && (s) <= 0x1100ffff))
284 get_symbol (const KeySym *syms,
285 GdkKeymapX11 *keymap_x11,
291 index = KEYSYM_INDEX(keymap_x11, group, level);
292 if (index > keymap_x11->keysyms_per_keycode)
299 update_keymaps (GdkKeymapX11 *keymap_x11)
301 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_KEYMAP (keymap_x11)->display);
302 Display *xdisplay = display_x11->xdisplay;
305 g_assert (!KEYMAP_USE_XKB (GDK_KEYMAP (keymap_x11)));
308 if (keymap_x11->keymap == NULL ||
309 keymap_x11->current_serial != display_x11->keymap_serial)
315 keymap_x11->current_serial = display_x11->keymap_serial;
317 update_keyrange (keymap_x11);
319 if (keymap_x11->keymap)
320 XFree (keymap_x11->keymap);
322 if (keymap_x11->mod_keymap)
323 XFreeModifiermap (keymap_x11->mod_keymap);
325 keymap_x11->keymap = XGetKeyboardMapping (xdisplay, keymap_x11->min_keycode,
326 keymap_x11->max_keycode - keymap_x11->min_keycode + 1,
327 &keymap_x11->keysyms_per_keycode);
330 /* GDK_ISO_Left_Tab, as usually configured through XKB, really messes
331 * up the whole idea of "consumed modifiers" because shift is consumed.
332 * However, <shift>Tab is not usually GDK_ISO_Left_Tab without XKB,
333 * we we fudge the map here.
335 keycode = keymap_x11->min_keycode;
336 while (keycode <= keymap_x11->max_keycode)
338 KeySym *syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
339 /* Check both groups */
340 for (i = 0 ; i < 2 ; i++)
342 if (get_symbol (syms, keymap_x11, i, 0) == GDK_Tab)
343 syms[KEYSYM_INDEX (keymap_x11, i, 1)] = GDK_ISO_Left_Tab;
347 * If there is one keysym and the key symbol has upper and lower
348 * case variants fudge the keymap
350 if (get_symbol (syms, keymap_x11, 0, 1) == 0)
355 gdk_keyval_convert_case (get_symbol (syms, keymap_x11, 0, 0), &lower, &upper);
358 syms[KEYSYM_INDEX (keymap_x11, 0, 0)] = lower;
359 syms[KEYSYM_INDEX (keymap_x11, 0, 1)] = upper;
367 keymap_x11->mod_keymap = XGetModifierMapping (xdisplay);
369 keymap_x11->lock_keysym = GDK_VoidSymbol;
370 keymap_x11->group_switch_mask = 0;
371 keymap_x11->num_lock_mask = 0;
373 for (i = 0; i < 8; i++)
374 keymap_x11->modmap[i] = 1 << i;
376 /* There are 8 sets of modifiers, with each set containing
377 * max_keypermod keycodes.
379 map_size = 8 * keymap_x11->mod_keymap->max_keypermod;
380 for (i = 0; i < map_size; i++)
382 /* Get the key code at this point in the map. */
383 gint keycode = keymap_x11->mod_keymap->modifiermap[i];
388 /* Ignore invalid keycodes. */
389 if (keycode < keymap_x11->min_keycode ||
390 keycode > keymap_x11->max_keycode)
393 syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
396 for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
398 if (syms[j] == GDK_Meta_L ||
399 syms[j] == GDK_Meta_R)
400 mask |= GDK_META_MASK;
401 else if (syms[j] == GDK_Hyper_L ||
402 syms[j] == GDK_Hyper_R)
403 mask |= GDK_HYPER_MASK;
404 else if (syms[j] == GDK_Super_L ||
405 syms[j] == GDK_Super_R)
406 mask |= GDK_SUPER_MASK;
409 keymap_x11->modmap[i/keymap_x11->mod_keymap->max_keypermod] |= mask;
411 /* The fourth modifier, GDK_MOD1_MASK is 1 << 3.
412 * Each group of max_keypermod entries refers to the same modifier.
414 mask = 1 << (i / keymap_x11->mod_keymap->max_keypermod);
419 /* Get the Lock keysym. If any keysym bound to the Lock modifier
420 * is Caps_Lock, we will interpret the modifier as Caps_Lock;
421 * otherwise, if any is bound to Shift_Lock, we will interpret
422 * the modifier as Shift_Lock. Otherwise, the lock modifier
425 for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
427 if (syms[j] == GDK_Caps_Lock)
428 keymap_x11->lock_keysym = GDK_Caps_Lock;
429 else if (syms[j] == GDK_Shift_Lock &&
430 keymap_x11->lock_keysym == GDK_VoidSymbol)
431 keymap_x11->lock_keysym = GDK_Shift_Lock;
435 case GDK_CONTROL_MASK:
438 /* Some keyboard maps are known to map Mode_Switch as an
439 * extra Mod1 key. In circumstances like that, it won't be
440 * used to switch groups.
445 /* Find the Mode_Switch and Num_Lock modifiers. */
446 for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
448 if (syms[j] == GDK_Mode_switch)
450 /* This modifier swaps groups */
451 keymap_x11->group_switch_mask |= mask;
453 else if (syms[j] == GDK_Num_Lock)
455 /* This modifier is used for Num_Lock */
456 keymap_x11->num_lock_mask |= mask;
463 /* Hack: The Sun X server puts the keysym to use when the Num Lock
464 * modifier is on in the third element of the keysym array, instead
467 if ((strcmp (ServerVendor (xdisplay), "Sun Microsystems, Inc.") == 0) &&
468 (keymap_x11->keysyms_per_keycode > 2))
469 keymap_x11->sun_keypad = TRUE;
471 keymap_x11->sun_keypad = FALSE;
476 get_keymap (GdkKeymapX11 *keymap_x11)
478 update_keymaps (keymap_x11);
480 return keymap_x11->keymap;
483 #define GET_EFFECTIVE_KEYMAP(keymap) get_effective_keymap ((keymap), G_STRFUNC)
486 get_effective_keymap (GdkKeymap *keymap,
487 const char *function)
492 g_message ("reverting to default display keymap in %s",
494 return gdk_keymap_get_default ();
501 static PangoDirection
502 get_direction (XkbDescRec *xkb,
507 gint rtl_minus_ltr = 0; /* total number of RTL keysyms minus LTR ones */
509 for (code = xkb->min_key_code; code <= xkb->max_key_code; code++)
511 gint width = XkbKeyGroupWidth (xkb, code, group);
513 for (level = 0; level < width; level++)
515 KeySym sym = XkbKeySymEntry (xkb, code, level, group);
516 PangoDirection dir = pango_unichar_direction (gdk_keyval_to_unicode (sym));
520 case PANGO_DIRECTION_RTL:
523 case PANGO_DIRECTION_LTR:
532 if (rtl_minus_ltr > 0)
533 return PANGO_DIRECTION_RTL;
535 return PANGO_DIRECTION_LTR;
539 update_direction (GdkKeymapX11 *keymap_x11)
541 XkbDescRec *xkb = get_xkb (keymap_x11);
542 XkbStateRec state_rec;
543 GdkDisplay *display = GDK_KEYMAP (keymap_x11)->display;
547 XkbGetState (GDK_DISPLAY_XDISPLAY (display), XkbUseCoreKbd, &state_rec);
548 group = XkbGroupLock (&state_rec);
549 group_atom = xkb->names->groups[group];
551 /* a group change? */
552 if (!keymap_x11->have_direction || keymap_x11->current_group_atom != group_atom)
554 gboolean cache_hit = FALSE;
555 DirectionCacheEntry *cache = keymap_x11->group_direction_cache;
557 PangoDirection direction = PANGO_DIRECTION_NEUTRAL;
560 if (keymap_x11->have_direction)
562 /* lookup in cache */
563 for (i = 0; i < G_N_ELEMENTS (keymap_x11->group_direction_cache); i++)
565 if (cache[i].group_atom == group_atom)
568 cache[i].serial = keymap_x11->current_cache_serial++; /* freshen */
569 direction = cache[i].direction;
570 group_atom = cache[i].group_atom;
577 /* initialize cache */
578 for (i = 0; i < G_N_ELEMENTS (keymap_x11->group_direction_cache); i++)
580 cache[i].group_atom = 0;
581 cache[i].serial = keymap_x11->current_cache_serial;
583 keymap_x11->current_cache_serial++;
586 /* insert in cache */
591 direction = get_direction (xkb, group);
593 /* remove the oldest entry */
594 for (i = 0; i < G_N_ELEMENTS (keymap_x11->group_direction_cache); i++)
596 if (cache[i].serial < cache[oldest].serial)
600 cache[oldest].group_atom = group_atom;
601 cache[oldest].direction = direction;
602 cache[oldest].serial = keymap_x11->current_cache_serial++;
605 keymap_x11->current_group_atom = group_atom;
607 keymap_x11->have_direction = TRUE;
608 keymap_x11->current_direction = direction;
613 _gdk_keymap_direction_changed (GdkKeymapX11 *keymap_x11)
615 gboolean had_direction;
616 PangoDirection direction;
618 had_direction = keymap_x11->have_direction;
619 direction = keymap_x11->current_direction;
621 update_direction (keymap_x11);
623 if (!had_direction || direction != keymap_x11->current_direction)
624 g_signal_emit_by_name (keymap_x11, "direction_changed");
629 _gdk_keymap_state_changed (GdkDisplay *display)
631 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
633 if (display_x11->keymap)
635 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (display_x11->keymap);
637 _gdk_keymap_direction_changed (keymap_x11);
641 #endif /* HAVE_XKB */
644 _gdk_keymap_keys_changed (GdkDisplay *display)
646 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
648 ++display_x11->keymap_serial;
650 if (display_x11->keymap)
651 g_signal_emit_by_name (display_x11->keymap, "keys_changed", 0);
655 gdk_keymap_get_direction (GdkKeymap *keymap)
657 keymap = GET_EFFECTIVE_KEYMAP (keymap);
660 if (KEYMAP_USE_XKB (keymap))
662 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (keymap);
664 if (!keymap_x11->have_direction)
665 update_direction (keymap_x11);
667 return keymap_x11->current_direction;
670 #endif /* HAVE_XKB */
671 return PANGO_DIRECTION_NEUTRAL;
675 * gdk_keymap_get_entries_for_keyval:
676 * @keymap: a #GdkKeymap, or %NULL to use the default keymap
677 * @keyval: a keyval, such as %GDK_a, %GDK_Up, %GDK_Return, etc.
678 * @keys: return location for an array of #GdkKeymapKey
679 * @n_keys: return location for number of elements in returned array
681 * Obtains a list of keycode/group/level combinations that will
682 * generate @keyval. Groups and levels are two kinds of keyboard mode;
683 * in general, the level determines whether the top or bottom symbol
684 * on a key is used, and the group determines whether the left or
685 * right symbol is used. On US keyboards, the shift key changes the
686 * keyboard level, and there are no groups. A group switch key might
687 * convert a keyboard between Hebrew to English modes, for example.
688 * #GdkEventKey contains a %group field that indicates the active
689 * keyboard group. The level is computed from the modifier mask.
690 * The returned array should be freed
693 * Return value: %TRUE if keys were found and returned
696 gdk_keymap_get_entries_for_keyval (GdkKeymap *keymap,
702 GdkKeymapX11 *keymap_x11;
704 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
705 g_return_val_if_fail (keys != NULL, FALSE);
706 g_return_val_if_fail (n_keys != NULL, FALSE);
707 g_return_val_if_fail (keyval != 0, FALSE);
709 keymap = GET_EFFECTIVE_KEYMAP (keymap);
710 keymap_x11 = GDK_KEYMAP_X11 (keymap);
712 retval = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
715 if (KEYMAP_USE_XKB (keymap))
717 /* See sec 15.3.4 in XKB docs */
719 XkbDescRec *xkb = get_xkb (keymap_x11);
722 keycode = keymap_x11->min_keycode;
724 while (keycode <= keymap_x11->max_keycode)
726 gint max_shift_levels = XkbKeyGroupsWidth (xkb, keycode); /* "key width" */
729 gint total_syms = XkbKeyNumSyms (xkb, keycode);
733 /* entry is an array with all syms for group 0, all
734 * syms for group 1, etc. and for each group the
735 * shift level syms are in order
737 entry = XkbKeySymsPtr (xkb, keycode);
739 while (i < total_syms)
741 /* check out our cool loop invariant */
742 g_assert (i == (group * max_shift_levels + level));
744 if (entry[i] == keyval)
749 key.keycode = keycode;
753 g_array_append_val (retval, key);
755 g_assert (XkbKeySymEntry (xkb, keycode, level, group) ==
761 if (level == max_shift_levels)
776 const KeySym *map = get_keymap (keymap_x11);
779 keycode = keymap_x11->min_keycode;
780 while (keycode <= keymap_x11->max_keycode)
782 const KeySym *syms = map + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
785 while (i < keymap_x11->keysyms_per_keycode)
787 if (syms[i] == keyval)
792 key.keycode = keycode;
794 /* The "classic" non-XKB keymap has 2 levels per group */
798 g_array_append_val (retval, key);
810 *keys = (GdkKeymapKey*) retval->data;
811 *n_keys = retval->len;
819 g_array_free (retval, retval->len > 0 ? FALSE : TRUE);
825 * gdk_keymap_get_entries_for_keycode:
826 * @keymap: a #GdkKeymap or %NULL to use the default keymap
827 * @hardware_keycode: a keycode
828 * @keys: return location for array of #GdkKeymapKey, or NULL
829 * @keyvals: return location for array of keyvals, or NULL
830 * @n_entries: length of @keys and @keyvals
832 * Returns the keyvals bound to @hardware_keycode.
833 * The Nth #GdkKeymapKey in @keys is bound to the Nth
834 * keyval in @keyvals. Free the returned arrays with g_free().
835 * When a keycode is pressed by the user, the keyval from
836 * this list of entries is selected by considering the effective
837 * keyboard group and level. See gdk_keymap_translate_keyboard_state().
839 * Returns: %TRUE if there were any entries
842 gdk_keymap_get_entries_for_keycode (GdkKeymap *keymap,
843 guint hardware_keycode,
848 GdkKeymapX11 *keymap_x11;
851 GArray *keyval_array;
853 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
854 g_return_val_if_fail (n_entries != NULL, FALSE);
856 keymap = GET_EFFECTIVE_KEYMAP (keymap);
857 keymap_x11 = GDK_KEYMAP_X11 (keymap);
859 update_keyrange (keymap_x11);
861 if (hardware_keycode < keymap_x11->min_keycode ||
862 hardware_keycode > keymap_x11->max_keycode)
874 key_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
879 keyval_array = g_array_new (FALSE, FALSE, sizeof (guint));
884 if (KEYMAP_USE_XKB (keymap))
886 /* See sec 15.3.4 in XKB docs */
888 XkbDescRec *xkb = get_xkb (keymap_x11);
889 gint max_shift_levels;
896 max_shift_levels = XkbKeyGroupsWidth (xkb, hardware_keycode); /* "key width" */
897 total_syms = XkbKeyNumSyms (xkb, hardware_keycode);
899 /* entry is an array with all syms for group 0, all
900 * syms for group 1, etc. and for each group the
901 * shift level syms are in order
903 entry = XkbKeySymsPtr (xkb, hardware_keycode);
905 while (i < total_syms)
907 /* check out our cool loop invariant */
908 g_assert (i == (group * max_shift_levels + level));
914 key.keycode = hardware_keycode;
918 g_array_append_val (key_array, key);
922 g_array_append_val (keyval_array, entry[i]);
926 if (level == max_shift_levels)
938 const KeySym *map = get_keymap (keymap_x11);
942 syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
944 while (i < keymap_x11->keysyms_per_keycode)
950 key.keycode = hardware_keycode;
952 /* The "classic" non-XKB keymap has 2 levels per group */
956 g_array_append_val (key_array, key);
960 g_array_append_val (keyval_array, syms[i]);
966 if ((key_array && key_array->len > 0) ||
967 (keyval_array && keyval_array->len > 0))
970 *keys = (GdkKeymapKey*) key_array->data;
973 *keyvals = (guint*) keyval_array->data;
976 *n_entries = key_array->len;
978 *n_entries = keyval_array->len;
992 g_array_free (key_array, key_array->len > 0 ? FALSE : TRUE);
994 g_array_free (keyval_array, keyval_array->len > 0 ? FALSE : TRUE);
996 return *n_entries > 0;
1001 * gdk_keymap_lookup_key:
1002 * @keymap: a #GdkKeymap or %NULL to use the default keymap
1003 * @key: a #GdkKeymapKey with keycode, group, and level initialized
1005 * Looks up the keyval mapped to a keycode/group/level triplet.
1006 * If no keyval is bound to @key, returns 0. For normal user input,
1007 * you want to use gdk_keymap_translate_keyboard_state() instead of
1008 * this function, since the effective group/level may not be
1009 * the same as the current keyboard state.
1011 * Return value: a keyval, or 0 if none was mapped to the given @key
1014 gdk_keymap_lookup_key (GdkKeymap *keymap,
1015 const GdkKeymapKey *key)
1017 GdkKeymapX11 *keymap_x11;
1019 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), 0);
1020 g_return_val_if_fail (key != NULL, 0);
1021 g_return_val_if_fail (key->group < 4, 0);
1023 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1024 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1027 if (KEYMAP_USE_XKB (keymap))
1029 XkbDescRec *xkb = get_xkb (keymap_x11);
1031 return XkbKeySymEntry (xkb, key->keycode, key->level, key->group);
1036 const KeySym *map = get_keymap (keymap_x11);
1037 const KeySym *syms = map + (key->keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1038 return get_symbol (syms, keymap_x11, key->group, key->level);
1043 /* This is copied straight from XFree86 Xlib, to:
1044 * - add the group and level return.
1045 * - change the interpretation of mods_rtrn as described
1046 * in the docs for gdk_keymap_translate_keyboard_state()
1047 * It's unchanged for ease of diff against the Xlib sources; don't
1051 MyEnhancedXkbTranslateKeyCode(register XkbDescPtr xkb,
1053 register unsigned int mods,
1054 unsigned int * mods_rtrn,
1055 KeySym * keysym_rtrn,
1056 unsigned int * group_rtrn,
1057 unsigned int * level_rtrn)
1059 XkbKeyTypeRec *type;
1061 unsigned preserve,effectiveGroup;
1064 if (mods_rtrn!=NULL)
1067 nKeyGroups= XkbKeyNumGroups(xkb,key);
1068 if ((!XkbKeycodeInRange(xkb,key))||(nKeyGroups==0)) {
1069 if (keysym_rtrn!=NULL)
1070 *keysym_rtrn = NoSymbol;
1074 syms = XkbKeySymsPtr(xkb,key);
1076 /* find the offset of the effective group */
1078 effectiveGroup= XkbGroupForCoreState(mods);
1079 if ( effectiveGroup>=nKeyGroups ) {
1080 unsigned groupInfo= XkbKeyGroupInfo(xkb,key);
1081 switch (XkbOutOfRangeGroupAction(groupInfo)) {
1083 effectiveGroup %= nKeyGroups;
1085 case XkbClampIntoRange:
1086 effectiveGroup = nKeyGroups-1;
1088 case XkbRedirectIntoRange:
1089 effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
1090 if (effectiveGroup>=nKeyGroups)
1095 col= effectiveGroup*XkbKeyGroupsWidth(xkb,key);
1096 type = XkbKeyKeyType(xkb,key,effectiveGroup);
1099 if (type->map) { /* find the column (shift level) within the group */
1101 register XkbKTMapEntryPtr entry;
1102 /* ---- Begin section modified for GDK ---- */
1105 for (i=0,entry=type->map;i<type->map_count;i++,entry++) {
1108 unsigned long tmp = entry->mods.mask;
1114 /* We always add one-modifiers levels to mods_rtrn since
1115 * they can't wipe out bits in the state unless the
1116 * level would be triggered. But return other modifiers
1119 if (bits == 1 || (mods&type->mods.mask)==entry->mods.mask)
1120 *mods_rtrn |= entry->mods.mask;
1123 if (!found&&entry->active&&((mods&type->mods.mask)==entry->mods.mask)) {
1126 preserve= type->preserve[i].mask;
1129 *level_rtrn = entry->level;
1134 /* ---- End section modified for GDK ---- */
1137 if (keysym_rtrn!=NULL)
1138 *keysym_rtrn= syms[col];
1140 /* ---- Begin section modified for GDK ---- */
1141 *mods_rtrn &= ~preserve;
1142 /* ---- End section modified for GDK ---- */
1144 /* ---- Begin stuff GDK comments out of the original Xlib version ---- */
1145 /* This is commented out because xkb_info is a private struct */
1148 /* The Motif VTS doesn't get the help callback called if help
1149 * is bound to Shift+<whatever>, and it appears as though it
1150 * is XkbTranslateKeyCode that is causing the problem. The
1151 * core X version of XTranslateKey always OR's in ShiftMask
1152 * and LockMask for mods_rtrn, so this "fix" keeps this behavior
1153 * and solves the VTS problem.
1155 if ((xkb->dpy)&&(xkb->dpy->xkb_info)&&
1156 (xkb->dpy->xkb_info->xlib_ctrls&XkbLC_AlwaysConsumeShiftAndLock)) { *mods_rtrn|= (ShiftMask|LockMask);
1160 /* ---- End stuff GDK comments out of the original Xlib version ---- */
1163 /* ---- Begin stuff GDK adds to the original Xlib version ---- */
1166 *group_rtrn = effectiveGroup;
1168 /* ---- End stuff GDK adds to the original Xlib version ---- */
1170 return (syms[col]!=NoSymbol);
1172 #endif /* HAVE_XKB */
1174 /* Translates from keycode/state to keysymbol using the traditional interpretation
1175 * of the keyboard map. See section 12.7 of the Xlib reference manual
1178 translate_keysym (GdkKeymapX11 *keymap_x11,
1179 guint hardware_keycode,
1181 GdkModifierType state,
1182 guint *effective_group,
1183 guint *effective_level)
1185 const KeySym *map = get_keymap (keymap_x11);
1186 const KeySym *syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1188 #define SYM(k,g,l) get_symbol (syms, k,g,l)
1190 GdkModifierType shift_modifiers;
1193 gint num_lock_index;
1195 shift_modifiers = GDK_SHIFT_MASK;
1196 if (keymap_x11->lock_keysym == GDK_Shift_Lock)
1197 shift_modifiers |= GDK_LOCK_MASK;
1199 /* Fall back to the first group if the passed in group is empty
1201 if (!(SYM (keymap_x11, group, 0) || SYM (keymap_x11, group, 1)) &&
1202 (SYM (keymap_x11, 0, 0) || SYM (keymap_x11, 0, 1)))
1205 /* Hack: On Sun, the Num Lock modifier uses the third element in the
1206 * keysym array, and Mode_Switch does not apply for a keypad key.
1208 if (keymap_x11->sun_keypad)
1216 for (i = 0; i < keymap_x11->keysyms_per_keycode; i++)
1217 if (KEYSYM_IS_KEYPAD (SYM (keymap_x11, 0, i)))
1224 if ((state & keymap_x11->num_lock_mask) &&
1225 KEYSYM_IS_KEYPAD (SYM (keymap_x11, group, num_lock_index)))
1227 /* Shift, Shift_Lock cancel Num_Lock
1229 shift_level = (state & shift_modifiers) ? 0 : num_lock_index;
1230 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1233 tmp_keyval = SYM (keymap_x11, group, shift_level);
1237 /* Fall back to the first level if no symbol for the level
1240 shift_level = (state & shift_modifiers) ? 1 : 0;
1241 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1244 tmp_keyval = SYM (keymap_x11, group, shift_level);
1246 if (keymap_x11->lock_keysym == GDK_Caps_Lock && (state & GDK_LOCK_MASK) != 0)
1248 guint upper = gdk_keyval_to_upper (tmp_keyval);
1249 if (upper != tmp_keyval)
1254 if (effective_group)
1255 *effective_group = group;
1257 if (effective_level)
1258 *effective_level = shift_level;
1266 * gdk_keymap_translate_keyboard_state:
1267 * @keymap: a #GdkKeymap, or %NULL to use the default
1268 * @hardware_keycode: a keycode
1269 * @state: a modifier state
1270 * @group: active keyboard group
1271 * @keyval: return location for keyval
1272 * @effective_group: return location for effective group
1273 * @level: return location for level
1274 * @consumed_modifiers: return location for modifiers that were used to determine the group or level
1277 * Translates the contents of a #GdkEventKey into a keyval, effective
1278 * group, and level. Modifiers that affected the translation and
1279 * are thus unavailable for application use are returned in
1280 * @consumed_modifiers. See gdk_keyval_get_keys() for an explanation of
1281 * groups and levels. The @effective_group is the group that was
1282 * actually used for the translation; some keys such as Enter are not
1283 * affected by the active keyboard group. The @level is derived from
1284 * @state. For convenience, #GdkEventKey already contains the translated
1285 * keyval, so this function isn't as useful as you might think.
1288 * @consumed_modifiers gives modifiers that should be masked out
1289 * from @state when comparing this key press to a hot key. For
1290 * instance, on a US keyboard, the <literal>plus</literal>
1291 * symbol is shifted, so when comparing a key press to a
1292 * <literal><Control>plus</literal> accelerator <Shift> should
1295 * <informalexample><programlisting>
1296 * /* We want to ignore irrelevant modifiers like ScrollLock */
1297 * #define ALL_ACCELS_MASK (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK)
1298 * gdk_keymap_translate_keyboard_state (keymap, event->hardware_keycode,
1299 * event->state, event->group,
1300 * &keyval, NULL, NULL, &consumed);
1301 * if (keyval == GDK_PLUS &&
1302 * (event->state & ~consumed & ALL_ACCELS_MASK) == GDK_CONTROL_MASK)
1303 * /* Control was pressed */
1304 * </programlisting></informalexample>
1306 * An older interpretation @consumed_modifiers was that it contained
1307 * all modifiers that might affect the translation of the key;
1308 * this allowed accelerators to be stored with irrelevant consumed
1309 * modifiers, by doing:</para>
1310 * <informalexample><programlisting>
1311 * /* XXX Don't do this XXX */
1312 * if (keyval == accel_keyval &&
1313 * (event->state & ~consumed & ALL_ACCELS_MASK) == (accel_mods & ~consumed))
1314 * /* Accelerator was pressed */
1315 * </programlisting></informalexample>
1317 * However, this did not work if multi-modifier combinations were
1318 * used in the keymap, since, for instance, <literal><Control></literal>
1319 * would be masked out even if only <literal><Control><Alt></literal>
1320 * was used in the keymap. To support this usage as well as well as
1321 * possible, all <emphasis>single modifier</emphasis> combinations
1322 * that could affect the key for any combination of modifiers will
1323 * be returned in @consumed_modifiers; multi-modifier combinations
1324 * are returned only when actually found in @state. When you store
1325 * accelerators, you should always store them with consumed modifiers
1326 * removed. Store <literal><Control>plus</literal>,
1327 * not <literal><Control><Shift>plus</literal>,
1330 * Return value: %TRUE if there was a keyval bound to the keycode/state/group
1333 gdk_keymap_translate_keyboard_state (GdkKeymap *keymap,
1334 guint hardware_keycode,
1335 GdkModifierType state,
1338 gint *effective_group,
1340 GdkModifierType *consumed_modifiers)
1342 GdkKeymapX11 *keymap_x11;
1343 KeySym tmp_keyval = NoSymbol;
1344 guint tmp_modifiers;
1346 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
1347 g_return_val_if_fail (group < 4, FALSE);
1349 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1350 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1354 if (effective_group)
1355 *effective_group = 0;
1358 if (consumed_modifiers)
1359 *consumed_modifiers = 0;
1361 update_keyrange (keymap_x11);
1363 if (hardware_keycode < keymap_x11->min_keycode ||
1364 hardware_keycode > keymap_x11->max_keycode)
1368 if (KEYMAP_USE_XKB (keymap))
1370 XkbDescRec *xkb = get_xkb (keymap_x11);
1372 /* replace bits 13 and 14 with the provided group */
1373 state &= ~(1 << 13 | 1 << 14);
1374 state |= group << 13;
1376 MyEnhancedXkbTranslateKeyCode (xkb,
1384 if (state & ~tmp_modifiers & LockMask)
1385 tmp_keyval = gdk_keyval_to_upper (tmp_keyval);
1387 /* We need to augment the consumed modifiers with LockMask, since
1388 * we handle that ourselves, and also with the group bits
1390 tmp_modifiers |= LockMask | 1 << 13 | 1 << 14;
1395 GdkModifierType bit;
1399 /* We see what modifiers matter by trying the translation with
1400 * and without each possible modifier
1402 for (bit = GDK_SHIFT_MASK; bit < GDK_BUTTON1_MASK; bit <<= 1)
1404 /* Handling of the group here is a bit funky; a traditional
1405 * X keyboard map can have more than two groups, but no way
1406 * of accessing the extra groups is defined. We allow a
1407 * caller to pass in any group to this function, but we
1408 * only can represent switching between group 0 and 1 in
1409 * consumed modifiers.
1411 if (translate_keysym (keymap_x11, hardware_keycode,
1412 (bit == keymap_x11->group_switch_mask) ? 0 : group,
1415 translate_keysym (keymap_x11, hardware_keycode,
1416 (bit == keymap_x11->group_switch_mask) ? 1 : group,
1419 tmp_modifiers |= bit;
1422 tmp_keyval = translate_keysym (keymap_x11, hardware_keycode,
1424 level, effective_group);
1427 if (consumed_modifiers)
1428 *consumed_modifiers = tmp_modifiers;
1431 *keyval = tmp_keyval;
1433 return tmp_keyval != NoSymbol;
1437 /* Key handling not part of the keymap */
1440 gdk_keyval_name (guint keyval)
1448 case GDK_KP_Page_Up:
1449 return "KP_Page_Up";
1450 case GDK_KP_Page_Down:
1451 return "KP_Page_Down";
1454 return XKeysymToString (keyval);
1458 gdk_keyval_from_name (const gchar *keyval_name)
1460 g_return_val_if_fail (keyval_name != NULL, 0);
1462 return XStringToKeysym (keyval_name);
1465 #ifdef HAVE_XCONVERTCASE
1467 gdk_keyval_convert_case (guint symbol,
1474 /* Check for directly encoded 24-bit UCS characters: */
1475 if ((symbol & 0xff000000) == 0x01000000)
1478 *lower = gdk_unicode_to_keyval (g_unichar_tolower (symbol & 0x00ffffff));
1480 *upper = gdk_unicode_to_keyval (g_unichar_toupper (symbol & 0x00ffffff));
1485 XConvertCase (symbol, &xlower, &xupper);
1492 #endif /* HAVE_XCONVERTCASE */
1495 _gdk_x11_get_group_for_state (GdkDisplay *display,
1496 GdkModifierType state)
1498 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1501 if (display_x11->use_xkb)
1503 return XkbGroupForCoreState (state);
1508 GdkKeymapX11 *keymap_impl = GDK_KEYMAP_X11 (gdk_keymap_get_for_display (display));
1509 update_keymaps (keymap_impl);
1510 return (state & keymap_impl->group_switch_mask) ? 1 : 0;
1515 _gdk_keymap_add_virtual_modifiers (GdkKeymap *keymap,
1516 GdkModifierType *modifiers)
1518 GdkKeymapX11 *keymap_x11;
1521 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1522 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1524 for (i = 3; i < 8; i++)
1526 if ((1 << i) & *modifiers)
1528 if (keymap_x11->modmap[i] & GDK_MOD1_MASK)
1529 *modifiers |= GDK_MOD1_MASK;
1530 else if (keymap_x11->modmap[i] & GDK_SUPER_MASK)
1531 *modifiers |= GDK_SUPER_MASK;
1532 else if (keymap_x11->modmap[i] & GDK_HYPER_MASK)
1533 *modifiers |= GDK_HYPER_MASK;
1534 else if (keymap_x11->modmap[i] & GDK_META_MASK)
1535 *modifiers |= GDK_META_MASK;
1541 #define __GDK_KEYS_X11_C__
1542 #include "gdkaliasdef.c"