Unify startup sequence for Wine and WineLib.
[wine/testsucceed.git] / windows / x11drv / keyboard.c
blob41696f4f0df6f6c87f5dba9b1b00da8a11123068
1 /*
2 * X11 keyboard driver
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
12 #include "config.h"
14 #ifndef X_DISPLAY_MISSING
16 #include <ctype.h>
17 #include <string.h>
18 #include <X11/Xatom.h>
19 #include <X11/keysym.h>
20 #include "ts_xlib.h"
21 #include "ts_xresource.h"
22 #include "ts_xutil.h"
24 #include "wine/winuser16.h"
25 #include "dinput.h"
26 #include "debugtools.h"
27 #include "keyboard.h"
28 #include "message.h"
29 #include "windef.h"
30 #include "winnls.h"
31 #include "x11drv.h"
33 DECLARE_DEBUG_CHANNEL(key)
34 DECLARE_DEBUG_CHANNEL(keyboard)
35 DECLARE_DEBUG_CHANNEL(x11drv)
37 extern BYTE InputKeyStateTable[256];
39 extern LPBYTE pKeyStateTable;
41 int min_keycode, max_keycode, keysyms_per_keycode;
42 WORD keyc2vkey[256], keyc2scan[256];
44 static int NumLockMask, AltGrMask; /* mask in the XKeyEvent state */
45 static int kcControl, kcAlt, kcShift, kcNumLock, kcCapsLock; /* keycodes */
47 static char KEYBOARD_MapDeadKeysym(KeySym keysym);
49 /* Keyboard translation tables */
50 #define MAIN_LEN 48
51 static const int main_key_scan[MAIN_LEN] =
53 /* this is my (102-key) keyboard layout, sorry if it doesn't quite match yours */
54 0x29,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,
55 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,
56 0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x2B,
57 0x2C,0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
58 0x56 /* the 102nd key (actually to the right of l-shift) */
61 /*** DEFINE YOUR NEW LANGUAGE-SPECIFIC MAPPINGS BELOW, SEE EXISTING TABLES */
63 /* the VK mappings for the main keyboard will be auto-assigned as before,
64 so what we have here is just the character tables */
65 /* order: Normal, Shift, AltGr, Shift-AltGr */
66 /* We recommend you write just what is guaranteed to be correct (i.e. what's
67 written on the keycaps), not the bunch of special characters behind AltGr
68 and Shift-AltGr if it can vary among different X servers */
69 /* Remember that your 102nd key (to the right of l-shift) should be on a
70 separate line, see existing tables */
71 /* If Wine fails to match your new table, use -debugmsg +key to find out why */
72 /* Remember to also add your new table to the layout index table far below! */
74 /*** United States keyboard layout (mostly contributed by Uwe Bonnes) */
75 static const char main_key_US[MAIN_LEN][4] =
77 "`~","1!","2@","3#","4$","5%","6^","7&","8*","9(","0)","-_","=+",
78 "qQ","wW","eE","rR","tT","yY","uU","iI","oO","pP","[{","]}",
79 "aA","sS","dD","fF","gG","hH","jJ","kK","lL",";:","'\"","\\|",
80 "zZ","xX","cC","vV","bB","nN","mM",",<",".>","/?"
83 /*** British keyboard layout */
84 static const char main_key_UK[MAIN_LEN][4] =
86 "`","1!","2\"","3£","4$","5%","6^","7&","8*","9(","0)","-_","=+",
87 "qQ","wW","eE","rR","tT","yY","uU","iI","oO","pP","[{","]}",
88 "aA","sS","dD","fF","gG","hH","jJ","kK","lL",";:","'@","#~",
89 "zZ","xX","cC","vV","bB","nN","mM",",<",".>","/?",
90 "\\|"
93 /*** French keyboard layout (contributed by Eric Pouech) */
94 static const char main_key_FR[MAIN_LEN][4] =
96 "²","&1","é2~","\"3#","'4{","(5[","-6|","è7","_8\\","ç9^±","à0@",")°]","=+}",
97 "aA","zZ","eE","rR","tT","yY","uU","iI","oO","pP","^¨","$£¤",
98 "qQ","sSß","dD","fF","gG","hH","jJ","kK","lL","mM","ù%","*µ",
99 "wW","xX","cC","vV","bB","nN",",?",";.",":/","!§",
100 "<>"
103 /*** Icelandic keyboard layout (contributed by Ríkharður Egilsson) */
104 static const char main_key_IS[MAIN_LEN][4] =
106 "°","1!","2\"","3#","4$","5%","6&","7/{","8([","9)]","0=}","öÖ\\","-_",
107 "qQ@","wW","eE","rR","tT","yY","uU","iI","oO","pP","ðÐ","'?~",
108 "aA","sS","dD","fF","gG","hH","jJ","kK","lL","æÆ","´^","+*`",
109 "zZ","xX","cC","vV","bB","nN","mM",",;",".:","þÞ",
110 "<>|"
113 /*** German keyboard layout (contributed by Ulrich Weigand) */
114 static const char main_key_DE[MAIN_LEN][4] =
116 "^°","1!","2\"","3§","4$","5%","6&","7/{","8([","9)]","0=}","ß?\\","'",
117 "qQ","wW","eE","rR","tT","zZ","uU","iI","oO","pP","üÜ","+*~",
118 "aA","sS","dD","fF","gG","hH","jJ","kK","lL","öÖ","äÄ","#´",
119 "yY","xX","cC","vV","bB","nN","mM",",;",".:","-_",
120 "<>"
123 /*** Swiss German keyboard layout (contributed by Jonathan Naylor) */
124 static const char main_key_SG[MAIN_LEN][4] =
126 "§°","1+|","2\"@","3*#","4ç","5%","6&¬","7/¦","8(¢","9)","0=","'?´","^`~",
127 "qQ","wW","eE","rR","tT","zZ","uU","iI","oO","pP","üè[","¨!]",
128 "aA","sS","dD","fF","gG","hH","jJ","kK","lL","öé","äà{","$£}",
129 "yY","xX","cC","vV","bB","nN","mM",",;",".:","-_",
130 "<>\\"
133 /*** Swiss French keyboard layout (contributed by Philippe Froidevaux) */
134 static const char main_key_SF[MAIN_LEN][4] =
136 "§°","1+|","2\"@","3*#","4ç","5%","6&¬","7/¦","8(¢","9)","0=","'?´","^`~",
137 "qQ","wW","eE","rR","tT","zZ","uU","iI","oO","pP","èü[","¨!]",
138 "aA","sS","dD","fF","gG","hH","jJ","kK","lL","éö","àä{","$£}",
139 "yY","xX","cC","vV","bB","nN","mM",",;",".:","-_",
140 "<>\\"
143 /*** Norwegian keyboard layout (contributed by Ove Kåven) */
144 static const char main_key_NO[MAIN_LEN][4] =
146 "|§","1!","2\"@","3#£","4¤$","5%","6&","7/{","8([","9)]","0=}","+?","\\`´",
147 "qQ","wW","eE","rR","tT","yY","uU","iI","oO","pP","åÅ","¨^~",
148 "aA","sS","dD","fF","gG","hH","jJ","kK","lL","øØ","æÆ","'*",
149 "zZ","xX","cC","vV","bB","nN","mM",",;",".:","-_",
150 "<>"
153 /*** Danish keyboard layout (contributed by Bertho Stultiens) */
154 static const char main_key_DA[MAIN_LEN][4] =
156 "½§","1!","2\"@","3#£","4¤$","5%","6&","7/{","8([","9)]","0=}","+?","´`|",
157 "qQ","wW","eE","rR","tT","yY","uU","iI","oO","pP","åÅ","¨^~",
158 "aA","sS","dD","fF","gG","hH","jJ","kK","lL","æÆ","øØ","'*",
159 "zZ","xX","cC","vV","bB","nN","mM",",;",".:","-_",
160 "<>\\"
163 /*** Swedish keyboard layout (contributed by Peter Bortas) */
164 static const char main_key_SE[MAIN_LEN][4] =
166 "§½","1!","2\"@","3#£","4¤$","5%","6&","7/{","8([","9)]","0=}","+?\\","´`",
167 "qQ","wW","eE","rR","tT","yY","uU","iI","oO","pP","åÅ","¨^~",
168 "aA","sS","dD","fF","gG","hH","jJ","kK","lL","öÖ","äÄ","'*",
169 "zZ","xX","cC","vV","bB","nN","mM",",;",".:","-_",
170 "<>|"
173 /*** Canadian French keyboard layout */
174 static const char main_key_CF[MAIN_LEN][4] =
176 "#|\\","1!±","2\"@","3/£","4$¢","5%¤","6?¬","7&¦","8*²","9(³","0)³","-_½","=+¾",
177 "qQ","wW","eE","rR","tT","yY","uU","iI","oO§","pP¶","^^[","¨]",
178 "aA","sS","dD","fF","gG","hH","jJ","kK","lL",";:~","``{","<>}",
179 "zZ","xX","cC","vV","bB","nN","mM",",'_",".","éÉ",
180 "«»°"
183 /*** Portuguese keyboard layout */
184 static const char main_key_PT[MAIN_LEN][4] =
186 "\\¦","1!","2\"@","3#£","4$§","5%","6&","7/{","8([","9)]","0=}","'?","«»",
187 "qQ", "wW","eE", "rR", "tT", "yY", "uU", "iI", "oO", "pP", "+*\\¨","\\'\\`",
188 "aA", "sS","dD", "fF", "gG", "hH", "jJ", "kK", "lL", "çÇ", "ºª", "\\~\\^",
189 "zZ", "xX","cC", "vV", "bB", "nN", "mM", ",;", ".:", "-_",
190 "<>"
193 /*** Italian keyboard layout */
194 static const char main_key_IT[MAIN_LEN][4] =
196 "\\|","1!1","2\"2","3?3","4$","5%½","6&¾","7/{","8([","9)]","0=}","'?`","i^~",
197 "qQ@","wW","eE","rR?","tT","yY","uU","iI","oOo","pP?","ee[","+*]",
198 "aAae","sS?","dD?","fF","gG","hH","jJ","kK","lL","oc@","a?#","u?",
199 "zZ<","xX>","cC","vV","bB","nN","mM?",",;",".:*","-_","<>|"
202 /*** Finnish keyboard layout */
203 static const char main_key_FI[MAIN_LEN][4] =
205 "","1!","2\"@","3#","4$","5%","6&","7/{","8([","9)]","0=}","+?\\","\'`",
206 "qQ","wW","eE","rR","tT","yY","uU","iI","oO","pP","","\"^~",
207 "aA","sS","dD","fF","gG","hH","jJ","kK","lL","","","'*",
208 "zZ","xX","cC","vV","bB","nN","mM",",;",".:","-_",
209 "<>|"
212 /*** Russian keyboard layout (contributed by Pavel Roskin) */
213 static const char main_key_RU[MAIN_LEN][4] =
215 "`~","1!","2@","3#","4$","5%","6^","7&","8*","9(","0)","-_","=+",
216 "qQÊê","wWÃã","eEÕõ","rRËë","tTÅå","yYÎî","uUÇç","iIÛû","oOÝý","pPÚú","[{Èè","]}ßÿ",
217 "aAÆæ","sSÙù","dD×÷","fFÁá","gGÐð","hHÒò","jJÏï","kKÌì","lLÄä",";:Öö","'\"Üü","\\|",
218 "zZÑñ","xXÞþ","cCÓó","vVÍí","bBÉé","nNÔô","mMØø",",<Ââ",".>Àà","/?"
221 /*** Spanish keyboard layout (contributed by José Marcos López) */
222 static const char main_key_ES[MAIN_LEN][4] =
224 "ºª\\","1!|","2\"@","3·#","4$","5%","6&¬","7/","8(","9)","0=","'?","¡¿",
225 "qQ","wW","eE","rR","tT","yY","uU","iI","oO","pP","`^[","+*]",
226 "aA","sS","dD","fF","gG","hH","jJ","kK","lL","ñÑ","'¨{","çÇ}",
227 "zZ","xX","cC","vV","bB","nN","mM",",;",".:","-_",
228 "<>"
231 /*** Belgian keyboard layout ***/
232 static const char main_key_BE[MAIN_LEN][4] =
234 "","&1|","é2@","\"3#","'4","(5","§6^","è7","!8","ç9{","à0}",")°","-_",
235 "aA","zZ","eE¤","rR","tT","yY","uU","iI","oO","pP","^¨[","$*]",
236 "qQ","sSß","dD","fF","gG","hH","jJ","kK","lL","mM","ù%´","µ£`",
237 "wW","xX","cC","vV","bB","nN",",?",";.",":/","=+~",
238 "<>\\"
241 /*** Layout table. Add your keyboard mappings to this list */
242 static struct {
243 WORD lang, ansi_codepage, oem_codepage;
244 const char (*key)[MAIN_LEN][4];
245 } main_key_tab[]={
246 {MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US), 1252, 437, &main_key_US},
247 {MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_UK), 1252, 850, &main_key_UK},
248 {MAKELANGID(LANG_GERMAN,SUBLANG_DEFAULT), 1252, 850, &main_key_DE},
249 {MAKELANGID(LANG_GERMAN,SUBLANG_GERMAN_SWISS), 1252, 850, &main_key_SG},
250 {MAKELANGID(LANG_SWEDISH,SUBLANG_SWEDISH), 1252, 850, &main_key_SE},
251 {MAKELANGID(LANG_NORWEGIAN,SUBLANG_DEFAULT), 1252, 865, &main_key_NO},
252 {MAKELANGID(LANG_DANISH,SUBLANG_DEFAULT), 1252, 865, &main_key_DA},
253 {MAKELANGID(LANG_FRENCH,SUBLANG_DEFAULT), 1252, 850, &main_key_FR},
254 {MAKELANGID(LANG_FRENCH,SUBLANG_FRENCH_CANADIAN), 1252, 863, &main_key_CF},
255 {MAKELANGID(LANG_FRENCH,SUBLANG_FRENCH_BELGIAN), 1252, 850, &main_key_BE},
256 {MAKELANGID(LANG_FRENCH,SUBLANG_FRENCH_SWISS), 1252, 850, &main_key_SF},
257 {MAKELANGID(LANG_WALON,SUBLANG_DEFAULT), 1252, 850, &main_key_BE},
258 {MAKELANGID(LANG_PORTUGUESE,SUBLANG_DEFAULT), 1252, 860, &main_key_PT},
259 {MAKELANGID(LANG_FINNISH,SUBLANG_DEFAULT), 1252, 850, &main_key_FI},
260 {MAKELANGID(LANG_RUSSIAN,SUBLANG_DEFAULT), 1251, 866, &main_key_RU},
261 {MAKELANGID(LANG_SPANISH,SUBLANG_DEFAULT), 1252, 850, &main_key_ES},
262 {MAKELANGID(LANG_DUTCH,SUBLANG_DUTCH_BELGIAN), 1252, 850, &main_key_BE},
263 {MAKELANGID(LANG_ITALIAN,SUBLANG_DEFAULT), 1252, 850, &main_key_IT},
264 {MAKELANGID(LANG_ICELANDIC,SUBLANG_DEFAULT), 1252, 850, &main_key_IS},
266 {0} /* sentinel */
268 static unsigned kbd_layout=0; /* index into above table of layouts */
270 /* maybe more of these scancodes should be extended? */
271 /* extended must be set for ALT_R, CTRL_R,
272 INS, DEL, HOME, END, PAGE_UP, PAGE_DOWN, ARROW keys,
273 keypad / and keypad ENTER (SDK 3.1 Vol.3 p 138) */
274 /* FIXME should we set extended bit for NumLock ? My
275 * Windows does ... DF */
276 static const int special_key_vkey[] =
278 VK_BACK, VK_TAB, 0, VK_CLEAR, 0, VK_RETURN, 0, 0, /* FF08 */
279 0, 0, 0, VK_PAUSE, VK_SCROLL, 0, 0, 0, /* FF10 */
280 0, 0, 0, VK_ESCAPE /* FF18 */
282 static const int special_key_scan[] =
284 0x0E, 0x0F, 0, /*?*/ 0, 0, 0x1C, 0, 0, /* FF08 */
285 0, 0, 0, 0x45, 0x46, 0 , 0, 0, /* FF10 */
286 0, 0, 0, 0x01 /* FF18 */
289 static const int cursor_key_vkey[] =
291 VK_HOME, VK_LEFT, VK_UP, VK_RIGHT, VK_DOWN, VK_PRIOR,
292 VK_NEXT, VK_END /* FF50 */
294 static const int cursor_key_scan[] =
296 0x147, 0x14B, 0x148, 0x14D, 0x150, 0x149, 0x151, 0x14F /* FF50 */
299 static const int misc_key_vkey[] =
301 VK_SELECT, VK_SNAPSHOT, VK_EXECUTE, VK_INSERT, 0, 0, 0, 0, /* FF60 */
302 VK_CANCEL, VK_HELP, VK_CANCEL, VK_MENU /* FF68 */
304 static const int misc_key_scan[] =
306 /*?*/ 0, 0x37, /*?*/ 0, 0x152, 0, 0, 0, 0, /* FF60 */
307 /*?*/ 0, /*?*/ 0, 0x38 /* FF68 */
310 static const int keypad_key_vkey[] =
312 0, VK_NUMLOCK, /* FF7E */
313 0, 0, 0, 0, 0, 0, 0, 0, /* FF80 */
314 0, 0, 0, 0, 0, VK_RETURN, 0, 0, /* FF88 */
315 0, 0, 0, 0, 0, VK_HOME, VK_LEFT, VK_UP, /* FF90 */
316 VK_RIGHT, VK_DOWN, VK_PRIOR, VK_NEXT, VK_END, 0,
317 VK_INSERT, VK_DELETE, /* FF98 */
318 0, 0, 0, 0, 0, 0, 0, 0, /* FFA0 */
319 0, 0, VK_MULTIPLY, VK_ADD, VK_SEPARATOR, VK_SUBTRACT,
320 VK_DECIMAL, VK_DIVIDE, /* FFA8 */
321 VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3, VK_NUMPAD4,
322 VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, /* FFB0 */
323 VK_NUMPAD8, VK_NUMPAD9 /* FFB8 */
325 static const int keypad_key_scan[] =
327 0x138, 0x45, /* FF7E */
328 0, 0, 0, 0, 0, 0, 0, 0, /* FF80 */
329 0, 0, 0, 0, 0, 0x11C, 0, 0, /* FF88 */
330 0, 0, 0, 0, 0, 0x47, 0x4B, 0x48, /* FF90 */
331 0x4D, 0x50, 0x49, 0x51, 0x4F, 0x4C, 0x52, 0x53, /* FF98 */
332 0, 0, 0, 0, 0, 0, 0, 0, /* FFA0 */
333 0, 0, 0x37, 0x4E, /*?*/ 0, 0x4A, 0x53, 0x135, /* FFA8 */
334 0x52, 0x4F, 0x50, 0x51, 0x4B, 0x4C, 0x4D, 0x47, /* FFB0 */
335 0x48, 0x49 /* FFB8 */
338 static const int function_key_vkey[] =
340 VK_F1, VK_F2, /* FFBE */
341 VK_F3, VK_F4, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9, VK_F10, /* FFC0 */
342 VK_F11, VK_F12, VK_F13, VK_F14, VK_F15, VK_F16 /* FFC8 */
344 static const int function_key_scan[] =
346 0x3B, 0x3C, /* FFBE */
347 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, /* FFC0 */
348 0x57, 0x58, 0, 0, 0, 0 /* FFC8 */
351 static const int modifier_key_vkey[] =
353 VK_SHIFT, VK_SHIFT, VK_CONTROL, VK_CONTROL, VK_CAPITAL, 0, /* FFE1 */
354 VK_MENU, VK_MENU, VK_MENU, VK_MENU /* FFE7 */
356 static const int modifier_key_scan[] =
358 0x2A, 0x36, 0x1D, 0x11D, 0x3A, 0, /* FFE1 */
359 0x38, 0x138, 0x38, 0x138 /* FFE7 */
362 /* Returns the Windows virtual key code associated with the X event <e> */
363 static WORD EVENT_event_to_vkey( XKeyEvent *e)
365 KeySym keysym;
367 TSXLookupString(e, NULL, 0, &keysym, NULL);
369 if ((keysym >= 0xFFAE) && (keysym <= 0xFFB9) && (keysym != 0xFFAF)
370 && (e->state & NumLockMask))
371 /* Only the Keypad keys 0-9 and . send different keysyms
372 * depending on the NumLock state */
373 return keypad_key_vkey[(keysym & 0xFF) - 0x7E];
375 return keyc2vkey[e->keycode];
378 static BOOL NumState=FALSE, CapsState=FALSE;
380 /**********************************************************************
381 * KEYBOARD_GenerateMsg
383 * Generate Down+Up messages when NumLock or CapsLock is pressed.
385 * Convention : called with vkey only VK_NUMLOCK or VK_CAPITAL
388 static void KEYBOARD_GenerateMsg( WORD vkey, WORD scan, int Evtype, INT event_x, INT event_y,
389 DWORD event_time )
391 BOOL * State = (vkey==VK_NUMLOCK? &NumState : &CapsState);
392 DWORD up, down;
394 if (*State) {
395 /* The INTERMEDIARY state means : just after a 'press' event, if a 'release' event comes,
396 don't treat it. It's from the same key press. Then the state goes to ON.
397 And from there, a 'release' event will switch off the toggle key. */
398 *State=FALSE;
399 TRACE_(keyboard)("INTERM : don\'t treat release of toggle key. InputKeyStateTable[%#x] = %#x\n",vkey,pKeyStateTable[vkey]);
400 } else
402 down = (vkey==VK_NUMLOCK ? KEYEVENTF_EXTENDEDKEY : 0);
403 up = (vkey==VK_NUMLOCK ? KEYEVENTF_EXTENDEDKEY : 0) | KEYEVENTF_KEYUP;
404 if ( pKeyStateTable[vkey] & 0x1 ) /* it was ON */
406 if (Evtype!=KeyPress)
408 TRACE_(keyboard)("ON + KeyRelease => generating DOWN and UP messages.\n");
409 KEYBOARD_SendEvent( vkey, scan, down,
410 event_x, event_y, event_time );
411 KEYBOARD_SendEvent( vkey, scan, up,
412 event_x, event_y, event_time );
413 *State=FALSE;
414 pKeyStateTable[vkey] &= ~0x01; /* Toggle state to off. */
417 else /* it was OFF */
418 if (Evtype==KeyPress)
420 TRACE_(keyboard)("OFF + Keypress => generating DOWN and UP messages.\n");
421 KEYBOARD_SendEvent( vkey, scan, down,
422 event_x, event_y, event_time );
423 KEYBOARD_SendEvent( vkey, scan, up,
424 event_x, event_y, event_time );
425 *State=TRUE; /* Goes to intermediary state before going to ON */
426 pKeyStateTable[vkey] |= 0x01; /* Toggle state to on. */
431 /***********************************************************************
432 * KEYBOARD_UpdateOneState
434 * Updates internal state for <vkey>, depending on key <state> under X
437 static void KEYBOARD_UpdateOneState ( int vkey, int state )
439 /* Do something if internal table state != X state for keycode */
440 if (((pKeyStateTable[vkey] & 0x80)!=0) != state)
442 TRACE_(keyboard)("Adjusting state for vkey %#.2x. State before %#.2x \n",
443 vkey, pKeyStateTable[vkey]);
445 /* Fake key being pressed inside wine */
446 KEYBOARD_SendEvent( vkey, 0, state? 0 : KEYEVENTF_KEYUP,
447 0, 0, GetTickCount() );
449 TRACE_(keyboard)("State after %#.2x \n",pKeyStateTable[vkey]);
453 /***********************************************************************
454 * X11DRV_KEYBOARD_UpdateState
456 * Update modifiers state (Ctrl, Alt, Shift)
457 * when window is activated (called by EVENT_FocusIn in event.c)
459 * This handles the case where one uses Ctrl+... Alt+... or Shift+.. to switch
460 * from wine to another application and back.
461 * Toggle keys are handled in HandleEvent. (because XQueryKeymap says nothing
462 * about them)
464 void X11DRV_KEYBOARD_UpdateState ( void )
466 /* extract a bit from the char[32] bit suite */
467 #define KeyState(keycode) ((keys_return[keycode/8] & (1<<(keycode%8)))!=0)
469 char keys_return[32];
471 TRACE_(keyboard)("called\n");
472 if (!TSXQueryKeymap(display, keys_return)) {
473 ERR_(keyboard)("Error getting keymap !");
474 return;
477 /* Adjust the ALT and CONTROL state if any has been changed outside wine */
478 KEYBOARD_UpdateOneState(VK_MENU, KeyState(kcAlt));
479 KEYBOARD_UpdateOneState(VK_CONTROL, KeyState(kcControl));
480 KEYBOARD_UpdateOneState(VK_SHIFT, KeyState(kcShift));
481 #undef KeyState
484 /***********************************************************************
485 * X11DRV_KEYBOARD_HandleEvent
487 * Handle a X key event
489 void X11DRV_KEYBOARD_HandleEvent( WND *pWnd, XKeyEvent *event )
491 char Str[24];
492 XComposeStatus cs;
493 KeySym keysym;
494 WORD vkey = 0, bScan;
495 DWORD dwFlags;
496 static BOOL force_extended = FALSE; /* hack for AltGr translation */
498 int ascii_chars;
500 INT event_x = (pWnd? pWnd->rectWindow.left : 0) + event->x;
501 INT event_y = (pWnd? pWnd->rectWindow.top : 0) + event->y;
502 DWORD event_time = event->time - MSG_WineStartTicks;
504 /* this allows support for dead keys */
505 if ((event->keycode >> 8) == 0x10)
506 event->keycode=(event->keycode & 0xff);
508 ascii_chars = TSXLookupString(event, Str, 1, &keysym, &cs);
510 TRACE_(key)("EVENT_key : state = %X\n", event->state);
511 if (keysym == XK_Mode_switch)
513 TRACE_(key)("Alt Gr key event received\n");
514 event->keycode = kcControl; /* Simulate Control */
515 X11DRV_KEYBOARD_HandleEvent( pWnd, event );
517 event->keycode = kcAlt; /* Simulate Alt */
518 force_extended = TRUE;
519 X11DRV_KEYBOARD_HandleEvent( pWnd, event );
520 force_extended = FALSE;
521 return;
524 Str[ascii_chars] = '\0';
525 if (TRACE_ON(key)){
526 char *ksname;
528 ksname = TSXKeysymToString(keysym);
529 if (!ksname)
530 ksname = "No Name";
531 TRACE_(key)("%s : keysym=%lX (%s), ascii chars=%u / %X / '%s'\n",
532 (event->type == KeyPress) ? "KeyPress" : "KeyRelease",
533 keysym, ksname, ascii_chars, Str[0] & 0xff, Str);
536 vkey = EVENT_event_to_vkey(event);
537 if (force_extended) vkey |= 0x100;
539 TRACE_(key)("keycode 0x%x converted to vkey 0x%x\n",
540 event->keycode, vkey);
542 if (vkey)
544 switch (vkey & 0xff)
546 case VK_NUMLOCK:
547 KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, event->type, event_x, event_y,
548 event_time );
549 break;
550 case VK_CAPITAL:
551 TRACE_(keyboard)("Caps Lock event. (type %d). State before : %#.2x\n",event->type,pKeyStateTable[vkey]);
552 KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, event->type, event_x, event_y,
553 event_time );
554 TRACE_(keyboard)("State after : %#.2x\n",pKeyStateTable[vkey]);
555 break;
556 default:
557 /* Adjust the NUMLOCK state if it has been changed outside wine */
558 if (!(pKeyStateTable[VK_NUMLOCK] & 0x01) != !(event->state & NumLockMask))
560 TRACE_(keyboard)("Adjusting NumLock state. \n");
561 KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, KeyPress, event_x, event_y,
562 event_time );
563 KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, KeyRelease, event_x, event_y,
564 event_time );
566 /* Adjust the CAPSLOCK state if it has been changed outside wine */
567 if (!(pKeyStateTable[VK_CAPITAL] & 0x01) != !(event->state & LockMask))
569 TRACE_(keyboard)("Adjusting Caps Lock state.\n");
570 KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, KeyPress, event_x, event_y,
571 event_time );
572 KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, KeyRelease, event_x, event_y,
573 event_time );
575 /* Not Num nor Caps : end of intermediary states for both. */
576 NumState = FALSE;
577 CapsState = FALSE;
579 bScan = keyc2scan[event->keycode] & 0xFF;
580 TRACE_(key)("bScan = 0x%02x.\n", bScan);
582 dwFlags = 0;
583 if ( event->type == KeyRelease ) dwFlags |= KEYEVENTF_KEYUP;
584 if ( vkey & 0x100 ) dwFlags |= KEYEVENTF_EXTENDEDKEY;
585 if ( force_extended ) dwFlags |= KEYEVENTF_WINE_FORCEEXTENDED;
587 KEYBOARD_SendEvent( vkey & 0xff, bScan, dwFlags,
588 event_x, event_y, event_time );
593 /**********************************************************************
594 * X11DRV_KEYBOARD_DetectLayout
596 * Called from X11DRV_KEYBOARD_Init
597 * This routine walks through the defined keyboard layouts and selects
598 * whichever matches most closely.
600 static void
601 X11DRV_KEYBOARD_DetectLayout (void)
603 unsigned current, match, mismatch, seq;
604 int score, keyc, i, key, pkey, ok, syms;
605 KeySym keysym;
606 const char (*lkey)[MAIN_LEN][4];
607 unsigned max_seq = 0;
608 int max_score = 0, ismatch = 0;
609 char ckey[4] =
610 {0, 0, 0, 0};
612 syms = keysyms_per_keycode;
613 if (syms > 4) {
614 WARN_(keyboard)("%d keysyms per keycode not supported, set to 4", syms);
615 syms = 4;
617 for (current = 0; main_key_tab[current].lang; current++) {
618 TRACE_(keyboard)("Attempting to match against layout %04x\n",
619 main_key_tab[current].lang);
620 match = 0;
621 mismatch = 0;
622 score = 0;
623 seq = 0;
624 lkey = main_key_tab[current].key;
625 pkey = -1;
626 for (keyc = min_keycode; keyc <= max_keycode; keyc++) {
627 /* get data for keycode from X server */
628 for (i = 0; i < syms; i++) {
629 keysym = TSXKeycodeToKeysym (display, keyc, i);
630 /* Allow both one-byte and two-byte national keysyms */
631 if ((keysym < 0x800) && (keysym != ' '))
632 ckey[i] = keysym & 0xFF;
633 else {
634 ckey[i] = KEYBOARD_MapDeadKeysym(keysym);
637 if (ckey[0]) {
638 /* search for a match in layout table */
639 /* right now, we just find an absolute match for defined positions */
640 /* (undefined positions are ignored, so if it's defined as "3#" in */
641 /* the table, it's okay that the X server has "3#£", for example) */
642 /* however, the score will be higher for longer matches */
643 for (key = 0; key < MAIN_LEN; key++) {
644 for (ok = 0, i = 0; (ok >= 0) && (i < syms); i++) {
645 if ((*lkey)[key][i] && ((*lkey)[key][i] == ckey[i]))
646 ok++;
647 if ((*lkey)[key][i] && ((*lkey)[key][i] != ckey[i]))
648 ok = -1;
650 if (ok > 0) {
651 score += ok;
652 break;
655 /* count the matches and mismatches */
656 if (ok > 0) {
657 match++;
658 /* and how much the keycode order matches */
659 if (key > pkey) seq++;
660 pkey = key;
661 } else {
662 TRACE_(key)("mismatch for keycode %d, character %c\n", keyc,
663 ckey[0]);
664 mismatch++;
665 score -= syms;
669 TRACE_(keyboard)("matches=%d, mismatches=%d, score=%d\n",
670 match, mismatch, score);
671 if ((score > max_score) ||
672 ((score == max_score) && (seq > max_seq))) {
673 /* best match so far */
674 kbd_layout = current;
675 max_score = score;
676 max_seq = seq;
677 ismatch = !mismatch;
680 /* we're done, report results if necessary */
681 if (!ismatch) {
682 FIXME_(keyboard)(
683 "Your keyboard layout was not found!\n"
684 "Instead using closest match (%04x) for scancode mapping.\n"
685 "Please define your layout in windows/x11drv/keyboard.c and submit them\n"
686 "to us for inclusion into future Wine releases.\n"
687 "See documentation/keyboard for more information.\n",
688 main_key_tab[kbd_layout].lang);
690 TRACE_(keyboard)("detected layout is %04x\n", main_key_tab[kbd_layout].lang);
693 /**********************************************************************
694 * X11DRV_KEYBOARD_Init
696 void X11DRV_KEYBOARD_Init(void)
698 KeySym *ksp;
699 XModifierKeymap *mmp;
700 KeySym keysym;
701 KeyCode *kcp;
702 XKeyEvent e2;
703 WORD scan, vkey, OEMvkey;
704 int keyc, i, keyn, syms;
705 char ckey[4]={0,0,0,0};
706 const char (*lkey)[MAIN_LEN][4];
708 TSXDisplayKeycodes(display, &min_keycode, &max_keycode);
709 ksp = TSXGetKeyboardMapping(display, min_keycode,
710 max_keycode + 1 - min_keycode, &keysyms_per_keycode);
711 /* We are only interested in keysyms_per_keycode.
712 There is no need to hold a local copy of the keysyms table */
713 TSXFree(ksp);
714 mmp = TSXGetModifierMapping(display);
715 kcp = mmp->modifiermap;
716 for (i = 0; i < 8; i += 1) /* There are 8 modifier keys */
718 int j;
720 for (j = 0; j < mmp->max_keypermod; j += 1, kcp += 1)
721 if (*kcp)
723 int k;
725 for (k = 0; k < keysyms_per_keycode; k += 1)
726 if (TSXKeycodeToKeysym(display, *kcp, k) == XK_Mode_switch)
728 AltGrMask = 1 << i;
729 TRACE_(key)("AltGrMask is %x\n", AltGrMask);
731 else if (TSXKeycodeToKeysym(display, *kcp, k) == XK_Num_Lock)
733 NumLockMask = 1 << i;
734 TRACE_(key)("NumLockMask is %x\n", NumLockMask);
738 TSXFreeModifiermap(mmp);
740 /* Detect the keyboard layout */
741 X11DRV_KEYBOARD_DetectLayout();
742 lkey = main_key_tab[kbd_layout].key;
743 syms = (keysyms_per_keycode > 4) ? 4 : keysyms_per_keycode;
745 /* Now build two conversion arrays :
746 * keycode -> vkey + scancode + extended
747 * vkey + extended -> keycode */
749 e2.display = display;
750 e2.state = 0;
752 OEMvkey = VK_OEM_7; /* next is available. */
753 for (keyc = min_keycode; keyc <= max_keycode; keyc++)
755 e2.keycode = (KeyCode)keyc;
756 TSXLookupString(&e2, NULL, 0, &keysym, NULL);
757 vkey = 0; scan = 0;
758 if (keysym) /* otherwise, keycode not used */
760 if ((keysym >> 8) == 0xFF) /* non-character key */
762 int key = keysym & 0xff;
764 if (key >= 0x08 && key <= 0x1B) { /* special key */
765 vkey = special_key_vkey[key - 0x08];
766 scan = special_key_scan[key - 0x08];
767 } else if (key >= 0x50 && key <= 0x57) { /* cursor key */
768 vkey = cursor_key_vkey[key - 0x50];
769 scan = cursor_key_scan[key - 0x50];
770 } else if (key >= 0x60 && key <= 0x6B) { /* miscellaneous key */
771 vkey = misc_key_vkey[key - 0x60];
772 scan = misc_key_scan[key - 0x60];
773 } else if (key >= 0x7E && key <= 0xB9) { /* keypad key */
774 vkey = keypad_key_vkey[key - 0x7E];
775 scan = keypad_key_scan[key - 0x7E];
776 } else if (key >= 0xBE && key <= 0xCD) { /* function key */
777 vkey = function_key_vkey[key - 0xBE] | 0x100; /* set extended bit */
778 scan = function_key_scan[key - 0xBE];
779 } else if (key >= 0xE1 && key <= 0xEA) { /* modifier key */
780 vkey = modifier_key_vkey[key - 0xE1];
781 scan = modifier_key_scan[key - 0xE1];
782 } else if (key == 0xFF) { /* DEL key */
783 vkey = VK_DELETE;
784 scan = 0x153;
786 /* set extended bit when necessary */
787 if (scan & 0x100) vkey |= 0x100;
788 } else if (keysym == 0x20) { /* Spacebar */
789 vkey = VK_SPACE;
790 scan = 0x39;
791 } else {
792 /* we seem to need to search the layout-dependent scancodes */
793 int maxlen=0,maxval=-1,ok;
794 for (i=0; i<syms; i++) {
795 keysym = TSXKeycodeToKeysym(display, keyc, i);
796 if ((keysym<0x800) && (keysym!=' ')) {
797 ckey[i] = keysym & 0xFF;
798 } else {
799 ckey[i] = KEYBOARD_MapDeadKeysym(keysym);
802 /* find key with longest match streak */
803 for (keyn=0; keyn<MAIN_LEN; keyn++) {
804 for (ok=(*lkey)[keyn][i=0]; ok&&(i<4); i++)
805 if ((*lkey)[keyn][i] && (*lkey)[keyn][i]!=ckey[i]) ok=0;
806 if (ok||(i>maxlen)) {
807 maxlen=i; maxval=keyn;
809 if (ok) break;
811 if (maxval>=0) {
812 /* got it */
813 scan = main_key_scan[maxval];
817 /* find a suitable layout-dependent VK code */
818 /* (most Winelib apps ought to be able to work without layout tables!) */
819 for (i = 0; (i < keysyms_per_keycode) && (!vkey); i++)
821 keysym = TSXLookupKeysym(&e2, i);
822 if ((keysym >= VK_0 && keysym <= VK_9)
823 || (keysym >= VK_A && keysym <= VK_Z)) {
824 vkey = keysym;
828 for (i = 0; (i < keysyms_per_keycode) && (!vkey); i++)
830 keysym = TSXLookupKeysym(&e2, i);
831 switch (keysym)
833 case ';': vkey = VK_OEM_1; break;
834 case '/': vkey = VK_OEM_2; break;
835 case '`': vkey = VK_OEM_3; break;
836 case '[': vkey = VK_OEM_4; break;
837 case '\\': vkey = VK_OEM_5; break;
838 case ']': vkey = VK_OEM_6; break;
839 case '\'': vkey = VK_OEM_7; break;
840 case ',': vkey = VK_OEM_COMMA; break;
841 case '.': vkey = VK_OEM_PERIOD; break;
842 case '-': vkey = VK_OEM_MINUS; break;
843 case '+': vkey = VK_OEM_PLUS; break;
847 if (!vkey)
849 /* Others keys: let's assign OEM virtual key codes in the allowed range,
850 * that is ([0xba,0xc0], [0xdb,0xe4], 0xe6 (given up) et [0xe9,0xf5]) */
851 switch (++OEMvkey)
853 case 0xc1 : OEMvkey=0xdb; break;
854 case 0xe5 : OEMvkey=0xe9; break;
855 case 0xf6 : OEMvkey=0xf5; WARN_(keyboard)("No more OEM vkey available!\n");
858 vkey = OEMvkey;
860 if (TRACE_ON(keyboard))
862 dbg_decl_str(keyboard, 1024);
864 TRACE_(keyboard)("OEM specific virtual key %X assigned "
865 "to keycode %X:\n", OEMvkey, e2.keycode);
866 for (i = 0; i < keysyms_per_keycode; i += 1)
868 char *ksname;
870 keysym = TSXLookupKeysym(&e2, i);
871 ksname = TSXKeysymToString(keysym);
872 if (!ksname)
873 ksname = "NoSymbol";
874 dsprintf(keyboard, "%lX (%s) ", keysym, ksname);
876 TRACE_(keyboard)("(%s)\n", dbg_str(keyboard));
880 keyc2vkey[e2.keycode] = vkey;
881 keyc2scan[e2.keycode] = scan;
882 } /* for */
884 /* If some keys still lack scancodes, assign some arbitrary ones to them now */
885 for (scan = 0x60, keyc = min_keycode; keyc <= max_keycode; keyc++)
886 if (keyc2vkey[keyc]&&!keyc2scan[keyc]) {
887 char *ksname;
888 keysym = TSXKeycodeToKeysym(display, keyc, 0);
889 ksname = TSXKeysymToString(keysym);
890 if (!ksname) ksname = "NoSymbol";
892 /* should make sure the scancode is unassigned here, but >=0x60 currently always is */
894 TRACE_(key)("assigning scancode %02x to unidentified keycode %02x (%s)\n",scan,keyc,ksname);
895 keyc2scan[keyc]=scan++;
898 /* Now store one keycode for each modifier. Used to simulate keypresses. */
899 kcControl = TSXKeysymToKeycode(display, XK_Control_L);
900 kcAlt = TSXKeysymToKeycode(display, XK_Alt_L);
901 if (!kcAlt) kcAlt = TSXKeysymToKeycode(display, XK_Meta_L);
902 kcShift = TSXKeysymToKeycode(display, XK_Shift_L);
903 kcNumLock = TSXKeysymToKeycode(display, XK_Num_Lock);
904 kcCapsLock = TSXKeysymToKeycode(display, XK_Caps_Lock);
907 /***********************************************************************
908 * X11DRV_KEYBOARD_VkKeyScan
910 WORD X11DRV_KEYBOARD_VkKeyScan(CHAR cChar)
912 KeyCode keycode;
913 KeySym keysym;
914 int i,index;
915 int highbyte=0;
917 /* char->keysym (same for ANSI chars) */
918 keysym=(unsigned char) cChar;/* (!) cChar is signed */
919 if (keysym<=27) keysym+=0xFF00;/*special chars : return, backspace...*/
921 keycode = TSXKeysymToKeycode(display, keysym); /* keysym -> keycode */
922 if (!keycode)
923 { /* It didn't work ... let's try with deadchar code. */
924 keycode = TSXKeysymToKeycode(display, keysym | 0xFE00);
927 TRACE_(keyboard)("VkKeyScan '%c'(%#lx, %lu): got keycode %#.2x\n",
928 cChar,keysym,keysym,keycode);
930 if (keycode)
932 for (index=-1, i=0; (i<8) && (index<0); i++) /* find shift state */
933 if (TSXKeycodeToKeysym(display,keycode,i)==keysym) index=i;
934 switch (index) {
935 case -1 :
936 WARN_(keyboard)("Keysym %lx not found while parsing the keycode table\n",keysym); break;
937 case 0 : break;
938 case 1 : highbyte = 0x0100; break;
939 case 2 : highbyte = 0x0600; break;
940 case 3 : highbyte = 0x0700; break;
941 default : ERR_(keyboard)("index %d found by XKeycodeToKeysym. please report! \n",index);
944 index : 0 adds 0x0000
945 index : 1 adds 0x0100 (shift)
946 index : ? adds 0x0200 (ctrl)
947 index : 2 adds 0x0600 (ctrl+alt)
948 index : 3 adds 0x0700 (ctrl+alt+shift)
951 TRACE_(keyboard)(" ... returning %#.2x\n", keyc2vkey[keycode]+highbyte);
952 return keyc2vkey[keycode]+highbyte; /* keycode -> (keyc2vkey) vkey */
955 /***********************************************************************
956 * X11DRV_KEYBOARD_MapVirtualKey
958 UINT16 X11DRV_KEYBOARD_MapVirtualKey(UINT16 wCode, UINT16 wMapType)
960 #define returnMVK(value) { TRACE_(keyboard)("returning 0x%x.\n",value); return value; }
962 TRACE_(keyboard)("MapVirtualKey wCode=0x%x wMapType=%d ... \n",
963 wCode,wMapType);
964 switch(wMapType) {
965 case 0: { /* vkey-code to scan-code */
966 /* let's do vkey -> keycode -> scan */
967 int keyc;
968 for (keyc=min_keycode; keyc<=max_keycode; keyc++)
969 if ((keyc2vkey[keyc] & 0xFF) == wCode)
970 returnMVK (keyc2scan[keyc] & 0xFF);
971 TRACE_(keyboard)("returning no scan-code.\n");
972 return 0; }
974 case 1: { /* scan-code to vkey-code */
975 /* let's do scan -> keycode -> vkey */
976 int keyc;
977 for (keyc=min_keycode; keyc<=max_keycode; keyc++)
978 if ((keyc2scan[keyc] & 0xFF) == (wCode & 0xFF))
979 returnMVK (keyc2vkey[keyc] & 0xFF);
980 TRACE_(keyboard)("returning no vkey-code.\n");
981 return 0; }
983 case 2: { /* vkey-code to unshifted ANSI code */
984 /* (was FIXME) : what does unshifted mean ? 'a' or 'A' ? */
985 /* My Windows returns 'A'. */
986 /* let's do vkey -> keycode -> (XLookupString) ansi char */
987 XKeyEvent e;
988 KeySym keysym;
989 int keyc;
990 char s[2];
991 e.display = display;
992 e.state = 0; /* unshifted */
994 e.keycode = 0;
995 /* We exit on the first keycode found, to speed up the thing. */
996 for (keyc=min_keycode; (keyc<=max_keycode) && (!e.keycode) ; keyc++)
997 { /* Find a keycode that could have generated this virtual key */
998 if ((keyc2vkey[keyc] & 0xFF) == wCode)
999 { /* We filter the extended bit, we don't know it */
1000 e.keycode = keyc; /* Store it temporarily */
1001 if ((EVENT_event_to_vkey(&e) & 0xFF) != wCode) {
1002 e.keycode = 0; /* Wrong one (ex: because of the NumLock
1003 state), so set it to 0, we'll find another one */
1008 if ((wCode>=VK_NUMPAD0) && (wCode<=VK_NUMPAD9))
1009 e.keycode = TSXKeysymToKeycode(e.display, wCode-VK_NUMPAD0+XK_KP_0);
1011 if (wCode==VK_DECIMAL)
1012 e.keycode = TSXKeysymToKeycode(e.display, XK_KP_Decimal);
1014 if (!e.keycode)
1016 WARN_(keyboard)("Unknown virtual key %X !!! \n", wCode);
1017 return 0; /* whatever */
1019 TRACE_(keyboard)("Found keycode %d (0x%2X)\n",e.keycode,e.keycode);
1021 if (TSXLookupString(&e, s, 2, &keysym, NULL))
1022 returnMVK (*s);
1024 TRACE_(keyboard)("returning no ANSI.\n");
1025 return 0;
1028 case 3: /* **NT only** scan-code to vkey-code but distinguish between */
1029 /* left and right */
1030 FIXME_(keyboard)(" stub for NT\n");
1031 return 0;
1033 default: /* reserved */
1034 WARN_(keyboard)("Unknown wMapType %d !\n",
1035 wMapType);
1036 return 0;
1038 return 0;
1041 /***********************************************************************
1042 * X11DRV_KEYBOARD_GetKeyNameText
1044 INT16 X11DRV_KEYBOARD_GetKeyNameText(LONG lParam, LPSTR lpBuffer, INT16 nSize)
1046 int vkey, ansi, scanCode;
1048 scanCode = lParam >> 16;
1049 scanCode &= 0x1ff; /* keep "extended-key" flag with code */
1051 /* FIXME: should use MVK type 3 (NT version that distinguishes right and left */
1052 vkey = X11DRV_KEYBOARD_MapVirtualKey(scanCode, 1);
1054 /* handle "don't care" bit (0x02000000) */
1055 if (!(lParam & 0x02000000)) {
1056 switch (vkey) {
1057 case VK_LSHIFT:
1058 case VK_RSHIFT:
1059 vkey = VK_SHIFT;
1060 break;
1061 case VK_LCONTROL:
1062 case VK_RCONTROL:
1063 vkey = VK_CONTROL;
1064 break;
1065 case VK_LMENU:
1066 case VK_RMENU:
1067 vkey = VK_MENU;
1068 break;
1069 default:
1070 break;
1074 ansi = X11DRV_KEYBOARD_MapVirtualKey(vkey, 2);
1075 TRACE_(keyboard)("scan 0x%04x, vkey 0x%04x, ANSI 0x%04x\n",
1076 scanCode, vkey, ansi);
1078 if ( ((vkey >= 0x30) && (vkey <= 0x39)) ||
1079 ( (vkey >= 0x41) && (vkey <= 0x5a)) ) /* Windows VK_* are ANSI codes */
1081 if ((nSize >= 2) && lpBuffer)
1083 *lpBuffer = toupper((char)ansi);
1084 *(lpBuffer+1) = 0;
1085 return 1;
1087 else
1088 return 0;
1091 /* use vkey values to construct names */
1093 FIXME_(keyboard)("(%08lx,%p,%d): unsupported key\n",lParam,lpBuffer,nSize);
1095 if (lpBuffer && nSize)
1096 *lpBuffer = 0;
1097 return 0;
1100 /***********************************************************************
1101 * X11DRV_KEYBOARD_MapDeadKeysym
1103 static char KEYBOARD_MapDeadKeysym(KeySym keysym)
1105 switch (keysym)
1107 /* symbolic ASCII is the same as defined in rfc1345 */
1108 #ifdef XK_dead_tilde
1109 case XK_dead_tilde :
1110 #endif
1111 case 0x1000FE7E : /* Xfree's XK_Dtilde */
1112 return '~'; /* '? */
1113 #ifdef XK_dead_acute
1114 case XK_dead_acute :
1115 #endif
1116 case 0x1000FE27 : /* Xfree's XK_Dacute_accent */
1117 return 0xb4; /* '' */
1118 #ifdef XK_dead_circumflex
1119 case XK_dead_circumflex:
1120 #endif
1121 case 0x1000FE5E : /* Xfree's XK_Dcircumflex_accent */
1122 return '^'; /* '> */
1123 #ifdef XK_dead_grave
1124 case XK_dead_grave :
1125 #endif
1126 case 0x1000FE60 : /* Xfree's XK_Dgrave_accent */
1127 return '`'; /* '! */
1128 #ifdef XK_dead_diaeresis
1129 case XK_dead_diaeresis :
1130 #endif
1131 case 0x1000FE22 : /* Xfree's XK_Ddiaeresis */
1132 return 0xa8; /* ': */
1133 #ifdef XK_dead_cedilla
1134 case XK_dead_cedilla :
1135 return 0xb8; /* ', */
1136 #endif
1137 #ifdef XK_dead_macron
1138 case XK_dead_macron :
1139 return '-'; /* 'm isn't defined on iso-8859-x */
1140 #endif
1141 #ifdef XK_dead_breve
1142 case XK_dead_breve :
1143 return 0xa2; /* '( */
1144 #endif
1145 #ifdef XK_dead_abovedot
1146 case XK_dead_abovedot :
1147 return 0xff; /* '. */
1148 #endif
1149 #ifdef XK_dead_abovering
1150 case XK_dead_abovering :
1151 return '0'; /* '0 isn't defined on iso-8859-x */
1152 #endif
1153 #ifdef XK_dead_doubleacute
1154 case XK_dead_doubleacute :
1155 return 0xbd; /* '" */
1156 #endif
1157 #ifdef XK_dead_caron
1158 case XK_dead_caron :
1159 return 0xb7; /* '< */
1160 #endif
1161 #ifdef XK_dead_ogonek
1162 case XK_dead_ogonek :
1163 return 0xb2; /* '; */
1164 #endif
1165 /* FIXME: I don't know this three.
1166 case XK_dead_iota :
1167 return 'i';
1168 case XK_dead_voiced_sound :
1169 return 'v';
1170 case XK_dead_semivoiced_sound :
1171 return 's';
1174 TRACE_(keyboard)("no character for dead keysym 0x%08lx\n",keysym);
1175 return 0;
1178 /***********************************************************************
1179 * X11DRV_KEYBOARD_ToAscii
1181 * The ToAscii function translates the specified virtual-key code and keyboard
1182 * state to the corresponding Windows character or characters.
1184 * If the specified key is a dead key, the return value is negative. Otherwise,
1185 * it is one of the following values:
1186 * Value Meaning
1187 * 0 The specified virtual key has no translation for the current state of the keyboard.
1188 * 1 One Windows character was copied to the buffer.
1189 * 2 Two characters were copied to the buffer. This usually happens when a
1190 * dead-key character (accent or diacritic) stored in the keyboard layout cannot
1191 * be composed with the specified virtual key to form a single character.
1193 * FIXME : should do the above (return 2 for non matching deadchar+char combinations)
1196 INT16 X11DRV_KEYBOARD_ToAscii(
1197 UINT16 virtKey,UINT16 scanCode, LPBYTE lpKeyState,
1198 LPVOID lpChar, UINT16 flags)
1200 XKeyEvent e;
1201 KeySym keysym;
1202 static XComposeStatus cs;
1203 INT ret;
1204 int keyc;
1206 if (scanCode==0) {
1207 /* This happens when doing Alt+letter : a fake 'down arrow' key press
1208 event is generated by windows. Just ignore it. */
1209 TRACE_(keyboard)("scanCode=0, doing nothing\n");
1210 return 0;
1212 if (scanCode & 0x8000)
1214 TRACE_(keyboard)("Key UP, doing nothing\n" );
1215 return 0;
1217 e.display = display;
1218 e.keycode = 0;
1219 e.state = 0;
1220 if (lpKeyState[VK_SHIFT] & 0x80)
1221 e.state |= ShiftMask;
1222 if (lpKeyState[VK_CAPITAL] & 0x01)
1223 e.state |= LockMask;
1224 if (lpKeyState[VK_CONTROL] & 0x80)
1226 if (lpKeyState[VK_MENU] & 0x80)
1227 e.state |= AltGrMask;
1228 else
1229 e.state |= ControlMask;
1231 if (lpKeyState[VK_NUMLOCK] & 0x01)
1232 e.state |= NumLockMask;
1233 TRACE_(key)("(%04X, %04X) : faked state = %X\n",
1234 virtKey, scanCode, e.state);
1235 /* We exit on the first keycode found, to speed up the thing. */
1236 for (keyc=min_keycode; (keyc<=max_keycode) && (!e.keycode) ; keyc++)
1237 { /* Find a keycode that could have generated this virtual key */
1238 if ((keyc2vkey[keyc] & 0xFF) == virtKey)
1239 { /* We filter the extended bit, we don't know it */
1240 e.keycode = keyc; /* Store it temporarily */
1241 if ((EVENT_event_to_vkey(&e) & 0xFF) != virtKey) {
1242 e.keycode = 0; /* Wrong one (ex: because of the NumLock
1243 state), so set it to 0, we'll find another one */
1248 if ((virtKey>=VK_NUMPAD0) && (virtKey<=VK_NUMPAD9))
1249 e.keycode = TSXKeysymToKeycode(e.display, virtKey-VK_NUMPAD0+XK_KP_0);
1251 if (virtKey==VK_DECIMAL)
1252 e.keycode = TSXKeysymToKeycode(e.display, XK_KP_Decimal);
1254 if (!e.keycode)
1256 WARN_(keyboard)("Unknown virtual key %X !!! \n",virtKey);
1257 return virtKey; /* whatever */
1259 else TRACE_(keyboard)("Found keycode %d (0x%2X)\n",e.keycode,e.keycode);
1261 ret = TSXLookupString(&e, (LPVOID)lpChar, 2, &keysym, &cs);
1262 if (ret == 0)
1264 BYTE dead_char = 0;
1266 ((char*)lpChar)[1] = '\0';
1267 dead_char = KEYBOARD_MapDeadKeysym(keysym);
1268 if (dead_char)
1270 *(char*)lpChar = dead_char;
1271 ret = -1;
1273 else
1275 char *ksname;
1277 ksname = TSXKeysymToString(keysym);
1278 if (!ksname)
1279 ksname = "No Name";
1280 if ((keysym >> 8) != 0xff)
1282 ERR_(keyboard)("Please report: no char for keysym %04lX (%s) :\n",
1283 keysym, ksname);
1284 ERR_(keyboard)("(virtKey=%X,scanCode=%X,keycode=%X,state=%X)\n",
1285 virtKey, scanCode, e.keycode, e.state);
1289 else { /* ret = 1 */
1290 /* We have a special case to handle : Shift + arrow, shift + home, ...
1291 X returns a char for it, but Windows doesn't. Let's eat it. */
1292 if (!(lpKeyState[VK_NUMLOCK] & 0x01) /* NumLock is off */
1293 && (lpKeyState[VK_SHIFT] & 0x80) /* Shift is pressed */
1294 && (keysym>=XK_KP_0) && (keysym<=XK_KP_9))
1296 *(char*)lpChar = 0;
1297 ret = 0;
1301 TRACE_(key)("ToAscii about to return %d with char %x\n",
1302 ret, *(char*)lpChar);
1303 return ret;
1306 /***********************************************************************
1307 * X11DRV_KEYBOARD_GetBeepActive
1309 BOOL X11DRV_KEYBOARD_GetBeepActive()
1311 XKeyboardState keyboard_state;
1313 TSXGetKeyboardControl(display, &keyboard_state);
1315 return keyboard_state.bell_percent != 0;
1318 /***********************************************************************
1319 * X11DRV_KEYBOARD_SetBeepActive
1321 void X11DRV_KEYBOARD_SetBeepActive(BOOL bActivate)
1323 XKeyboardControl keyboard_value;
1325 if(bActivate)
1326 keyboard_value.bell_percent = -1;
1327 else
1328 keyboard_value.bell_percent = 0;
1330 TSXChangeKeyboardControl(display, KBBellPercent, &keyboard_value);
1333 /***********************************************************************
1334 * X11DRV_KEYBOARD_Beep
1336 void X11DRV_KEYBOARD_Beep()
1338 TSXBell(display, 0);
1341 /***********************************************************************
1342 * X11DRV_KEYBOARD_GetDIState
1344 BOOL X11DRV_KEYBOARD_GetDIState(DWORD len, LPVOID ptr)
1346 if (len==256) {
1347 int keyc,vkey;
1349 memset(ptr,0,256);
1350 for (keyc=min_keycode;keyc<max_keycode;keyc++)
1352 /* X keycode to virtual key */
1353 vkey = keyc2vkey[keyc] & 0xFF;
1354 /* The windows scancode is keyc-min_keycode */
1355 if (InputKeyStateTable[vkey]&0x80) {
1356 ((LPBYTE)ptr)[keyc-min_keycode]=0x80;
1357 ((LPBYTE)ptr)[(keyc-min_keycode)|0x80]=0x80;
1360 return TRUE;
1362 WARN_(x11drv)("whoops, X11DRV_KEYBOARD_GetState got len %ld?\n", len);
1363 return TRUE;
1366 /***********************************************************************
1367 * X11DRV_KEYBOARD_GetDIData
1369 BOOL X11DRV_KEYBOARD_GetDIData(
1370 BYTE *keystate,
1371 DWORD dodsize, LPDIDEVICEOBJECTDATA dod,
1372 LPDWORD entries, DWORD flags)
1374 int keyc,n,vkey,xentries;
1376 /* FIXME !!! */
1378 if (entries)
1379 xentries = *entries;
1380 else
1381 xentries = 1;
1383 n = 0;
1385 for (keyc=min_keycode;(keyc<max_keycode) && (n<*entries);keyc++)
1387 /* X keycode to virtual key */
1388 vkey = keyc2vkey[keyc] & 0xFF;
1389 if (keystate[vkey] == (InputKeyStateTable[vkey]&0x80))
1390 continue;
1391 if (dod) {
1392 /* add an entry */
1393 dod[n].dwOfs = keyc-min_keycode; /* scancode */
1394 dod[n].dwData = InputKeyStateTable[vkey]&0x80;
1395 dod[n].dwTimeStamp = 0; /* umm */
1396 dod[n].dwSequence = 0; /* umm */
1397 n++;
1399 if (!(flags & DIGDD_PEEK))
1400 keystate[vkey] = InputKeyStateTable[vkey]&0x80;
1404 if (n) fprintf(stderr,"%d entries\n",n);
1405 *entries = n;
1407 return TRUE;
1410 #endif /* !defined(X_DISPLAY_MISSING) */