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"
45 #include <X11/XKBlib.h>
47 /* OSF-4.0 is apparently missing this macro
49 # ifndef XkbKeySymEntry
50 # define XkbKeySymEntry(d,k,sl,g) \
51 (XkbKeySym(d,k,((XkbKeyGroupsWidth(d,k)*(g))+(sl))))
55 typedef struct _GdkKeymapX11 GdkKeymapX11;
57 #define GDK_TYPE_KEYMAP_X11 (gdk_keymap_x11_get_type ())
58 #define GDK_KEYMAP_X11(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_KEYMAP_X11, GdkKeymapX11))
59 #define GDK_IS_KEYMAP_X11(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_KEYMAP_X11))
63 GdkKeymap parent_instance;
68 gint keysyms_per_keycode;
69 XModifierKeymap* mod_keymap;
71 GdkModifierType group_switch_mask;
72 GdkModifierType num_lock_mask;
74 PangoDirection current_direction;
75 gboolean have_direction;
83 #define KEYMAP_USE_XKB(keymap) GDK_DISPLAY_X11 ((keymap)->display)->use_xkb
84 #define KEYMAP_XDISPLAY(keymap) GDK_DISPLAY_XDISPLAY ((keymap)->display)
86 static GType gdk_keymap_x11_get_type (void);
87 static void gdk_keymap_x11_init (GdkKeymapX11 *keymap);
90 gdk_keymap_x11_get_type (void)
92 static GType object_type = 0;
96 static const GTypeInfo object_info =
98 sizeof (GdkKeymapClass),
100 (GBaseFinalizeFunc) NULL,
101 (GClassInitFunc) NULL,
102 NULL, /* class_finalize */
103 NULL, /* class_data */
104 sizeof (GdkKeymapX11),
106 (GInstanceInitFunc) gdk_keymap_x11_init,
109 object_type = g_type_register_static (GDK_TYPE_KEYMAP,
118 gdk_keymap_x11_init (GdkKeymapX11 *keymap)
120 keymap->min_keycode = 0;
121 keymap->max_keycode = 0;
123 keymap->keymap = NULL;
124 keymap->keysyms_per_keycode = 0;
125 keymap->mod_keymap = NULL;
127 keymap->num_lock_mask = 0;
128 keymap->sun_keypad = FALSE;
129 keymap->group_switch_mask = 0;
130 keymap->lock_keysym = GDK_Caps_Lock;
131 keymap->have_direction = FALSE;
134 keymap->xkb_desc = NULL;
137 keymap->current_serial = 0;
141 update_keyrange (GdkKeymapX11 *keymap_x11)
143 if (keymap_x11->max_keycode == 0)
144 XDisplayKeycodes (KEYMAP_XDISPLAY (GDK_KEYMAP (keymap_x11)),
145 &keymap_x11->min_keycode, &keymap_x11->max_keycode);
151 get_xkb (GdkKeymapX11 *keymap_x11)
153 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_KEYMAP (keymap_x11)->display);
154 Display *xdisplay = display_x11->xdisplay;
156 update_keyrange (keymap_x11);
158 if (keymap_x11->xkb_desc == NULL)
160 keymap_x11->xkb_desc = XkbGetMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask, XkbUseCoreKbd);
161 if (keymap_x11->xkb_desc == NULL)
162 g_error ("Failed to get keymap");
164 XkbGetNames (xdisplay, XkbGroupNamesMask, keymap_x11->xkb_desc);
166 else if (keymap_x11->current_serial != display_x11->keymap_serial)
168 XkbGetUpdatedMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask,
169 keymap_x11->xkb_desc);
170 XkbGetNames (xdisplay, XkbGroupNamesMask, keymap_x11->xkb_desc);
173 keymap_x11->current_serial = display_x11->keymap_serial;
175 return keymap_x11->xkb_desc;
177 #endif /* HAVE_XKB */
179 /* Whether we were able to turn on detectable-autorepeat using
180 * XkbSetDetectableAutorepeat. If FALSE, we'll fall back
181 * to checking the next event with XPending().
185 * gdk_keymap_get_for_display:
186 * @display: the #GdkDisplay.
187 * @returns: the #GdkKeymap attached to @display.
189 * Returns the #GdkKeymap attached to @display.
194 gdk_keymap_get_for_display (GdkDisplay *display)
196 GdkDisplayX11 *display_x11;
197 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
198 display_x11 = GDK_DISPLAY_X11 (display);
200 if (!display_x11->keymap)
201 display_x11->keymap = g_object_new (gdk_keymap_x11_get_type (), NULL);
203 display_x11->keymap->display = display;
205 return display_x11->keymap;
208 /* Find the index of the group/level pair within the keysyms for a key.
210 #define KEYSYM_INDEX(keymap_impl, group, level) \
211 (2 * ((group) % (keymap_impl->keysyms_per_keycode / 2)) + (level))
212 #define KEYSYM_IS_KEYPAD(s) (((s) >= 0xff80 && (s) <= 0xffbd) || \
213 ((s) >= 0x11000000 && (s) <= 0x1100ffff))
216 update_keymaps (GdkKeymapX11 *keymap_x11)
218 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_KEYMAP (keymap_x11)->display);
219 Display *xdisplay = display_x11->xdisplay;
222 g_assert (!KEYMAP_USE_XKB (GDK_KEYMAP (keymap_x11)));
225 if (keymap_x11->keymap == NULL ||
226 keymap_x11->current_serial != display_x11->keymap_serial)
232 keymap_x11->current_serial = display_x11->keymap_serial;
234 update_keyrange (keymap_x11);
236 if (keymap_x11->keymap)
237 XFree (keymap_x11->keymap);
239 if (keymap_x11->mod_keymap)
240 XFreeModifiermap (keymap_x11->mod_keymap);
242 keymap_x11->keymap = XGetKeyboardMapping (xdisplay, keymap_x11->min_keycode,
243 keymap_x11->max_keycode - keymap_x11->min_keycode + 1,
244 &keymap_x11->keysyms_per_keycode);
247 /* GDK_ISO_Left_Tab, as usually configured through XKB, really messes
248 * up the whole idea of "consumed modifiers" because shift is consumed.
249 * However, <shift>Tab is not usually GDK_ISO_Left_Tab without XKB,
250 * we we fudge the map here.
252 keycode = keymap_x11->min_keycode;
253 while (keycode <= keymap_x11->max_keycode)
255 KeySym *syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
256 /* Check both groups */
257 for (i = 0 ; i < 2 ; i++)
259 if (syms[KEYSYM_INDEX (keymap_x11, i, 0)] == GDK_Tab)
260 syms[KEYSYM_INDEX (keymap_x11, i, 1)] = GDK_ISO_Left_Tab;
264 * If there is one keysym and the key symbol has upper and lower
265 * case variants fudge the keymap
267 if (syms[KEYSYM_INDEX (keymap_x11, 0, 1)] == 0)
272 gdk_keyval_convert_case (syms[KEYSYM_INDEX (keymap_x11, 0, 0)], &lower, &upper);
275 syms[KEYSYM_INDEX (keymap_x11, 0, 0)] = lower;
276 syms[KEYSYM_INDEX (keymap_x11, 0, 1)] = upper;
284 keymap_x11->mod_keymap = XGetModifierMapping (xdisplay);
286 keymap_x11->lock_keysym = GDK_VoidSymbol;
287 keymap_x11->group_switch_mask = 0;
288 keymap_x11->num_lock_mask = 0;
290 /* There are 8 sets of modifiers, with each set containing
291 * max_keypermod keycodes.
293 map_size = 8 * keymap_x11->mod_keymap->max_keypermod;
294 for (i = 0; i < map_size; i++)
296 /* Get the key code at this point in the map. */
297 gint keycode = keymap_x11->mod_keymap->modifiermap[i];
302 /* Ignore invalid keycodes. */
303 if (keycode < keymap_x11->min_keycode ||
304 keycode > keymap_x11->max_keycode)
307 syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
309 /* The fourth modifier, GDK_MOD1_MASK is 1 << 3.
310 * Each group of max_keypermod entries refers to the same modifier.
312 mask = 1 << (i / keymap_x11->mod_keymap->max_keypermod);
317 /* Get the Lock keysym. If any keysym bound to the Lock modifier
318 * is Caps_Lock, we will interpret the modifier as Caps_Lock;
319 * otherwise, if any is bound to Shift_Lock, we will interpret
320 * the modifier as Shift_Lock. Otherwise, the lock modifier
323 for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
325 if (syms[j] == GDK_Caps_Lock)
326 keymap_x11->lock_keysym = GDK_Caps_Lock;
327 else if (syms[j] == GDK_Shift_Lock &&
328 keymap_x11->lock_keysym == GDK_VoidSymbol)
329 keymap_x11->lock_keysym = GDK_Shift_Lock;
333 case GDK_CONTROL_MASK:
336 /* Some keyboard maps are known to map Mode_Switch as an
337 * extra Mod1 key. In circumstances like that, it won't be
338 * used to switch groups.
343 /* Find the Mode_Switch and Num_Lock modifiers. */
344 for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
346 if (syms[j] == GDK_Mode_switch)
348 /* This modifier swaps groups */
349 keymap_x11->group_switch_mask |= mask;
351 else if (syms[j] == GDK_Num_Lock)
353 /* This modifier is used for Num_Lock */
354 keymap_x11->num_lock_mask |= mask;
361 /* Hack: The Sun X server puts the keysym to use when the Num Lock
362 * modifier is on in the third element of the keysym array, instead
365 if ((strcmp (ServerVendor (xdisplay), "Sun Microsystems, Inc.") == 0) &&
366 (keymap_x11->keysyms_per_keycode > 2))
367 keymap_x11->sun_keypad = TRUE;
369 keymap_x11->sun_keypad = FALSE;
374 get_keymap (GdkKeymapX11 *keymap_x11)
376 update_keymaps (keymap_x11);
378 return keymap_x11->keymap;
382 static PangoDirection
383 get_direction (GdkKeymapX11 *keymap_x11)
385 XkbDescRec *xkb = get_xkb (keymap_x11);
387 XkbStateRec state_rec;
388 PangoDirection result;
390 GdkDisplay *display = GDK_KEYMAP (keymap_x11)->display;
392 XkbGetState (GDK_DISPLAY_XDISPLAY (display), XkbUseCoreKbd, &state_rec);
394 if (xkb->names->groups[state_rec.locked_group] == None)
395 result = PANGO_DIRECTION_LTR;
398 name = gdk_x11_get_xatom_name_for_display (display, xkb->names->groups[state_rec.locked_group]);
400 if (g_ascii_strcasecmp (name, "arabic") == 0 ||
401 g_ascii_strcasecmp (name, "hebrew") == 0 ||
402 g_ascii_strcasecmp (name, "israelian") == 0)
403 result = PANGO_DIRECTION_RTL;
405 result = PANGO_DIRECTION_LTR;
412 _gdk_keymap_state_changed (GdkDisplay *display)
414 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
416 if (display_x11->keymap)
418 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (display_x11->keymap);
420 PangoDirection new_direction = get_direction (keymap_x11);
422 if (!keymap_x11->have_direction || new_direction != keymap_x11->current_direction)
424 keymap_x11->have_direction = TRUE;
425 keymap_x11->current_direction = new_direction;
426 g_signal_emit_by_name (keymap_x11, "direction_changed");
431 #endif /* HAVE_XKB */
434 _gdk_keymap_keys_changed (GdkDisplay *display)
436 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
438 ++display_x11->keymap_serial;
440 if (display_x11->keymap)
441 g_signal_emit_by_name (display_x11->keymap, "keys_changed", 0);
445 gdk_keymap_get_direction (GdkKeymap *keymap)
449 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
451 g_message ("_multihead : reverting to default display keymap "
452 "in gdk_keymap_get_direction"));
456 if (KEYMAP_USE_XKB (keymap))
458 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (keymap);
460 if (!keymap_x11->have_direction)
462 keymap_x11->current_direction = get_direction (keymap_x11);
463 keymap_x11->have_direction = TRUE;
466 return keymap_x11->current_direction;
469 #endif /* HAVE_XKB */
470 return PANGO_DIRECTION_LTR;
474 * gdk_keymap_get_entries_for_keyval:
475 * @keymap: a #GdkKeymap, or %NULL to use the default keymap
476 * @keyval: a keyval, such as %GDK_a, %GDK_Up, %GDK_Return, etc.
477 * @keys: return location for an array of #GdkKeymapKey
478 * @n_keys: return location for number of elements in returned array
480 * Obtains a list of keycode/group/level combinations that will
481 * generate @keyval. Groups and levels are two kinds of keyboard mode;
482 * in general, the level determines whether the top or bottom symbol
483 * on a key is used, and the group determines whether the left or
484 * right symbol is used. On US keyboards, the shift key changes the
485 * keyboard level, and there are no groups. A group switch key might
486 * convert a keyboard between Hebrew to English modes, for example.
487 * #GdkEventKey contains a %group field that indicates the active
488 * keyboard group. The level is computed from the modifier mask.
489 * The returned array should be freed
492 * Return value: %TRUE if keys were found and returned
495 gdk_keymap_get_entries_for_keyval (GdkKeymap *keymap,
501 GdkKeymapX11 *keymap_x11;
503 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
504 g_return_val_if_fail (keys != NULL, FALSE);
505 g_return_val_if_fail (n_keys != NULL, FALSE);
506 g_return_val_if_fail (keyval != 0, FALSE);
510 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
512 g_message ("_multihead : reverting to default display keymap "
513 "in gdk_keymap_get_entries_for_keyval\n"));
516 keymap_x11 = GDK_KEYMAP_X11 (keymap);
518 retval = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
521 if (KEYMAP_USE_XKB (keymap))
523 /* See sec 15.3.4 in XKB docs */
525 XkbDescRec *xkb = get_xkb (keymap_x11);
528 keycode = keymap_x11->min_keycode;
530 while (keycode <= keymap_x11->max_keycode)
532 gint max_shift_levels = XkbKeyGroupsWidth (xkb, keycode); /* "key width" */
535 gint total_syms = XkbKeyNumSyms (xkb, keycode);
539 /* entry is an array with all syms for group 0, all
540 * syms for group 1, etc. and for each group the
541 * shift level syms are in order
543 entry = XkbKeySymsPtr (xkb, keycode);
545 while (i < total_syms)
547 /* check out our cool loop invariant */
548 g_assert (i == (group * max_shift_levels + level));
550 if (entry[i] == keyval)
555 key.keycode = keycode;
559 g_array_append_val (retval, key);
561 g_assert (XkbKeySymEntry (xkb, keycode, level, group) ==
567 if (level == max_shift_levels)
582 const KeySym *map = get_keymap (keymap_x11);
585 keycode = keymap_x11->min_keycode;
586 while (keycode <= keymap_x11->max_keycode)
588 const KeySym *syms = map + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
591 while (i < keymap_x11->keysyms_per_keycode)
593 if (syms[i] == keyval)
598 key.keycode = keycode;
600 /* The "classic" non-XKB keymap has 2 levels per group */
604 g_array_append_val (retval, key);
616 *keys = (GdkKeymapKey*) retval->data;
617 *n_keys = retval->len;
625 g_array_free (retval, retval->len > 0 ? FALSE : TRUE);
631 * gdk_keymap_get_entries_for_keycode:
632 * @keymap: a #GdkKeymap or %NULL to use the default keymap
633 * @hardware_keycode: a keycode
634 * @keys: return location for array of #GdkKeymapKey, or NULL
635 * @keyvals: return location for array of keyvals, or NULL
636 * @n_entries: length of @keys and @keyvals
638 * Returns the keyvals bound to @hardware_keycode.
639 * The Nth #GdkKeymapKey in @keys is bound to the Nth
640 * keyval in @keyvals. Free the returned arrays with g_free().
641 * When a keycode is pressed by the user, the keyval from
642 * this list of entries is selected by considering the effective
643 * keyboard group and level. See gdk_keymap_translate_keyboard_state().
645 * Returns: %TRUE if there were any entries
648 gdk_keymap_get_entries_for_keycode (GdkKeymap *keymap,
649 guint hardware_keycode,
654 GdkKeymapX11 *keymap_x11;
657 GArray *keyval_array;
659 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
660 g_return_val_if_fail (n_entries != NULL, FALSE);
664 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
666 g_message ("_multihead : reverting to default display keymap "
667 "in gdk_keymap_get_entries_for_keycode\n"));
670 keymap_x11 = GDK_KEYMAP_X11 (keymap);
672 update_keyrange (keymap_x11);
674 if (hardware_keycode < keymap_x11->min_keycode ||
675 hardware_keycode > keymap_x11->max_keycode)
687 key_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
692 keyval_array = g_array_new (FALSE, FALSE, sizeof (guint));
697 if (KEYMAP_USE_XKB (keymap))
699 /* See sec 15.3.4 in XKB docs */
701 XkbDescRec *xkb = get_xkb (keymap_x11);
702 gint max_shift_levels;
709 max_shift_levels = XkbKeyGroupsWidth (xkb, hardware_keycode); /* "key width" */
710 total_syms = XkbKeyNumSyms (xkb, hardware_keycode);
712 /* entry is an array with all syms for group 0, all
713 * syms for group 1, etc. and for each group the
714 * shift level syms are in order
716 entry = XkbKeySymsPtr (xkb, hardware_keycode);
718 while (i < total_syms)
720 /* check out our cool loop invariant */
721 g_assert (i == (group * max_shift_levels + level));
727 key.keycode = hardware_keycode;
731 g_array_append_val (key_array, key);
735 g_array_append_val (keyval_array, entry[i]);
739 if (level == max_shift_levels)
751 const KeySym *map = get_keymap (keymap_x11);
755 syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
757 while (i < keymap_x11->keysyms_per_keycode)
763 key.keycode = hardware_keycode;
765 /* The "classic" non-XKB keymap has 2 levels per group */
769 g_array_append_val (key_array, key);
773 g_array_append_val (keyval_array, syms[i]);
779 if ((key_array && key_array->len > 0) ||
780 (keyval_array && keyval_array->len > 0))
783 *keys = (GdkKeymapKey*) key_array->data;
786 *keyvals = (guint*) keyval_array->data;
789 *n_entries = key_array->len;
791 *n_entries = keyval_array->len;
805 g_array_free (key_array, key_array->len > 0 ? FALSE : TRUE);
807 g_array_free (keyval_array, keyval_array->len > 0 ? FALSE : TRUE);
809 return *n_entries > 0;
814 * gdk_keymap_lookup_key:
815 * @keymap: a #GdkKeymap or %NULL to use the default keymap
816 * @key: a #GdkKeymapKey with keycode, group, and level initialized
818 * Looks up the keyval mapped to a keycode/group/level triplet.
819 * If no keyval is bound to @key, returns 0. For normal user input,
820 * you want to use gdk_keymap_translate_keyboard_state() instead of
821 * this function, since the effective group/level may not be
822 * the same as the current keyboard state.
824 * Return value: a keyval, or 0 if none was mapped to the given @key
827 gdk_keymap_lookup_key (GdkKeymap *keymap,
828 const GdkKeymapKey *key)
830 GdkKeymapX11 *keymap_x11;
832 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), 0);
833 g_return_val_if_fail (key != NULL, 0);
834 g_return_val_if_fail (key->group < 4, 0);
838 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
840 g_message ("_multihead : reverting to default display keymap "
841 "in gdk_keymap_lookup_key\n"));
844 keymap_x11 = GDK_KEYMAP_X11 (keymap);
847 if (KEYMAP_USE_XKB (keymap))
849 XkbDescRec *xkb = get_xkb (keymap_x11);
851 return XkbKeySymEntry (xkb, key->keycode, key->level, key->group);
856 const KeySym *map = get_keymap (keymap_x11);
857 const KeySym *syms = map + (key->keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
858 return syms [KEYSYM_INDEX (keymap_x11, key->group, key->level)];
863 /* This is copied straight from XFree86 Xlib, to:
864 * - add the group and level return.
865 * - change the interpretation of mods_rtrn as described
866 * in the docs for gdk_keymap_translate_keyboard_state()
867 * It's unchanged for ease of diff against the Xlib sources; don't
871 MyEnhancedXkbTranslateKeyCode(register XkbDescPtr xkb,
873 register unsigned int mods,
874 unsigned int * mods_rtrn,
875 KeySym * keysym_rtrn,
876 unsigned int * group_rtrn,
877 unsigned int * level_rtrn)
881 unsigned preserve,effectiveGroup;
887 nKeyGroups= XkbKeyNumGroups(xkb,key);
888 if ((!XkbKeycodeInRange(xkb,key))||(nKeyGroups==0)) {
889 if (keysym_rtrn!=NULL)
890 *keysym_rtrn = NoSymbol;
894 syms = XkbKeySymsPtr(xkb,key);
896 /* find the offset of the effective group */
898 effectiveGroup= XkbGroupForCoreState(mods);
899 if ( effectiveGroup>=nKeyGroups ) {
900 unsigned groupInfo= XkbKeyGroupInfo(xkb,key);
901 switch (XkbOutOfRangeGroupAction(groupInfo)) {
903 effectiveGroup %= nKeyGroups;
905 case XkbClampIntoRange:
906 effectiveGroup = nKeyGroups-1;
908 case XkbRedirectIntoRange:
909 effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
910 if (effectiveGroup>=nKeyGroups)
915 col= effectiveGroup*XkbKeyGroupsWidth(xkb,key);
916 type = XkbKeyKeyType(xkb,key,effectiveGroup);
919 if (type->map) { /* find the column (shift level) within the group */
921 register XkbKTMapEntryPtr entry;
922 /* ---- Begin section modified for GDK ---- */
925 for (i=0,entry=type->map;i<type->map_count;i++,entry++) {
928 unsigned long tmp = entry->mods.mask;
934 /* We always add one-modifiers levels to mods_rtrn since
935 * they can't wipe out bits in the state unless the
936 * level would be triggered. But return other modifiers
939 if (bits == 1 || (mods&type->mods.mask)==entry->mods.mask)
940 *mods_rtrn |= entry->mods.mask;
943 if (!found&&entry->active&&((mods&type->mods.mask)==entry->mods.mask)) {
946 preserve= type->preserve[i].mask;
949 *level_rtrn = entry->level;
954 /* ---- End section modified for GDK ---- */
957 if (keysym_rtrn!=NULL)
958 *keysym_rtrn= syms[col];
960 /* ---- Begin section modified for GDK ---- */
961 *mods_rtrn &= ~preserve;
962 /* ---- End section modified for GDK ---- */
964 /* ---- Begin stuff GDK comments out of the original Xlib version ---- */
965 /* This is commented out because xkb_info is a private struct */
968 /* The Motif VTS doesn't get the help callback called if help
969 * is bound to Shift+<whatever>, and it appears as though it
970 * is XkbTranslateKeyCode that is causing the problem. The
971 * core X version of XTranslateKey always OR's in ShiftMask
972 * and LockMask for mods_rtrn, so this "fix" keeps this behavior
973 * and solves the VTS problem.
975 if ((xkb->dpy)&&(xkb->dpy->xkb_info)&&
976 (xkb->dpy->xkb_info->xlib_ctrls&XkbLC_AlwaysConsumeShiftAndLock)) { *mods_rtrn|= (ShiftMask|LockMask);
980 /* ---- End stuff GDK comments out of the original Xlib version ---- */
983 /* ---- Begin stuff GDK adds to the original Xlib version ---- */
986 *group_rtrn = effectiveGroup;
988 /* ---- End stuff GDK adds to the original Xlib version ---- */
990 return (syms[col]!=NoSymbol);
992 #endif /* HAVE_XKB */
994 /* Translates from keycode/state to keysymbol using the traditional interpretation
995 * of the keyboard map. See section 12.7 of the Xlib reference manual
998 translate_keysym (GdkKeymapX11 *keymap_x11,
999 guint hardware_keycode,
1001 GdkModifierType state,
1002 guint *effective_group,
1003 guint *effective_level)
1005 const KeySym *map = get_keymap (keymap_x11);
1006 const KeySym *syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1008 #define SYM(k,g,l) syms[KEYSYM_INDEX (k,g,l)]
1010 GdkModifierType shift_modifiers;
1013 gint num_lock_index;
1015 shift_modifiers = GDK_SHIFT_MASK;
1016 if (keymap_x11->lock_keysym == GDK_Shift_Lock)
1017 shift_modifiers |= GDK_LOCK_MASK;
1019 /* Fall back to the first group if the passed in group is empty
1021 if (!(SYM (keymap_x11, group, 0) || SYM (keymap_x11, group, 1)) &&
1022 (SYM (keymap_x11, 0, 0) || SYM (keymap_x11, 0, 1)))
1025 /* Hack: On Sun, the Num Lock modifier uses the third element in the
1026 * keysym array, and Mode_Switch does not apply for a keypad key.
1028 if (keymap_x11->sun_keypad)
1036 for (i = 0; i < keymap_x11->keysyms_per_keycode; i++)
1037 if (KEYSYM_IS_KEYPAD (SYM (keymap_x11, 0, i)))
1044 if ((state & keymap_x11->num_lock_mask) &&
1045 KEYSYM_IS_KEYPAD (SYM (keymap_x11, group, num_lock_index)))
1047 /* Shift, Shift_Lock cancel Num_Lock
1049 shift_level = (state & shift_modifiers) ? 0 : num_lock_index;
1050 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1053 tmp_keyval = SYM (keymap_x11, group, shift_level);
1057 /* Fall back to the the first level if no symbol for the level
1060 shift_level = (state & shift_modifiers) ? 1 : 0;
1061 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1064 tmp_keyval = SYM (keymap_x11, group, shift_level);
1066 if (keymap_x11->lock_keysym == GDK_Caps_Lock && (state & GDK_LOCK_MASK) != 0)
1068 guint upper = gdk_keyval_to_upper (tmp_keyval);
1069 if (upper != tmp_keyval)
1074 if (effective_group)
1075 *effective_group = group;
1077 if (effective_level)
1078 *effective_level = shift_level;
1086 * gdk_keymap_translate_keyboard_state:
1087 * @keymap: a #GdkKeymap, or %NULL to use the default
1088 * @hardware_keycode: a keycode
1089 * @state: a modifier state
1090 * @group: active keyboard group
1091 * @keyval: return location for keyval
1092 * @effective_group: return location for effective group
1093 * @level: return location for level
1094 * @consumed_modifiers: return location for modifiers that were used to determine the group or level
1097 * Translates the contents of a #GdkEventKey into a keyval, effective
1098 * group, and level. Modifiers that affected the translation and
1099 * are thus unavailable for application use are returned in
1100 * @consumed_modifiers. See gdk_keyval_get_keys() for an explanation of
1101 * groups and levels. The @effective_group is the group that was
1102 * actually used for the translation; some keys such as Enter are not
1103 * affected by the active keyboard group. The @level is derived from
1104 * @state. For convenience, #GdkEventKey already contains the translated
1105 * keyval, so this function isn't as useful as you might think.
1108 * @consumed_modifiers gives modifiers that should be masked out
1109 * from @state when comparing this key press to a hot key. For
1110 * instance, on a US keyboard, the <literal>plus</literal>
1111 * symbol is shifted, so when comparing a key press to a
1112 * <literal><Control>plus</literal> accelerator <Shift> should
1115 * <informalexample><programlisting>
1116 * /* We want to ignore irrelevant modifiers like ScrollLock */
1117 * #define ALL_ACCELS_MASK (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK)
1118 * gdk_keymap_translate_keyboard_state (keymap, event->hardware_keycode,
1119 * event->state, event->group,
1120 * &keyval, NULL, NULL, &consumed);
1121 * if (keyval == GDK_PLUS &&
1122 * (event->state & ~consumed & ALL_ACCELS_MASK) == GDK_CONTROL_MASK)
1123 * /* Control was pressed */
1124 * </programlisting></informalexample>
1126 * An older interpretation @consumed_modifiers was that it contained
1127 * all modifiers that might affect the translation of the key;
1128 * this allowed accelerators to be stored with irrelevant consumed
1129 * modifiers, by doing:</para>
1130 * <informalexample><programlisting>
1131 * /* XXX Don't do this XXX */
1132 * if (keyval == accel_keyval &&
1133 * (event->state & ~consumed & ALL_ACCELS_MASK) == (accel_mods & ~consumed))
1134 * /* Accelerator was pressed */
1135 * </programlisting></informalexample>
1137 * However, this did not work if multi-modifier combinations were
1138 * used in the keymap, since, for instance, <literal><Control></literal>
1139 * would be masked out even if only <literal><Control><Alt></literal>
1140 * was used in the keymap. To support this usage as well as well as
1141 * possible, all <emphasis>single modifier</emphasis> combinations
1142 * that could affect the key for any combination of modifiers will
1143 * be returned in @consumed_modifiers; multi-modifier combinations
1144 * are returned only when actually found in @state. When you store
1145 * accelerators, you should always store them with consumed modifiers
1146 * removed. Store <literal><Control>plus</literal>,
1147 * not <literal><Control><Shift>plus</literal>,
1150 * Return value: %TRUE if there was a keyval bound to the keycode/state/group
1153 gdk_keymap_translate_keyboard_state (GdkKeymap *keymap,
1154 guint hardware_keycode,
1155 GdkModifierType state,
1158 gint *effective_group,
1160 GdkModifierType *consumed_modifiers)
1162 GdkKeymapX11 *keymap_x11;
1163 KeySym tmp_keyval = NoSymbol;
1164 guint tmp_modifiers;
1166 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
1167 g_return_val_if_fail (group < 4, FALSE);
1169 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1173 if (effective_group)
1174 *effective_group = 0;
1177 if (consumed_modifiers)
1178 *consumed_modifiers = 0;
1180 update_keyrange (keymap_x11);
1182 if (hardware_keycode < keymap_x11->min_keycode ||
1183 hardware_keycode > keymap_x11->max_keycode)
1187 if (KEYMAP_USE_XKB (keymap))
1189 XkbDescRec *xkb = get_xkb (keymap_x11);
1191 /* replace bits 13 and 14 with the provided group */
1192 state &= ~(1 << 13 | 1 << 14);
1193 state |= group << 13;
1195 MyEnhancedXkbTranslateKeyCode (xkb,
1203 if (state & ~tmp_modifiers & LockMask)
1204 tmp_keyval = gdk_keyval_to_upper (tmp_keyval);
1206 /* We need to augment the consumed modifiers with LockMask, since
1207 * we handle that ourselves, and also with the group bits
1209 tmp_modifiers |= LockMask | 1 << 13 | 1 << 14;
1214 GdkModifierType bit;
1218 /* We see what modifiers matter by trying the translation with
1219 * and without each possible modifier
1221 for (bit = GDK_SHIFT_MASK; bit < GDK_BUTTON1_MASK; bit <<= 1)
1223 /* Handling of the group here is a bit funky; a traditional
1224 * X keyboard map can have more than two groups, but no way
1225 * of accessing the extra groups is defined. We allow a
1226 * caller to pass in any group to this function, but we
1227 * only can represent switching between group 0 and 1 in
1228 * consumed modifiers.
1230 if (translate_keysym (keymap_x11, hardware_keycode,
1231 (bit == keymap_x11->group_switch_mask) ? 0 : group,
1234 translate_keysym (keymap_x11, hardware_keycode,
1235 (bit == keymap_x11->group_switch_mask) ? 1 : group,
1238 tmp_modifiers |= bit;
1241 tmp_keyval = translate_keysym (keymap_x11, hardware_keycode,
1243 level, effective_group);
1246 if (consumed_modifiers)
1247 *consumed_modifiers = tmp_modifiers;
1250 *keyval = tmp_keyval;
1252 return tmp_keyval != NoSymbol;
1256 /* Key handling not part of the keymap */
1259 gdk_keyval_name (guint keyval)
1267 case GDK_KP_Page_Up:
1268 return "KP_Page_Up";
1269 case GDK_KP_Page_Down:
1270 return "KP_Page_Down";
1273 return XKeysymToString (keyval);
1277 gdk_keyval_from_name (const gchar *keyval_name)
1279 g_return_val_if_fail (keyval_name != NULL, 0);
1281 return XStringToKeysym (keyval_name);
1284 #ifdef HAVE_XCONVERTCASE
1286 gdk_keyval_convert_case (guint symbol,
1293 /* Check for directly encoded 24-bit UCS characters: */
1294 if ((symbol & 0xff000000) == 0x01000000)
1297 *lower = gdk_unicode_to_keyval (g_unichar_tolower (symbol & 0x00ffffff));
1299 *upper = gdk_unicode_to_keyval (g_unichar_toupper (symbol & 0x00ffffff));
1304 XConvertCase (symbol, &xlower, &xupper);
1311 #endif /* HAVE_XCONVERTCASE */
1314 _gdk_x11_get_group_for_state (GdkDisplay *display,
1315 GdkModifierType state)
1317 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1320 if (display_x11->use_xkb)
1322 return XkbGroupForCoreState (state);
1327 GdkKeymapX11 *keymap_impl = GDK_KEYMAP_X11 (gdk_keymap_get_for_display (display));
1328 update_keymaps (keymap_impl);
1329 return (state & keymap_impl->group_switch_mask) ? 1 : 0;