]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkkeys-x11.c
Throughout: assorted docs
[~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 <ctype.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <limits.h>
33 #include <errno.h>
34
35 #include "gdk.h"
36
37 #include "gdkprivate-x11.h"
38 #include "gdkinternals.h"
39 #include "gdkkeysyms.h"
40
41 #include "config.h"
42
43 guint _gdk_keymap_serial = 0;
44
45 static gint min_keycode = 0;
46 static gint max_keycode = 0;
47
48 static inline void
49 update_keyrange (void)
50 {
51   if (max_keycode == 0)
52     XDisplayKeycodes (gdk_display, &min_keycode, &max_keycode);
53 }
54
55 #ifdef HAVE_XKB
56 #include <X11/XKBlib.h>
57
58 gboolean _gdk_use_xkb = FALSE;
59 gint _gdk_xkb_event_type;
60 static XkbDescPtr xkb_desc = NULL;
61
62 static XkbDescPtr
63 get_xkb (void)
64 {
65   static guint current_serial = 0;
66
67   update_keyrange ();
68   
69   if (xkb_desc == NULL)
70     {
71       xkb_desc = XkbGetMap (gdk_display, XkbKeySymsMask, XkbUseCoreKbd);
72       if (xkb_desc == NULL)
73         g_error ("Failed to get keymap");
74
75       XkbGetNames (gdk_display, XkbGroupNamesMask, xkb_desc);
76     }
77   else if (current_serial != _gdk_keymap_serial)
78     {
79       XkbGetUpdatedMap (gdk_display, XkbKeySymsMask, xkb_desc);
80       XkbGetNames (gdk_display, XkbGroupNamesMask, xkb_desc);
81     }
82
83   current_serial = _gdk_keymap_serial;
84
85   return xkb_desc;
86 }
87 #endif /* HAVE_XKB */
88
89 /* Whether we were able to turn on detectable-autorepeat using
90  * XkbSetDetectableAutorepeat. If FALSE, we'll fall back
91  * to checking the next event with XPending().
92  */
93 gboolean _gdk_have_xkb_autorepeat = FALSE;
94
95 static KeySym* keymap = NULL;
96 static gint keysyms_per_keycode = 0;
97 static XModifierKeymap* mod_keymap = NULL;
98 static GdkModifierType group_switch_mask = 0;
99 static PangoDirection current_direction;
100 static gboolean       have_direction = FALSE;
101 static GdkKeymap *default_keymap = NULL;
102
103 /**
104  * gdk_keymap_get_default:
105  * 
106  * Gets the #GdkKeymap for the default display.
107  * 
108  * Return value: the default keymap
109  **/
110 GdkKeymap*
111 gdk_keymap_get_default (void)
112 {
113   if (default_keymap == NULL)
114     default_keymap = g_object_new (gdk_keymap_get_type (), NULL);
115
116   return default_keymap;
117 }
118
119 static void
120 update_keymaps (void)
121 {
122   static guint current_serial = 0;
123   
124 #ifdef HAVE_XKB
125   g_assert (!_gdk_use_xkb);
126 #endif
127   
128   if (keymap == NULL ||
129       current_serial != _gdk_keymap_serial)
130     {
131       gint i;
132       gint map_size;
133
134       current_serial = _gdk_keymap_serial;
135
136       update_keyrange ();
137       
138       if (keymap)
139         XFree (keymap);
140
141       if (mod_keymap)
142         XFreeModifiermap (mod_keymap);
143       
144       keymap = XGetKeyboardMapping (gdk_display, min_keycode,
145                                     max_keycode - min_keycode,
146                                     &keysyms_per_keycode);
147
148       mod_keymap = XGetModifierMapping (gdk_display);
149
150
151       group_switch_mask = 0;
152
153       /* there are 8 modifiers, and the first 3 are shift, shift lock,
154        * and control
155        */
156       map_size = 8 * mod_keymap->max_keypermod;
157       i = 3 * mod_keymap->max_keypermod;
158       while (i < map_size)
159         {
160           /* get the key code at this point in the map,
161            * see if its keysym is GDK_Mode_switch, if so
162            * we have the mode key
163            */
164           gint keycode = mod_keymap->modifiermap[i];
165       
166           if (keycode >= min_keycode &&
167               keycode <= max_keycode)
168             {
169               gint j = 0;
170               KeySym *syms = keymap + (keycode - min_keycode) * keysyms_per_keycode;
171               while (j < keysyms_per_keycode)
172                 {
173                   if (syms[j] == GDK_Mode_switch)
174                     {
175                       /* This modifier swaps groups */
176
177                       /* GDK_MOD1_MASK is 1 << 3 for example, i.e. the
178                        * fourth modifier, i / keyspermod is the modifier
179                        * index
180                        */
181                   
182                       group_switch_mask |= (1 << ( i / mod_keymap->max_keypermod));
183                       break;
184                     }
185               
186                   ++j;
187                 }
188             }
189       
190           ++i;
191         }
192     }
193 }
194
195 static const KeySym*
196 get_keymap (void)
197 {
198   update_keymaps ();  
199   
200   return keymap;
201 }
202
203 #if HAVE_XKB
204 static PangoDirection
205 get_direction (void)
206 {
207   XkbDescRec *xkb = get_xkb ();
208   char *name;
209   XkbStateRec state_rec;
210   PangoDirection result;
211
212   XkbGetState (gdk_display, XkbUseCoreKbd, &state_rec);
213
214   if (xkb->names->groups[state_rec.locked_group] == None)
215     result = PANGO_DIRECTION_LTR;
216   else
217     {
218       name = gdk_atom_name (xkb->names->groups[state_rec.locked_group]);
219       if (g_strcasecmp (name, "arabic") == 0 ||
220           g_strcasecmp (name, "hebrew") == 0 ||
221           g_strcasecmp (name, "israelian") == 0)
222         result = PANGO_DIRECTION_RTL;
223       else
224         result = PANGO_DIRECTION_LTR;
225       g_free (name);
226     }
227     
228   return result;
229 }
230
231 void
232 _gdk_keymap_state_changed (void)
233 {
234   if (default_keymap)
235     {
236       PangoDirection new_direction = get_direction ();
237       
238       if (!have_direction || new_direction != current_direction)
239         {
240           have_direction = TRUE;
241           current_direction = new_direction;
242           g_signal_emit_by_name (G_OBJECT (default_keymap), "direction_changed");
243         }
244     }
245 }
246 #endif /* HAVE_XKB */
247   
248 PangoDirection
249 gdk_keymap_get_direction (GdkKeymap *keymap)
250 {
251 #if HAVE_XKB
252   if (_gdk_use_xkb)
253     {
254       if (!have_direction)
255         {
256           current_direction = get_direction ();
257           have_direction = TRUE;
258         }
259   
260       return current_direction;
261     }
262   else
263 #endif /* HAVE_XKB */
264     return PANGO_DIRECTION_LTR;
265 }
266
267 /**
268  * gdk_keymap_get_entries_for_keyval:
269  * @keymap: a #GdkKeymap, or %NULL to use the default keymap
270  * @keyval: a keyval, such as %GDK_a, %GDK_Up, %GDK_Return, etc.
271  * @keys: return location for an array of #GdkKeymapKey
272  * @n_keys: return location for number of elements in returned array
273  * 
274  * Obtains a list of keycode/group/level combinations that will
275  * generate @keyval. Groups and levels are two kinds of keyboard mode;
276  * in general, the level determines whether the top or bottom symbol
277  * on a key is used, and the group determines whether the left or
278  * right symbol is used. On US keyboards, the shift key changes the
279  * keyboard level, and there are no groups. A group switch key might
280  * convert a keyboard between Hebrew to English modes, for example.
281  * #GdkEventKey contains a %group field that indicates the active
282  * keyboard group. The level is computed from the modifier mask.
283  * The returned array should be freed
284  * with g_free().
285  *
286  * Return value: %TRUE if keys were found and returned
287  **/
288 gboolean
289 gdk_keymap_get_entries_for_keyval (GdkKeymap     *keymap,
290                                    guint          keyval,
291                                    GdkKeymapKey **keys,
292                                    gint          *n_keys)
293 {
294   GArray *retval;
295
296   g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
297   g_return_val_if_fail (keys != NULL, FALSE);
298   g_return_val_if_fail (n_keys != NULL, FALSE);
299   g_return_val_if_fail (keyval != 0, FALSE);
300   
301   retval = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
302
303 #ifdef HAVE_XKB
304   if (_gdk_use_xkb)
305     {
306       /* See sec 15.3.4 in XKB docs */
307
308       XkbDescRec *xkb = get_xkb ();
309       gint keycode;
310       
311       keycode = min_keycode;
312
313       while (keycode <= max_keycode)
314         {
315           gint max_shift_levels = XkbKeyGroupsWidth (xkb, keycode); /* "key width" */
316           gint group = 0;
317           gint level = 0;
318           gint total_syms = XkbKeyNumSyms (xkb, keycode);
319           gint i = 0;
320           KeySym *entry;
321
322           /* entry is an array with all syms for group 0, all
323            * syms for group 1, etc. and for each group the
324            * shift level syms are in order
325            */
326           entry = XkbKeySymsPtr (xkb, keycode);
327
328           while (i < total_syms)
329             {
330               /* check out our cool loop invariant */
331               g_assert (i == (group * max_shift_levels + level));
332
333               if (entry[i] == keyval)
334                 {
335                   /* Found a match */
336                   GdkKeymapKey key;
337
338                   key.keycode = keycode;
339                   key.group = group;
340                   key.level = level;
341
342                   g_array_append_val (retval, key);
343
344                   g_assert (XkbKeySymEntry (xkb, keycode, level, group) == keyval);
345                 }
346
347               ++level;
348
349               if (level == max_shift_levels)
350                 {
351                   level = 0;
352                   ++group;
353                 }
354
355               ++i;
356             }
357
358           ++keycode;
359         }
360     }
361   else
362 #endif
363     {
364       const KeySym *map = get_keymap ();
365       gint keycode;
366       
367       keycode = min_keycode;
368       while (keycode < max_keycode)
369         {
370           const KeySym *syms = map + (keycode - min_keycode) * keysyms_per_keycode;
371           gint i = 0;
372
373           while (i < keysyms_per_keycode)
374             {
375               if (syms[i] == keyval)
376                 {
377                   /* found a match */
378                   GdkKeymapKey key;
379
380                   key.keycode = keycode;
381
382                   /* The "classic" non-XKB keymap has 2 levels per group */
383                   key.group = i / 2;
384                   key.level = i % 2;
385
386                   g_array_append_val (retval, key);
387                 }
388               
389               ++i;
390             }
391           
392           ++keycode;
393         }
394     }
395
396   if (retval->len > 0)
397     {
398       *keys = (GdkKeymapKey*) retval->data;
399       *n_keys = retval->len;
400     }
401   else
402     {
403       *keys = NULL;
404       *n_keys = 0;
405     }
406       
407   g_array_free (retval, retval->len > 0 ? FALSE : TRUE);
408
409   return *n_keys > 0;
410 }
411
412 /**
413  * gdk_keymap_get_entries_for_keycode:
414  * @keymap: a #GdkKeymap or %NULL to use the default keymap
415  * @hardware_keycode: a keycode
416  * @keys: return location for array of #GdkKeymapKey, or NULL
417  * @keyvals: return location for array of keyvals, or NULL
418  * @n_entries: length of @keys and @keyvals
419  *
420  * Returns the keyvals bound to @hardware_keycode.
421  * The Nth #GdkKeymapKey in @keys is bound to the Nth
422  * keyval in @keyvals. Free the returned arrays with g_free().
423  * When a keycode is pressed by the user, the keyval from
424  * this list of entries is selected by considering the effective
425  * keyboard group and level. See gdk_keymap_translate_keyboard_state().
426  *
427  * Returns: %TRUE if there were any entries
428  **/
429 gboolean
430 gdk_keymap_get_entries_for_keycode (GdkKeymap     *keymap,
431                                     guint          hardware_keycode,
432                                     GdkKeymapKey **keys,
433                                     guint        **keyvals,
434                                     gint          *n_entries)
435 {
436   GArray *key_array;
437   GArray *keyval_array;
438
439   g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
440   g_return_val_if_fail (n_entries != NULL, FALSE);
441
442   update_keyrange ();
443
444   if (hardware_keycode < min_keycode ||
445       hardware_keycode > max_keycode)
446     {
447       if (keys)
448         *keys = NULL;
449       if (keyvals)
450         *keyvals = NULL;
451
452       *n_entries = 0;
453       return FALSE;
454     }
455   
456   if (keys)
457     key_array = g_array_new (FALSE, FALSE, sizeof (GdkKeymapKey));
458   else
459     key_array = NULL;
460   
461   if (keyvals)
462     keyval_array = g_array_new (FALSE, FALSE, sizeof (guint));
463   else
464     keyval_array = NULL;
465   
466 #ifdef HAVE_XKB
467   if (_gdk_use_xkb)
468     {
469       /* See sec 15.3.4 in XKB docs */
470
471       XkbDescRec *xkb = get_xkb ();
472       gint max_shift_levels;
473       gint group = 0;
474       gint level = 0;
475       gint total_syms;
476       gint i = 0;
477       KeySym *entry;
478       
479       max_shift_levels = XkbKeyGroupsWidth (xkb, hardware_keycode); /* "key width" */
480       total_syms = XkbKeyNumSyms (xkb, hardware_keycode);
481
482       /* entry is an array with all syms for group 0, all
483        * syms for group 1, etc. and for each group the
484        * shift level syms are in order
485        */
486       entry = XkbKeySymsPtr (xkb, hardware_keycode);
487
488       while (i < total_syms)
489         {          
490           /* check out our cool loop invariant */          
491           g_assert (i == (group * max_shift_levels + level));
492
493           if (key_array)
494             {
495               GdkKeymapKey key;
496               
497               key.keycode = hardware_keycode;
498               key.group = group;
499               key.level = level;
500               
501               g_array_append_val (key_array, key);
502             }
503
504           if (keyval_array)
505             g_array_append_val (keyval_array, entry[i]);
506           
507           ++level;
508           
509           if (level == max_shift_levels)
510             {
511               level = 0;
512               ++group;
513             }
514           
515           ++i;
516         }
517     }
518   else
519 #endif
520     {
521       const KeySym *map = get_keymap ();
522       const KeySym *syms;
523       gint i = 0;
524
525       syms = map + (hardware_keycode - min_keycode) * keysyms_per_keycode;
526
527       while (i < keysyms_per_keycode)
528         {
529           if (key_array)
530             {
531               GdkKeymapKey key;
532           
533               key.keycode = hardware_keycode;
534               
535               /* The "classic" non-XKB keymap has 2 levels per group */
536               key.group = i / 2;
537               key.level = i % 2;
538               
539               g_array_append_val (key_array, key);
540             }
541
542           if (keyval_array)
543             g_array_append_val (keyval_array, syms[i]);
544           
545           ++i;
546         }
547     }
548   
549   if ((key_array && key_array->len > 0) ||
550       (keyval_array && keyval_array->len > 0))
551     {
552       if (keys)
553         *keys = (GdkKeymapKey*) key_array->data;
554
555       if (keyvals)
556         *keyvals = (guint*) keyval_array->data;
557
558       if (key_array)
559         *n_entries = key_array->len;
560       else
561         *n_entries = keyval_array->len;
562     }
563   else
564     {
565       if (keys)
566         *keys = NULL;
567
568       if (keyvals)
569         *keyvals = NULL;
570       
571       *n_entries = 0;
572     }
573
574   if (key_array)
575     g_array_free (key_array, key_array->len > 0 ? FALSE : TRUE);
576   if (keyval_array)
577     g_array_free (keyval_array, keyval_array->len > 0 ? FALSE : TRUE);
578
579   return *n_entries > 0;
580 }
581
582
583 /**
584  * gdk_keymap_lookup_key:
585  * @keymap: a #GdkKeymap or %NULL to use the default keymap
586  * @key: a #GdkKeymapKey with keycode, group, and level initialized
587  * 
588  * Looks up the keyval mapped to a keycode/group/level triplet.
589  * If no keyval is bound to @key, returns 0. For normal user input,
590  * you want to use gdk_keymap_translate_keyboard_state() instead of
591  * this function, since the effective group/level may not be
592  * the same as the current keyboard state.
593  * 
594  * Return value: a keyval, or 0 if none was mapped to the given @key
595  **/
596 guint
597 gdk_keymap_lookup_key (GdkKeymap          *keymap,
598                        const GdkKeymapKey *key)
599 {
600   g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), 0);
601   g_return_val_if_fail (key != NULL, 0);
602   g_return_val_if_fail (key->group < 4, 0);
603   
604 #ifdef HAVE_XKB
605   if (_gdk_use_xkb)
606     {
607       XkbDescRec *xkb = get_xkb ();
608       
609       return XkbKeySymEntry (xkb, key->keycode, key->level, key->group);
610     }
611   else
612 #endif
613     {
614       update_keymaps ();
615       
616       return XKeycodeToKeysym (gdk_display, key->keycode,
617                                key->group * keysyms_per_keycode + key->level);
618     }
619 }
620
621 #ifdef HAVE_XKB
622 /* This is copied straight from XFree86 Xlib, because I needed to
623  * add the group and level return. It's unchanged for ease of
624  * diff against the Xlib sources; don't reformat it.
625  */
626 static Bool
627 MyEnhancedXkbTranslateKeyCode(register XkbDescPtr     xkb,
628                               KeyCode                 key,
629                               register unsigned int   mods,
630                               unsigned int *          mods_rtrn,
631                               KeySym *                keysym_rtrn,
632                               unsigned int *          group_rtrn,
633                               unsigned int *          level_rtrn)
634 {
635     XkbKeyTypeRec *type;
636     int col,nKeyGroups;
637     unsigned preserve,effectiveGroup;
638     KeySym *syms;
639
640     if (mods_rtrn!=NULL)
641         *mods_rtrn = 0;
642
643     nKeyGroups= XkbKeyNumGroups(xkb,key);
644     if ((!XkbKeycodeInRange(xkb,key))||(nKeyGroups==0)) {
645         if (keysym_rtrn!=NULL)
646             *keysym_rtrn = NoSymbol;
647         return False;
648     }
649
650     syms = XkbKeySymsPtr(xkb,key);
651
652     /* find the offset of the effective group */
653     col = 0;
654     effectiveGroup= XkbGroupForCoreState(mods);
655     if ( effectiveGroup>=nKeyGroups ) {
656         unsigned groupInfo= XkbKeyGroupInfo(xkb,key);
657         switch (XkbOutOfRangeGroupAction(groupInfo)) {
658             default:
659                 effectiveGroup %= nKeyGroups;
660                 break;
661             case XkbClampIntoRange:
662                 effectiveGroup = nKeyGroups-1;
663                 break;
664             case XkbRedirectIntoRange:
665                 effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo);
666                 if (effectiveGroup>=nKeyGroups)
667                     effectiveGroup= 0;
668                 break;
669         }
670     }
671     col= effectiveGroup*XkbKeyGroupsWidth(xkb,key);
672     type = XkbKeyKeyType(xkb,key,effectiveGroup);
673
674     preserve= 0;
675     if (type->map) { /* find the column (shift level) within the group */
676         register int i;
677         register XkbKTMapEntryPtr entry;
678         for (i=0,entry=type->map;i<type->map_count;i++,entry++) {
679             if ((entry->active)&&((mods&type->mods.mask)==entry->mods.mask)) {
680                 col+= entry->level;
681                 if (type->preserve)
682                     preserve= type->preserve[i].mask;
683
684                 /* ---- Begin stuff GDK adds to the original Xlib version ---- */
685                 
686                 if (level_rtrn)
687                   *level_rtrn = entry->level;
688                 
689                 /* ---- End stuff GDK adds to the original Xlib version ---- */
690                 
691                 break;
692             }
693         }
694     }
695
696     if (keysym_rtrn!=NULL)
697         *keysym_rtrn= syms[col];
698     if (mods_rtrn) {
699         *mods_rtrn= type->mods.mask&(~preserve);
700
701         /* ---- Begin stuff GDK comments out of the original Xlib version ---- */
702         /* This is commented out because xkb_info is a private struct */
703
704 #if 0
705         /* The Motif VTS doesn't get the help callback called if help
706          * is bound to Shift+<whatever>, and it appears as though it 
707          * is XkbTranslateKeyCode that is causing the problem.  The 
708          * core X version of XTranslateKey always OR's in ShiftMask 
709          * and LockMask for mods_rtrn, so this "fix" keeps this behavior 
710          * and solves the VTS problem.
711          */
712         if ((xkb->dpy)&&(xkb->dpy->xkb_info)&&
713             (xkb->dpy->xkb_info->xlib_ctrls&XkbLC_AlwaysConsumeShiftAndLock)) {            *mods_rtrn|= (ShiftMask|LockMask);
714         }
715 #endif
716         
717         /* ---- End stuff GDK comments out of the original Xlib version ---- */
718     }
719
720     /* ---- Begin stuff GDK adds to the original Xlib version ---- */
721
722     if (group_rtrn)
723       *group_rtrn = effectiveGroup;
724     
725     /* ---- End stuff GDK adds to the original Xlib version ---- */
726     
727     return (syms[col]!=NoSymbol);
728 }
729 #endif /* HAVE_XKB */
730
731 /**
732  * gdk_keymap_translate_keyboard_state:
733  * @keymap: a #GdkKeymap, or %NULL to use the default
734  * @hardware_keycode: a keycode
735  * @state: a modifier state 
736  * @group: active keyboard group
737  * @keyval: return location for keyval
738  * @effective_group: return location for effective group
739  * @level: return location for level
740  * @unused_modifiers: return location for modifiers that didn't affect the group or level
741  * 
742  *
743  * Translates the contents of a #GdkEventKey into a keyval, effective
744  * group, and level. Modifiers that didn't affect the translation and
745  * are thus available for application use are returned in
746  * @unused_modifiers.  See gdk_keyval_get_keys() for an explanation of
747  * groups and levels.  The @effective_group is the group that was
748  * actually used for the translation; some keys such as Enter are not
749  * affected by the active keyboard group. The @level is derived from
750  * @state. For convenience, #GdkEventKey already contains the translated
751  * keyval, so this function isn't as useful as you might think.
752  * 
753  * Return value: %TRUE if there was a keyval bound to the keycode/state/group
754  **/
755 gboolean
756 gdk_keymap_translate_keyboard_state (GdkKeymap       *keymap,
757                                      guint            hardware_keycode,
758                                      GdkModifierType  state,
759                                      gint             group,
760                                      guint           *keyval,
761                                      gint            *effective_group,
762                                      gint            *level,
763                                      GdkModifierType *unused_modifiers)
764 {
765   KeySym tmp_keyval = NoSymbol;
766
767   g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
768   g_return_val_if_fail (group < 4, FALSE);
769   
770   if (keyval)
771     *keyval = NoSymbol;
772   if (effective_group)
773     *effective_group = 0;
774   if (level)
775     *level = 0;
776   if (unused_modifiers)
777     *unused_modifiers = state;
778
779   update_keyrange ();
780   
781   if (hardware_keycode < min_keycode ||
782       hardware_keycode > max_keycode)
783     return FALSE;
784   
785 #ifdef HAVE_XKB
786   if (_gdk_use_xkb)
787     {
788       XkbDescRec *xkb = get_xkb ();
789
790       /* replace bits 13 and 14 with the provided group */
791       state &= ~(1 << 13 | 1 << 14);
792       state |= group << 13;
793       
794       MyEnhancedXkbTranslateKeyCode (xkb,
795                                      hardware_keycode,
796                                      state,
797                                      unused_modifiers,
798                                      &tmp_keyval,
799                                      effective_group,
800                                      level);
801
802       if (keyval)
803         *keyval = tmp_keyval;
804     }
805   else
806 #endif
807     {
808       gint shift_level;
809       
810       update_keymaps ();
811
812       if ((state & GDK_SHIFT_MASK) &&
813           (state & GDK_LOCK_MASK))
814         shift_level = 0; /* shift disables shift lock */
815       else if ((state & GDK_SHIFT_MASK) ||
816                (state & GDK_LOCK_MASK))
817         shift_level = 1;
818       else
819         shift_level = 0;
820
821       tmp_keyval = XKeycodeToKeysym (gdk_display, hardware_keycode,
822                                      group * keysyms_per_keycode + shift_level);
823       
824       if (keyval)
825         *keyval = tmp_keyval;
826
827       if (unused_modifiers)
828         {
829           *unused_modifiers = state;
830           *unused_modifiers &= ~(GDK_SHIFT_MASK | GDK_LOCK_MASK | group_switch_mask);
831         }
832       
833       if (effective_group)
834         *effective_group = (state & group_switch_mask) ? 1 : 0;
835
836       if (level)
837         *level = shift_level;
838     }
839
840   return tmp_keyval != NoSymbol;
841 }
842
843
844 /* Key handling not part of the keymap */
845
846 gchar*
847 gdk_keyval_name (guint        keyval)
848 {
849   return XKeysymToString (keyval);
850 }
851
852 guint
853 gdk_keyval_from_name (const gchar *keyval_name)
854 {
855   g_return_val_if_fail (keyval_name != NULL, 0);
856   
857   return XStringToKeysym (keyval_name);
858 }
859
860 #ifdef HAVE_XCONVERTCASE
861 void
862 gdk_keyval_convert_case (guint symbol,
863                          guint *lower,
864                          guint *upper)
865 {
866   KeySym xlower = 0;
867   KeySym xupper = 0;
868
869   if (symbol)
870     XConvertCase (symbol, &xlower, &xupper);
871
872   if (lower)
873     *lower = xlower;
874   if (upper)
875     *upper = xupper;
876 }  
877 #endif /* HAVE_XCONVERTCASE */