Fixed an entry in the keyboard translation table for Ctrl-Break.
[wine/dcerpc.git] / windows / x11drv / keyboard.c
blob5d97d72d47fefd49c1805e1e11d4e49f63fa1f67
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 /*** Hungarian keyboard layout (contributed by Zoltán Kovács) */
252 static const char main_key_HU[MAIN_LEN][4] =
254 "0§","1'~","2\"·","3+^","4!¢","5%°","6/²","7=`","8(ÿ","9)´","öÖ½","üܨ","óÓ¸",
255 "qQ\\","wW|","eE","rR","tT","zZ","uU","iIÍ","oOø","pP","õÕ÷","úÚ×",
256 "aA","sSð","dDÐ","fF[","gG]","hH","jJí","kK³","lL£","éÉ$","áÁß","ûÛ¤",
257 "yY>","xX#","cC&","vV@","bB{","nN}","mM",",?;",".:·","-_*",
258 "íÍ<"
261 /*** Layout table. Add your keyboard mappings to this list */
262 static struct {
263 WORD lang, ansi_codepage, oem_codepage;
264 const char (*key)[MAIN_LEN][4];
265 } main_key_tab[]={
266 {MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US), 1252, 437, &main_key_US},
267 {MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_UK), 1252, 850, &main_key_UK},
268 {MAKELANGID(LANG_GERMAN,SUBLANG_DEFAULT), 1252, 850, &main_key_DE},
269 {MAKELANGID(LANG_GERMAN,SUBLANG_DEFAULT), 1252, 850, &main_key_DE_nodead},
270 {MAKELANGID(LANG_GERMAN,SUBLANG_GERMAN_SWISS), 1252, 850, &main_key_SG},
271 {MAKELANGID(LANG_SWEDISH,SUBLANG_SWEDISH), 1252, 850, &main_key_SE},
272 {MAKELANGID(LANG_NORWEGIAN,SUBLANG_DEFAULT), 1252, 865, &main_key_NO},
273 {MAKELANGID(LANG_DANISH,SUBLANG_DEFAULT), 1252, 865, &main_key_DA},
274 {MAKELANGID(LANG_FRENCH,SUBLANG_DEFAULT), 1252, 850, &main_key_FR},
275 {MAKELANGID(LANG_FRENCH,SUBLANG_FRENCH_CANADIAN), 1252, 863, &main_key_CF},
276 {MAKELANGID(LANG_FRENCH,SUBLANG_FRENCH_BELGIAN), 1252, 850, &main_key_BE},
277 {MAKELANGID(LANG_FRENCH,SUBLANG_FRENCH_SWISS), 1252, 850, &main_key_SF},
278 {MAKELANGID(LANG_WALON,SUBLANG_DEFAULT), 1252, 850, &main_key_BE},
279 {MAKELANGID(LANG_PORTUGUESE,SUBLANG_DEFAULT), 1252, 860, &main_key_PT},
280 {MAKELANGID(LANG_FINNISH,SUBLANG_DEFAULT), 1252, 850, &main_key_FI},
281 {MAKELANGID(LANG_RUSSIAN,SUBLANG_DEFAULT), 1251, 866, &main_key_RU},
282 {MAKELANGID(LANG_SPANISH,SUBLANG_DEFAULT), 1252, 850, &main_key_ES},
283 {MAKELANGID(LANG_DUTCH,SUBLANG_DUTCH_BELGIAN), 1252, 850, &main_key_BE},
284 {MAKELANGID(LANG_ITALIAN,SUBLANG_DEFAULT), 1252, 850, &main_key_IT},
285 {MAKELANGID(LANG_ICELANDIC,SUBLANG_DEFAULT), 1252, 850, &main_key_IS},
286 {MAKELANGID(LANG_HUNGARIAN,SUBLANG_DEFAULT), 1252, 850, &main_key_HU},
288 {0} /* sentinel */
290 static unsigned kbd_layout=0; /* index into above table of layouts */
292 /* maybe more of these scancodes should be extended? */
293 /* extended must be set for ALT_R, CTRL_R,
294 INS, DEL, HOME, END, PAGE_UP, PAGE_DOWN, ARROW keys,
295 keypad / and keypad ENTER (SDK 3.1 Vol.3 p 138) */
296 /* FIXME should we set extended bit for NumLock ? My
297 * Windows does ... DF */
298 /* Yes, to distinguish based on scan codes, also
299 for PrtScn key ... GA */
301 static const int special_key_vkey[] =
303 VK_BACK, VK_TAB, 0, VK_CLEAR, 0, VK_RETURN, 0, 0, /* FF08 */
304 0, 0, 0, VK_PAUSE, VK_SCROLL, 0, 0, 0, /* FF10 */
305 0, 0, 0, VK_ESCAPE /* FF18 */
307 static const int special_key_scan[] =
309 0x0E, 0x0F, 0, /*?*/ 0, 0, 0x1C, 0, 0, /* FF08 */
310 0, 0, 0, 0x45, 0x46, 0 , 0, 0, /* FF10 */
311 0, 0, 0, 0x01 /* FF18 */
314 static const int cursor_key_vkey[] =
316 VK_HOME, VK_LEFT, VK_UP, VK_RIGHT, VK_DOWN, VK_PRIOR,
317 VK_NEXT, VK_END /* FF50 */
319 static const int cursor_key_scan[] =
321 0x147, 0x14B, 0x148, 0x14D, 0x150, 0x149, 0x151, 0x14F /* FF50 */
324 static const int misc_key_vkey[] =
326 VK_SELECT, VK_SNAPSHOT, VK_EXECUTE, VK_INSERT, 0, 0, 0, 0, /* FF60 */
327 VK_CANCEL, VK_HELP, VK_CANCEL, VK_CANCEL /* FF68 */
329 static const int misc_key_scan[] =
331 /*?*/ 0, 0x137, /*?*/ 0, 0x152, 0, 0, 0, 0, /* FF60 */
332 /*?*/ 0, /*?*/ 0, 0x38, 0x146 /* FF68 */
335 static const int keypad_key_vkey[] =
337 0, VK_NUMLOCK, /* FF7E */
338 0, 0, 0, 0, 0, 0, 0, 0, /* FF80 */
339 0, 0, 0, 0, 0, VK_RETURN, 0, 0, /* FF88 */
340 0, 0, 0, 0, 0, VK_HOME, VK_LEFT, VK_UP, /* FF90 */
341 VK_RIGHT, VK_DOWN, VK_PRIOR, VK_NEXT, VK_END, 0,
342 VK_INSERT, VK_DELETE, /* FF98 */
343 0, 0, 0, 0, 0, 0, 0, 0, /* FFA0 */
344 0, 0, VK_MULTIPLY, VK_ADD, VK_SEPARATOR, VK_SUBTRACT,
345 VK_DECIMAL, VK_DIVIDE, /* FFA8 */
346 VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3, VK_NUMPAD4,
347 VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, /* FFB0 */
348 VK_NUMPAD8, VK_NUMPAD9 /* FFB8 */
350 static const int keypad_key_scan[] =
352 0x138, 0x145, /* FF7E */
353 0, 0, 0, 0, 0, 0, 0, 0, /* FF80 */
354 0, 0, 0, 0, 0, 0x11C, 0, 0, /* FF88 */
355 0, 0, 0, 0, 0, 0x47, 0x4B, 0x48, /* FF90 */
356 0x4D, 0x50, 0x49, 0x51, 0x4F, 0x4C, 0x52, 0x53, /* FF98 */
357 0, 0, 0, 0, 0, 0, 0, 0, /* FFA0 */
358 0, 0, 0x37, 0x4E, /*?*/ 0, 0x4A, 0x53, 0x135, /* FFA8 */
359 0x52, 0x4F, 0x50, 0x51, 0x4B, 0x4C, 0x4D, 0x47, /* FFB0 */
360 0x48, 0x49 /* FFB8 */
363 static const int function_key_vkey[] =
365 VK_F1, VK_F2, /* FFBE */
366 VK_F3, VK_F4, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9, VK_F10, /* FFC0 */
367 VK_F11, VK_F12, VK_F13, VK_F14, VK_F15, VK_F16 /* FFC8 */
369 static const int function_key_scan[] =
371 0x3B, 0x3C, /* FFBE */
372 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, /* FFC0 */
373 0x57, 0x58, 0, 0, 0, 0 /* FFC8 */
376 static const int modifier_key_vkey[] =
378 VK_SHIFT, VK_SHIFT, VK_CONTROL, VK_CONTROL, VK_CAPITAL, 0, /* FFE1 */
379 VK_MENU, VK_MENU, VK_MENU, VK_MENU /* FFE7 */
381 static const int modifier_key_scan[] =
383 0x2A, 0x36, 0x1D, 0x11D, 0x3A, 0, /* FFE1 */
384 0x38, 0x138, 0x38, 0x138 /* FFE7 */
387 /* Returns the Windows virtual key code associated with the X event <e> */
388 static WORD EVENT_event_to_vkey( XKeyEvent *e)
390 KeySym keysym;
392 TSXLookupString(e, NULL, 0, &keysym, NULL);
394 if ((keysym >= 0xFFAE) && (keysym <= 0xFFB9) && (keysym != 0xFFAF)
395 && (e->state & NumLockMask))
396 /* Only the Keypad keys 0-9 and . send different keysyms
397 * depending on the NumLock state */
398 return keypad_key_vkey[(keysym & 0xFF) - 0x7E];
400 return keyc2vkey[e->keycode];
403 static BOOL NumState=FALSE, CapsState=FALSE;
405 /**********************************************************************
406 * KEYBOARD_GenerateMsg
408 * Generate Down+Up messages when NumLock or CapsLock is pressed.
410 * Convention : called with vkey only VK_NUMLOCK or VK_CAPITAL
413 static void KEYBOARD_GenerateMsg( WORD vkey, WORD scan, int Evtype, INT event_x, INT event_y,
414 DWORD event_time )
416 BOOL * State = (vkey==VK_NUMLOCK? &NumState : &CapsState);
417 DWORD up, down;
419 if (*State) {
420 /* The INTERMEDIARY state means : just after a 'press' event, if a 'release' event comes,
421 don't treat it. It's from the same key press. Then the state goes to ON.
422 And from there, a 'release' event will switch off the toggle key. */
423 *State=FALSE;
424 TRACE_(keyboard)("INTERM : don\'t treat release of toggle key. InputKeyStateTable[%#x] = %#x\n",vkey,pKeyStateTable[vkey]);
425 } else
427 down = (vkey==VK_NUMLOCK ? KEYEVENTF_EXTENDEDKEY : 0);
428 up = (vkey==VK_NUMLOCK ? KEYEVENTF_EXTENDEDKEY : 0) | KEYEVENTF_KEYUP;
429 if ( pKeyStateTable[vkey] & 0x1 ) /* it was ON */
431 if (Evtype!=KeyPress)
433 TRACE_(keyboard)("ON + KeyRelease => generating DOWN and UP messages.\n");
434 KEYBOARD_SendEvent( vkey, scan, down,
435 event_x, event_y, event_time );
436 KEYBOARD_SendEvent( vkey, scan, up,
437 event_x, event_y, event_time );
438 *State=FALSE;
439 pKeyStateTable[vkey] &= ~0x01; /* Toggle state to off. */
442 else /* it was OFF */
443 if (Evtype==KeyPress)
445 TRACE_(keyboard)("OFF + Keypress => generating DOWN and UP messages.\n");
446 KEYBOARD_SendEvent( vkey, scan, down,
447 event_x, event_y, event_time );
448 KEYBOARD_SendEvent( vkey, scan, up,
449 event_x, event_y, event_time );
450 *State=TRUE; /* Goes to intermediary state before going to ON */
451 pKeyStateTable[vkey] |= 0x01; /* Toggle state to on. */
456 /***********************************************************************
457 * KEYBOARD_UpdateOneState
459 * Updates internal state for <vkey>, depending on key <state> under X
462 static void KEYBOARD_UpdateOneState ( int vkey, int state )
464 /* Do something if internal table state != X state for keycode */
465 if (((pKeyStateTable[vkey] & 0x80)!=0) != state)
467 TRACE_(keyboard)("Adjusting state for vkey %#.2x. State before %#.2x \n",
468 vkey, pKeyStateTable[vkey]);
470 /* Fake key being pressed inside wine */
471 KEYBOARD_SendEvent( vkey, 0, state? 0 : KEYEVENTF_KEYUP,
472 0, 0, GetTickCount() );
474 TRACE_(keyboard)("State after %#.2x \n",pKeyStateTable[vkey]);
478 /***********************************************************************
479 * X11DRV_KEYBOARD_UpdateState
481 * Update modifiers state (Ctrl, Alt, Shift)
482 * when window is activated (called by EVENT_FocusIn in event.c)
484 * This handles the case where one uses Ctrl+... Alt+... or Shift+.. to switch
485 * from wine to another application and back.
486 * Toggle keys are handled in HandleEvent. (because XQueryKeymap says nothing
487 * about them)
489 void X11DRV_KEYBOARD_UpdateState ( void )
491 /* extract a bit from the char[32] bit suite */
492 #define KeyState(keycode) ((keys_return[keycode/8] & (1<<(keycode%8)))!=0)
494 char keys_return[32];
496 TRACE_(keyboard)("called\n");
497 if (!TSXQueryKeymap(display, keys_return)) {
498 ERR_(keyboard)("Error getting keymap !");
499 return;
502 /* Adjust the ALT and CONTROL state if any has been changed outside wine */
503 KEYBOARD_UpdateOneState(VK_MENU, KeyState(kcAlt));
504 KEYBOARD_UpdateOneState(VK_CONTROL, KeyState(kcControl));
505 KEYBOARD_UpdateOneState(VK_SHIFT, KeyState(kcShift));
506 #undef KeyState
509 /***********************************************************************
510 * X11DRV_KEYBOARD_HandleEvent
512 * Handle a X key event
514 void X11DRV_KEYBOARD_HandleEvent( WND *pWnd, XKeyEvent *event )
516 char Str[24];
517 XComposeStatus cs;
518 KeySym keysym;
519 WORD vkey = 0, bScan;
520 DWORD dwFlags;
521 static BOOL force_extended = FALSE; /* hack for AltGr translation */
523 int ascii_chars;
525 INT event_x = (pWnd? pWnd->rectWindow.left : 0) + event->x;
526 INT event_y = (pWnd? pWnd->rectWindow.top : 0) + event->y;
527 DWORD event_time = event->time - MSG_WineStartTicks;
529 /* this allows support for dead keys */
530 if ((event->keycode >> 8) == 0x10)
531 event->keycode=(event->keycode & 0xff);
533 ascii_chars = TSXLookupString(event, Str, 1, &keysym, &cs);
535 TRACE_(key)("EVENT_key : state = %X\n", event->state);
536 if (keysym == XK_Mode_switch)
538 TRACE_(key)("Alt Gr key event received\n");
539 event->keycode = kcControl; /* Simulate Control */
540 X11DRV_KEYBOARD_HandleEvent( pWnd, event );
542 event->keycode = kcAlt; /* Simulate Alt */
543 force_extended = TRUE;
544 X11DRV_KEYBOARD_HandleEvent( pWnd, event );
545 force_extended = FALSE;
546 return;
549 Str[ascii_chars] = '\0';
550 if (TRACE_ON(key)){
551 char *ksname;
553 ksname = TSXKeysymToString(keysym);
554 if (!ksname)
555 ksname = "No Name";
556 TRACE_(key)("%s : keysym=%lX (%s), ascii chars=%u / %X / '%s'\n",
557 (event->type == KeyPress) ? "KeyPress" : "KeyRelease",
558 keysym, ksname, ascii_chars, Str[0] & 0xff, Str);
561 vkey = EVENT_event_to_vkey(event);
562 if (force_extended) vkey |= 0x100;
564 TRACE_(key)("keycode 0x%x converted to vkey 0x%x\n",
565 event->keycode, vkey);
567 if (vkey)
569 switch (vkey & 0xff)
571 case VK_NUMLOCK:
572 KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, event->type, event_x, event_y,
573 event_time );
574 break;
575 case VK_CAPITAL:
576 TRACE_(keyboard)("Caps Lock event. (type %d). State before : %#.2x\n",event->type,pKeyStateTable[vkey]);
577 KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, event->type, event_x, event_y,
578 event_time );
579 TRACE_(keyboard)("State after : %#.2x\n",pKeyStateTable[vkey]);
580 break;
581 default:
582 /* Adjust the NUMLOCK state if it has been changed outside wine */
583 if (!(pKeyStateTable[VK_NUMLOCK] & 0x01) != !(event->state & NumLockMask))
585 TRACE_(keyboard)("Adjusting NumLock state. \n");
586 KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, KeyPress, event_x, event_y,
587 event_time );
588 KEYBOARD_GenerateMsg( VK_NUMLOCK, 0x45, KeyRelease, event_x, event_y,
589 event_time );
591 /* Adjust the CAPSLOCK state if it has been changed outside wine */
592 if (!(pKeyStateTable[VK_CAPITAL] & 0x01) != !(event->state & LockMask))
594 TRACE_(keyboard)("Adjusting Caps Lock state.\n");
595 KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, KeyPress, event_x, event_y,
596 event_time );
597 KEYBOARD_GenerateMsg( VK_CAPITAL, 0x3A, KeyRelease, event_x, event_y,
598 event_time );
600 /* Not Num nor Caps : end of intermediary states for both. */
601 NumState = FALSE;
602 CapsState = FALSE;
604 bScan = keyc2scan[event->keycode] & 0xFF;
605 TRACE_(key)("bScan = 0x%02x.\n", bScan);
607 dwFlags = 0;
608 if ( event->type == KeyRelease ) dwFlags |= KEYEVENTF_KEYUP;
609 if ( vkey & 0x100 ) dwFlags |= KEYEVENTF_EXTENDEDKEY;
610 if ( force_extended ) dwFlags |= KEYEVENTF_WINE_FORCEEXTENDED;
612 KEYBOARD_SendEvent( vkey & 0xff, bScan, dwFlags,
613 event_x, event_y, event_time );
618 /**********************************************************************
619 * X11DRV_KEYBOARD_DetectLayout
621 * Called from X11DRV_KEYBOARD_Init
622 * This routine walks through the defined keyboard layouts and selects
623 * whichever matches most closely.
625 static void
626 X11DRV_KEYBOARD_DetectLayout (void)
628 unsigned current, match, mismatch, seq;
629 int score, keyc, i, key, pkey, ok, syms;
630 KeySym keysym;
631 const char (*lkey)[MAIN_LEN][4];
632 unsigned max_seq = 0;
633 int max_score = 0, ismatch = 0;
634 char ckey[4] =
635 {0, 0, 0, 0};
637 syms = keysyms_per_keycode;
638 if (syms > 4) {
639 WARN_(keyboard)("%d keysyms per keycode not supported, set to 4", syms);
640 syms = 4;
642 for (current = 0; main_key_tab[current].lang; current++) {
643 TRACE_(keyboard)("Attempting to match against layout %04x\n",
644 main_key_tab[current].lang);
645 match = 0;
646 mismatch = 0;
647 score = 0;
648 seq = 0;
649 lkey = main_key_tab[current].key;
650 pkey = -1;
651 for (keyc = min_keycode; keyc <= max_keycode; keyc++) {
652 /* get data for keycode from X server */
653 for (i = 0; i < syms; i++) {
654 keysym = TSXKeycodeToKeysym (display, keyc, i);
655 /* Allow both one-byte and two-byte national keysyms */
656 if ((keysym < 0x800) && (keysym != ' '))
657 ckey[i] = keysym & 0xFF;
658 else {
659 ckey[i] = KEYBOARD_MapDeadKeysym(keysym);
662 if (ckey[0]) {
663 /* search for a match in layout table */
664 /* right now, we just find an absolute match for defined positions */
665 /* (undefined positions are ignored, so if it's defined as "3#" in */
666 /* the table, it's okay that the X server has "3#£", for example) */
667 /* however, the score will be higher for longer matches */
668 for (key = 0; key < MAIN_LEN; key++) {
669 for (ok = 0, i = 0; (ok >= 0) && (i < syms); i++) {
670 if ((*lkey)[key][i] && ((*lkey)[key][i] == ckey[i]))
671 ok++;
672 if ((*lkey)[key][i] && ((*lkey)[key][i] != ckey[i]))
673 ok = -1;
675 if (ok > 0) {
676 score += ok;
677 break;
680 /* count the matches and mismatches */
681 if (ok > 0) {
682 match++;
683 /* and how much the keycode order matches */
684 if (key > pkey) seq++;
685 pkey = key;
686 } else {
687 TRACE_(key)("mismatch for keycode %d, character %c\n", keyc,
688 ckey[0]);
689 mismatch++;
690 score -= syms;
694 TRACE_(keyboard)("matches=%d, mismatches=%d, score=%d\n",
695 match, mismatch, score);
696 if ((score > max_score) ||
697 ((score == max_score) && (seq > max_seq))) {
698 /* best match so far */
699 kbd_layout = current;
700 max_score = score;
701 max_seq = seq;
702 ismatch = !mismatch;
705 /* we're done, report results if necessary */
706 if (!ismatch) {
707 FIXME_(keyboard)(
708 "Your keyboard layout was not found!\n"
709 "Instead using closest match (%04x) for scancode mapping.\n"
710 "Please define your layout in windows/x11drv/keyboard.c and submit them\n"
711 "to us for inclusion into future Wine releases.\n"
712 "See documentation/keyboard for more information.\n",
713 main_key_tab[kbd_layout].lang);
715 TRACE_(keyboard)("detected layout is %04x\n", main_key_tab[kbd_layout].lang);
718 /**********************************************************************
719 * X11DRV_KEYBOARD_Init
721 void X11DRV_KEYBOARD_Init(void)
723 KeySym *ksp;
724 XModifierKeymap *mmp;
725 KeySym keysym;
726 KeyCode *kcp;
727 XKeyEvent e2;
728 WORD scan, vkey, OEMvkey;
729 int keyc, i, keyn, syms;
730 char ckey[4]={0,0,0,0};
731 const char (*lkey)[MAIN_LEN][4];
733 TSXDisplayKeycodes(display, &min_keycode, &max_keycode);
734 ksp = TSXGetKeyboardMapping(display, min_keycode,
735 max_keycode + 1 - min_keycode, &keysyms_per_keycode);
736 /* We are only interested in keysyms_per_keycode.
737 There is no need to hold a local copy of the keysyms table */
738 TSXFree(ksp);
739 mmp = TSXGetModifierMapping(display);
740 kcp = mmp->modifiermap;
741 for (i = 0; i < 8; i += 1) /* There are 8 modifier keys */
743 int j;
745 for (j = 0; j < mmp->max_keypermod; j += 1, kcp += 1)
746 if (*kcp)
748 int k;
750 for (k = 0; k < keysyms_per_keycode; k += 1)
751 if (TSXKeycodeToKeysym(display, *kcp, k) == XK_Mode_switch)
753 AltGrMask = 1 << i;
754 TRACE_(key)("AltGrMask is %x\n", AltGrMask);
756 else if (TSXKeycodeToKeysym(display, *kcp, k) == XK_Num_Lock)
758 NumLockMask = 1 << i;
759 TRACE_(key)("NumLockMask is %x\n", NumLockMask);
763 TSXFreeModifiermap(mmp);
765 /* Detect the keyboard layout */
766 X11DRV_KEYBOARD_DetectLayout();
767 lkey = main_key_tab[kbd_layout].key;
768 syms = (keysyms_per_keycode > 4) ? 4 : keysyms_per_keycode;
770 /* Now build two conversion arrays :
771 * keycode -> vkey + scancode + extended
772 * vkey + extended -> keycode */
774 e2.display = display;
775 e2.state = 0;
777 OEMvkey = VK_OEM_7; /* next is available. */
778 for (keyc = min_keycode; keyc <= max_keycode; keyc++)
780 e2.keycode = (KeyCode)keyc;
781 TSXLookupString(&e2, NULL, 0, &keysym, NULL);
782 vkey = 0; scan = 0;
783 if (keysym) /* otherwise, keycode not used */
785 if ((keysym >> 8) == 0xFF) /* non-character key */
787 int key = keysym & 0xff;
789 if (key >= 0x08 && key <= 0x1B) { /* special key */
790 vkey = special_key_vkey[key - 0x08];
791 scan = special_key_scan[key - 0x08];
792 } else if (key >= 0x50 && key <= 0x57) { /* cursor key */
793 vkey = cursor_key_vkey[key - 0x50];
794 scan = cursor_key_scan[key - 0x50];
795 } else if (key >= 0x60 && key <= 0x6B) { /* miscellaneous key */
796 vkey = misc_key_vkey[key - 0x60];
797 scan = misc_key_scan[key - 0x60];
798 } else if (key >= 0x7E && key <= 0xB9) { /* keypad key */
799 vkey = keypad_key_vkey[key - 0x7E];
800 scan = keypad_key_scan[key - 0x7E];
801 } else if (key >= 0xBE && key <= 0xCD) { /* function key */
802 vkey = function_key_vkey[key - 0xBE] | 0x100; /* set extended bit */
803 scan = function_key_scan[key - 0xBE];
804 } else if (key >= 0xE1 && key <= 0xEA) { /* modifier key */
805 vkey = modifier_key_vkey[key - 0xE1];
806 scan = modifier_key_scan[key - 0xE1];
807 } else if (key == 0xFF) { /* DEL key */
808 vkey = VK_DELETE;
809 scan = 0x153;
811 /* set extended bit when necessary */
812 if (scan & 0x100) vkey |= 0x100;
813 } else if (keysym == 0x20) { /* Spacebar */
814 vkey = VK_SPACE;
815 scan = 0x39;
816 } else {
817 /* we seem to need to search the layout-dependent scancodes */
818 int maxlen=0,maxval=-1,ok;
819 for (i=0; i<syms; i++) {
820 keysym = TSXKeycodeToKeysym(display, keyc, i);
821 if ((keysym<0x800) && (keysym!=' ')) {
822 ckey[i] = keysym & 0xFF;
823 } else {
824 ckey[i] = KEYBOARD_MapDeadKeysym(keysym);
827 /* find key with longest match streak */
828 for (keyn=0; keyn<MAIN_LEN; keyn++) {
829 for (ok=(*lkey)[keyn][i=0]; ok&&(i<4); i++)
830 if ((*lkey)[keyn][i] && (*lkey)[keyn][i]!=ckey[i]) ok=0;
831 if (ok||(i>maxlen)) {
832 maxlen=i; maxval=keyn;
834 if (ok) break;
836 if (maxval>=0) {
837 /* got it */
838 scan = main_key_scan[maxval];
842 /* find a suitable layout-dependent VK code */
843 /* (most Winelib apps ought to be able to work without layout tables!) */
844 for (i = 0; (i < keysyms_per_keycode) && (!vkey); i++)
846 keysym = TSXLookupKeysym(&e2, i);
847 if ((keysym >= VK_0 && keysym <= VK_9)
848 || (keysym >= VK_A && keysym <= VK_Z)) {
849 vkey = keysym;
853 for (i = 0; (i < keysyms_per_keycode) && (!vkey); i++)
855 keysym = TSXLookupKeysym(&e2, i);
856 switch (keysym)
858 case ';': vkey = VK_OEM_1; break;
859 case '/': vkey = VK_OEM_2; break;
860 case '`': vkey = VK_OEM_3; break;
861 case '[': vkey = VK_OEM_4; break;
862 case '\\': vkey = VK_OEM_5; break;
863 case ']': vkey = VK_OEM_6; break;
864 case '\'': vkey = VK_OEM_7; break;
865 case ',': vkey = VK_OEM_COMMA; break;
866 case '.': vkey = VK_OEM_PERIOD; break;
867 case '-': vkey = VK_OEM_MINUS; break;
868 case '+': vkey = VK_OEM_PLUS; break;
872 if (!vkey)
874 /* Others keys: let's assign OEM virtual key codes in the allowed range,
875 * that is ([0xba,0xc0], [0xdb,0xe4], 0xe6 (given up) et [0xe9,0xf5]) */
876 switch (++OEMvkey)
878 case 0xc1 : OEMvkey=0xdb; break;
879 case 0xe5 : OEMvkey=0xe9; break;
880 case 0xf6 : OEMvkey=0xf5; WARN_(keyboard)("No more OEM vkey available!\n");
883 vkey = OEMvkey;
885 if (TRACE_ON(keyboard))
887 TRACE_(keyboard)("OEM specific virtual key %X assigned to keycode %X:\n",
888 OEMvkey, e2.keycode);
889 TRACE_(keyboard)("(");
890 for (i = 0; i < keysyms_per_keycode; i += 1)
892 char *ksname;
894 keysym = TSXLookupKeysym(&e2, i);
895 ksname = TSXKeysymToString(keysym);
896 if (!ksname)
897 ksname = "NoSymbol";
898 DPRINTF( "%lX (%s) ", keysym, ksname);
900 DPRINTF(")\n");
904 keyc2vkey[e2.keycode] = vkey;
905 keyc2scan[e2.keycode] = scan;
906 } /* for */
908 /* If some keys still lack scancodes, assign some arbitrary ones to them now */
909 for (scan = 0x60, keyc = min_keycode; keyc <= max_keycode; keyc++)
910 if (keyc2vkey[keyc]&&!keyc2scan[keyc]) {
911 char *ksname;
912 keysym = TSXKeycodeToKeysym(display, keyc, 0);
913 ksname = TSXKeysymToString(keysym);
914 if (!ksname) ksname = "NoSymbol";
916 /* should make sure the scancode is unassigned here, but >=0x60 currently always is */
918 TRACE_(key)("assigning scancode %02x to unidentified keycode %02x (%s)\n",scan,keyc,ksname);
919 keyc2scan[keyc]=scan++;
922 /* Now store one keycode for each modifier. Used to simulate keypresses. */
923 kcControl = TSXKeysymToKeycode(display, XK_Control_L);
924 kcAlt = TSXKeysymToKeycode(display, XK_Alt_L);
925 if (!kcAlt) kcAlt = TSXKeysymToKeycode(display, XK_Meta_L);
926 kcShift = TSXKeysymToKeycode(display, XK_Shift_L);
927 kcNumLock = TSXKeysymToKeycode(display, XK_Num_Lock);
928 kcCapsLock = TSXKeysymToKeycode(display, XK_Caps_Lock);
931 /***********************************************************************
932 * X11DRV_KEYBOARD_VkKeyScan
934 WORD X11DRV_KEYBOARD_VkKeyScan(CHAR cChar)
936 KeyCode keycode;
937 KeySym keysym;
938 int i,index;
939 int highbyte=0;
941 /* char->keysym (same for ANSI chars) */
942 keysym=(unsigned char) cChar;/* (!) cChar is signed */
943 if (keysym<=27) keysym+=0xFF00;/*special chars : return, backspace...*/
945 keycode = TSXKeysymToKeycode(display, keysym); /* keysym -> keycode */
946 if (!keycode)
947 { /* It didn't work ... let's try with deadchar code. */
948 keycode = TSXKeysymToKeycode(display, keysym | 0xFE00);
951 TRACE_(keyboard)("VkKeyScan '%c'(%#lx, %lu): got keycode %#.2x\n",
952 cChar,keysym,keysym,keycode);
954 if (keycode)
956 for (index=-1, i=0; (i<8) && (index<0); i++) /* find shift state */
957 if (TSXKeycodeToKeysym(display,keycode,i)==keysym) index=i;
958 switch (index) {
959 case -1 :
960 WARN_(keyboard)("Keysym %lx not found while parsing the keycode table\n",keysym); break;
961 case 0 : break;
962 case 1 : highbyte = 0x0100; break;
963 case 2 : highbyte = 0x0600; break;
964 case 3 : highbyte = 0x0700; break;
965 default : ERR_(keyboard)("index %d found by XKeycodeToKeysym. please report! \n",index);
968 index : 0 adds 0x0000
969 index : 1 adds 0x0100 (shift)
970 index : ? adds 0x0200 (ctrl)
971 index : 2 adds 0x0600 (ctrl+alt)
972 index : 3 adds 0x0700 (ctrl+alt+shift)
975 TRACE_(keyboard)(" ... returning %#.2x\n", keyc2vkey[keycode]+highbyte);
976 return keyc2vkey[keycode]+highbyte; /* keycode -> (keyc2vkey) vkey */
979 /***********************************************************************
980 * X11DRV_KEYBOARD_MapVirtualKey
982 UINT16 X11DRV_KEYBOARD_MapVirtualKey(UINT16 wCode, UINT16 wMapType)
984 #define returnMVK(value) { TRACE_(keyboard)("returning 0x%x.\n",value); return value; }
986 TRACE_(keyboard)("MapVirtualKey wCode=0x%x wMapType=%d ... \n",
987 wCode,wMapType);
988 switch(wMapType) {
989 case 0: { /* vkey-code to scan-code */
990 /* let's do vkey -> keycode -> scan */
991 int keyc;
992 for (keyc=min_keycode; keyc<=max_keycode; keyc++)
993 if ((keyc2vkey[keyc] & 0xFF) == wCode)
994 returnMVK (keyc2scan[keyc] & 0xFF);
995 TRACE_(keyboard)("returning no scan-code.\n");
996 return 0; }
998 case 1: { /* scan-code to vkey-code */
999 /* let's do scan -> keycode -> vkey */
1000 int keyc;
1001 for (keyc=min_keycode; keyc<=max_keycode; keyc++)
1002 if ((keyc2scan[keyc] & 0xFF) == (wCode & 0xFF))
1003 returnMVK (keyc2vkey[keyc] & 0xFF);
1004 TRACE_(keyboard)("returning no vkey-code.\n");
1005 return 0; }
1007 case 2: { /* vkey-code to unshifted ANSI code */
1008 /* (was FIXME) : what does unshifted mean ? 'a' or 'A' ? */
1009 /* My Windows returns 'A'. */
1010 /* let's do vkey -> keycode -> (XLookupString) ansi char */
1011 XKeyEvent e;
1012 KeySym keysym;
1013 int keyc;
1014 char s[2];
1015 e.display = display;
1016 e.state = 0; /* unshifted */
1018 e.keycode = 0;
1019 /* We exit on the first keycode found, to speed up the thing. */
1020 for (keyc=min_keycode; (keyc<=max_keycode) && (!e.keycode) ; keyc++)
1021 { /* Find a keycode that could have generated this virtual key */
1022 if ((keyc2vkey[keyc] & 0xFF) == wCode)
1023 { /* We filter the extended bit, we don't know it */
1024 e.keycode = keyc; /* Store it temporarily */
1025 if ((EVENT_event_to_vkey(&e) & 0xFF) != wCode) {
1026 e.keycode = 0; /* Wrong one (ex: because of the NumLock
1027 state), so set it to 0, we'll find another one */
1032 if ((wCode>=VK_NUMPAD0) && (wCode<=VK_NUMPAD9))
1033 e.keycode = TSXKeysymToKeycode(e.display, wCode-VK_NUMPAD0+XK_KP_0);
1035 if (wCode==VK_DECIMAL)
1036 e.keycode = TSXKeysymToKeycode(e.display, XK_KP_Decimal);
1038 if (!e.keycode)
1040 WARN_(keyboard)("Unknown virtual key %X !!! \n", wCode);
1041 return 0; /* whatever */
1043 TRACE_(keyboard)("Found keycode %d (0x%2X)\n",e.keycode,e.keycode);
1045 if (TSXLookupString(&e, s, 2, &keysym, NULL))
1046 returnMVK (*s);
1048 TRACE_(keyboard)("returning no ANSI.\n");
1049 return 0;
1052 case 3: /* **NT only** scan-code to vkey-code but distinguish between */
1053 /* left and right */
1054 FIXME_(keyboard)(" stub for NT\n");
1055 return 0;
1057 default: /* reserved */
1058 WARN_(keyboard)("Unknown wMapType %d !\n",
1059 wMapType);
1060 return 0;
1062 return 0;
1065 /***********************************************************************
1066 * X11DRV_KEYBOARD_GetKeyNameText
1068 INT16 X11DRV_KEYBOARD_GetKeyNameText(LONG lParam, LPSTR lpBuffer, INT16 nSize)
1070 int vkey, ansi, scanCode;
1071 KeyCode keyc;
1072 KeySym keys;
1073 char *name;
1075 scanCode = lParam >> 16;
1076 scanCode &= 0x1ff; /* keep "extended-key" flag with code */
1078 /* FIXME: should use MVK type 3 (NT version that distinguishes right and left */
1079 vkey = X11DRV_KEYBOARD_MapVirtualKey(scanCode, 1);
1081 /* handle "don't care" bit (0x02000000) */
1082 if (!(lParam & 0x02000000)) {
1083 switch (vkey) {
1084 case VK_LSHIFT:
1085 case VK_RSHIFT:
1086 vkey = VK_SHIFT;
1087 break;
1088 case VK_LCONTROL:
1089 case VK_RCONTROL:
1090 vkey = VK_CONTROL;
1091 break;
1092 case VK_LMENU:
1093 case VK_RMENU:
1094 vkey = VK_MENU;
1095 break;
1096 default:
1097 break;
1101 ansi = X11DRV_KEYBOARD_MapVirtualKey(vkey, 2);
1102 TRACE_(keyboard)("scan 0x%04x, vkey 0x%04x, ANSI 0x%04x\n",
1103 scanCode, vkey, ansi);
1105 /* first get the name of the "regular" keys which is the Upper case
1106 value of the keycap imprint. */
1107 if ( ((ansi >= 0x21) && (ansi <= 0x7e)) &&
1108 (scanCode != 0x137) && /* PrtScn */
1109 (scanCode != 0x135) && /* numpad / */
1110 (scanCode != 0x37 ) && /* numpad * */
1111 (scanCode != 0x4a ) && /* numpad - */
1112 (scanCode != 0x4e ) ) /* numpad + */
1114 if ((nSize >= 2) && lpBuffer)
1116 *lpBuffer = toupper((char)ansi);
1117 *(lpBuffer+1) = 0;
1118 return 1;
1120 else
1121 return 0;
1124 /* FIXME: horrible hack to fix function keys. Windows reports scancode
1125 without "extended-key" flag. However Wine generates scancode
1126 *with* "extended-key" flag. Seems to occur *only* for the
1127 function keys. Soooo.. We will leave the table alone and
1128 fudge the lookup here till the other part is found and fixed!!! */
1130 if ( ((scanCode >= 0x13b) && (scanCode <= 0x144)) ||
1131 (scanCode == 0x157) || (scanCode == 0x158))
1132 scanCode &= 0xff; /* remove "extended-key" flag for Fx keys */
1134 /* let's do scancode -> keycode -> keysym -> String */
1136 for (keyc=min_keycode; keyc<=max_keycode; keyc++)
1137 if ((keyc2scan[keyc]) == scanCode)
1138 break;
1139 if (keyc <= max_keycode)
1141 keys = TSXKeycodeToKeysym(display, keyc, 0);
1142 name = TSXKeysymToString(keys);
1143 TRACE_(keyboard)("found scan=%04x keyc=%04x keysym=%04x string=%s\n",
1144 scanCode, keyc, (int)keys, name);
1145 if (lpBuffer && nSize && name)
1147 strncpy(lpBuffer, name, nSize);
1148 return 1;
1152 /* Finally issue FIXME for unknown keys */
1154 FIXME_(keyboard)("(%08lx,%p,%d): unsupported key, vkey=%04x, ansi=%04x\n",lParam,lpBuffer,nSize,vkey,ansi);
1155 if (lpBuffer && nSize)
1156 *lpBuffer = 0;
1157 return 0;
1160 /***********************************************************************
1161 * X11DRV_KEYBOARD_MapDeadKeysym
1163 static char KEYBOARD_MapDeadKeysym(KeySym keysym)
1165 switch (keysym)
1167 /* symbolic ASCII is the same as defined in rfc1345 */
1168 #ifdef XK_dead_tilde
1169 case XK_dead_tilde :
1170 #endif
1171 case 0x1000FE7E : /* Xfree's XK_Dtilde */
1172 return '~'; /* '? */
1173 #ifdef XK_dead_acute
1174 case XK_dead_acute :
1175 #endif
1176 case 0x1000FE27 : /* Xfree's XK_Dacute_accent */
1177 return 0xb4; /* '' */
1178 #ifdef XK_dead_circumflex
1179 case XK_dead_circumflex:
1180 #endif
1181 case 0x1000FE5E : /* Xfree's XK_Dcircumflex_accent */
1182 return '^'; /* '> */
1183 #ifdef XK_dead_grave
1184 case XK_dead_grave :
1185 #endif
1186 case 0x1000FE60 : /* Xfree's XK_Dgrave_accent */
1187 return '`'; /* '! */
1188 #ifdef XK_dead_diaeresis
1189 case XK_dead_diaeresis :
1190 #endif
1191 case 0x1000FE22 : /* Xfree's XK_Ddiaeresis */
1192 return 0xa8; /* ': */
1193 #ifdef XK_dead_cedilla
1194 case XK_dead_cedilla :
1195 return 0xb8; /* ', */
1196 #endif
1197 #ifdef XK_dead_macron
1198 case XK_dead_macron :
1199 return '-'; /* 'm isn't defined on iso-8859-x */
1200 #endif
1201 #ifdef XK_dead_breve
1202 case XK_dead_breve :
1203 return 0xa2; /* '( */
1204 #endif
1205 #ifdef XK_dead_abovedot
1206 case XK_dead_abovedot :
1207 return 0xff; /* '. */
1208 #endif
1209 #ifdef XK_dead_abovering
1210 case XK_dead_abovering :
1211 return '0'; /* '0 isn't defined on iso-8859-x */
1212 #endif
1213 #ifdef XK_dead_doubleacute
1214 case XK_dead_doubleacute :
1215 return 0xbd; /* '" */
1216 #endif
1217 #ifdef XK_dead_caron
1218 case XK_dead_caron :
1219 return 0xb7; /* '< */
1220 #endif
1221 #ifdef XK_dead_ogonek
1222 case XK_dead_ogonek :
1223 return 0xb2; /* '; */
1224 #endif
1225 /* FIXME: I don't know this three.
1226 case XK_dead_iota :
1227 return 'i';
1228 case XK_dead_voiced_sound :
1229 return 'v';
1230 case XK_dead_semivoiced_sound :
1231 return 's';
1234 TRACE_(keyboard)("no character for dead keysym 0x%08lx\n",keysym);
1235 return 0;
1238 /***********************************************************************
1239 * X11DRV_KEYBOARD_ToAscii
1241 * The ToAscii function translates the specified virtual-key code and keyboard
1242 * state to the corresponding Windows character or characters.
1244 * If the specified key is a dead key, the return value is negative. Otherwise,
1245 * it is one of the following values:
1246 * Value Meaning
1247 * 0 The specified virtual key has no translation for the current state of the keyboard.
1248 * 1 One Windows character was copied to the buffer.
1249 * 2 Two characters were copied to the buffer. This usually happens when a
1250 * dead-key character (accent or diacritic) stored in the keyboard layout cannot
1251 * be composed with the specified virtual key to form a single character.
1253 * FIXME : should do the above (return 2 for non matching deadchar+char combinations)
1256 INT16 X11DRV_KEYBOARD_ToAscii(
1257 UINT16 virtKey,UINT16 scanCode, LPBYTE lpKeyState,
1258 LPVOID lpChar, UINT16 flags)
1260 XKeyEvent e;
1261 KeySym keysym;
1262 static XComposeStatus cs;
1263 INT ret;
1264 int keyc;
1266 if (scanCode==0) {
1267 /* This happens when doing Alt+letter : a fake 'down arrow' key press
1268 event is generated by windows. Just ignore it. */
1269 TRACE_(keyboard)("scanCode=0, doing nothing\n");
1270 return 0;
1272 if (scanCode & 0x8000)
1274 TRACE_(keyboard)("Key UP, doing nothing\n" );
1275 return 0;
1277 e.display = display;
1278 e.keycode = 0;
1279 e.state = 0;
1280 if (lpKeyState[VK_SHIFT] & 0x80)
1281 e.state |= ShiftMask;
1282 if (lpKeyState[VK_CAPITAL] & 0x01)
1283 e.state |= LockMask;
1284 if (lpKeyState[VK_CONTROL] & 0x80)
1286 if (lpKeyState[VK_MENU] & 0x80)
1287 e.state |= AltGrMask;
1288 else
1289 e.state |= ControlMask;
1291 if (lpKeyState[VK_NUMLOCK] & 0x01)
1292 e.state |= NumLockMask;
1293 TRACE_(key)("(%04X, %04X) : faked state = %X\n",
1294 virtKey, scanCode, e.state);
1295 /* We exit on the first keycode found, to speed up the thing. */
1296 for (keyc=min_keycode; (keyc<=max_keycode) && (!e.keycode) ; keyc++)
1297 { /* Find a keycode that could have generated this virtual key */
1298 if ((keyc2vkey[keyc] & 0xFF) == virtKey)
1299 { /* We filter the extended bit, we don't know it */
1300 e.keycode = keyc; /* Store it temporarily */
1301 if ((EVENT_event_to_vkey(&e) & 0xFF) != virtKey) {
1302 e.keycode = 0; /* Wrong one (ex: because of the NumLock
1303 state), so set it to 0, we'll find another one */
1308 if ((virtKey>=VK_NUMPAD0) && (virtKey<=VK_NUMPAD9))
1309 e.keycode = TSXKeysymToKeycode(e.display, virtKey-VK_NUMPAD0+XK_KP_0);
1311 if (virtKey==VK_DECIMAL)
1312 e.keycode = TSXKeysymToKeycode(e.display, XK_KP_Decimal);
1314 if (!e.keycode)
1316 WARN_(keyboard)("Unknown virtual key %X !!! \n",virtKey);
1317 return virtKey; /* whatever */
1319 else TRACE_(keyboard)("Found keycode %d (0x%2X)\n",e.keycode,e.keycode);
1321 ret = TSXLookupString(&e, (LPVOID)lpChar, 2, &keysym, &cs);
1322 if (ret == 0)
1324 BYTE dead_char = 0;
1326 ((char*)lpChar)[1] = '\0';
1327 dead_char = KEYBOARD_MapDeadKeysym(keysym);
1328 if (dead_char)
1330 *(char*)lpChar = dead_char;
1331 ret = -1;
1333 else
1335 char *ksname;
1337 ksname = TSXKeysymToString(keysym);
1338 if (!ksname)
1339 ksname = "No Name";
1340 if ((keysym >> 8) != 0xff)
1342 ERR_(keyboard)("Please report: no char for keysym %04lX (%s) :\n",
1343 keysym, ksname);
1344 ERR_(keyboard)("(virtKey=%X,scanCode=%X,keycode=%X,state=%X)\n",
1345 virtKey, scanCode, e.keycode, e.state);
1349 else { /* ret = 1 */
1350 /* We have a special case to handle : Shift + arrow, shift + home, ...
1351 X returns a char for it, but Windows doesn't. Let's eat it. */
1352 if (!(lpKeyState[VK_NUMLOCK] & 0x01) /* NumLock is off */
1353 && (lpKeyState[VK_SHIFT] & 0x80) /* Shift is pressed */
1354 && (keysym>=XK_KP_0) && (keysym<=XK_KP_9))
1356 *(char*)lpChar = 0;
1357 ret = 0;
1360 /* We have another special case for delete key (XK_Delete) on an
1361 extended keyboard. X returns a char for it, but Windows doesn't */
1362 if (keysym == XK_Delete)
1364 *(char*)lpChar = 0;
1365 ret = 0;
1369 TRACE_(key)("ToAscii about to return %d with char %x\n",
1370 ret, *(char*)lpChar);
1371 return ret;
1374 /***********************************************************************
1375 * X11DRV_KEYBOARD_GetBeepActive
1377 BOOL X11DRV_KEYBOARD_GetBeepActive()
1379 XKeyboardState keyboard_state;
1381 TSXGetKeyboardControl(display, &keyboard_state);
1383 return keyboard_state.bell_percent != 0;
1386 /***********************************************************************
1387 * X11DRV_KEYBOARD_SetBeepActive
1389 void X11DRV_KEYBOARD_SetBeepActive(BOOL bActivate)
1391 XKeyboardControl keyboard_value;
1393 if(bActivate)
1394 keyboard_value.bell_percent = -1;
1395 else
1396 keyboard_value.bell_percent = 0;
1398 TSXChangeKeyboardControl(display, KBBellPercent, &keyboard_value);
1401 /***********************************************************************
1402 * X11DRV_KEYBOARD_Beep
1404 void X11DRV_KEYBOARD_Beep()
1406 TSXBell(display, 0);
1409 /***********************************************************************
1410 * X11DRV_KEYBOARD_GetDIState
1412 BOOL X11DRV_KEYBOARD_GetDIState(DWORD len, LPVOID ptr)
1414 if (len==256) {
1415 int keyc,vkey;
1417 memset(ptr,0,256);
1418 for (keyc=min_keycode;keyc<max_keycode;keyc++)
1420 /* X keycode to virtual key */
1421 vkey = keyc2vkey[keyc] & 0xFF;
1422 /* The windows scancode is keyc-min_keycode */
1423 if (InputKeyStateTable[vkey]&0x80) {
1424 ((LPBYTE)ptr)[keyc-min_keycode]=0x80;
1425 ((LPBYTE)ptr)[(keyc-min_keycode)|0x80]=0x80;
1428 return TRUE;
1430 WARN_(x11drv)("whoops, X11DRV_KEYBOARD_GetState got len %ld?\n", len);
1431 return TRUE;
1434 /***********************************************************************
1435 * X11DRV_KEYBOARD_GetDIData
1437 BOOL X11DRV_KEYBOARD_GetDIData(
1438 BYTE *keystate,
1439 DWORD dodsize, LPDIDEVICEOBJECTDATA dod,
1440 LPDWORD entries, DWORD flags)
1442 int keyc,n,vkey,xentries;
1444 /* FIXME !!! */
1446 if (entries)
1447 xentries = *entries;
1448 else
1449 xentries = 1;
1451 n = 0;
1453 for (keyc=min_keycode;(keyc<max_keycode) && (n<*entries);keyc++)
1455 /* X keycode to virtual key */
1456 vkey = keyc2vkey[keyc] & 0xFF;
1457 if (keystate[vkey] == (InputKeyStateTable[vkey]&0x80))
1458 continue;
1459 if (dod) {
1460 /* add an entry */
1461 dod[n].dwOfs = keyc-min_keycode; /* scancode */
1462 dod[n].dwData = InputKeyStateTable[vkey]&0x80;
1463 dod[n].dwTimeStamp = 0; /* umm */
1464 dod[n].dwSequence = 0; /* umm */
1465 n++;
1467 if (!(flags & DIGDD_PEEK))
1468 keystate[vkey] = InputKeyStateTable[vkey]&0x80;
1472 if (n) fprintf(stderr,"%d entries\n",n);
1473 *entries = n;
1475 return TRUE;
1478 #endif /* !defined(X_DISPLAY_MISSING) */