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;
126 keymap->xkb_desc = NULL;
128 keymap->current_serial = 0;
132 update_keyrange (GdkKeymapX11 *keymap_x11)
134 if (keymap_x11->max_keycode == 0)
135 XDisplayKeycodes (KEYMAP_XDISPLAY (GDK_KEYMAP (keymap_x11)),
136 &keymap_x11->min_keycode, &keymap_x11->max_keycode);
142 get_xkb (GdkKeymapX11 *keymap_x11)
144 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_KEYMAP (keymap_x11)->display);
145 Display *xdisplay = display_x11->xdisplay;
147 update_keyrange (keymap_x11);
149 if (keymap_x11->xkb_desc == NULL)
151 keymap_x11->xkb_desc = XkbGetMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask, XkbUseCoreKbd);
152 if (keymap_x11->xkb_desc == NULL)
153 g_error ("Failed to get keymap");
155 XkbGetNames (xdisplay, XkbGroupNamesMask, keymap_x11->xkb_desc);
157 else if (keymap_x11->current_serial != display_x11->keymap_serial)
159 XkbGetUpdatedMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask,
160 keymap_x11->xkb_desc);
161 XkbGetNames (xdisplay, XkbGroupNamesMask, keymap_x11->xkb_desc);
164 keymap_x11->current_serial = display_x11->keymap_serial;
166 return keymap_x11->xkb_desc;
168 #endif /* HAVE_XKB */
170 /* Whether we were able to turn on detectable-autorepeat using
171 * XkbSetDetectableAutorepeat. If FALSE, we'll fall back
172 * to checking the next event with XPending().
176 * gdk_keymap_get_for_display:
177 * @display: the #GdkDisplay.
178 * @returns: the #GdkKeymap attached to @display.
180 * Returns the #GdkKeymap attached to @display.
183 gdk_keymap_get_for_display (GdkDisplay *display)
185 GdkDisplayX11 *display_x11;
186 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
187 display_x11 = GDK_DISPLAY_X11 (display);
189 if (!display_x11->keymap)
190 display_x11->keymap = g_object_new (gdk_keymap_x11_get_type (), NULL);
192 display_x11->keymap->display = display;
194 return display_x11->keymap;
197 /* Find the index of the group/level pair within the keysyms for a key.
199 #define KEYSYM_INDEX(keymap_impl, group, level) \
200 (2 * ((group) % (keymap_impl->keysyms_per_keycode / 2)) + (level))
203 update_keymaps (GdkKeymapX11 *keymap_x11)
205 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_KEYMAP (keymap_x11)->display);
206 Display *xdisplay = display_x11->xdisplay;
209 g_assert (!KEYMAP_USE_XKB (GDK_KEYMAP (keymap_x11)));
212 if (keymap_x11->keymap == NULL ||
213 keymap_x11->current_serial != display_x11->keymap_serial)
219 keymap_x11->current_serial = display_x11->keymap_serial;
221 update_keyrange (keymap_x11);
223 if (keymap_x11->keymap)
224 XFree (keymap_x11->keymap);
226 if (keymap_x11->mod_keymap)
227 XFreeModifiermap (keymap_x11->mod_keymap);
229 keymap_x11->keymap = XGetKeyboardMapping (xdisplay, keymap_x11->min_keycode,
230 keymap_x11->max_keycode - keymap_x11->min_keycode,
231 &keymap_x11->keysyms_per_keycode);
234 /* GDK_ISO_Left_Tab, as usually configured through XKB, really messes
235 * up the whole idea of "consumed modifiers" because shift is consumed.
236 * However, <shift>Tab is not usually GDK_ISO_Left_Tab without XKB,
237 * we we fudge the map here.
239 keycode = keymap_x11->min_keycode;
240 while (keycode < keymap_x11->max_keycode)
242 KeySym *syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
243 /* Check both groups */
244 for (i = 0 ; i < 2 ; i++)
246 if (syms[KEYSYM_INDEX (keymap_x11, i, 0)] == GDK_Tab)
247 syms[KEYSYM_INDEX (keymap_x11, i, 1)] = GDK_ISO_Left_Tab;
251 * If there is one keysym and the key symbol has upper and lower
252 * case variants fudge the keymap
254 if (syms[KEYSYM_INDEX (keymap_x11, 0, 1)] == 0)
259 gdk_keyval_convert_case (syms[KEYSYM_INDEX (keymap_x11, 0, 0)], &lower, &upper);
262 syms[KEYSYM_INDEX (keymap_x11, 0, 0)] = lower;
263 syms[KEYSYM_INDEX (keymap_x11, 0, 1)] = upper;
271 keymap_x11->mod_keymap = XGetModifierMapping (xdisplay);
274 keymap_x11->group_switch_mask = 0;
276 /* there are 8 modifiers, and the first 3 are shift, shift lock,
279 map_size = 8 * keymap_x11->mod_keymap->max_keypermod;
280 i = 3 * keymap_x11->mod_keymap->max_keypermod;
283 /* get the key code at this point in the map,
284 * see if its keysym is GDK_Mode_switch, if so
285 * we have the mode key
287 gint keycode = keymap_x11->mod_keymap->modifiermap[i];
289 if (keycode >= keymap_x11->min_keycode &&
290 keycode <= keymap_x11->max_keycode)
293 KeySym *syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
295 while (j < keymap_x11->keysyms_per_keycode)
297 if (syms[j] == GDK_Mode_switch)
299 /* This modifier swaps groups */
301 /* GDK_MOD1_MASK is 1 << 3 for example, i.e. the
302 * fourth modifier, i / keyspermod is the modifier
306 keymap_x11->group_switch_mask |= (1 << ( i / keymap_x11->mod_keymap->max_keypermod));
320 get_keymap (GdkKeymapX11 *keymap_x11)
322 update_keymaps (keymap_x11);
324 return keymap_x11->keymap;
328 static PangoDirection
329 get_direction (GdkKeymapX11 *keymap_x11)
331 XkbDescRec *xkb = get_xkb (keymap_x11);
333 XkbStateRec state_rec;
334 PangoDirection result;
336 GdkDisplay *display = GDK_KEYMAP (keymap_x11)->display;
338 XkbGetState (GDK_DISPLAY_XDISPLAY (display), XkbUseCoreKbd, &state_rec);
340 if (xkb->names->groups[state_rec.locked_group] == None)
341 result = PANGO_DIRECTION_LTR;
344 name = gdk_x11_get_xatom_name_for_display (display, xkb->names->groups[state_rec.locked_group]);
346 if (g_strcasecmp (name, "arabic") == 0 ||
347 g_strcasecmp (name, "hebrew") == 0 ||
348 g_strcasecmp (name, "israelian") == 0)
349 result = PANGO_DIRECTION_RTL;
351 result = PANGO_DIRECTION_LTR;
358 _gdk_keymap_state_changed (GdkDisplay *display)
360 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
362 if (display_x11->keymap)
364 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (display_x11->keymap);
366 PangoDirection new_direction = get_direction (keymap_x11);
368 if (!keymap_x11->have_direction || new_direction != keymap_x11->current_direction)
370 keymap_x11->have_direction = TRUE;
371 keymap_x11->current_direction = new_direction;
372 g_signal_emit_by_name (G_OBJECT (keymap_x11), "direction_changed");
376 #endif /* HAVE_XKB */
379 gdk_keymap_get_direction (GdkKeymap *keymap)
383 keymap = gdk_keymap_get_for_display (gdk_get_default_display ());
385 g_message ("_multihead : reverting to default display keymap "
386 "in gdk_keymap_get_direction"));
390 if (KEYMAP_USE_XKB (keymap))
392 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (keymap);
394 if (!keymap_x11->have_direction)
396 keymap_x11->current_direction = get_direction (keymap_x11);
397 keymap_x11->have_direction = TRUE;
400 return keymap_x11->current_direction;
403 #endif /* HAVE_XKB */
404 return PANGO_DIRECTION_LTR;
408 * gdk_keymap_get_entries_for_keyval:
409 * @keymap: a #GdkKeymap, or %NULL to use the default keymap
410 * @keyval: a keyval, such as %GDK_a, %GDK_Up, %GDK_Return, etc.
411 * @keys: return location for an array of #GdkKeymapKey
412 * @n_keys: return location for number of elements in returned array
414 * Obtains a list of keycode/group/level combinations that will
415 * generate @keyval. Groups and levels are two kinds of keyboard mode;
416 * in general, the level determines whether the top or bottom symbol
417 * on a key is used, and the group determines whether the left or
418 * right symbol is used. On US keyboards, the shift key changes the
419 * keyboard level, and there are no groups. A group switch key might
420 * convert a keyboard between Hebrew to English modes, for example.
421 * #GdkEventKey contains a %group field that indicates the active
422 * keyboard group. The level is computed from the modifier mask.
423 * The returned array should be freed
426 * Return value: %TRUE if keys were found and returned
429 gdk_keymap_get_entries_for_keyval (GdkKeymap *keymap,
435 GdkKeymapX11 *keymap_x11;
437 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
438 g_return_val_if_fail (keys != NULL, FALSE);
439 g_return_val_if_fail (n_keys != NULL, FALSE);
440 g_return_val_if_fail (keyval != 0, FALSE);
444 keymap = gdk_keymap_get_for_display (gdk_get_default_display ());
446 g_message ("_multihead : reverting to default display keymap "
447 "in gdk_keymap_get_entries_for_keyval\n"));
450 keymap_x11 = GDK_KEYMAP_X11 (keymap);
452 retval = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
455 if (KEYMAP_USE_XKB (keymap))
457 /* See sec 15.3.4 in XKB docs */
459 XkbDescRec *xkb = get_xkb (keymap_x11);
462 keycode = keymap_x11->min_keycode;
464 while (keycode <= keymap_x11->max_keycode)
466 gint max_shift_levels = XkbKeyGroupsWidth (xkb, keycode); /* "key width" */
469 gint total_syms = XkbKeyNumSyms (xkb, keycode);
473 /* entry is an array with all syms for group 0, all
474 * syms for group 1, etc. and for each group the
475 * shift level syms are in order
477 entry = XkbKeySymsPtr (xkb, keycode);
479 while (i < total_syms)
481 /* check out our cool loop invariant */
482 g_assert (i == (group * max_shift_levels + level));
484 if (entry[i] == keyval)
489 key.keycode = keycode;
493 g_array_append_val (retval, key);
495 g_assert (XkbKeySymEntry (xkb, keycode, level, group) ==
501 if (level == max_shift_levels)
516 const KeySym *map = get_keymap (keymap_x11);
519 keycode = keymap_x11->min_keycode;
520 while (keycode < keymap_x11->max_keycode)
522 const KeySym *syms = map + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
525 while (i < keymap_x11->keysyms_per_keycode)
527 if (syms[i] == keyval)
532 key.keycode = keycode;
534 /* The "classic" non-XKB keymap has 2 levels per group */
538 g_array_append_val (retval, key);
550 *keys = (GdkKeymapKey*) retval->data;
551 *n_keys = retval->len;
559 g_array_free (retval, retval->len > 0 ? FALSE : TRUE);
565 * gdk_keymap_get_entries_for_keycode:
566 * @keymap: a #GdkKeymap or %NULL to use the default keymap
567 * @hardware_keycode: a keycode
568 * @keys: return location for array of #GdkKeymapKey, or NULL
569 * @keyvals: return location for array of keyvals, or NULL
570 * @n_entries: length of @keys and @keyvals
572 * Returns the keyvals bound to @hardware_keycode.
573 * The Nth #GdkKeymapKey in @keys is bound to the Nth
574 * keyval in @keyvals. Free the returned arrays with g_free().
575 * When a keycode is pressed by the user, the keyval from
576 * this list of entries is selected by considering the effective
577 * keyboard group and level. See gdk_keymap_translate_keyboard_state().
579 * Returns: %TRUE if there were any entries
582 gdk_keymap_get_entries_for_keycode (GdkKeymap *keymap,
583 guint hardware_keycode,
588 GdkKeymapX11 *keymap_x11;
591 GArray *keyval_array;
593 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
594 g_return_val_if_fail (n_entries != NULL, FALSE);
598 keymap = gdk_keymap_get_for_display (gdk_get_default_display ());
600 g_message ("_multihead : reverting to default display keymap "
601 "in gdk_keymap_get_entries_for_keycode\n"));
604 keymap_x11 = GDK_KEYMAP_X11 (keymap);
606 update_keyrange (keymap_x11);
608 if (hardware_keycode < keymap_x11->min_keycode ||
609 hardware_keycode > keymap_x11->max_keycode)
621 key_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
626 keyval_array = g_array_new (FALSE, FALSE, sizeof (guint));
631 if (KEYMAP_USE_XKB (keymap))
633 /* See sec 15.3.4 in XKB docs */
635 XkbDescRec *xkb = get_xkb (keymap_x11);
636 gint max_shift_levels;
643 max_shift_levels = XkbKeyGroupsWidth (xkb, hardware_keycode); /* "key width" */
644 total_syms = XkbKeyNumSyms (xkb, hardware_keycode);
646 /* entry is an array with all syms for group 0, all
647 * syms for group 1, etc. and for each group the
648 * shift level syms are in order
650 entry = XkbKeySymsPtr (xkb, hardware_keycode);
652 while (i < total_syms)
654 /* check out our cool loop invariant */
655 g_assert (i == (group * max_shift_levels + level));
661 key.keycode = hardware_keycode;
665 g_array_append_val (key_array, key);
669 g_array_append_val (keyval_array, entry[i]);
673 if (level == max_shift_levels)
685 const KeySym *map = get_keymap (keymap_x11);
689 syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
691 while (i < keymap_x11->keysyms_per_keycode)
697 key.keycode = hardware_keycode;
699 /* The "classic" non-XKB keymap has 2 levels per group */
703 g_array_append_val (key_array, key);
707 g_array_append_val (keyval_array, syms[i]);
713 if ((key_array && key_array->len > 0) ||
714 (keyval_array && keyval_array->len > 0))
717 *keys = (GdkKeymapKey*) key_array->data;
720 *keyvals = (guint*) keyval_array->data;
723 *n_entries = key_array->len;
725 *n_entries = keyval_array->len;
739 g_array_free (key_array, key_array->len > 0 ? FALSE : TRUE);
741 g_array_free (keyval_array, keyval_array->len > 0 ? FALSE : TRUE);
743 return *n_entries > 0;
748 * gdk_keymap_lookup_key:
749 * @keymap: a #GdkKeymap or %NULL to use the default keymap
750 * @key: a #GdkKeymapKey with keycode, group, and level initialized
752 * Looks up the keyval mapped to a keycode/group/level triplet.
753 * If no keyval is bound to @key, returns 0. For normal user input,
754 * you want to use gdk_keymap_translate_keyboard_state() instead of
755 * this function, since the effective group/level may not be
756 * the same as the current keyboard state.
758 * Return value: a keyval, or 0 if none was mapped to the given @key
761 gdk_keymap_lookup_key (GdkKeymap *keymap,
762 const GdkKeymapKey *key)
764 GdkKeymapX11 *keymap_x11;
766 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), 0);
767 g_return_val_if_fail (key != NULL, 0);
768 g_return_val_if_fail (key->group < 4, 0);
772 keymap = gdk_keymap_get_for_display (gdk_get_default_display ());
774 g_message ("_multihead : reverting to default display keymap "
775 "in gdk_keymap_lookup_key\n"));
778 keymap_x11 = GDK_KEYMAP_X11 (keymap);
781 if (KEYMAP_USE_XKB (keymap))
783 XkbDescRec *xkb = get_xkb (keymap_x11);
785 return XkbKeySymEntry (xkb, key->keycode, key->level, key->group);
790 const KeySym *map = get_keymap (keymap_x11);
791 const KeySym *syms = map + (key->keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
792 return syms [KEYSYM_INDEX (keymap_x11, key->group, key->level)];
797 /* This is copied straight from XFree86 Xlib, because I needed to
798 * add the group and level return. It's unchanged for ease of
799 * diff against the Xlib sources; don't reformat it.
802 MyEnhancedXkbTranslateKeyCode(register XkbDescPtr xkb,
804 register unsigned int mods,
805 unsigned int * mods_rtrn,
806 KeySym * keysym_rtrn,
807 unsigned int * group_rtrn,
808 unsigned int * level_rtrn)
812 unsigned preserve,effectiveGroup;
818 nKeyGroups= XkbKeyNumGroups(xkb,key);
819 if ((!XkbKeycodeInRange(xkb,key))||(nKeyGroups==0)) {
820 if (keysym_rtrn!=NULL)
821 *keysym_rtrn = NoSymbol;
825 syms = XkbKeySymsPtr(xkb,key);
827 /* find the offset of the effective group */
829 effectiveGroup= XkbGroupForCoreState(mods);
830 if ( effectiveGroup>=nKeyGroups ) {
831 unsigned groupInfo= XkbKeyGroupInfo(xkb,key);
832 switch (XkbOutOfRangeGroupAction(groupInfo)) {
834 effectiveGroup %= nKeyGroups;
836 case XkbClampIntoRange:
837 effectiveGroup = nKeyGroups-1;
839 case XkbRedirectIntoRange:
840 effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
841 if (effectiveGroup>=nKeyGroups)
846 col= effectiveGroup*XkbKeyGroupsWidth(xkb,key);
847 type = XkbKeyKeyType(xkb,key,effectiveGroup);
850 if (type->map) { /* find the column (shift level) within the group */
852 register XkbKTMapEntryPtr entry;
853 for (i=0,entry=type->map;i<type->map_count;i++,entry++) {
854 if ((entry->active)&&((mods&type->mods.mask)==entry->mods.mask)) {
857 preserve= type->preserve[i].mask;
859 /* ---- Begin stuff GDK adds to the original Xlib version ---- */
862 *level_rtrn = entry->level;
864 /* ---- End stuff GDK adds to the original Xlib version ---- */
871 if (keysym_rtrn!=NULL)
872 *keysym_rtrn= syms[col];
874 *mods_rtrn= type->mods.mask&(~preserve);
876 /* ---- Begin stuff GDK comments out of the original Xlib version ---- */
877 /* This is commented out because xkb_info is a private struct */
880 /* The Motif VTS doesn't get the help callback called if help
881 * is bound to Shift+<whatever>, and it appears as though it
882 * is XkbTranslateKeyCode that is causing the problem. The
883 * core X version of XTranslateKey always OR's in ShiftMask
884 * and LockMask for mods_rtrn, so this "fix" keeps this behavior
885 * and solves the VTS problem.
887 if ((xkb->dpy)&&(xkb->dpy->xkb_info)&&
888 (xkb->dpy->xkb_info->xlib_ctrls&XkbLC_AlwaysConsumeShiftAndLock)) { *mods_rtrn|= (ShiftMask|LockMask);
892 /* ---- End stuff GDK comments out of the original Xlib version ---- */
895 /* ---- Begin stuff GDK adds to the original Xlib version ---- */
898 *group_rtrn = effectiveGroup;
900 /* ---- End stuff GDK adds to the original Xlib version ---- */
902 return (syms[col]!=NoSymbol);
904 #endif /* HAVE_XKB */
907 * gdk_keymap_translate_keyboard_state:
908 * @keymap: a #GdkKeymap, or %NULL to use the default
909 * @hardware_keycode: a keycode
910 * @state: a modifier state
911 * @group: active keyboard group
912 * @keyval: return location for keyval
913 * @effective_group: return location for effective group
914 * @level: return location for level
915 * @consumed_modifiers: return location for modifiers that were used to determine the group or level
918 * Translates the contents of a #GdkEventKey into a keyval, effective
919 * group, and level. Modifiers that affected the translation and
920 * are thus unavailable for application use are returned in
921 * @consumed_modifiers. See gdk_keyval_get_keys() for an explanation of
922 * groups and levels. The @effective_group is the group that was
923 * actually used for the translation; some keys such as Enter are not
924 * affected by the active keyboard group. The @level is derived from
925 * @state. For convenience, #GdkEventKey already contains the translated
926 * keyval, so this function isn't as useful as you might think.
928 * Return value: %TRUE if there was a keyval bound to the keycode/state/group
931 gdk_keymap_translate_keyboard_state (GdkKeymap *keymap,
932 guint hardware_keycode,
933 GdkModifierType state,
936 gint *effective_group,
938 GdkModifierType *consumed_modifiers)
940 GdkKeymapX11 *keymap_x11;
941 KeySym tmp_keyval = NoSymbol;
944 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
945 g_return_val_if_fail (group < 4, FALSE);
947 keymap_x11 = GDK_KEYMAP_X11 (keymap);
952 *effective_group = 0;
955 if (consumed_modifiers)
956 *consumed_modifiers = 0;
958 update_keyrange (keymap_x11);
960 if (hardware_keycode < keymap_x11->min_keycode ||
961 hardware_keycode > keymap_x11->max_keycode)
965 if (KEYMAP_USE_XKB (keymap))
967 XkbDescRec *xkb = get_xkb (keymap_x11);
969 /* replace bits 13 and 14 with the provided group */
970 state &= ~(1 << 13 | 1 << 14);
971 state |= group << 13;
973 MyEnhancedXkbTranslateKeyCode (xkb,
981 if (state & ~tmp_modifiers & LockMask)
982 tmp_keyval = gdk_keyval_to_upper (tmp_keyval);
984 /* We need to augment the consumed modifiers with LockMask, since
985 * we handle that ourselves, and also with the group bits
987 tmp_modifiers |= LockMask | 1 << 13 | 1 << 14;
992 const KeySym *map = get_keymap (keymap_x11);
995 gboolean ignore_shift = FALSE;
996 gboolean ignore_group = FALSE;
998 if ((state & GDK_SHIFT_MASK) &&
999 (state & GDK_LOCK_MASK))
1000 shift_level = 0; /* shift disables shift lock */
1001 else if ((state & GDK_SHIFT_MASK) ||
1002 (state & GDK_LOCK_MASK))
1007 syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1009 #define SYM(k,g,l) syms[KEYSYM_INDEX (k,g,l)]
1011 /* Drop group and shift if there are no keysymbols on
1012 * the specified key.
1014 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1017 ignore_shift = TRUE;
1019 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, 0, shift_level))
1022 ignore_group = TRUE;
1024 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, 0, 0))
1028 ignore_group = TRUE;
1029 ignore_shift = TRUE;
1032 /* See whether the group and shift level actually mattered
1033 * to know what to put in consumed_modifiers
1035 if (!SYM (keymap_x11, group, 1) ||
1036 SYM (keymap_x11, group, 0) == SYM (keymap_x11, group, 1))
1037 ignore_shift = TRUE;
1039 if (!SYM (keymap_x11, 1, shift_level) ||
1040 SYM (keymap_x11, 0, shift_level) == SYM (keymap_x11, 1, shift_level))
1041 ignore_group = TRUE;
1043 tmp_keyval = SYM (keymap_x11, group, shift_level);
1045 tmp_modifiers = ignore_group ? 0 : keymap_x11->group_switch_mask;
1046 tmp_modifiers |= ignore_shift ? 0 : (GDK_SHIFT_MASK | GDK_LOCK_MASK);
1048 if (effective_group)
1049 *effective_group = group;
1052 *level = shift_level;
1056 if (consumed_modifiers)
1057 *consumed_modifiers = tmp_modifiers;
1060 *keyval = tmp_keyval;
1062 return tmp_keyval != NoSymbol;
1066 /* Key handling not part of the keymap */
1069 gdk_keyval_name (guint keyval)
1077 case GDK_KP_Page_Up:
1078 return "KP_Page_Up";
1079 case GDK_KP_Page_Down:
1080 return "KP_Page_Down";
1083 return XKeysymToString (keyval);
1087 gdk_keyval_from_name (const gchar *keyval_name)
1089 g_return_val_if_fail (keyval_name != NULL, 0);
1091 return XStringToKeysym (keyval_name);
1094 #ifdef HAVE_XCONVERTCASE
1096 gdk_keyval_convert_case (guint symbol,
1103 /* Check for directly encoded 24-bit UCS characters: */
1104 if ((symbol & 0xff000000) == 0x01000000)
1107 *lower = gdk_unicode_to_keyval (g_unichar_tolower (symbol & 0x00ffffff));
1109 *upper = gdk_unicode_to_keyval (g_unichar_toupper (symbol & 0x00ffffff));
1114 XConvertCase (symbol, &xlower, &xupper);
1121 #endif /* HAVE_XCONVERTCASE */
1124 _gdk_x11_get_group_for_state (GdkDisplay *display,
1125 GdkModifierType state)
1127 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1130 if (display_x11->use_xkb)
1132 return XkbGroupForCoreState (state);
1137 GdkKeymapX11 *keymap_impl = GDK_KEYMAP_X11 (gdk_keymap_get_for_display (display));
1138 update_keymaps (keymap_impl);
1139 return (state & keymap_impl->group_switch_mask) ? 1 : 0;