]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkkeys-x11.c
Fix from Phil Blundell for compiling without Xkb. (#95909)
[~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 <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <limits.h>
32 #include <errno.h>
33
34 #include "gdk.h"
35 #include "gdkx.h"
36
37 #include "gdkprivate-x11.h"
38 #include "gdkinternals.h"
39 #include "gdkdisplay-x11.h"
40 #include "gdkkeysyms.h"
41
42 #include "config.h"
43
44 #ifdef HAVE_XKB
45 #include <X11/XKBlib.h>
46
47 /* OSF-4.0 is apparently missing this macro
48  */
49 #  ifndef XkbKeySymEntry
50 #    define XkbKeySymEntry(d,k,sl,g) \
51         (XkbKeySym(d,k,((XkbKeyGroupsWidth(d,k)*(g))+(sl))))
52 #  endif
53 #endif /* HAVE_XKB */
54
55 typedef struct _GdkKeymapX11 GdkKeymapX11;
56
57 #define GDK_TYPE_KEYMAP_X11          (gdk_keymap_x11_get_type ())
58 #define GDK_KEYMAP_X11(object)       (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_KEYMAP_X11, GdkKeymapX11))
59 #define GDK_IS_KEYMAP_X11(object)    (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_KEYMAP_X11))
60
61 struct _GdkKeymapX11
62 {
63   GdkKeymap     parent_instance;
64   
65   gint min_keycode;
66   gint max_keycode;
67   KeySym* keymap;
68   gint keysyms_per_keycode;
69   XModifierKeymap* mod_keymap;
70   GdkModifierType group_switch_mask;
71   PangoDirection current_direction;
72   gboolean have_direction;
73   guint current_serial;
74   
75 #ifdef HAVE_XKB
76   XkbDescPtr xkb_desc;
77 #endif
78 };
79
80 #define KEYMAP_USE_XKB(keymap) GDK_DISPLAY_X11 ((keymap)->display)->use_xkb
81 #define KEYMAP_XDISPLAY(keymap) GDK_DISPLAY_XDISPLAY ((keymap)->display)
82
83 static GType gdk_keymap_x11_get_type (void);
84 static void  gdk_keymap_x11_init     (GdkKeymapX11 *keymap);
85
86 static GType
87 gdk_keymap_x11_get_type (void)
88 {
89   static GType object_type = 0;
90
91   if (!object_type)
92     {
93       static const GTypeInfo object_info =
94         {
95           sizeof (GdkKeymapClass),
96           (GBaseInitFunc) NULL,
97           (GBaseFinalizeFunc) NULL,
98           (GClassInitFunc) NULL,
99           NULL,           /* class_finalize */
100           NULL,           /* class_data */
101           sizeof (GdkKeymapX11),
102           0,              /* n_preallocs */
103           (GInstanceInitFunc) gdk_keymap_x11_init,
104         };
105       
106       object_type = g_type_register_static (GDK_TYPE_KEYMAP,
107                                             "GdkKeymapX11",
108                                             &object_info, 0);
109     }
110   
111   return object_type;
112 }
113
114 static void
115 gdk_keymap_x11_init (GdkKeymapX11 *keymap)
116 {
117   keymap->min_keycode = 0;
118   keymap->max_keycode = 0;
119
120   keymap->keymap = NULL;
121   keymap->keysyms_per_keycode = 0;
122   keymap->mod_keymap = NULL;
123   
124   keymap->group_switch_mask = 0;
125   keymap->have_direction = FALSE;
126   
127 #ifdef HAVE_XKB
128   keymap->xkb_desc = NULL;
129 #endif
130
131   keymap->current_serial = 0;
132 }
133
134 static inline void
135 update_keyrange (GdkKeymapX11 *keymap_x11)
136 {
137   if (keymap_x11->max_keycode == 0)
138     XDisplayKeycodes (KEYMAP_XDISPLAY (GDK_KEYMAP (keymap_x11)),
139                       &keymap_x11->min_keycode, &keymap_x11->max_keycode);
140 }
141
142 #ifdef HAVE_XKB
143
144 static XkbDescPtr
145 get_xkb (GdkKeymapX11 *keymap_x11)
146 {
147   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_KEYMAP (keymap_x11)->display);
148   Display *xdisplay = display_x11->xdisplay;
149   
150   update_keyrange (keymap_x11);
151   
152   if (keymap_x11->xkb_desc == NULL)
153     {
154       keymap_x11->xkb_desc = XkbGetMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask, XkbUseCoreKbd);
155       if (keymap_x11->xkb_desc == NULL)
156         g_error ("Failed to get keymap");
157
158       XkbGetNames (xdisplay, XkbGroupNamesMask, keymap_x11->xkb_desc);
159     }
160   else if (keymap_x11->current_serial != display_x11->keymap_serial)
161     {
162       XkbGetUpdatedMap (xdisplay, XkbKeySymsMask | XkbKeyTypesMask,
163                         keymap_x11->xkb_desc);
164       XkbGetNames (xdisplay, XkbGroupNamesMask, keymap_x11->xkb_desc);
165     }
166
167   keymap_x11->current_serial = display_x11->keymap_serial;
168
169   return keymap_x11->xkb_desc;
170 }
171 #endif /* HAVE_XKB */
172
173 /* Whether we were able to turn on detectable-autorepeat using
174  * XkbSetDetectableAutorepeat. If FALSE, we'll fall back
175  * to checking the next event with XPending().
176  */
177
178 /** 
179  * gdk_keymap_get_for_display:
180  * @display: the #GdkDisplay.
181  * @returns: the #GdkKeymap attached to @display.
182  *
183  * Returns the #GdkKeymap attached to @display.
184  **/
185 GdkKeymap*
186 gdk_keymap_get_for_display (GdkDisplay *display)
187 {
188   GdkDisplayX11 *display_x11;
189   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
190   display_x11 = GDK_DISPLAY_X11 (display);
191   
192   if (!display_x11->keymap)
193     display_x11->keymap = g_object_new (gdk_keymap_x11_get_type (), NULL);
194
195   display_x11->keymap->display = display;
196
197   return display_x11->keymap;
198 }
199
200 /* Find the index of the group/level pair within the keysyms for a key.
201  */
202 #define KEYSYM_INDEX(keymap_impl, group, level) \
203   (2 * ((group) % (keymap_impl->keysyms_per_keycode / 2)) + (level))
204
205 static void
206 update_keymaps (GdkKeymapX11 *keymap_x11)
207 {
208   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (GDK_KEYMAP (keymap_x11)->display);
209   Display *xdisplay = display_x11->xdisplay;
210   
211 #ifdef HAVE_XKB
212   g_assert (!KEYMAP_USE_XKB (GDK_KEYMAP (keymap_x11)));
213 #endif
214   
215   if (keymap_x11->keymap == NULL ||
216       keymap_x11->current_serial != display_x11->keymap_serial)
217     {
218       gint i;
219       gint map_size;
220       gint keycode;
221
222       keymap_x11->current_serial = display_x11->keymap_serial;
223       
224       update_keyrange (keymap_x11);
225       
226       if (keymap_x11->keymap)
227         XFree (keymap_x11->keymap);
228
229       if (keymap_x11->mod_keymap)
230         XFreeModifiermap (keymap_x11->mod_keymap);
231       
232       keymap_x11->keymap = XGetKeyboardMapping (xdisplay, keymap_x11->min_keycode,
233                                                 keymap_x11->max_keycode - keymap_x11->min_keycode + 1,
234                                                 &keymap_x11->keysyms_per_keycode);
235
236
237       /* GDK_ISO_Left_Tab, as usually configured through XKB, really messes
238        * up the whole idea of "consumed modifiers" because shift is consumed.
239        * However, <shift>Tab is not usually GDK_ISO_Left_Tab without XKB,
240        * we we fudge the map here.
241        */
242       keycode = keymap_x11->min_keycode;
243       while (keycode <= keymap_x11->max_keycode)
244         {
245           KeySym *syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
246           /* Check both groups */
247           for (i = 0 ; i < 2 ; i++)
248             {
249               if (syms[KEYSYM_INDEX (keymap_x11, i, 0)] == GDK_Tab)
250                 syms[KEYSYM_INDEX (keymap_x11, i, 1)] = GDK_ISO_Left_Tab;
251             }
252
253           /*
254            * If there is one keysym and the key symbol has upper and lower
255            * case variants fudge the keymap
256            */
257           if (syms[KEYSYM_INDEX (keymap_x11, 0, 1)] == 0)
258             {
259               guint lower;
260               guint upper;
261
262               gdk_keyval_convert_case (syms[KEYSYM_INDEX (keymap_x11, 0, 0)], &lower, &upper);
263               if (lower != upper)
264                 {
265                   syms[KEYSYM_INDEX (keymap_x11, 0, 0)] = lower;
266                   syms[KEYSYM_INDEX (keymap_x11, 0, 1)] = upper;
267                 }
268             }
269       
270           
271           ++keycode;
272         }
273
274       keymap_x11->mod_keymap = XGetModifierMapping (xdisplay);
275
276
277       keymap_x11->group_switch_mask = 0;
278
279       /* there are 8 modifiers, and the first 3 are shift, shift lock,
280        * and control
281        */
282       map_size = 8 * keymap_x11->mod_keymap->max_keypermod;
283       i = 3 * keymap_x11->mod_keymap->max_keypermod;
284       while (i < map_size)
285         {
286           /* get the key code at this point in the map,
287            * see if its keysym is GDK_Mode_switch, if so
288            * we have the mode key
289            */
290           gint keycode = keymap_x11->mod_keymap->modifiermap[i];
291       
292           if (keycode >= keymap_x11->min_keycode &&
293               keycode <= keymap_x11->max_keycode)
294             {
295               gint j = 0;
296               KeySym *syms = keymap_x11->keymap + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
297               /* GDK_MOD1_MASK is 1 << 3 for example, i.e. the
298                * fourth modifier, i / keyspermod is the modifier
299                * index
300                */
301               guint mask = 1 << ( i / keymap_x11->mod_keymap->max_keypermod);
302
303               /* Some keyboard maps are known to map Mode_Switch as an extra
304                * Mod1 key. In circumstances like that, it won't be used to
305                * switch groups.
306                */
307               if (mask == GDK_CONTROL_MASK || mask == GDK_SHIFT_MASK ||
308                   mask == GDK_LOCK_MASK || mask == GDK_MOD1_MASK)
309                 goto next;
310               
311               while (j < keymap_x11->keysyms_per_keycode)
312                 {
313                   if (syms[j] == GDK_Mode_switch)
314                     {
315                       /* This modifier swaps groups */
316
317                       keymap_x11->group_switch_mask |= mask;
318                       break;
319                     }
320               
321                   ++j;
322                 }
323             }
324
325         next:
326           ++i;
327         }
328     }
329 }
330
331 static const KeySym*
332 get_keymap (GdkKeymapX11 *keymap_x11)
333 {
334   update_keymaps (keymap_x11);
335   
336   return keymap_x11->keymap;
337 }
338
339 #if HAVE_XKB
340 static PangoDirection
341 get_direction (GdkKeymapX11 *keymap_x11)
342 {
343   XkbDescRec *xkb = get_xkb (keymap_x11);
344   const char *name;
345   XkbStateRec state_rec;
346   PangoDirection result;
347
348   GdkDisplay *display = GDK_KEYMAP (keymap_x11)->display;
349
350   XkbGetState (GDK_DISPLAY_XDISPLAY (display), XkbUseCoreKbd, &state_rec);
351   
352   if (xkb->names->groups[state_rec.locked_group] == None)
353     result = PANGO_DIRECTION_LTR;
354   else
355     {
356       name = gdk_x11_get_xatom_name_for_display (display, xkb->names->groups[state_rec.locked_group]);
357
358       if (g_ascii_strcasecmp (name, "arabic") == 0 ||
359           g_ascii_strcasecmp (name, "hebrew") == 0 ||
360           g_ascii_strcasecmp (name, "israelian") == 0)
361         result = PANGO_DIRECTION_RTL;
362       else
363         result = PANGO_DIRECTION_LTR;
364     }
365     
366   return result;
367 }
368
369 void
370 _gdk_keymap_state_changed (GdkDisplay *display)
371 {
372   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
373   
374   if (display_x11->keymap)
375     {
376       GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (display_x11->keymap);
377       
378       PangoDirection new_direction = get_direction (keymap_x11);
379       
380       if (!keymap_x11->have_direction || new_direction != keymap_x11->current_direction)
381         {
382           keymap_x11->have_direction = TRUE;
383           keymap_x11->current_direction = new_direction;
384           g_signal_emit_by_name (keymap_x11, "direction_changed");
385         }
386     }
387 }
388
389 #endif /* HAVE_XKB */
390
391 void
392 _gdk_keymap_keys_changed (GdkDisplay *display)
393 {
394   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
395   
396   ++display_x11->keymap_serial;
397   
398   if (display_x11->keymap)
399     g_signal_emit_by_name (display_x11->keymap, "keys_changed", 0);
400 }
401
402 PangoDirection
403 gdk_keymap_get_direction (GdkKeymap *keymap)
404 {
405   if (!keymap)
406     {
407       keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
408       GDK_NOTE (MULTIHEAD,
409                 g_message ("_multihead : reverting to default display keymap "
410                            "in gdk_keymap_get_direction"));
411     }
412   
413 #if HAVE_XKB
414   if (KEYMAP_USE_XKB (keymap))
415     {
416       GdkKeymapX11 *keymap_x11 = GDK_KEYMAP_X11 (keymap);
417       
418       if (!keymap_x11->have_direction)
419         {
420           keymap_x11->current_direction = get_direction (keymap_x11);
421           keymap_x11->have_direction = TRUE;
422         }
423   
424       return keymap_x11->current_direction;
425     }
426   else
427 #endif /* HAVE_XKB */
428     return PANGO_DIRECTION_LTR;
429 }
430
431 /**
432  * gdk_keymap_get_entries_for_keyval:
433  * @keymap: a #GdkKeymap, or %NULL to use the default keymap
434  * @keyval: a keyval, such as %GDK_a, %GDK_Up, %GDK_Return, etc.
435  * @keys: return location for an array of #GdkKeymapKey
436  * @n_keys: return location for number of elements in returned array
437  * 
438  * Obtains a list of keycode/group/level combinations that will
439  * generate @keyval. Groups and levels are two kinds of keyboard mode;
440  * in general, the level determines whether the top or bottom symbol
441  * on a key is used, and the group determines whether the left or
442  * right symbol is used. On US keyboards, the shift key changes the
443  * keyboard level, and there are no groups. A group switch key might
444  * convert a keyboard between Hebrew to English modes, for example.
445  * #GdkEventKey contains a %group field that indicates the active
446  * keyboard group. The level is computed from the modifier mask.
447  * The returned array should be freed
448  * with g_free().
449  *
450  * Return value: %TRUE if keys were found and returned
451  **/
452 gboolean
453 gdk_keymap_get_entries_for_keyval (GdkKeymap     *keymap,
454                                    guint          keyval,
455                                    GdkKeymapKey **keys,
456                                    gint          *n_keys)
457 {
458   GArray *retval;
459   GdkKeymapX11 *keymap_x11;
460
461   g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
462   g_return_val_if_fail (keys != NULL, FALSE);
463   g_return_val_if_fail (n_keys != NULL, FALSE);
464   g_return_val_if_fail (keyval != 0, FALSE);
465
466   if (!keymap)
467     {
468       keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
469       GDK_NOTE (MULTIHEAD,
470                 g_message ("_multihead : reverting to default display keymap "
471                            "in gdk_keymap_get_entries_for_keyval\n"));
472     }
473
474   keymap_x11 = GDK_KEYMAP_X11 (keymap);
475   
476   retval = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
477
478 #ifdef HAVE_XKB
479   if (KEYMAP_USE_XKB (keymap))
480     {
481       /* See sec 15.3.4 in XKB docs */
482
483       XkbDescRec *xkb = get_xkb (keymap_x11);
484       gint keycode;
485       
486       keycode = keymap_x11->min_keycode;
487
488       while (keycode <= keymap_x11->max_keycode)
489         {
490           gint max_shift_levels = XkbKeyGroupsWidth (xkb, keycode); /* "key width" */
491           gint group = 0;
492           gint level = 0;
493           gint total_syms = XkbKeyNumSyms (xkb, keycode);
494           gint i = 0;
495           KeySym *entry;
496
497           /* entry is an array with all syms for group 0, all
498            * syms for group 1, etc. and for each group the
499            * shift level syms are in order
500            */
501           entry = XkbKeySymsPtr (xkb, keycode);
502
503           while (i < total_syms)
504             {
505               /* check out our cool loop invariant */
506               g_assert (i == (group * max_shift_levels + level));
507
508               if (entry[i] == keyval)
509                 {
510                   /* Found a match */
511                   GdkKeymapKey key;
512
513                   key.keycode = keycode;
514                   key.group = group;
515                   key.level = level;
516
517                   g_array_append_val (retval, key);
518
519                   g_assert (XkbKeySymEntry (xkb, keycode, level, group) == 
520                             keyval);
521                 }
522
523               ++level;
524
525               if (level == max_shift_levels)
526                 {
527                   level = 0;
528                   ++group;
529                 }
530
531               ++i;
532             }
533
534           ++keycode;
535         }
536     }
537   else
538 #endif
539     {
540       const KeySym *map = get_keymap (keymap_x11);
541       gint keycode;
542       
543       keycode = keymap_x11->min_keycode;
544       while (keycode <= keymap_x11->max_keycode)
545         {
546           const KeySym *syms = map + (keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
547           gint i = 0;
548
549           while (i < keymap_x11->keysyms_per_keycode)
550             {
551               if (syms[i] == keyval)
552                 {
553                   /* found a match */
554                   GdkKeymapKey key;
555
556                   key.keycode = keycode;
557
558                   /* The "classic" non-XKB keymap has 2 levels per group */
559                   key.group = i / 2;
560                   key.level = i % 2;
561
562                   g_array_append_val (retval, key);
563                 }
564               
565               ++i;
566             }
567           
568           ++keycode;
569         }
570     }
571
572   if (retval->len > 0)
573     {
574       *keys = (GdkKeymapKey*) retval->data;
575       *n_keys = retval->len;
576     }
577   else
578     {
579       *keys = NULL;
580       *n_keys = 0;
581     }
582       
583   g_array_free (retval, retval->len > 0 ? FALSE : TRUE);
584
585   return *n_keys > 0;
586 }
587
588 /**
589  * gdk_keymap_get_entries_for_keycode:
590  * @keymap: a #GdkKeymap or %NULL to use the default keymap
591  * @hardware_keycode: a keycode
592  * @keys: return location for array of #GdkKeymapKey, or NULL
593  * @keyvals: return location for array of keyvals, or NULL
594  * @n_entries: length of @keys and @keyvals
595  *
596  * Returns the keyvals bound to @hardware_keycode.
597  * The Nth #GdkKeymapKey in @keys is bound to the Nth
598  * keyval in @keyvals. Free the returned arrays with g_free().
599  * When a keycode is pressed by the user, the keyval from
600  * this list of entries is selected by considering the effective
601  * keyboard group and level. See gdk_keymap_translate_keyboard_state().
602  *
603  * Returns: %TRUE if there were any entries
604  **/
605 gboolean
606 gdk_keymap_get_entries_for_keycode (GdkKeymap     *keymap,
607                                     guint          hardware_keycode,
608                                     GdkKeymapKey **keys,
609                                     guint        **keyvals,
610                                     gint          *n_entries)
611 {
612   GdkKeymapX11 *keymap_x11;
613   
614   GArray *key_array;
615   GArray *keyval_array;
616
617   g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
618   g_return_val_if_fail (n_entries != NULL, FALSE);
619
620   if (!keymap)
621     {
622       keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
623       GDK_NOTE (MULTIHEAD,
624                 g_message ("_multihead : reverting to default display keymap "
625                            "in gdk_keymap_get_entries_for_keycode\n"));
626     }
627
628   keymap_x11 = GDK_KEYMAP_X11 (keymap);
629
630   update_keyrange (keymap_x11);
631
632   if (hardware_keycode < keymap_x11->min_keycode ||
633       hardware_keycode > keymap_x11->max_keycode)
634     {
635       if (keys)
636         *keys = NULL;
637       if (keyvals)
638         *keyvals = NULL;
639
640       *n_entries = 0;
641       return FALSE;
642     }
643   
644   if (keys)
645     key_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
646   else
647     key_array = NULL;
648   
649   if (keyvals)
650     keyval_array = g_array_new (FALSE, FALSE, sizeof (guint));
651   else
652     keyval_array = NULL;
653   
654 #ifdef HAVE_XKB
655   if (KEYMAP_USE_XKB (keymap))
656     {
657       /* See sec 15.3.4 in XKB docs */
658
659       XkbDescRec *xkb = get_xkb (keymap_x11);
660       gint max_shift_levels;
661       gint group = 0;
662       gint level = 0;
663       gint total_syms;
664       gint i = 0;
665       KeySym *entry;
666       
667       max_shift_levels = XkbKeyGroupsWidth (xkb, hardware_keycode); /* "key width" */
668       total_syms = XkbKeyNumSyms (xkb, hardware_keycode);
669
670       /* entry is an array with all syms for group 0, all
671        * syms for group 1, etc. and for each group the
672        * shift level syms are in order
673        */
674       entry = XkbKeySymsPtr (xkb, hardware_keycode);
675
676       while (i < total_syms)
677         {          
678           /* check out our cool loop invariant */          
679           g_assert (i == (group * max_shift_levels + level));
680
681           if (key_array)
682             {
683               GdkKeymapKey key;
684               
685               key.keycode = hardware_keycode;
686               key.group = group;
687               key.level = level;
688               
689               g_array_append_val (key_array, key);
690             }
691
692           if (keyval_array)
693             g_array_append_val (keyval_array, entry[i]);
694           
695           ++level;
696           
697           if (level == max_shift_levels)
698             {
699               level = 0;
700               ++group;
701             }
702           
703           ++i;
704         }
705     }
706   else
707 #endif
708     {
709       const KeySym *map = get_keymap (keymap_x11);
710       const KeySym *syms;
711       gint i = 0;
712
713       syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
714
715       while (i < keymap_x11->keysyms_per_keycode)
716         {
717           if (key_array)
718             {
719               GdkKeymapKey key;
720           
721               key.keycode = hardware_keycode;
722               
723               /* The "classic" non-XKB keymap has 2 levels per group */
724               key.group = i / 2;
725               key.level = i % 2;
726               
727               g_array_append_val (key_array, key);
728             }
729
730           if (keyval_array)
731             g_array_append_val (keyval_array, syms[i]);
732           
733           ++i;
734         }
735     }
736   
737   if ((key_array && key_array->len > 0) ||
738       (keyval_array && keyval_array->len > 0))
739     {
740       if (keys)
741         *keys = (GdkKeymapKey*) key_array->data;
742
743       if (keyvals)
744         *keyvals = (guint*) keyval_array->data;
745
746       if (key_array)
747         *n_entries = key_array->len;
748       else
749         *n_entries = keyval_array->len;
750     }
751   else
752     {
753       if (keys)
754         *keys = NULL;
755
756       if (keyvals)
757         *keyvals = NULL;
758       
759       *n_entries = 0;
760     }
761
762   if (key_array)
763     g_array_free (key_array, key_array->len > 0 ? FALSE : TRUE);
764   if (keyval_array)
765     g_array_free (keyval_array, keyval_array->len > 0 ? FALSE : TRUE);
766
767   return *n_entries > 0;
768 }
769
770
771 /**
772  * gdk_keymap_lookup_key:
773  * @keymap: a #GdkKeymap or %NULL to use the default keymap
774  * @key: a #GdkKeymapKey with keycode, group, and level initialized
775  * 
776  * Looks up the keyval mapped to a keycode/group/level triplet.
777  * If no keyval is bound to @key, returns 0. For normal user input,
778  * you want to use gdk_keymap_translate_keyboard_state() instead of
779  * this function, since the effective group/level may not be
780  * the same as the current keyboard state.
781  * 
782  * Return value: a keyval, or 0 if none was mapped to the given @key
783  **/
784 guint
785 gdk_keymap_lookup_key (GdkKeymap          *keymap,
786                        const GdkKeymapKey *key)
787 {
788   GdkKeymapX11 *keymap_x11;
789   
790   g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), 0);
791   g_return_val_if_fail (key != NULL, 0);
792   g_return_val_if_fail (key->group < 4, 0);
793   
794   if (!keymap)
795     {
796       keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
797       GDK_NOTE (MULTIHEAD,
798                 g_message ("_multihead : reverting to default display keymap "
799                            "in gdk_keymap_lookup_key\n"));
800     }
801
802   keymap_x11 = GDK_KEYMAP_X11 (keymap);
803   
804 #ifdef HAVE_XKB
805   if (KEYMAP_USE_XKB (keymap))
806     {
807       XkbDescRec *xkb = get_xkb (keymap_x11);
808       
809       return XkbKeySymEntry (xkb, key->keycode, key->level, key->group);
810     }
811   else
812 #endif
813     {
814       const KeySym *map = get_keymap (keymap_x11);
815       const KeySym *syms = map + (key->keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
816       return syms [KEYSYM_INDEX (keymap_x11, key->group, key->level)];
817     }
818 }
819
820 #ifdef HAVE_XKB
821 /* This is copied straight from XFree86 Xlib, because I needed to
822  * add the group and level return. It's unchanged for ease of
823  * diff against the Xlib sources; don't reformat it.
824  */
825 static Bool
826 MyEnhancedXkbTranslateKeyCode(register XkbDescPtr     xkb,
827                               KeyCode                 key,
828                               register unsigned int   mods,
829                               unsigned int *          mods_rtrn,
830                               KeySym *                keysym_rtrn,
831                               unsigned int *          group_rtrn,
832                               unsigned int *          level_rtrn)
833 {
834     XkbKeyTypeRec *type;
835     int col,nKeyGroups;
836     unsigned preserve,effectiveGroup;
837     KeySym *syms;
838
839     if (mods_rtrn!=NULL)
840         *mods_rtrn = 0;
841
842     nKeyGroups= XkbKeyNumGroups(xkb,key);
843     if ((!XkbKeycodeInRange(xkb,key))||(nKeyGroups==0)) {
844         if (keysym_rtrn!=NULL)
845             *keysym_rtrn = NoSymbol;
846         return False;
847     }
848
849     syms = XkbKeySymsPtr(xkb,key);
850
851     /* find the offset of the effective group */
852     col = 0;
853     effectiveGroup= XkbGroupForCoreState(mods);
854     if ( effectiveGroup>=nKeyGroups ) {
855         unsigned groupInfo= XkbKeyGroupInfo(xkb,key);
856         switch (XkbOutOfRangeGroupAction(groupInfo)) {
857             default:
858                 effectiveGroup %= nKeyGroups;
859                 break;
860             case XkbClampIntoRange:
861                 effectiveGroup = nKeyGroups-1;
862                 break;
863             case XkbRedirectIntoRange:
864                 effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
865                 if (effectiveGroup>=nKeyGroups)
866                     effectiveGroup= 0;
867                 break;
868         }
869     }
870     col= effectiveGroup*XkbKeyGroupsWidth(xkb,key);
871     type = XkbKeyKeyType(xkb,key,effectiveGroup);
872
873     preserve= 0;
874     if (type->map) { /* find the column (shift level) within the group */
875         register int i;
876         register XkbKTMapEntryPtr entry;
877         for (i=0,entry=type->map;i<type->map_count;i++,entry++) {
878             if ((entry->active)&&((mods&type->mods.mask)==entry->mods.mask)) {
879                 col+= entry->level;
880                 if (type->preserve)
881                     preserve= type->preserve[i].mask;
882
883                 /* ---- Begin stuff GDK adds to the original Xlib version ---- */
884                 
885                 if (level_rtrn)
886                   *level_rtrn = entry->level;
887                 
888                 /* ---- End stuff GDK adds to the original Xlib version ---- */
889                 
890                 break;
891             }
892         }
893     }
894
895     if (keysym_rtrn!=NULL)
896         *keysym_rtrn= syms[col];
897     if (mods_rtrn) {
898         *mods_rtrn= type->mods.mask&(~preserve);
899
900         /* ---- Begin stuff GDK comments out of the original Xlib version ---- */
901         /* This is commented out because xkb_info is a private struct */
902
903 #if 0
904         /* The Motif VTS doesn't get the help callback called if help
905          * is bound to Shift+<whatever>, and it appears as though it 
906          * is XkbTranslateKeyCode that is causing the problem.  The 
907          * core X version of XTranslateKey always OR's in ShiftMask 
908          * and LockMask for mods_rtrn, so this "fix" keeps this behavior 
909          * and solves the VTS problem.
910          */
911         if ((xkb->dpy)&&(xkb->dpy->xkb_info)&&
912             (xkb->dpy->xkb_info->xlib_ctrls&XkbLC_AlwaysConsumeShiftAndLock)) {            *mods_rtrn|= (ShiftMask|LockMask);
913         }
914 #endif
915         
916         /* ---- End stuff GDK comments out of the original Xlib version ---- */
917     }
918
919     /* ---- Begin stuff GDK adds to the original Xlib version ---- */
920
921     if (group_rtrn)
922       *group_rtrn = effectiveGroup;
923     
924     /* ---- End stuff GDK adds to the original Xlib version ---- */
925     
926     return (syms[col]!=NoSymbol);
927 }
928 #endif /* HAVE_XKB */
929
930 /**
931  * gdk_keymap_translate_keyboard_state:
932  * @keymap: a #GdkKeymap, or %NULL to use the default
933  * @hardware_keycode: a keycode
934  * @state: a modifier state 
935  * @group: active keyboard group
936  * @keyval: return location for keyval
937  * @effective_group: return location for effective group
938  * @level: return location for level
939  * @consumed_modifiers: return location for modifiers that were used to determine the group or level
940  * 
941  *
942  * Translates the contents of a #GdkEventKey into a keyval, effective
943  * group, and level. Modifiers that affected the translation and
944  * are thus unavailable for application use are returned in
945  * @consumed_modifiers.  See gdk_keyval_get_keys() for an explanation of
946  * groups and levels.  The @effective_group is the group that was
947  * actually used for the translation; some keys such as Enter are not
948  * affected by the active keyboard group. The @level is derived from
949  * @state. For convenience, #GdkEventKey already contains the translated
950  * keyval, so this function isn't as useful as you might think.
951  * 
952  * Return value: %TRUE if there was a keyval bound to the keycode/state/group
953  **/
954 gboolean
955 gdk_keymap_translate_keyboard_state (GdkKeymap       *keymap,
956                                      guint            hardware_keycode,
957                                      GdkModifierType  state,
958                                      gint             group,
959                                      guint           *keyval,
960                                      gint            *effective_group,
961                                      gint            *level,
962                                      GdkModifierType *consumed_modifiers)
963 {
964   GdkKeymapX11 *keymap_x11;
965   KeySym tmp_keyval = NoSymbol;
966   guint tmp_modifiers;
967
968   g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
969   g_return_val_if_fail (group < 4, FALSE);
970   
971   keymap_x11 = GDK_KEYMAP_X11 (keymap);
972
973   if (keyval)
974     *keyval = NoSymbol;
975   if (effective_group)
976     *effective_group = 0;
977   if (level)
978     *level = 0;
979   if (consumed_modifiers)
980     *consumed_modifiers = 0;
981
982   update_keyrange (keymap_x11);
983   
984   if (hardware_keycode < keymap_x11->min_keycode ||
985       hardware_keycode > keymap_x11->max_keycode)
986     return FALSE;
987   
988 #ifdef HAVE_XKB
989   if (KEYMAP_USE_XKB (keymap))
990     {
991       XkbDescRec *xkb = get_xkb (keymap_x11);
992
993       /* replace bits 13 and 14 with the provided group */
994       state &= ~(1 << 13 | 1 << 14);
995       state |= group << 13;
996       
997       MyEnhancedXkbTranslateKeyCode (xkb,
998                                      hardware_keycode,
999                                      state,
1000                                      &tmp_modifiers,
1001                                      &tmp_keyval,
1002                                      effective_group,
1003                                      level);
1004
1005       if (state & ~tmp_modifiers & LockMask)
1006         tmp_keyval = gdk_keyval_to_upper (tmp_keyval);
1007
1008       /* We need to augment the consumed modifiers with LockMask, since
1009        * we handle that ourselves, and also with the group bits
1010        */
1011       tmp_modifiers |= LockMask | 1 << 13 | 1 << 14;
1012     }
1013   else
1014 #endif
1015     {
1016       const KeySym *map = get_keymap (keymap_x11);
1017       const KeySym *syms;
1018       gint shift_level;
1019       gboolean ignore_shift = FALSE;
1020       gboolean ignore_group = FALSE;
1021       
1022       if ((state & GDK_SHIFT_MASK) &&
1023           (state & GDK_LOCK_MASK))
1024         shift_level = 0; /* shift disables shift lock */
1025       else if ((state & GDK_SHIFT_MASK) ||
1026                (state & GDK_LOCK_MASK))
1027         shift_level = 1;
1028       else
1029         shift_level = 0;
1030
1031       syms = map + (hardware_keycode - keymap_x11->min_keycode) * keymap_x11->keysyms_per_keycode;
1032
1033 #define SYM(k,g,l) syms[KEYSYM_INDEX (k,g,l)]
1034
1035       /* Drop group and shift if there are no keysymbols on
1036        * the specified key.
1037        */
1038       if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, group, 0))
1039         {
1040           shift_level = 0;
1041           ignore_shift = TRUE;
1042         }
1043       if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, 0, shift_level))
1044         {
1045           group = 0;
1046           ignore_group = TRUE;
1047         }
1048       if (!SYM (keymap_x11, group, shift_level) && SYM (keymap_x11, 0, 0))
1049         {
1050           shift_level = 0;
1051           group = 0;
1052           ignore_group = TRUE;
1053           ignore_shift = TRUE;
1054         }
1055
1056       /* See whether the group and shift level actually mattered
1057        * to know what to put in consumed_modifiers
1058        */
1059       if (!SYM (keymap_x11, group, 1) ||
1060           SYM (keymap_x11, group, 0) == SYM (keymap_x11, group, 1))
1061         ignore_shift = TRUE;
1062
1063       if (!SYM (keymap_x11, 1, shift_level) ||
1064           SYM (keymap_x11, 0, shift_level) == SYM (keymap_x11, 1, shift_level))
1065         ignore_group = TRUE;
1066
1067       tmp_keyval = SYM (keymap_x11, group, shift_level);
1068
1069       tmp_modifiers = ignore_group ? 0 : keymap_x11->group_switch_mask;
1070       tmp_modifiers |= ignore_shift ? 0 : (GDK_SHIFT_MASK | GDK_LOCK_MASK);
1071
1072       if (effective_group)
1073         *effective_group = group;
1074
1075       if (level)
1076         *level = shift_level;
1077 #undef SYM        
1078     }
1079
1080   if (consumed_modifiers)
1081     *consumed_modifiers = tmp_modifiers;
1082                                 
1083   if (keyval)
1084     *keyval = tmp_keyval;
1085
1086   return tmp_keyval != NoSymbol;
1087 }
1088
1089
1090 /* Key handling not part of the keymap */
1091
1092 gchar*
1093 gdk_keyval_name (guint        keyval)
1094 {
1095   switch (keyval)
1096     {
1097     case GDK_Page_Up:
1098       return "Page_Up";
1099     case GDK_Page_Down:
1100       return "Page_Down";
1101     case GDK_KP_Page_Up:
1102       return "KP_Page_Up";
1103     case GDK_KP_Page_Down:
1104       return "KP_Page_Down";
1105     }
1106   
1107   return XKeysymToString (keyval);
1108 }
1109
1110 guint
1111 gdk_keyval_from_name (const gchar *keyval_name)
1112 {
1113   g_return_val_if_fail (keyval_name != NULL, 0);
1114   
1115   return XStringToKeysym (keyval_name);
1116 }
1117
1118 #ifdef HAVE_XCONVERTCASE
1119 void
1120 gdk_keyval_convert_case (guint symbol,
1121                          guint *lower,
1122                          guint *upper)
1123 {
1124   KeySym xlower = 0;
1125   KeySym xupper = 0;
1126
1127   /* Check for directly encoded 24-bit UCS characters: */
1128   if ((symbol & 0xff000000) == 0x01000000)
1129     {
1130       if (lower)
1131         *lower = gdk_unicode_to_keyval (g_unichar_tolower (symbol & 0x00ffffff));
1132       if (upper)
1133         *upper = gdk_unicode_to_keyval (g_unichar_toupper (symbol & 0x00ffffff));
1134       return;
1135     }
1136   
1137   if (symbol)
1138     XConvertCase (symbol, &xlower, &xupper);
1139
1140   if (lower)
1141     *lower = xlower;
1142   if (upper)
1143     *upper = xupper;
1144 }  
1145 #endif /* HAVE_XCONVERTCASE */
1146
1147 gint
1148 _gdk_x11_get_group_for_state (GdkDisplay      *display,
1149                               GdkModifierType  state)
1150 {
1151   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1152   
1153 #ifdef HAVE_XKB
1154   if (display_x11->use_xkb)
1155     {
1156       return XkbGroupForCoreState (state);
1157     }
1158   else
1159 #endif
1160     {
1161       GdkKeymapX11 *keymap_impl = GDK_KEYMAP_X11 (gdk_keymap_get_for_display (display));
1162       update_keymaps (keymap_impl);
1163       return (state & keymap_impl->group_switch_mask) ? 1 : 0;
1164     }
1165 }