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/.
37 #include "gdkprivate-x11.h"
38 #include "gdkinternals.h"
39 #include "gdkdisplay-x11.h"
40 #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;
70 GdkModifierType group_switch_mask;
71 PangoDirection current_direction;
72 gboolean have_direction;
80 #define KEYMAP_USE_XKB(keymap) GDK_DISPLAY_X11 ((keymap)->display)->use_xkb
81 #define KEYMAP_XDISPLAY(keymap) GDK_DISPLAY_XDISPLAY ((keymap)->display)
83 static GType gdk_keymap_x11_get_type (void);
84 static void gdk_keymap_x11_init (GdkKeymapX11 *keymap);
87 gdk_keymap_x11_get_type (void)
89 static GType object_type = 0;
93 static const GTypeInfo object_info =
95 sizeof (GdkKeymapClass),
97 (GBaseFinalizeFunc) NULL,
98 (GClassInitFunc) NULL,
99 NULL, /* class_finalize */
100 NULL, /* class_data */
101 sizeof (GdkKeymapX11),
103 (GInstanceInitFunc) gdk_keymap_x11_init,
106 object_type = g_type_register_static (GDK_TYPE_KEYMAP,
115 gdk_keymap_x11_init (GdkKeymapX11 *keymap)
117 keymap->min_keycode = 0;
118 keymap->max_keycode = 0;
120 keymap->keymap = NULL;
121 keymap->keysyms_per_keycode = 0;
122 keymap->mod_keymap = NULL;
124 keymap->group_switch_mask = 0;
125 keymap->have_direction = FALSE;
128 keymap->xkb_desc = NULL;
131 keymap->current_serial = 0;
135 update_keyrange (GdkKeymapX11 *keymap_x11)
137 if (keymap_x11->max_keycode == 0)
138 XDisplayKeycodes (KEYMAP_XDISPLAY (GDK_KEYMAP (keymap_x11)),
139 &keymap_x11->min_keycode, &keymap_x11->max_keycode);
145 get_xkb (GdkKeymapX11 *keymap_x11)
147 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_KEYMAP (keymap_x11)->display);
148 Display *xdisplay = display_x11->xdisplay;
150 update_keyrange (keymap_x11);
152 if (keymap_x11->xkb_desc == NULL)
154 keymap_x11->xkb_desc = XkbGetMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask, XkbUseCoreKbd);
155 if (keymap_x11->xkb_desc == NULL)
156 g_error ("Failed to get keymap");
158 XkbGetNames (xdisplay, XkbGroupNamesMask, keymap_x11->xkb_desc);
160 else if (keymap_x11->current_serial != display_x11->keymap_serial)
162 XkbGetUpdatedMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask,
163 keymap_x11->xkb_desc);
164 XkbGetNames (xdisplay, XkbGroupNamesMask, keymap_x11->xkb_desc);
167 keymap_x11->current_serial = display_x11->keymap_serial;
169 return keymap_x11->xkb_desc;
171 #endif /* HAVE_XKB */
173 /* Whether we were able to turn on detectable-autorepeat using
174 * XkbSetDetectableAutorepeat. If FALSE, we'll fall back
175 * to checking the next event with XPending().
179 * gdk_keymap_get_for_display:
180 * @display: the #GdkDisplay.
181 * @returns: the #GdkKeymap attached to @display.
183 * Returns the #GdkKeymap attached to @display.
186 gdk_keymap_get_for_display (GdkDisplay *display)
188 GdkDisplayX11 *display_x11;
189 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
190 display_x11 = GDK_DISPLAY_X11 (display);
192 if (!display_x11->keymap)
193 display_x11->keymap = g_object_new (gdk_keymap_x11_get_type (), NULL);
195 display_x11->keymap->display = display;
197 return display_x11->keymap;
200 /* Find the index of the group/level pair within the keysyms for a key.
202 #define KEYSYM_INDEX(keymap_impl, group, level) \
203 (2 * ((group) % (keymap_impl->keysyms_per_keycode / 2)) + (level))
206 update_keymaps (GdkKeymapX11 *keymap_x11)
208 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_KEYMAP (keymap_x11)->display);
209 Display *xdisplay = display_x11->xdisplay;
212 g_assert (!KEYMAP_USE_XKB (GDK_KEYMAP (keymap_x11)));
215 if (keymap_x11->keymap == NULL ||
216 keymap_x11->current_serial != display_x11->keymap_serial)
222 keymap_x11->current_serial = display_x11->keymap_serial;
224 update_keyrange (keymap_x11);
226 if (keymap_x11->keymap)
227 XFree (keymap_x11->keymap);
229 if (keymap_x11->mod_keymap)
230 XFreeModifiermap (keymap_x11->mod_keymap);
232 keymap_x11->keymap = XGetKeyboardMapping (xdisplay, keymap_x11->min_keycode,
233 keymap_x11->max_keycode - keymap_x11->min_keycode + 1,
234 &keymap_x11->keysyms_per_keycode);
237 /* GDK_ISO_Left_Tab, as usually configured through XKB, really messes
238 * up the whole idea of "consumed modifiers" because shift is consumed.
239 * However, <shift>Tab is not usually GDK_ISO_Left_Tab without XKB,
240 * we we fudge the map here.
242 keycode = keymap_x11->min_keycode;
243 while (keycode <= keymap_x11->max_keycode)
245 KeySym *syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
246 /* Check both groups */
247 for (i = 0 ; i < 2 ; i++)
249 if (syms[KEYSYM_INDEX (keymap_x11, i, 0)] == GDK_Tab)
250 syms[KEYSYM_INDEX (keymap_x11, i, 1)] = GDK_ISO_Left_Tab;
254 * If there is one keysym and the key symbol has upper and lower
255 * case variants fudge the keymap
257 if (syms[KEYSYM_INDEX (keymap_x11, 0, 1)] == 0)
262 gdk_keyval_convert_case (syms[KEYSYM_INDEX (keymap_x11, 0, 0)], &lower, &upper);
265 syms[KEYSYM_INDEX (keymap_x11, 0, 0)] = lower;
266 syms[KEYSYM_INDEX (keymap_x11, 0, 1)] = upper;
274 keymap_x11->mod_keymap = XGetModifierMapping (xdisplay);
277 keymap_x11->group_switch_mask = 0;
279 /* there are 8 modifiers, and the first 3 are shift, shift lock,
282 map_size = 8 * keymap_x11->mod_keymap->max_keypermod;
283 i = 3 * keymap_x11->mod_keymap->max_keypermod;
286 /* get the key code at this point in the map,
287 * see if its keysym is GDK_Mode_switch, if so
288 * we have the mode key
290 gint keycode = keymap_x11->mod_keymap->modifiermap[i];
292 if (keycode >= keymap_x11->min_keycode &&
293 keycode <= keymap_x11->max_keycode)
296 KeySym *syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
297 /* GDK_MOD1_MASK is 1 << 3 for example, i.e. the
298 * fourth modifier, i / keyspermod is the modifier
301 guint mask = 1 << ( i / keymap_x11->mod_keymap->max_keypermod);
303 /* Some keyboard maps are known to map Mode_Switch as an extra
304 * Mod1 key. In circumstances like that, it won't be used to
307 if (mask == GDK_CONTROL_MASK || mask == GDK_SHIFT_MASK ||
308 mask == GDK_LOCK_MASK || mask == GDK_MOD1_MASK)
311 while (j < keymap_x11->keysyms_per_keycode)
313 if (syms[j] == GDK_Mode_switch)
315 /* This modifier swaps groups */
317 keymap_x11->group_switch_mask |= mask;
332 get_keymap (GdkKeymapX11 *keymap_x11)
334 update_keymaps (keymap_x11);
336 return keymap_x11->keymap;
340 static PangoDirection
341 get_direction (GdkKeymapX11 *keymap_x11)
343 XkbDescRec *xkb = get_xkb (keymap_x11);
345 XkbStateRec state_rec;
346 PangoDirection result;
348 GdkDisplay *display = GDK_KEYMAP (keymap_x11)->display;
350 XkbGetState (GDK_DISPLAY_XDISPLAY (display), XkbUseCoreKbd, &state_rec);
352 if (xkb->names->groups[state_rec.locked_group] == None)
353 result = PANGO_DIRECTION_LTR;
356 name = gdk_x11_get_xatom_name_for_display (display, xkb->names->groups[state_rec.locked_group]);
358 if (g_ascii_strcasecmp (name, "arabic") == 0 ||
359 g_ascii_strcasecmp (name, "hebrew") == 0 ||
360 g_ascii_strcasecmp (name, "israelian") == 0)
361 result = PANGO_DIRECTION_RTL;
363 result = PANGO_DIRECTION_LTR;
370 _gdk_keymap_state_changed (GdkDisplay *display)
372 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
374 if (display_x11->keymap)
376 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (display_x11->keymap);
378 PangoDirection new_direction = get_direction (keymap_x11);
380 if (!keymap_x11->have_direction || new_direction != keymap_x11->current_direction)
382 keymap_x11->have_direction = TRUE;
383 keymap_x11->current_direction = new_direction;
384 g_signal_emit_by_name (keymap_x11, "direction_changed");
389 #endif /* HAVE_XKB */
392 _gdk_keymap_keys_changed (GdkDisplay *display)
394 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
396 ++display_x11->keymap_serial;
398 if (display_x11->keymap)
399 g_signal_emit_by_name (display_x11->keymap, "keys_changed", 0);
403 gdk_keymap_get_direction (GdkKeymap *keymap)
407 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
409 g_message ("_multihead : reverting to default display keymap "
410 "in gdk_keymap_get_direction"));
414 if (KEYMAP_USE_XKB (keymap))
416 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (keymap);
418 if (!keymap_x11->have_direction)
420 keymap_x11->current_direction = get_direction (keymap_x11);
421 keymap_x11->have_direction = TRUE;
424 return keymap_x11->current_direction;
427 #endif /* HAVE_XKB */
428 return PANGO_DIRECTION_LTR;
432 * gdk_keymap_get_entries_for_keyval:
433 * @keymap: a #GdkKeymap, or %NULL to use the default keymap
434 * @keyval: a keyval, such as %GDK_a, %GDK_Up, %GDK_Return, etc.
435 * @keys: return location for an array of #GdkKeymapKey
436 * @n_keys: return location for number of elements in returned array
438 * Obtains a list of keycode/group/level combinations that will
439 * generate @keyval. Groups and levels are two kinds of keyboard mode;
440 * in general, the level determines whether the top or bottom symbol
441 * on a key is used, and the group determines whether the left or
442 * right symbol is used. On US keyboards, the shift key changes the
443 * keyboard level, and there are no groups. A group switch key might
444 * convert a keyboard between Hebrew to English modes, for example.
445 * #GdkEventKey contains a %group field that indicates the active
446 * keyboard group. The level is computed from the modifier mask.
447 * The returned array should be freed
450 * Return value: %TRUE if keys were found and returned
453 gdk_keymap_get_entries_for_keyval (GdkKeymap *keymap,
459 GdkKeymapX11 *keymap_x11;
461 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
462 g_return_val_if_fail (keys != NULL, FALSE);
463 g_return_val_if_fail (n_keys != NULL, FALSE);
464 g_return_val_if_fail (keyval != 0, FALSE);
468 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
470 g_message ("_multihead : reverting to default display keymap "
471 "in gdk_keymap_get_entries_for_keyval\n"));
474 keymap_x11 = GDK_KEYMAP_X11 (keymap);
476 retval = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
479 if (KEYMAP_USE_XKB (keymap))
481 /* See sec 15.3.4 in XKB docs */
483 XkbDescRec *xkb = get_xkb (keymap_x11);
486 keycode = keymap_x11->min_keycode;
488 while (keycode <= keymap_x11->max_keycode)
490 gint max_shift_levels = XkbKeyGroupsWidth (xkb, keycode); /* "key width" */
493 gint total_syms = XkbKeyNumSyms (xkb, keycode);
497 /* entry is an array with all syms for group 0, all
498 * syms for group 1, etc. and for each group the
499 * shift level syms are in order
501 entry = XkbKeySymsPtr (xkb, keycode);
503 while (i < total_syms)
505 /* check out our cool loop invariant */
506 g_assert (i == (group * max_shift_levels + level));
508 if (entry[i] == keyval)
513 key.keycode = keycode;
517 g_array_append_val (retval, key);
519 g_assert (XkbKeySymEntry (xkb, keycode, level, group) ==
525 if (level == max_shift_levels)
540 const KeySym *map = get_keymap (keymap_x11);
543 keycode = keymap_x11->min_keycode;
544 while (keycode <= keymap_x11->max_keycode)
546 const KeySym *syms = map + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
549 while (i < keymap_x11->keysyms_per_keycode)
551 if (syms[i] == keyval)
556 key.keycode = keycode;
558 /* The "classic" non-XKB keymap has 2 levels per group */
562 g_array_append_val (retval, key);
574 *keys = (GdkKeymapKey*) retval->data;
575 *n_keys = retval->len;
583 g_array_free (retval, retval->len > 0 ? FALSE : TRUE);
589 * gdk_keymap_get_entries_for_keycode:
590 * @keymap: a #GdkKeymap or %NULL to use the default keymap
591 * @hardware_keycode: a keycode
592 * @keys: return location for array of #GdkKeymapKey, or NULL
593 * @keyvals: return location for array of keyvals, or NULL
594 * @n_entries: length of @keys and @keyvals
596 * Returns the keyvals bound to @hardware_keycode.
597 * The Nth #GdkKeymapKey in @keys is bound to the Nth
598 * keyval in @keyvals. Free the returned arrays with g_free().
599 * When a keycode is pressed by the user, the keyval from
600 * this list of entries is selected by considering the effective
601 * keyboard group and level. See gdk_keymap_translate_keyboard_state().
603 * Returns: %TRUE if there were any entries
606 gdk_keymap_get_entries_for_keycode (GdkKeymap *keymap,
607 guint hardware_keycode,
612 GdkKeymapX11 *keymap_x11;
615 GArray *keyval_array;
617 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
618 g_return_val_if_fail (n_entries != NULL, FALSE);
622 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
624 g_message ("_multihead : reverting to default display keymap "
625 "in gdk_keymap_get_entries_for_keycode\n"));
628 keymap_x11 = GDK_KEYMAP_X11 (keymap);
630 update_keyrange (keymap_x11);
632 if (hardware_keycode < keymap_x11->min_keycode ||
633 hardware_keycode > keymap_x11->max_keycode)
645 key_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
650 keyval_array = g_array_new (FALSE, FALSE, sizeof (guint));
655 if (KEYMAP_USE_XKB (keymap))
657 /* See sec 15.3.4 in XKB docs */
659 XkbDescRec *xkb = get_xkb (keymap_x11);
660 gint max_shift_levels;
667 max_shift_levels = XkbKeyGroupsWidth (xkb, hardware_keycode); /* "key width" */
668 total_syms = XkbKeyNumSyms (xkb, hardware_keycode);
670 /* entry is an array with all syms for group 0, all
671 * syms for group 1, etc. and for each group the
672 * shift level syms are in order
674 entry = XkbKeySymsPtr (xkb, hardware_keycode);
676 while (i < total_syms)
678 /* check out our cool loop invariant */
679 g_assert (i == (group * max_shift_levels + level));
685 key.keycode = hardware_keycode;
689 g_array_append_val (key_array, key);
693 g_array_append_val (keyval_array, entry[i]);
697 if (level == max_shift_levels)
709 const KeySym *map = get_keymap (keymap_x11);
713 syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
715 while (i < keymap_x11->keysyms_per_keycode)
721 key.keycode = hardware_keycode;
723 /* The "classic" non-XKB keymap has 2 levels per group */
727 g_array_append_val (key_array, key);
731 g_array_append_val (keyval_array, syms[i]);
737 if ((key_array && key_array->len > 0) ||
738 (keyval_array && keyval_array->len > 0))
741 *keys = (GdkKeymapKey*) key_array->data;
744 *keyvals = (guint*) keyval_array->data;
747 *n_entries = key_array->len;
749 *n_entries = keyval_array->len;
763 g_array_free (key_array, key_array->len > 0 ? FALSE : TRUE);
765 g_array_free (keyval_array, keyval_array->len > 0 ? FALSE : TRUE);
767 return *n_entries > 0;
772 * gdk_keymap_lookup_key:
773 * @keymap: a #GdkKeymap or %NULL to use the default keymap
774 * @key: a #GdkKeymapKey with keycode, group, and level initialized
776 * Looks up the keyval mapped to a keycode/group/level triplet.
777 * If no keyval is bound to @key, returns 0. For normal user input,
778 * you want to use gdk_keymap_translate_keyboard_state() instead of
779 * this function, since the effective group/level may not be
780 * the same as the current keyboard state.
782 * Return value: a keyval, or 0 if none was mapped to the given @key
785 gdk_keymap_lookup_key (GdkKeymap *keymap,
786 const GdkKeymapKey *key)
788 GdkKeymapX11 *keymap_x11;
790 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), 0);
791 g_return_val_if_fail (key != NULL, 0);
792 g_return_val_if_fail (key->group < 4, 0);
796 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
798 g_message ("_multihead : reverting to default display keymap "
799 "in gdk_keymap_lookup_key\n"));
802 keymap_x11 = GDK_KEYMAP_X11 (keymap);
805 if (KEYMAP_USE_XKB (keymap))
807 XkbDescRec *xkb = get_xkb (keymap_x11);
809 return XkbKeySymEntry (xkb, key->keycode, key->level, key->group);
814 const KeySym *map = get_keymap (keymap_x11);
815 const KeySym *syms = map + (key->keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
816 return syms [KEYSYM_INDEX (keymap_x11, key->group, key->level)];
821 /* This is copied straight from XFree86 Xlib, because I needed to
822 * add the group and level return. It's unchanged for ease of
823 * diff against the Xlib sources; don't reformat it.
826 MyEnhancedXkbTranslateKeyCode(register XkbDescPtr xkb,
828 register unsigned int mods,
829 unsigned int * mods_rtrn,
830 KeySym * keysym_rtrn,
831 unsigned int * group_rtrn,
832 unsigned int * level_rtrn)
836 unsigned preserve,effectiveGroup;
842 nKeyGroups= XkbKeyNumGroups(xkb,key);
843 if ((!XkbKeycodeInRange(xkb,key))||(nKeyGroups==0)) {
844 if (keysym_rtrn!=NULL)
845 *keysym_rtrn = NoSymbol;
849 syms = XkbKeySymsPtr(xkb,key);
851 /* find the offset of the effective group */
853 effectiveGroup= XkbGroupForCoreState(mods);
854 if ( effectiveGroup>=nKeyGroups ) {
855 unsigned groupInfo= XkbKeyGroupInfo(xkb,key);
856 switch (XkbOutOfRangeGroupAction(groupInfo)) {
858 effectiveGroup %= nKeyGroups;
860 case XkbClampIntoRange:
861 effectiveGroup = nKeyGroups-1;
863 case XkbRedirectIntoRange:
864 effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
865 if (effectiveGroup>=nKeyGroups)
870 col= effectiveGroup*XkbKeyGroupsWidth(xkb,key);
871 type = XkbKeyKeyType(xkb,key,effectiveGroup);
874 if (type->map) { /* find the column (shift level) within the group */
876 register XkbKTMapEntryPtr entry;
877 for (i=0,entry=type->map;i<type->map_count;i++,entry++) {
878 if ((entry->active)&&((mods&type->mods.mask)==entry->mods.mask)) {
881 preserve= type->preserve[i].mask;
883 /* ---- Begin stuff GDK adds to the original Xlib version ---- */
886 *level_rtrn = entry->level;
888 /* ---- End stuff GDK adds to the original Xlib version ---- */
895 if (keysym_rtrn!=NULL)
896 *keysym_rtrn= syms[col];
898 *mods_rtrn= type->mods.mask&(~preserve);
900 /* ---- Begin stuff GDK comments out of the original Xlib version ---- */
901 /* This is commented out because xkb_info is a private struct */
904 /* The Motif VTS doesn't get the help callback called if help
905 * is bound to Shift+<whatever>, and it appears as though it
906 * is XkbTranslateKeyCode that is causing the problem. The
907 * core X version of XTranslateKey always OR's in ShiftMask
908 * and LockMask for mods_rtrn, so this "fix" keeps this behavior
909 * and solves the VTS problem.
911 if ((xkb->dpy)&&(xkb->dpy->xkb_info)&&
912 (xkb->dpy->xkb_info->xlib_ctrls&XkbLC_AlwaysConsumeShiftAndLock)) { *mods_rtrn|= (ShiftMask|LockMask);
916 /* ---- End stuff GDK comments out of the original Xlib version ---- */
919 /* ---- Begin stuff GDK adds to the original Xlib version ---- */
922 *group_rtrn = effectiveGroup;
924 /* ---- End stuff GDK adds to the original Xlib version ---- */
926 return (syms[col]!=NoSymbol);
928 #endif /* HAVE_XKB */
931 * gdk_keymap_translate_keyboard_state:
932 * @keymap: a #GdkKeymap, or %NULL to use the default
933 * @hardware_keycode: a keycode
934 * @state: a modifier state
935 * @group: active keyboard group
936 * @keyval: return location for keyval
937 * @effective_group: return location for effective group
938 * @level: return location for level
939 * @consumed_modifiers: return location for modifiers that were used to determine the group or level
942 * Translates the contents of a #GdkEventKey into a keyval, effective
943 * group, and level. Modifiers that affected the translation and
944 * are thus unavailable for application use are returned in
945 * @consumed_modifiers. See gdk_keyval_get_keys() for an explanation of
946 * groups and levels. The @effective_group is the group that was
947 * actually used for the translation; some keys such as Enter are not
948 * affected by the active keyboard group. The @level is derived from
949 * @state. For convenience, #GdkEventKey already contains the translated
950 * keyval, so this function isn't as useful as you might think.
952 * Return value: %TRUE if there was a keyval bound to the keycode/state/group
955 gdk_keymap_translate_keyboard_state (GdkKeymap *keymap,
956 guint hardware_keycode,
957 GdkModifierType state,
960 gint *effective_group,
962 GdkModifierType *consumed_modifiers)
964 GdkKeymapX11 *keymap_x11;
965 KeySym tmp_keyval = NoSymbol;
968 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
969 g_return_val_if_fail (group < 4, FALSE);
971 keymap_x11 = GDK_KEYMAP_X11 (keymap);
976 *effective_group = 0;
979 if (consumed_modifiers)
980 *consumed_modifiers = 0;
982 update_keyrange (keymap_x11);
984 if (hardware_keycode < keymap_x11->min_keycode ||
985 hardware_keycode > keymap_x11->max_keycode)
989 if (KEYMAP_USE_XKB (keymap))
991 XkbDescRec *xkb = get_xkb (keymap_x11);
993 /* replace bits 13 and 14 with the provided group */
994 state &= ~(1 << 13 | 1 << 14);
995 state |= group << 13;
997 MyEnhancedXkbTranslateKeyCode (xkb,
1005 if (state & ~tmp_modifiers & LockMask)
1006 tmp_keyval = gdk_keyval_to_upper (tmp_keyval);
1008 /* We need to augment the consumed modifiers with LockMask, since
1009 * we handle that ourselves, and also with the group bits
1011 tmp_modifiers |= LockMask | 1 << 13 | 1 << 14;
1016 const KeySym *map = get_keymap (keymap_x11);
1019 gboolean ignore_shift = FALSE;
1020 gboolean ignore_group = FALSE;
1022 if ((state & GDK_SHIFT_MASK) &&
1023 (state & GDK_LOCK_MASK))
1024 shift_level = 0; /* shift disables shift lock */
1025 else if ((state & GDK_SHIFT_MASK) ||
1026 (state & GDK_LOCK_MASK))
1031 syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1033 #define SYM(k,g,l) syms[KEYSYM_INDEX (k,g,l)]
1035 /* Drop group and shift if there are no keysymbols on
1036 * the specified key.
1038 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1041 ignore_shift = TRUE;
1043 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, 0, shift_level))
1046 ignore_group = TRUE;
1048 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, 0, 0))
1052 ignore_group = TRUE;
1053 ignore_shift = TRUE;
1056 /* See whether the group and shift level actually mattered
1057 * to know what to put in consumed_modifiers
1059 if (!SYM (keymap_x11, group, 1) ||
1060 SYM (keymap_x11, group, 0) == SYM (keymap_x11, group, 1))
1061 ignore_shift = TRUE;
1063 if (!SYM (keymap_x11, 1, shift_level) ||
1064 SYM (keymap_x11, 0, shift_level) == SYM (keymap_x11, 1, shift_level))
1065 ignore_group = TRUE;
1067 tmp_keyval = SYM (keymap_x11, group, shift_level);
1069 tmp_modifiers = ignore_group ? 0 : keymap_x11->group_switch_mask;
1070 tmp_modifiers |= ignore_shift ? 0 : (GDK_SHIFT_MASK | GDK_LOCK_MASK);
1072 if (effective_group)
1073 *effective_group = group;
1076 *level = shift_level;
1080 if (consumed_modifiers)
1081 *consumed_modifiers = tmp_modifiers;
1084 *keyval = tmp_keyval;
1086 return tmp_keyval != NoSymbol;
1090 /* Key handling not part of the keymap */
1093 gdk_keyval_name (guint keyval)
1101 case GDK_KP_Page_Up:
1102 return "KP_Page_Up";
1103 case GDK_KP_Page_Down:
1104 return "KP_Page_Down";
1107 return XKeysymToString (keyval);
1111 gdk_keyval_from_name (const gchar *keyval_name)
1113 g_return_val_if_fail (keyval_name != NULL, 0);
1115 return XStringToKeysym (keyval_name);
1118 #ifdef HAVE_XCONVERTCASE
1120 gdk_keyval_convert_case (guint symbol,
1127 /* Check for directly encoded 24-bit UCS characters: */
1128 if ((symbol & 0xff000000) == 0x01000000)
1131 *lower = gdk_unicode_to_keyval (g_unichar_tolower (symbol & 0x00ffffff));
1133 *upper = gdk_unicode_to_keyval (g_unichar_toupper (symbol & 0x00ffffff));
1138 XConvertCase (symbol, &xlower, &xupper);
1145 #endif /* HAVE_XCONVERTCASE */
1148 _gdk_x11_get_group_for_state (GdkDisplay *display,
1149 GdkModifierType state)
1151 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1154 if (display_x11->use_xkb)
1156 return XkbGroupForCoreState (state);
1161 GdkKeymapX11 *keymap_impl = GDK_KEYMAP_X11 (gdk_keymap_get_for_display (display));
1162 update_keymaps (keymap_impl);
1163 return (state & keymap_impl->group_switch_mask) ? 1 : 0;