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 + 1,
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;
294 /* GDK_MOD1_MASK is 1 << 3 for example, i.e. the
295 * fourth modifier, i / keyspermod is the modifier
298 guint mask = 1 << ( i / keymap_x11->mod_keymap->max_keypermod);
300 /* Some keyboard maps are known to map Mode_Switch as an extra
301 * Mod1 key. In circumstances like that, it won't be used to
304 if (mask == GDK_CONTROL_MASK || mask == GDK_SHIFT_MASK ||
305 mask == GDK_LOCK_MASK || mask == GDK_MOD1_MASK)
308 while (j < keymap_x11->keysyms_per_keycode)
310 if (syms[j] == GDK_Mode_switch)
312 /* This modifier swaps groups */
314 keymap_x11->group_switch_mask |= mask;
329 get_keymap (GdkKeymapX11 *keymap_x11)
331 update_keymaps (keymap_x11);
333 return keymap_x11->keymap;
337 static PangoDirection
338 get_direction (GdkKeymapX11 *keymap_x11)
340 XkbDescRec *xkb = get_xkb (keymap_x11);
342 XkbStateRec state_rec;
343 PangoDirection result;
345 GdkDisplay *display = GDK_KEYMAP (keymap_x11)->display;
347 XkbGetState (GDK_DISPLAY_XDISPLAY (display), XkbUseCoreKbd, &state_rec);
349 if (xkb->names->groups[state_rec.locked_group] == None)
350 result = PANGO_DIRECTION_LTR;
353 name = gdk_x11_get_xatom_name_for_display (display, xkb->names->groups[state_rec.locked_group]);
355 if (g_ascii_strcasecmp (name, "arabic") == 0 ||
356 g_ascii_strcasecmp (name, "hebrew") == 0 ||
357 g_ascii_strcasecmp (name, "israelian") == 0)
358 result = PANGO_DIRECTION_RTL;
360 result = PANGO_DIRECTION_LTR;
367 _gdk_keymap_state_changed (GdkDisplay *display)
369 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
371 if (display_x11->keymap)
373 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (display_x11->keymap);
375 PangoDirection new_direction = get_direction (keymap_x11);
377 if (!keymap_x11->have_direction || new_direction != keymap_x11->current_direction)
379 keymap_x11->have_direction = TRUE;
380 keymap_x11->current_direction = new_direction;
381 g_signal_emit_by_name (keymap_x11, "direction_changed");
387 _gdk_keymap_keys_changed (GdkDisplay *display)
389 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
391 ++display_x11->keymap_serial;
393 if (display_x11->keymap)
394 g_signal_emit_by_name (display_x11->keymap, "keys_changed", 0);
397 #endif /* HAVE_XKB */
400 gdk_keymap_get_direction (GdkKeymap *keymap)
404 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
406 g_message ("_multihead : reverting to default display keymap "
407 "in gdk_keymap_get_direction"));
411 if (KEYMAP_USE_XKB (keymap))
413 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (keymap);
415 if (!keymap_x11->have_direction)
417 keymap_x11->current_direction = get_direction (keymap_x11);
418 keymap_x11->have_direction = TRUE;
421 return keymap_x11->current_direction;
424 #endif /* HAVE_XKB */
425 return PANGO_DIRECTION_LTR;
429 * gdk_keymap_get_entries_for_keyval:
430 * @keymap: a #GdkKeymap, or %NULL to use the default keymap
431 * @keyval: a keyval, such as %GDK_a, %GDK_Up, %GDK_Return, etc.
432 * @keys: return location for an array of #GdkKeymapKey
433 * @n_keys: return location for number of elements in returned array
435 * Obtains a list of keycode/group/level combinations that will
436 * generate @keyval. Groups and levels are two kinds of keyboard mode;
437 * in general, the level determines whether the top or bottom symbol
438 * on a key is used, and the group determines whether the left or
439 * right symbol is used. On US keyboards, the shift key changes the
440 * keyboard level, and there are no groups. A group switch key might
441 * convert a keyboard between Hebrew to English modes, for example.
442 * #GdkEventKey contains a %group field that indicates the active
443 * keyboard group. The level is computed from the modifier mask.
444 * The returned array should be freed
447 * Return value: %TRUE if keys were found and returned
450 gdk_keymap_get_entries_for_keyval (GdkKeymap *keymap,
456 GdkKeymapX11 *keymap_x11;
458 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
459 g_return_val_if_fail (keys != NULL, FALSE);
460 g_return_val_if_fail (n_keys != NULL, FALSE);
461 g_return_val_if_fail (keyval != 0, FALSE);
465 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
467 g_message ("_multihead : reverting to default display keymap "
468 "in gdk_keymap_get_entries_for_keyval\n"));
471 keymap_x11 = GDK_KEYMAP_X11 (keymap);
473 retval = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
476 if (KEYMAP_USE_XKB (keymap))
478 /* See sec 15.3.4 in XKB docs */
480 XkbDescRec *xkb = get_xkb (keymap_x11);
483 keycode = keymap_x11->min_keycode;
485 while (keycode <= keymap_x11->max_keycode)
487 gint max_shift_levels = XkbKeyGroupsWidth (xkb, keycode); /* "key width" */
490 gint total_syms = XkbKeyNumSyms (xkb, keycode);
494 /* entry is an array with all syms for group 0, all
495 * syms for group 1, etc. and for each group the
496 * shift level syms are in order
498 entry = XkbKeySymsPtr (xkb, keycode);
500 while (i < total_syms)
502 /* check out our cool loop invariant */
503 g_assert (i == (group * max_shift_levels + level));
505 if (entry[i] == keyval)
510 key.keycode = keycode;
514 g_array_append_val (retval, key);
516 g_assert (XkbKeySymEntry (xkb, keycode, level, group) ==
522 if (level == max_shift_levels)
537 const KeySym *map = get_keymap (keymap_x11);
540 keycode = keymap_x11->min_keycode;
541 while (keycode <= keymap_x11->max_keycode)
543 const KeySym *syms = map + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
546 while (i < keymap_x11->keysyms_per_keycode)
548 if (syms[i] == keyval)
553 key.keycode = keycode;
555 /* The "classic" non-XKB keymap has 2 levels per group */
559 g_array_append_val (retval, key);
571 *keys = (GdkKeymapKey*) retval->data;
572 *n_keys = retval->len;
580 g_array_free (retval, retval->len > 0 ? FALSE : TRUE);
586 * gdk_keymap_get_entries_for_keycode:
587 * @keymap: a #GdkKeymap or %NULL to use the default keymap
588 * @hardware_keycode: a keycode
589 * @keys: return location for array of #GdkKeymapKey, or NULL
590 * @keyvals: return location for array of keyvals, or NULL
591 * @n_entries: length of @keys and @keyvals
593 * Returns the keyvals bound to @hardware_keycode.
594 * The Nth #GdkKeymapKey in @keys is bound to the Nth
595 * keyval in @keyvals. Free the returned arrays with g_free().
596 * When a keycode is pressed by the user, the keyval from
597 * this list of entries is selected by considering the effective
598 * keyboard group and level. See gdk_keymap_translate_keyboard_state().
600 * Returns: %TRUE if there were any entries
603 gdk_keymap_get_entries_for_keycode (GdkKeymap *keymap,
604 guint hardware_keycode,
609 GdkKeymapX11 *keymap_x11;
612 GArray *keyval_array;
614 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
615 g_return_val_if_fail (n_entries != NULL, FALSE);
619 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
621 g_message ("_multihead : reverting to default display keymap "
622 "in gdk_keymap_get_entries_for_keycode\n"));
625 keymap_x11 = GDK_KEYMAP_X11 (keymap);
627 update_keyrange (keymap_x11);
629 if (hardware_keycode < keymap_x11->min_keycode ||
630 hardware_keycode > keymap_x11->max_keycode)
642 key_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
647 keyval_array = g_array_new (FALSE, FALSE, sizeof (guint));
652 if (KEYMAP_USE_XKB (keymap))
654 /* See sec 15.3.4 in XKB docs */
656 XkbDescRec *xkb = get_xkb (keymap_x11);
657 gint max_shift_levels;
664 max_shift_levels = XkbKeyGroupsWidth (xkb, hardware_keycode); /* "key width" */
665 total_syms = XkbKeyNumSyms (xkb, hardware_keycode);
667 /* entry is an array with all syms for group 0, all
668 * syms for group 1, etc. and for each group the
669 * shift level syms are in order
671 entry = XkbKeySymsPtr (xkb, hardware_keycode);
673 while (i < total_syms)
675 /* check out our cool loop invariant */
676 g_assert (i == (group * max_shift_levels + level));
682 key.keycode = hardware_keycode;
686 g_array_append_val (key_array, key);
690 g_array_append_val (keyval_array, entry[i]);
694 if (level == max_shift_levels)
706 const KeySym *map = get_keymap (keymap_x11);
710 syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
712 while (i < keymap_x11->keysyms_per_keycode)
718 key.keycode = hardware_keycode;
720 /* The "classic" non-XKB keymap has 2 levels per group */
724 g_array_append_val (key_array, key);
728 g_array_append_val (keyval_array, syms[i]);
734 if ((key_array && key_array->len > 0) ||
735 (keyval_array && keyval_array->len > 0))
738 *keys = (GdkKeymapKey*) key_array->data;
741 *keyvals = (guint*) keyval_array->data;
744 *n_entries = key_array->len;
746 *n_entries = keyval_array->len;
760 g_array_free (key_array, key_array->len > 0 ? FALSE : TRUE);
762 g_array_free (keyval_array, keyval_array->len > 0 ? FALSE : TRUE);
764 return *n_entries > 0;
769 * gdk_keymap_lookup_key:
770 * @keymap: a #GdkKeymap or %NULL to use the default keymap
771 * @key: a #GdkKeymapKey with keycode, group, and level initialized
773 * Looks up the keyval mapped to a keycode/group/level triplet.
774 * If no keyval is bound to @key, returns 0. For normal user input,
775 * you want to use gdk_keymap_translate_keyboard_state() instead of
776 * this function, since the effective group/level may not be
777 * the same as the current keyboard state.
779 * Return value: a keyval, or 0 if none was mapped to the given @key
782 gdk_keymap_lookup_key (GdkKeymap *keymap,
783 const GdkKeymapKey *key)
785 GdkKeymapX11 *keymap_x11;
787 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), 0);
788 g_return_val_if_fail (key != NULL, 0);
789 g_return_val_if_fail (key->group < 4, 0);
793 keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
795 g_message ("_multihead : reverting to default display keymap "
796 "in gdk_keymap_lookup_key\n"));
799 keymap_x11 = GDK_KEYMAP_X11 (keymap);
802 if (KEYMAP_USE_XKB (keymap))
804 XkbDescRec *xkb = get_xkb (keymap_x11);
806 return XkbKeySymEntry (xkb, key->keycode, key->level, key->group);
811 const KeySym *map = get_keymap (keymap_x11);
812 const KeySym *syms = map + (key->keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
813 return syms [KEYSYM_INDEX (keymap_x11, key->group, key->level)];
818 /* This is copied straight from XFree86 Xlib, because I needed to
819 * add the group and level return. It's unchanged for ease of
820 * diff against the Xlib sources; don't reformat it.
823 MyEnhancedXkbTranslateKeyCode(register XkbDescPtr xkb,
825 register unsigned int mods,
826 unsigned int * mods_rtrn,
827 KeySym * keysym_rtrn,
828 unsigned int * group_rtrn,
829 unsigned int * level_rtrn)
833 unsigned preserve,effectiveGroup;
839 nKeyGroups= XkbKeyNumGroups(xkb,key);
840 if ((!XkbKeycodeInRange(xkb,key))||(nKeyGroups==0)) {
841 if (keysym_rtrn!=NULL)
842 *keysym_rtrn = NoSymbol;
846 syms = XkbKeySymsPtr(xkb,key);
848 /* find the offset of the effective group */
850 effectiveGroup= XkbGroupForCoreState(mods);
851 if ( effectiveGroup>=nKeyGroups ) {
852 unsigned groupInfo= XkbKeyGroupInfo(xkb,key);
853 switch (XkbOutOfRangeGroupAction(groupInfo)) {
855 effectiveGroup %= nKeyGroups;
857 case XkbClampIntoRange:
858 effectiveGroup = nKeyGroups-1;
860 case XkbRedirectIntoRange:
861 effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
862 if (effectiveGroup>=nKeyGroups)
867 col= effectiveGroup*XkbKeyGroupsWidth(xkb,key);
868 type = XkbKeyKeyType(xkb,key,effectiveGroup);
871 if (type->map) { /* find the column (shift level) within the group */
873 register XkbKTMapEntryPtr entry;
874 for (i=0,entry=type->map;i<type->map_count;i++,entry++) {
875 if ((entry->active)&&((mods&type->mods.mask)==entry->mods.mask)) {
878 preserve= type->preserve[i].mask;
880 /* ---- Begin stuff GDK adds to the original Xlib version ---- */
883 *level_rtrn = entry->level;
885 /* ---- End stuff GDK adds to the original Xlib version ---- */
892 if (keysym_rtrn!=NULL)
893 *keysym_rtrn= syms[col];
895 *mods_rtrn= type->mods.mask&(~preserve);
897 /* ---- Begin stuff GDK comments out of the original Xlib version ---- */
898 /* This is commented out because xkb_info is a private struct */
901 /* The Motif VTS doesn't get the help callback called if help
902 * is bound to Shift+<whatever>, and it appears as though it
903 * is XkbTranslateKeyCode that is causing the problem. The
904 * core X version of XTranslateKey always OR's in ShiftMask
905 * and LockMask for mods_rtrn, so this "fix" keeps this behavior
906 * and solves the VTS problem.
908 if ((xkb->dpy)&&(xkb->dpy->xkb_info)&&
909 (xkb->dpy->xkb_info->xlib_ctrls&XkbLC_AlwaysConsumeShiftAndLock)) { *mods_rtrn|= (ShiftMask|LockMask);
913 /* ---- End stuff GDK comments out of the original Xlib version ---- */
916 /* ---- Begin stuff GDK adds to the original Xlib version ---- */
919 *group_rtrn = effectiveGroup;
921 /* ---- End stuff GDK adds to the original Xlib version ---- */
923 return (syms[col]!=NoSymbol);
925 #endif /* HAVE_XKB */
928 * gdk_keymap_translate_keyboard_state:
929 * @keymap: a #GdkKeymap, or %NULL to use the default
930 * @hardware_keycode: a keycode
931 * @state: a modifier state
932 * @group: active keyboard group
933 * @keyval: return location for keyval
934 * @effective_group: return location for effective group
935 * @level: return location for level
936 * @consumed_modifiers: return location for modifiers that were used to determine the group or level
939 * Translates the contents of a #GdkEventKey into a keyval, effective
940 * group, and level. Modifiers that affected the translation and
941 * are thus unavailable for application use are returned in
942 * @consumed_modifiers. See gdk_keyval_get_keys() for an explanation of
943 * groups and levels. The @effective_group is the group that was
944 * actually used for the translation; some keys such as Enter are not
945 * affected by the active keyboard group. The @level is derived from
946 * @state. For convenience, #GdkEventKey already contains the translated
947 * keyval, so this function isn't as useful as you might think.
949 * Return value: %TRUE if there was a keyval bound to the keycode/state/group
952 gdk_keymap_translate_keyboard_state (GdkKeymap *keymap,
953 guint hardware_keycode,
954 GdkModifierType state,
957 gint *effective_group,
959 GdkModifierType *consumed_modifiers)
961 GdkKeymapX11 *keymap_x11;
962 KeySym tmp_keyval = NoSymbol;
965 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
966 g_return_val_if_fail (group < 4, FALSE);
968 keymap_x11 = GDK_KEYMAP_X11 (keymap);
973 *effective_group = 0;
976 if (consumed_modifiers)
977 *consumed_modifiers = 0;
979 update_keyrange (keymap_x11);
981 if (hardware_keycode < keymap_x11->min_keycode ||
982 hardware_keycode > keymap_x11->max_keycode)
986 if (KEYMAP_USE_XKB (keymap))
988 XkbDescRec *xkb = get_xkb (keymap_x11);
990 /* replace bits 13 and 14 with the provided group */
991 state &= ~(1 << 13 | 1 << 14);
992 state |= group << 13;
994 MyEnhancedXkbTranslateKeyCode (xkb,
1002 if (state & ~tmp_modifiers & LockMask)
1003 tmp_keyval = gdk_keyval_to_upper (tmp_keyval);
1005 /* We need to augment the consumed modifiers with LockMask, since
1006 * we handle that ourselves, and also with the group bits
1008 tmp_modifiers |= LockMask | 1 << 13 | 1 << 14;
1013 const KeySym *map = get_keymap (keymap_x11);
1016 gboolean ignore_shift = FALSE;
1017 gboolean ignore_group = FALSE;
1019 if ((state & GDK_SHIFT_MASK) &&
1020 (state & GDK_LOCK_MASK))
1021 shift_level = 0; /* shift disables shift lock */
1022 else if ((state & GDK_SHIFT_MASK) ||
1023 (state & GDK_LOCK_MASK))
1028 syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1030 #define SYM(k,g,l) syms[KEYSYM_INDEX (k,g,l)]
1032 /* Drop group and shift if there are no keysymbols on
1033 * the specified key.
1035 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1038 ignore_shift = TRUE;
1040 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, 0, shift_level))
1043 ignore_group = TRUE;
1045 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, 0, 0))
1049 ignore_group = TRUE;
1050 ignore_shift = TRUE;
1053 /* See whether the group and shift level actually mattered
1054 * to know what to put in consumed_modifiers
1056 if (!SYM (keymap_x11, group, 1) ||
1057 SYM (keymap_x11, group, 0) == SYM (keymap_x11, group, 1))
1058 ignore_shift = TRUE;
1060 if (!SYM (keymap_x11, 1, shift_level) ||
1061 SYM (keymap_x11, 0, shift_level) == SYM (keymap_x11, 1, shift_level))
1062 ignore_group = TRUE;
1064 tmp_keyval = SYM (keymap_x11, group, shift_level);
1066 tmp_modifiers = ignore_group ? 0 : keymap_x11->group_switch_mask;
1067 tmp_modifiers |= ignore_shift ? 0 : (GDK_SHIFT_MASK | GDK_LOCK_MASK);
1069 if (effective_group)
1070 *effective_group = group;
1073 *level = shift_level;
1077 if (consumed_modifiers)
1078 *consumed_modifiers = tmp_modifiers;
1081 *keyval = tmp_keyval;
1083 return tmp_keyval != NoSymbol;
1087 /* Key handling not part of the keymap */
1090 gdk_keyval_name (guint keyval)
1098 case GDK_KP_Page_Up:
1099 return "KP_Page_Up";
1100 case GDK_KP_Page_Down:
1101 return "KP_Page_Down";
1104 return XKeysymToString (keyval);
1108 gdk_keyval_from_name (const gchar *keyval_name)
1110 g_return_val_if_fail (keyval_name != NULL, 0);
1112 return XStringToKeysym (keyval_name);
1115 #ifdef HAVE_XCONVERTCASE
1117 gdk_keyval_convert_case (guint symbol,
1124 /* Check for directly encoded 24-bit UCS characters: */
1125 if ((symbol & 0xff000000) == 0x01000000)
1128 *lower = gdk_unicode_to_keyval (g_unichar_tolower (symbol & 0x00ffffff));
1130 *upper = gdk_unicode_to_keyval (g_unichar_toupper (symbol & 0x00ffffff));
1135 XConvertCase (symbol, &xlower, &xupper);
1142 #endif /* HAVE_XCONVERTCASE */
1145 _gdk_x11_get_group_for_state (GdkDisplay *display,
1146 GdkModifierType state)
1148 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1151 if (display_x11->use_xkb)
1153 return XkbGroupForCoreState (state);
1158 GdkKeymapX11 *keymap_impl = GDK_KEYMAP_X11 (gdk_keymap_get_for_display (display));
1159 update_keymaps (keymap_impl);
1160 return (state & keymap_impl->group_switch_mask) ? 1 : 0;