Release 970329
[wine/multimedia.git] / windows / keyboard.c
blob01853aa74a6a3508a10a372d583877e8e520a8d9
1 /*
2 * Keyboard related functions
4 * Copyright 1993 Bob Amstadt
5 * Copyright 1996 Albrecht Kleine
6 * Copyright 1997 David Faure
8 */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <X11/keysym.h>
15 #include <X11/Xlib.h>
16 #include <X11/Xresource.h>
17 #include <X11/Xutil.h>
18 #include <X11/Xatom.h>
20 #include "windows.h"
21 #include "win.h"
22 #include "gdi.h"
23 #include "keyboard.h"
24 #include "message.h"
25 #include "stddebug.h"
26 /* #define DEBUG_KEYBOARD */
27 #include "debug.h"
28 #include "xmalloc.h"
29 #include "accel.h"
30 #include "struct32.h"
32 BOOL32 MouseButtonsStates[3];
33 BOOL32 AsyncMouseButtonsStates[3];
34 BYTE InputKeyStateTable[256];
35 BYTE QueueKeyStateTable[256];
36 BYTE AsyncKeyStateTable[256];
38 static int NumLockMask;
39 static int AltGrMask;
40 static int min_keycode, max_keycode;
41 static int keyc2vkey[256];
43 typedef union
45 struct
47 unsigned long count : 16;
48 unsigned long code : 8;
49 unsigned long extended : 1;
50 unsigned long unused : 2;
51 unsigned long win_internal : 2;
52 unsigned long context : 1;
53 unsigned long previous : 1;
54 unsigned long transition : 1;
55 } lp1;
56 unsigned long lp2;
57 } KEYLP;
59 /* Keyboard translation tables */
60 static const int special_key[] =
62 VK_BACK, VK_TAB, 0, VK_CLEAR, 0, VK_RETURN, 0, 0, /* FF08 */
63 0, 0, 0, VK_PAUSE, VK_SCROLL, 0, 0, 0, /* FF10 */
64 0, 0, 0, VK_ESCAPE /* FF18 */
67 static const int cursor_key[] =
69 VK_HOME, VK_LEFT, VK_UP, VK_RIGHT, VK_DOWN, VK_PRIOR,
70 VK_NEXT, VK_END /* FF50 */
73 static const int misc_key[] =
75 VK_SELECT, VK_SNAPSHOT, VK_EXECUTE, VK_INSERT, 0, 0, 0, 0, /* FF60 */
76 VK_CANCEL, VK_HELP, VK_CANCEL, VK_MENU /* FF68 */
79 static const int keypad_key[] =
81 0, VK_NUMLOCK, /* FF7E */
82 0, 0, 0, 0, 0, 0, 0, 0, /* FF80 */
83 0, 0, 0, 0, 0, VK_RETURN, 0, 0, /* FF88 */
84 0, 0, 0, 0, 0, VK_HOME, VK_LEFT, VK_UP, /* FF90 */
85 VK_RIGHT, VK_DOWN, VK_PRIOR, VK_NEXT, VK_END, 0,
86 VK_INSERT, VK_DELETE, /* FF98 */
87 0, 0, 0, 0, 0, 0, 0, 0, /* FFA0 */
88 0, 0, VK_MULTIPLY, VK_ADD, VK_SEPARATOR, VK_SUBTRACT,
89 VK_DECIMAL, VK_DIVIDE, /* FFA8 */
90 VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3, VK_NUMPAD4,
91 VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7, /* FFB0 */
92 VK_NUMPAD8, VK_NUMPAD9 /* FFB8 */
95 static const int function_key[] =
97 VK_F1, VK_F2, /* FFBE */
98 VK_F3, VK_F4, VK_F5, VK_F6, VK_F7, VK_F8, VK_F9, VK_F10, /* FFC0 */
99 VK_F11, VK_F12, VK_F13, VK_F14, VK_F15, VK_F16 /* FFC8 */
102 static const int modifier_key[] =
104 VK_SHIFT, VK_SHIFT, VK_CONTROL, VK_CONTROL, VK_CAPITAL, 0, /* FFE1 */
105 VK_MENU, VK_MENU, VK_MENU, VK_MENU /* FFE7 */
109 static WORD EVENT_event_to_vkey( XKeyEvent *e)
111 KeySym keysym;
113 XLookupString(e, NULL, 0, &keysym, NULL);
115 if ((keysym >= 0xFFAE) && (keysym <= 0xFFB9) && (e->state & NumLockMask))
116 /* Only the Keypad keys 0-9 and . send different keysyms
117 * depending on the NumLock state */
118 return keypad_key[(keysym & 0xFF) - 0x7E];
120 return keyc2vkey[e->keycode];
123 /**********************************************************************
124 * KEYBOARD_Init
126 BOOL32 KEYBOARD_Init(void)
128 int i, keysyms_per_keycode;
129 KeySym *ksp;
130 XModifierKeymap *mmp;
131 KeySym keysym;
132 KeyCode *kcp;
133 XKeyEvent e2;
134 WORD vkey, OEMvkey;
136 XDisplayKeycodes(display, &min_keycode, &max_keycode);
137 ksp = XGetKeyboardMapping(display, min_keycode,
138 max_keycode + 1 - min_keycode, &keysyms_per_keycode);
139 /* We are only interested in keysyms_per_keycode.
140 There is no need to hold a local copy of the keysyms table */
141 XFree(ksp);
142 mmp = XGetModifierMapping(display);
143 kcp = mmp->modifiermap;
144 for (i = 0; i < 8; i += 1) /* There are 8 modifier keys */
146 int j;
148 for (j = 0; j < mmp->max_keypermod; j += 1, kcp += 1)
149 if (*kcp)
151 int k;
153 for (k = 0; k < keysyms_per_keycode; k += 1)
154 if (XKeycodeToKeysym(display, *kcp, k) == XK_Mode_switch)
156 AltGrMask = 1 << i;
157 dprintf_key(stddeb, "AltGrMask is %x\n", AltGrMask);
159 else if (XKeycodeToKeysym(display, *kcp, k) == XK_Num_Lock)
161 NumLockMask = 1 << i;
162 dprintf_key(stddeb, "NumLockMask is %x\n", NumLockMask);
166 XFreeModifiermap(mmp);
168 /* Now build two conversion arrays :
169 * keycode -> vkey + extended
170 * vkey + extended -> keycode */
172 e2.display = display;
173 e2.state = 0;
175 OEMvkey = 0xb9; /* first OEM virtual key available is ba */
176 for (e2.keycode=min_keycode; e2.keycode<=max_keycode; e2.keycode++)
178 XLookupString(&e2, NULL, 0, &keysym, NULL);
179 vkey = 0;
180 if (keysym) /* otherwise, keycode not used */
182 if ((keysym >> 8) == 0xFF) /* non-character key */
184 int key = keysym & 0xff;
186 if (key >= 0x08 && key <= 0x1B) /* special key */
187 vkey = special_key[key - 0x08];
188 else if (key >= 0x50 && key <= 0x57) /* cursor key */
189 vkey = cursor_key[key - 0x50];
190 else if (key >= 0x60 && key <= 0x6B) /* miscellaneous key */
191 vkey = misc_key[key - 0x60];
192 else if (key >= 0x7E && key <= 0xB9) /* keypad key */
193 vkey = keypad_key[key - 0x7E];
194 else if (key >= 0xBE && key <= 0xCD) /* function key */
196 vkey = function_key[key - 0xBE];
197 vkey |= 0x100; /* set extended bit */
199 else if (key >= 0xE1 && key <= 0xEA) /* modifier key */
200 vkey = modifier_key[key - 0xE1];
201 else if (key == 0xFF) /* DEL key */
202 vkey = VK_DELETE;
203 /* extended must also be set for ALT_R, CTRL_R,
204 INS, DEL, HOME, END, PAGE_UP, PAGE_DOWN, ARROW keys,
205 keypad / and keypad ENTER (SDK 3.1 Vol.3 p 138) */
206 /* FIXME should we set extended bit for NumLock ? My
207 * Windows does ... DF */
208 switch (keysym)
210 case XK_Control_R :
211 case XK_Alt_R :
212 case XK_Insert :
213 case XK_Delete :
214 case XK_Home :
215 case XK_End :
216 case XK_Prior :
217 case XK_Next :
218 case XK_Left :
219 case XK_Up :
220 case XK_Right :
221 case XK_Down :
222 case XK_KP_Divide :
223 case XK_KP_Enter :
224 vkey |= 0x100;
227 for (i = 0; (i < keysyms_per_keycode) && (!vkey); i++)
229 keysym = XLookupKeysym(&e2, i);
230 if ((keysym >= VK_0 && keysym <= VK_9)
231 || (keysym >= VK_A && keysym <= VK_Z)
232 || keysym == VK_SPACE)
233 vkey = keysym;
236 if (!vkey)
238 /* Others keys: let's assign OEM virtual key codes in the allowed range,
239 * that is ([0xba,0xc0], [0xdb,0xe4], 0xe6 (given up) et [0xe9,0xf5]) */
240 switch (++OEMvkey)
242 case 0xc1 : OEMvkey=0xdb; break;
243 case 0xe5 : OEMvkey=0xe9; break;
244 case 0xf6 : OEMvkey=0xf5; fprintf(stderr,"No more OEM vkey available!\n");
247 vkey = OEMvkey;
249 if (debugging_keyboard)
251 fprintf(stddeb,"OEM specific virtual key %X assigned to keycode %X :\n ("
252 ,OEMvkey,e2.keycode);
253 for (i = 0; i < keysyms_per_keycode; i += 1)
255 char *ksname;
257 keysym = XLookupKeysym(&e2, i);
258 ksname = XKeysymToString(keysym);
259 if (!ksname)
260 ksname = "NoSymbol";
261 fprintf(stddeb, "%lX (%s) ", keysym, ksname);
263 fprintf(stddeb, ")\n");
267 keyc2vkey[e2.keycode] = vkey;
268 } /* for */
269 return TRUE;
272 static BOOL32 NumState=FALSE, CapsState=FALSE;
274 void KEYBOARD_GenerateMsg( WORD vkey, int Evtype, XKeyEvent * event, KEYLP localkeylp )
276 BOOL32 * State = (vkey==VK_NUMLOCK? &NumState : &CapsState);
278 if (*State) {
279 /* The INTERMEDIARY state means : just after a 'press' event, if a 'release' event comes,
280 don't treat it. It's from the same key press. Then the state goes to ON.
281 And from there, a 'release' event will switch off the toggle key. */
282 *State=FALSE;
283 dprintf_keyboard(stddeb,"INTERM : don\'t treat release of toggle key. InputKeyStateTable[%#x] = %#x\n",vkey,InputKeyStateTable[vkey]);
284 } else
286 if ( InputKeyStateTable[vkey] & 0x1 ) /* it was ON */
288 if (Evtype!=KeyPress)
290 dprintf_keyboard(stddeb,"ON + KeyRelease => generating DOWN and UP messages.\n");
291 localkeylp.lp1.previous = 0; /* ? */
292 localkeylp.lp1.transition = 0;
293 hardware_event( WM_KEYDOWN, vkey, localkeylp.lp2, event->x_root - desktopX,
294 event->y_root - desktopY, event->time - MSG_WineStartTicks, 0);
295 hardware_event( WM_KEYUP, vkey, localkeylp.lp2, event->x_root - desktopX,
296 event->y_root - desktopY, event->time - MSG_WineStartTicks, 0);
297 *State=FALSE;
298 InputKeyStateTable[vkey] &= ~0x01; /* Toggle state to off. */
301 else /* it was OFF */
302 if (Evtype==KeyPress)
304 dprintf_keyboard(stddeb,"OFF + Keypress => generating DOWN and UP messages.\n");
305 hardware_event( WM_KEYDOWN, vkey, localkeylp.lp2, event->x_root - desktopX,
306 event->y_root - desktopY, event->time - MSG_WineStartTicks, 0);
307 localkeylp.lp1.previous = 1;
308 localkeylp.lp1.transition = 1;
309 hardware_event( WM_KEYUP, vkey, localkeylp.lp2, event->x_root - desktopX,
310 event->y_root - desktopY, event->time - MSG_WineStartTicks, 0);
311 *State=TRUE; /* Goes to intermediary state before going to ON */
312 InputKeyStateTable[vkey] |= 0x01; /* Toggle state to on. */
317 /***********************************************************************
318 * KEYBOARD_HandleEvent
320 * Handle a X key event
322 void KEYBOARD_HandleEvent( XKeyEvent *event )
324 char Str[24];
325 XComposeStatus cs;
326 KeySym keysym;
327 WORD vkey = 0;
328 KEYLP keylp;
329 static BOOL32 force_extended = FALSE; /* hack for AltGr translation */
331 int ascii_chars = XLookupString(event, Str, 1, &keysym, &cs);
333 dprintf_key(stddeb, "EVENT_key : state = %X\n", event->state);
334 if (keysym == XK_Mode_switch)
336 dprintf_key(stddeb, "Alt Gr key event received\n");
337 event->keycode = XKeysymToKeycode(event->display, XK_Control_L);
338 dprintf_key(stddeb, "Control_L is keycode 0x%x\n", event->keycode);
339 KEYBOARD_HandleEvent(event);
340 event->keycode = XKeysymToKeycode(event->display, XK_Alt_L);
341 dprintf_key(stddeb, "Alt_L is keycode 0x%x\n", event->keycode);
342 force_extended = TRUE;
343 KEYBOARD_HandleEvent(event);
344 force_extended = FALSE;
345 return;
348 Str[ascii_chars] = '\0';
349 if (debugging_key)
351 char *ksname;
353 ksname = XKeysymToString(keysym);
354 if (!ksname)
355 ksname = "No Name";
356 fprintf(stddeb, "%s : keysym=%lX (%s), ascii chars=%u / %X / '%s'\n",
357 (event->type == KeyPress) ? "KeyPress" : "KeyRelease",
358 keysym, ksname, ascii_chars, Str[0] & 0xff, Str);
361 vkey = EVENT_event_to_vkey(event);
362 if (force_extended) vkey |= 0x100;
364 dprintf_key(stddeb, "keycode 0x%x converted to vkey 0x%x\n",
365 event->keycode, vkey);
367 if (vkey)
369 keylp.lp1.count = 1;
370 keylp.lp1.code = LOBYTE(event->keycode) - 8;
371 keylp.lp1.extended = (vkey & 0x100 ? 1 : 0);
372 keylp.lp1.win_internal = 0; /* this has something to do with dialogs,
373 * don't remember where I read it - AK */
374 /* it's '1' under windows, when a dialog box appears
375 * and you press one of the underlined keys - DF*/
376 vkey &= 0xff;
378 switch(vkey)
380 case VK_NUMLOCK:
381 KEYBOARD_GenerateMsg(VK_NUMLOCK,event->type,event,keylp); break;
382 case VK_CAPITAL:
383 dprintf_keyboard(stddeb,"Caps Lock event. (type %d). State before : %#.2x\n",event->type,InputKeyStateTable[vkey]);
384 KEYBOARD_GenerateMsg(VK_CAPITAL,event->type,event,keylp);
385 dprintf_keyboard(stddeb,"State after : %#.2x\n",InputKeyStateTable[vkey]);
386 break;
387 default:
389 WORD message;
390 if (event->type == KeyPress)
392 keylp.lp1.previous = (InputKeyStateTable[vkey] & 0x80) != 0;
393 if (!(InputKeyStateTable[vkey] & 0x80))
394 InputKeyStateTable[vkey] ^= 0x01;
395 InputKeyStateTable[vkey] |= 0x80;
396 keylp.lp1.transition = 0;
397 message = (InputKeyStateTable[VK_MENU] & 0x80)
398 && !(InputKeyStateTable[VK_CONTROL] & 0x80)
399 ? WM_SYSKEYDOWN : WM_KEYDOWN;
401 else
403 BOOL32 sysKey = (InputKeyStateTable[VK_MENU] & 0x80)
404 && !(InputKeyStateTable[VK_CONTROL] & 0x80)
405 && (force_extended == FALSE); /* for Alt from AltGr */
407 InputKeyStateTable[vkey] &= ~0x80;
408 keylp.lp1.previous = 1;
409 keylp.lp1.transition = 1;
410 message = sysKey ? WM_SYSKEYUP : WM_KEYUP;
412 keylp.lp1.context = ( (event->state & Mod1Mask) ||
413 (InputKeyStateTable[VK_MENU] & 0x80)) ? 1 : 0;
414 if (!(InputKeyStateTable[VK_NUMLOCK] & 0x01) != !(event->state & NumLockMask))
416 dprintf_keyboard(stddeb,"Adjusting NumLock state. \n");
417 KEYBOARD_GenerateMsg(VK_NUMLOCK,KeyPress,event,keylp);
418 KEYBOARD_GenerateMsg(VK_NUMLOCK,KeyRelease,event,keylp);
420 if (!(InputKeyStateTable[VK_CAPITAL] & 0x01) != !(event->state & LockMask))
422 dprintf_keyboard(stddeb,"Adjusting Caps Lock state. State before %#.2x \n",InputKeyStateTable[VK_CAPITAL]);
423 KEYBOARD_GenerateMsg(VK_CAPITAL,KeyPress,event,keylp);
424 KEYBOARD_GenerateMsg(VK_CAPITAL,KeyRelease,event,keylp);
425 dprintf_keyboard(stddeb,"State after %#.2x \n",InputKeyStateTable[VK_CAPITAL]);
427 /* End of intermediary states. */
428 NumState = FALSE;
429 CapsState = FALSE;
431 dprintf_key(stddeb," wParam=%04X, lParam=%08lX\n",
432 vkey, keylp.lp2 );
433 dprintf_key(stddeb," InputKeyState=%X\n",
434 InputKeyStateTable[vkey]);
436 hardware_event( message, vkey, keylp.lp2, event->x_root - desktopX,
437 event->y_root - desktopY, event->time - MSG_WineStartTicks, 0 );
444 /**********************************************************************
445 * GetKeyState [USER.106]
447 WORD GetKeyState16(INT16 vkey)
449 return GetKeyState32(vkey);
452 /**********************************************************************
453 * GetKeyState [USER32.248]
454 * An application calls the GetKeyState function in response to a
455 * keyboard-input message. This function retrieves the state of the key
456 * at the time the input message was generated. (SDK 3.1 Vol 2. p 390)
458 WORD GetKeyState32(INT32 vkey)
460 INT32 retval;
462 switch (vkey)
464 case VK_LBUTTON : /* VK_LBUTTON is 1 */
465 retval = MouseButtonsStates[0];
466 break;
467 case VK_MBUTTON : /* VK_MBUTTON is 4 */
468 retval = MouseButtonsStates[1];
469 break;
470 case VK_RBUTTON : /* VK_RBUTTON is 2 */
471 retval = MouseButtonsStates[2];
472 break;
473 default :
474 if (vkey >= 'a' && vkey <= 'z')
475 vkey += 'A' - 'a';
476 retval = ( (WORD)(QueueKeyStateTable[vkey] & 0x80) << 8 ) |
477 (WORD)(QueueKeyStateTable[vkey] & 0x01);
479 dprintf_key(stddeb, "GetKeyState(0x%x) -> %x\n", vkey, retval);
480 return retval;
483 /**********************************************************************
484 * GetKeyboardState [USER.222][USER32.253]
485 * An application calls the GetKeyboardState function in response to a
486 * keyboard-input message. This function retrieves the state of the keyboard
487 * at the time the input message was generated. (SDK 3.1 Vol 2. p 387)
489 VOID GetKeyboardState(LPBYTE lpKeyState)
491 dprintf_key(stddeb, "GetKeyboardState()\n");
492 if (lpKeyState != NULL) {
493 QueueKeyStateTable[VK_LBUTTON] = MouseButtonsStates[0] >> 8;
494 QueueKeyStateTable[VK_MBUTTON] = MouseButtonsStates[1] >> 8;
495 QueueKeyStateTable[VK_RBUTTON] = MouseButtonsStates[2] >> 8;
496 memcpy(lpKeyState, QueueKeyStateTable, 256);
500 /**********************************************************************
501 * SetKeyboardState [USER.223][USER32.483]
503 VOID SetKeyboardState(LPBYTE lpKeyState)
505 dprintf_key(stddeb, "SetKeyboardState()\n");
506 if (lpKeyState != NULL) {
507 memcpy(QueueKeyStateTable, lpKeyState, 256);
508 MouseButtonsStates[0] = QueueKeyStateTable[VK_LBUTTON]? 0x8000: 0;
509 MouseButtonsStates[1] = QueueKeyStateTable[VK_MBUTTON]? 0x8000: 0;
510 MouseButtonsStates[2] = QueueKeyStateTable[VK_RBUTTON]? 0x8000: 0;
514 /**********************************************************************
515 * GetAsyncKeyState (USER32.206)
517 * Determine if a key is or was pressed. retval has high-order
518 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
519 * been pressed.
521 * This uses the variable AsyncMouseButtonsStates and
522 * AsyncKeyStateTable (set in event.c) which have the mouse button
523 * number or key number (whichever is applicable) set to true if the
524 * mouse or key had been depressed since the last call to
525 * GetAsyncKeyState.
527 WORD GetAsyncKeyState32(INT32 nKey)
529 short retval;
531 switch (nKey) {
532 case VK_LBUTTON:
533 retval = AsyncMouseButtonsStates[0] |
534 MouseButtonsStates[0]? 0x0001: 0;
535 break;
536 case VK_MBUTTON:
537 retval = AsyncMouseButtonsStates[1] |
538 MouseButtonsStates[1]? 0x0001: 0;
539 break;
540 case VK_RBUTTON:
541 retval = AsyncMouseButtonsStates[2] |
542 MouseButtonsStates[2]? 0x0001: 0;
543 break;
544 default:
545 retval = AsyncKeyStateTable[nKey] |
546 ((InputKeyStateTable[nKey] & 0x80) ? 0x8000 : 0);
547 break;
550 memset( AsyncMouseButtonsStates, 0, 3 ); /* all states to false */
551 memset( AsyncKeyStateTable, 0, 256 );
553 dprintf_key(stddeb, "GetAsyncKeyState(%x) -> %x\n", nKey, retval);
554 return retval;
557 /**********************************************************************
558 * GetAsyncKeyState (USER.249)
560 WORD GetAsyncKeyState16(INT16 nKey)
562 return GetAsyncKeyState32(nKey);
567 /**********************************************************************
568 * TranslateAccelerator [USER.178]
570 * FIXME: should send some WM_INITMENU or/and WM_INITMENUPOPUP -messages
572 INT32 TranslateAccelerator32(HWND32 hWnd, HACCEL32 hAccel, LPMSG32 msg)
574 MSG16 msg16;
576 STRUCT32_MSG32to16(msg,&msg16);
577 return TranslateAccelerator16(hWnd,hAccel,&msg16);
580 INT16 TranslateAccelerator16(HWND16 hWnd, HACCEL16 hAccel, LPMSG16 msg)
582 ACCELHEADER *lpAccelTbl;
583 int i;
584 BOOL32 sendmsg;
586 if (hAccel == 0 || msg == NULL) return 0;
587 if (msg->message != WM_KEYDOWN &&
588 msg->message != WM_KEYUP &&
589 msg->message != WM_SYSKEYDOWN &&
590 msg->message != WM_SYSKEYUP &&
591 msg->message != WM_CHAR) return 0;
593 dprintf_accel(stddeb, "TranslateAccelerators hAccel=%04x, hWnd=%04x,\
594 msg->hwnd=%04x, msg->message=%04x\n", hAccel,hWnd,msg->hwnd,msg->message);
596 lpAccelTbl = (LPACCELHEADER)GlobalLock16(hAccel);
597 for (sendmsg= i = 0; i < lpAccelTbl->wCount; i++)
599 if(msg->wParam == lpAccelTbl->tbl[i].wEvent)
601 if (msg->message == WM_CHAR)
603 if ( !(lpAccelTbl->tbl[i].type & ALT_ACCEL) &&
604 !(lpAccelTbl->tbl[i].type & VIRTKEY_ACCEL) )
606 dprintf_accel(stddeb,"found accel for WM_CHAR: ('%c')",msg->wParam&0xff);
607 sendmsg=TRUE;
610 else
612 if(lpAccelTbl->tbl[i].type & VIRTKEY_ACCEL)
614 INT32 mask = 0;
615 dprintf_accel(stddeb,"found accel for virt_key %04x (scan %04x)",
616 msg->wParam,0xff & HIWORD(msg->lParam));
617 if(GetKeyState32(VK_SHIFT) & 0x8000) mask |= SHIFT_ACCEL;
618 if(GetKeyState32(VK_CONTROL) & 0x8000) mask |= CONTROL_ACCEL;
619 if(GetKeyState32(VK_MENU) & 0x8000) mask |= ALT_ACCEL;
620 if(mask == (lpAccelTbl->tbl[i].type &
621 (SHIFT_ACCEL | CONTROL_ACCEL | ALT_ACCEL)))
622 sendmsg=TRUE;
623 else
624 dprintf_accel(stddeb,", but incorrect SHIFT/CTRL/ALT-state\n");
626 else
628 if (!(msg->lParam & 0x01000000)) /* no special_key */
630 if ((lpAccelTbl->tbl[i].type & ALT_ACCEL) && (msg->lParam & 0x20000000))
631 { /* ^^ ALT pressed */
632 dprintf_accel(stddeb,"found accel for Alt-%c", msg->wParam&0xff);
633 sendmsg=TRUE;
639 if (sendmsg) /* found an accelerator, but send a message... ? */
641 INT16 iSysStat,iStat,mesg=0;
642 HMENU16 hSysMenu,hMenu;
644 if (msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP)
645 mesg=1;
646 else
647 if (GetCapture16())
648 mesg=2;
649 else
650 if (!IsWindowEnabled16(hWnd))
651 mesg=3;
652 else
654 hMenu=GetMenu32(hWnd);
655 hSysMenu=GetSystemMenu32(hWnd,FALSE);
656 if (hSysMenu)
657 iSysStat=GetMenuState32(hSysMenu,lpAccelTbl->tbl[i].wIDval,MF_BYCOMMAND);
658 else
659 iSysStat=-1;
660 if (hMenu)
661 iStat=GetMenuState32(hMenu,lpAccelTbl->tbl[i].wIDval,MF_BYCOMMAND);
662 else
663 iStat=-1;
664 if (iSysStat!=-1)
666 if (iSysStat & (MF_DISABLED|MF_GRAYED))
667 mesg=4;
668 else
669 mesg=WM_SYSCOMMAND;
671 else
673 if (iStat!=-1)
675 if (IsIconic32(hWnd))
676 mesg=5;
677 else
679 if (iStat & (MF_DISABLED|MF_GRAYED))
680 mesg=6;
681 else
682 mesg=WM_COMMAND;
685 else
686 mesg=WM_COMMAND;
689 if ( mesg==WM_COMMAND || mesg==WM_SYSCOMMAND )
691 dprintf_accel(stddeb,", sending %s, wParam=%0x\n",
692 mesg==WM_COMMAND ? "WM_COMMAND" : "WM_SYSCOMMAND",
693 lpAccelTbl->tbl[i].wIDval);
694 SendMessage16(hWnd, mesg, lpAccelTbl->tbl[i].wIDval,0x00010000L);
696 else
698 /* some reasons for NOT sending the WM_{SYS}COMMAND message:
699 * #0: unknown (please report!)
700 * #1: for WM_KEYUP,WM_SYSKEYUP
701 * #2: mouse is captured
702 * #3: window is disabled
703 * #4: it's a disabled system menu option
704 * #5: it's a menu option, but window is iconic
705 * #6: it's a menu option, but disabled
707 dprintf_accel(stddeb,", but won't send WM_{SYS}COMMAND, reason is #%d\n",mesg);
709 GlobalUnlock16(hAccel);
710 return 1;
714 GlobalUnlock16(hAccel);
715 return 0;
719 /******************************************************************************
720 * OemKeyScan [KEYBOARD.128][USER32.400]
722 DWORD OemKeyScan(WORD wOemChar)
724 dprintf_keyboard(stddeb,"*OemKeyScan (%d)\n",wOemChar);
726 return wOemChar;
729 /******************************************************************************
730 * VkKeyScanA [USER32.572]
732 /* VkKeyScan translates an ANSI character to a virtual-key and shift code
733 * for the current keyboard.
734 * high-order byte yields :
735 * 0 Unshifted
736 * 1 Shift
737 * 2 Ctrl
738 * 3-5 Shift-key combinations that are not used for characters
739 * 6 Ctrl-Alt
740 * 7 Ctrl-Alt-Shift
741 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
742 * FIXME : works ok except for dead chars :
743 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
744 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
747 WORD VkKeyScan32A(CHAR cChar)
749 KeyCode keycode;
750 KeySym keysym;
751 int i,index;
752 int highbyte=0;
754 /* char->keysym (same for ANSI chars) */
755 keysym=(unsigned char) cChar;/* (!) cChar is signed */
756 if (keysym<=27) keysym+=0xFF00;/*special chars : return, backspace...*/
758 keycode = XKeysymToKeycode(display, keysym); /* keysym -> keycode */
759 if (!keycode)
760 { /* It didn't work ... let's try with deadchar code. */
761 keycode = XKeysymToKeycode(display, keysym | 0xFE00);
764 dprintf_keyboard(stddeb,"VkKeyScan '%c'(%#lx, %lu) : got keycode %#.2x ",
765 cChar,keysym,keysym,keycode);
767 if (keycode)
769 for (index=-1, i=0; (i<8) && (index<0); i++) /* find shift state */
770 if (XKeycodeToKeysym(display,keycode,i)==keysym) index=i;
771 switch (index) {
772 case -1 :
773 fprintf(stderr,"Keysym %lx not found while parsing the keycode table\n",keysym); break;
774 case 0 : break;
775 case 1 : highbyte = 0x0100; break;
776 case 2 : highbyte = 0X0600; break;
777 default : fprintf(stderr,"index %d found by XKeycodeToKeysym. please report! \n",index);
780 index : 0 adds 0x0000
781 index : 1 adds 0x0100 (shift)
782 index : ? adds 0x0200 (ctrl)
783 index : 2 adds 0x0600 (ctrl+alt)
784 index : ? adds 0x0700 (ctrl+alt+shit (used?))
787 dprintf_keyboard(stddeb," ... returning %#.2x\n", keyc2vkey[keycode]+highbyte);
788 return keyc2vkey[keycode]+highbyte; /* keycode -> (keyc2vkey) vkey */
791 /******************************************************************************
792 * VkKeyScan [KEYBOARD.129]
794 WORD VkKeyScan16(CHAR cChar)
796 return VkKeyScan32A(cChar);
799 /******************************************************************************
800 * VkKeyScanW [USER32.575]
802 WORD VkKeyScan32W(WCHAR cChar)
804 return VkKeyScan32A((CHAR)cChar); /* FIXME: check unicode */
807 /******************************************************************************
808 * GetKeyboardType [KEYBOARD.130]
810 INT16 GetKeyboardType16(INT16 nTypeFlag)
812 return GetKeyboardType32(nTypeFlag);
815 /******************************************************************************
816 * GetKeyboardType [USER32.254]
818 INT32 GetKeyboardType32(INT32 nTypeFlag)
820 dprintf_keyboard(stddeb,"GetKeyboardType(%d)\n",nTypeFlag);
821 switch(nTypeFlag)
823 case 0: /* Keyboard type */
824 return 4; /* AT-101 */
825 break;
826 case 1: /* Keyboard Subtype */
827 return 0; /* There are no defined subtypes */
828 break;
829 case 2: /* Number of F-keys */
830 return 12; /* We're doing an 101 for now, so return 12 F-keys */
831 break;
832 default:
833 fprintf(stderr, "Unknown type on GetKeyboardType\n");
834 return 0; /* The book says 0 here, so 0 */
839 /******************************************************************************
840 * MapVirtualKeyA [USER32.382]
842 UINT32 MapVirtualKey32A(UINT32 code, UINT32 maptype) {
843 return MapVirtualKey16(code,maptype);
846 /******************************************************************************
847 * MapVirtualKeyA [USER32.384]
849 UINT32 MapVirtualKey32W(UINT32 code, UINT32 maptype) {
850 return MapVirtualKey16(code,maptype);
853 /******************************************************************************
854 * MapVirtualKeyA [KEYBOARD.131]
855 * MapVirtualKey translates keycodes from one format to another
857 UINT16 MapVirtualKey16(UINT16 wCode, UINT16 wMapType)
859 #define returnMVK(value) { dprintf_keyboard(stddeb,"returning 0x%x.\n",value); return value; }
861 dprintf_keyboard(stddeb,"MapVirtualKey wCode=0x%x wMapType=%d ... ",wCode,wMapType);
862 switch(wMapType) {
863 case 0: { /* vkey-code to scan-code */
864 /* let's do vkey -> keycode -> scan */
865 KeyCode keyc;
866 for (keyc=min_keycode; keyc<=max_keycode; keyc++) /* see event.c */
867 if ((keyc2vkey[keyc] & 0xFF)== wCode)
868 returnMVK (keyc - 8);
869 return 0; }
871 case 1: /* scan-code to vkey-code */
872 /* let's do scan -> keycode -> vkey */
874 returnMVK (keyc2vkey[(wCode & 0xFF) + 8]);
876 case 2: { /* vkey-code to unshifted ANSI code */
877 /* (was FIXME) : what does unshifted mean ? 'a' or 'A' ? */
878 /* My Windows returns 'A'. */
879 /* let's do vkey -> keycode -> (XLookupString) ansi char */
880 XKeyEvent e;
881 KeySym keysym;
882 char s[2];
883 e.display = display;
884 e.state = 0; /* unshifted */
885 e.keycode = MapVirtualKey16( wCode, 0);
886 if (!XLookupString(&e, s , 2 , &keysym, NULL))
887 returnMVK (*s);
889 return 0;
891 default: /* reserved */
892 fprintf(stderr, "MapVirtualKey: unknown wMapType %d !\n",
893 wMapType);
894 return 0;
896 return 0;
900 /****************************************************************************
901 * GetKBCodePage16 (KEYBOARD.132)
903 INT16 GetKBCodePage16(void)
905 dprintf_keyboard(stddeb,"GetKBCodePage()\n");
906 return 850;
910 /****************************************************************************
911 * GetKBCodePage32 (USER32.245)
913 UINT32 GetKBCodePage32(void)
915 dprintf_keyboard(stddeb,"GetKbCodePage()\n");
916 return 850;
920 /****************************************************************************
921 * GetKeyNameText32W (USER32.247)
923 INT32 GetKeyNameText32W(LONG lParam, LPWSTR lpBuffer, INT32 nSize)
925 LPSTR buf = xmalloc(nSize);
926 int res = GetKeyNameText32A(lParam,buf,nSize);
928 lstrcpynAtoW(lpBuffer,buf,nSize);
929 free(buf);
930 return res;
933 /****************************************************************************
934 * GetKeyNameText32A (USER32.246)
936 INT32 GetKeyNameText32A(LONG lParam, LPSTR lpBuffer, INT32 nSize)
938 return GetKeyNameText16(lParam,lpBuffer,nSize);
941 /****************************************************************************
942 * GetKeyNameText16 (KEYBOARD.133)
944 INT16 GetKeyNameText16(LONG lParam, LPSTR lpBuffer, INT16 nSize)
946 /* int i; */
948 dprintf_keyboard(stddeb,"GetKeyNameText(%ld,<ptr>,%d)\n",lParam,nSize);
950 lParam >>= 16;
951 lParam &= 0xff;
953 /* for (i = 0 ; i != KeyTableSize ; i++)
954 if (KeyTable[i].scancode == lParam) {
955 lstrcpyn32A( lpBuffer, KeyTable[i].name, nSize );
956 return strlen(lpBuffer);
959 /* FIXME ! GetKeyNameText is still to do...
961 *lpBuffer = 0;
962 return 0;
966 /****************************************************************************
967 * ToAscii (KEYBOARD.4)
969 INT16 ToAscii16(UINT16 virtKey,UINT16 scanCode, LPBYTE lpKeyState,
970 LPVOID lpChar, UINT16 flags)
972 return ToAscii32(virtKey,scanCode,lpKeyState,lpChar,flags);
975 /****************************************************************************
976 * ToAscii (USER32.545)
978 INT32 ToAscii32(
979 UINT32 virtKey,UINT32 scanCode,LPBYTE lpKeyState,
980 LPWORD lpChar,UINT32 flags
982 XKeyEvent e;
983 KeySym keysym;
984 static XComposeStatus cs;
985 INT32 ret;
986 WORD keyc;
988 e.display = display;
989 e.keycode = 0;
990 for (keyc=min_keycode; keyc<=max_keycode; keyc++)
991 { /* this could be speeded up by making another table, an array of struct vkey,keycode
992 * (vkey -> keycode) with vkeys sorted .... but it takes memory (512*3 bytes)! DF */
993 if ((keyc2vkey[keyc] & 0xFF)== virtKey) /* no need to make a more precise test (with the extended bit correctly set above virtKey ... VK* are different enough... */
995 if ((e.keycode) && ((virtKey<0x10) || (virtKey>0x12)))
996 /* it's normal to have 2 shift, control, and alt ! */
997 dprintf_keyboard(stddeb,"ToAscii : The keycodes %d and %d are matching the same vkey %#X\n",
998 e.keycode,keyc,virtKey);
999 e.keycode = keyc;
1002 if ((!e.keycode) && (lpKeyState[VK_NUMLOCK] & 0x01))
1004 if ((virtKey>=VK_NUMPAD0) && (virtKey<=VK_NUMPAD9))
1005 e.keycode = XKeysymToKeycode(e.display, virtKey-VK_NUMPAD0+XK_KP_0);
1006 if (virtKey==VK_DECIMAL)
1007 e.keycode = XKeysymToKeycode(e.display, XK_KP_Decimal);
1009 if (!e.keycode)
1011 fprintf(stderr,"ToAscii : Unknown virtual key %X !!! \n",virtKey);
1012 return virtKey; /* whatever */
1014 e.state = 0;
1015 if (lpKeyState[VK_SHIFT] & 0x80)
1016 e.state |= ShiftMask;
1017 dprintf_keyboard(stddeb,"ToAscii : lpKeyState[0x14(VK_CAPITAL)]=%#x\n",lpKeyState[VK_CAPITAL]);
1018 if (lpKeyState[VK_CAPITAL] & 0x01)
1019 e.state |= LockMask;
1020 if (lpKeyState[VK_CONTROL] & 0x80)
1021 if (lpKeyState[VK_MENU] & 0x80)
1022 e.state |= AltGrMask;
1023 else
1024 e.state |= ControlMask;
1025 if (lpKeyState[VK_NUMLOCK] & 0x01)
1026 e.state |= NumLockMask;
1027 dprintf_key(stddeb, "ToAscii(%04X, %04X) : faked state = %X\n",
1028 virtKey, scanCode, e.state);
1029 ret = XLookupString(&e, (LPVOID)lpChar, 2, &keysym, &cs);
1030 if (ret == 0)
1032 BYTE dead_char = 0;
1034 ((char*)lpChar)[1] = '\0';
1035 switch (keysym)
1037 /* symbolic ASCII is the same as defined in rfc1345 */
1038 case XK_dead_tilde :
1039 case 0x1000FE7E : /* Xfree's XK_Dtilde */
1040 dead_char = '~'; /* '? */
1041 break;
1042 case XK_dead_acute :
1043 case 0x1000FE27 : /* Xfree's XK_Dacute_accent */
1044 dead_char = 0xb4; /* '' */
1045 break;
1046 case XK_dead_circumflex :
1047 case 0x1000FE5E : /* Xfree's XK_Dcircumflex_accent */
1048 dead_char = '^'; /* '> */
1049 break;
1050 case XK_dead_grave :
1051 case 0x1000FE60 : /* Xfree's XK_Dgrave_accent */
1052 dead_char = '`'; /* '! */
1053 break;
1054 case XK_dead_diaeresis :
1055 case 0x1000FE22 : /* Xfree's XK_Ddiaeresis */
1056 dead_char = 0xa8; /* ': */
1057 break;
1058 case XK_dead_cedilla :
1059 dead_char = 0xb8; /* ', */
1060 break;
1061 case XK_dead_macron :
1062 dead_char = '-'; /* 'm isn't defined on iso-8859-x */
1063 break;
1064 case XK_dead_breve :
1065 dead_char = 0xa2; /* '( */
1066 break;
1067 case XK_dead_abovedot :
1068 dead_char = 0xff; /* '. */
1069 break;
1070 case XK_dead_abovering :
1071 dead_char = '0'; /* '0 isn't defined on iso-8859-x */
1072 break;
1073 case XK_dead_doubleacute :
1074 dead_char = 0xbd; /* '" */
1075 break;
1076 case XK_dead_caron :
1077 dead_char = 0xb7; /* '< */
1078 break;
1079 case XK_dead_ogonek :
1080 dead_char = 0xb2; /* '; */
1081 break;
1082 /* FIXME: I don't know this three.
1083 case XK_dead_iota :
1084 dead_char = 'i';
1085 break;
1086 case XK_dead_voiced_sound :
1087 dead_char = 'v';
1088 break;
1089 case XK_dead_semivoiced_sound :
1090 dead_char = 's';
1091 break;
1094 if (dead_char)
1096 *(char*)lpChar = dead_char;
1097 ret = -1;
1099 else
1101 char *ksname;
1103 ksname = XKeysymToString(keysym);
1104 if (!ksname)
1105 ksname = "No Name";
1106 if ((keysym >> 8) != 0xff)
1108 fprintf(stderr, "Please report : no char for keysym %04lX (%s) :\n",
1109 keysym, ksname);
1110 fprintf(stderr, " virtKey = %X, scanCode = %X, keycode = %X, state = %X\n",
1111 virtKey, scanCode, e.keycode, e.state);
1115 dprintf_key(stddeb, "ToAscii about to return %d with char %x\n",
1116 ret, *(char*)lpChar);
1117 return ret;