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