]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkkeys-x11.c
c39092a7ea67271010b5ac4bc5fb3201dee7aa03
[~andy/gtk] / gdk / x11 / gdkkeys-x11.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2000 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/.
25  */
26
27 #include "config.h"
28
29 #include "gdkx11keys.h"
30 #include "gdkkeysprivate.h"
31 #include "gdkkeysyms.h"
32 #include "gdkprivate-x11.h"
33 #include "gdkdisplay-x11.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <limits.h>
40 #include <errno.h>
41
42 #ifdef HAVE_XKB
43 #include <X11/XKBlib.h>
44
45 /* OSF-4.0 is apparently missing this macro
46  */
47 #  ifndef XkbKeySymEntry
48 #    define XkbKeySymEntry(d,k,sl,g) \
49         (XkbKeySym(d,k,((XkbKeyGroupsWidth(d,k)*(g))+(sl))))
50 #  endif
51 #endif /* HAVE_XKB */
52
53 typedef struct _DirectionCacheEntry DirectionCacheEntry;
54
55 struct _DirectionCacheEntry
56 {
57   guint serial;
58   Atom group_atom;
59   PangoDirection direction;
60 };
61
62 struct _GdkX11Keymap
63 {
64   GdkKeymap     parent_instance;
65
66   gint min_keycode;
67   gint max_keycode;
68   KeySym* keymap;
69   gint keysyms_per_keycode;
70   XModifierKeymap* mod_keymap;
71   guint lock_keysym;
72   GdkModifierType group_switch_mask;
73   GdkModifierType num_lock_mask;
74   GdkModifierType modmap[8];
75   PangoDirection current_direction;
76   guint sun_keypad      : 1;
77   guint have_direction  : 1;
78   guint have_lock_state : 1;
79   guint caps_lock_state : 1;
80   guint num_lock_state  : 1;
81   guint modifier_state;
82   guint current_serial;
83
84 #ifdef HAVE_XKB
85   XkbDescPtr xkb_desc;
86   /* We cache the directions */
87   Atom current_group_atom;
88   guint current_cache_serial;
89   /* A cache of size four should be more than enough, people usually
90    * have two groups around, and the xkb limit is four.  It still
91    * works correct for more than four groups.  It's just the
92    * cache.
93    */
94   DirectionCacheEntry group_direction_cache[4];
95 #endif
96 };
97
98 struct _GdkX11KeymapClass
99 {
100   GdkKeymapClass parent_class;
101 };
102
103 #define KEYMAP_USE_XKB(keymap) GDK_X11_DISPLAY ((keymap)->display)->use_xkb
104 #define KEYMAP_XDISPLAY(keymap) GDK_DISPLAY_XDISPLAY ((keymap)->display)
105
106 G_DEFINE_TYPE (GdkX11Keymap, gdk_x11_keymap, GDK_TYPE_KEYMAP)
107
108 static void
109 gdk_x11_keymap_init (GdkX11Keymap *keymap)
110 {
111   keymap->min_keycode = 0;
112   keymap->max_keycode = 0;
113
114   keymap->keymap = NULL;
115   keymap->keysyms_per_keycode = 0;
116   keymap->mod_keymap = NULL;
117
118   keymap->num_lock_mask = 0;
119   keymap->sun_keypad = FALSE;
120   keymap->group_switch_mask = 0;
121   keymap->lock_keysym = GDK_KEY_Caps_Lock;
122   keymap->have_direction = FALSE;
123   keymap->have_lock_state = FALSE;
124   keymap->current_serial = 0;
125
126 #ifdef HAVE_XKB
127   keymap->xkb_desc = NULL;
128   keymap->current_group_atom = 0;
129   keymap->current_cache_serial = 0;
130 #endif
131
132 }
133
134 static void
135 gdk_x11_keymap_finalize (GObject *object)
136 {
137   GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (object);
138
139   if (keymap_x11->keymap)
140     XFree (keymap_x11->keymap);
141
142   if (keymap_x11->mod_keymap)
143     XFreeModifiermap (keymap_x11->mod_keymap);
144
145 #ifdef HAVE_XKB
146   if (keymap_x11->xkb_desc)
147     XkbFreeKeyboard (keymap_x11->xkb_desc, XkbAllComponentsMask, True);
148 #endif
149
150   G_OBJECT_CLASS (gdk_x11_keymap_parent_class)->finalize (object);
151 }
152
153 static inline void
154 update_keyrange (GdkX11Keymap *keymap_x11)
155 {
156   if (keymap_x11->max_keycode == 0)
157     XDisplayKeycodes (KEYMAP_XDISPLAY (GDK_KEYMAP (keymap_x11)),
158                       &keymap_x11->min_keycode, &keymap_x11->max_keycode);
159 }
160
161 #ifdef HAVE_XKB
162
163 static void
164 update_modmap (Display      *display,
165                GdkX11Keymap *keymap_x11)
166 {
167   static struct {
168     const gchar *name;
169     Atom atom;
170     GdkModifierType mask;
171   } vmods[] = {
172     { "Meta", 0, GDK_META_MASK },
173     { "Super", 0, GDK_SUPER_MASK },
174     { "Hyper", 0, GDK_HYPER_MASK },
175     { NULL, 0, 0 }
176   };
177
178   gint i, j, k;
179
180   if (!vmods[0].atom)
181     for (i = 0; vmods[i].name; i++)
182       vmods[i].atom = XInternAtom (display, vmods[i].name, FALSE);
183
184   for (i = 0; i < 8; i++)
185     keymap_x11->modmap[i] = 1 << i;
186
187   for (i = 0; i < XkbNumVirtualMods; i++)
188     {
189       for (j = 0; vmods[j].atom; j++)
190         {
191           if (keymap_x11->xkb_desc->names->vmods[i] == vmods[j].atom)
192             {
193               for (k = 0; k < 8; k++)
194                 {
195                   if (keymap_x11->xkb_desc->server->vmods[i] & (1 << k))
196                     keymap_x11->modmap[k] |= vmods[j].mask;
197                 }
198             }
199         }
200     }
201 }
202
203 static XkbDescPtr
204 get_xkb (GdkX11Keymap *keymap_x11)
205 {
206   GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_KEYMAP (keymap_x11)->display);
207   Display *xdisplay = display_x11->xdisplay;
208
209   update_keyrange (keymap_x11);
210
211   if (keymap_x11->xkb_desc == NULL)
212     {
213       keymap_x11->xkb_desc = XkbGetMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask | XkbModifierMapMask | XkbVirtualModsMask, XkbUseCoreKbd);
214       if (keymap_x11->xkb_desc == NULL)
215         {
216           g_error ("Failed to get keymap");
217           return NULL;
218         }
219
220       XkbGetNames (xdisplay, XkbGroupNamesMask | XkbVirtualModNamesMask, keymap_x11->xkb_desc);
221
222       update_modmap (xdisplay, keymap_x11);
223     }
224   else if (keymap_x11->current_serial != display_x11->keymap_serial)
225     {
226       XkbGetUpdatedMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask | XkbModifierMapMask | XkbVirtualModsMask,
227                         keymap_x11->xkb_desc);
228       XkbGetNames (xdisplay, XkbGroupNamesMask | XkbVirtualModNamesMask, keymap_x11->xkb_desc);
229
230       update_modmap (xdisplay, keymap_x11);
231     }
232
233   keymap_x11->current_serial = display_x11->keymap_serial;
234
235   if (keymap_x11->num_lock_mask == 0)
236     keymap_x11->num_lock_mask = XkbKeysymToModifiers (KEYMAP_XDISPLAY (GDK_KEYMAP (keymap_x11)), GDK_KEY_Num_Lock);
237
238   return keymap_x11->xkb_desc;
239 }
240 #endif /* HAVE_XKB */
241
242 /* Whether we were able to turn on detectable-autorepeat using
243  * XkbSetDetectableAutorepeat. If FALSE, we'll fall back
244  * to checking the next event with XPending().
245  */
246
247 /* Find the index of the group/level pair within the keysyms for a key.
248  * We round up the number of keysyms per keycode to the next even number,
249  * otherwise we lose a whole group of keys
250  */
251 #define KEYSYM_INDEX(keymap_impl, group, level) \
252   (2 * ((group) % (gint)((keymap_impl->keysyms_per_keycode + 1) / 2)) + (level))
253 #define KEYSYM_IS_KEYPAD(s) (((s) >= 0xff80 && (s) <= 0xffbd) || \
254                              ((s) >= 0x11000000 && (s) <= 0x1100ffff))
255
256 static gint
257 get_symbol (const KeySym *syms,
258             GdkX11Keymap *keymap_x11,
259             gint          group,
260             gint          level)
261 {
262   gint index;
263
264   index = KEYSYM_INDEX(keymap_x11, group, level);
265   if (index >= keymap_x11->keysyms_per_keycode)
266       return NoSymbol;
267
268   return syms[index];
269 }
270
271 static void
272 set_symbol (KeySym       *syms,
273             GdkX11Keymap *keymap_x11,
274             gint          group,
275             gint          level,
276             KeySym        sym)
277 {
278   gint index;
279
280   index = KEYSYM_INDEX(keymap_x11, group, level);
281   if (index >= keymap_x11->keysyms_per_keycode)
282       return;
283
284   syms[index] = sym;
285 }
286
287 static void
288 update_keymaps (GdkX11Keymap *keymap_x11)
289 {
290   GdkX11Display *display_x11 = GDK_X11_DISPLAY (GDK_KEYMAP (keymap_x11)->display);
291   Display *xdisplay = display_x11->xdisplay;
292
293 #ifdef HAVE_XKB
294   g_assert (!KEYMAP_USE_XKB (GDK_KEYMAP (keymap_x11)));
295 #endif
296
297   if (keymap_x11->keymap == NULL ||
298       keymap_x11->current_serial != display_x11->keymap_serial)
299     {
300       gint i;
301       gint map_size;
302       gint keycode;
303
304       keymap_x11->current_serial = display_x11->keymap_serial;
305
306       update_keyrange (keymap_x11);
307
308       if (keymap_x11->keymap)
309         XFree (keymap_x11->keymap);
310
311       if (keymap_x11->mod_keymap)
312         XFreeModifiermap (keymap_x11->mod_keymap);
313
314       keymap_x11->keymap = XGetKeyboardMapping (xdisplay, keymap_x11->min_keycode,
315                                                 keymap_x11->max_keycode - keymap_x11->min_keycode + 1,
316                                                 &keymap_x11->keysyms_per_keycode);
317
318
319       /* GDK_KEY_ISO_Left_Tab, as usually configured through XKB, really messes
320        * up the whole idea of "consumed modifiers" because shift is consumed.
321        * However, <shift>Tab is not usually GDK_KEY_ISO_Left_Tab without XKB,
322        * we we fudge the map here.
323        */
324       keycode = keymap_x11->min_keycode;
325       while (keycode <= keymap_x11->max_keycode)
326         {
327           KeySym *syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
328           /* Check both groups */
329           for (i = 0 ; i < 2 ; i++)
330             {
331               if (get_symbol (syms, keymap_x11, i, 0) == GDK_KEY_Tab)
332                 set_symbol (syms, keymap_x11, i, 1, GDK_KEY_ISO_Left_Tab);
333             }
334
335           /*
336            * If there is one keysym and the key symbol has upper and lower
337            * case variants fudge the keymap
338            */
339           if (get_symbol (syms, keymap_x11, 0, 1) == 0)
340             {
341               guint lower;
342               guint upper;
343
344               gdk_keyval_convert_case (get_symbol (syms, keymap_x11, 0, 0), &lower, &upper);
345               if (lower != upper)
346                 {
347                   set_symbol (syms, keymap_x11, 0, 0, lower);
348                   set_symbol (syms, keymap_x11, 0, 1, upper);
349                 }
350             }
351
352           ++keycode;
353         }
354
355       keymap_x11->mod_keymap = XGetModifierMapping (xdisplay);
356
357       keymap_x11->lock_keysym = GDK_KEY_VoidSymbol;
358       keymap_x11->group_switch_mask = 0;
359       keymap_x11->num_lock_mask = 0;
360
361       for (i = 0; i < 8; i++)
362         keymap_x11->modmap[i] = 1 << i;
363
364       /* There are 8 sets of modifiers, with each set containing
365        * max_keypermod keycodes.
366        */
367       map_size = 8 * keymap_x11->mod_keymap->max_keypermod;
368       for (i = 0; i < map_size; i++)
369         {
370           /* Get the key code at this point in the map. */
371           gint keycode = keymap_x11->mod_keymap->modifiermap[i];
372           gint j;
373           KeySym *syms;
374           guint mask;
375
376           /* Ignore invalid keycodes. */
377           if (keycode < keymap_x11->min_keycode ||
378               keycode > keymap_x11->max_keycode)
379             continue;
380
381           syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
382
383           mask = 0;
384           for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
385             {
386               if (syms[j] == GDK_KEY_Meta_L ||
387                   syms[j] == GDK_KEY_Meta_R)
388                 mask |= GDK_META_MASK;
389               else if (syms[j] == GDK_KEY_Hyper_L ||
390                        syms[j] == GDK_KEY_Hyper_R)
391                 mask |= GDK_HYPER_MASK;
392               else if (syms[j] == GDK_KEY_Super_L ||
393                        syms[j] == GDK_KEY_Super_R)
394                 mask |= GDK_SUPER_MASK;
395             }
396
397           keymap_x11->modmap[i/keymap_x11->mod_keymap->max_keypermod] |= mask;
398
399           /* The fourth modifier, GDK_MOD1_MASK is 1 << 3.
400            * Each group of max_keypermod entries refers to the same modifier.
401            */
402           mask = 1 << (i / keymap_x11->mod_keymap->max_keypermod);
403
404           switch (mask)
405             {
406             case GDK_LOCK_MASK:
407               /* Get the Lock keysym.  If any keysym bound to the Lock modifier
408                * is Caps_Lock, we will interpret the modifier as Caps_Lock;
409                * otherwise, if any is bound to Shift_Lock, we will interpret
410                * the modifier as Shift_Lock. Otherwise, the lock modifier
411                * has no effect.
412                */
413               for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
414                 {
415                   if (syms[j] == GDK_KEY_Caps_Lock)
416                     keymap_x11->lock_keysym = GDK_KEY_Caps_Lock;
417                   else if (syms[j] == GDK_KEY_Shift_Lock &&
418                            keymap_x11->lock_keysym == GDK_KEY_VoidSymbol)
419                     keymap_x11->lock_keysym = GDK_KEY_Shift_Lock;
420                 }
421               break;
422
423             case GDK_CONTROL_MASK:
424             case GDK_SHIFT_MASK:
425             case GDK_MOD1_MASK:
426               /* Some keyboard maps are known to map Mode_Switch as an
427                * extra Mod1 key. In circumstances like that, it won't be
428                * used to switch groups.
429                */
430               break;
431
432             default:
433               /* Find the Mode_Switch and Num_Lock modifiers. */
434               for (j = 0; j < keymap_x11->keysyms_per_keycode; j++)
435                 {
436                   if (syms[j] == GDK_KEY_Mode_switch)
437                     {
438                       /* This modifier swaps groups */
439                       keymap_x11->group_switch_mask |= mask;
440                     }
441                   else if (syms[j] == GDK_KEY_Num_Lock)
442                     {
443                       /* This modifier is used for Num_Lock */
444                       keymap_x11->num_lock_mask |= mask;
445                     }
446                 }
447               break;
448             }
449         }
450
451       /* Hack: The Sun X server puts the keysym to use when the Num Lock
452        * modifier is on in the third element of the keysym array, instead
453        * of the second.
454        */
455       if ((strcmp (ServerVendor (xdisplay), "Sun Microsystems, Inc.") == 0) &&
456           (keymap_x11->keysyms_per_keycode > 2))
457         keymap_x11->sun_keypad = TRUE;
458       else
459         keymap_x11->sun_keypad = FALSE;
460     }
461 }
462
463 static const KeySym*
464 get_keymap (GdkX11Keymap *keymap_x11)
465 {
466   update_keymaps (keymap_x11);
467
468   return keymap_x11->keymap;
469 }
470
471 #ifdef HAVE_XKB
472 static PangoDirection
473 get_direction (XkbDescRec *xkb,
474                gint        group)
475 {
476   gint code;
477
478   gint rtl_minus_ltr = 0; /* total number of RTL keysyms minus LTR ones */
479
480   for (code = xkb->min_key_code; code <= xkb->max_key_code; code++)
481     {
482       gint level = 0;
483       KeySym sym = XkbKeySymEntry (xkb, code, level, group);
484       PangoDirection dir = pango_unichar_direction (gdk_keyval_to_unicode (sym));
485
486       switch (dir)
487         {
488         case PANGO_DIRECTION_RTL:
489           rtl_minus_ltr++;
490           break;
491         case PANGO_DIRECTION_LTR:
492           rtl_minus_ltr--;
493           break;
494         default:
495           break;
496         }
497     }
498
499   if (rtl_minus_ltr > 0)
500     return PANGO_DIRECTION_RTL;
501   else
502     return PANGO_DIRECTION_LTR;
503 }
504
505 static PangoDirection
506 get_direction_from_cache (GdkX11Keymap *keymap_x11,
507                           XkbDescPtr    xkb,
508                           gint          group)
509 {
510   Atom group_atom = xkb->names->groups[group];
511
512   gboolean cache_hit = FALSE;
513   DirectionCacheEntry *cache = keymap_x11->group_direction_cache;
514
515   PangoDirection direction = PANGO_DIRECTION_NEUTRAL;
516   gint i;
517
518   if (keymap_x11->have_direction)
519     {
520       /* lookup in cache */
521       for (i = 0; i < G_N_ELEMENTS (keymap_x11->group_direction_cache); i++)
522       {
523         if (cache[i].group_atom == group_atom)
524           {
525             cache_hit = TRUE;
526             cache[i].serial = keymap_x11->current_cache_serial++; /* freshen */
527             direction = cache[i].direction;
528             group_atom = cache[i].group_atom;
529             break;
530           }
531       }
532     }
533   else
534     {
535       /* initialize cache */
536       for (i = 0; i < G_N_ELEMENTS (keymap_x11->group_direction_cache); i++)
537         {
538           cache[i].group_atom = 0;
539           cache[i].serial = keymap_x11->current_cache_serial;
540         }
541       keymap_x11->current_cache_serial++;
542     }
543
544   /* insert in cache */
545   if (!cache_hit)
546     {
547       gint oldest = 0;
548
549       direction = get_direction (xkb, group);
550
551       /* remove the oldest entry */
552       for (i = 0; i < G_N_ELEMENTS (keymap_x11->group_direction_cache); i++)
553         {
554           if (cache[i].serial < cache[oldest].serial)
555             oldest = i;
556         }
557
558       cache[oldest].group_atom = group_atom;
559       cache[oldest].direction = direction;
560       cache[oldest].serial = keymap_x11->current_cache_serial++;
561     }
562
563   return direction;
564 }
565
566 static int
567 get_num_groups (GdkKeymap *keymap,
568                 XkbDescPtr xkb)
569 {
570       Display *display = KEYMAP_XDISPLAY (keymap);
571       XkbGetControls(display, XkbSlowKeysMask, xkb);
572       XkbGetUpdatedMap (display, XkbKeySymsMask | XkbKeyTypesMask |
573                         XkbModifierMapMask | XkbVirtualModsMask, xkb);
574       return xkb->ctrls->num_groups;
575 }
576
577 static gboolean
578 update_direction (GdkX11Keymap *keymap_x11,
579                   gint          group)
580 {
581   XkbDescPtr xkb = get_xkb (keymap_x11);
582   Atom group_atom;
583   gboolean had_direction;
584   PangoDirection old_direction;
585
586   had_direction = keymap_x11->have_direction;
587   old_direction = keymap_x11->current_direction;
588
589   group_atom = xkb->names->groups[group];
590
591   /* a group change? */
592   if (!keymap_x11->have_direction || keymap_x11->current_group_atom != group_atom)
593     {
594       keymap_x11->current_direction = get_direction_from_cache (keymap_x11, xkb, group);
595       keymap_x11->current_group_atom = group_atom;
596       keymap_x11->have_direction = TRUE;
597     }
598
599   return !had_direction || old_direction != keymap_x11->current_direction;
600 }
601
602 static gboolean
603 update_lock_state (GdkX11Keymap *keymap_x11,
604                    gint          locked_mods,
605                    gint          effective_mods)
606 {
607   XkbDescPtr xkb G_GNUC_UNUSED;
608   gboolean have_lock_state;
609   gboolean caps_lock_state;
610   gboolean num_lock_state;
611   guint modifier_state;
612
613   /* ensure keymap_x11->num_lock_mask is initialized */
614   xkb = get_xkb (keymap_x11);
615
616   have_lock_state = keymap_x11->have_lock_state;
617   caps_lock_state = keymap_x11->caps_lock_state;
618   num_lock_state = keymap_x11->num_lock_state;
619   modifier_state = keymap_x11->modifier_state;
620
621   keymap_x11->have_lock_state = TRUE;
622   keymap_x11->caps_lock_state = (locked_mods & GDK_LOCK_MASK) != 0;
623   keymap_x11->num_lock_state = (locked_mods & keymap_x11->num_lock_mask) != 0;
624   /* FIXME: sanitize this */
625   keymap_x11->modifier_state = (guint)effective_mods;
626
627   return !have_lock_state
628          || (caps_lock_state != keymap_x11->caps_lock_state)
629          || (num_lock_state != keymap_x11->num_lock_state)
630          || (modifier_state != keymap_x11->modifier_state);
631 }
632
633 /* keep this in sync with the XkbSelectEventDetails()
634  * call in gdk_display_open()
635  */
636 void
637 _gdk_x11_keymap_state_changed (GdkDisplay *display,
638                                XEvent     *xevent)
639 {
640   GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
641   XkbEvent *xkb_event = (XkbEvent *)xevent;
642
643   if (display_x11->keymap)
644     {
645       GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (display_x11->keymap);
646
647       if (update_direction (keymap_x11, XkbStateGroup (&xkb_event->state)))
648         g_signal_emit_by_name (keymap_x11, "direction-changed");
649
650       if (update_lock_state (keymap_x11,
651                              xkb_event->state.locked_mods,
652                              xkb_event->state.mods))
653         g_signal_emit_by_name (keymap_x11, "state-changed");
654     }
655 }
656
657 #endif /* HAVE_XKB */
658
659 static void
660 ensure_lock_state (GdkKeymap *keymap)
661 {
662 #ifdef HAVE_XKB
663   if (KEYMAP_USE_XKB (keymap))
664     {
665       GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
666
667       if (!keymap_x11->have_lock_state)
668         {
669           GdkDisplay *display = keymap->display;
670           XkbStateRec state_rec;
671
672           XkbGetState (GDK_DISPLAY_XDISPLAY (display), XkbUseCoreKbd, &state_rec);
673           update_lock_state (keymap_x11, state_rec.locked_mods, state_rec.mods);
674         }
675     }
676 #endif /* HAVE_XKB */
677 }
678
679 void
680 _gdk_x11_keymap_keys_changed (GdkDisplay *display)
681 {
682   GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
683
684   ++display_x11->keymap_serial;
685
686   if (display_x11->keymap)
687     g_signal_emit_by_name (display_x11->keymap, "keys_changed", 0);
688 }
689
690 static PangoDirection
691 gdk_x11_keymap_get_direction (GdkKeymap *keymap)
692 {
693 #ifdef HAVE_XKB
694   if (KEYMAP_USE_XKB (keymap))
695     {
696       GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
697
698       if (!keymap_x11->have_direction)
699         {
700           GdkDisplay *display = keymap->display;
701           XkbStateRec state_rec;
702
703           XkbGetState (GDK_DISPLAY_XDISPLAY (display), XkbUseCoreKbd,
704                        &state_rec);
705           update_direction (keymap_x11, XkbStateGroup (&state_rec));
706         }
707
708       return keymap_x11->current_direction;
709     }
710   else
711 #endif /* HAVE_XKB */
712     return PANGO_DIRECTION_NEUTRAL;
713 }
714
715 static gboolean
716 gdk_x11_keymap_have_bidi_layouts (GdkKeymap *keymap)
717 {
718 #ifdef HAVE_XKB
719   if (KEYMAP_USE_XKB (keymap))
720     {
721       GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
722       XkbDescPtr xkb = get_xkb (keymap_x11);
723       int num_groups = get_num_groups (keymap, xkb);
724
725       int i;
726       gboolean have_ltr_keyboard = FALSE;
727       gboolean have_rtl_keyboard = FALSE;
728
729       for (i = 0; i < num_groups; i++)
730       {
731         if (get_direction_from_cache (keymap_x11, xkb, i) == PANGO_DIRECTION_RTL)
732           have_rtl_keyboard = TRUE;
733         else
734           have_ltr_keyboard = TRUE;
735       }
736
737       return have_ltr_keyboard && have_rtl_keyboard;
738     }
739   else
740 #endif /* HAVE_XKB */
741     return FALSE;
742 }
743
744 static gboolean
745 gdk_x11_keymap_get_caps_lock_state (GdkKeymap *keymap)
746 {
747   GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
748
749   ensure_lock_state (keymap);
750
751   return keymap_x11->caps_lock_state;
752 }
753
754 static gboolean
755 gdk_x11_keymap_get_num_lock_state (GdkKeymap *keymap)
756 {
757   GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
758
759   ensure_lock_state (keymap);
760
761   return keymap_x11->num_lock_state;
762 }
763
764 static guint
765 gdk_x11_keymap_get_modifier_state (GdkKeymap *keymap)
766 {
767   GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
768
769   ensure_lock_state (keymap);
770
771   return keymap_x11->modifier_state;
772 }
773
774 static gboolean
775 gdk_x11_keymap_get_entries_for_keyval (GdkKeymap     *keymap,
776                                        guint          keyval,
777                                        GdkKeymapKey **keys,
778                                        gint          *n_keys)
779 {
780   GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
781   GArray *retval;
782
783   retval = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
784
785 #ifdef HAVE_XKB
786   if (KEYMAP_USE_XKB (keymap))
787     {
788       /* See sec 15.3.4 in XKB docs */
789
790       XkbDescRec *xkb = get_xkb (keymap_x11);
791       gint keycode;
792
793       keycode = keymap_x11->min_keycode;
794
795       while (keycode <= keymap_x11->max_keycode)
796         {
797           gint max_shift_levels = XkbKeyGroupsWidth (xkb, keycode); /* "key width" */
798           gint group = 0;
799           gint level = 0;
800           gint total_syms = XkbKeyNumSyms (xkb, keycode);
801           gint i = 0;
802           KeySym *entry;
803
804           /* entry is an array with all syms for group 0, all
805            * syms for group 1, etc. and for each group the
806            * shift level syms are in order
807            */
808           entry = XkbKeySymsPtr (xkb, keycode);
809
810           while (i < total_syms)
811             {
812               /* check out our cool loop invariant */
813               g_assert (i == (group * max_shift_levels + level));
814
815               if (entry[i] == keyval)
816                 {
817                   /* Found a match */
818                   GdkKeymapKey key;
819
820                   key.keycode = keycode;
821                   key.group = group;
822                   key.level = level;
823
824                   g_array_append_val (retval, key);
825
826                   g_assert (XkbKeySymEntry (xkb, keycode, level, group) ==
827                             keyval);
828                 }
829
830               ++level;
831
832               if (level == max_shift_levels)
833                 {
834                   level = 0;
835                   ++group;
836                 }
837
838               ++i;
839             }
840
841           ++keycode;
842         }
843     }
844   else
845 #endif
846     {
847       const KeySym *map = get_keymap (keymap_x11);
848       gint keycode;
849
850       keycode = keymap_x11->min_keycode;
851       while (keycode <= keymap_x11->max_keycode)
852         {
853           const KeySym *syms = map + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
854           gint i = 0;
855
856           while (i < keymap_x11->keysyms_per_keycode)
857             {
858               if (syms[i] == keyval)
859                 {
860                   /* found a match */
861                   GdkKeymapKey key;
862
863                   key.keycode = keycode;
864
865                   /* The "classic" non-XKB keymap has 2 levels per group */
866                   key.group = i / 2;
867                   key.level = i % 2;
868
869                   g_array_append_val (retval, key);
870                 }
871
872               ++i;
873             }
874
875           ++keycode;
876         }
877     }
878
879   if (retval->len > 0)
880     {
881       *keys = (GdkKeymapKey*) retval->data;
882       *n_keys = retval->len;
883     }
884   else
885     {
886       *keys = NULL;
887       *n_keys = 0;
888     }
889
890   g_array_free (retval, retval->len > 0 ? FALSE : TRUE);
891
892   return *n_keys > 0;
893 }
894
895 static gboolean
896 gdk_x11_keymap_get_entries_for_keycode (GdkKeymap     *keymap,
897                                         guint          hardware_keycode,
898                                         GdkKeymapKey **keys,
899                                         guint        **keyvals,
900                                         gint          *n_entries)
901 {
902   GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
903   GArray *key_array;
904   GArray *keyval_array;
905
906   update_keyrange (keymap_x11);
907
908   if (hardware_keycode < keymap_x11->min_keycode ||
909       hardware_keycode > keymap_x11->max_keycode)
910     {
911       if (keys)
912         *keys = NULL;
913       if (keyvals)
914         *keyvals = NULL;
915
916       *n_entries = 0;
917       return FALSE;
918     }
919
920   if (keys)
921     key_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
922   else
923     key_array = NULL;
924
925   if (keyvals)
926     keyval_array = g_array_new (FALSE, FALSE, sizeof (guint));
927   else
928     keyval_array = NULL;
929
930 #ifdef HAVE_XKB
931   if (KEYMAP_USE_XKB (keymap))
932     {
933       /* See sec 15.3.4 in XKB docs */
934
935       XkbDescRec *xkb = get_xkb (keymap_x11);
936       gint max_shift_levels;
937       gint group = 0;
938       gint level = 0;
939       gint total_syms;
940       gint i = 0;
941       KeySym *entry;
942
943       max_shift_levels = XkbKeyGroupsWidth (xkb, hardware_keycode); /* "key width" */
944       total_syms = XkbKeyNumSyms (xkb, hardware_keycode);
945
946       /* entry is an array with all syms for group 0, all
947        * syms for group 1, etc. and for each group the
948        * shift level syms are in order
949        */
950       entry = XkbKeySymsPtr (xkb, hardware_keycode);
951
952       while (i < total_syms)
953         {
954           /* check out our cool loop invariant */
955           g_assert (i == (group * max_shift_levels + level));
956
957           if (key_array)
958             {
959               GdkKeymapKey key;
960
961               key.keycode = hardware_keycode;
962               key.group = group;
963               key.level = level;
964
965               g_array_append_val (key_array, key);
966             }
967
968           if (keyval_array)
969             g_array_append_val (keyval_array, entry[i]);
970
971           ++level;
972
973           if (level == max_shift_levels)
974             {
975               level = 0;
976               ++group;
977             }
978
979           ++i;
980         }
981     }
982   else
983 #endif
984     {
985       const KeySym *map = get_keymap (keymap_x11);
986       const KeySym *syms;
987       gint i = 0;
988
989       syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
990
991       while (i < keymap_x11->keysyms_per_keycode)
992         {
993           if (key_array)
994             {
995               GdkKeymapKey key;
996
997               key.keycode = hardware_keycode;
998
999               /* The "classic" non-XKB keymap has 2 levels per group */
1000               key.group = i / 2;
1001               key.level = i % 2;
1002
1003               g_array_append_val (key_array, key);
1004             }
1005
1006           if (keyval_array)
1007             g_array_append_val (keyval_array, syms[i]);
1008
1009           ++i;
1010         }
1011     }
1012
1013   *n_entries = 0;
1014
1015   if (keys)
1016     {
1017       *n_entries = key_array->len;
1018       *keys = (GdkKeymapKey*) g_array_free (key_array, FALSE);
1019     }
1020
1021   if (keyvals)
1022     {
1023       *n_entries = keyval_array->len;
1024       *keyvals = (guint*) g_array_free (keyval_array, FALSE);
1025     }
1026
1027   return *n_entries > 0;
1028 }
1029
1030 static guint
1031 gdk_x11_keymap_lookup_key (GdkKeymap          *keymap,
1032                            const GdkKeymapKey *key)
1033 {
1034   GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
1035
1036   g_return_val_if_fail (key->group < 4, 0);
1037
1038 #ifdef HAVE_XKB
1039   if (KEYMAP_USE_XKB (keymap))
1040     {
1041       XkbDescRec *xkb = get_xkb (keymap_x11);
1042
1043       return XkbKeySymEntry (xkb, key->keycode, key->level, key->group);
1044     }
1045   else
1046 #endif
1047     {
1048       const KeySym *map = get_keymap (keymap_x11);
1049       const KeySym *syms = map + (key->keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1050       return get_symbol (syms, keymap_x11, key->group, key->level);
1051     }
1052 }
1053
1054 #ifdef HAVE_XKB
1055 /* This is copied straight from XFree86 Xlib, to:
1056  *  - add the group and level return.
1057  *  - change the interpretation of mods_rtrn as described
1058  *    in the docs for gdk_keymap_translate_keyboard_state()
1059  * It's unchanged for ease of diff against the Xlib sources; don't
1060  * reformat it.
1061  */
1062 static Bool
1063 MyEnhancedXkbTranslateKeyCode(register XkbDescPtr     xkb,
1064                               KeyCode                 key,
1065                               register unsigned int   mods,
1066                               unsigned int *          mods_rtrn,
1067                               KeySym *                keysym_rtrn,
1068                               int *                   group_rtrn,
1069                               int *                   level_rtrn)
1070 {
1071     XkbKeyTypeRec *type;
1072     int col,nKeyGroups;
1073     unsigned preserve,effectiveGroup;
1074     KeySym *syms;
1075
1076     if (mods_rtrn!=NULL)
1077         *mods_rtrn = 0;
1078
1079     nKeyGroups= XkbKeyNumGroups(xkb,key);
1080     if ((!XkbKeycodeInRange(xkb,key))||(nKeyGroups==0)) {
1081         if (keysym_rtrn!=NULL)
1082             *keysym_rtrn = NoSymbol;
1083         return False;
1084     }
1085
1086     syms = XkbKeySymsPtr(xkb,key);
1087
1088     /* find the offset of the effective group */
1089     col = 0;
1090     effectiveGroup= XkbGroupForCoreState(mods);
1091     if ( effectiveGroup>=nKeyGroups ) {
1092         unsigned groupInfo= XkbKeyGroupInfo(xkb,key);
1093         switch (XkbOutOfRangeGroupAction(groupInfo)) {
1094             default:
1095                 effectiveGroup %= nKeyGroups;
1096                 break;
1097             case XkbClampIntoRange:
1098                 effectiveGroup = nKeyGroups-1;
1099                 break;
1100             case XkbRedirectIntoRange:
1101                 effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
1102                 if (effectiveGroup>=nKeyGroups)
1103                     effectiveGroup= 0;
1104                 break;
1105         }
1106     }
1107     col= effectiveGroup*XkbKeyGroupsWidth(xkb,key);
1108     type = XkbKeyKeyType(xkb,key,effectiveGroup);
1109
1110     preserve= 0;
1111     if (type->map) { /* find the column (shift level) within the group */
1112         register int i;
1113         register XkbKTMapEntryPtr entry;
1114         /* ---- Begin section modified for GDK  ---- */
1115         int found = 0;
1116
1117         for (i=0,entry=type->map;i<type->map_count;i++,entry++) {
1118             if (mods_rtrn) {
1119                 int bits = 0;
1120                 unsigned long tmp = entry->mods.mask;
1121                 while (tmp) {
1122                     if ((tmp & 1) == 1)
1123                         bits++;
1124                     tmp >>= 1;
1125                 }
1126                 /* We always add one-modifiers levels to mods_rtrn since
1127                  * they can't wipe out bits in the state unless the
1128                  * level would be triggered. But return other modifiers
1129                  *
1130                  */
1131                 if (bits == 1 || (mods&type->mods.mask)==entry->mods.mask)
1132                     *mods_rtrn |= entry->mods.mask;
1133             }
1134
1135             if (!found&&entry->active&&((mods&type->mods.mask)==entry->mods.mask)) {
1136                 col+= entry->level;
1137                 if (type->preserve)
1138                     preserve= type->preserve[i].mask;
1139
1140                 if (level_rtrn)
1141                   *level_rtrn = entry->level;
1142
1143                 found = 1;
1144             }
1145         }
1146         /* ---- End section modified for GDK ---- */
1147     }
1148
1149     if (keysym_rtrn!=NULL)
1150         *keysym_rtrn= syms[col];
1151     if (mods_rtrn) {
1152         /* ---- Begin section modified for GDK  ---- */
1153         *mods_rtrn &= ~preserve;
1154         /* ---- End section modified for GDK ---- */
1155
1156         /* ---- Begin stuff GDK comments out of the original Xlib version ---- */
1157         /* This is commented out because xkb_info is a private struct */
1158
1159 #if 0
1160         /* The Motif VTS doesn't get the help callback called if help
1161          * is bound to Shift+<whatever>, and it appears as though it
1162          * is XkbTranslateKeyCode that is causing the problem.  The
1163          * core X version of XTranslateKey always OR's in ShiftMask
1164          * and LockMask for mods_rtrn, so this "fix" keeps this behavior
1165          * and solves the VTS problem.
1166          */
1167         if ((xkb->dpy)&&(xkb->dpy->xkb_info)&&
1168             (xkb->dpy->xkb_info->xlib_ctrls&XkbLC_AlwaysConsumeShiftAndLock)) {            *mods_rtrn|= (ShiftMask|LockMask);
1169         }
1170 #endif
1171
1172         /* ---- End stuff GDK comments out of the original Xlib version ---- */
1173     }
1174
1175     /* ---- Begin stuff GDK adds to the original Xlib version ---- */
1176
1177     if (group_rtrn)
1178       *group_rtrn = effectiveGroup;
1179
1180     /* ---- End stuff GDK adds to the original Xlib version ---- */
1181
1182     return (syms[col] != NoSymbol);
1183 }
1184 #endif /* HAVE_XKB */
1185
1186 /* Translates from keycode/state to keysymbol using the traditional interpretation
1187  * of the keyboard map. See section 12.7 of the Xlib reference manual
1188  */
1189 static guint
1190 translate_keysym (GdkX11Keymap   *keymap_x11,
1191                   guint           hardware_keycode,
1192                   gint            group,
1193                   GdkModifierType state,
1194                   gint           *effective_group,
1195                   gint           *effective_level)
1196 {
1197   const KeySym *map = get_keymap (keymap_x11);
1198   const KeySym *syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1199
1200 #define SYM(k,g,l) get_symbol (syms, k,g,l)
1201
1202   GdkModifierType shift_modifiers;
1203   gint shift_level;
1204   guint tmp_keyval;
1205   gint num_lock_index;
1206
1207   shift_modifiers = GDK_SHIFT_MASK;
1208   if (keymap_x11->lock_keysym == GDK_KEY_Shift_Lock)
1209     shift_modifiers |= GDK_LOCK_MASK;
1210
1211   /* Fall back to the first group if the passed in group is empty
1212    */
1213   if (!(SYM (keymap_x11, group, 0) || SYM (keymap_x11, group, 1)) &&
1214       (SYM (keymap_x11, 0, 0) || SYM (keymap_x11, 0, 1)))
1215     group = 0;
1216
1217   /* Hack: On Sun, the Num Lock modifier uses the third element in the
1218    * keysym array, and Mode_Switch does not apply for a keypad key.
1219    */
1220   if (keymap_x11->sun_keypad)
1221     {
1222       num_lock_index = 2;
1223
1224       if (group != 0)
1225         {
1226           gint i;
1227
1228           for (i = 0; i < keymap_x11->keysyms_per_keycode; i++)
1229             if (KEYSYM_IS_KEYPAD (SYM (keymap_x11, 0, i)))
1230               group = 0;
1231         }
1232     }
1233   else
1234     num_lock_index = 1;
1235
1236   if ((state & keymap_x11->num_lock_mask) &&
1237       KEYSYM_IS_KEYPAD (SYM (keymap_x11, group, num_lock_index)))
1238     {
1239       /* Shift, Shift_Lock cancel Num_Lock
1240        */
1241       shift_level = (state & shift_modifiers) ? 0 : num_lock_index;
1242       if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1243         shift_level = 0;
1244
1245        tmp_keyval = SYM (keymap_x11, group, shift_level);
1246     }
1247   else
1248     {
1249       /* Fall back to the first level if no symbol for the level
1250        * we were passed.
1251        */
1252       shift_level = (state & shift_modifiers) ? 1 : 0;
1253       if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1254         shift_level = 0;
1255
1256       tmp_keyval = SYM (keymap_x11, group, shift_level);
1257
1258       if (keymap_x11->lock_keysym == GDK_KEY_Caps_Lock && (state & GDK_LOCK_MASK) != 0)
1259         {
1260           guint upper = gdk_keyval_to_upper (tmp_keyval);
1261           if (upper != tmp_keyval)
1262             tmp_keyval = upper;
1263         }
1264     }
1265
1266   if (effective_group)
1267     *effective_group = group;
1268
1269   if (effective_level)
1270     *effective_level = shift_level;
1271
1272   return tmp_keyval;
1273
1274 #undef SYM
1275 }
1276
1277 static gboolean
1278 gdk_x11_keymap_translate_keyboard_state (GdkKeymap       *keymap,
1279                                          guint            hardware_keycode,
1280                                          GdkModifierType  state,
1281                                          gint             group,
1282                                          guint           *keyval,
1283                                          gint            *effective_group,
1284                                          gint            *level,
1285                                          GdkModifierType *consumed_modifiers)
1286 {
1287   GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
1288   KeySym tmp_keyval = NoSymbol;
1289   guint tmp_modifiers;
1290
1291   g_return_val_if_fail (group < 4, FALSE);
1292
1293   if (keyval)
1294     *keyval = NoSymbol;
1295   if (effective_group)
1296     *effective_group = 0;
1297   if (level)
1298     *level = 0;
1299   if (consumed_modifiers)
1300     *consumed_modifiers = 0;
1301
1302   update_keyrange (keymap_x11);
1303
1304   if (hardware_keycode < keymap_x11->min_keycode ||
1305       hardware_keycode > keymap_x11->max_keycode)
1306     return FALSE;
1307
1308 #ifdef HAVE_XKB
1309   if (KEYMAP_USE_XKB (keymap))
1310     {
1311       XkbDescRec *xkb = get_xkb (keymap_x11);
1312
1313       /* replace bits 13 and 14 with the provided group */
1314       state &= ~(1 << 13 | 1 << 14);
1315       state |= group << 13;
1316
1317       MyEnhancedXkbTranslateKeyCode (xkb,
1318                                      hardware_keycode,
1319                                      state,
1320                                      &tmp_modifiers,
1321                                      &tmp_keyval,
1322                                      effective_group,
1323                                      level);
1324
1325       if (state & ~tmp_modifiers & LockMask)
1326         tmp_keyval = gdk_keyval_to_upper (tmp_keyval);
1327
1328       /* We need to augment the consumed modifiers with LockMask, since
1329        * we handle that ourselves, and also with the group bits
1330        */
1331       tmp_modifiers |= LockMask | 1 << 13 | 1 << 14;
1332     }
1333   else
1334 #endif
1335     {
1336       GdkModifierType bit;
1337
1338       tmp_modifiers = 0;
1339
1340       /* We see what modifiers matter by trying the translation with
1341        * and without each possible modifier
1342        */
1343       for (bit = GDK_SHIFT_MASK; bit < GDK_BUTTON1_MASK; bit <<= 1)
1344         {
1345           /* Handling of the group here is a bit funky; a traditional
1346            * X keyboard map can have more than two groups, but no way
1347            * of accessing the extra groups is defined. We allow a
1348            * caller to pass in any group to this function, but we
1349            * only can represent switching between group 0 and 1 in
1350            * consumed modifiers.
1351            */
1352           if (translate_keysym (keymap_x11, hardware_keycode,
1353                                 (bit == keymap_x11->group_switch_mask) ? 0 : group,
1354                                 state & ~bit,
1355                                 NULL, NULL) !=
1356               translate_keysym (keymap_x11, hardware_keycode,
1357                                 (bit == keymap_x11->group_switch_mask) ? 1 : group,
1358                                 state | bit,
1359                                 NULL, NULL))
1360             tmp_modifiers |= bit;
1361         }
1362
1363       tmp_keyval = translate_keysym (keymap_x11, hardware_keycode,
1364                                      group, state,
1365                                      level, effective_group);
1366     }
1367
1368   if (consumed_modifiers)
1369     *consumed_modifiers = tmp_modifiers;
1370
1371   if (keyval)
1372     *keyval = tmp_keyval;
1373
1374   return tmp_keyval != NoSymbol;
1375 }
1376
1377 /* Key handling not part of the keymap */
1378 gchar*
1379 _gdk_x11_display_manager_get_keyval_name (GdkDisplayManager *manager,
1380                                           guint              keyval)
1381 {
1382   switch (keyval)
1383     {
1384     case GDK_KEY_Page_Up:
1385       return "Page_Up";
1386     case GDK_KEY_Page_Down:
1387       return "Page_Down";
1388     case GDK_KEY_KP_Page_Up:
1389       return "KP_Page_Up";
1390     case GDK_KEY_KP_Page_Down:
1391       return "KP_Page_Down";
1392     }
1393
1394   return XKeysymToString (keyval);
1395 }
1396
1397 guint
1398 _gdk_x11_display_manager_lookup_keyval (GdkDisplayManager *manager,
1399                                         const gchar       *keyval_name)
1400 {
1401   g_return_val_if_fail (keyval_name != NULL, 0);
1402
1403   return XStringToKeysym (keyval_name);
1404 }
1405
1406 #ifdef HAVE_XCONVERTCASE
1407 void
1408 _gdk_x11_display_manager_keyval_convert_case (GdkDisplayManager *manager,
1409                                               guint              symbol,
1410                                               guint             *lower,
1411                                               guint             *upper)
1412 {
1413   KeySym xlower = 0;
1414   KeySym xupper = 0;
1415
1416   /* Check for directly encoded 24-bit UCS characters: */
1417   if ((symbol & 0xff000000) == 0x01000000)
1418     {
1419       if (lower)
1420         *lower = gdk_unicode_to_keyval (g_unichar_tolower (symbol & 0x00ffffff));
1421       if (upper)
1422         *upper = gdk_unicode_to_keyval (g_unichar_toupper (symbol & 0x00ffffff));
1423       return;
1424     }
1425
1426   if (symbol)
1427     XConvertCase (symbol, &xlower, &xupper);
1428
1429   if (lower)
1430     *lower = xlower;
1431   if (upper)
1432     *upper = xupper;
1433 }
1434 #endif /* HAVE_XCONVERTCASE */
1435
1436 gint
1437 _gdk_x11_get_group_for_state (GdkDisplay      *display,
1438                               GdkModifierType  state)
1439 {
1440   GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1441
1442 #ifdef HAVE_XKB
1443   if (display_x11->use_xkb)
1444     {
1445       return XkbGroupForCoreState (state);
1446     }
1447   else
1448 #endif
1449     {
1450       GdkX11Keymap *keymap_impl = GDK_X11_KEYMAP (gdk_keymap_get_for_display (display));
1451       update_keymaps (keymap_impl);
1452       return (state & keymap_impl->group_switch_mask) ? 1 : 0;
1453     }
1454 }
1455
1456 void
1457 _gdk_x11_keymap_add_virt_mods (GdkKeymap       *keymap,
1458                                GdkModifierType *modifiers)
1459 {
1460   GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
1461   int i;
1462
1463   /* See comment in add_virtual_modifiers() */
1464   for (i = 4; i < 8; i++)
1465     {
1466       if ((1 << i) & *modifiers)
1467         {
1468           if (keymap_x11->modmap[i] & GDK_SUPER_MASK)
1469             *modifiers |= GDK_SUPER_MASK;
1470           else if (keymap_x11->modmap[i] & GDK_HYPER_MASK)
1471             *modifiers |= GDK_HYPER_MASK;
1472           else if (keymap_x11->modmap[i] & GDK_META_MASK)
1473             *modifiers |= GDK_META_MASK;
1474         }
1475     }
1476 }
1477
1478 static void
1479 gdk_x11_keymap_add_virtual_modifiers (GdkKeymap       *keymap,
1480                                       GdkModifierType *state)
1481 {
1482   GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
1483   int i;
1484
1485   /*  This loop used to start at 3, which included MOD1 in the
1486    *  virtual mapping. However, all of GTK+ treats MOD1 as a
1487    *  synonym for Alt, and does not expect it to be mapped around,
1488    *  therefore it's more sane to simply treat MOD1 like SHIFT and
1489    *  CONTROL, which are not mappable either.
1490    */
1491   for (i = 4; i < 8; i++)
1492     {
1493       if ((1 << i) & *state)
1494         {
1495           if (keymap_x11->modmap[i] & GDK_SUPER_MASK)
1496             *state |= GDK_SUPER_MASK;
1497           if (keymap_x11->modmap[i] & GDK_HYPER_MASK)
1498             *state |= GDK_HYPER_MASK;
1499           if (keymap_x11->modmap[i] & GDK_META_MASK)
1500             *state |= GDK_META_MASK;
1501         }
1502     }
1503 }
1504
1505 gboolean
1506 _gdk_x11_keymap_key_is_modifier (GdkKeymap *keymap,
1507                                  guint      keycode)
1508 {
1509   GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
1510   gint i;
1511
1512   update_keyrange (keymap_x11);
1513   if (keycode < keymap_x11->min_keycode ||
1514       keycode > keymap_x11->max_keycode)
1515     return FALSE;
1516
1517 #ifdef HAVE_XKB
1518   if (KEYMAP_USE_XKB (keymap))
1519     {
1520       XkbDescRec *xkb = get_xkb (keymap_x11);
1521
1522       if (xkb->map->modmap && xkb->map->modmap[keycode] != 0)
1523         return TRUE;
1524     }
1525   else
1526 #endif
1527     {
1528       for (i = 0; i < 8 * keymap_x11->mod_keymap->max_keypermod; i++)
1529         {
1530           if (keycode == keymap_x11->mod_keymap->modifiermap[i])
1531             return TRUE;
1532         }
1533     }
1534
1535   return FALSE;
1536 }
1537
1538 static gboolean
1539 gdk_x11_keymap_map_virtual_modifiers (GdkKeymap       *keymap,
1540                                       GdkModifierType *state)
1541 {
1542   GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
1543   const guint vmods[] = { GDK_SUPER_MASK, GDK_HYPER_MASK, GDK_META_MASK };
1544   int i, j;
1545   gboolean retval;
1546
1547   if (KEYMAP_USE_XKB (keymap))
1548     get_xkb (keymap_x11);
1549
1550   retval = TRUE;
1551
1552   for (j = 0; j < 3; j++)
1553     {
1554       if (*state & vmods[j])
1555         {
1556           /* See comment in add_virtual_modifiers() */
1557           for (i = 4; i < 8; i++)
1558             {
1559               if (keymap_x11->modmap[i] & vmods[j])
1560                 {
1561                   if (*state & (1 << i))
1562                     retval = FALSE;
1563                   else
1564                     *state |= 1 << i;
1565                 }
1566             }
1567         }
1568     }
1569
1570   return retval;
1571 }
1572
1573 static GdkModifierType
1574 gdk_x11_keymap_get_modifier_mask (GdkKeymap         *keymap,
1575                                   GdkModifierIntent  intent)
1576 {
1577   GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
1578
1579   switch (intent)
1580     {
1581     case GDK_MODIFIER_INTENT_SHIFT_GROUP:
1582       return keymap_x11->group_switch_mask;
1583
1584     default:
1585       return GDK_KEYMAP_CLASS (gdk_x11_keymap_parent_class)->get_modifier_mask (keymap,
1586                                                                                 intent);
1587     }
1588 }
1589
1590 static void
1591 gdk_x11_keymap_class_init (GdkX11KeymapClass *klass)
1592 {
1593   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1594   GdkKeymapClass *keymap_class = GDK_KEYMAP_CLASS (klass);
1595
1596   object_class->finalize = gdk_x11_keymap_finalize;
1597
1598   keymap_class->get_direction = gdk_x11_keymap_get_direction;
1599   keymap_class->have_bidi_layouts = gdk_x11_keymap_have_bidi_layouts;
1600   keymap_class->get_caps_lock_state = gdk_x11_keymap_get_caps_lock_state;
1601   keymap_class->get_num_lock_state = gdk_x11_keymap_get_num_lock_state;
1602   keymap_class->get_modifier_state = gdk_x11_keymap_get_modifier_state;
1603   keymap_class->get_entries_for_keyval = gdk_x11_keymap_get_entries_for_keyval;
1604   keymap_class->get_entries_for_keycode = gdk_x11_keymap_get_entries_for_keycode;
1605   keymap_class->lookup_key = gdk_x11_keymap_lookup_key;
1606   keymap_class->translate_keyboard_state = gdk_x11_keymap_translate_keyboard_state;
1607   keymap_class->add_virtual_modifiers = gdk_x11_keymap_add_virtual_modifiers;
1608   keymap_class->map_virtual_modifiers = gdk_x11_keymap_map_virtual_modifiers;
1609   keymap_class->get_modifier_mask = gdk_x11_keymap_get_modifier_mask;
1610 }