Added german keyboard layout with "nodeadkeys" option enabled (just to
[wine/wine64.git] / windows / x11drv / keyboard.c
blobf82872764a245861df031581a4fe730ff31c4001
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 /*** German keyboard layout without dead keys */
124 static const char main_key_DE_nodead[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 German keyboard layout (contributed by Jonathan Naylor) */
134 static const char main_key_SG[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 /*** Swiss French keyboard layout (contributed by Philippe Froidevaux) */
144 static const char main_key_SF[MAIN_LEN][4] =
146 "§°","1+|","2\"@","3*#","4ç","5%","6&¬","7/¦","8(¢","9)","0=","'?´","^`~",
147 "qQ","wW","eE","rR","tT","zZ","uU","iI","oO","pP","èü[","¨!]",
148 "aA","sS","dD","fF","gG","hH","jJ","kK","lL","éö","àä{","$£}",
149 "yY","xX","cC","vV","bB","nN","mM",",;",".:","-_",
150 "<>\\"
153 /*** Norwegian keyboard layout (contributed by Ove Kåven) */
154 static const char main_key_NO[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 /*** Danish keyboard layout (contributed by Bertho Stultiens) */
164 static const char main_key_DA[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 /*** Swedish keyboard layout (contributed by Peter Bortas) */
174 static const char main_key_SE[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 /*** Canadian French keyboard layout */
184 static const char main_key_CF[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 /*** Portuguese keyboard layout */
194 static const char main_key_PT[MAIN_LEN][4] =
196 "\\¦","1!","2\"@","3#£","4$§","5%","6&","7/{","8([","9)]","0=}","'?","«»",
197 "qQ", "wW","eE", "rR", "tT", "yY", "uU", "iI", "oO", "pP", "+*\\¨","\\'\\`",
198 "aA", "sS","dD", "fF", "gG", "hH", "jJ", "kK", "lL", "çÇ", "ºª", "\\~\\^",
199 "zZ", "xX","cC", "vV", "bB", "nN", "mM", ",;", ".:", "-_",
200 "<>"
203 /*** Italian keyboard layout */
204 static const char main_key_IT[MAIN_LEN][4] =
206 "\\|","1!1","2\"2","3?3","4$","5%½","6&¾","7/{","8([","9)]","0=}","'?`","i^~",
207 "qQ@","wW","eE","rR?","tT","yY","uU","iI","oOo","pP?","ee[","+*]",
208 "aAae","sS?","dD?","fF","gG","hH","jJ","kK","lL","oc@","a?#","u?",
209 "zZ<","xX>","cC","vV","bB","nN","mM?",",;",".:*","-_","<>|"
212 /*** Finnish keyboard layout */
213 static const char main_key_FI[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",",;",".:","-_",
219 "<>|"
222 /*** Russian keyboard layout (contributed by Pavel Roskin) */
223 static const char main_key_RU[MAIN_LEN][4] =
225 "`~","1!","2@","3#","4$","5%","6^","7&","8*","9(","0)","-_","=+",
226 "qQÊê","wWÃã","eEÕõ","rRËë","tTÅå","yYÎî","uUÇç","iIÛû","oOÝý","pPÚú","[{Èè","]}ßÿ",
227 "aAÆæ","sSÙù","dD×÷","fFÁá","gGÐð","hHÒò","jJÏï","kKÌì","lLÄä",";:Öö","'\"Üü","\\|",
228 "zZÑñ","xXÞþ","cCÓó","vVÍí","bBÉé","nNÔô","mMØø",",<Ââ",".>Àà","/?"
231 /*** Spanish keyboard layout (contributed by José Marcos López) */
232 static const char main_key_ES[MAIN_LEN][4] =
234 "ºª\\","1!|","2\"@","3·#","4$","5%","6&¬","7/","8(","9)","0=","'?","¡¿",
235 "qQ","wW","eE","rR","tT","yY","uU","iI","oO","pP","`^[","+*]",
236 "aA","sS","dD","fF","gG","hH","jJ","kK","lL","ñÑ","'¨{","çÇ}",
237 "zZ","xX","cC","vV","bB","nN","mM",",;",".:","-_",
238 "<>"
241 /*** Belgian keyboard layout ***/
242 static const char main_key_BE[MAIN_LEN][4] =
244 "","&1|","é2@","\"3#","'4","(5","§6^","è7","!8","ç9{","à0}",")°","-_",
245 "aA","zZ","eE¤","rR","tT","yY","uU","iI","oO","pP","^¨[","$*]",
246 "qQ","sSß","dD","fF","gG","hH","jJ","kK","lL","mM","ù%´","µ£`",
247 "wW","xX","cC","vV","bB","nN",",?",";.",":/","=+~",
248 "<>\\"
251 /*** Layout table. Add your keyboard mappings to this list */
252 static struct {
253 WORD lang, ansi_codepage, oem_codepage;
254 const char (*key)[MAIN_LEN][4];
255 } main_key_tab[]={
256 {MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US), 1252, 437, &main_key_US},
257 {MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_UK), 1252, 850, &main_key_UK},
258 {MAKELANGID(LANG_GERMAN,SUBLANG_DEFAULT), 1252, 850, &main_key_DE},
259 {MAKELANGID(LANG_GERMAN,SUBLANG_DEFAULT), 1252, 850, &main_key_DE_nodead},
260 {MAKELANGID(LANG_GERMAN,SUBLANG_GERMAN_SWISS), 1252, 850, &main_key_SG},
261 {MAKELANGID(LANG_SWEDISH,SUBLANG_SWEDISH), 1252, 850, &main_key_SE},
262 {MAKELANGID(LANG_NORWEGIAN,SUBLANG_DEFAULT), 1252, 865, &main_key_NO},
263 {MAKELANGID(LANG_DANISH,SUBLANG_DEFAULT), 1252, 865, &main_key_DA},
264 {MAKELANGID(LANG_FRENCH,SUBLANG_DEFAULT), 1252, 850, &main_key_FR},
265 {MAKELANGID(LANG_FRENCH,SUBLANG_FRENCH_CANADIAN), 1252, 863, &main_key_CF},
266 {MAKELANGID(LANG_FRENCH,SUBLANG_FRENCH_BELGIAN), 1252, 850, &main_key_BE},
267 {MAKELANGID(LANG_FRENCH,SUBLANG_FRENCH_SWISS), 1252, 850, &main_key_SF},
268 {MAKELANGID(LANG_WALON,SUBLANG_DEFAULT), 1252, 850, &main_key_BE},
269 {MAKELANGID(LANG_PORTUGUESE,SUBLANG_DEFAULT), 1252, 860, &main_key_PT},
270 {MAKELANGID(LANG_FINNISH,SUBLANG_DEFAULT), 1252, 850, &main_key_FI},
271 {MAKELANGID(LANG_RUSSIAN,SUBLANG_DEFAULT), 1251, 866, &main_key_RU},
272 {MAKELANGID(LANG_SPANISH,SUBLANG_DEFAULT), 1252, 850, &main_key_ES},
273 {MAKELANGID(LANG_DUTCH,SUBLANG_DUTCH_BELGIAN), 1252, 850, &main_key_BE},
274 {MAKELANGID(LANG_ITALIAN,SUBLANG_DEFAULT), 1252, 850, &main_key_IT},
275 {MAKELANGID(LANG_ICELANDIC,SUBLANG_DEFAULT), 1252, 850, &main_key_IS},
277 {0} /* sentinel */
279 static unsigned kbd_layout=0; /* index into above table of layouts */
281 /* maybe more of these scancodes should be extended? */
282 /* extended must be set for ALT_R, CTRL_R,
283 INS, DEL, HOME, END, PAGE_UP, PAGE_DOWN, ARROW keys,
284 keypad / and keypad ENTER (SDK 3.1 Vol.3 p 138) */
285 /* FIXME should we set extended bit for NumLock ? My
286 * Windows does ... DF */
287 /* Yes, to distinguish based on scan codes, also
288 for PrtScn key ... GA */
290 static const int special_key_vkey[] =
292 VK_BACK, VK_TAB, 0, VK_CLEAR, 0, VK_RETURN, 0, 0, /* FF08 */
293 0, 0, 0, VK_PAUSE, VK_SCROLL, 0, 0, 0, /* FF10 */
294 0, 0, 0, VK_ESCAPE /* FF18 */
296 static const int special_key_scan[] =
298 0x0E, 0x0F, 0, /*?*/ 0, 0, 0x1C, 0, 0, /* FF08 */
299 0, 0, 0, 0x45, 0x46, 0 , 0, 0, /* FF10 */
300 0, 0, 0, 0x01 /* FF18 */
303 static const int cursor_key_vkey[] =
305 VK_HOME, VK_LEFT, VK_UP, VK_RIGHT, VK_DOWN, VK_PRIOR,
306 VK_NEXT, VK_END /* FF50 */
308 static const int cursor_key_scan[] =
310 0x147, 0x14B, 0x148, 0x14D, 0x150, 0x149, 0x151, 0x14F /* FF50 */
313 static const int misc_key_vkey[] =
315 VK_SELECT, VK_SNAPSHOT, VK_EXECUTE, VK_INSERT, 0, 0, 0, 0, /* FF60 */
316 VK_CANCEL, VK_HELP, VK_CANCEL, VK_MENU /* FF68 */
318 static const int misc_key_scan[] =
320 /*?*/ 0, 0x137, /*?*/ 0, 0x152, 0, 0, 0, 0, /* FF60 */
321 /*?*/ 0, /*?*/ 0, 0x38 /* FF68 */
324 static const int keypad_key_vkey[] =
326 0, VK_NUMLOCK, /* FF7E */
327 0, 0, 0, 0, 0, 0, 0, 0, /* FF80 */
328 0, 0, 0, 0, 0, VK_RETURN, 0, 0, /* FF88 */
329 0, 0, 0, 0, 0, VK_HOME, VK_LEFT, VK_UP, /* FF90 */
330 VK_RIGHT, VK_DOWN, VK_PRIOR, VK_NEXT, VK_END, 0,
331 VK_INSERT, VK_DELETE, /* FF98 */
332 0, 0, 0, 0, 0, 0, 0, 0, /* FFA0 */
333 0, 0, VK_MULTIPLY, VK_ADD, VK_SEPARATOR, VK_SUBTRACT,
334 VK_DECIMAL, VK_DIVIDE, /* FFA8 */
335 VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3, VK_NUMPAD4,
336 VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, /* FFB0 */
337 VK_NUMPAD8, VK_NUMPAD9 /* FFB8 */
339 static const int keypad_key_scan[] =
341 0x138, 0x145, /* FF7E */
342 0, 0, 0, 0, 0, 0, 0, 0, /* FF80 */
343 0, 0, 0, 0, 0, 0x11C, 0, 0, /* FF88 */
344 0, 0, 0, 0, 0, 0x47, 0x4B, 0x48, /* FF90 */
345 0x4D, 0x50, 0x49, 0x51, 0x4F, 0x4C, 0x52, 0x53, /* FF98 */
346 0, 0, 0, 0, 0, 0, 0, 0, /* FFA0 */
347 0, 0, 0x37, 0x4E, /*?*/ 0, 0x4A, 0x53, 0x135, /* FFA8 */
348 0x52, 0x4F, 0x50, 0x51, 0x4B, 0x4C, 0x4D, 0x47, /* FFB0 */
349 0x48, 0x49 /* FFB8 */
352 static const int function_key_vkey[] =
354 VK_F1, VK_F2, /* FFBE */
355 VK_F3, VK_F4, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9, VK_F10, /* FFC0 */
356 VK_F11, VK_F12, VK_F13, VK_F14, VK_F15, VK_F16 /* FFC8 */
358 static const int function_key_scan[] =
360 0x3B, 0x3C, /* FFBE */
361 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, /* FFC0 */
362 0x57, 0x58, 0, 0, 0, 0 /* FFC8 */
365 static const int modifier_key_vkey[] =
367 VK_SHIFT, VK_SHIFT, VK_CONTROL, VK_CONTROL, VK_CAPITAL, 0, /* FFE1 */
368 VK_MENU, VK_MENU, VK_MENU, VK_MENU /* FFE7 */
370 static const int modifier_key_scan[] =
372 0x2A, 0x36, 0x1D, 0x11D, 0x3A, 0, /* FFE1 */
373 0x38, 0x138, 0x38, 0x138 /* FFE7 */
376 /* Returns the Windows virtual key code associated with the X event <e> */
377 static WORD EVENT_event_to_vkey( XKeyEvent *e)
379 KeySym keysym;
381 TSXLookupString(e, NULL, 0, &keysym, NULL);
383 if ((keysym >= 0xFFAE) && (keysym <= 0xFFB9) && (keysym != 0xFFAF)
384 && (e->state & NumLockMask))
385 /* Only the Keypad keys 0-9 and . send different keysyms
386 * depending on the NumLock state */
387 return keypad_key_vkey[(keysym & 0xFF) - 0x7E];
389 return keyc2vkey[e->keycode];
392 static BOOL NumState=FALSE, CapsState=FALSE;
394 /**********************************************************************
395 * KEYBOARD_GenerateMsg
397 * Generate Down+Up messages when NumLock or CapsLock is pressed.
399 * Convention : called with vkey only VK_NUMLOCK or VK_CAPITAL
402 static void KEYBOARD_GenerateMsg( WORD vkey, WORD scan, int Evtype, INT event_x, INT event_y,
403 DWORD event_time )
405 BOOL * State = (vkey==VK_NUMLOCK? &NumState : &CapsState);
406 DWORD up, down;
408 if (*State) {
409 /* The INTERMEDIARY state means : just after a 'press' event, if a 'release' event comes,
410 don't treat it. It's from the same key press. Then the state goes to ON.
411 And from there, a 'release' event will switch off the toggle key. */
412 *State=FALSE;
413 TRACE_(keyboard)("INTERM : don\'t treat release of toggle key. InputKeyStateTable[%#x] = %#x\n",vkey,pKeyStateTable[vkey]);
414 } else
416 down = (vkey==VK_NUMLOCK ? KEYEVENTF_EXTENDEDKEY : 0);
417 up = (vkey==VK_NUMLOCK ? KEYEVENTF_EXTENDEDKEY : 0) | KEYEVENTF_KEYUP;
418 if ( pKeyStateTable[vkey] & 0x1 ) /* it was ON */
420 if (Evtype!=KeyPress)
422 TRACE_(keyboard)("ON + KeyRelease => generating DOWN and UP messages.\n");
423 KEYBOARD_SendEvent( vkey, scan, down,
424 event_x, event_y, event_time );
425 KEYBOARD_SendEvent( vkey, scan, up,
426 event_x, event_y, event_time );
427 *State=FALSE;
428 pKeyStateTable[vkey] &= ~0x01; /* Toggle state to off. */
431 else /* it was OFF */
432 if (Evtype==KeyPress)
434 TRACE_(keyboard)("OFF + Keypress => generating DOWN and UP messages.\n");
435 KEYBOARD_SendEvent( vkey, scan, down,
436 event_x, event_y, event_time );
437 KEYBOARD_SendEvent( vkey, scan, up,
438 event_x, event_y, event_time );
439 *State=TRUE; /* Goes to intermediary state before going to ON */
440 pKeyStateTable[vkey] |= 0x01; /* Toggle state to on. */
445 /***********************************************************************
446 * KEYBOARD_UpdateOneState
448 * Updates internal state for <vkey>, depending on key <state> under X
451 static void KEYBOARD_UpdateOneState ( int vkey, int state )
453 /* Do something if internal table state != X state for keycode */
454 if (((pKeyStateTable[vkey] & 0x80)!=0) != state)
456 TRACE_(keyboard)("Adjusting state for vkey %#.2x. State before %#.2x \n",
457 vkey, pKeyStateTable[vkey]);
459 /* Fake key being pressed inside wine */
460 KEYBOARD_SendEvent( vkey, 0, state? 0 : KEYEVENTF_KEYUP,
461 0, 0, GetTickCount() );
463 TRACE_(keyboard)("State after %#.2x \n",pKeyStateTable[vkey]);
467 /***********************************************************************
468 * X11DRV_KEYBOARD_UpdateState
470 * Update modifiers state (Ctrl, Alt, Shift)
471 * when window is activated (called by EVENT_FocusIn in event.c)
473 * This handles the case where one uses Ctrl+... Alt+... or Shift+.. to switch
474 * from wine to another application and back.
475 * Toggle keys are handled in HandleEvent. (because XQueryKeymap says nothing
476 * about them)
478 void X11DRV_KEYBOARD_UpdateState ( void )
480 /* extract a bit from the char[32] bit suite */
481 #define KeyState(keycode) ((keys_return[keycode/8] & (1<<(keycode%8)))!=0)
483 char keys_return[32];
485 TRACE_(keyboard)("called\n");
486 if (!TSXQueryKeymap(display, keys_return)) {
487 ERR_(keyboard)("Error getting keymap !");
488 return;
491 /* Adjust the ALT and CONTROL state if any has been changed outside wine */
492 KEYBOARD_UpdateOneState(VK_MENU, KeyState(kcAlt));
493 KEYBOARD_UpdateOneState(VK_CONTROL, KeyState(kcControl));
494 KEYBOARD_UpdateOneState(VK_SHIFT, KeyState(kcShift));
495 #undef KeyState
498 /***********************************************************************
499 * X11DRV_KEYBOARD_HandleEvent
501 * Handle a X key event
503 void X11DRV_KEYBOARD_HandleEvent( WND *pWnd, XKeyEvent *event )
505 char Str[24];
506 XComposeStatus cs;
507 KeySym keysym;
508 WORD vkey = 0, bScan;
509 DWORD dwFlags;
510 static BOOL force_extended = FALSE; /* hack for AltGr translation */
512 int ascii_chars;
514 INT event_x = (pWnd? pWnd->rectWindow.left : 0) + event->x;
515 INT event_y = (pWnd? pWnd->rectWindow.top : 0) + event->y;
516 DWORD event_time = event->time - MSG_WineStartTicks;
518 /* this allows support for dead keys */
519 if ((event->keycode >> 8) == 0x10)
520 event->keycode=(event->keycode & 0xff);
522 ascii_chars = TSXLookupString(event, Str, 1, &keysym, &cs);
524 TRACE_(key)("EVENT_key : state = %X\n", event->state);
525 if (keysym == XK_Mode_switch)
527 TRACE_(key)("Alt Gr key event received\n");
528 event->keycode = kcControl; /* Simulate Control */
529 X11DRV_KEYBOARD_HandleEvent( pWnd, event );
531 event->keycode = kcAlt; /* Simulate Alt */
532 force_extended = TRUE;
533 X11DRV_KEYBOARD_HandleEvent( pWnd, event );
534 force_extended = FALSE;
535 return;
538 Str[ascii_chars] = '\0';
539 if (TRACE_ON(key)){
540 char *ksname;
542 ksname = TSXKeysymToString(keysym);
543 if (!ksname)
544 ksname = "No Name";
545 TRACE_(key)("%s : keysym=%lX (%s), ascii chars=%u / %X / '%s'\n",
546 (event->type == KeyPress) ? "KeyPress" : "KeyRelease",
547 keysym, ksname, ascii_chars, Str[0] & 0xff, Str);
550 vkey = EVENT_event_to_vkey(event);
551 if (force_extended) vkey |= 0x100;
553 TRACE_(key)("keycode 0x%x converted to vkey 0x%x\n",
554 event->keycode, vkey);
556 if (vkey)
558 switch (vkey & 0xff)
560 case VK_NUMLOCK:
561 KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, event->type, event_x, event_y,
562 event_time );
563 break;
564 case VK_CAPITAL:
565 TRACE_(keyboard)("Caps Lock event. (type %d). State before : %#.2x\n",event->type,pKeyStateTable[vkey]);
566 KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, event->type, event_x, event_y,
567 event_time );
568 TRACE_(keyboard)("State after : %#.2x\n",pKeyStateTable[vkey]);
569 break;
570 default:
571 /* Adjust the NUMLOCK state if it has been changed outside wine */
572 if (!(pKeyStateTable[VK_NUMLOCK] & 0x01) != !(event->state & NumLockMask))
574 TRACE_(keyboard)("Adjusting NumLock state. \n");
575 KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, KeyPress, event_x, event_y,
576 event_time );
577 KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, KeyRelease, event_x, event_y,
578 event_time );
580 /* Adjust the CAPSLOCK state if it has been changed outside wine */
581 if (!(pKeyStateTable[VK_CAPITAL] & 0x01) != !(event->state & LockMask))
583 TRACE_(keyboard)("Adjusting Caps Lock state.\n");
584 KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, KeyPress, event_x, event_y,
585 event_time );
586 KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, KeyRelease, event_x, event_y,
587 event_time );
589 /* Not Num nor Caps : end of intermediary states for both. */
590 NumState = FALSE;
591 CapsState = FALSE;
593 bScan = keyc2scan[event->keycode] & 0xFF;
594 TRACE_(key)("bScan = 0x%02x.\n", bScan);
596 dwFlags = 0;
597 if ( event->type == KeyRelease ) dwFlags |= KEYEVENTF_KEYUP;
598 if ( vkey & 0x100 ) dwFlags |= KEYEVENTF_EXTENDEDKEY;
599 if ( force_extended ) dwFlags |= KEYEVENTF_WINE_FORCEEXTENDED;
601 KEYBOARD_SendEvent( vkey & 0xff, bScan, dwFlags,
602 event_x, event_y, event_time );
607 /**********************************************************************
608 * X11DRV_KEYBOARD_DetectLayout
610 * Called from X11DRV_KEYBOARD_Init
611 * This routine walks through the defined keyboard layouts and selects
612 * whichever matches most closely.
614 static void
615 X11DRV_KEYBOARD_DetectLayout (void)
617 unsigned current, match, mismatch, seq;
618 int score, keyc, i, key, pkey, ok, syms;
619 KeySym keysym;
620 const char (*lkey)[MAIN_LEN][4];
621 unsigned max_seq = 0;
622 int max_score = 0, ismatch = 0;
623 char ckey[4] =
624 {0, 0, 0, 0};
626 syms = keysyms_per_keycode;
627 if (syms > 4) {
628 WARN_(keyboard)("%d keysyms per keycode not supported, set to 4", syms);
629 syms = 4;
631 for (current = 0; main_key_tab[current].lang; current++) {
632 TRACE_(keyboard)("Attempting to match against layout %04x\n",
633 main_key_tab[current].lang);
634 match = 0;
635 mismatch = 0;
636 score = 0;
637 seq = 0;
638 lkey = main_key_tab[current].key;
639 pkey = -1;
640 for (keyc = min_keycode; keyc <= max_keycode; keyc++) {
641 /* get data for keycode from X server */
642 for (i = 0; i < syms; i++) {
643 keysym = TSXKeycodeToKeysym (display, keyc, i);
644 /* Allow both one-byte and two-byte national keysyms */
645 if ((keysym < 0x800) && (keysym != ' '))
646 ckey[i] = keysym & 0xFF;
647 else {
648 ckey[i] = KEYBOARD_MapDeadKeysym(keysym);
651 if (ckey[0]) {
652 /* search for a match in layout table */
653 /* right now, we just find an absolute match for defined positions */
654 /* (undefined positions are ignored, so if it's defined as "3#" in */
655 /* the table, it's okay that the X server has "3#£", for example) */
656 /* however, the score will be higher for longer matches */
657 for (key = 0; key < MAIN_LEN; key++) {
658 for (ok = 0, i = 0; (ok >= 0) && (i < syms); i++) {
659 if ((*lkey)[key][i] && ((*lkey)[key][i] == ckey[i]))
660 ok++;
661 if ((*lkey)[key][i] && ((*lkey)[key][i] != ckey[i]))
662 ok = -1;
664 if (ok > 0) {
665 score += ok;
666 break;
669 /* count the matches and mismatches */
670 if (ok > 0) {
671 match++;
672 /* and how much the keycode order matches */
673 if (key > pkey) seq++;
674 pkey = key;
675 } else {
676 TRACE_(key)("mismatch for keycode %d, character %c\n", keyc,
677 ckey[0]);
678 mismatch++;
679 score -= syms;
683 TRACE_(keyboard)("matches=%d, mismatches=%d, score=%d\n",
684 match, mismatch, score);
685 if ((score > max_score) ||
686 ((score == max_score) && (seq > max_seq))) {
687 /* best match so far */
688 kbd_layout = current;
689 max_score = score;
690 max_seq = seq;
691 ismatch = !mismatch;
694 /* we're done, report results if necessary */
695 if (!ismatch) {
696 FIXME_(keyboard)(
697 "Your keyboard layout was not found!\n"
698 "Instead using closest match (%04x) for scancode mapping.\n"
699 "Please define your layout in windows/x11drv/keyboard.c and submit them\n"
700 "to us for inclusion into future Wine releases.\n"
701 "See documentation/keyboard for more information.\n",
702 main_key_tab[kbd_layout].lang);
704 TRACE_(keyboard)("detected layout is %04x\n", main_key_tab[kbd_layout].lang);
707 /**********************************************************************
708 * X11DRV_KEYBOARD_Init
710 void X11DRV_KEYBOARD_Init(void)
712 KeySym *ksp;
713 XModifierKeymap *mmp;
714 KeySym keysym;
715 KeyCode *kcp;
716 XKeyEvent e2;
717 WORD scan, vkey, OEMvkey;
718 int keyc, i, keyn, syms;
719 char ckey[4]={0,0,0,0};
720 const char (*lkey)[MAIN_LEN][4];
722 TSXDisplayKeycodes(display, &min_keycode, &max_keycode);
723 ksp = TSXGetKeyboardMapping(display, min_keycode,
724 max_keycode + 1 - min_keycode, &keysyms_per_keycode);
725 /* We are only interested in keysyms_per_keycode.
726 There is no need to hold a local copy of the keysyms table */
727 TSXFree(ksp);
728 mmp = TSXGetModifierMapping(display);
729 kcp = mmp->modifiermap;
730 for (i = 0; i < 8; i += 1) /* There are 8 modifier keys */
732 int j;
734 for (j = 0; j < mmp->max_keypermod; j += 1, kcp += 1)
735 if (*kcp)
737 int k;
739 for (k = 0; k < keysyms_per_keycode; k += 1)
740 if (TSXKeycodeToKeysym(display, *kcp, k) == XK_Mode_switch)
742 AltGrMask = 1 << i;
743 TRACE_(key)("AltGrMask is %x\n", AltGrMask);
745 else if (TSXKeycodeToKeysym(display, *kcp, k) == XK_Num_Lock)
747 NumLockMask = 1 << i;
748 TRACE_(key)("NumLockMask is %x\n", NumLockMask);
752 TSXFreeModifiermap(mmp);
754 /* Detect the keyboard layout */
755 X11DRV_KEYBOARD_DetectLayout();
756 lkey = main_key_tab[kbd_layout].key;
757 syms = (keysyms_per_keycode > 4) ? 4 : keysyms_per_keycode;
759 /* Now build two conversion arrays :
760 * keycode -> vkey + scancode + extended
761 * vkey + extended -> keycode */
763 e2.display = display;
764 e2.state = 0;
766 OEMvkey = VK_OEM_7; /* next is available. */
767 for (keyc = min_keycode; keyc <= max_keycode; keyc++)
769 e2.keycode = (KeyCode)keyc;
770 TSXLookupString(&e2, NULL, 0, &keysym, NULL);
771 vkey = 0; scan = 0;
772 if (keysym) /* otherwise, keycode not used */
774 if ((keysym >> 8) == 0xFF) /* non-character key */
776 int key = keysym & 0xff;
778 if (key >= 0x08 && key <= 0x1B) { /* special key */
779 vkey = special_key_vkey[key - 0x08];
780 scan = special_key_scan[key - 0x08];
781 } else if (key >= 0x50 && key <= 0x57) { /* cursor key */
782 vkey = cursor_key_vkey[key - 0x50];
783 scan = cursor_key_scan[key - 0x50];
784 } else if (key >= 0x60 && key <= 0x6B) { /* miscellaneous key */
785 vkey = misc_key_vkey[key - 0x60];
786 scan = misc_key_scan[key - 0x60];
787 } else if (key >= 0x7E && key <= 0xB9) { /* keypad key */
788 vkey = keypad_key_vkey[key - 0x7E];
789 scan = keypad_key_scan[key - 0x7E];
790 } else if (key >= 0xBE && key <= 0xCD) { /* function key */
791 vkey = function_key_vkey[key - 0xBE] | 0x100; /* set extended bit */
792 scan = function_key_scan[key - 0xBE];
793 } else if (key >= 0xE1 && key <= 0xEA) { /* modifier key */
794 vkey = modifier_key_vkey[key - 0xE1];
795 scan = modifier_key_scan[key - 0xE1];
796 } else if (key == 0xFF) { /* DEL key */
797 vkey = VK_DELETE;
798 scan = 0x153;
800 /* set extended bit when necessary */
801 if (scan & 0x100) vkey |= 0x100;
802 } else if (keysym == 0x20) { /* Spacebar */
803 vkey = VK_SPACE;
804 scan = 0x39;
805 } else {
806 /* we seem to need to search the layout-dependent scancodes */
807 int maxlen=0,maxval=-1,ok;
808 for (i=0; i<syms; i++) {
809 keysym = TSXKeycodeToKeysym(display, keyc, i);
810 if ((keysym<0x800) && (keysym!=' ')) {
811 ckey[i] = keysym & 0xFF;
812 } else {
813 ckey[i] = KEYBOARD_MapDeadKeysym(keysym);
816 /* find key with longest match streak */
817 for (keyn=0; keyn<MAIN_LEN; keyn++) {
818 for (ok=(*lkey)[keyn][i=0]; ok&&(i<4); i++)
819 if ((*lkey)[keyn][i] && (*lkey)[keyn][i]!=ckey[i]) ok=0;
820 if (ok||(i>maxlen)) {
821 maxlen=i; maxval=keyn;
823 if (ok) break;
825 if (maxval>=0) {
826 /* got it */
827 scan = main_key_scan[maxval];
831 /* find a suitable layout-dependent VK code */
832 /* (most Winelib apps ought to be able to work without layout tables!) */
833 for (i = 0; (i < keysyms_per_keycode) && (!vkey); i++)
835 keysym = TSXLookupKeysym(&e2, i);
836 if ((keysym >= VK_0 && keysym <= VK_9)
837 || (keysym >= VK_A && keysym <= VK_Z)) {
838 vkey = keysym;
842 for (i = 0; (i < keysyms_per_keycode) && (!vkey); i++)
844 keysym = TSXLookupKeysym(&e2, i);
845 switch (keysym)
847 case ';': vkey = VK_OEM_1; break;
848 case '/': vkey = VK_OEM_2; break;
849 case '`': vkey = VK_OEM_3; break;
850 case '[': vkey = VK_OEM_4; break;
851 case '\\': vkey = VK_OEM_5; break;
852 case ']': vkey = VK_OEM_6; break;
853 case '\'': vkey = VK_OEM_7; break;
854 case ',': vkey = VK_OEM_COMMA; break;
855 case '.': vkey = VK_OEM_PERIOD; break;
856 case '-': vkey = VK_OEM_MINUS; break;
857 case '+': vkey = VK_OEM_PLUS; break;
861 if (!vkey)
863 /* Others keys: let's assign OEM virtual key codes in the allowed range,
864 * that is ([0xba,0xc0], [0xdb,0xe4], 0xe6 (given up) et [0xe9,0xf5]) */
865 switch (++OEMvkey)
867 case 0xc1 : OEMvkey=0xdb; break;
868 case 0xe5 : OEMvkey=0xe9; break;
869 case 0xf6 : OEMvkey=0xf5; WARN_(keyboard)("No more OEM vkey available!\n");
872 vkey = OEMvkey;
874 if (TRACE_ON(keyboard))
876 TRACE_(keyboard)("OEM specific virtual key %X assigned to keycode %X:\n",
877 OEMvkey, e2.keycode);
878 TRACE_(keyboard)("(");
879 for (i = 0; i < keysyms_per_keycode; i += 1)
881 char *ksname;
883 keysym = TSXLookupKeysym(&e2, i);
884 ksname = TSXKeysymToString(keysym);
885 if (!ksname)
886 ksname = "NoSymbol";
887 DPRINTF( "%lX (%s) ", keysym, ksname);
889 DPRINTF(")\n");
893 keyc2vkey[e2.keycode] = vkey;
894 keyc2scan[e2.keycode] = scan;
895 } /* for */
897 /* If some keys still lack scancodes, assign some arbitrary ones to them now */
898 for (scan = 0x60, keyc = min_keycode; keyc <= max_keycode; keyc++)
899 if (keyc2vkey[keyc]&&!keyc2scan[keyc]) {
900 char *ksname;
901 keysym = TSXKeycodeToKeysym(display, keyc, 0);
902 ksname = TSXKeysymToString(keysym);
903 if (!ksname) ksname = "NoSymbol";
905 /* should make sure the scancode is unassigned here, but >=0x60 currently always is */
907 TRACE_(key)("assigning scancode %02x to unidentified keycode %02x (%s)\n",scan,keyc,ksname);
908 keyc2scan[keyc]=scan++;
911 /* Now store one keycode for each modifier. Used to simulate keypresses. */
912 kcControl = TSXKeysymToKeycode(display, XK_Control_L);
913 kcAlt = TSXKeysymToKeycode(display, XK_Alt_L);
914 if (!kcAlt) kcAlt = TSXKeysymToKeycode(display, XK_Meta_L);
915 kcShift = TSXKeysymToKeycode(display, XK_Shift_L);
916 kcNumLock = TSXKeysymToKeycode(display, XK_Num_Lock);
917 kcCapsLock = TSXKeysymToKeycode(display, XK_Caps_Lock);
920 /***********************************************************************
921 * X11DRV_KEYBOARD_VkKeyScan
923 WORD X11DRV_KEYBOARD_VkKeyScan(CHAR cChar)
925 KeyCode keycode;
926 KeySym keysym;
927 int i,index;
928 int highbyte=0;
930 /* char->keysym (same for ANSI chars) */
931 keysym=(unsigned char) cChar;/* (!) cChar is signed */
932 if (keysym<=27) keysym+=0xFF00;/*special chars : return, backspace...*/
934 keycode = TSXKeysymToKeycode(display, keysym); /* keysym -> keycode */
935 if (!keycode)
936 { /* It didn't work ... let's try with deadchar code. */
937 keycode = TSXKeysymToKeycode(display, keysym | 0xFE00);
940 TRACE_(keyboard)("VkKeyScan '%c'(%#lx, %lu): got keycode %#.2x\n",
941 cChar,keysym,keysym,keycode);
943 if (keycode)
945 for (index=-1, i=0; (i<8) && (index<0); i++) /* find shift state */
946 if (TSXKeycodeToKeysym(display,keycode,i)==keysym) index=i;
947 switch (index) {
948 case -1 :
949 WARN_(keyboard)("Keysym %lx not found while parsing the keycode table\n",keysym); break;
950 case 0 : break;
951 case 1 : highbyte = 0x0100; break;
952 case 2 : highbyte = 0x0600; break;
953 case 3 : highbyte = 0x0700; break;
954 default : ERR_(keyboard)("index %d found by XKeycodeToKeysym. please report! \n",index);
957 index : 0 adds 0x0000
958 index : 1 adds 0x0100 (shift)
959 index : ? adds 0x0200 (ctrl)
960 index : 2 adds 0x0600 (ctrl+alt)
961 index : 3 adds 0x0700 (ctrl+alt+shift)
964 TRACE_(keyboard)(" ... returning %#.2x\n", keyc2vkey[keycode]+highbyte);
965 return keyc2vkey[keycode]+highbyte; /* keycode -> (keyc2vkey) vkey */
968 /***********************************************************************
969 * X11DRV_KEYBOARD_MapVirtualKey
971 UINT16 X11DRV_KEYBOARD_MapVirtualKey(UINT16 wCode, UINT16 wMapType)
973 #define returnMVK(value) { TRACE_(keyboard)("returning 0x%x.\n",value); return value; }
975 TRACE_(keyboard)("MapVirtualKey wCode=0x%x wMapType=%d ... \n",
976 wCode,wMapType);
977 switch(wMapType) {
978 case 0: { /* vkey-code to scan-code */
979 /* let's do vkey -> keycode -> scan */
980 int keyc;
981 for (keyc=min_keycode; keyc<=max_keycode; keyc++)
982 if ((keyc2vkey[keyc] & 0xFF) == wCode)
983 returnMVK (keyc2scan[keyc] & 0xFF);
984 TRACE_(keyboard)("returning no scan-code.\n");
985 return 0; }
987 case 1: { /* scan-code to vkey-code */
988 /* let's do scan -> keycode -> vkey */
989 int keyc;
990 for (keyc=min_keycode; keyc<=max_keycode; keyc++)
991 if ((keyc2scan[keyc] & 0xFF) == (wCode & 0xFF))
992 returnMVK (keyc2vkey[keyc] & 0xFF);
993 TRACE_(keyboard)("returning no vkey-code.\n");
994 return 0; }
996 case 2: { /* vkey-code to unshifted ANSI code */
997 /* (was FIXME) : what does unshifted mean ? 'a' or 'A' ? */
998 /* My Windows returns 'A'. */
999 /* let's do vkey -> keycode -> (XLookupString) ansi char */
1000 XKeyEvent e;
1001 KeySym keysym;
1002 int keyc;
1003 char s[2];
1004 e.display = display;
1005 e.state = 0; /* unshifted */
1007 e.keycode = 0;
1008 /* We exit on the first keycode found, to speed up the thing. */
1009 for (keyc=min_keycode; (keyc<=max_keycode) && (!e.keycode) ; keyc++)
1010 { /* Find a keycode that could have generated this virtual key */
1011 if ((keyc2vkey[keyc] & 0xFF) == wCode)
1012 { /* We filter the extended bit, we don't know it */
1013 e.keycode = keyc; /* Store it temporarily */
1014 if ((EVENT_event_to_vkey(&e) & 0xFF) != wCode) {
1015 e.keycode = 0; /* Wrong one (ex: because of the NumLock
1016 state), so set it to 0, we'll find another one */
1021 if ((wCode>=VK_NUMPAD0) && (wCode<=VK_NUMPAD9))
1022 e.keycode = TSXKeysymToKeycode(e.display, wCode-VK_NUMPAD0+XK_KP_0);
1024 if (wCode==VK_DECIMAL)
1025 e.keycode = TSXKeysymToKeycode(e.display, XK_KP_Decimal);
1027 if (!e.keycode)
1029 WARN_(keyboard)("Unknown virtual key %X !!! \n", wCode);
1030 return 0; /* whatever */
1032 TRACE_(keyboard)("Found keycode %d (0x%2X)\n",e.keycode,e.keycode);
1034 if (TSXLookupString(&e, s, 2, &keysym, NULL))
1035 returnMVK (*s);
1037 TRACE_(keyboard)("returning no ANSI.\n");
1038 return 0;
1041 case 3: /* **NT only** scan-code to vkey-code but distinguish between */
1042 /* left and right */
1043 FIXME_(keyboard)(" stub for NT\n");
1044 return 0;
1046 default: /* reserved */
1047 WARN_(keyboard)("Unknown wMapType %d !\n",
1048 wMapType);
1049 return 0;
1051 return 0;
1054 /***********************************************************************
1055 * X11DRV_KEYBOARD_GetKeyNameText
1057 INT16 X11DRV_KEYBOARD_GetKeyNameText(LONG lParam, LPSTR lpBuffer, INT16 nSize)
1059 int vkey, ansi, scanCode;
1060 KeyCode keyc;
1061 KeySym keys;
1062 char *name;
1064 scanCode = lParam >> 16;
1065 scanCode &= 0x1ff; /* keep "extended-key" flag with code */
1067 /* FIXME: should use MVK type 3 (NT version that distinguishes right and left */
1068 vkey = X11DRV_KEYBOARD_MapVirtualKey(scanCode, 1);
1070 /* handle "don't care" bit (0x02000000) */
1071 if (!(lParam & 0x02000000)) {
1072 switch (vkey) {
1073 case VK_LSHIFT:
1074 case VK_RSHIFT:
1075 vkey = VK_SHIFT;
1076 break;
1077 case VK_LCONTROL:
1078 case VK_RCONTROL:
1079 vkey = VK_CONTROL;
1080 break;
1081 case VK_LMENU:
1082 case VK_RMENU:
1083 vkey = VK_MENU;
1084 break;
1085 default:
1086 break;
1090 ansi = X11DRV_KEYBOARD_MapVirtualKey(vkey, 2);
1091 TRACE_(keyboard)("scan 0x%04x, vkey 0x%04x, ANSI 0x%04x\n",
1092 scanCode, vkey, ansi);
1094 /* first get the name of the "regular" keys which is the Upper case
1095 value of the keycap imprint. */
1096 if ( ((ansi >= 0x21) && (ansi <= 0x7e)) &&
1097 (scanCode != 0x137) && /* PrtScn */
1098 (scanCode != 0x135) && /* numpad / */
1099 (scanCode != 0x37 ) && /* numpad * */
1100 (scanCode != 0x4a ) && /* numpad - */
1101 (scanCode != 0x4e ) ) /* numpad + */
1103 if ((nSize >= 2) && lpBuffer)
1105 *lpBuffer = toupper((char)ansi);
1106 *(lpBuffer+1) = 0;
1107 return 1;
1109 else
1110 return 0;
1113 /* FIXME: horrible hack to fix function keys. Windows reports scancode
1114 without "extended-key" flag. However Wine generates scancode
1115 *with* "extended-key" flag. Seems to occur *only* for the
1116 function keys. Soooo.. We will leave the table alone and
1117 fudge the lookup here till the other part is found and fixed!!! */
1119 if ( ((scanCode >= 0x13b) && (scanCode <= 0x144)) ||
1120 (scanCode == 0x157) || (scanCode == 0x158))
1121 scanCode &= 0xff; /* remove "extended-key" flag for Fx keys */
1123 /* let's do scancode -> keycode -> keysym -> String */
1125 for (keyc=min_keycode; keyc<=max_keycode; keyc++)
1126 if ((keyc2scan[keyc]) == scanCode)
1127 break;
1128 if (keyc <= max_keycode)
1130 keys = TSXKeycodeToKeysym(display, keyc, 0);
1131 name = TSXKeysymToString(keys);
1132 TRACE_(keyboard)("found scan=%04x keyc=%04x keysym=%04x string=%s\n",
1133 scanCode, keyc, (int)keys, name);
1134 if (lpBuffer && nSize && name)
1136 strncpy(lpBuffer, name, nSize);
1137 return 1;
1141 /* Finally issue FIXME for unknown keys */
1143 FIXME_(keyboard)("(%08lx,%p,%d): unsupported key, vkey=%04x, ansi=%04x\n",lParam,lpBuffer,nSize,vkey,ansi);
1144 if (lpBuffer && nSize)
1145 *lpBuffer = 0;
1146 return 0;
1149 /***********************************************************************
1150 * X11DRV_KEYBOARD_MapDeadKeysym
1152 static char KEYBOARD_MapDeadKeysym(KeySym keysym)
1154 switch (keysym)
1156 /* symbolic ASCII is the same as defined in rfc1345 */
1157 #ifdef XK_dead_tilde
1158 case XK_dead_tilde :
1159 #endif
1160 case 0x1000FE7E : /* Xfree's XK_Dtilde */
1161 return '~'; /* '? */
1162 #ifdef XK_dead_acute
1163 case XK_dead_acute :
1164 #endif
1165 case 0x1000FE27 : /* Xfree's XK_Dacute_accent */
1166 return 0xb4; /* '' */
1167 #ifdef XK_dead_circumflex
1168 case XK_dead_circumflex:
1169 #endif
1170 case 0x1000FE5E : /* Xfree's XK_Dcircumflex_accent */
1171 return '^'; /* '> */
1172 #ifdef XK_dead_grave
1173 case XK_dead_grave :
1174 #endif
1175 case 0x1000FE60 : /* Xfree's XK_Dgrave_accent */
1176 return '`'; /* '! */
1177 #ifdef XK_dead_diaeresis
1178 case XK_dead_diaeresis :
1179 #endif
1180 case 0x1000FE22 : /* Xfree's XK_Ddiaeresis */
1181 return 0xa8; /* ': */
1182 #ifdef XK_dead_cedilla
1183 case XK_dead_cedilla :
1184 return 0xb8; /* ', */
1185 #endif
1186 #ifdef XK_dead_macron
1187 case XK_dead_macron :
1188 return '-'; /* 'm isn't defined on iso-8859-x */
1189 #endif
1190 #ifdef XK_dead_breve
1191 case XK_dead_breve :
1192 return 0xa2; /* '( */
1193 #endif
1194 #ifdef XK_dead_abovedot
1195 case XK_dead_abovedot :
1196 return 0xff; /* '. */
1197 #endif
1198 #ifdef XK_dead_abovering
1199 case XK_dead_abovering :
1200 return '0'; /* '0 isn't defined on iso-8859-x */
1201 #endif
1202 #ifdef XK_dead_doubleacute
1203 case XK_dead_doubleacute :
1204 return 0xbd; /* '" */
1205 #endif
1206 #ifdef XK_dead_caron
1207 case XK_dead_caron :
1208 return 0xb7; /* '< */
1209 #endif
1210 #ifdef XK_dead_ogonek
1211 case XK_dead_ogonek :
1212 return 0xb2; /* '; */
1213 #endif
1214 /* FIXME: I don't know this three.
1215 case XK_dead_iota :
1216 return 'i';
1217 case XK_dead_voiced_sound :
1218 return 'v';
1219 case XK_dead_semivoiced_sound :
1220 return 's';
1223 TRACE_(keyboard)("no character for dead keysym 0x%08lx\n",keysym);
1224 return 0;
1227 /***********************************************************************
1228 * X11DRV_KEYBOARD_ToAscii
1230 * The ToAscii function translates the specified virtual-key code and keyboard
1231 * state to the corresponding Windows character or characters.
1233 * If the specified key is a dead key, the return value is negative. Otherwise,
1234 * it is one of the following values:
1235 * Value Meaning
1236 * 0 The specified virtual key has no translation for the current state of the keyboard.
1237 * 1 One Windows character was copied to the buffer.
1238 * 2 Two characters were copied to the buffer. This usually happens when a
1239 * dead-key character (accent or diacritic) stored in the keyboard layout cannot
1240 * be composed with the specified virtual key to form a single character.
1242 * FIXME : should do the above (return 2 for non matching deadchar+char combinations)
1245 INT16 X11DRV_KEYBOARD_ToAscii(
1246 UINT16 virtKey,UINT16 scanCode, LPBYTE lpKeyState,
1247 LPVOID lpChar, UINT16 flags)
1249 XKeyEvent e;
1250 KeySym keysym;
1251 static XComposeStatus cs;
1252 INT ret;
1253 int keyc;
1255 if (scanCode==0) {
1256 /* This happens when doing Alt+letter : a fake 'down arrow' key press
1257 event is generated by windows. Just ignore it. */
1258 TRACE_(keyboard)("scanCode=0, doing nothing\n");
1259 return 0;
1261 if (scanCode & 0x8000)
1263 TRACE_(keyboard)("Key UP, doing nothing\n" );
1264 return 0;
1266 e.display = display;
1267 e.keycode = 0;
1268 e.state = 0;
1269 if (lpKeyState[VK_SHIFT] & 0x80)
1270 e.state |= ShiftMask;
1271 if (lpKeyState[VK_CAPITAL] & 0x01)
1272 e.state |= LockMask;
1273 if (lpKeyState[VK_CONTROL] & 0x80)
1275 if (lpKeyState[VK_MENU] & 0x80)
1276 e.state |= AltGrMask;
1277 else
1278 e.state |= ControlMask;
1280 if (lpKeyState[VK_NUMLOCK] & 0x01)
1281 e.state |= NumLockMask;
1282 TRACE_(key)("(%04X, %04X) : faked state = %X\n",
1283 virtKey, scanCode, e.state);
1284 /* We exit on the first keycode found, to speed up the thing. */
1285 for (keyc=min_keycode; (keyc<=max_keycode) && (!e.keycode) ; keyc++)
1286 { /* Find a keycode that could have generated this virtual key */
1287 if ((keyc2vkey[keyc] & 0xFF) == virtKey)
1288 { /* We filter the extended bit, we don't know it */
1289 e.keycode = keyc; /* Store it temporarily */
1290 if ((EVENT_event_to_vkey(&e) & 0xFF) != virtKey) {
1291 e.keycode = 0; /* Wrong one (ex: because of the NumLock
1292 state), so set it to 0, we'll find another one */
1297 if ((virtKey>=VK_NUMPAD0) && (virtKey<=VK_NUMPAD9))
1298 e.keycode = TSXKeysymToKeycode(e.display, virtKey-VK_NUMPAD0+XK_KP_0);
1300 if (virtKey==VK_DECIMAL)
1301 e.keycode = TSXKeysymToKeycode(e.display, XK_KP_Decimal);
1303 if (!e.keycode)
1305 WARN_(keyboard)("Unknown virtual key %X !!! \n",virtKey);
1306 return virtKey; /* whatever */
1308 else TRACE_(keyboard)("Found keycode %d (0x%2X)\n",e.keycode,e.keycode);
1310 ret = TSXLookupString(&e, (LPVOID)lpChar, 2, &keysym, &cs);
1311 if (ret == 0)
1313 BYTE dead_char = 0;
1315 ((char*)lpChar)[1] = '\0';
1316 dead_char = KEYBOARD_MapDeadKeysym(keysym);
1317 if (dead_char)
1319 *(char*)lpChar = dead_char;
1320 ret = -1;
1322 else
1324 char *ksname;
1326 ksname = TSXKeysymToString(keysym);
1327 if (!ksname)
1328 ksname = "No Name";
1329 if ((keysym >> 8) != 0xff)
1331 ERR_(keyboard)("Please report: no char for keysym %04lX (%s) :\n",
1332 keysym, ksname);
1333 ERR_(keyboard)("(virtKey=%X,scanCode=%X,keycode=%X,state=%X)\n",
1334 virtKey, scanCode, e.keycode, e.state);
1338 else { /* ret = 1 */
1339 /* We have a special case to handle : Shift + arrow, shift + home, ...
1340 X returns a char for it, but Windows doesn't. Let's eat it. */
1341 if (!(lpKeyState[VK_NUMLOCK] & 0x01) /* NumLock is off */
1342 && (lpKeyState[VK_SHIFT] & 0x80) /* Shift is pressed */
1343 && (keysym>=XK_KP_0) && (keysym<=XK_KP_9))
1345 *(char*)lpChar = 0;
1346 ret = 0;
1349 /* We have another special case for delete key (XK_Delete) on an
1350 extended keyboard. X returns a char for it, but Windows doesn't */
1351 if (keysym == XK_Delete)
1353 *(char*)lpChar = 0;
1354 ret = 0;
1358 TRACE_(key)("ToAscii about to return %d with char %x\n",
1359 ret, *(char*)lpChar);
1360 return ret;
1363 /***********************************************************************
1364 * X11DRV_KEYBOARD_GetBeepActive
1366 BOOL X11DRV_KEYBOARD_GetBeepActive()
1368 XKeyboardState keyboard_state;
1370 TSXGetKeyboardControl(display, &keyboard_state);
1372 return keyboard_state.bell_percent != 0;
1375 /***********************************************************************
1376 * X11DRV_KEYBOARD_SetBeepActive
1378 void X11DRV_KEYBOARD_SetBeepActive(BOOL bActivate)
1380 XKeyboardControl keyboard_value;
1382 if(bActivate)
1383 keyboard_value.bell_percent = -1;
1384 else
1385 keyboard_value.bell_percent = 0;
1387 TSXChangeKeyboardControl(display, KBBellPercent, &keyboard_value);
1390 /***********************************************************************
1391 * X11DRV_KEYBOARD_Beep
1393 void X11DRV_KEYBOARD_Beep()
1395 TSXBell(display, 0);
1398 /***********************************************************************
1399 * X11DRV_KEYBOARD_GetDIState
1401 BOOL X11DRV_KEYBOARD_GetDIState(DWORD len, LPVOID ptr)
1403 if (len==256) {
1404 int keyc,vkey;
1406 memset(ptr,0,256);
1407 for (keyc=min_keycode;keyc<max_keycode;keyc++)
1409 /* X keycode to virtual key */
1410 vkey = keyc2vkey[keyc] & 0xFF;
1411 /* The windows scancode is keyc-min_keycode */
1412 if (InputKeyStateTable[vkey]&0x80) {
1413 ((LPBYTE)ptr)[keyc-min_keycode]=0x80;
1414 ((LPBYTE)ptr)[(keyc-min_keycode)|0x80]=0x80;
1417 return TRUE;
1419 WARN_(x11drv)("whoops, X11DRV_KEYBOARD_GetState got len %ld?\n", len);
1420 return TRUE;
1423 /***********************************************************************
1424 * X11DRV_KEYBOARD_GetDIData
1426 BOOL X11DRV_KEYBOARD_GetDIData(
1427 BYTE *keystate,
1428 DWORD dodsize, LPDIDEVICEOBJECTDATA dod,
1429 LPDWORD entries, DWORD flags)
1431 int keyc,n,vkey,xentries;
1433 /* FIXME !!! */
1435 if (entries)
1436 xentries = *entries;
1437 else
1438 xentries = 1;
1440 n = 0;
1442 for (keyc=min_keycode;(keyc<max_keycode) && (n<*entries);keyc++)
1444 /* X keycode to virtual key */
1445 vkey = keyc2vkey[keyc] & 0xFF;
1446 if (keystate[vkey] == (InputKeyStateTable[vkey]&0x80))
1447 continue;
1448 if (dod) {
1449 /* add an entry */
1450 dod[n].dwOfs = keyc-min_keycode; /* scancode */
1451 dod[n].dwData = InputKeyStateTable[vkey]&0x80;
1452 dod[n].dwTimeStamp = 0; /* umm */
1453 dod[n].dwSequence = 0; /* umm */
1454 n++;
1456 if (!(flags & DIGDD_PEEK))
1457 keystate[vkey] = InputKeyStateTable[vkey]&0x80;
1461 if (n) fprintf(stderr,"%d entries\n",n);
1462 *entries = n;
1464 return TRUE;
1467 #endif /* !defined(X_DISPLAY_MISSING) */