Removed winuser16.h from x11drv.h.
[wine] / windows / x11drv / keyboard.c
1 /*
2  * X11 keyboard driver
3  *
4  * Copyright 1993 Bob Amstadt
5  * Copyright 1996 Albrecht Kleine 
6  * Copyright 1997 David Faure
7  * Copyright 1998 Morten Welinder
8  * Copyright 1998 Ulrich Weigand
9  * Copyright 1999 Ove Kåven
10  */
11
12 #include "config.h"
13
14 #ifndef X_DISPLAY_MISSING
15
16 #include <X11/Xatom.h>
17 #include <X11/keysym.h>
18 #include "ts_xlib.h"
19 #include "ts_xresource.h"
20 #include "ts_xutil.h"
21
22 #include "wine/winuser16.h"
23 #include "debug.h"
24 #include "keyboard.h"
25 #include "message.h"
26 #include "windef.h"
27 #include "x11drv.h"
28 #include "winnls.h"
29
30 extern LPBYTE pKeyStateTable;
31
32 int min_keycode, max_keycode, keysyms_per_keycode;
33 WORD keyc2vkey[256], keyc2scan[256];
34
35 static int NumLockMask, AltGrMask; /* mask in the XKeyEvent state */
36 static int kcControl, kcAlt, kcShift, kcNumLock, kcCapsLock; /* keycodes */
37
38 /* Keyboard translation tables */
39 #define MAIN_LEN 48
40 static const int main_key_scan[MAIN_LEN] =
41 {
42 /* this is my (102-key) keyboard layout, sorry if it doesn't quite match yours */
43    0x29,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,
44    0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,
45    0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x2B,
46    0x2C,0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
47    0x56 /* the 102nd key (actually to the right of l-shift) */
48 };
49
50 /*** DEFINE YOUR NEW LANGUAGE-SPECIFIC MAPPINGS BELOW, SEE EXISTING TABLES */
51
52 /* the VK mappings for the main keyboard will be auto-assigned as before,
53    so what we have here is just the character tables */
54 /* order: Normal, Shift, AltGr, Shift-AltGr */
55 /* We recommend you write just what is guaranteed to be correct (i.e. what's
56    written on the keycaps), not the bunch of special characters behind AltGr
57    and Shift-AltGr if it can vary among different X servers */
58 /* Remember that your 102nd key (to the right of l-shift) should be on a
59    separate line, see existing tables */
60 /* If Wine fails to match your new table, use -debugmsg +key to find out why */
61 /* Remember to also add your new table to the layout index table far below! */
62
63 /*** United States keyboard layout (mostly contributed by Uwe Bonnes) */
64 static const char main_key_US[MAIN_LEN][4] =
65 {
66  "`~","1!","2@","3#","4$","5%","6^","7&","8*","9(","0)","-_","=+",
67  "qQ","wW","eE","rR","tT","yY","uU","iI","oO","pP","[{","]}",
68  "aA","sS","dD","fF","gG","hH","jJ","kK","lL",";:","'\"","\\|",
69  "zZ","xX","cC","vV","bB","nN","mM",",<",".>","/?"
70 };
71
72 /*** British keyboard layout */
73 static const char main_key_UK[MAIN_LEN][4] =
74 {
75  "`","1!","2\"","3£","4$","5%","6^","7&","8*","9(","0)","-_","=+",
76  "qQ","wW","eE","rR","tT","yY","uU","iI","oO","pP","[{","]}",
77  "aA","sS","dD","fF","gG","hH","jJ","kK","lL",";:","'@","#~",
78  "zZ","xX","cC","vV","bB","nN","mM",",<",".>","/?",
79  "\\|"
80 };
81
82 /*** French keyboard layout (contributed by Eric Pouech) */
83 static const char main_key_FR[MAIN_LEN][4] =
84 {
85  "²","&1","é2~","\"3#","'4{","(5[","-6|","è7","_8\\","ç9^±","à0@",")°]","=+}",
86  "aA","zZ","eE","rR","tT","yY","uU","iI","oO","pP","","$£¤",
87  "qQ","sSß","dD","fF","gG","hH","jJ","kK","lL","mM","ù%","*µ",
88  "wW","xX","cC","vV","bB","nN",",?",";.",":/","!§",
89  "<>"
90 };
91
92 /*** German keyboard layout (contributed by Ulrich Weigand) */
93 static const char main_key_DE[MAIN_LEN][4] =
94 {
95  "^°","1!","2\"","3§","4$","5%","6&","7/{","8([","9)]","0=}","ß?\\","'",
96  "qQ","wW","eE","rR","tT","zZ","uU","iI","oO","pP","üÜ","+*~",
97  "aA","sS","dD","fF","gG","hH","jJ","kK","lL","öÖ","äÄ","#´",
98  "yY","xX","cC","vV","bB","nN","mM",",;",".:","-_",
99  "<>"
100 };
101
102 /*** Norwegian keyboard layout (contributed by Ove Kåven) */
103 static const char main_key_NO[MAIN_LEN][4] =
104 {
105  "|§","1!","2\"@","3#£","4¤$","5%","6&","7/{","8([","9)]","0=}","+?","\\`´",
106  "qQ","wW","eE","rR","tT","yY","uU","iI","oO","pP","åÅ","¨^~",
107  "aA","sS","dD","fF","gG","hH","jJ","kK","lL","øØ","æÆ","'*",
108  "zZ","xX","cC","vV","bB","nN","mM",",;",".:","-_",
109  "<>"
110 };
111
112 /*** Danish keyboard layout (contributed by Bertho Stultiens) */
113 static const char main_key_DA[MAIN_LEN][4] =
114 {
115  "½§","1!","2\"@","3#£","4¤$","5%","6&","7/{","8([","9)]","0=}","+?","´`|",
116  "qQ","wW","eE","rR","tT","yY","uU","iI","oO","pP","åÅ","¨^~",
117  "aA","sS","dD","fF","gG","hH","jJ","kK","lL","æÆ","øØ","'*",
118  "zZ","xX","cC","vV","bB","nN","mM",",;",".:","-_",
119  "<>\\"
120 };
121
122 /*** Canadian French keyboard layout */
123 static const char main_key_CF[MAIN_LEN][4] =
124 {
125  "#|\\","1!±","2\"@","3/£","4$¢","5%¤","6?¬","7&¦","8*²","9(³","0)³","-_½","=+¾",
126  "qQ","wW","eE","rR","tT","yY","uU","iI","oO§","pP¶","^^[","¨]",
127  "aA","sS","dD","fF","gG","hH","jJ","kK","lL",";:~","``{","<>}",
128  "zZ","xX","cC","vV","bB","nN","mM",",'_",".","éÉ",
129  "«»°"
130 };
131
132 /*** Portuguese keyboard layout */
133 static const char main_key_PT[MAIN_LEN][4] =
134 {
135  "\\¦","1!","2\"@","3#£","4$§","5%","6&","7/{","8([","9)]","0=}","'?","«»",
136  "qQ",  "wW","eE",  "rR", "tT", "yY", "uU", "iI", "oO", "pP", "+*\\¨","\\'\\`",
137  "aA",  "sS","dD",  "fF", "gG", "hH", "jJ", "kK", "lL", "çÇ", "ºª", "\\~\\^",
138  "zZ",  "xX","cC",  "vV", "bB", "nN", "mM", ",;", ".:", "-_",
139  "<>"
140 };
141
142 /*** Layout table. Add your keyboard mappings to this list */
143 static struct {
144  WORD lang, ansi_codepage, oem_codepage;
145  const char (*key)[MAIN_LEN][4];
146 } main_key_tab[]={
147  {MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US), 1252, 437, &main_key_US},
148  {MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_UK), 1252, 850, &main_key_UK},
149  {MAKELANGID(LANG_FRENCH,SUBLANG_DEFAULT),     1252, 850, &main_key_FR},
150  {MAKELANGID(LANG_GERMAN,SUBLANG_DEFAULT),     1252, 850, &main_key_DE},
151  {MAKELANGID(LANG_NORWEGIAN,SUBLANG_DEFAULT),  1252, 865, &main_key_NO},
152  {MAKELANGID(LANG_DANISH,SUBLANG_DEFAULT),     1252, 865, &main_key_DA},
153  {MAKELANGID(LANG_FRENCH,SUBLANG_FRENCH_CANADIAN),1252,863,&main_key_CF},
154  {MAKELANGID(LANG_PORTUGUESE,SUBLANG_DEFAULT), 1252, 860, &main_key_PT},
155
156  {0} /* sentinel */
157 };
158 static unsigned kbd_layout=0; /* index into above table of layouts */
159
160 /* maybe more of these scancodes should be extended? */
161                 /* extended must be set for ALT_R, CTRL_R,
162                    INS, DEL, HOME, END, PAGE_UP, PAGE_DOWN, ARROW keys,
163                    keypad / and keypad ENTER (SDK 3.1 Vol.3 p 138) */
164                 /* FIXME should we set extended bit for NumLock ? My
165                  * Windows does ... DF */
166 static const int special_key_vkey[] =
167 {
168     VK_BACK, VK_TAB, 0, VK_CLEAR, 0, VK_RETURN, 0, 0,           /* FF08 */
169     0, 0, 0, VK_PAUSE, VK_SCROLL, 0, 0, 0,                      /* FF10 */
170     0, 0, 0, VK_ESCAPE                                          /* FF18 */
171 };
172 static const int special_key_scan[] =
173 {
174     0x0E, 0x0F, 0, /*?*/ 0, 0, 0x1C, 0, 0,                      /* FF08 */
175     0,    0,    0, 0x45, 0x46, 0   , 0, 0,                      /* FF10 */
176     0,    0,    0, 0x01                                         /* FF18 */
177 };
178
179 static const int cursor_key_vkey[] =
180 {
181     VK_HOME, VK_LEFT, VK_UP, VK_RIGHT, VK_DOWN, VK_PRIOR, 
182                                        VK_NEXT, VK_END          /* FF50 */
183 };
184 static const int cursor_key_scan[] =
185 {
186     0x147, 0x14B, 0x148, 0x14D, 0x150, 0x149, 0x151, 0x14F      /* FF50 */
187 };
188
189 static const int misc_key_vkey[] =
190 {
191     VK_SELECT, VK_SNAPSHOT, VK_EXECUTE, VK_INSERT, 0, 0, 0, 0,  /* FF60 */
192     VK_CANCEL, VK_HELP, VK_CANCEL, VK_MENU                      /* FF68 */
193 };
194 static const int misc_key_scan[] =
195 {
196     /*?*/ 0, 0x37, /*?*/ 0, 0x152, 0, 0, 0, 0,                  /* FF60 */
197     /*?*/ 0, /*?*/ 0, 0x38                                      /* FF68 */
198 };
199
200 static const int keypad_key_vkey[] =
201 {
202     0, VK_NUMLOCK,                                              /* FF7E */
203     0, 0, 0, 0, 0, 0, 0, 0,                                     /* FF80 */
204     0, 0, 0, 0, 0, VK_RETURN, 0, 0,                             /* FF88 */
205     0, 0, 0, 0, 0, VK_HOME, VK_LEFT, VK_UP,                     /* FF90 */
206     VK_RIGHT, VK_DOWN, VK_PRIOR, VK_NEXT, VK_END, 0,
207                                  VK_INSERT, VK_DELETE,          /* FF98 */
208     0, 0, 0, 0, 0, 0, 0, 0,                                     /* FFA0 */
209     0, 0, VK_MULTIPLY, VK_ADD, VK_SEPARATOR, VK_SUBTRACT, 
210                                VK_DECIMAL, VK_DIVIDE,           /* FFA8 */
211     VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3, VK_NUMPAD4,
212                             VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, /* FFB0 */
213     VK_NUMPAD8, VK_NUMPAD9                                      /* FFB8 */
214 };
215 static const int keypad_key_scan[] =
216 {
217     0x138, 0x45,                                                /* FF7E */
218     0, 0, 0, 0, 0, 0, 0, 0,                                     /* FF80 */
219     0, 0, 0, 0, 0, 0x11C, 0, 0,                                 /* FF88 */
220     0, 0, 0, 0, 0, 0x47, 0x4B, 0x48,                            /* FF90 */
221     0x4D, 0x50, 0x49, 0x51, 0x4F, 0x4C, 0x52, 0x53,             /* FF98 */
222     0, 0, 0, 0, 0, 0, 0, 0,                                     /* FFA0 */
223     0, 0, 0x37, 0x4E, /*?*/ 0, 0x4A, 0x53, 0x135,               /* FFA8 */
224     0x52, 0x4F, 0x50, 0x51, 0x4B, 0x4C, 0x4D, 0x47,             /* FFB0 */
225     0x48, 0x49                                                  /* FFB8 */
226 };
227     
228 static const int function_key_vkey[] =
229 {
230     VK_F1, VK_F2,                                               /* FFBE */
231     VK_F3, VK_F4, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9, VK_F10,    /* FFC0 */
232     VK_F11, VK_F12, VK_F13, VK_F14, VK_F15, VK_F16              /* FFC8 */
233 };
234 static const int function_key_scan[] =
235 {
236     0x3B, 0x3C,                                                 /* FFBE */
237     0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44,             /* FFC0 */
238     0x57, 0x58, 0, 0, 0, 0                                      /* FFC8 */
239 };
240
241 static const int modifier_key_vkey[] =
242 {
243     VK_SHIFT, VK_SHIFT, VK_CONTROL, VK_CONTROL, VK_CAPITAL, 0, /* FFE1 */
244     VK_MENU, VK_MENU, VK_MENU, VK_MENU                         /* FFE7 */
245 };
246 static const int modifier_key_scan[] =
247 {
248     0x2A, 0x36, 0x1D, 0x11D, 0x3A, 0,                          /* FFE1 */
249     0x38, 0x138, 0x38, 0x138                                   /* FFE7 */
250 };
251
252 /* Returns the Windows virtual key code associated with the X event <e> */
253 static WORD EVENT_event_to_vkey( XKeyEvent *e)
254 {
255     KeySym keysym;
256
257     TSXLookupString(e, NULL, 0, &keysym, NULL);
258
259     if ((keysym >= 0xFFAE) && (keysym <= 0xFFB9) && (e->state & NumLockMask)) 
260         /* Only the Keypad keys 0-9 and . send different keysyms
261          * depending on the NumLock state */
262         return keypad_key_vkey[(keysym & 0xFF) - 0x7E];
263
264     return keyc2vkey[e->keycode];
265 }
266
267 static BOOL NumState=FALSE, CapsState=FALSE;
268
269 /**********************************************************************
270  *              KEYBOARD_GenerateMsg
271  *
272  * Generate Down+Up messages when NumLock or CapsLock is pressed.
273  *
274  * Convention : called with vkey only VK_NUMLOCK or VK_CAPITAL
275  *
276  */
277 void KEYBOARD_GenerateMsg( WORD vkey, WORD scan, int Evtype, INT event_x, INT event_y,
278                            DWORD event_time )
279 {
280   BOOL * State = (vkey==VK_NUMLOCK? &NumState : &CapsState);
281
282   if (*State) {
283     /* The INTERMEDIARY state means : just after a 'press' event, if a 'release' event comes,
284        don't treat it. It's from the same key press. Then the state goes to ON.
285        And from there, a 'release' event will switch off the toggle key. */
286     *State=FALSE;
287     TRACE(keyboard,"INTERM : don\'t treat release of toggle key. InputKeyStateTable[%#x] = %#x\n",vkey,pKeyStateTable[vkey]);
288   } else
289     {
290         if ( pKeyStateTable[vkey] & 0x1 ) /* it was ON */
291           {
292             if (Evtype!=KeyPress)
293               {
294                 TRACE(keyboard,"ON + KeyRelease => generating DOWN and UP messages.\n");
295                 KEYBOARD_SendEvent( vkey, scan, 0,
296                                     event_x, event_y, event_time );
297                 KEYBOARD_SendEvent( vkey, scan, KEYEVENTF_KEYUP, 
298                                     event_x, event_y, event_time );
299                 *State=FALSE;
300                 pKeyStateTable[vkey] &= ~0x01; /* Toggle state to off. */ 
301               } 
302           }
303         else /* it was OFF */
304           if (Evtype==KeyPress)
305             {
306               TRACE(keyboard,"OFF + Keypress => generating DOWN and UP messages.\n");
307               KEYBOARD_SendEvent( vkey, scan, 0,
308                                   event_x, event_y, event_time );
309               KEYBOARD_SendEvent( vkey, scan, KEYEVENTF_KEYUP, 
310                                   event_x, event_y, event_time );
311               *State=TRUE; /* Goes to intermediary state before going to ON */
312               pKeyStateTable[vkey] |= 0x01; /* Toggle state to on. */
313             }
314     }
315 }
316
317 /***********************************************************************
318  *           KEYBOARD_UpdateOneState
319  *
320  * Updates internal state for <vkey>, depending on key <state> under X
321  *
322  */
323 void KEYBOARD_UpdateOneState ( int vkey, int state )
324 {
325     /* Do something if internal table state != X state for keycode */
326     if (((pKeyStateTable[vkey] & 0x80)!=0) != state)
327     {
328         TRACE(keyboard,"Adjusting state for vkey %#.2x. State before %#.2x \n",
329               vkey, pKeyStateTable[vkey]);
330
331         /* Fake key being pressed inside wine */
332         KEYBOARD_SendEvent( vkey, 0, state? 0 : KEYEVENTF_KEYUP, 
333                             0, 0, GetTickCount() );
334
335         TRACE(keyboard,"State after %#.2x \n",pKeyStateTable[vkey]);
336     }
337 }
338
339 /***********************************************************************
340  *           X11DRV_KEYBOARD_UpdateState
341  *
342  * Update modifiers state (Ctrl, Alt, Shift)
343  * when window is activated (called by EVENT_FocusIn in event.c)
344  *
345  * This handles the case where one uses Ctrl+... Alt+... or Shift+.. to switch
346  * from wine to another application and back.
347  * Toggle keys are handled in HandleEvent. (because XQueryKeymap says nothing
348  *  about them)
349  */
350 void X11DRV_KEYBOARD_UpdateState ( void )
351 {
352 /* extract a bit from the char[32] bit suite */
353 #define KeyState(keycode) ((keys_return[keycode/8] & (1<<(keycode%8)))!=0)
354
355     char keys_return[32];
356
357     TRACE(keyboard,"called\n");
358     if (!TSXQueryKeymap(display, keys_return)) {
359         ERR(keyboard,"Error getting keymap !");
360         return;
361     }
362
363     /* Adjust the ALT and CONTROL state if any has been changed outside wine */
364     KEYBOARD_UpdateOneState(VK_MENU, KeyState(kcAlt));
365     KEYBOARD_UpdateOneState(VK_CONTROL, KeyState(kcControl));
366     KEYBOARD_UpdateOneState(VK_SHIFT, KeyState(kcShift));
367 #undef KeyState
368 }
369
370 /***********************************************************************
371  *           X11DRV_KEYBOARD_HandleEvent
372  *
373  * Handle a X key event
374  */
375 void X11DRV_KEYBOARD_HandleEvent( WND *pWnd, XKeyEvent *event )
376 {
377     char Str[24]; 
378     XComposeStatus cs; 
379     KeySym keysym;
380     WORD vkey = 0, bScan;
381     DWORD dwFlags;
382     static BOOL force_extended = FALSE; /* hack for AltGr translation */
383     
384     int ascii_chars;
385
386     INT event_x = (pWnd? pWnd->rectWindow.left : 0) + event->x;
387     INT event_y = (pWnd? pWnd->rectWindow.top  : 0) + event->y;
388     DWORD event_time = event->time - MSG_WineStartTicks;
389
390     /* this allows support for dead keys */
391     if ((event->keycode >> 8) == 0x10)
392         event->keycode=(event->keycode & 0xff);
393
394     ascii_chars = TSXLookupString(event, Str, 1, &keysym, &cs);
395
396     TRACE(key, "EVENT_key : state = %X\n", event->state);
397     if (keysym == XK_Mode_switch)
398         {
399         TRACE(key, "Alt Gr key event received\n");
400         event->keycode = kcControl; /* Simulate Control */
401         X11DRV_KEYBOARD_HandleEvent( pWnd, event );
402
403         event->keycode = kcAlt; /* Simulate Alt */
404         force_extended = TRUE;
405         X11DRV_KEYBOARD_HandleEvent( pWnd, event );
406         force_extended = FALSE;
407         return;
408         }
409
410     Str[ascii_chars] = '\0';
411     if (TRACE_ON(key)){
412         char    *ksname;
413
414         ksname = TSXKeysymToString(keysym);
415         if (!ksname)
416           ksname = "No Name";
417         TRACE(key, "%s : keysym=%lX (%s), ascii chars=%u / %X / '%s'\n", 
418                      (event->type == KeyPress) ? "KeyPress" : "KeyRelease",
419                      keysym, ksname, ascii_chars, Str[0] & 0xff, Str);
420     }
421
422     vkey = EVENT_event_to_vkey(event);
423     if (force_extended) vkey |= 0x100;
424
425     TRACE(key, "keycode 0x%x converted to vkey 0x%x\n",
426                     event->keycode, vkey);
427
428    if (vkey)
429    {
430     switch (vkey & 0xff)
431     {
432     case VK_NUMLOCK:    
433       KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, event->type, event_x, event_y,
434                             event_time );
435       break;
436     case VK_CAPITAL:
437       TRACE(keyboard,"Caps Lock event. (type %d). State before : %#.2x\n",event->type,pKeyStateTable[vkey]);
438       KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, event->type, event_x, event_y,
439                             event_time ); 
440       TRACE(keyboard,"State after : %#.2x\n",pKeyStateTable[vkey]);
441       break;
442     default:
443         /* Adjust the NUMLOCK state if it has been changed outside wine */
444         if (!(pKeyStateTable[VK_NUMLOCK] & 0x01) != !(event->state & NumLockMask))
445           { 
446             TRACE(keyboard,"Adjusting NumLock state. \n");
447             KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, KeyPress, event_x, event_y,
448                                   event_time );
449             KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, KeyRelease, event_x, event_y,
450                                   event_time );
451           }
452         /* Adjust the CAPSLOCK state if it has been changed outside wine */
453         if (!(pKeyStateTable[VK_CAPITAL] & 0x01) != !(event->state & LockMask))
454           {
455               TRACE(keyboard,"Adjusting Caps Lock state.\n");
456             KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, KeyPress, event_x, event_y,
457                                   event_time );
458             KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, KeyRelease, event_x, event_y,
459                                   event_time );
460           }
461         /* Not Num nor Caps : end of intermediary states for both. */
462         NumState = FALSE;
463         CapsState = FALSE;
464
465         bScan = keyc2scan[event->keycode] & 0xFF;
466         TRACE(key, "bScan = 0x%02x.\n", bScan);
467
468         dwFlags = 0;
469         if ( event->type == KeyRelease ) dwFlags |= KEYEVENTF_KEYUP;
470         if ( vkey & 0x100 )              dwFlags |= KEYEVENTF_EXTENDEDKEY;
471         if ( force_extended )            dwFlags |= KEYEVENTF_WINE_FORCEEXTENDED;
472
473         KEYBOARD_SendEvent( vkey & 0xff, bScan, dwFlags, 
474                             event_x, event_y, event_time );
475     }
476    }
477 }
478
479 /**********************************************************************
480  *              X11DRV_KEYBOARD_DetectLayout
481  *
482  * Called from X11DRV_KEYBOARD_Init
483  *  This routine walks through the defined keyboard layouts and selects
484  *  whichever matches most closely.
485  */
486 void X11DRV_KEYBOARD_DetectLayout(void)
487 {
488     unsigned current, match, mismatch;
489     int max_score = 0, ismatch = 0;
490     int score, keyc, i, key, ok, syms = (keysyms_per_keycode>4) ? 4 : keysyms_per_keycode;
491     KeySym keysym;
492     char ckey[4]={0,0,0,0};
493     const char (*lkey)[MAIN_LEN][4];
494
495     for (current=0; main_key_tab[current].lang; current++) {
496       TRACE(keyboard,"Attempting to match against layout %04x\n",main_key_tab[current].lang);
497       match = 0; mismatch = 0;
498       lkey = main_key_tab[current].key;
499       for (keyc=min_keycode; keyc<=max_keycode; keyc++) {
500         /* get data for keycode from X server */
501         for (i=0; i<syms; i++) {
502           keysym = TSXKeycodeToKeysym(display, keyc, i);
503           if ((keysym<0x100)&&(keysym!=' ')) ckey[i] = keysym;
504           else ckey[i] = 0;
505         }
506         if (ckey[0]) {
507           /* search for a match in layout table */
508           /* right now, we just find an absolute match for defined positions */
509           /* (undefined positions are ignored, so if it's defined as "3#"
510              in the table, it's okay that the X server has "3#£", for example) */
511           for (key=0; key<MAIN_LEN; key++) {
512             for (ok=(*lkey)[key][i=0]; ok&&(i<4); i++)
513               if ((*lkey)[key][i] && (*lkey)[key][i]!=ckey[i]) ok=0;
514             if (ok) break;
515           }
516           /* count the matches and mismatches */
517           if (key<MAIN_LEN) match++; else {
518             TRACE(key,"mismatch for keycode %d, character %c\n",keyc,ckey[0]);
519             mismatch++;
520           }
521         }
522       }
523       /* estimate and check score */
524       score = match - mismatch;
525       TRACE(keyboard,"matches=%d, mismatches=%d, score=%d\n",match,mismatch,score);
526       if (score > max_score) {
527         /* best match so far */
528         kbd_layout = current;
529         max_score = score;
530         ismatch = !mismatch;
531       }
532     }
533     /* we're done, report results if necessary */
534     if (!ismatch) {
535       FIXME(keyboard,"Your keyboard layout was not found! Using closest match (%04x).\n",
536             main_key_tab[kbd_layout].lang);
537       FIXME(keyboard,"Please define your layout in windows/x11drv/keyboard.c, and submit them\n");
538       FIXME(keyboard,"to us for inclusion into future Wine releases.\n");
539       FIXME(keyboard,"See documentation/keyboard for more information.\n");
540     }
541     TRACE(keyboard,"detected layout is %04x\n",main_key_tab[kbd_layout].lang);
542 }
543
544 /**********************************************************************
545  *              X11DRV_KEYBOARD_Init
546  */
547 void X11DRV_KEYBOARD_Init(void)
548 {
549     KeySym *ksp;
550     XModifierKeymap *mmp;
551     KeySym keysym;
552     KeyCode *kcp;
553     XKeyEvent e2;
554     WORD scan, vkey, OEMvkey;
555     int keyc, i, keyn, syms;
556     char ckey[4]={0,0,0,0};
557     const char (*lkey)[MAIN_LEN][4];
558
559     TSXDisplayKeycodes(display, &min_keycode, &max_keycode);
560     ksp = TSXGetKeyboardMapping(display, min_keycode,
561                               max_keycode + 1 - min_keycode, &keysyms_per_keycode);
562     /* We are only interested in keysyms_per_keycode.
563        There is no need to hold a local copy of the keysyms table */
564     TSXFree(ksp);
565     mmp = TSXGetModifierMapping(display);
566     kcp = mmp->modifiermap;
567     for (i = 0; i < 8; i += 1) /* There are 8 modifier keys */
568     {
569         int j;
570         
571         for (j = 0; j < mmp->max_keypermod; j += 1, kcp += 1)
572             if (*kcp)
573             {
574                 int k;
575                 
576                 for (k = 0; k < keysyms_per_keycode; k += 1)
577                     if (TSXKeycodeToKeysym(display, *kcp, k) == XK_Mode_switch)
578                     {
579                         AltGrMask = 1 << i;
580                         TRACE(key, "AltGrMask is %x\n", AltGrMask);
581                     }
582                     else if (TSXKeycodeToKeysym(display, *kcp, k) == XK_Num_Lock)
583                     {
584                         NumLockMask = 1 << i;
585                         TRACE(key, "NumLockMask is %x\n", NumLockMask);
586                     }
587             }
588     }
589     TSXFreeModifiermap(mmp);
590
591     /* Detect the keyboard layout */
592     X11DRV_KEYBOARD_DetectLayout();
593     lkey = main_key_tab[kbd_layout].key;
594     syms = (keysyms_per_keycode > 4) ? 4 : keysyms_per_keycode;
595
596     /* Now build two conversion arrays :
597      * keycode -> vkey + scancode + extended
598      * vkey + extended -> keycode */
599
600     e2.display = display;
601     e2.state = 0;
602
603     OEMvkey = VK_OEM_7; /* next is available.  */
604     for (keyc = min_keycode; keyc <= max_keycode; keyc++)
605     {
606         e2.keycode = (KeyCode)keyc;
607         TSXLookupString(&e2, NULL, 0, &keysym, NULL);
608         vkey = 0; scan = 0;
609         if (keysym)  /* otherwise, keycode not used */
610         {
611             if ((keysym >> 8) == 0xFF)         /* non-character key */
612             {
613                 int key = keysym & 0xff;
614                 
615                 if (key >= 0x08 && key <= 0x1B) {        /* special key */
616                     vkey = special_key_vkey[key - 0x08];
617                     scan = special_key_scan[key - 0x08];
618                 } else if (key >= 0x50 && key <= 0x57) { /* cursor key */
619                     vkey = cursor_key_vkey[key - 0x50];
620                     scan = cursor_key_scan[key - 0x50];
621                 } else if (key >= 0x60 && key <= 0x6B) { /* miscellaneous key */
622                     vkey = misc_key_vkey[key - 0x60];
623                     scan = misc_key_scan[key - 0x60];
624                 } else if (key >= 0x7E && key <= 0xB9) { /* keypad key */
625                     vkey = keypad_key_vkey[key - 0x7E];
626                     scan = keypad_key_scan[key - 0x7E];
627                 } else if (key >= 0xBE && key <= 0xCD) { /* function key */
628                     vkey = function_key_vkey[key - 0xBE] | 0x100; /* set extended bit */
629                     scan = function_key_scan[key - 0xBE];
630                 } else if (key >= 0xE1 && key <= 0xEA) { /* modifier key */
631                     vkey = modifier_key_vkey[key - 0xE1];
632                     scan = modifier_key_scan[key - 0xE1];
633                 } else if (key == 0xFF) {                /* DEL key */
634                     vkey = VK_DELETE;
635                     scan = 0x153;
636                 }
637                 /* set extended bit when necessary */
638                 if (scan & 0x100) vkey |= 0x100;
639             } else if (keysym == 0x20) {                 /* Spacebar */
640                 vkey = VK_SPACE;
641                 scan = 0x39;
642             } else {
643               /* we seem to need to search the layout-dependent scancodes */
644               int maxlen=0,maxval=-1,ok;
645               for (i=0; i<syms; i++) {
646                 keysym = TSXKeycodeToKeysym(display, keyc, i);
647                 if ((keysym<0x100)&&(keysym!=' ')) ckey[i] = keysym;
648                 else ckey[i] = 0;
649               }
650               /* find key with longest match streak */
651               for (keyn=0; keyn<MAIN_LEN; keyn++) {
652                 for (ok=(*lkey)[keyn][i=0]; ok&&(i<4); i++)
653                   if ((*lkey)[keyn][i] && (*lkey)[keyn][i]!=ckey[i]) ok=0;
654                 if (ok||(i>maxlen)) {
655                   maxlen=i; maxval=keyn;
656                 }
657                 if (ok) break;
658               }
659               if (maxval>=0) {
660                 /* got it */
661                 scan = main_key_scan[maxval];
662               }
663             }
664
665             /* find a suitable layout-dependent VK code */
666             /* (most Winelib apps ought to be able to work without layout tables!) */
667             for (i = 0; (i < keysyms_per_keycode) && (!vkey); i++)
668             {
669                 keysym = TSXLookupKeysym(&e2, i);
670                 if ((keysym >= VK_0 && keysym <= VK_9)
671                     || (keysym >= VK_A && keysym <= VK_Z)) {
672                     vkey = keysym;
673                 }
674             }
675
676             for (i = 0; (i < keysyms_per_keycode) && (!vkey); i++)
677             {
678                 keysym = TSXLookupKeysym(&e2, i);
679                 switch (keysym)
680                 {
681                 case ';':             vkey = VK_OEM_1; break;
682                 case '/':             vkey = VK_OEM_2; break;
683                 case '`':             vkey = VK_OEM_3; break;
684                 case '[':             vkey = VK_OEM_4; break;
685                 case '\\':            vkey = VK_OEM_5; break;
686                 case ']':             vkey = VK_OEM_6; break;
687                 case '\'':            vkey = VK_OEM_7; break;
688                 case ',':             vkey = VK_OEM_COMMA; break;
689                 case '.':             vkey = VK_OEM_PERIOD; break;
690                 case '-':             vkey = VK_OEM_MINUS; break;
691                 case '+':             vkey = VK_OEM_PLUS; break;
692                 }
693             }
694
695             if (!vkey)
696             {
697                 /* Others keys: let's assign OEM virtual key codes in the allowed range,
698                  * that is ([0xba,0xc0], [0xdb,0xe4], 0xe6 (given up) et [0xe9,0xf5]) */
699                 switch (++OEMvkey)
700                 {
701                 case 0xc1 : OEMvkey=0xdb; break;
702                 case 0xe5 : OEMvkey=0xe9; break;
703                 case 0xf6 : OEMvkey=0xf5; WARN(keyboard,"No more OEM vkey available!\n");
704                 }
705
706                 vkey = OEMvkey;
707                   
708                 if (TRACE_ON(keyboard))
709                 {
710                     dbg_decl_str(keyboard, 1024);
711
712                     TRACE(keyboard, "OEM specific virtual key %X assigned "
713                                  "to keycode %X:\n", OEMvkey, e2.keycode);
714                     for (i = 0; i < keysyms_per_keycode; i += 1)
715                     {
716                         char    *ksname;
717                         
718                         keysym = TSXLookupKeysym(&e2, i);
719                         ksname = TSXKeysymToString(keysym);
720                         if (!ksname)
721                             ksname = "NoSymbol";
722                         dsprintf(keyboard, "%lX (%s) ", keysym, ksname);
723                     }
724                     TRACE(keyboard, "(%s)\n", dbg_str(keyboard));
725                 }
726             }
727         }
728         keyc2vkey[e2.keycode] = vkey;
729         keyc2scan[e2.keycode] = scan;
730     } /* for */
731
732     /* If some keys still lack scancodes, assign some arbitrary ones to them now */
733     for (scan = 0x60, keyc = min_keycode; keyc <= max_keycode; keyc++)
734       if (keyc2vkey[keyc]&&!keyc2scan[keyc]) {
735         char *ksname;
736         keysym = TSXKeycodeToKeysym(display, keyc, 0);
737         ksname = TSXKeysymToString(keysym);
738         if (!ksname) ksname = "NoSymbol";
739
740         /* should make sure the scancode is unassigned here, but >=0x60 currently always is */
741
742         TRACE(key,"assigning scancode %02x to unidentified keycode %02x (%s)\n",scan,keyc,ksname);
743         keyc2scan[keyc]=scan++;
744       }
745
746     /* Now store one keycode for each modifier. Used to simulate keypresses. */
747     kcControl = TSXKeysymToKeycode(display, XK_Control_L);
748     kcAlt = TSXKeysymToKeycode(display, XK_Alt_L);
749     if (!kcAlt) kcAlt = TSXKeysymToKeycode(display, XK_Meta_L);
750     kcShift = TSXKeysymToKeycode(display, XK_Shift_L);
751     kcNumLock = TSXKeysymToKeycode(display, XK_Num_Lock);
752     kcCapsLock = TSXKeysymToKeycode(display, XK_Caps_Lock);
753 }
754
755 /***********************************************************************
756  *              X11DRV_KEYBOARD_VkKeyScan
757  */
758 WORD X11DRV_KEYBOARD_VkKeyScan(CHAR cChar)
759 {
760         KeyCode keycode;
761         KeySym keysym;          
762         int i,index;
763         int highbyte=0;
764
765         /* char->keysym (same for ANSI chars) */
766         keysym=(unsigned char) cChar;/* (!) cChar is signed */
767         if (keysym<=27) keysym+=0xFF00;/*special chars : return, backspace...*/
768         
769         keycode = TSXKeysymToKeycode(display, keysym);  /* keysym -> keycode */
770         if (!keycode)
771         { /* It didn't work ... let's try with deadchar code. */
772           keycode = TSXKeysymToKeycode(display, keysym | 0xFE00);
773         }
774
775         TRACE(keyboard,"VkKeyScan '%c'(%#lx, %lu): got keycode %#.2x\n",
776                          cChar,keysym,keysym,keycode);
777         
778         if (keycode)
779           {
780             for (index=-1, i=0; (i<8) && (index<0); i++) /* find shift state */
781               if (TSXKeycodeToKeysym(display,keycode,i)==keysym) index=i;
782             switch (index) {
783             case -1 :
784               WARN(keyboard,"Keysym %lx not found while parsing the keycode table\n",keysym); break;
785             case 0 : break;
786             case 1 : highbyte = 0x0100; break;
787             case 2 : highbyte = 0X0600; break;
788             default : ERR(keyboard,"index %d found by XKeycodeToKeysym. please report! \n",index);
789             }
790             /*
791               index : 0     adds 0x0000
792               index : 1     adds 0x0100 (shift)
793               index : ?     adds 0x0200 (ctrl)
794               index : 2     adds 0x0600 (ctrl+alt)
795               index : ?     adds 0x0700 (ctrl+alt+shift (used?))
796              */
797           }
798         TRACE(keyboard," ... returning %#.2x\n", keyc2vkey[keycode]+highbyte);
799         return keyc2vkey[keycode]+highbyte;   /* keycode -> (keyc2vkey) vkey */
800 }
801
802 /***********************************************************************
803  *              X11DRV_KEYBOARD_MapVirtualKey
804  */
805 UINT16 X11DRV_KEYBOARD_MapVirtualKey(UINT16 wCode, UINT16 wMapType)
806 {
807 #define returnMVK(value) { TRACE(keyboard,"returning 0x%x.\n",value); return value; }
808
809         TRACE(keyboard,"MapVirtualKey wCode=0x%x wMapType=%d ... \n",
810                          wCode,wMapType);
811         switch(wMapType) {
812                 case 0: { /* vkey-code to scan-code */
813                         /* let's do vkey -> keycode -> scan */
814                         int keyc;
815                         for (keyc=min_keycode; keyc<=max_keycode; keyc++)
816                                 if ((keyc2vkey[keyc] & 0xFF) == wCode)
817                                         returnMVK (keyc2scan[keyc] & 0xFF);
818                         return 0; }
819
820                 case 1: { /* scan-code to vkey-code */
821                         /* let's do scan -> keycode -> vkey */
822                         int keyc;
823                         for (keyc=min_keycode; keyc<=max_keycode; keyc++)
824                                 if ((keyc2scan[keyc] & 0xFF) == wCode)
825                                         returnMVK (keyc2vkey[keyc] & 0xFF);
826                         return 0; }
827
828                 case 2: { /* vkey-code to unshifted ANSI code */
829                         /* (was FIXME) : what does unshifted mean ? 'a' or 'A' ? */
830                         /* My Windows returns 'A'. */
831                         /* let's do vkey -> keycode -> (XLookupString) ansi char */
832                         XKeyEvent e;
833                         KeySym keysym;
834                         char s[2];
835                         e.display = display;
836                         e.state = 0; /* unshifted */
837                         e.keycode = MapVirtualKey16( wCode, 0);
838                         if (!TSXLookupString(&e, s , 2 , &keysym, NULL))
839                           returnMVK (*s);
840                         
841                         return 0;
842                         }
843                 default: /* reserved */
844                         WARN(keyboard, "Unknown wMapType %d !\n",
845                                 wMapType);
846                         return 0;       
847         }
848         return 0;
849 }
850
851 /***********************************************************************
852  *              X11DRV_KEYBOARD_GetKeyNameText
853  */
854 INT16 X11DRV_KEYBOARD_GetKeyNameText(LONG lParam, LPSTR lpBuffer, INT16 nSize)
855 {
856 #if 0
857   int i;
858 #endif
859         
860   FIXME(keyboard,"(%ld,<ptr>,%d): stub\n",lParam,nSize);
861
862 #if 0
863   lParam >>= 16;
864   lParam &= 0xff;
865   
866   for (i = 0 ; i != KeyTableSize ; i++) 
867     if (KeyTable[i].scancode == lParam)  {
868       lstrcpynA( lpBuffer, KeyTable[i].name, nSize );
869       return strlen(lpBuffer);
870     }
871 #endif
872
873   *lpBuffer = 0;
874   return 0;
875 }
876
877 /***********************************************************************
878  *              X11DRV_KEYBOARD_ToAscii
879  *
880  * The ToAscii function translates the specified virtual-key code and keyboard
881  * state to the corresponding Windows character or characters.
882  *
883  * If the specified key is a dead key, the return value is negative. Otherwise,
884  * it is one of the following values:
885  * Value        Meaning
886  * 0    The specified virtual key has no translation for the current state of the keyboard.
887  * 1    One Windows character was copied to the buffer.
888  * 2    Two characters were copied to the buffer. This usually happens when a
889  *      dead-key character (accent or diacritic) stored in the keyboard layout cannot
890  *      be composed with the specified virtual key to form a single character.
891  *
892  * FIXME : should do the above (return 2 for non matching deadchar+char combinations)
893  *
894  */
895 INT16 X11DRV_KEYBOARD_ToAscii(
896     UINT16 virtKey,UINT16 scanCode, LPBYTE lpKeyState, 
897     LPVOID lpChar, UINT16 flags)
898 {
899     XKeyEvent e;
900     KeySym keysym;
901     static XComposeStatus cs;
902     INT ret;
903     int keyc;
904
905     if (scanCode==0) {
906         /* This happens when doing Alt+letter : a fake 'down arrow' key press
907            event is generated by windows. Just ignore it. */
908         TRACE(keyboard,"scanCode=0, doing nothing\n");
909         return 0;
910     }
911     if (scanCode & 0x8000)
912     {
913         TRACE( keyboard, "Key UP, doing nothing\n" );
914         return 0;
915     }
916     e.display = display;
917     e.keycode = 0;
918     e.state = 0;
919     if (lpKeyState[VK_SHIFT] & 0x80)
920         e.state |= ShiftMask;
921     if (lpKeyState[VK_CAPITAL] & 0x01)
922         e.state |= LockMask;
923     if (lpKeyState[VK_CONTROL] & 0x80)
924     {
925         if (lpKeyState[VK_MENU] & 0x80)
926             e.state |= AltGrMask;
927         else
928             e.state |= ControlMask;
929     }
930     if (lpKeyState[VK_NUMLOCK] & 0x01)
931         e.state |= NumLockMask;
932     TRACE(key, "(%04X, %04X) : faked state = %X\n",
933                 virtKey, scanCode, e.state);
934     /* We exit on the first keycode found, to speed up the thing. */
935     for (keyc=min_keycode; (keyc<=max_keycode) && (!e.keycode) ; keyc++)
936       { /* Find a keycode that could have generated this virtual key */
937           if  ((keyc2vkey[keyc] & 0xFF) == virtKey)
938           { /* We filter the extended bit, we don't know it */
939               e.keycode = keyc; /* Store it temporarily */
940               if ((EVENT_event_to_vkey(&e) & 0xFF) != virtKey) {
941                   e.keycode = 0; /* Wrong one (ex: because of the NumLock
942                          state), so set it to 0, we'll find another one */
943               }
944           }
945       }
946
947     if ((virtKey>=VK_NUMPAD0) && (virtKey<=VK_NUMPAD9))
948         e.keycode = TSXKeysymToKeycode(e.display, virtKey-VK_NUMPAD0+XK_KP_0);
949           
950     if (virtKey==VK_DECIMAL)
951         e.keycode = TSXKeysymToKeycode(e.display, XK_KP_Decimal);
952
953     if (!e.keycode)
954       {
955         WARN(keyboard,"Unknown virtual key %X !!! \n",virtKey);
956         return virtKey; /* whatever */
957       }
958     else TRACE(keyboard,"Found keycode %d (0x%2X)\n",e.keycode,e.keycode);
959
960     ret = TSXLookupString(&e, (LPVOID)lpChar, 2, &keysym, &cs);
961     if (ret == 0)
962         {
963         BYTE dead_char = 0;
964
965         ((char*)lpChar)[1] = '\0';
966         switch (keysym)
967             {
968         /* symbolic ASCII is the same as defined in rfc1345 */
969 #ifdef XK_dead_tilde
970             case XK_dead_tilde :
971 #endif
972             case 0x1000FE7E : /* Xfree's XK_Dtilde */
973                 dead_char = '~';        /* '? */
974                 break;
975 #ifdef XK_dead_acute
976             case XK_dead_acute :
977 #endif
978             case 0x1000FE27 : /* Xfree's XK_Dacute_accent */
979                 dead_char = 0xb4;       /* '' */
980                 break;
981 #ifdef XK_dead_circumflex
982             case XK_dead_circumflex :
983 #endif
984             case 0x1000FE5E : /* Xfree's XK_Dcircumflex_accent */
985                 dead_char = '^';        /* '> */
986                 break;
987 #ifdef XK_dead_grave
988             case XK_dead_grave :
989 #endif
990             case 0x1000FE60 : /* Xfree's XK_Dgrave_accent */
991                 dead_char = '`';        /* '! */
992                 break;
993 #ifdef XK_dead_diaeresis
994             case XK_dead_diaeresis :
995 #endif
996             case 0x1000FE22 : /* Xfree's XK_Ddiaeresis */
997                 dead_char = 0xa8;       /* ': */
998                 break;
999 #ifdef XK_dead_cedilla
1000             case XK_dead_cedilla :
1001                 dead_char = 0xb8;       /* ', */
1002                 break;
1003 #endif
1004 #ifdef XK_dead_macron
1005             case XK_dead_macron :
1006                 dead_char = '-';        /* 'm isn't defined on iso-8859-x */
1007                 break;
1008 #endif
1009 #ifdef XK_dead_breve
1010             case XK_dead_breve :
1011                 dead_char = 0xa2;       /* '( */
1012                 break;
1013 #endif
1014 #ifdef XK_dead_abovedot
1015             case XK_dead_abovedot :
1016                 dead_char = 0xff;       /* '. */
1017                 break;
1018 #endif
1019 #ifdef XK_dead_abovering
1020             case XK_dead_abovering :
1021                 dead_char = '0';        /* '0 isn't defined on iso-8859-x */
1022                 break;
1023 #endif
1024 #ifdef XK_dead_doubleacute
1025             case XK_dead_doubleacute :
1026                 dead_char = 0xbd;       /* '" */
1027                 break;
1028 #endif
1029 #ifdef XK_dead_caron
1030             case XK_dead_caron :
1031                 dead_char = 0xb7;       /* '< */
1032                 break;
1033 #endif
1034 #ifdef XK_dead_ogonek
1035             case XK_dead_ogonek :
1036                 dead_char = 0xb2;       /* '; */
1037                 break;
1038 #endif
1039 /* FIXME: I don't know this three.
1040             case XK_dead_iota :
1041                 dead_char = 'i';         
1042                 break;
1043             case XK_dead_voiced_sound :
1044                 dead_char = 'v';
1045                 break;
1046             case XK_dead_semivoiced_sound :
1047                 dead_char = 's';
1048                 break;
1049 */
1050             }
1051         if (dead_char)
1052             {
1053             *(char*)lpChar = dead_char;
1054             ret = -1;
1055             }
1056         else
1057             {
1058             char        *ksname;
1059
1060             ksname = TSXKeysymToString(keysym);
1061             if (!ksname)
1062                 ksname = "No Name";
1063             if ((keysym >> 8) != 0xff)
1064                 {
1065                 ERR(keyboard, "Please report: no char for keysym %04lX (%s) :\n",
1066                         keysym, ksname);
1067                 ERR(keyboard, "(virtKey=%X,scanCode=%X,keycode=%X,state=%X)\n",
1068                         virtKey, scanCode, e.keycode, e.state);
1069                 }
1070             }
1071         }
1072     else {  /* ret = 1 */
1073         /* We have a special case to handle : Shift + arrow, shift + home, ...
1074            X returns a char for it, but Windows doesn't. Let's eat it. */
1075         if (!(lpKeyState[VK_NUMLOCK] & 0x01)  /* NumLock is off */
1076             && (lpKeyState[VK_SHIFT] & 0x80) /* Shift is pressed */
1077             && (keysym>=XK_KP_0) && (keysym<=XK_KP_9))
1078         {
1079             *(char*)lpChar = 0;
1080             ret = 0;
1081         }
1082     }
1083
1084     TRACE(key, "ToAscii about to return %d with char %x\n",
1085                 ret, *(char*)lpChar);
1086     return ret;
1087 }
1088
1089 #endif /* !defined(X_DISPLAY_MISSING) */
1090
1091