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.
209 * We round up the number of keysyms per keycode to the next even number,
210 * otherwise we lose a whole group of keys
212 #define KEYSYM_INDEX(keymap_impl, group, level) \
213 (2 * ((group) % (int)((keymap_impl->keysyms_per_keycode + 1) / 2)) + (level))
214 #define KEYSYM_IS_KEYPAD(s) (((s) >= 0xff80 && (s) <= 0xffbd) || \
215 ((s) >= 0x11000000 && (s) <= 0x1100ffff))
218 get_symbol (const KeySym *syms, GdkKeymapX11 *keymap_x11, int group, int level)
222 index = KEYSYM_INDEX(keymap_x11, group, level);
223 if (index > keymap_x11->keysyms_per_keycode)
230 update_keymaps (GdkKeymapX11 *keymap_x11)
232 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_KEYMAP (keymap_x11)->display);
233 Display *xdisplay = display_x11->xdisplay;
236 g_assert (!KEYMAP_USE_XKB (GDK_KEYMAP (keymap_x11)));
239 if (keymap_x11->keymap == NULL ||
240 keymap_x11->current_serial != display_x11->keymap_serial)
246 keymap_x11->current_serial = display_x11->keymap_serial;
248 update_keyrange (keymap_x11);
250 if (keymap_x11->keymap)
251 XFree (keymap_x11->keymap);
253 if (keymap_x11->mod_keymap)
254 XFreeModifiermap (keymap_x11->mod_keymap);
256 keymap_x11->keymap = XGetKeyboardMapping (xdisplay, keymap_x11->min_keycode,
257 keymap_x11->max_keycode - keymap_x11->min_keycode + 1,
258 &keymap_x11->keysyms_per_keycode);
261 /* GDK_ISO_Left_Tab, as usually configured through XKB, really messes
262 * up the whole idea of "consumed modifiers" because shift is consumed.
263 * However, <shift>Tab is not usually GDK_ISO_Left_Tab without XKB,
264 * we we fudge the map here.
266 keycode = keymap_x11->min_keycode;
267 while (keycode <= keymap_x11->max_keycode)
269 KeySym *syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
270 /* Check both groups */
271 for (i = 0 ; i < 2 ; i++)
273 if (get_symbol (syms, keymap_x11, i, 0) == GDK_Tab)
274 syms[KEYSYM_INDEX (keymap_x11, i, 1)] = GDK_ISO_Left_Tab;
278 * If there is one keysym and the key symbol has upper and lower
279 * case variants fudge the keymap
281 if (get_symbol (syms, keymap_x11, 0, 1) == 0)
286 gdk_keyval_convert_case (get_symbol (syms, keymap_x11, 0, 0), &lower, &upper);
289 syms[KEYSYM_INDEX (keymap_x11, 0, 0)] = lower;
290 syms[KEYSYM_INDEX (keymap_x11, 0, 1)] = upper;
298 keymap_x11->mod_keymap = XGetModifierMapping (xdisplay);
300 keymap_x11->lock_keysym = GDK_VoidSymbol;
301 keymap_x11->group_switch_mask = 0;
302 keymap_x11->num_lock_mask = 0;
304 /* There are 8 sets of modifiers, with each set containing
305 * max_keypermod keycodes.
307 map_size = 8 * keymap_x11->mod_keymap->max_keypermod;
308 for (i = 0; i < map_size; i++)
310 /* Get the key code at this point in the map. */
311 gint keycode = keymap_x11->mod_keymap->modifiermap[i];
316 /* Ignore invalid keycodes. */
317 if (keycode < keymap_x11->min_keycode ||
318 keycode > keymap_x11->max_keycode)
321 syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
323 /* The fourth modifier, GDK_MOD1_MASK is 1 << 3.
324 * Each group of max_keypermod entries refers to the same modifier.
326 mask = 1 << (i / keymap_x11->mod_keymap->max_keypermod);
331 /* Get the Lock keysym. If any keysym bound to the Lock modifier
332 * is Caps_Lock, we will interpret the modifier as Caps_Lock;
333 * otherwise, if any is bound to Shift_Lock, we will interpret
334 * the modifier as Shift_Lock. Otherwise, the lock modifier
337 for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
339 if (syms[j] == GDK_Caps_Lock)
340 keymap_x11->lock_keysym = GDK_Caps_Lock;
341 else if (syms[j] == GDK_Shift_Lock &&
342 keymap_x11->lock_keysym == GDK_VoidSymbol)
343 keymap_x11->lock_keysym = GDK_Shift_Lock;
347 case GDK_CONTROL_MASK:
350 /* Some keyboard maps are known to map Mode_Switch as an
351 * extra Mod1 key. In circumstances like that, it won't be
352 * used to switch groups.
357 /* Find the Mode_Switch and Num_Lock modifiers. */
358 for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
360 if (syms[j] == GDK_Mode_switch)
362 /* This modifier swaps groups */
363 keymap_x11->group_switch_mask |= mask;
365 else if (syms[j] == GDK_Num_Lock)
367 /* This modifier is used for Num_Lock */
368 keymap_x11->num_lock_mask |= mask;
375 /* Hack: The Sun X server puts the keysym to use when the Num Lock
376 * modifier is on in the third element of the keysym array, instead
379 if ((strcmp (ServerVendor (xdisplay), "Sun Microsystems, Inc.") == 0) &&
380 (keymap_x11->keysyms_per_keycode > 2))
381 keymap_x11->sun_keypad = TRUE;
383 keymap_x11->sun_keypad = FALSE;
388 get_keymap (GdkKeymapX11 *keymap_x11)
390 update_keymaps (keymap_x11);
392 return keymap_x11->keymap;
395 #define GET_EFFECTIVE_KEYMAP(keymap) get_effective_keymap ((keymap), G_STRFUNC)
398 get_effective_keymap (GdkKeymap *keymap,
399 const char *function)
404 g_message ("reverting to default display keymap in %s",
406 return gdk_keymap_get_default ();
413 static PangoDirection
414 get_direction (GdkKeymapX11 *keymap_x11)
416 XkbDescRec *xkb = get_xkb (keymap_x11);
418 XkbStateRec state_rec;
419 PangoDirection result;
421 GdkDisplay *display = GDK_KEYMAP (keymap_x11)->display;
423 XkbGetState (GDK_DISPLAY_XDISPLAY (display), XkbUseCoreKbd, &state_rec);
425 if (xkb->names->groups[state_rec.locked_group] == None)
426 result = PANGO_DIRECTION_LTR;
429 name = gdk_x11_get_xatom_name_for_display (display, xkb->names->groups[state_rec.locked_group]);
431 if (g_ascii_strcasecmp (name, "arabic") == 0 ||
432 g_ascii_strcasecmp (name, "hebrew") == 0 ||
433 g_ascii_strcasecmp (name, "israelian") == 0)
434 result = PANGO_DIRECTION_RTL;
436 result = PANGO_DIRECTION_LTR;
443 _gdk_keymap_state_changed (GdkDisplay *display)
445 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
447 if (display_x11->keymap)
449 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (display_x11->keymap);
451 PangoDirection new_direction = get_direction (keymap_x11);
453 if (!keymap_x11->have_direction || new_direction != keymap_x11->current_direction)
455 keymap_x11->have_direction = TRUE;
456 keymap_x11->current_direction = new_direction;
457 g_signal_emit_by_name (keymap_x11, "direction_changed");
462 #endif /* HAVE_XKB */
465 _gdk_keymap_keys_changed (GdkDisplay *display)
467 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
469 ++display_x11->keymap_serial;
471 if (display_x11->keymap)
472 g_signal_emit_by_name (display_x11->keymap, "keys_changed", 0);
476 gdk_keymap_get_direction (GdkKeymap *keymap)
478 keymap = GET_EFFECTIVE_KEYMAP (keymap);
481 if (KEYMAP_USE_XKB (keymap))
483 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (keymap);
485 if (!keymap_x11->have_direction)
487 keymap_x11->current_direction = get_direction (keymap_x11);
488 keymap_x11->have_direction = TRUE;
491 return keymap_x11->current_direction;
494 #endif /* HAVE_XKB */
495 return PANGO_DIRECTION_LTR;
499 * gdk_keymap_get_entries_for_keyval:
500 * @keymap: a #GdkKeymap, or %NULL to use the default keymap
501 * @keyval: a keyval, such as %GDK_a, %GDK_Up, %GDK_Return, etc.
502 * @keys: return location for an array of #GdkKeymapKey
503 * @n_keys: return location for number of elements in returned array
505 * Obtains a list of keycode/group/level combinations that will
506 * generate @keyval. Groups and levels are two kinds of keyboard mode;
507 * in general, the level determines whether the top or bottom symbol
508 * on a key is used, and the group determines whether the left or
509 * right symbol is used. On US keyboards, the shift key changes the
510 * keyboard level, and there are no groups. A group switch key might
511 * convert a keyboard between Hebrew to English modes, for example.
512 * #GdkEventKey contains a %group field that indicates the active
513 * keyboard group. The level is computed from the modifier mask.
514 * The returned array should be freed
517 * Return value: %TRUE if keys were found and returned
520 gdk_keymap_get_entries_for_keyval (GdkKeymap *keymap,
526 GdkKeymapX11 *keymap_x11;
528 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
529 g_return_val_if_fail (keys != NULL, FALSE);
530 g_return_val_if_fail (n_keys != NULL, FALSE);
531 g_return_val_if_fail (keyval != 0, FALSE);
533 keymap = GET_EFFECTIVE_KEYMAP (keymap);
534 keymap_x11 = GDK_KEYMAP_X11 (keymap);
536 retval = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
539 if (KEYMAP_USE_XKB (keymap))
541 /* See sec 15.3.4 in XKB docs */
543 XkbDescRec *xkb = get_xkb (keymap_x11);
546 keycode = keymap_x11->min_keycode;
548 while (keycode <= keymap_x11->max_keycode)
550 gint max_shift_levels = XkbKeyGroupsWidth (xkb, keycode); /* "key width" */
553 gint total_syms = XkbKeyNumSyms (xkb, keycode);
557 /* entry is an array with all syms for group 0, all
558 * syms for group 1, etc. and for each group the
559 * shift level syms are in order
561 entry = XkbKeySymsPtr (xkb, keycode);
563 while (i < total_syms)
565 /* check out our cool loop invariant */
566 g_assert (i == (group * max_shift_levels + level));
568 if (entry[i] == keyval)
573 key.keycode = keycode;
577 g_array_append_val (retval, key);
579 g_assert (XkbKeySymEntry (xkb, keycode, level, group) ==
585 if (level == max_shift_levels)
600 const KeySym *map = get_keymap (keymap_x11);
603 keycode = keymap_x11->min_keycode;
604 while (keycode <= keymap_x11->max_keycode)
606 const KeySym *syms = map + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
609 while (i < keymap_x11->keysyms_per_keycode)
611 if (syms[i] == keyval)
616 key.keycode = keycode;
618 /* The "classic" non-XKB keymap has 2 levels per group */
622 g_array_append_val (retval, key);
634 *keys = (GdkKeymapKey*) retval->data;
635 *n_keys = retval->len;
643 g_array_free (retval, retval->len > 0 ? FALSE : TRUE);
649 * gdk_keymap_get_entries_for_keycode:
650 * @keymap: a #GdkKeymap or %NULL to use the default keymap
651 * @hardware_keycode: a keycode
652 * @keys: return location for array of #GdkKeymapKey, or NULL
653 * @keyvals: return location for array of keyvals, or NULL
654 * @n_entries: length of @keys and @keyvals
656 * Returns the keyvals bound to @hardware_keycode.
657 * The Nth #GdkKeymapKey in @keys is bound to the Nth
658 * keyval in @keyvals. Free the returned arrays with g_free().
659 * When a keycode is pressed by the user, the keyval from
660 * this list of entries is selected by considering the effective
661 * keyboard group and level. See gdk_keymap_translate_keyboard_state().
663 * Returns: %TRUE if there were any entries
666 gdk_keymap_get_entries_for_keycode (GdkKeymap *keymap,
667 guint hardware_keycode,
672 GdkKeymapX11 *keymap_x11;
675 GArray *keyval_array;
677 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
678 g_return_val_if_fail (n_entries != NULL, FALSE);
680 keymap = GET_EFFECTIVE_KEYMAP (keymap);
681 keymap_x11 = GDK_KEYMAP_X11 (keymap);
683 update_keyrange (keymap_x11);
685 if (hardware_keycode < keymap_x11->min_keycode ||
686 hardware_keycode > keymap_x11->max_keycode)
698 key_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
703 keyval_array = g_array_new (FALSE, FALSE, sizeof (guint));
708 if (KEYMAP_USE_XKB (keymap))
710 /* See sec 15.3.4 in XKB docs */
712 XkbDescRec *xkb = get_xkb (keymap_x11);
713 gint max_shift_levels;
720 max_shift_levels = XkbKeyGroupsWidth (xkb, hardware_keycode); /* "key width" */
721 total_syms = XkbKeyNumSyms (xkb, hardware_keycode);
723 /* entry is an array with all syms for group 0, all
724 * syms for group 1, etc. and for each group the
725 * shift level syms are in order
727 entry = XkbKeySymsPtr (xkb, hardware_keycode);
729 while (i < total_syms)
731 /* check out our cool loop invariant */
732 g_assert (i == (group * max_shift_levels + level));
738 key.keycode = hardware_keycode;
742 g_array_append_val (key_array, key);
746 g_array_append_val (keyval_array, entry[i]);
750 if (level == max_shift_levels)
762 const KeySym *map = get_keymap (keymap_x11);
766 syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
768 while (i < keymap_x11->keysyms_per_keycode)
774 key.keycode = hardware_keycode;
776 /* The "classic" non-XKB keymap has 2 levels per group */
780 g_array_append_val (key_array, key);
784 g_array_append_val (keyval_array, syms[i]);
790 if ((key_array && key_array->len > 0) ||
791 (keyval_array && keyval_array->len > 0))
794 *keys = (GdkKeymapKey*) key_array->data;
797 *keyvals = (guint*) keyval_array->data;
800 *n_entries = key_array->len;
802 *n_entries = keyval_array->len;
816 g_array_free (key_array, key_array->len > 0 ? FALSE : TRUE);
818 g_array_free (keyval_array, keyval_array->len > 0 ? FALSE : TRUE);
820 return *n_entries > 0;
825 * gdk_keymap_lookup_key:
826 * @keymap: a #GdkKeymap or %NULL to use the default keymap
827 * @key: a #GdkKeymapKey with keycode, group, and level initialized
829 * Looks up the keyval mapped to a keycode/group/level triplet.
830 * If no keyval is bound to @key, returns 0. For normal user input,
831 * you want to use gdk_keymap_translate_keyboard_state() instead of
832 * this function, since the effective group/level may not be
833 * the same as the current keyboard state.
835 * Return value: a keyval, or 0 if none was mapped to the given @key
838 gdk_keymap_lookup_key (GdkKeymap *keymap,
839 const GdkKeymapKey *key)
841 GdkKeymapX11 *keymap_x11;
843 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), 0);
844 g_return_val_if_fail (key != NULL, 0);
845 g_return_val_if_fail (key->group < 4, 0);
847 keymap = GET_EFFECTIVE_KEYMAP (keymap);
848 keymap_x11 = GDK_KEYMAP_X11 (keymap);
851 if (KEYMAP_USE_XKB (keymap))
853 XkbDescRec *xkb = get_xkb (keymap_x11);
855 return XkbKeySymEntry (xkb, key->keycode, key->level, key->group);
860 const KeySym *map = get_keymap (keymap_x11);
861 const KeySym *syms = map + (key->keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
862 return get_symbol (syms, keymap_x11, key->group, key->level);
867 /* This is copied straight from XFree86 Xlib, to:
868 * - add the group and level return.
869 * - change the interpretation of mods_rtrn as described
870 * in the docs for gdk_keymap_translate_keyboard_state()
871 * It's unchanged for ease of diff against the Xlib sources; don't
875 MyEnhancedXkbTranslateKeyCode(register XkbDescPtr xkb,
877 register unsigned int mods,
878 unsigned int * mods_rtrn,
879 KeySym * keysym_rtrn,
880 unsigned int * group_rtrn,
881 unsigned int * level_rtrn)
885 unsigned preserve,effectiveGroup;
891 nKeyGroups= XkbKeyNumGroups(xkb,key);
892 if ((!XkbKeycodeInRange(xkb,key))||(nKeyGroups==0)) {
893 if (keysym_rtrn!=NULL)
894 *keysym_rtrn = NoSymbol;
898 syms = XkbKeySymsPtr(xkb,key);
900 /* find the offset of the effective group */
902 effectiveGroup= XkbGroupForCoreState(mods);
903 if ( effectiveGroup>=nKeyGroups ) {
904 unsigned groupInfo= XkbKeyGroupInfo(xkb,key);
905 switch (XkbOutOfRangeGroupAction(groupInfo)) {
907 effectiveGroup %= nKeyGroups;
909 case XkbClampIntoRange:
910 effectiveGroup = nKeyGroups-1;
912 case XkbRedirectIntoRange:
913 effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
914 if (effectiveGroup>=nKeyGroups)
919 col= effectiveGroup*XkbKeyGroupsWidth(xkb,key);
920 type = XkbKeyKeyType(xkb,key,effectiveGroup);
923 if (type->map) { /* find the column (shift level) within the group */
925 register XkbKTMapEntryPtr entry;
926 /* ---- Begin section modified for GDK ---- */
929 for (i=0,entry=type->map;i<type->map_count;i++,entry++) {
932 unsigned long tmp = entry->mods.mask;
938 /* We always add one-modifiers levels to mods_rtrn since
939 * they can't wipe out bits in the state unless the
940 * level would be triggered. But return other modifiers
943 if (bits == 1 || (mods&type->mods.mask)==entry->mods.mask)
944 *mods_rtrn |= entry->mods.mask;
947 if (!found&&entry->active&&((mods&type->mods.mask)==entry->mods.mask)) {
950 preserve= type->preserve[i].mask;
953 *level_rtrn = entry->level;
958 /* ---- End section modified for GDK ---- */
961 if (keysym_rtrn!=NULL)
962 *keysym_rtrn= syms[col];
964 /* ---- Begin section modified for GDK ---- */
965 *mods_rtrn &= ~preserve;
966 /* ---- End section modified for GDK ---- */
968 /* ---- Begin stuff GDK comments out of the original Xlib version ---- */
969 /* This is commented out because xkb_info is a private struct */
972 /* The Motif VTS doesn't get the help callback called if help
973 * is bound to Shift+<whatever>, and it appears as though it
974 * is XkbTranslateKeyCode that is causing the problem. The
975 * core X version of XTranslateKey always OR's in ShiftMask
976 * and LockMask for mods_rtrn, so this "fix" keeps this behavior
977 * and solves the VTS problem.
979 if ((xkb->dpy)&&(xkb->dpy->xkb_info)&&
980 (xkb->dpy->xkb_info->xlib_ctrls&XkbLC_AlwaysConsumeShiftAndLock)) { *mods_rtrn|= (ShiftMask|LockMask);
984 /* ---- End stuff GDK comments out of the original Xlib version ---- */
987 /* ---- Begin stuff GDK adds to the original Xlib version ---- */
990 *group_rtrn = effectiveGroup;
992 /* ---- End stuff GDK adds to the original Xlib version ---- */
994 return (syms[col]!=NoSymbol);
996 #endif /* HAVE_XKB */
998 /* Translates from keycode/state to keysymbol using the traditional interpretation
999 * of the keyboard map. See section 12.7 of the Xlib reference manual
1002 translate_keysym (GdkKeymapX11 *keymap_x11,
1003 guint hardware_keycode,
1005 GdkModifierType state,
1006 guint *effective_group,
1007 guint *effective_level)
1009 const KeySym *map = get_keymap (keymap_x11);
1010 const KeySym *syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1012 #define SYM(k,g,l) get_symbol (syms, k,g,l)
1014 GdkModifierType shift_modifiers;
1017 gint num_lock_index;
1019 shift_modifiers = GDK_SHIFT_MASK;
1020 if (keymap_x11->lock_keysym == GDK_Shift_Lock)
1021 shift_modifiers |= GDK_LOCK_MASK;
1023 /* Fall back to the first group if the passed in group is empty
1025 if (!(SYM (keymap_x11, group, 0) || SYM (keymap_x11, group, 1)) &&
1026 (SYM (keymap_x11, 0, 0) || SYM (keymap_x11, 0, 1)))
1029 /* Hack: On Sun, the Num Lock modifier uses the third element in the
1030 * keysym array, and Mode_Switch does not apply for a keypad key.
1032 if (keymap_x11->sun_keypad)
1040 for (i = 0; i < keymap_x11->keysyms_per_keycode; i++)
1041 if (KEYSYM_IS_KEYPAD (SYM (keymap_x11, 0, i)))
1048 if ((state & keymap_x11->num_lock_mask) &&
1049 KEYSYM_IS_KEYPAD (SYM (keymap_x11, group, num_lock_index)))
1051 /* Shift, Shift_Lock cancel Num_Lock
1053 shift_level = (state & shift_modifiers) ? 0 : num_lock_index;
1054 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1057 tmp_keyval = SYM (keymap_x11, group, shift_level);
1061 /* Fall back to the the first level if no symbol for the level
1064 shift_level = (state & shift_modifiers) ? 1 : 0;
1065 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1068 tmp_keyval = SYM (keymap_x11, group, shift_level);
1070 if (keymap_x11->lock_keysym == GDK_Caps_Lock && (state & GDK_LOCK_MASK) != 0)
1072 guint upper = gdk_keyval_to_upper (tmp_keyval);
1073 if (upper != tmp_keyval)
1078 if (effective_group)
1079 *effective_group = group;
1081 if (effective_level)
1082 *effective_level = shift_level;
1090 * gdk_keymap_translate_keyboard_state:
1091 * @keymap: a #GdkKeymap, or %NULL to use the default
1092 * @hardware_keycode: a keycode
1093 * @state: a modifier state
1094 * @group: active keyboard group
1095 * @keyval: return location for keyval
1096 * @effective_group: return location for effective group
1097 * @level: return location for level
1098 * @consumed_modifiers: return location for modifiers that were used to determine the group or level
1101 * Translates the contents of a #GdkEventKey into a keyval, effective
1102 * group, and level. Modifiers that affected the translation and
1103 * are thus unavailable for application use are returned in
1104 * @consumed_modifiers. See gdk_keyval_get_keys() for an explanation of
1105 * groups and levels. The @effective_group is the group that was
1106 * actually used for the translation; some keys such as Enter are not
1107 * affected by the active keyboard group. The @level is derived from
1108 * @state. For convenience, #GdkEventKey already contains the translated
1109 * keyval, so this function isn't as useful as you might think.
1112 * @consumed_modifiers gives modifiers that should be masked out
1113 * from @state when comparing this key press to a hot key. For
1114 * instance, on a US keyboard, the <literal>plus</literal>
1115 * symbol is shifted, so when comparing a key press to a
1116 * <literal><Control>plus</literal> accelerator <Shift> should
1119 * <informalexample><programlisting>
1120 * /* We want to ignore irrelevant modifiers like ScrollLock */
1121 * #define ALL_ACCELS_MASK (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK)
1122 * gdk_keymap_translate_keyboard_state (keymap, event->hardware_keycode,
1123 * event->state, event->group,
1124 * &keyval, NULL, NULL, &consumed);
1125 * if (keyval == GDK_PLUS &&
1126 * (event->state & ~consumed & ALL_ACCELS_MASK) == GDK_CONTROL_MASK)
1127 * /* Control was pressed */
1128 * </programlisting></informalexample>
1130 * An older interpretation @consumed_modifiers was that it contained
1131 * all modifiers that might affect the translation of the key;
1132 * this allowed accelerators to be stored with irrelevant consumed
1133 * modifiers, by doing:</para>
1134 * <informalexample><programlisting>
1135 * /* XXX Don't do this XXX */
1136 * if (keyval == accel_keyval &&
1137 * (event->state & ~consumed & ALL_ACCELS_MASK) == (accel_mods & ~consumed))
1138 * /* Accelerator was pressed */
1139 * </programlisting></informalexample>
1141 * However, this did not work if multi-modifier combinations were
1142 * used in the keymap, since, for instance, <literal><Control></literal>
1143 * would be masked out even if only <literal><Control><Alt></literal>
1144 * was used in the keymap. To support this usage as well as well as
1145 * possible, all <emphasis>single modifier</emphasis> combinations
1146 * that could affect the key for any combination of modifiers will
1147 * be returned in @consumed_modifiers; multi-modifier combinations
1148 * are returned only when actually found in @state. When you store
1149 * accelerators, you should always store them with consumed modifiers
1150 * removed. Store <literal><Control>plus</literal>,
1151 * not <literal><Control><Shift>plus</literal>,
1154 * Return value: %TRUE if there was a keyval bound to the keycode/state/group
1157 gdk_keymap_translate_keyboard_state (GdkKeymap *keymap,
1158 guint hardware_keycode,
1159 GdkModifierType state,
1162 gint *effective_group,
1164 GdkModifierType *consumed_modifiers)
1166 GdkKeymapX11 *keymap_x11;
1167 KeySym tmp_keyval = NoSymbol;
1168 guint tmp_modifiers;
1170 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
1171 g_return_val_if_fail (group < 4, FALSE);
1173 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1174 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1178 if (effective_group)
1179 *effective_group = 0;
1182 if (consumed_modifiers)
1183 *consumed_modifiers = 0;
1185 update_keyrange (keymap_x11);
1187 if (hardware_keycode < keymap_x11->min_keycode ||
1188 hardware_keycode > keymap_x11->max_keycode)
1192 if (KEYMAP_USE_XKB (keymap))
1194 XkbDescRec *xkb = get_xkb (keymap_x11);
1196 /* replace bits 13 and 14 with the provided group */
1197 state &= ~(1 << 13 | 1 << 14);
1198 state |= group << 13;
1200 MyEnhancedXkbTranslateKeyCode (xkb,
1208 if (state & ~tmp_modifiers & LockMask)
1209 tmp_keyval = gdk_keyval_to_upper (tmp_keyval);
1211 /* We need to augment the consumed modifiers with LockMask, since
1212 * we handle that ourselves, and also with the group bits
1214 tmp_modifiers |= LockMask | 1 << 13 | 1 << 14;
1219 GdkModifierType bit;
1223 /* We see what modifiers matter by trying the translation with
1224 * and without each possible modifier
1226 for (bit = GDK_SHIFT_MASK; bit < GDK_BUTTON1_MASK; bit <<= 1)
1228 /* Handling of the group here is a bit funky; a traditional
1229 * X keyboard map can have more than two groups, but no way
1230 * of accessing the extra groups is defined. We allow a
1231 * caller to pass in any group to this function, but we
1232 * only can represent switching between group 0 and 1 in
1233 * consumed modifiers.
1235 if (translate_keysym (keymap_x11, hardware_keycode,
1236 (bit == keymap_x11->group_switch_mask) ? 0 : group,
1239 translate_keysym (keymap_x11, hardware_keycode,
1240 (bit == keymap_x11->group_switch_mask) ? 1 : group,
1243 tmp_modifiers |= bit;
1246 tmp_keyval = translate_keysym (keymap_x11, hardware_keycode,
1248 level, effective_group);
1251 if (consumed_modifiers)
1252 *consumed_modifiers = tmp_modifiers;
1255 *keyval = tmp_keyval;
1257 return tmp_keyval != NoSymbol;
1261 /* Key handling not part of the keymap */
1264 gdk_keyval_name (guint keyval)
1272 case GDK_KP_Page_Up:
1273 return "KP_Page_Up";
1274 case GDK_KP_Page_Down:
1275 return "KP_Page_Down";
1278 return XKeysymToString (keyval);
1282 gdk_keyval_from_name (const gchar *keyval_name)
1284 g_return_val_if_fail (keyval_name != NULL, 0);
1286 return XStringToKeysym (keyval_name);
1289 #ifdef HAVE_XCONVERTCASE
1291 gdk_keyval_convert_case (guint symbol,
1298 /* Check for directly encoded 24-bit UCS characters: */
1299 if ((symbol & 0xff000000) == 0x01000000)
1302 *lower = gdk_unicode_to_keyval (g_unichar_tolower (symbol & 0x00ffffff));
1304 *upper = gdk_unicode_to_keyval (g_unichar_toupper (symbol & 0x00ffffff));
1309 XConvertCase (symbol, &xlower, &xupper);
1316 #endif /* HAVE_XCONVERTCASE */
1319 _gdk_x11_get_group_for_state (GdkDisplay *display,
1320 GdkModifierType state)
1322 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1325 if (display_x11->use_xkb)
1327 return XkbGroupForCoreState (state);
1332 GdkKeymapX11 *keymap_impl = GDK_KEYMAP_X11 (gdk_keymap_get_for_display (display));
1333 update_keymaps (keymap_impl);
1334 return (state & keymap_impl->group_switch_mask) ? 1 : 0;