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
14 #ifndef X_DISPLAY_MISSING
16 #include <X11/Xatom.h>
17 #include <X11/keysym.h>
19 #include "ts_xresource.h"
29 extern LPBYTE pKeyStateTable
;
31 int min_keycode
, max_keycode
, keysyms_per_keycode
;
32 WORD keyc2vkey
[256], keyc2scan
[256];
34 static int NumLockMask
, AltGrMask
; /* mask in the XKeyEvent state */
35 static int kcControl
, kcAlt
, kcShift
, kcNumLock
, kcCapsLock
; /* keycodes */
37 /* Keyboard translation tables */
39 static const int main_key_scan
[MAIN_LEN
] =
41 /* this is my (102-key) keyboard layout, sorry if it doesn't quite match yours */
42 0x29,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,
43 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,
44 0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x2B,
45 0x2C,0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
46 0x56 /* the 102th key (actually to the left of the Z) */
49 /*** DEFINE YOUR NEW LANGUAGE-SPECIFIC MAPPINGS BELOW, SEE EXISTING TABLES */
51 /* the VK mappings for the main keyboard will be auto-assigned as before */
52 /* so what we have here is just the character tables */
53 /* order: Normal, Shift, AltGr, Shift-AltGr */
54 /* I just wrote in what is guaranteed to be correct (i.e. what's written on the keycaps),
55 not the bunch of special characters behind AltGr and Shift-AltGr */
57 /*** United States keyboard layout */
58 static const char main_key_US
[MAIN_LEN
][4] =
60 /* FIXME: what did the US keyboard look like again? fill in the blanks, please */
61 "","1!","2\"","3#","4$","5%","6","7","8","9(","0)","-","+",
62 "qQ","wW","eE","rR","tT","yY","uU","iI","oO","pP","[","]",
63 "aA","sS","dD","fF","gG","hH","jJ","kK","lL",";","","",
64 "zZ","xX","cC","vV","bB","nN","mM",",<",".>","?"
67 /*** Norwegian keyboard layout */
68 static const char main_key_NO
[MAIN_LEN
][4] =
70 "|§","1!","2\"@","3#£","4¤$","5%","6&","7/{","8([","9)]","0=}","+?","\\`´",
71 "qQ","wW","eE","rR","tT","yY","uU","iI","oO","pP","åÅ","¨^~",
72 "aA","sS","dD","fF","gG","hH","jJ","kK","lL","øØ","æÆ","'*",
73 "zZ","xX","cC","vV","bB","nN","mM",",;",".:","-_",
77 /*** Layout table. Add your keyboard mappings to this list */
80 const char (*key
)[MAIN_LEN
][4];
82 {MAKELANGID(LANG_ENGLISH
,SUBLANG_ENGLISH_US
),437,&main_key_US
},
83 {MAKELANGID(LANG_NORWEGIAN
,SUBLANG_DEFAULT
),865,&main_key_NO
},
87 static unsigned kbd_layout
=0; /* index into above table of layouts */
89 /* maybe more of these scancodes should be extended? */
90 /* extended must be set for ALT_R, CTRL_R,
91 INS, DEL, HOME, END, PAGE_UP, PAGE_DOWN, ARROW keys,
92 keypad / and keypad ENTER (SDK 3.1 Vol.3 p 138) */
93 /* FIXME should we set extended bit for NumLock ? My
94 * Windows does ... DF */
95 static const int special_key_vkey
[] =
97 VK_BACK
, VK_TAB
, 0, VK_CLEAR
, 0, VK_RETURN
, 0, 0, /* FF08 */
98 0, 0, 0, VK_PAUSE
, VK_SCROLL
, 0, 0, 0, /* FF10 */
99 0, 0, 0, VK_ESCAPE
/* FF18 */
101 static const int special_key_scan
[] =
103 0x0E, 0x0F, 0, /*?*/ 0, 0, 0x1C, 0, 0, /* FF08 */
104 0, 0, 0, 0x45, 0x46, 0 , 0, 0, /* FF10 */
105 0, 0, 0, 0x01 /* FF18 */
108 static const int cursor_key_vkey
[] =
110 VK_HOME
, VK_LEFT
, VK_UP
, VK_RIGHT
, VK_DOWN
, VK_PRIOR
,
111 VK_NEXT
, VK_END
/* FF50 */
113 static const int cursor_key_scan
[] =
115 0x147, 0x14B, 0x148, 0x14D, 0x150, 0x149, 0x151, 0x14F /* FF50 */
118 static const int misc_key_vkey
[] =
120 VK_SELECT
, VK_SNAPSHOT
, VK_EXECUTE
, VK_INSERT
, 0, 0, 0, 0, /* FF60 */
121 VK_CANCEL
, VK_HELP
, VK_CANCEL
, VK_MENU
/* FF68 */
123 static const int misc_key_scan
[] =
125 /*?*/ 0, 0x37, /*?*/ 0, 0x152, 0, 0, 0, 0, /* FF60 */
126 /*?*/ 0, /*?*/ 0, 0x38 /* FF68 */
129 static const int keypad_key_vkey
[] =
131 0, VK_NUMLOCK
, /* FF7E */
132 0, 0, 0, 0, 0, 0, 0, 0, /* FF80 */
133 0, 0, 0, 0, 0, VK_RETURN
, 0, 0, /* FF88 */
134 0, 0, 0, 0, 0, VK_HOME
, VK_LEFT
, VK_UP
, /* FF90 */
135 VK_RIGHT
, VK_DOWN
, VK_PRIOR
, VK_NEXT
, VK_END
, 0,
136 VK_INSERT
, VK_DELETE
, /* FF98 */
137 0, 0, 0, 0, 0, 0, 0, 0, /* FFA0 */
138 0, 0, VK_MULTIPLY
, VK_ADD
, VK_SEPARATOR
, VK_SUBTRACT
,
139 VK_DECIMAL
, VK_DIVIDE
, /* FFA8 */
140 VK_NUMPAD0
, VK_NUMPAD1
, VK_NUMPAD2
, VK_NUMPAD3
, VK_NUMPAD4
,
141 VK_NUMPAD5
, VK_NUMPAD6
, VK_NUMPAD7
, /* FFB0 */
142 VK_NUMPAD8
, VK_NUMPAD9
/* FFB8 */
144 static const int keypad_key_scan
[] =
146 0x138, 0x45, /* FF7E */
147 0, 0, 0, 0, 0, 0, 0, 0, /* FF80 */
148 0, 0, 0, 0, 0, 0x11C, 0, 0, /* FF88 */
149 0, 0, 0, 0, 0, 0x47, 0x4B, 0x48, /* FF90 */
150 0x4D, 0x50, 0x49, 0x51, 0x4F, 0x4C, 0x52, 0x53, /* FF98 */
151 0, 0, 0, 0, 0, 0, 0, 0, /* FFA0 */
152 0, 0, 0x37, 0x4E, /*?*/ 0, 0x4A, 0x53, 0x135, /* FFA8 */
153 0x52, 0x4F, 0x50, 0x51, 0x4B, 0x4C, 0x4D, 0x47, /* FFB0 */
154 0x48, 0x49 /* FFB8 */
157 static const int function_key_vkey
[] =
159 VK_F1
, VK_F2
, /* FFBE */
160 VK_F3
, VK_F4
, VK_F5
, VK_F6
, VK_F7
, VK_F8
, VK_F9
, VK_F10
, /* FFC0 */
161 VK_F11
, VK_F12
, VK_F13
, VK_F14
, VK_F15
, VK_F16
/* FFC8 */
163 static const int function_key_scan
[] =
165 0x3B, 0x3C, /* FFBE */
166 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, /* FFC0 */
167 0x57, 0x58, 0, 0, 0, 0 /* FFC8 */
170 static const int modifier_key_vkey
[] =
172 VK_SHIFT
, VK_SHIFT
, VK_CONTROL
, VK_CONTROL
, VK_CAPITAL
, 0, /* FFE1 */
173 VK_MENU
, VK_MENU
, VK_MENU
, VK_MENU
/* FFE7 */
175 static const int modifier_key_scan
[] =
177 0x2A, 0x36, 0x1D, 0x11D, 0x3A, 0, /* FFE1 */
178 0x38, 0x138, 0x38, 0x138 /* FFE7 */
181 /* Returns the Windows virtual key code associated with the X event <e> */
182 static WORD
EVENT_event_to_vkey( XKeyEvent
*e
)
186 TSXLookupString(e
, NULL
, 0, &keysym
, NULL
);
188 if ((keysym
>= 0xFFAE) && (keysym
<= 0xFFB9) && (e
->state
& NumLockMask
))
189 /* Only the Keypad keys 0-9 and . send different keysyms
190 * depending on the NumLock state */
191 return keypad_key_vkey
[(keysym
& 0xFF) - 0x7E];
193 return keyc2vkey
[e
->keycode
];
196 static BOOL NumState
=FALSE
, CapsState
=FALSE
;
198 /**********************************************************************
199 * KEYBOARD_GenerateMsg
201 * Generate Down+Up messages when NumLock or CapsLock is pressed.
203 * Convention : called with vkey only VK_NUMLOCK or VK_CAPITAL
206 void KEYBOARD_GenerateMsg( WORD vkey
, WORD scan
, int Evtype
, INT event_x
, INT event_y
,
209 BOOL
* State
= (vkey
==VK_NUMLOCK
? &NumState
: &CapsState
);
212 /* The INTERMEDIARY state means : just after a 'press' event, if a 'release' event comes,
213 don't treat it. It's from the same key press. Then the state goes to ON.
214 And from there, a 'release' event will switch off the toggle key. */
216 TRACE(keyboard
,"INTERM : don\'t treat release of toggle key. InputKeyStateTable[%#x] = %#x\n",vkey
,pKeyStateTable
[vkey
]);
219 if ( pKeyStateTable
[vkey
] & 0x1 ) /* it was ON */
221 if (Evtype
!=KeyPress
)
223 TRACE(keyboard
,"ON + KeyRelease => generating DOWN and UP messages.\n");
224 KEYBOARD_SendEvent( vkey
, scan
, 0,
225 event_x
, event_y
, event_time
);
226 KEYBOARD_SendEvent( vkey
, scan
, KEYEVENTF_KEYUP
,
227 event_x
, event_y
, event_time
);
229 pKeyStateTable
[vkey
] &= ~0x01; /* Toggle state to off. */
232 else /* it was OFF */
233 if (Evtype
==KeyPress
)
235 TRACE(keyboard
,"OFF + Keypress => generating DOWN and UP messages.\n");
236 KEYBOARD_SendEvent( vkey
, scan
, 0,
237 event_x
, event_y
, event_time
);
238 KEYBOARD_SendEvent( vkey
, scan
, KEYEVENTF_KEYUP
,
239 event_x
, event_y
, event_time
);
240 *State
=TRUE
; /* Goes to intermediary state before going to ON */
241 pKeyStateTable
[vkey
] |= 0x01; /* Toggle state to on. */
246 /***********************************************************************
247 * KEYBOARD_UpdateOneState
249 * Updates internal state for <vkey>, depending on key <state> under X
252 void KEYBOARD_UpdateOneState ( int vkey
, int state
)
254 /* Do something if internal table state != X state for keycode */
255 if (((pKeyStateTable
[vkey
] & 0x80)!=0) != state
)
257 TRACE(keyboard
,"Adjusting state for vkey %#.2x. State before %#.2x \n",
258 vkey
, pKeyStateTable
[vkey
]);
260 /* Fake key being pressed inside wine */
261 KEYBOARD_SendEvent( vkey
, 0, state
? 0 : KEYEVENTF_KEYUP
,
262 0, 0, GetTickCount() );
264 TRACE(keyboard
,"State after %#.2x \n",pKeyStateTable
[vkey
]);
268 /***********************************************************************
269 * X11DRV_KEYBOARD_UpdateState
271 * Update modifiers state (Ctrl, Alt, Shift)
272 * when window is activated (called by EVENT_FocusIn in event.c)
274 * This handles the case where one uses Ctrl+... Alt+... or Shift+.. to switch
275 * from wine to another application and back.
276 * Toggle keys are handled in HandleEvent. (because XQueryKeymap says nothing
279 void X11DRV_KEYBOARD_UpdateState ( void )
281 /* extract a bit from the char[32] bit suite */
282 #define KeyState(keycode) ((keys_return[keycode/8] & (1<<(keycode%8)))!=0)
284 char keys_return
[32];
286 TRACE(keyboard
,"called\n");
287 if (!TSXQueryKeymap(display
, keys_return
)) {
288 ERR(keyboard
,"Error getting keymap !");
292 /* Adjust the ALT and CONTROL state if any has been changed outside wine */
293 KEYBOARD_UpdateOneState(VK_MENU
, KeyState(kcAlt
));
294 KEYBOARD_UpdateOneState(VK_CONTROL
, KeyState(kcControl
));
295 KEYBOARD_UpdateOneState(VK_SHIFT
, KeyState(kcShift
));
299 /***********************************************************************
300 * X11DRV_KEYBOARD_HandleEvent
302 * Handle a X key event
304 void X11DRV_KEYBOARD_HandleEvent( WND
*pWnd
, XKeyEvent
*event
)
309 WORD vkey
= 0, bScan
;
311 static BOOL force_extended
= FALSE
; /* hack for AltGr translation */
315 INT event_x
= (pWnd
? pWnd
->rectWindow
.left
: 0) + event
->x
;
316 INT event_y
= (pWnd
? pWnd
->rectWindow
.top
: 0) + event
->y
;
317 DWORD event_time
= event
->time
- MSG_WineStartTicks
;
319 /* this allows support for dead keys */
320 if ((event
->keycode
>> 8) == 0x10)
321 event
->keycode
=(event
->keycode
& 0xff);
323 ascii_chars
= TSXLookupString(event
, Str
, 1, &keysym
, &cs
);
325 TRACE(key
, "EVENT_key : state = %X\n", event
->state
);
326 if (keysym
== XK_Mode_switch
)
328 TRACE(key
, "Alt Gr key event received\n");
329 event
->keycode
= kcControl
; /* Simulate Control */
330 X11DRV_KEYBOARD_HandleEvent( pWnd
, event
);
332 event
->keycode
= kcAlt
; /* Simulate Alt */
333 force_extended
= TRUE
;
334 X11DRV_KEYBOARD_HandleEvent( pWnd
, event
);
335 force_extended
= FALSE
;
339 Str
[ascii_chars
] = '\0';
343 ksname
= TSXKeysymToString(keysym
);
346 TRACE(key
, "%s : keysym=%lX (%s), ascii chars=%u / %X / '%s'\n",
347 (event
->type
== KeyPress
) ? "KeyPress" : "KeyRelease",
348 keysym
, ksname
, ascii_chars
, Str
[0] & 0xff, Str
);
351 vkey
= EVENT_event_to_vkey(event
);
352 if (force_extended
) vkey
|= 0x100;
354 TRACE(key
, "keycode 0x%x converted to vkey 0x%x\n",
355 event
->keycode
, vkey
);
362 KEYBOARD_GenerateMsg( VK_NUMLOCK
, 0x45, event
->type
, event_x
, event_y
,
366 TRACE(keyboard
,"Caps Lock event. (type %d). State before : %#.2x\n",event
->type
,pKeyStateTable
[vkey
]);
367 KEYBOARD_GenerateMsg( VK_CAPITAL
, 0x3A, event
->type
, event_x
, event_y
,
369 TRACE(keyboard
,"State after : %#.2x\n",pKeyStateTable
[vkey
]);
372 /* Adjust the NUMLOCK state if it has been changed outside wine */
373 if (!(pKeyStateTable
[VK_NUMLOCK
] & 0x01) != !(event
->state
& NumLockMask
))
375 TRACE(keyboard
,"Adjusting NumLock state. \n");
376 KEYBOARD_GenerateMsg( VK_NUMLOCK
, 0x45, KeyPress
, event_x
, event_y
,
378 KEYBOARD_GenerateMsg( VK_NUMLOCK
, 0x45, KeyRelease
, event_x
, event_y
,
381 /* Adjust the CAPSLOCK state if it has been changed outside wine */
382 if (!(pKeyStateTable
[VK_CAPITAL
] & 0x01) != !(event
->state
& LockMask
))
384 TRACE(keyboard
,"Adjusting Caps Lock state.\n");
385 KEYBOARD_GenerateMsg( VK_CAPITAL
, 0x3A, KeyPress
, event_x
, event_y
,
387 KEYBOARD_GenerateMsg( VK_CAPITAL
, 0x3A, KeyRelease
, event_x
, event_y
,
390 /* Not Num nor Caps : end of intermediary states for both. */
394 bScan
= keyc2scan
[event
->keycode
] & 0xFF;
395 TRACE(key
, "bScan = 0x%02x.\n", bScan
);
398 if ( event
->type
== KeyRelease
) dwFlags
|= KEYEVENTF_KEYUP
;
399 if ( vkey
& 0x100 ) dwFlags
|= KEYEVENTF_EXTENDEDKEY
;
400 if ( force_extended
) dwFlags
|= KEYEVENTF_WINE_FORCEEXTENDED
;
402 KEYBOARD_SendEvent( vkey
& 0xff, bScan
, dwFlags
,
403 event_x
, event_y
, event_time
);
408 /**********************************************************************
409 * X11DRV_KEYBOARD_DetectLayout
411 * Called from X11DRV_KEYBOARD_Init
412 * This routine walks through the defined keyboard layouts and selects
413 * whichever matches most closely.
415 void X11DRV_KEYBOARD_DetectLayout(void)
417 unsigned current
, match
, mismatch
;
418 int max_score
= 0, ismatch
= 0;
419 int score
, keyc
, i
, key
, ok
, syms
= (keysyms_per_keycode
>4) ? 4 : keysyms_per_keycode
;
421 char ckey
[4]={0,0,0,0};
422 const char (*lkey
)[MAIN_LEN
][4];
424 for (current
=0; main_key_tab
[current
].lang
; current
++) {
425 TRACE(keyboard
,"Attempting to match against layout %04x\n",main_key_tab
[current
].lang
);
426 match
= 0; mismatch
= 0;
427 lkey
= main_key_tab
[current
].key
;
428 for (keyc
=min_keycode
; keyc
<=max_keycode
; keyc
++) {
429 /* get data for keycode from X server */
430 for (i
=0; i
<syms
; i
++) {
431 keysym
= TSXKeycodeToKeysym(display
, keyc
, i
);
432 if ((keysym
<0x100)&&(keysym
!=' ')) ckey
[i
] = keysym
;
436 /* search for a match in layout table */
437 /* right now, we just find an absolute match for defined positions */
438 /* (undefined positions are ignored, so if it's defined as "3#"
439 in the table, it's okay that the X server has "3#£", for example) */
440 for (key
=0; key
<MAIN_LEN
; key
++) {
441 for (ok
=(*lkey
)[key
][i
=0]; ok
&&(i
<4); i
++)
442 if ((*lkey
)[key
][i
] && (*lkey
)[key
][i
]!=ckey
[i
]) ok
=0;
445 /* count the matches and mismatches */
446 if (key
<MAIN_LEN
) match
++; else {
447 TRACE(key
,"mismatch for keycode %d, character %c\n",keyc
,ckey
[0]);
452 /* estimate and check score */
453 score
= match
- mismatch
;
454 TRACE(keyboard
,"matches=%d, mismatches=%d, score=%d\n",match
,mismatch
,score
);
455 if (score
> max_score
) {
456 /* best match so far */
457 kbd_layout
= current
;
462 /* we're done, report results if necessary */
464 FIXME(keyboard
,"Your keyboard layout was not found! Using closest match (%04x).\n",
465 main_key_tab
[kbd_layout
].lang
);
466 FIXME(keyboard
,"Please define your layout in windows/x11drv/keyboard.c, and submit them\n");
467 FIXME(keyboard
,"to us for inclusion into future Wine releases.\n");
469 TRACE(keyboard
,"detected layout is %04x\n",main_key_tab
[kbd_layout
].lang
);
472 /**********************************************************************
473 * X11DRV_KEYBOARD_Init
475 void X11DRV_KEYBOARD_Init(void)
478 XModifierKeymap
*mmp
;
482 WORD scan
, vkey
, OEMvkey
;
483 int keyc
, i
, keyn
, syms
;
484 char ckey
[4]={0,0,0,0};
485 const char (*lkey
)[MAIN_LEN
][4];
487 TSXDisplayKeycodes(display
, &min_keycode
, &max_keycode
);
488 ksp
= TSXGetKeyboardMapping(display
, min_keycode
,
489 max_keycode
+ 1 - min_keycode
, &keysyms_per_keycode
);
490 /* We are only interested in keysyms_per_keycode.
491 There is no need to hold a local copy of the keysyms table */
493 mmp
= TSXGetModifierMapping(display
);
494 kcp
= mmp
->modifiermap
;
495 for (i
= 0; i
< 8; i
+= 1) /* There are 8 modifier keys */
499 for (j
= 0; j
< mmp
->max_keypermod
; j
+= 1, kcp
+= 1)
504 for (k
= 0; k
< keysyms_per_keycode
; k
+= 1)
505 if (TSXKeycodeToKeysym(display
, *kcp
, k
) == XK_Mode_switch
)
508 TRACE(key
, "AltGrMask is %x\n", AltGrMask
);
510 else if (TSXKeycodeToKeysym(display
, *kcp
, k
) == XK_Num_Lock
)
512 NumLockMask
= 1 << i
;
513 TRACE(key
, "NumLockMask is %x\n", NumLockMask
);
517 TSXFreeModifiermap(mmp
);
519 /* Detect the keyboard layout */
520 X11DRV_KEYBOARD_DetectLayout();
521 lkey
= main_key_tab
[kbd_layout
].key
;
522 syms
= (keysyms_per_keycode
> 4) ? 4 : keysyms_per_keycode
;
524 /* Now build two conversion arrays :
525 * keycode -> vkey + scancode + extended
526 * vkey + extended -> keycode */
528 e2
.display
= display
;
531 OEMvkey
= VK_OEM_7
; /* next is available. */
532 for (keyc
= min_keycode
; keyc
<= max_keycode
; keyc
++)
534 e2
.keycode
= (KeyCode
)keyc
;
535 TSXLookupString(&e2
, NULL
, 0, &keysym
, NULL
);
537 if (keysym
) /* otherwise, keycode not used */
539 if ((keysym
>> 8) == 0xFF) /* non-character key */
541 int key
= keysym
& 0xff;
543 if (key
>= 0x08 && key
<= 0x1B) { /* special key */
544 vkey
= special_key_vkey
[key
- 0x08];
545 scan
= special_key_scan
[key
- 0x08];
546 } else if (key
>= 0x50 && key
<= 0x57) { /* cursor key */
547 vkey
= cursor_key_vkey
[key
- 0x50];
548 scan
= cursor_key_scan
[key
- 0x50];
549 } else if (key
>= 0x60 && key
<= 0x6B) { /* miscellaneous key */
550 vkey
= misc_key_vkey
[key
- 0x60];
551 scan
= misc_key_scan
[key
- 0x60];
552 } else if (key
>= 0x7E && key
<= 0xB9) { /* keypad key */
553 vkey
= keypad_key_vkey
[key
- 0x7E];
554 scan
= keypad_key_scan
[key
- 0x7E];
555 } else if (key
>= 0xBE && key
<= 0xCD) { /* function key */
556 vkey
= function_key_vkey
[key
- 0xBE] | 0x100; /* set extended bit */
557 scan
= function_key_scan
[key
- 0xBE];
558 } else if (key
>= 0xE1 && key
<= 0xEA) { /* modifier key */
559 vkey
= modifier_key_vkey
[key
- 0xE1];
560 scan
= modifier_key_scan
[key
- 0xE1];
561 } else if (key
== 0xFF) { /* DEL key */
565 /* set extended bit when necessary */
566 if (scan
& 0x100) vkey
|= 0x100;
567 } else if (keysym
== 0x20) { /* Spacebar */
571 /* we seem to need to search the layout-dependent scancodes */
572 int maxlen
=0,maxval
=-1,ok
;
573 for (i
=0; i
<syms
; i
++) {
574 keysym
= TSXKeycodeToKeysym(display
, keyc
, i
);
575 if ((keysym
<0x100)&&(keysym
!=' ')) ckey
[i
] = keysym
;
578 /* find key with longest match streak */
579 for (keyn
=0; keyn
<MAIN_LEN
; keyn
++) {
580 for (ok
=(*lkey
)[keyn
][i
=0]; ok
&&(i
<4); i
++)
581 if ((*lkey
)[keyn
][i
] && (*lkey
)[keyn
][i
]!=ckey
[i
]) ok
=0;
582 if (ok
||(i
>maxlen
)) {
583 maxlen
=i
; maxval
=keyn
;
589 scan
= main_key_scan
[maxval
];
593 /* find a suitable layout-dependent VK code */
594 /* (most Winelib apps ought to be able to work without layout tables!) */
595 for (i
= 0; (i
< keysyms_per_keycode
) && (!vkey
); i
++)
597 keysym
= TSXLookupKeysym(&e2
, i
);
598 if ((keysym
>= VK_0
&& keysym
<= VK_9
)
599 || (keysym
>= VK_A
&& keysym
<= VK_Z
)) {
604 for (i
= 0; (i
< keysyms_per_keycode
) && (!vkey
); i
++)
606 keysym
= TSXLookupKeysym(&e2
, i
);
609 case ';': vkey
= VK_OEM_1
; break;
610 case '/': vkey
= VK_OEM_2
; break;
611 case '`': vkey
= VK_OEM_3
; break;
612 case '[': vkey
= VK_OEM_4
; break;
613 case '\\': vkey
= VK_OEM_5
; break;
614 case ']': vkey
= VK_OEM_6
; break;
615 case '\'': vkey
= VK_OEM_7
; break;
616 case ',': vkey
= VK_OEM_COMMA
; break;
617 case '.': vkey
= VK_OEM_PERIOD
; break;
618 case '-': vkey
= VK_OEM_MINUS
; break;
619 case '+': vkey
= VK_OEM_PLUS
; break;
625 /* Others keys: let's assign OEM virtual key codes in the allowed range,
626 * that is ([0xba,0xc0], [0xdb,0xe4], 0xe6 (given up) et [0xe9,0xf5]) */
629 case 0xc1 : OEMvkey
=0xdb; break;
630 case 0xe5 : OEMvkey
=0xe9; break;
631 case 0xf6 : OEMvkey
=0xf5; WARN(keyboard
,"No more OEM vkey available!\n");
636 if (TRACE_ON(keyboard
))
638 dbg_decl_str(keyboard
, 1024);
640 TRACE(keyboard
, "OEM specific virtual key %X assigned "
641 "to keycode %X:\n", OEMvkey
, e2
.keycode
);
642 for (i
= 0; i
< keysyms_per_keycode
; i
+= 1)
646 keysym
= TSXLookupKeysym(&e2
, i
);
647 ksname
= TSXKeysymToString(keysym
);
650 dsprintf(keyboard
, "%lX (%s) ", keysym
, ksname
);
652 TRACE(keyboard
, "(%s)\n", dbg_str(keyboard
));
656 keyc2vkey
[e2
.keycode
] = vkey
;
657 keyc2scan
[e2
.keycode
] = scan
;
660 /* If some keys still lack scancodes, assign some arbitrary ones to them now */
661 for (scan
= 0x60, keyc
= min_keycode
; keyc
<= max_keycode
; keyc
++)
662 if (keyc2vkey
[keyc
]&&!keyc2scan
[keyc
]) {
664 keysym
= TSXKeycodeToKeysym(display
, keyc
, 0);
665 ksname
= TSXKeysymToString(keysym
);
666 if (!ksname
) ksname
= "NoSymbol";
668 /* should make sure the scancode is unassigned here, but >=0x60 currently always is */
670 TRACE(key
,"assigning scancode %02x to unidentified keycode %02x (%s)\n",scan
,keyc
,ksname
);
671 keyc2scan
[keyc
]=scan
++;
674 /* Now store one keycode for each modifier. Used to simulate keypresses. */
675 kcControl
= TSXKeysymToKeycode(display
, XK_Control_L
);
676 kcAlt
= TSXKeysymToKeycode(display
, XK_Alt_L
);
677 if (!kcAlt
) kcAlt
= TSXKeysymToKeycode(display
, XK_Meta_L
);
678 kcShift
= TSXKeysymToKeycode(display
, XK_Shift_L
);
679 kcNumLock
= TSXKeysymToKeycode(display
, XK_Num_Lock
);
680 kcCapsLock
= TSXKeysymToKeycode(display
, XK_Caps_Lock
);
683 /***********************************************************************
684 * X11DRV_KEYBOARD_VkKeyScan
686 WORD
X11DRV_KEYBOARD_VkKeyScan(CHAR cChar
)
693 /* char->keysym (same for ANSI chars) */
694 keysym
=(unsigned char) cChar
;/* (!) cChar is signed */
695 if (keysym
<=27) keysym
+=0xFF00;/*special chars : return, backspace...*/
697 keycode
= TSXKeysymToKeycode(display
, keysym
); /* keysym -> keycode */
699 { /* It didn't work ... let's try with deadchar code. */
700 keycode
= TSXKeysymToKeycode(display
, keysym
| 0xFE00);
703 TRACE(keyboard
,"VkKeyScan '%c'(%#lx, %lu): got keycode %#.2x\n",
704 cChar
,keysym
,keysym
,keycode
);
708 for (index
=-1, i
=0; (i
<8) && (index
<0); i
++) /* find shift state */
709 if (TSXKeycodeToKeysym(display
,keycode
,i
)==keysym
) index
=i
;
712 WARN(keyboard
,"Keysym %lx not found while parsing the keycode table\n",keysym
); break;
714 case 1 : highbyte
= 0x0100; break;
715 case 2 : highbyte
= 0X0600; break;
716 default : ERR(keyboard
,"index %d found by XKeycodeToKeysym. please report! \n",index
);
719 index : 0 adds 0x0000
720 index : 1 adds 0x0100 (shift)
721 index : ? adds 0x0200 (ctrl)
722 index : 2 adds 0x0600 (ctrl+alt)
723 index : ? adds 0x0700 (ctrl+alt+shift (used?))
726 TRACE(keyboard
," ... returning %#.2x\n", keyc2vkey
[keycode
]+highbyte
);
727 return keyc2vkey
[keycode
]+highbyte
; /* keycode -> (keyc2vkey) vkey */
730 /***********************************************************************
731 * X11DRV_KEYBOARD_MapVirtualKey
733 UINT16
X11DRV_KEYBOARD_MapVirtualKey(UINT16 wCode
, UINT16 wMapType
)
735 #define returnMVK(value) { TRACE(keyboard,"returning 0x%x.\n",value); return value; }
737 TRACE(keyboard
,"MapVirtualKey wCode=0x%x wMapType=%d ... \n",
740 case 0: { /* vkey-code to scan-code */
741 /* let's do vkey -> keycode -> scan */
743 for (keyc
=min_keycode
; keyc
<=max_keycode
; keyc
++)
744 if ((keyc2vkey
[keyc
] & 0xFF) == wCode
)
745 returnMVK (keyc2scan
[keyc
] & 0xFF);
748 case 1: { /* scan-code to vkey-code */
749 /* let's do scan -> keycode -> vkey */
751 for (keyc
=min_keycode
; keyc
<=max_keycode
; keyc
++)
752 if ((keyc2scan
[keyc
] & 0xFF) == wCode
)
753 returnMVK (keyc2vkey
[keyc
] & 0xFF);
756 case 2: { /* vkey-code to unshifted ANSI code */
757 /* (was FIXME) : what does unshifted mean ? 'a' or 'A' ? */
758 /* My Windows returns 'A'. */
759 /* let's do vkey -> keycode -> (XLookupString) ansi char */
764 e
.state
= 0; /* unshifted */
765 e
.keycode
= MapVirtualKey16( wCode
, 0);
766 if (!TSXLookupString(&e
, s
, 2 , &keysym
, NULL
))
771 default: /* reserved */
772 WARN(keyboard
, "Unknown wMapType %d !\n",
779 /***********************************************************************
780 * X11DRV_KEYBOARD_GetKeyNameText
782 INT16
X11DRV_KEYBOARD_GetKeyNameText(LONG lParam
, LPSTR lpBuffer
, INT16 nSize
)
788 FIXME(keyboard
,"(%ld,<ptr>,%d): stub\n",lParam
,nSize
);
794 for (i
= 0 ; i
!= KeyTableSize
; i
++)
795 if (KeyTable
[i
].scancode
== lParam
) {
796 lstrcpynA( lpBuffer
, KeyTable
[i
].name
, nSize
);
797 return strlen(lpBuffer
);
805 /***********************************************************************
806 * X11DRV_KEYBOARD_ToAscii
808 * The ToAscii function translates the specified virtual-key code and keyboard
809 * state to the corresponding Windows character or characters.
811 * If the specified key is a dead key, the return value is negative. Otherwise,
812 * it is one of the following values:
814 * 0 The specified virtual key has no translation for the current state of the keyboard.
815 * 1 One Windows character was copied to the buffer.
816 * 2 Two characters were copied to the buffer. This usually happens when a
817 * dead-key character (accent or diacritic) stored in the keyboard layout cannot
818 * be composed with the specified virtual key to form a single character.
820 * FIXME : should do the above (return 2 for non matching deadchar+char combinations)
823 INT16
X11DRV_KEYBOARD_ToAscii(
824 UINT16 virtKey
,UINT16 scanCode
, LPBYTE lpKeyState
,
825 LPVOID lpChar
, UINT16 flags
)
829 static XComposeStatus cs
;
834 /* This happens when doing Alt+letter : a fake 'down arrow' key press
835 event is generated by windows. Just ignore it. */
836 TRACE(keyboard
,"scanCode=0, doing nothing\n");
839 if (scanCode
& 0x8000)
841 TRACE( keyboard
, "Key UP, doing nothing\n" );
847 if (lpKeyState
[VK_SHIFT
] & 0x80)
848 e
.state
|= ShiftMask
;
849 if (lpKeyState
[VK_CAPITAL
] & 0x01)
851 if (lpKeyState
[VK_CONTROL
] & 0x80)
853 if (lpKeyState
[VK_MENU
] & 0x80)
854 e
.state
|= AltGrMask
;
856 e
.state
|= ControlMask
;
858 if (lpKeyState
[VK_NUMLOCK
] & 0x01)
859 e
.state
|= NumLockMask
;
860 TRACE(key
, "(%04X, %04X) : faked state = %X\n",
861 virtKey
, scanCode
, e
.state
);
862 /* We exit on the first keycode found, to speed up the thing. */
863 for (keyc
=min_keycode
; (keyc
<=max_keycode
) && (!e
.keycode
) ; keyc
++)
864 { /* Find a keycode that could have generated this virtual key */
865 if ((keyc2vkey
[keyc
] & 0xFF) == virtKey
)
866 { /* We filter the extended bit, we don't know it */
867 e
.keycode
= keyc
; /* Store it temporarily */
868 if ((EVENT_event_to_vkey(&e
) & 0xFF) != virtKey
) {
869 e
.keycode
= 0; /* Wrong one (ex: because of the NumLock
870 state), so set it to 0, we'll find another one */
875 if ((virtKey
>=VK_NUMPAD0
) && (virtKey
<=VK_NUMPAD9
))
876 e
.keycode
= TSXKeysymToKeycode(e
.display
, virtKey
-VK_NUMPAD0
+XK_KP_0
);
878 if (virtKey
==VK_DECIMAL
)
879 e
.keycode
= TSXKeysymToKeycode(e
.display
, XK_KP_Decimal
);
883 WARN(keyboard
,"Unknown virtual key %X !!! \n",virtKey
);
884 return virtKey
; /* whatever */
886 else TRACE(keyboard
,"Found keycode %d (0x%2X)\n",e
.keycode
,e
.keycode
);
888 ret
= TSXLookupString(&e
, (LPVOID
)lpChar
, 2, &keysym
, &cs
);
893 ((char*)lpChar
)[1] = '\0';
896 /* symbolic ASCII is the same as defined in rfc1345 */
900 case 0x1000FE7E : /* Xfree's XK_Dtilde */
901 dead_char
= '~'; /* '? */
906 case 0x1000FE27 : /* Xfree's XK_Dacute_accent */
907 dead_char
= 0xb4; /* '' */
909 #ifdef XK_dead_circumflex
910 case XK_dead_circumflex
:
912 case 0x1000FE5E : /* Xfree's XK_Dcircumflex_accent */
913 dead_char
= '^'; /* '> */
918 case 0x1000FE60 : /* Xfree's XK_Dgrave_accent */
919 dead_char
= '`'; /* '! */
921 #ifdef XK_dead_diaeresis
922 case XK_dead_diaeresis
:
924 case 0x1000FE22 : /* Xfree's XK_Ddiaeresis */
925 dead_char
= 0xa8; /* ': */
927 #ifdef XK_dead_cedilla
928 case XK_dead_cedilla
:
929 dead_char
= 0xb8; /* ', */
932 #ifdef XK_dead_macron
933 case XK_dead_macron
:
934 dead_char
= '-'; /* 'm isn't defined on iso-8859-x */
939 dead_char
= 0xa2; /* '( */
942 #ifdef XK_dead_abovedot
943 case XK_dead_abovedot
:
944 dead_char
= 0xff; /* '. */
947 #ifdef XK_dead_abovering
948 case XK_dead_abovering
:
949 dead_char
= '0'; /* '0 isn't defined on iso-8859-x */
952 #ifdef XK_dead_doubleacute
953 case XK_dead_doubleacute
:
954 dead_char
= 0xbd; /* '" */
959 dead_char
= 0xb7; /* '< */
962 #ifdef XK_dead_ogonek
963 case XK_dead_ogonek
:
964 dead_char
= 0xb2; /* '; */
967 /* FIXME: I don't know this three.
971 case XK_dead_voiced_sound :
974 case XK_dead_semivoiced_sound :
981 *(char*)lpChar
= dead_char
;
988 ksname
= TSXKeysymToString(keysym
);
991 if ((keysym
>> 8) != 0xff)
993 ERR(keyboard
, "Please report: no char for keysym %04lX (%s) :\n",
995 ERR(keyboard
, "(virtKey=%X,scanCode=%X,keycode=%X,state=%X)\n",
996 virtKey
, scanCode
, e
.keycode
, e
.state
);
1000 else { /* ret = 1 */
1001 /* We have a special case to handle : Shift + arrow, shift + home, ...
1002 X returns a char for it, but Windows doesn't. Let's eat it. */
1003 if (!(lpKeyState
[VK_NUMLOCK
] & 0x01) /* NumLock is off */
1004 && (lpKeyState
[VK_SHIFT
] & 0x80) /* Shift is pressed */
1005 && (keysym
>=XK_KP_0
) && (keysym
<=XK_KP_9
))
1012 TRACE(key
, "ToAscii about to return %d with char %x\n",
1013 ret
, *(char*)lpChar
);
1017 #endif /* !defined(X_DISPLAY_MISSING) */