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/.
30 #include "gdkprivate-x11.h"
31 #include "gdkinternals.h"
32 #include "gdkdisplay-x11.h"
33 #include "gdkkeysyms.h"
43 #include <X11/XKBlib.h>
45 /* OSF-4.0 is apparently missing this macro
47 # ifndef XkbKeySymEntry
48 # define XkbKeySymEntry(d,k,sl,g) \
49 (XkbKeySym(d,k,((XkbKeyGroupsWidth(d,k)*(g))+(sl))))
53 typedef struct _GdkKeymapX11 GdkKeymapX11;
54 typedef struct _GdkKeymapClass GdkKeymapX11Class;
56 #define GDK_TYPE_KEYMAP_X11 (gdk_keymap_x11_get_type ())
57 #define GDK_KEYMAP_X11(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_KEYMAP_X11, GdkKeymapX11))
58 #define GDK_IS_KEYMAP_X11(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_KEYMAP_X11))
60 typedef struct _DirectionCacheEntry DirectionCacheEntry;
62 struct _DirectionCacheEntry
66 PangoDirection direction;
71 GdkKeymap parent_instance;
76 gint keysyms_per_keycode;
77 XModifierKeymap* mod_keymap;
79 GdkModifierType group_switch_mask;
80 GdkModifierType num_lock_mask;
81 GdkModifierType modmap[8];
82 PangoDirection current_direction;
84 guint have_direction : 1;
85 guint caps_lock_state : 1;
86 guint num_lock_state : 1;
91 /* We cache the directions */
92 Atom current_group_atom;
93 guint current_cache_serial;
94 /* A cache of size four should be more than enough, people usually
95 * have two groups around, and the xkb limit is four. It still
96 * works correct for more than four groups. It's just the
99 DirectionCacheEntry group_direction_cache[4];
103 #define KEYMAP_USE_XKB(keymap) GDK_DISPLAY_X11 ((keymap)->display)->use_xkb
104 #define KEYMAP_XDISPLAY(keymap) GDK_DISPLAY_XDISPLAY ((keymap)->display)
106 static GType gdk_keymap_x11_get_type (void);
107 static void gdk_keymap_x11_class_init (GdkKeymapX11Class *klass);
108 static void gdk_keymap_x11_init (GdkKeymapX11 *keymap);
109 static void gdk_keymap_x11_finalize (GObject *object);
111 static GdkKeymapClass *parent_class = NULL;
114 gdk_keymap_x11_get_type (void)
116 static GType object_type = 0;
120 const GTypeInfo object_info =
122 sizeof (GdkKeymapClass),
123 (GBaseInitFunc) NULL,
124 (GBaseFinalizeFunc) NULL,
125 (GClassInitFunc) gdk_keymap_x11_class_init,
126 NULL, /* class_finalize */
127 NULL, /* class_data */
128 sizeof (GdkKeymapX11),
130 (GInstanceInitFunc) gdk_keymap_x11_init,
133 object_type = g_type_register_static (GDK_TYPE_KEYMAP,
134 g_intern_static_string ("GdkKeymapX11"),
142 gdk_keymap_x11_class_init (GdkKeymapX11Class *klass)
144 GObjectClass *object_class = G_OBJECT_CLASS (klass);
146 parent_class = g_type_class_peek_parent (klass);
148 object_class->finalize = gdk_keymap_x11_finalize;
152 gdk_keymap_x11_init (GdkKeymapX11 *keymap)
154 keymap->min_keycode = 0;
155 keymap->max_keycode = 0;
157 keymap->keymap = NULL;
158 keymap->keysyms_per_keycode = 0;
159 keymap->mod_keymap = NULL;
161 keymap->num_lock_mask = 0;
162 keymap->sun_keypad = FALSE;
163 keymap->group_switch_mask = 0;
164 keymap->lock_keysym = GDK_KEY_Caps_Lock;
165 keymap->have_direction = FALSE;
166 keymap->current_serial = 0;
169 keymap->xkb_desc = NULL;
170 keymap->current_group_atom = 0;
171 keymap->current_cache_serial = 0;
177 gdk_keymap_x11_finalize (GObject *object)
179 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (object);
181 if (keymap_x11->keymap)
182 XFree (keymap_x11->keymap);
184 if (keymap_x11->mod_keymap)
185 XFreeModifiermap (keymap_x11->mod_keymap);
188 if (keymap_x11->xkb_desc)
189 XkbFreeKeyboard (keymap_x11->xkb_desc, XkbAllComponentsMask, True);
192 G_OBJECT_CLASS (parent_class)->finalize (object);
196 update_keyrange (GdkKeymapX11 *keymap_x11)
198 if (keymap_x11->max_keycode == 0)
199 XDisplayKeycodes (KEYMAP_XDISPLAY (GDK_KEYMAP (keymap_x11)),
200 &keymap_x11->min_keycode, &keymap_x11->max_keycode);
206 update_modmap (Display *display,
207 GdkKeymapX11 *keymap_x11)
212 GdkModifierType mask;
214 { "Meta", 0, GDK_META_MASK },
215 { "Super", 0, GDK_SUPER_MASK },
216 { "Hyper", 0, GDK_HYPER_MASK },
223 for (i = 0; vmods[i].name; i++)
224 vmods[i].atom = XInternAtom (display, vmods[i].name, FALSE);
226 for (i = 0; i < 8; i++)
227 keymap_x11->modmap[i] = 1 << i;
229 for (i = 0; i < XkbNumVirtualMods; i++)
231 for (j = 0; vmods[j].atom; j++)
233 if (keymap_x11->xkb_desc->names->vmods[i] == vmods[j].atom)
235 for (k = 0; k < 8; k++)
237 if (keymap_x11->xkb_desc->server->vmods[i] & (1 << k))
238 keymap_x11->modmap[k] |= vmods[j].mask;
246 get_xkb (GdkKeymapX11 *keymap_x11)
248 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_KEYMAP (keymap_x11)->display);
249 Display *xdisplay = display_x11->xdisplay;
251 update_keyrange (keymap_x11);
253 if (keymap_x11->xkb_desc == NULL)
255 keymap_x11->xkb_desc = XkbGetMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask | XkbModifierMapMask | XkbVirtualModsMask, XkbUseCoreKbd);
256 if (keymap_x11->xkb_desc == NULL)
258 g_error ("Failed to get keymap");
262 XkbGetNames (xdisplay, XkbGroupNamesMask | XkbVirtualModNamesMask, keymap_x11->xkb_desc);
264 update_modmap (xdisplay, keymap_x11);
266 else if (keymap_x11->current_serial != display_x11->keymap_serial)
268 XkbGetUpdatedMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask | XkbModifierMapMask | XkbVirtualModsMask,
269 keymap_x11->xkb_desc);
270 XkbGetNames (xdisplay, XkbGroupNamesMask | XkbVirtualModNamesMask, keymap_x11->xkb_desc);
272 update_modmap (xdisplay, keymap_x11);
275 keymap_x11->current_serial = display_x11->keymap_serial;
277 if (keymap_x11->num_lock_mask == 0)
278 keymap_x11->num_lock_mask = XkbKeysymToModifiers (KEYMAP_XDISPLAY (GDK_KEYMAP (keymap_x11)), XK_Num_Lock);
280 return keymap_x11->xkb_desc;
282 #endif /* HAVE_XKB */
284 /* Whether we were able to turn on detectable-autorepeat using
285 * XkbSetDetectableAutorepeat. If FALSE, we'll fall back
286 * to checking the next event with XPending().
290 * gdk_keymap_get_for_display:
291 * @display: the #GdkDisplay.
293 * Returns the #GdkKeymap attached to @display.
295 * Return value: (transfer none): the #GdkKeymap attached to @display.
300 gdk_keymap_get_for_display (GdkDisplay *display)
302 GdkDisplayX11 *display_x11;
303 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
304 display_x11 = GDK_DISPLAY_X11 (display);
306 if (!display_x11->keymap)
307 display_x11->keymap = g_object_new (gdk_keymap_x11_get_type (), NULL);
309 display_x11->keymap->display = display;
311 return display_x11->keymap;
314 /* Find the index of the group/level pair within the keysyms for a key.
315 * We round up the number of keysyms per keycode to the next even number,
316 * otherwise we lose a whole group of keys
318 #define KEYSYM_INDEX(keymap_impl, group, level) \
319 (2 * ((group) % (gint)((keymap_impl->keysyms_per_keycode + 1) / 2)) + (level))
320 #define KEYSYM_IS_KEYPAD(s) (((s) >= 0xff80 && (s) <= 0xffbd) || \
321 ((s) >= 0x11000000 && (s) <= 0x1100ffff))
324 get_symbol (const KeySym *syms,
325 GdkKeymapX11 *keymap_x11,
331 index = KEYSYM_INDEX(keymap_x11, group, level);
332 if (index >= keymap_x11->keysyms_per_keycode)
339 set_symbol (KeySym *syms,
340 GdkKeymapX11 *keymap_x11,
347 index = KEYSYM_INDEX(keymap_x11, group, level);
348 if (index >= keymap_x11->keysyms_per_keycode)
355 update_keymaps (GdkKeymapX11 *keymap_x11)
357 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_KEYMAP (keymap_x11)->display);
358 Display *xdisplay = display_x11->xdisplay;
361 g_assert (!KEYMAP_USE_XKB (GDK_KEYMAP (keymap_x11)));
364 if (keymap_x11->keymap == NULL ||
365 keymap_x11->current_serial != display_x11->keymap_serial)
371 keymap_x11->current_serial = display_x11->keymap_serial;
373 update_keyrange (keymap_x11);
375 if (keymap_x11->keymap)
376 XFree (keymap_x11->keymap);
378 if (keymap_x11->mod_keymap)
379 XFreeModifiermap (keymap_x11->mod_keymap);
381 keymap_x11->keymap = XGetKeyboardMapping (xdisplay, keymap_x11->min_keycode,
382 keymap_x11->max_keycode - keymap_x11->min_keycode + 1,
383 &keymap_x11->keysyms_per_keycode);
386 /* GDK_KEY_ISO_Left_Tab, as usually configured through XKB, really messes
387 * up the whole idea of "consumed modifiers" because shift is consumed.
388 * However, <shift>Tab is not usually GDK_KEY_ISO_Left_Tab without XKB,
389 * we we fudge the map here.
391 keycode = keymap_x11->min_keycode;
392 while (keycode <= keymap_x11->max_keycode)
394 KeySym *syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
395 /* Check both groups */
396 for (i = 0 ; i < 2 ; i++)
398 if (get_symbol (syms, keymap_x11, i, 0) == GDK_KEY_Tab)
399 set_symbol (syms, keymap_x11, i, 1, GDK_KEY_ISO_Left_Tab);
403 * If there is one keysym and the key symbol has upper and lower
404 * case variants fudge the keymap
406 if (get_symbol (syms, keymap_x11, 0, 1) == 0)
411 gdk_keyval_convert_case (get_symbol (syms, keymap_x11, 0, 0), &lower, &upper);
414 set_symbol (syms, keymap_x11, 0, 0, lower);
415 set_symbol (syms, keymap_x11, 0, 1, upper);
423 keymap_x11->mod_keymap = XGetModifierMapping (xdisplay);
425 keymap_x11->lock_keysym = GDK_KEY_VoidSymbol;
426 keymap_x11->group_switch_mask = 0;
427 keymap_x11->num_lock_mask = 0;
429 for (i = 0; i < 8; i++)
430 keymap_x11->modmap[i] = 1 << i;
432 /* There are 8 sets of modifiers, with each set containing
433 * max_keypermod keycodes.
435 map_size = 8 * keymap_x11->mod_keymap->max_keypermod;
436 for (i = 0; i < map_size; i++)
438 /* Get the key code at this point in the map. */
439 gint keycode = keymap_x11->mod_keymap->modifiermap[i];
444 /* Ignore invalid keycodes. */
445 if (keycode < keymap_x11->min_keycode ||
446 keycode > keymap_x11->max_keycode)
449 syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
452 for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
454 if (syms[j] == GDK_KEY_Meta_L ||
455 syms[j] == GDK_KEY_Meta_R)
456 mask |= GDK_META_MASK;
457 else if (syms[j] == GDK_KEY_Hyper_L ||
458 syms[j] == GDK_KEY_Hyper_R)
459 mask |= GDK_HYPER_MASK;
460 else if (syms[j] == GDK_KEY_Super_L ||
461 syms[j] == GDK_KEY_Super_R)
462 mask |= GDK_SUPER_MASK;
465 keymap_x11->modmap[i/keymap_x11->mod_keymap->max_keypermod] |= mask;
467 /* The fourth modifier, GDK_MOD1_MASK is 1 << 3.
468 * Each group of max_keypermod entries refers to the same modifier.
470 mask = 1 << (i / keymap_x11->mod_keymap->max_keypermod);
475 /* Get the Lock keysym. If any keysym bound to the Lock modifier
476 * is Caps_Lock, we will interpret the modifier as Caps_Lock;
477 * otherwise, if any is bound to Shift_Lock, we will interpret
478 * the modifier as Shift_Lock. Otherwise, the lock modifier
481 for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
483 if (syms[j] == GDK_KEY_Caps_Lock)
484 keymap_x11->lock_keysym = GDK_KEY_Caps_Lock;
485 else if (syms[j] == GDK_KEY_Shift_Lock &&
486 keymap_x11->lock_keysym == GDK_KEY_VoidSymbol)
487 keymap_x11->lock_keysym = GDK_KEY_Shift_Lock;
491 case GDK_CONTROL_MASK:
494 /* Some keyboard maps are known to map Mode_Switch as an
495 * extra Mod1 key. In circumstances like that, it won't be
496 * used to switch groups.
501 /* Find the Mode_Switch and Num_Lock modifiers. */
502 for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
504 if (syms[j] == GDK_KEY_Mode_switch)
506 /* This modifier swaps groups */
507 keymap_x11->group_switch_mask |= mask;
509 else if (syms[j] == GDK_KEY_Num_Lock)
511 /* This modifier is used for Num_Lock */
512 keymap_x11->num_lock_mask |= mask;
519 /* Hack: The Sun X server puts the keysym to use when the Num Lock
520 * modifier is on in the third element of the keysym array, instead
523 if ((strcmp (ServerVendor (xdisplay), "Sun Microsystems, Inc.") == 0) &&
524 (keymap_x11->keysyms_per_keycode > 2))
525 keymap_x11->sun_keypad = TRUE;
527 keymap_x11->sun_keypad = FALSE;
532 get_keymap (GdkKeymapX11 *keymap_x11)
534 update_keymaps (keymap_x11);
536 return keymap_x11->keymap;
539 #define GET_EFFECTIVE_KEYMAP(keymap) get_effective_keymap ((keymap), G_STRFUNC)
542 get_effective_keymap (GdkKeymap *keymap,
543 const char *function)
548 g_message ("reverting to default display keymap in %s",
550 return gdk_keymap_get_default ();
557 static PangoDirection
558 get_direction (XkbDescRec *xkb,
563 gint rtl_minus_ltr = 0; /* total number of RTL keysyms minus LTR ones */
565 for (code = xkb->min_key_code; code <= xkb->max_key_code; code++)
568 KeySym sym = XkbKeySymEntry (xkb, code, level, group);
569 PangoDirection dir = pango_unichar_direction (gdk_keyval_to_unicode (sym));
573 case PANGO_DIRECTION_RTL:
576 case PANGO_DIRECTION_LTR:
584 if (rtl_minus_ltr > 0)
585 return PANGO_DIRECTION_RTL;
587 return PANGO_DIRECTION_LTR;
590 static PangoDirection
591 get_direction_from_cache (GdkKeymapX11 *keymap_x11,
595 Atom group_atom = xkb->names->groups[group];
597 gboolean cache_hit = FALSE;
598 DirectionCacheEntry *cache = keymap_x11->group_direction_cache;
600 PangoDirection direction = PANGO_DIRECTION_NEUTRAL;
603 if (keymap_x11->have_direction)
605 /* lookup in cache */
606 for (i = 0; i < G_N_ELEMENTS (keymap_x11->group_direction_cache); i++)
608 if (cache[i].group_atom == group_atom)
611 cache[i].serial = keymap_x11->current_cache_serial++; /* freshen */
612 direction = cache[i].direction;
613 group_atom = cache[i].group_atom;
620 /* initialize cache */
621 for (i = 0; i < G_N_ELEMENTS (keymap_x11->group_direction_cache); i++)
623 cache[i].group_atom = 0;
624 cache[i].serial = keymap_x11->current_cache_serial;
626 keymap_x11->current_cache_serial++;
629 /* insert in cache */
634 direction = get_direction (xkb, group);
636 /* remove the oldest entry */
637 for (i = 0; i < G_N_ELEMENTS (keymap_x11->group_direction_cache); i++)
639 if (cache[i].serial < cache[oldest].serial)
643 cache[oldest].group_atom = group_atom;
644 cache[oldest].direction = direction;
645 cache[oldest].serial = keymap_x11->current_cache_serial++;
652 get_num_groups (GdkKeymap *keymap,
655 Display *display = KEYMAP_XDISPLAY (keymap);
656 XkbGetControls(display, XkbSlowKeysMask, xkb);
657 XkbGetUpdatedMap (display, XkbKeySymsMask | XkbKeyTypesMask |
658 XkbModifierMapMask | XkbVirtualModsMask, xkb);
659 return xkb->ctrls->num_groups;
663 update_direction (GdkKeymapX11 *keymap_x11,
666 XkbDescPtr xkb = get_xkb (keymap_x11);
668 gboolean had_direction;
669 PangoDirection old_direction;
671 had_direction = keymap_x11->have_direction;
672 old_direction = keymap_x11->current_direction;
674 group_atom = xkb->names->groups[group];
676 /* a group change? */
677 if (!keymap_x11->have_direction || keymap_x11->current_group_atom != group_atom)
679 keymap_x11->current_direction = get_direction_from_cache (keymap_x11, xkb, group);
680 keymap_x11->current_group_atom = group_atom;
681 keymap_x11->have_direction = TRUE;
684 return !had_direction || old_direction != keymap_x11->current_direction;
688 update_lock_state (GdkKeymapX11 *keymap_x11,
691 gboolean caps_lock_state;
692 gboolean num_lock_state;
694 caps_lock_state = keymap_x11->caps_lock_state;
695 num_lock_state = keymap_x11->num_lock_state;
697 keymap_x11->caps_lock_state = (locked_mods & GDK_LOCK_MASK) != 0;
698 keymap_x11->num_lock_state = (locked_mods & keymap_x11->num_lock_mask) != 0;
700 return (caps_lock_state != keymap_x11->caps_lock_state)
701 || (num_lock_state != keymap_x11->num_lock_state);
704 /* keep this in sync with the XkbSelectEventDetails() call
705 * in gdk_display_open()
708 _gdk_keymap_state_changed (GdkDisplay *display,
711 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
712 XkbEvent *xkb_event = (XkbEvent *)xevent;
714 if (display_x11->keymap)
716 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (display_x11->keymap);
718 if (update_direction (keymap_x11, XkbStateGroup (&xkb_event->state)))
719 g_signal_emit_by_name (keymap_x11, "direction-changed");
721 if (update_lock_state (keymap_x11, xkb_event->state.locked_mods))
722 g_signal_emit_by_name (keymap_x11, "state-changed");
726 #endif /* HAVE_XKB */
729 _gdk_keymap_keys_changed (GdkDisplay *display)
731 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
733 ++display_x11->keymap_serial;
735 if (display_x11->keymap)
736 g_signal_emit_by_name (display_x11->keymap, "keys_changed", 0);
740 * gdk_keymap_get_direction:
741 * @keymap: a #GdkKeymap or %NULL to use the default keymap
743 * Returns the direction of effective layout of the keymap.
745 * Returns: %PANGO_DIRECTION_LTR or %PANGO_DIRECTION_RTL
746 * if it can determine the direction. %PANGO_DIRECTION_NEUTRAL
750 gdk_keymap_get_direction (GdkKeymap *keymap)
752 keymap = GET_EFFECTIVE_KEYMAP (keymap);
755 if (KEYMAP_USE_XKB (keymap))
757 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (keymap);
759 if (!keymap_x11->have_direction)
761 GdkDisplay *display = GDK_KEYMAP (keymap_x11)->display;
762 XkbStateRec state_rec;
764 XkbGetState (GDK_DISPLAY_XDISPLAY (display), XkbUseCoreKbd,
766 update_direction (keymap_x11, XkbStateGroup (&state_rec));
769 return keymap_x11->current_direction;
772 #endif /* HAVE_XKB */
773 return PANGO_DIRECTION_NEUTRAL;
777 * gdk_keymap_have_bidi_layouts:
778 * @keymap: a #GdkKeymap or %NULL to use the default keymap
780 * Determines if keyboard layouts for both right-to-left and left-to-right
781 * languages are in use.
783 * Returns: %TRUE if there are layouts in both directions, %FALSE otherwise
788 gdk_keymap_have_bidi_layouts (GdkKeymap *keymap)
790 keymap = GET_EFFECTIVE_KEYMAP (keymap);
793 if (KEYMAP_USE_XKB (keymap))
795 GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (keymap);
796 XkbDescPtr xkb = get_xkb (keymap_x11);
797 int num_groups = get_num_groups (keymap, xkb);
800 gboolean have_ltr_keyboard = FALSE;
801 gboolean have_rtl_keyboard = FALSE;
803 for (i = 0; i < num_groups; i++)
805 if (get_direction_from_cache (keymap_x11, xkb, i) == PANGO_DIRECTION_RTL)
806 have_rtl_keyboard = TRUE;
808 have_ltr_keyboard = TRUE;
811 return have_ltr_keyboard && have_rtl_keyboard;
814 #endif /* HAVE_XKB */
819 * gdk_keymap_get_caps_lock_state:
820 * @keymap: a #GdkKeymap
822 * Returns whether the Caps Lock modifer is locked.
824 * Returns: %TRUE if Caps Lock is on
829 gdk_keymap_get_caps_lock_state (GdkKeymap *keymap)
831 GdkKeymapX11 *keymap_x11;
833 keymap = GET_EFFECTIVE_KEYMAP (keymap);
835 keymap_x11 = GDK_KEYMAP_X11 (keymap);
837 return keymap_x11->caps_lock_state;
841 * gdk_keymap_get_num_lock_state:
842 * @keymap: a #GdkKeymap
844 * Returns whether the Num Lock modifer is locked.
846 * Returns: %TRUE if Num Lock is on
851 gdk_keymap_get_num_lock_state (GdkKeymap *keymap)
853 GdkKeymapX11 *keymap_x11;
855 keymap = GET_EFFECTIVE_KEYMAP (keymap);
857 keymap_x11 = GDK_KEYMAP_X11 (keymap);
859 return keymap_x11->num_lock_state;
863 * gdk_keymap_get_entries_for_keyval:
864 * @keymap: (allow-none): a #GdkKeymap, or %NULL to use the default keymap
865 * @keyval: a keyval, such as %GDK_a, %GDK_Up, %GDK_Return, etc.
866 * @keys: (out): return location for an array of #GdkKeymapKey
867 * @n_keys: (out): return location for number of elements in returned array
869 * Obtains a list of keycode/group/level combinations that will
870 * generate @keyval. Groups and levels are two kinds of keyboard mode;
871 * in general, the level determines whether the top or bottom symbol
872 * on a key is used, and the group determines whether the left or
873 * right symbol is used. On US keyboards, the shift key changes the
874 * keyboard level, and there are no groups. A group switch key might
875 * convert a keyboard between Hebrew to English modes, for example.
876 * #GdkEventKey contains a %group field that indicates the active
877 * keyboard group. The level is computed from the modifier mask.
878 * The returned array should be freed
881 * Return value: %TRUE if keys were found and returned
884 gdk_keymap_get_entries_for_keyval (GdkKeymap *keymap,
890 GdkKeymapX11 *keymap_x11;
892 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
893 g_return_val_if_fail (keys != NULL, FALSE);
894 g_return_val_if_fail (n_keys != NULL, FALSE);
895 g_return_val_if_fail (keyval != 0, FALSE);
897 keymap = GET_EFFECTIVE_KEYMAP (keymap);
898 keymap_x11 = GDK_KEYMAP_X11 (keymap);
900 retval = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
903 if (KEYMAP_USE_XKB (keymap))
905 /* See sec 15.3.4 in XKB docs */
907 XkbDescRec *xkb = get_xkb (keymap_x11);
910 keycode = keymap_x11->min_keycode;
912 while (keycode <= keymap_x11->max_keycode)
914 gint max_shift_levels = XkbKeyGroupsWidth (xkb, keycode); /* "key width" */
917 gint total_syms = XkbKeyNumSyms (xkb, keycode);
921 /* entry is an array with all syms for group 0, all
922 * syms for group 1, etc. and for each group the
923 * shift level syms are in order
925 entry = XkbKeySymsPtr (xkb, keycode);
927 while (i < total_syms)
929 /* check out our cool loop invariant */
930 g_assert (i == (group * max_shift_levels + level));
932 if (entry[i] == keyval)
937 key.keycode = keycode;
941 g_array_append_val (retval, key);
943 g_assert (XkbKeySymEntry (xkb, keycode, level, group) ==
949 if (level == max_shift_levels)
964 const KeySym *map = get_keymap (keymap_x11);
967 keycode = keymap_x11->min_keycode;
968 while (keycode <= keymap_x11->max_keycode)
970 const KeySym *syms = map + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
973 while (i < keymap_x11->keysyms_per_keycode)
975 if (syms[i] == keyval)
980 key.keycode = keycode;
982 /* The "classic" non-XKB keymap has 2 levels per group */
986 g_array_append_val (retval, key);
998 *keys = (GdkKeymapKey*) retval->data;
999 *n_keys = retval->len;
1007 g_array_free (retval, retval->len > 0 ? FALSE : TRUE);
1013 * gdk_keymap_get_entries_for_keycode:
1014 * @keymap: (allow-none): a #GdkKeymap or %NULL to use the default keymap
1015 * @hardware_keycode: a keycode
1016 * @keys: (out): return location for array of #GdkKeymapKey, or %NULL
1017 * @keyvals: (out): return location for array of keyvals, or %NULL
1018 * @n_entries: length of @keys and @keyvals
1020 * Returns the keyvals bound to @hardware_keycode.
1021 * The Nth #GdkKeymapKey in @keys is bound to the Nth
1022 * keyval in @keyvals. Free the returned arrays with g_free().
1023 * When a keycode is pressed by the user, the keyval from
1024 * this list of entries is selected by considering the effective
1025 * keyboard group and level. See gdk_keymap_translate_keyboard_state().
1027 * Returns: %TRUE if there were any entries
1030 gdk_keymap_get_entries_for_keycode (GdkKeymap *keymap,
1031 guint hardware_keycode,
1032 GdkKeymapKey **keys,
1036 GdkKeymapX11 *keymap_x11;
1039 GArray *keyval_array;
1041 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
1042 g_return_val_if_fail (n_entries != NULL, FALSE);
1044 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1045 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1047 update_keyrange (keymap_x11);
1049 if (hardware_keycode < keymap_x11->min_keycode ||
1050 hardware_keycode > keymap_x11->max_keycode)
1062 key_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
1067 keyval_array = g_array_new (FALSE, FALSE, sizeof (guint));
1069 keyval_array = NULL;
1072 if (KEYMAP_USE_XKB (keymap))
1074 /* See sec 15.3.4 in XKB docs */
1076 XkbDescRec *xkb = get_xkb (keymap_x11);
1077 gint max_shift_levels;
1084 max_shift_levels = XkbKeyGroupsWidth (xkb, hardware_keycode); /* "key width" */
1085 total_syms = XkbKeyNumSyms (xkb, hardware_keycode);
1087 /* entry is an array with all syms for group 0, all
1088 * syms for group 1, etc. and for each group the
1089 * shift level syms are in order
1091 entry = XkbKeySymsPtr (xkb, hardware_keycode);
1093 while (i < total_syms)
1095 /* check out our cool loop invariant */
1096 g_assert (i == (group * max_shift_levels + level));
1102 key.keycode = hardware_keycode;
1106 g_array_append_val (key_array, key);
1110 g_array_append_val (keyval_array, entry[i]);
1114 if (level == max_shift_levels)
1126 const KeySym *map = get_keymap (keymap_x11);
1130 syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1132 while (i < keymap_x11->keysyms_per_keycode)
1138 key.keycode = hardware_keycode;
1140 /* The "classic" non-XKB keymap has 2 levels per group */
1144 g_array_append_val (key_array, key);
1148 g_array_append_val (keyval_array, syms[i]);
1158 *n_entries = key_array->len;
1159 *keys = (GdkKeymapKey*) g_array_free (key_array, FALSE);
1164 *n_entries = keyval_array->len;
1165 *keyvals = (guint*) g_array_free (keyval_array, FALSE);
1168 return *n_entries > 0;
1173 * gdk_keymap_lookup_key:
1174 * @keymap: a #GdkKeymap or %NULL to use the default keymap
1175 * @key: a #GdkKeymapKey with keycode, group, and level initialized
1177 * Looks up the keyval mapped to a keycode/group/level triplet.
1178 * If no keyval is bound to @key, returns 0. For normal user input,
1179 * you want to use gdk_keymap_translate_keyboard_state() instead of
1180 * this function, since the effective group/level may not be
1181 * the same as the current keyboard state.
1183 * Return value: a keyval, or 0 if none was mapped to the given @key
1186 gdk_keymap_lookup_key (GdkKeymap *keymap,
1187 const GdkKeymapKey *key)
1189 GdkKeymapX11 *keymap_x11;
1191 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), 0);
1192 g_return_val_if_fail (key != NULL, 0);
1193 g_return_val_if_fail (key->group < 4, 0);
1195 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1196 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1199 if (KEYMAP_USE_XKB (keymap))
1201 XkbDescRec *xkb = get_xkb (keymap_x11);
1203 return XkbKeySymEntry (xkb, key->keycode, key->level, key->group);
1208 const KeySym *map = get_keymap (keymap_x11);
1209 const KeySym *syms = map + (key->keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1210 return get_symbol (syms, keymap_x11, key->group, key->level);
1215 /* This is copied straight from XFree86 Xlib, to:
1216 * - add the group and level return.
1217 * - change the interpretation of mods_rtrn as described
1218 * in the docs for gdk_keymap_translate_keyboard_state()
1219 * It's unchanged for ease of diff against the Xlib sources; don't
1223 MyEnhancedXkbTranslateKeyCode(register XkbDescPtr xkb,
1225 register unsigned int mods,
1226 unsigned int * mods_rtrn,
1227 KeySym * keysym_rtrn,
1231 XkbKeyTypeRec *type;
1233 unsigned preserve,effectiveGroup;
1236 if (mods_rtrn!=NULL)
1239 nKeyGroups= XkbKeyNumGroups(xkb,key);
1240 if ((!XkbKeycodeInRange(xkb,key))||(nKeyGroups==0)) {
1241 if (keysym_rtrn!=NULL)
1242 *keysym_rtrn = NoSymbol;
1246 syms = XkbKeySymsPtr(xkb,key);
1248 /* find the offset of the effective group */
1250 effectiveGroup= XkbGroupForCoreState(mods);
1251 if ( effectiveGroup>=nKeyGroups ) {
1252 unsigned groupInfo= XkbKeyGroupInfo(xkb,key);
1253 switch (XkbOutOfRangeGroupAction(groupInfo)) {
1255 effectiveGroup %= nKeyGroups;
1257 case XkbClampIntoRange:
1258 effectiveGroup = nKeyGroups-1;
1260 case XkbRedirectIntoRange:
1261 effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
1262 if (effectiveGroup>=nKeyGroups)
1267 col= effectiveGroup*XkbKeyGroupsWidth(xkb,key);
1268 type = XkbKeyKeyType(xkb,key,effectiveGroup);
1271 if (type->map) { /* find the column (shift level) within the group */
1273 register XkbKTMapEntryPtr entry;
1274 /* ---- Begin section modified for GDK ---- */
1277 for (i=0,entry=type->map;i<type->map_count;i++,entry++) {
1280 unsigned long tmp = entry->mods.mask;
1286 /* We always add one-modifiers levels to mods_rtrn since
1287 * they can't wipe out bits in the state unless the
1288 * level would be triggered. But return other modifiers
1291 if (bits == 1 || (mods&type->mods.mask)==entry->mods.mask)
1292 *mods_rtrn |= entry->mods.mask;
1295 if (!found&&entry->active&&((mods&type->mods.mask)==entry->mods.mask)) {
1298 preserve= type->preserve[i].mask;
1301 *level_rtrn = entry->level;
1306 /* ---- End section modified for GDK ---- */
1309 if (keysym_rtrn!=NULL)
1310 *keysym_rtrn= syms[col];
1312 /* ---- Begin section modified for GDK ---- */
1313 *mods_rtrn &= ~preserve;
1314 /* ---- End section modified for GDK ---- */
1316 /* ---- Begin stuff GDK comments out of the original Xlib version ---- */
1317 /* This is commented out because xkb_info is a private struct */
1320 /* The Motif VTS doesn't get the help callback called if help
1321 * is bound to Shift+<whatever>, and it appears as though it
1322 * is XkbTranslateKeyCode that is causing the problem. The
1323 * core X version of XTranslateKey always OR's in ShiftMask
1324 * and LockMask for mods_rtrn, so this "fix" keeps this behavior
1325 * and solves the VTS problem.
1327 if ((xkb->dpy)&&(xkb->dpy->xkb_info)&&
1328 (xkb->dpy->xkb_info->xlib_ctrls&XkbLC_AlwaysConsumeShiftAndLock)) { *mods_rtrn|= (ShiftMask|LockMask);
1332 /* ---- End stuff GDK comments out of the original Xlib version ---- */
1335 /* ---- Begin stuff GDK adds to the original Xlib version ---- */
1338 *group_rtrn = effectiveGroup;
1340 /* ---- End stuff GDK adds to the original Xlib version ---- */
1342 return (syms[col] != NoSymbol);
1344 #endif /* HAVE_XKB */
1346 /* Translates from keycode/state to keysymbol using the traditional interpretation
1347 * of the keyboard map. See section 12.7 of the Xlib reference manual
1350 translate_keysym (GdkKeymapX11 *keymap_x11,
1351 guint hardware_keycode,
1353 GdkModifierType state,
1354 gint *effective_group,
1355 gint *effective_level)
1357 const KeySym *map = get_keymap (keymap_x11);
1358 const KeySym *syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1360 #define SYM(k,g,l) get_symbol (syms, k,g,l)
1362 GdkModifierType shift_modifiers;
1365 gint num_lock_index;
1367 shift_modifiers = GDK_SHIFT_MASK;
1368 if (keymap_x11->lock_keysym == GDK_KEY_Shift_Lock)
1369 shift_modifiers |= GDK_LOCK_MASK;
1371 /* Fall back to the first group if the passed in group is empty
1373 if (!(SYM (keymap_x11, group, 0) || SYM (keymap_x11, group, 1)) &&
1374 (SYM (keymap_x11, 0, 0) || SYM (keymap_x11, 0, 1)))
1377 /* Hack: On Sun, the Num Lock modifier uses the third element in the
1378 * keysym array, and Mode_Switch does not apply for a keypad key.
1380 if (keymap_x11->sun_keypad)
1388 for (i = 0; i < keymap_x11->keysyms_per_keycode; i++)
1389 if (KEYSYM_IS_KEYPAD (SYM (keymap_x11, 0, i)))
1396 if ((state & keymap_x11->num_lock_mask) &&
1397 KEYSYM_IS_KEYPAD (SYM (keymap_x11, group, num_lock_index)))
1399 /* Shift, Shift_Lock cancel Num_Lock
1401 shift_level = (state & shift_modifiers) ? 0 : num_lock_index;
1402 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1405 tmp_keyval = SYM (keymap_x11, group, shift_level);
1409 /* Fall back to the first level if no symbol for the level
1412 shift_level = (state & shift_modifiers) ? 1 : 0;
1413 if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1416 tmp_keyval = SYM (keymap_x11, group, shift_level);
1418 if (keymap_x11->lock_keysym == GDK_KEY_Caps_Lock && (state & GDK_LOCK_MASK) != 0)
1420 guint upper = gdk_keyval_to_upper (tmp_keyval);
1421 if (upper != tmp_keyval)
1426 if (effective_group)
1427 *effective_group = group;
1429 if (effective_level)
1430 *effective_level = shift_level;
1438 * gdk_keymap_translate_keyboard_state:
1439 * @keymap: (allow-none): a #GdkKeymap, or %NULL to use the default
1440 * @hardware_keycode: a keycode
1441 * @state: a modifier state
1442 * @group: active keyboard group
1443 * @keyval: (out) (allow-none): return location for keyval, or %NULL
1444 * @effective_group: (out) (allow-none): return location for effective group, or %NULL
1445 * @level: (out) (allow-none): return location for level, or %NULL
1446 * @consumed_modifiers: (out) (allow-none): return location for modifiers that were used to
1447 * determine the group or level, or %NULL
1449 * Translates the contents of a #GdkEventKey into a keyval, effective
1450 * group, and level. Modifiers that affected the translation and
1451 * are thus unavailable for application use are returned in
1452 * @consumed_modifiers. See <xref linkend="key-group-explanation"/> for an explanation of
1453 * groups and levels. The @effective_group is the group that was
1454 * actually used for the translation; some keys such as Enter are not
1455 * affected by the active keyboard group. The @level is derived from
1456 * @state. For convenience, #GdkEventKey already contains the translated
1457 * keyval, so this function isn't as useful as you might think.
1460 * @consumed_modifiers gives modifiers that should be masked out
1461 * from @state when comparing this key press to a hot key. For
1462 * instance, on a US keyboard, the <literal>plus</literal>
1463 * symbol is shifted, so when comparing a key press to a
1464 * <literal><Control>plus</literal> accelerator <Shift> should
1467 * <informalexample><programlisting>
1468 * /* We want to ignore irrelevant modifiers like ScrollLock */
1469 * #define ALL_ACCELS_MASK (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK)
1470 * gdk_keymap_translate_keyboard_state (keymap, event->hardware_keycode,
1471 * event->state, event->group,
1472 * &keyval, NULL, NULL, &consumed);
1473 * if (keyval == GDK_PLUS &&
1474 * (event->state & ~consumed & ALL_ACCELS_MASK) == GDK_CONTROL_MASK)
1475 * /* Control was pressed */
1476 * </programlisting></informalexample>
1478 * An older interpretation @consumed_modifiers was that it contained
1479 * all modifiers that might affect the translation of the key;
1480 * this allowed accelerators to be stored with irrelevant consumed
1481 * modifiers, by doing:</para>
1482 * <informalexample><programlisting>
1483 * /* XXX Don't do this XXX */
1484 * if (keyval == accel_keyval &&
1485 * (event->state & ~consumed & ALL_ACCELS_MASK) == (accel_mods & ~consumed))
1486 * /* Accelerator was pressed */
1487 * </programlisting></informalexample>
1489 * However, this did not work if multi-modifier combinations were
1490 * used in the keymap, since, for instance, <literal><Control></literal>
1491 * would be masked out even if only <literal><Control><Alt></literal>
1492 * was used in the keymap. To support this usage as well as well as
1493 * possible, all <emphasis>single modifier</emphasis> combinations
1494 * that could affect the key for any combination of modifiers will
1495 * be returned in @consumed_modifiers; multi-modifier combinations
1496 * are returned only when actually found in @state. When you store
1497 * accelerators, you should always store them with consumed modifiers
1498 * removed. Store <literal><Control>plus</literal>,
1499 * not <literal><Control><Shift>plus</literal>,
1502 * Return value: %TRUE if there was a keyval bound to the keycode/state/group
1505 gdk_keymap_translate_keyboard_state (GdkKeymap *keymap,
1506 guint hardware_keycode,
1507 GdkModifierType state,
1510 gint *effective_group,
1512 GdkModifierType *consumed_modifiers)
1514 GdkKeymapX11 *keymap_x11;
1515 KeySym tmp_keyval = NoSymbol;
1516 guint tmp_modifiers;
1518 g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
1519 g_return_val_if_fail (group < 4, FALSE);
1521 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1522 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1526 if (effective_group)
1527 *effective_group = 0;
1530 if (consumed_modifiers)
1531 *consumed_modifiers = 0;
1533 update_keyrange (keymap_x11);
1535 if (hardware_keycode < keymap_x11->min_keycode ||
1536 hardware_keycode > keymap_x11->max_keycode)
1540 if (KEYMAP_USE_XKB (keymap))
1542 XkbDescRec *xkb = get_xkb (keymap_x11);
1544 /* replace bits 13 and 14 with the provided group */
1545 state &= ~(1 << 13 | 1 << 14);
1546 state |= group << 13;
1548 MyEnhancedXkbTranslateKeyCode (xkb,
1556 if (state & ~tmp_modifiers & LockMask)
1557 tmp_keyval = gdk_keyval_to_upper (tmp_keyval);
1559 /* We need to augment the consumed modifiers with LockMask, since
1560 * we handle that ourselves, and also with the group bits
1562 tmp_modifiers |= LockMask | 1 << 13 | 1 << 14;
1567 GdkModifierType bit;
1571 /* We see what modifiers matter by trying the translation with
1572 * and without each possible modifier
1574 for (bit = GDK_SHIFT_MASK; bit < GDK_BUTTON1_MASK; bit <<= 1)
1576 /* Handling of the group here is a bit funky; a traditional
1577 * X keyboard map can have more than two groups, but no way
1578 * of accessing the extra groups is defined. We allow a
1579 * caller to pass in any group to this function, but we
1580 * only can represent switching between group 0 and 1 in
1581 * consumed modifiers.
1583 if (translate_keysym (keymap_x11, hardware_keycode,
1584 (bit == keymap_x11->group_switch_mask) ? 0 : group,
1587 translate_keysym (keymap_x11, hardware_keycode,
1588 (bit == keymap_x11->group_switch_mask) ? 1 : group,
1591 tmp_modifiers |= bit;
1594 tmp_keyval = translate_keysym (keymap_x11, hardware_keycode,
1596 level, effective_group);
1599 if (consumed_modifiers)
1600 *consumed_modifiers = tmp_modifiers;
1603 *keyval = tmp_keyval;
1605 return tmp_keyval != NoSymbol;
1609 /* Key handling not part of the keymap */
1612 * @keyval: a key value.
1614 * Converts a key value into a symbolic name.
1615 * The names are the same as those in the
1616 * <filename><gdk/gdkkeysyms.h></filename> header file
1617 * but without the leading "GDK_KEY_".
1619 * Return value: (transfer none): a string containing the name of the key, or
1620 * %NULL if @keyval is not a valid key. The string should not be modified.
1623 gdk_keyval_name (guint keyval)
1627 case GDK_KEY_Page_Up:
1629 case GDK_KEY_Page_Down:
1631 case GDK_KEY_KP_Page_Up:
1632 return "KP_Page_Up";
1633 case GDK_KEY_KP_Page_Down:
1634 return "KP_Page_Down";
1637 return XKeysymToString (keyval);
1641 gdk_keyval_from_name (const gchar *keyval_name)
1643 g_return_val_if_fail (keyval_name != NULL, 0);
1645 return XStringToKeysym (keyval_name);
1648 #ifdef HAVE_XCONVERTCASE
1650 gdk_keyval_convert_case (guint symbol,
1657 /* Check for directly encoded 24-bit UCS characters: */
1658 if ((symbol & 0xff000000) == 0x01000000)
1661 *lower = gdk_unicode_to_keyval (g_unichar_tolower (symbol & 0x00ffffff));
1663 *upper = gdk_unicode_to_keyval (g_unichar_toupper (symbol & 0x00ffffff));
1668 XConvertCase (symbol, &xlower, &xupper);
1675 #endif /* HAVE_XCONVERTCASE */
1678 _gdk_x11_get_group_for_state (GdkDisplay *display,
1679 GdkModifierType state)
1681 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1684 if (display_x11->use_xkb)
1686 return XkbGroupForCoreState (state);
1691 GdkKeymapX11 *keymap_impl = GDK_KEYMAP_X11 (gdk_keymap_get_for_display (display));
1692 update_keymaps (keymap_impl);
1693 return (state & keymap_impl->group_switch_mask) ? 1 : 0;
1698 _gdk_keymap_add_virtual_modifiers_compat (GdkKeymap *keymap,
1699 GdkModifierType *modifiers)
1701 GdkKeymapX11 *keymap_x11;
1704 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1705 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1707 for (i = 3; i < 8; i++)
1709 if ((1 << i) & *modifiers)
1711 if (keymap_x11->modmap[i] & GDK_MOD1_MASK)
1712 *modifiers |= GDK_MOD1_MASK;
1713 else if (keymap_x11->modmap[i] & GDK_SUPER_MASK)
1714 *modifiers |= GDK_SUPER_MASK;
1715 else if (keymap_x11->modmap[i] & GDK_HYPER_MASK)
1716 *modifiers |= GDK_HYPER_MASK;
1717 else if (keymap_x11->modmap[i] & GDK_META_MASK)
1718 *modifiers |= GDK_META_MASK;
1724 * gdk_keymap_add_virtual_modifiers:
1725 * @keymap: a #GdkKeymap
1726 * @state: pointer to the modifier mask to change
1728 * Adds virtual modifiers (i.e. Super, Hyper and Meta) which correspond
1729 * to the real modifiers (i.e Mod2, Mod3, ...) in @modifiers.
1730 * are set in @state to their non-virtual counterparts (i.e. Mod2,
1731 * Mod3,...) and set the corresponding bits in @state.
1733 * GDK already does this before delivering key events, but for
1734 * compatibility reasons, it only sets the first virtual modifier
1735 * it finds, whereas this function sets all matching virtual modifiers.
1737 * This function is useful when matching key events against
1743 gdk_keymap_add_virtual_modifiers (GdkKeymap *keymap,
1744 GdkModifierType *state)
1746 GdkKeymapX11 *keymap_x11;
1749 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1750 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1752 for (i = 3; i < 8; i++)
1754 if ((1 << i) & *state)
1756 if (keymap_x11->modmap[i] & GDK_MOD1_MASK)
1757 *state |= GDK_MOD1_MASK;
1758 if (keymap_x11->modmap[i] & GDK_SUPER_MASK)
1759 *state |= GDK_SUPER_MASK;
1760 if (keymap_x11->modmap[i] & GDK_HYPER_MASK)
1761 *state |= GDK_HYPER_MASK;
1762 if (keymap_x11->modmap[i] & GDK_META_MASK)
1763 *state |= GDK_META_MASK;
1769 _gdk_keymap_key_is_modifier (GdkKeymap *keymap,
1772 GdkKeymapX11 *keymap_x11;
1775 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1776 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1778 if (keycode < keymap_x11->min_keycode ||
1779 keycode > keymap_x11->max_keycode)
1783 if (KEYMAP_USE_XKB (keymap))
1785 XkbDescRec *xkb = get_xkb (keymap_x11);
1787 if (xkb->map->modmap && xkb->map->modmap[keycode] != 0)
1793 for (i = 0; i < 8 * keymap_x11->mod_keymap->max_keypermod; i++)
1795 if (keycode == keymap_x11->mod_keymap->modifiermap[i])
1804 * gdk_keymap_map_virtual_modifiers:
1805 * @keymap: a #GdkKeymap
1806 * @state: pointer to the modifier state to map
1808 * Maps the virtual modifiers (i.e. Super, Hyper and Meta) which
1809 * are set in @state to their non-virtual counterparts (i.e. Mod2,
1810 * Mod3,...) and set the corresponding bits in @state.
1812 * This function is useful when matching key events against
1815 * Returns: %TRUE if no virtual modifiers were mapped to the
1816 * same non-virtual modifier. Note that %FALSE is also returned
1817 * if a virtual modifier is mapped to a non-virtual modifier that
1818 * was already set in @state.
1823 gdk_keymap_map_virtual_modifiers (GdkKeymap *keymap,
1824 GdkModifierType *state)
1826 GdkKeymapX11 *keymap_x11;
1827 const guint vmods[] = {
1828 GDK_SUPER_MASK, GDK_HYPER_MASK, GDK_META_MASK
1833 keymap = GET_EFFECTIVE_KEYMAP (keymap);
1834 keymap_x11 = GDK_KEYMAP_X11 (keymap);
1836 if (KEYMAP_USE_XKB (keymap))
1837 get_xkb (keymap_x11);
1841 for (j = 0; j < 3; j++)
1843 if (*state & vmods[j])
1845 for (i = 3; i < 8; i++)
1847 if (keymap_x11->modmap[i] & vmods[j])
1849 if (*state & (1 << i))