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;
56 typedef struct _GdkKeymapClass GdkKeymapX11Class;
58 #define GDK_TYPE_KEYMAP_X11 (gdk_keymap_x11_get_type ())
59 #define GDK_KEYMAP_X11(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_KEYMAP_X11, GdkKeymapX11))
60 #define GDK_IS_KEYMAP_X11(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_KEYMAP_X11))
62 typedef struct _DirectionCacheEntry DirectionCacheEntry;
64 struct _DirectionCacheEntry
68 PangoDirection direction;
73 GdkKeymap parent_instance;
78 gint keysyms_per_keycode;
79 XModifierKeymap* mod_keymap;
81 GdkModifierType group_switch_mask;
82 GdkModifierType num_lock_mask;
83 GdkModifierType modmap[8];
84 PangoDirection current_direction;
86 guint have_direction : 1;
87 guint caps_lock_state : 1;
88 guint num_lock_state : 1;
93 /* We cache the directions */
94 Atom current_group_atom;
95 guint current_cache_serial;
96 /* A cache of size four should be more than enough, people usually
97 * have two groups around, and the xkb limit is four. It still
98 * works correct for more than four groups. It's just the
101 DirectionCacheEntry group_direction_cache[4];
105 #define KEYMAP_USE_XKB(keymap) GDK_DISPLAY_X11 ((keymap)->display)->use_xkb
106 #define KEYMAP_XDISPLAY(keymap) GDK_DISPLAY_XDISPLAY ((keymap)->display)
108 static GType gdk_keymap_x11_get_type (void);
109 static void gdk_keymap_x11_class_init (GdkKeymapX11Class *klass);
110 static void gdk_keymap_x11_init (GdkKeymapX11 *keymap);
111 static void gdk_keymap_x11_finalize (GObject *object);
113 static GdkKeymapClass *parent_class = NULL;
116 gdk_keymap_x11_get_type (void)
118 static GType object_type = 0;
122 const GTypeInfo object_info =
124 sizeof (GdkKeymapClass),
125 (GBaseInitFunc) NULL,
126 (GBaseFinalizeFunc) NULL,
127 (GClassInitFunc) gdk_keymap_x11_class_init,
128 NULL, /* class_finalize */
129 NULL, /* class_data */
130 sizeof (GdkKeymapX11),
132 (GInstanceInitFunc) gdk_keymap_x11_init,
135 object_type = g_type_register_static (GDK_TYPE_KEYMAP,
136 g_intern_static_string ("GdkKeymapX11"),
144 gdk_keymap_x11_class_init (GdkKeymapX11Class *klass)
146 GObjectClass *object_class = G_OBJECT_CLASS (klass);
148 parent_class = g_type_class_peek_parent (klass);
150 object_class->finalize = gdk_keymap_x11_finalize;
154 gdk_keymap_x11_init (GdkKeymapX11 *keymap)
156 keymap->min_keycode = 0;
157 keymap->max_keycode = 0;
159 keymap->keymap = NULL;
160 keymap->keysyms_per_keycode = 0;
161 keymap->mod_keymap = NULL;
163 keymap->num_lock_mask = 0;
164 keymap->sun_keypad = FALSE;
165 keymap->group_switch_mask = 0;
166 keymap->lock_keysym = GDK_Caps_Lock;
167 keymap->have_direction = FALSE;
168 keymap->current_serial = 0;
171 keymap->xkb_desc = NULL;
172 keymap->current_group_atom = 0;
173 keymap->current_cache_serial = 0;
179 gdk_keymap_x11_finalize (GObject *object)
181 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (object);
183 if (keymap_x11->keymap)
184 XFree (keymap_x11->keymap);
186 if (keymap_x11->mod_keymap)
187 XFreeModifiermap (keymap_x11->mod_keymap);
190 if (keymap_x11->xkb_desc)
191 XkbFreeKeyboard (keymap_x11->xkb_desc, XkbAllComponentsMask, True);
194 G_OBJECT_CLASS (parent_class)->finalize (object);
198 update_keyrange (GdkKeymapX11 *keymap_x11)
200 if (keymap_x11->max_keycode == 0)
201 XDisplayKeycodes (KEYMAP_XDISPLAY (GDK_KEYMAP (keymap_x11)),
202 &keymap_x11->min_keycode, &keymap_x11->max_keycode);
208 update_modmap (Display *display,
209 GdkKeymapX11 *keymap_x11)
214 GdkModifierType mask;
216 { "Meta", 0, GDK_META_MASK },
217 { "Super", 0, GDK_SUPER_MASK },
218 { "Hyper", 0, GDK_HYPER_MASK },
225 for (i = 0; vmods[i].name; i++)
226 vmods[i].atom = XInternAtom (display, vmods[i].name, FALSE);
228 for (i = 0; i < 8; i++)
229 keymap_x11->modmap[i] = 1 << i;
231 for (i = 0; i < XkbNumVirtualMods; i++)
233 for (j = 0; vmods[j].atom; j++)
235 if (keymap_x11->xkb_desc->names->vmods[i] == vmods[j].atom)
237 for (k = 0; k < 8; k++)
239 if (keymap_x11->xkb_desc->server->vmods[i] & (1 << k))
240 keymap_x11->modmap[k] |= vmods[j].mask;
248 get_xkb (GdkKeymapX11 *keymap_x11)
250 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_KEYMAP (keymap_x11)->display);
251 Display *xdisplay = display_x11->xdisplay;
253 update_keyrange (keymap_x11);
255 if (keymap_x11->xkb_desc == NULL)
257 keymap_x11->xkb_desc = XkbGetMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask | XkbModifierMapMask | XkbVirtualModsMask, XkbUseCoreKbd);
258 if (keymap_x11->xkb_desc == NULL)
260 g_error ("Failed to get keymap");
264 XkbGetNames (xdisplay, XkbGroupNamesMask | XkbVirtualModNamesMask, keymap_x11->xkb_desc);
266 update_modmap (xdisplay, keymap_x11);
268 else if (keymap_x11->current_serial != display_x11->keymap_serial)
270 XkbGetUpdatedMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask | XkbModifierMapMask | XkbVirtualModsMask,
271 keymap_x11->xkb_desc);
272 XkbGetNames (xdisplay, XkbGroupNamesMask | XkbVirtualModNamesMask, keymap_x11->xkb_desc);
274 update_modmap (xdisplay, keymap_x11);
277 keymap_x11->current_serial = display_x11->keymap_serial;
279 if (keymap_x11->num_lock_mask == 0)
280 keymap_x11->num_lock_mask = XkbKeysymToModifiers (KEYMAP_XDISPLAY (GDK_KEYMAP (keymap_x11)), XK_Num_Lock);
282 return keymap_x11->xkb_desc;
284 #endif /* HAVE_XKB */
286 /* Whether we were able to turn on detectable-autorepeat using
287 * XkbSetDetectableAutorepeat. If FALSE, we'll fall back
288 * to checking the next event with XPending().
292 * gdk_keymap_get_for_display:
293 * @display: the #GdkDisplay.
294 * @returns: the #GdkKeymap attached to @display.
296 * Returns the #GdkKeymap attached to @display.
301 gdk_keymap_get_for_display (GdkDisplay *display)
303 GdkDisplayX11 *display_x11;
304 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
305 display_x11 = GDK_DISPLAY_X11 (display);
307 if (!display_x11->keymap)
308 display_x11->keymap = g_object_new (gdk_keymap_x11_get_type (), NULL);
310 display_x11->keymap->display = display;
312 return display_x11->keymap;
315 /* Find the index of the group/level pair within the keysyms for a key.
316 * We round up the number of keysyms per keycode to the next even number,
317 * otherwise we lose a whole group of keys
319 #define KEYSYM_INDEX(keymap_impl, group, level) \
320 (2 * ((group) % (gint)((keymap_impl->keysyms_per_keycode + 1) / 2)) + (level))
321 #define KEYSYM_IS_KEYPAD(s) (((s) >= 0xff80 && (s) <= 0xffbd) || \
322 ((s) >= 0x11000000 && (s) <= 0x1100ffff))
325 get_symbol (const KeySym *syms,
326 GdkKeymapX11 *keymap_x11,
332 index = KEYSYM_INDEX(keymap_x11, group, level);
333 if (index >= keymap_x11->keysyms_per_keycode)
340 set_symbol (KeySym *syms,
341 GdkKeymapX11 *keymap_x11,
348 index = KEYSYM_INDEX(keymap_x11, group, level);
349 if (index >= keymap_x11->keysyms_per_keycode)
356 update_keymaps (GdkKeymapX11 *keymap_x11)
358 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_KEYMAP (keymap_x11)->display);
359 Display *xdisplay = display_x11->xdisplay;
362 g_assert (!KEYMAP_USE_XKB (GDK_KEYMAP (keymap_x11)));
365 if (keymap_x11->keymap == NULL ||
366 keymap_x11->current_serial != display_x11->keymap_serial)
372 keymap_x11->current_serial = display_x11->keymap_serial;
374 update_keyrange (keymap_x11);
376 if (keymap_x11->keymap)
377 XFree (keymap_x11->keymap);
379 if (keymap_x11->mod_keymap)
380 XFreeModifiermap (keymap_x11->mod_keymap);
382 keymap_x11->keymap = XGetKeyboardMapping (xdisplay, keymap_x11->min_keycode,
383 keymap_x11->max_keycode - keymap_x11->min_keycode + 1,
384 &keymap_x11->keysyms_per_keycode);
387 /* GDK_ISO_Left_Tab, as usually configured through XKB, really messes
388 * up the whole idea of "consumed modifiers" because shift is consumed.
389 * However, <shift>Tab is not usually GDK_ISO_Left_Tab without XKB,
390 * we we fudge the map here.
392 keycode = keymap_x11->min_keycode;
393 while (keycode <= keymap_x11->max_keycode)
395 KeySym *syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
396 /* Check both groups */
397 for (i = 0 ; i < 2 ; i++)
399 if (get_symbol (syms, keymap_x11, i, 0) == GDK_Tab)
400 set_symbol (syms, keymap_x11, i, 1, GDK_ISO_Left_Tab);
404 * If there is one keysym and the key symbol has upper and lower
405 * case variants fudge the keymap
407 if (get_symbol (syms, keymap_x11, 0, 1) == 0)
412 gdk_keyval_convert_case (get_symbol (syms, keymap_x11, 0, 0), &lower, &upper);
415 set_symbol (syms, keymap_x11, 0, 0, lower);
416 set_symbol (syms, keymap_x11, 0, 1, upper);
424 keymap_x11->mod_keymap = XGetModifierMapping (xdisplay);
426 keymap_x11->lock_keysym = GDK_VoidSymbol;
427 keymap_x11->group_switch_mask = 0;
428 keymap_x11->num_lock_mask = 0;
430 for (i = 0; i < 8; i++)
431 keymap_x11->modmap[i] = 1 << i;
433 /* There are 8 sets of modifiers, with each set containing
434 * max_keypermod keycodes.
436 map_size = 8 * keymap_x11->mod_keymap->max_keypermod;
437 for (i = 0; i < map_size; i++)
439 /* Get the key code at this point in the map. */
440 gint keycode = keymap_x11->mod_keymap->modifiermap[i];
445 /* Ignore invalid keycodes. */
446 if (keycode < keymap_x11->min_keycode ||
447 keycode > keymap_x11->max_keycode)
450 syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
453 for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
455 if (syms[j] == GDK_Meta_L ||
456 syms[j] == GDK_Meta_R)
457 mask |= GDK_META_MASK;
458 else if (syms[j] == GDK_Hyper_L ||
459 syms[j] == GDK_Hyper_R)
460 mask |= GDK_HYPER_MASK;
461 else if (syms[j] == GDK_Super_L ||
462 syms[j] == GDK_Super_R)
463 mask |= GDK_SUPER_MASK;
466 keymap_x11->modmap[i/keymap_x11->mod_keymap->max_keypermod] |= mask;
468 /* The fourth modifier, GDK_MOD1_MASK is 1 << 3.
469 * Each group of max_keypermod entries refers to the same modifier.
471 mask = 1 << (i / keymap_x11->mod_keymap->max_keypermod);
476 /* Get the Lock keysym. If any keysym bound to the Lock modifier
477 * is Caps_Lock, we will interpret the modifier as Caps_Lock;
478 * otherwise, if any is bound to Shift_Lock, we will interpret
479 * the modifier as Shift_Lock. Otherwise, the lock modifier
482 for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
484 if (syms[j] == GDK_Caps_Lock)
485 keymap_x11->lock_keysym = GDK_Caps_Lock;
486 else if (syms[j] == GDK_Shift_Lock &&
487 keymap_x11->lock_keysym == GDK_VoidSymbol)
488 keymap_x11->lock_keysym = GDK_Shift_Lock;
492 case GDK_CONTROL_MASK:
495 /* Some keyboard maps are known to map Mode_Switch as an
496 * extra Mod1 key. In circumstances like that, it won't be
497 * used to switch groups.
502 /* Find the Mode_Switch and Num_Lock modifiers. */
503 for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
505 if (syms[j] == GDK_Mode_switch)
507 /* This modifier swaps groups */
508 keymap_x11->group_switch_mask |= mask;
510 else if (syms[j] == GDK_Num_Lock)
512 /* This modifier is used for Num_Lock */
513 keymap_x11->num_lock_mask |= mask;
520 /* Hack: The Sun X server puts the keysym to use when the Num Lock
521 * modifier is on in the third element of the keysym array, instead
524 if ((strcmp (ServerVendor (xdisplay), "Sun Microsystems, Inc.") == 0) &&
525 (keymap_x11->keysyms_per_keycode > 2))
526 keymap_x11->sun_keypad = TRUE;
528 keymap_x11->sun_keypad = FALSE;
533 get_keymap (GdkKeymapX11 *keymap_x11)
535 update_keymaps (keymap_x11);
537 return keymap_x11->keymap;
540 #define GET_EFFECTIVE_KEYMAP(keymap) get_effective_keymap ((keymap), G_STRFUNC)
543 get_effective_keymap (GdkKeymap *keymap,
544 const char *function)
549 g_message ("reverting to default display keymap in %s",
551 return gdk_keymap_get_default ();
558 static PangoDirection
559 get_direction (XkbDescRec *xkb,
564 gint rtl_minus_ltr = 0; /* total number of RTL keysyms minus LTR ones */
566 for (code = xkb->min_key_code; code <= xkb->max_key_code; code++)
569 KeySym sym = XkbKeySymEntry (xkb, code, level, group);
570 PangoDirection dir = pango_unichar_direction (gdk_keyval_to_unicode (sym));
574 case PANGO_DIRECTION_RTL:
577 case PANGO_DIRECTION_LTR:
585 if (rtl_minus_ltr > 0)
586 return PANGO_DIRECTION_RTL;
588 return PANGO_DIRECTION_LTR;
591 static PangoDirection
592 get_direction_from_cache (GdkKeymapX11 *keymap_x11,
596 Atom group_atom = xkb->names->groups[group];
598 gboolean cache_hit = FALSE;
599 DirectionCacheEntry *cache = keymap_x11->group_direction_cache;
601 PangoDirection direction = PANGO_DIRECTION_NEUTRAL;
604 if (keymap_x11->have_direction)
606 /* lookup in cache */
607 for (i = 0; i < G_N_ELEMENTS (keymap_x11->group_direction_cache); i++)
609 if (cache[i].group_atom == group_atom)
612 cache[i].serial = keymap_x11->current_cache_serial++; /* freshen */
613 direction = cache[i].direction;
614 group_atom = cache[i].group_atom;
621 /* initialize cache */
622 for (i = 0; i < G_N_ELEMENTS (keymap_x11->group_direction_cache); i++)
624 cache[i].group_atom = 0;
625 cache[i].serial = keymap_x11->current_cache_serial;
627 keymap_x11->current_cache_serial++;
630 /* insert in cache */
635 direction = get_direction (xkb, group);
637 /* remove the oldest entry */
638 for (i = 0; i < G_N_ELEMENTS (keymap_x11->group_direction_cache); i++)
640 if (cache[i].serial < cache[oldest].serial)
644 cache[oldest].group_atom = group_atom;
645 cache[oldest].direction = direction;
646 cache[oldest].serial = keymap_x11->current_cache_serial++;
653 get_num_groups (GdkKeymap *keymap,
656 Display *display = KEYMAP_XDISPLAY (keymap);
657 XkbGetControls(display, XkbSlowKeysMask, xkb);
658 XkbGetUpdatedMap (display, XkbKeySymsMask | XkbKeyTypesMask |
659 XkbModifierMapMask | XkbVirtualModsMask, xkb);
660 return xkb->ctrls->num_groups;
664 update_direction (GdkKeymapX11 *keymap_x11,
667 XkbDescPtr xkb = get_xkb (keymap_x11);
669 gboolean had_direction;
670 PangoDirection old_direction;
672 had_direction = keymap_x11->have_direction;
673 old_direction = keymap_x11->current_direction;
675 group_atom = xkb->names->groups[group];
677 /* a group change? */
678 if (!keymap_x11->have_direction || keymap_x11->current_group_atom != group_atom)
680 keymap_x11->current_direction = get_direction_from_cache (keymap_x11, xkb, group);
681 keymap_x11->current_group_atom = group_atom;
682 keymap_x11->have_direction = TRUE;
685 return !had_direction || old_direction != keymap_x11->current_direction;
689 update_lock_state (GdkKeymapX11 *keymap_x11,
692 gboolean caps_lock_state;
693 gboolean num_lock_state;
695 caps_lock_state = keymap_x11->caps_lock_state;
696 num_lock_state = keymap_x11->num_lock_state;
698 keymap_x11->caps_lock_state = (locked_mods & GDK_LOCK_MASK) != 0;
699 keymap_x11->num_lock_state = (locked_mods & keymap_x11->num_lock_mask) != 0;
701 return (caps_lock_state != keymap_x11->caps_lock_state)
702 || (num_lock_state != keymap_x11->num_lock_state);
705 /* keep this in sync with the XkbSelectEventDetails() call
706 * in gdk_display_open()
709 _gdk_keymap_state_changed (GdkDisplay *display,
712 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
713 XkbEvent *xkb_event = (XkbEvent *)xevent;
715 if (display_x11->keymap)
717 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (display_x11->keymap);
719 if (update_direction (keymap_x11, XkbStateGroup (&xkb_event->state)))
720 g_signal_emit_by_name (keymap_x11, "direction-changed");
722 if (update_lock_state (keymap_x11, xkb_event->state.locked_mods))
723 g_signal_emit_by_name (keymap_x11, "state-changed");
727 #endif /* HAVE_XKB */
730 _gdk_keymap_keys_changed (GdkDisplay *display)
732 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
734 ++display_x11->keymap_serial;
736 if (display_x11->keymap)
737 g_signal_emit_by_name (display_x11->keymap, "keys_changed", 0);
741 * gdk_keymap_get_direction:
742 * @keymap: a #GdkKeymap or %NULL to use the default keymap
744 * Returns the direction of effective layout of the keymap.
746 * Returns: %PANGO_DIRECTION_LTR or %PANGO_DIRECTION_RTL
747 * if it can determine the direction. %PANGO_DIRECTION_NEUTRAL
751 gdk_keymap_get_direction (GdkKeymap *keymap)
753 keymap = GET_EFFECTIVE_KEYMAP (keymap);
756 if (KEYMAP_USE_XKB (keymap))
758 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (keymap);
760 if (!keymap_x11->have_direction)
762 GdkDisplay *display = GDK_KEYMAP (keymap_x11)->display;
763 XkbStateRec state_rec;
765 XkbGetState (GDK_DISPLAY_XDISPLAY (display), XkbUseCoreKbd,
767 update_direction (keymap_x11, XkbStateGroup (&state_rec));
770 return keymap_x11->current_direction;
773 #endif /* HAVE_XKB */
774 return PANGO_DIRECTION_NEUTRAL;
778 * gdk_keymap_have_bidi_layouts:
779 * @keymap: a #GdkKeymap or %NULL to use the default keymap
781 * Determines if keyboard layouts for both right-to-left and left-to-right
782 * languages are in use.
784 * Returns: %TRUE if there are layouts in both directions, %FALSE otherwise
789 gdk_keymap_have_bidi_layouts (GdkKeymap *keymap)
791 keymap = GET_EFFECTIVE_KEYMAP (keymap);
794 if (KEYMAP_USE_XKB (keymap))
796 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (keymap);
797 XkbDescPtr xkb = get_xkb (keymap_x11);
798 int num_groups = get_num_groups (keymap, xkb);
801 gboolean have_ltr_keyboard = FALSE;
802 gboolean have_rtl_keyboard = FALSE;
804 for (i = 0; i < num_groups; i++)
806 if (get_direction_from_cache (keymap_x11, xkb, i) == PANGO_DIRECTION_RTL)
807 have_rtl_keyboard = TRUE;
809 have_ltr_keyboard = TRUE;
812 return have_ltr_keyboard && have_rtl_keyboard;
815 #endif /* HAVE_XKB */
820 * gdk_keymap_get_caps_lock_state:
821 * @keymap: a #GdkKeymap
823 * Returns whether the Caps Lock modifer is locked.
825 * Returns: %TRUE if Caps Lock is on
830 gdk_keymap_get_caps_lock_state (GdkKeymap *keymap)
832 GdkKeymapX11 *keymap_x11;
834 keymap = GET_EFFECTIVE_KEYMAP (keymap);
836 keymap_x11 = GDK_KEYMAP_X11 (keymap);
838 return keymap_x11->caps_lock_state;
842 * gdk_keymap_get_num_lock_state:
843 * @keymap: a #GdkKeymap
845 * Returns whether the Num Lock modifer is locked.
847 * Returns: %TRUE if Num Lock is on
852 gdk_keymap_get_num_lock_state (GdkKeymap *keymap)
854 GdkKeymapX11 *keymap_x11;
856 keymap = GET_EFFECTIVE_KEYMAP (keymap);
858 keymap_x11 = GDK_KEYMAP_X11 (keymap);
860 return keymap_x11->num_lock_state;
864 * gdk_keymap_get_entries_for_keyval:
865 * @keymap: (allow-none): a #GdkKeymap, or %NULL to use the default keymap
866 * @keyval: a keyval, such as %GDK_a, %GDK_Up, %GDK_Return, etc.
867 * @keys: (out): return location for an array of #GdkKeymapKey
868 * @n_keys: (out): return location for number of elements in returned array
870 * Obtains a list of keycode/group/level combinations that will
871 * generate @keyval. Groups and levels are two kinds of keyboard mode;
872 * in general, the level determines whether the top or bottom symbol
873 * on a key is used, and the group determines whether the left or
874 * right symbol is used. On US keyboards, the shift key changes the
875 * keyboard level, and there are no groups. A group switch key might
876 * convert a keyboard between Hebrew to English modes, for example.
877 * #GdkEventKey contains a %group field that indicates the active
878 * keyboard group. The level is computed from the modifier mask.
879 * The returned array should be freed
882 * Return value: %TRUE if keys were found and returned
885 gdk_keymap_get_entries_for_keyval (GdkKeymap *keymap,
891 GdkKeymapX11 *keymap_x11;
893 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
894 g_return_val_if_fail (keys != NULL, FALSE);
895 g_return_val_if_fail (n_keys != NULL, FALSE);
896 g_return_val_if_fail (keyval != 0, FALSE);
898 keymap = GET_EFFECTIVE_KEYMAP (keymap);
899 keymap_x11 = GDK_KEYMAP_X11 (keymap);
901 retval = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
904 if (KEYMAP_USE_XKB (keymap))
906 /* See sec 15.3.4 in XKB docs */
908 XkbDescRec *xkb = get_xkb (keymap_x11);
911 keycode = keymap_x11->min_keycode;
913 while (keycode <= keymap_x11->max_keycode)
915 gint max_shift_levels = XkbKeyGroupsWidth (xkb, keycode); /* "key width" */
918 gint total_syms = XkbKeyNumSyms (xkb, keycode);
922 /* entry is an array with all syms for group 0, all
923 * syms for group 1, etc. and for each group the
924 * shift level syms are in order
926 entry = XkbKeySymsPtr (xkb, keycode);
928 while (i < total_syms)
930 /* check out our cool loop invariant */
931 g_assert (i == (group * max_shift_levels + level));
933 if (entry[i] == keyval)
938 key.keycode = keycode;
942 g_array_append_val (retval, key);
944 g_assert (XkbKeySymEntry (xkb, keycode, level, group) ==
950 if (level == max_shift_levels)
965 const KeySym *map = get_keymap (keymap_x11);
968 keycode = keymap_x11->min_keycode;
969 while (keycode <= keymap_x11->max_keycode)
971 const KeySym *syms = map + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
974 while (i < keymap_x11->keysyms_per_keycode)
976 if (syms[i] == keyval)
981 key.keycode = keycode;
983 /* The "classic" non-XKB keymap has 2 levels per group */
987 g_array_append_val (retval, key);
999 *keys = (GdkKeymapKey*) retval->data;
1000 *n_keys = retval->len;
1008 g_array_free (retval, retval->len > 0 ? FALSE : TRUE);
1014 * gdk_keymap_get_entries_for_keycode:
1015 * @keymap: (allow-none): a #GdkKeymap or %NULL to use the default keymap
1016 * @hardware_keycode: a keycode
1017 * @keys: (out): return location for array of #GdkKeymapKey, or %NULL
1018 * @keyvals: (out): return location for array of keyvals, or %NULL
1019 * @n_entries: length of @keys and @keyvals
1021 * Returns the keyvals bound to @hardware_keycode.
1022 * The Nth #GdkKeymapKey in @keys is bound to the Nth
1023 * keyval in @keyvals. Free the returned arrays with g_free().
1024 * When a keycode is pressed by the user, the keyval from
1025 * this list of entries is selected by considering the effective
1026 * keyboard group and level. See gdk_keymap_translate_keyboard_state().
1028 * Returns: %TRUE if there were any entries
1031 gdk_keymap_get_entries_for_keycode (GdkKeymap *keymap,
1032 guint hardware_keycode,
1033 GdkKeymapKey **keys,
1037 GdkKeymapX11 *keymap_x11;
1040 GArray *keyval_array;
1042 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
1043 g_return_val_if_fail (n_entries != NULL, FALSE);
1045 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1046 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1048 update_keyrange (keymap_x11);
1050 if (hardware_keycode < keymap_x11->min_keycode ||
1051 hardware_keycode > keymap_x11->max_keycode)
1063 key_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
1068 keyval_array = g_array_new (FALSE, FALSE, sizeof (guint));
1070 keyval_array = NULL;
1073 if (KEYMAP_USE_XKB (keymap))
1075 /* See sec 15.3.4 in XKB docs */
1077 XkbDescRec *xkb = get_xkb (keymap_x11);
1078 gint max_shift_levels;
1085 max_shift_levels = XkbKeyGroupsWidth (xkb, hardware_keycode); /* "key width" */
1086 total_syms = XkbKeyNumSyms (xkb, hardware_keycode);
1088 /* entry is an array with all syms for group 0, all
1089 * syms for group 1, etc. and for each group the
1090 * shift level syms are in order
1092 entry = XkbKeySymsPtr (xkb, hardware_keycode);
1094 while (i < total_syms)
1096 /* check out our cool loop invariant */
1097 g_assert (i == (group * max_shift_levels + level));
1103 key.keycode = hardware_keycode;
1107 g_array_append_val (key_array, key);
1111 g_array_append_val (keyval_array, entry[i]);
1115 if (level == max_shift_levels)
1127 const KeySym *map = get_keymap (keymap_x11);
1131 syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1133 while (i < keymap_x11->keysyms_per_keycode)
1139 key.keycode = hardware_keycode;
1141 /* The "classic" non-XKB keymap has 2 levels per group */
1145 g_array_append_val (key_array, key);
1149 g_array_append_val (keyval_array, syms[i]);
1159 *n_entries = key_array->len;
1160 *keys = (GdkKeymapKey*) g_array_free (key_array, FALSE);
1165 *n_entries = keyval_array->len;
1166 *keyvals = (guint*) g_array_free (keyval_array, FALSE);
1169 return *n_entries > 0;
1174 * gdk_keymap_lookup_key:
1175 * @keymap: a #GdkKeymap or %NULL to use the default keymap
1176 * @key: a #GdkKeymapKey with keycode, group, and level initialized
1178 * Looks up the keyval mapped to a keycode/group/level triplet.
1179 * If no keyval is bound to @key, returns 0. For normal user input,
1180 * you want to use gdk_keymap_translate_keyboard_state() instead of
1181 * this function, since the effective group/level may not be
1182 * the same as the current keyboard state.
1184 * Return value: a keyval, or 0 if none was mapped to the given @key
1187 gdk_keymap_lookup_key (GdkKeymap *keymap,
1188 const GdkKeymapKey *key)
1190 GdkKeymapX11 *keymap_x11;
1192 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), 0);
1193 g_return_val_if_fail (key != NULL, 0);
1194 g_return_val_if_fail (key->group < 4, 0);
1196 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1197 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1200 if (KEYMAP_USE_XKB (keymap))
1202 XkbDescRec *xkb = get_xkb (keymap_x11);
1204 return XkbKeySymEntry (xkb, key->keycode, key->level, key->group);
1209 const KeySym *map = get_keymap (keymap_x11);
1210 const KeySym *syms = map + (key->keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1211 return get_symbol (syms, keymap_x11, key->group, key->level);
1216 /* This is copied straight from XFree86 Xlib, to:
1217 * - add the group and level return.
1218 * - change the interpretation of mods_rtrn as described
1219 * in the docs for gdk_keymap_translate_keyboard_state()
1220 * It's unchanged for ease of diff against the Xlib sources; don't
1224 MyEnhancedXkbTranslateKeyCode(register XkbDescPtr xkb,
1226 register unsigned int mods,
1227 unsigned int * mods_rtrn,
1228 KeySym * keysym_rtrn,
1232 XkbKeyTypeRec *type;
1234 unsigned preserve,effectiveGroup;
1237 if (mods_rtrn!=NULL)
1240 nKeyGroups= XkbKeyNumGroups(xkb,key);
1241 if ((!XkbKeycodeInRange(xkb,key))||(nKeyGroups==0)) {
1242 if (keysym_rtrn!=NULL)
1243 *keysym_rtrn = NoSymbol;
1247 syms = XkbKeySymsPtr(xkb,key);
1249 /* find the offset of the effective group */
1251 effectiveGroup= XkbGroupForCoreState(mods);
1252 if ( effectiveGroup>=nKeyGroups ) {
1253 unsigned groupInfo= XkbKeyGroupInfo(xkb,key);
1254 switch (XkbOutOfRangeGroupAction(groupInfo)) {
1256 effectiveGroup %= nKeyGroups;
1258 case XkbClampIntoRange:
1259 effectiveGroup = nKeyGroups-1;
1261 case XkbRedirectIntoRange:
1262 effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
1263 if (effectiveGroup>=nKeyGroups)
1268 col= effectiveGroup*XkbKeyGroupsWidth(xkb,key);
1269 type = XkbKeyKeyType(xkb,key,effectiveGroup);
1272 if (type->map) { /* find the column (shift level) within the group */
1274 register XkbKTMapEntryPtr entry;
1275 /* ---- Begin section modified for GDK ---- */
1278 for (i=0,entry=type->map;i<type->map_count;i++,entry++) {
1281 unsigned long tmp = entry->mods.mask;
1287 /* We always add one-modifiers levels to mods_rtrn since
1288 * they can't wipe out bits in the state unless the
1289 * level would be triggered. But return other modifiers
1292 if (bits == 1 || (mods&type->mods.mask)==entry->mods.mask)
1293 *mods_rtrn |= entry->mods.mask;
1296 if (!found&&entry->active&&((mods&type->mods.mask)==entry->mods.mask)) {
1299 preserve= type->preserve[i].mask;
1302 *level_rtrn = entry->level;
1307 /* ---- End section modified for GDK ---- */
1310 if (keysym_rtrn!=NULL)
1311 *keysym_rtrn= syms[col];
1313 /* ---- Begin section modified for GDK ---- */
1314 *mods_rtrn &= ~preserve;
1315 /* ---- End section modified for GDK ---- */
1317 /* ---- Begin stuff GDK comments out of the original Xlib version ---- */
1318 /* This is commented out because xkb_info is a private struct */
1321 /* The Motif VTS doesn't get the help callback called if help
1322 * is bound to Shift+<whatever>, and it appears as though it
1323 * is XkbTranslateKeyCode that is causing the problem. The
1324 * core X version of XTranslateKey always OR's in ShiftMask
1325 * and LockMask for mods_rtrn, so this "fix" keeps this behavior
1326 * and solves the VTS problem.
1328 if ((xkb->dpy)&&(xkb->dpy->xkb_info)&&
1329 (xkb->dpy->xkb_info->xlib_ctrls&XkbLC_AlwaysConsumeShiftAndLock)) { *mods_rtrn|= (ShiftMask|LockMask);
1333 /* ---- End stuff GDK comments out of the original Xlib version ---- */
1336 /* ---- Begin stuff GDK adds to the original Xlib version ---- */
1339 *group_rtrn = effectiveGroup;
1341 /* ---- End stuff GDK adds to the original Xlib version ---- */
1343 return (syms[col] != NoSymbol);
1345 #endif /* HAVE_XKB */
1347 /* Translates from keycode/state to keysymbol using the traditional interpretation
1348 * of the keyboard map. See section 12.7 of the Xlib reference manual
1351 translate_keysym (GdkKeymapX11 *keymap_x11,
1352 guint hardware_keycode,
1354 GdkModifierType state,
1355 gint *effective_group,
1356 gint *effective_level)
1358 const KeySym *map = get_keymap (keymap_x11);
1359 const KeySym *syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1361 #define SYM(k,g,l) get_symbol (syms, k,g,l)
1363 GdkModifierType shift_modifiers;
1366 gint num_lock_index;
1368 shift_modifiers = GDK_SHIFT_MASK;
1369 if (keymap_x11->lock_keysym == GDK_Shift_Lock)
1370 shift_modifiers |= GDK_LOCK_MASK;
1372 /* Fall back to the first group if the passed in group is empty
1374 if (!(SYM (keymap_x11, group, 0) || SYM (keymap_x11, group, 1)) &&
1375 (SYM (keymap_x11, 0, 0) || SYM (keymap_x11, 0, 1)))
1378 /* Hack: On Sun, the Num Lock modifier uses the third element in the
1379 * keysym array, and Mode_Switch does not apply for a keypad key.
1381 if (keymap_x11->sun_keypad)
1389 for (i = 0; i < keymap_x11->keysyms_per_keycode; i++)
1390 if (KEYSYM_IS_KEYPAD (SYM (keymap_x11, 0, i)))
1397 if ((state & keymap_x11->num_lock_mask) &&
1398 KEYSYM_IS_KEYPAD (SYM (keymap_x11, group, num_lock_index)))
1400 /* Shift, Shift_Lock cancel Num_Lock
1402 shift_level = (state & shift_modifiers) ? 0 : num_lock_index;
1403 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1406 tmp_keyval = SYM (keymap_x11, group, shift_level);
1410 /* Fall back to the first level if no symbol for the level
1413 shift_level = (state & shift_modifiers) ? 1 : 0;
1414 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1417 tmp_keyval = SYM (keymap_x11, group, shift_level);
1419 if (keymap_x11->lock_keysym == GDK_Caps_Lock && (state & GDK_LOCK_MASK) != 0)
1421 guint upper = gdk_keyval_to_upper (tmp_keyval);
1422 if (upper != tmp_keyval)
1427 if (effective_group)
1428 *effective_group = group;
1430 if (effective_level)
1431 *effective_level = shift_level;
1439 * gdk_keymap_translate_keyboard_state:
1440 * @keymap: (allow-none): a #GdkKeymap, or %NULL to use the default
1441 * @hardware_keycode: a keycode
1442 * @state: a modifier state
1443 * @group: active keyboard group
1444 * @keyval: (out) (allow-none): return location for keyval, or %NULL
1445 * @effective_group: (out) (allow-none): return location for effective group, or %NULL
1446 * @level: (out) (allow-none): return location for level, or %NULL
1447 * @consumed_modifiers: (out) (allow-none): return location for modifiers that were used to
1448 * determine the group or level, or %NULL
1450 * Translates the contents of a #GdkEventKey into a keyval, effective
1451 * group, and level. Modifiers that affected the translation and
1452 * are thus unavailable for application use are returned in
1453 * @consumed_modifiers. See <xref linkend="key-group-explanation"/> for an explanation of
1454 * groups and levels. The @effective_group is the group that was
1455 * actually used for the translation; some keys such as Enter are not
1456 * affected by the active keyboard group. The @level is derived from
1457 * @state. For convenience, #GdkEventKey already contains the translated
1458 * keyval, so this function isn't as useful as you might think.
1461 * @consumed_modifiers gives modifiers that should be masked out
1462 * from @state when comparing this key press to a hot key. For
1463 * instance, on a US keyboard, the <literal>plus</literal>
1464 * symbol is shifted, so when comparing a key press to a
1465 * <literal><Control>plus</literal> accelerator <Shift> should
1468 * <informalexample><programlisting>
1469 * /* We want to ignore irrelevant modifiers like ScrollLock */
1470 * #define ALL_ACCELS_MASK (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK)
1471 * gdk_keymap_translate_keyboard_state (keymap, event->hardware_keycode,
1472 * event->state, event->group,
1473 * &keyval, NULL, NULL, &consumed);
1474 * if (keyval == GDK_PLUS &&
1475 * (event->state & ~consumed & ALL_ACCELS_MASK) == GDK_CONTROL_MASK)
1476 * /* Control was pressed */
1477 * </programlisting></informalexample>
1479 * An older interpretation @consumed_modifiers was that it contained
1480 * all modifiers that might affect the translation of the key;
1481 * this allowed accelerators to be stored with irrelevant consumed
1482 * modifiers, by doing:</para>
1483 * <informalexample><programlisting>
1484 * /* XXX Don't do this XXX */
1485 * if (keyval == accel_keyval &&
1486 * (event->state & ~consumed & ALL_ACCELS_MASK) == (accel_mods & ~consumed))
1487 * /* Accelerator was pressed */
1488 * </programlisting></informalexample>
1490 * However, this did not work if multi-modifier combinations were
1491 * used in the keymap, since, for instance, <literal><Control></literal>
1492 * would be masked out even if only <literal><Control><Alt></literal>
1493 * was used in the keymap. To support this usage as well as well as
1494 * possible, all <emphasis>single modifier</emphasis> combinations
1495 * that could affect the key for any combination of modifiers will
1496 * be returned in @consumed_modifiers; multi-modifier combinations
1497 * are returned only when actually found in @state. When you store
1498 * accelerators, you should always store them with consumed modifiers
1499 * removed. Store <literal><Control>plus</literal>,
1500 * not <literal><Control><Shift>plus</literal>,
1503 * Return value: %TRUE if there was a keyval bound to the keycode/state/group
1506 gdk_keymap_translate_keyboard_state (GdkKeymap *keymap,
1507 guint hardware_keycode,
1508 GdkModifierType state,
1511 gint *effective_group,
1513 GdkModifierType *consumed_modifiers)
1515 GdkKeymapX11 *keymap_x11;
1516 KeySym tmp_keyval = NoSymbol;
1517 guint tmp_modifiers;
1519 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
1520 g_return_val_if_fail (group < 4, FALSE);
1522 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1523 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1527 if (effective_group)
1528 *effective_group = 0;
1531 if (consumed_modifiers)
1532 *consumed_modifiers = 0;
1534 update_keyrange (keymap_x11);
1536 if (hardware_keycode < keymap_x11->min_keycode ||
1537 hardware_keycode > keymap_x11->max_keycode)
1541 if (KEYMAP_USE_XKB (keymap))
1543 XkbDescRec *xkb = get_xkb (keymap_x11);
1545 /* replace bits 13 and 14 with the provided group */
1546 state &= ~(1 << 13 | 1 << 14);
1547 state |= group << 13;
1549 MyEnhancedXkbTranslateKeyCode (xkb,
1557 if (state & ~tmp_modifiers & LockMask)
1558 tmp_keyval = gdk_keyval_to_upper (tmp_keyval);
1560 /* We need to augment the consumed modifiers with LockMask, since
1561 * we handle that ourselves, and also with the group bits
1563 tmp_modifiers |= LockMask | 1 << 13 | 1 << 14;
1568 GdkModifierType bit;
1572 /* We see what modifiers matter by trying the translation with
1573 * and without each possible modifier
1575 for (bit = GDK_SHIFT_MASK; bit < GDK_BUTTON1_MASK; bit <<= 1)
1577 /* Handling of the group here is a bit funky; a traditional
1578 * X keyboard map can have more than two groups, but no way
1579 * of accessing the extra groups is defined. We allow a
1580 * caller to pass in any group to this function, but we
1581 * only can represent switching between group 0 and 1 in
1582 * consumed modifiers.
1584 if (translate_keysym (keymap_x11, hardware_keycode,
1585 (bit == keymap_x11->group_switch_mask) ? 0 : group,
1588 translate_keysym (keymap_x11, hardware_keycode,
1589 (bit == keymap_x11->group_switch_mask) ? 1 : group,
1592 tmp_modifiers |= bit;
1595 tmp_keyval = translate_keysym (keymap_x11, hardware_keycode,
1597 level, effective_group);
1600 if (consumed_modifiers)
1601 *consumed_modifiers = tmp_modifiers;
1604 *keyval = tmp_keyval;
1606 return tmp_keyval != NoSymbol;
1610 /* Key handling not part of the keymap */
1613 gdk_keyval_name (guint keyval)
1621 case GDK_KP_Page_Up:
1622 return "KP_Page_Up";
1623 case GDK_KP_Page_Down:
1624 return "KP_Page_Down";
1627 return XKeysymToString (keyval);
1631 gdk_keyval_from_name (const gchar *keyval_name)
1633 g_return_val_if_fail (keyval_name != NULL, 0);
1635 return XStringToKeysym (keyval_name);
1638 #ifdef HAVE_XCONVERTCASE
1640 gdk_keyval_convert_case (guint symbol,
1647 /* Check for directly encoded 24-bit UCS characters: */
1648 if ((symbol & 0xff000000) == 0x01000000)
1651 *lower = gdk_unicode_to_keyval (g_unichar_tolower (symbol & 0x00ffffff));
1653 *upper = gdk_unicode_to_keyval (g_unichar_toupper (symbol & 0x00ffffff));
1658 XConvertCase (symbol, &xlower, &xupper);
1665 #endif /* HAVE_XCONVERTCASE */
1668 _gdk_x11_get_group_for_state (GdkDisplay *display,
1669 GdkModifierType state)
1671 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1674 if (display_x11->use_xkb)
1676 return XkbGroupForCoreState (state);
1681 GdkKeymapX11 *keymap_impl = GDK_KEYMAP_X11 (gdk_keymap_get_for_display (display));
1682 update_keymaps (keymap_impl);
1683 return (state & keymap_impl->group_switch_mask) ? 1 : 0;
1688 _gdk_keymap_add_virtual_modifiers_compat (GdkKeymap *keymap,
1689 GdkModifierType *modifiers)
1691 GdkKeymapX11 *keymap_x11;
1694 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1695 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1697 for (i = 3; i < 8; i++)
1699 if ((1 << i) & *modifiers)
1701 if (keymap_x11->modmap[i] & GDK_MOD1_MASK)
1702 *modifiers |= GDK_MOD1_MASK;
1703 else if (keymap_x11->modmap[i] & GDK_SUPER_MASK)
1704 *modifiers |= GDK_SUPER_MASK;
1705 else if (keymap_x11->modmap[i] & GDK_HYPER_MASK)
1706 *modifiers |= GDK_HYPER_MASK;
1707 else if (keymap_x11->modmap[i] & GDK_META_MASK)
1708 *modifiers |= GDK_META_MASK;
1714 * gdk_keymap_add_virtual_modifiers:
1715 * @keymap: a #GdkKeymap
1716 * @state: pointer to the modifier mask to change
1718 * Adds virtual modifiers (i.e. Super, Hyper and Meta) which correspond
1719 * to the real modifiers (i.e Mod2, Mod3, ...) in @modifiers.
1720 * are set in @state to their non-virtual counterparts (i.e. Mod2,
1721 * Mod3,...) and set the corresponding bits in @state.
1723 * GDK already does this before delivering key events, but for
1724 * compatibility reasons, it only sets the first virtual modifier
1725 * it finds, whereas this function sets all matching virtual modifiers.
1727 * This function is useful when matching key events against
1733 gdk_keymap_add_virtual_modifiers (GdkKeymap *keymap,
1734 GdkModifierType *state)
1736 GdkKeymapX11 *keymap_x11;
1739 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1740 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1742 for (i = 3; i < 8; i++)
1744 if ((1 << i) & *state)
1746 if (keymap_x11->modmap[i] & GDK_MOD1_MASK)
1747 *state |= GDK_MOD1_MASK;
1748 if (keymap_x11->modmap[i] & GDK_SUPER_MASK)
1749 *state |= GDK_SUPER_MASK;
1750 if (keymap_x11->modmap[i] & GDK_HYPER_MASK)
1751 *state |= GDK_HYPER_MASK;
1752 if (keymap_x11->modmap[i] & GDK_META_MASK)
1753 *state |= GDK_META_MASK;
1759 _gdk_keymap_key_is_modifier (GdkKeymap *keymap,
1762 GdkKeymapX11 *keymap_x11;
1765 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1766 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1768 if (keycode < keymap_x11->min_keycode ||
1769 keycode > keymap_x11->max_keycode)
1773 if (KEYMAP_USE_XKB (keymap))
1775 XkbDescRec *xkb = get_xkb (keymap_x11);
1777 if (xkb->map->modmap && xkb->map->modmap[keycode] != 0)
1783 for (i = 0; i < 8 * keymap_x11->mod_keymap->max_keypermod; i++)
1785 if (keycode == keymap_x11->mod_keymap->modifiermap[i])
1794 * gdk_keymap_map_virtual_modifiers:
1795 * @keymap: a #GdkKeymap
1796 * @state: pointer to the modifier state to map
1798 * Maps the virtual modifiers (i.e. Super, Hyper and Meta) which
1799 * are set in @state to their non-virtual counterparts (i.e. Mod2,
1800 * Mod3,...) and set the corresponding bits in @state.
1802 * This function is useful when matching key events against
1805 * Returns: %TRUE if no virtual modifiers were mapped to the
1806 * same non-virtual modifier. Note that %FALSE is also returned
1807 * if a virtual modifier is mapped to a non-virtual modifier that
1808 * was already set in @state.
1813 gdk_keymap_map_virtual_modifiers (GdkKeymap *keymap,
1814 GdkModifierType *state)
1816 GdkKeymapX11 *keymap_x11;
1817 const guint vmods[] = {
1818 GDK_SUPER_MASK, GDK_HYPER_MASK, GDK_META_MASK
1823 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1824 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1826 if (KEYMAP_USE_XKB (keymap))
1827 get_xkb (keymap_x11);
1831 for (j = 0; j < 3; j++)
1833 if (*state & vmods[j])
1835 for (i = 3; i < 8; i++)
1837 if (keymap_x11->modmap[i] & vmods[j])
1839 if (*state & (1 << i))