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