Check that ci->hWindowMenu is not zero before using it.
[wine.git] / windows / input.c
blob1e67fccc0434707ff8ef3f643fa16cbd6dea7275
1 /*
2 * USER Input processing
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
12 #include <stdlib.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <ctype.h>
16 #include <assert.h>
18 #include "windef.h"
19 #include "wingdi.h"
20 #include "winuser.h"
21 #include "wine/winbase16.h"
22 #include "wine/winuser16.h"
23 #include "wine/keyboard16.h"
24 #include "win.h"
25 #include "heap.h"
26 #include "input.h"
27 #include "keyboard.h"
28 #include "mouse.h"
29 #include "message.h"
30 #include "module.h"
31 #include "debugtools.h"
32 #include "struct32.h"
33 #include "winerror.h"
34 #include "task.h"
36 DECLARE_DEBUG_CHANNEL(accel);
37 DECLARE_DEBUG_CHANNEL(event);
38 DECLARE_DEBUG_CHANNEL(key);
39 DECLARE_DEBUG_CHANNEL(keyboard);
40 DECLARE_DEBUG_CHANNEL(win);
42 static BOOL InputEnabled = TRUE;
43 BOOL SwappedButtons = FALSE;
45 BOOL MouseButtonsStates[3];
46 BOOL AsyncMouseButtonsStates[3];
47 BYTE InputKeyStateTable[256];
48 BYTE QueueKeyStateTable[256];
49 BYTE AsyncKeyStateTable[256];
51 /* Storage for the USER-maintained mouse positions */
52 DWORD PosX, PosY;
54 typedef union
56 struct
58 unsigned long count : 16;
59 unsigned long code : 8;
60 unsigned long extended : 1;
61 unsigned long unused : 2;
62 unsigned long win_internal : 2;
63 unsigned long context : 1;
64 unsigned long previous : 1;
65 unsigned long transition : 1;
66 } lp1;
67 unsigned long lp2;
68 } KEYLP;
70 /***********************************************************************
71 * keybd_event (USER32.583)
73 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
74 DWORD dwFlags, DWORD dwExtraInfo )
76 DWORD time, extra;
77 WORD message;
78 KEYLP keylp;
79 keylp.lp2 = 0;
81 if (!InputEnabled) return;
84 * If we are called by the Wine keyboard driver, use the additional
85 * info pointed to by the dwExtraInfo argument.
86 * Otherwise, we need to determine that info ourselves (probably
87 * less accurate, but we can't help that ...).
89 if ( !IsBadReadPtr( (LPVOID)dwExtraInfo, sizeof(WINE_KEYBDEVENT) )
90 && ((WINE_KEYBDEVENT *)dwExtraInfo)->magic == WINE_KEYBDEVENT_MAGIC )
92 WINE_KEYBDEVENT *wke = (WINE_KEYBDEVENT *)dwExtraInfo;
93 time = wke->time;
94 extra = 0;
96 else
98 time = GetTickCount();
99 extra = dwExtraInfo;
103 keylp.lp1.count = 1;
104 keylp.lp1.code = bScan;
105 keylp.lp1.extended = (dwFlags & KEYEVENTF_EXTENDEDKEY) != 0;
106 keylp.lp1.win_internal = 0; /* this has something to do with dialogs,
107 * don't remember where I read it - AK */
108 /* it's '1' under windows, when a dialog box appears
109 * and you press one of the underlined keys - DF*/
111 if ( dwFlags & KEYEVENTF_KEYUP )
113 BOOL sysKey = (InputKeyStateTable[VK_MENU] & 0x80)
114 && !(InputKeyStateTable[VK_CONTROL] & 0x80)
115 && !(dwFlags & KEYEVENTF_WINE_FORCEEXTENDED); /* for Alt from AltGr */
117 InputKeyStateTable[bVk] &= ~0x80;
118 keylp.lp1.previous = 1;
119 keylp.lp1.transition = 1;
120 message = sysKey ? WM_SYSKEYUP : WM_KEYUP;
122 else
124 keylp.lp1.previous = (InputKeyStateTable[bVk] & 0x80) != 0;
125 keylp.lp1.transition = 0;
127 if (!(InputKeyStateTable[bVk] & 0x80))
128 InputKeyStateTable[bVk] ^= 0x01;
129 InputKeyStateTable[bVk] |= 0x80;
131 message = (InputKeyStateTable[VK_MENU] & 0x80)
132 && !(InputKeyStateTable[VK_CONTROL] & 0x80)
133 ? WM_SYSKEYDOWN : WM_KEYDOWN;
136 if ( message == WM_SYSKEYDOWN || message == WM_SYSKEYUP )
137 keylp.lp1.context = (InputKeyStateTable[VK_MENU] & 0x80) != 0; /* 1 if alt */
140 TRACE_(key)(" wParam=%04X, lParam=%08lX\n", bVk, keylp.lp2 );
141 TRACE_(key)(" InputKeyState=%X\n", InputKeyStateTable[bVk] );
143 hardware_event( message, bVk, keylp.lp2, PosX, PosY, time, extra );
146 /***********************************************************************
147 * WIN16_keybd_event (USER.289)
149 void WINAPI WIN16_keybd_event( CONTEXT86 *context )
151 DWORD dwFlags = 0;
153 if (AH_reg(context) & 0x80) dwFlags |= KEYEVENTF_KEYUP;
154 if (BH_reg(context) & 1 ) dwFlags |= KEYEVENTF_EXTENDEDKEY;
156 keybd_event( AL_reg(context), BL_reg(context),
157 dwFlags, MAKELONG(SI_reg(context), DI_reg(context)) );
160 /***********************************************************************
161 * mouse_event (USER32.584)
163 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
164 DWORD cButtons, DWORD dwExtraInfo )
166 DWORD time, extra;
167 DWORD keyState;
169 if (!InputEnabled) return;
171 if ( dwFlags & MOUSEEVENTF_MOVE )
173 if ( dwFlags & MOUSEEVENTF_ABSOLUTE )
175 PosX = (dx * GetSystemMetrics(SM_CXSCREEN)) >> 16;
176 PosY = (dy * GetSystemMetrics(SM_CYSCREEN)) >> 16;
178 else
180 int width = GetSystemMetrics(SM_CXSCREEN);
181 int height = GetSystemMetrics(SM_CYSCREEN);
182 long posX = (long) PosX, posY = (long) PosY;
184 /* dx and dy can be negative numbers for relative movements */
185 posX += (long) dx;
186 posY += (long) dy;
188 /* Clip to the current screen size */
189 if (posX < 0) PosX = 0;
190 else if (posX >= width) PosX = width - 1;
191 else PosX = posX;
193 if (posY < 0) PosY = 0;
194 else if (posY >= height) PosY = height - 1;
195 else PosY = posY;
200 * If we are called by the Wine mouse driver, use the additional
201 * info pointed to by the dwExtraInfo argument.
202 * Otherwise, we need to determine that info ourselves (probably
203 * less accurate, but we can't help that ...).
205 if ( !IsBadReadPtr( (LPVOID)dwExtraInfo, sizeof(WINE_MOUSEEVENT) )
206 && ((WINE_MOUSEEVENT *)dwExtraInfo)->magic == WINE_MOUSEEVENT_MAGIC )
208 WINE_MOUSEEVENT *wme = (WINE_MOUSEEVENT *)dwExtraInfo;
209 time = wme->time;
210 extra = (DWORD)wme->hWnd;
211 keyState = wme->keyState;
213 if (keyState != GET_KEYSTATE()) {
214 /* We need to update the keystate with what X provides us */
215 MouseButtonsStates[SwappedButtons ? 2 : 0] = (keyState & MK_LBUTTON ? TRUE : FALSE);
216 MouseButtonsStates[SwappedButtons ? 0 : 2] = (keyState & MK_RBUTTON ? TRUE : FALSE);
217 MouseButtonsStates[1] = (keyState & MK_MBUTTON ? TRUE : FALSE);
218 InputKeyStateTable[VK_SHIFT] = (keyState & MK_SHIFT ? 0x80 : 0);
219 InputKeyStateTable[VK_CONTROL] = (keyState & MK_CONTROL ? 0x80 : 0);
222 else
224 time = GetTickCount();
225 extra = dwExtraInfo;
226 keyState = GET_KEYSTATE();
228 if ( dwFlags & MOUSEEVENTF_MOVE )
230 /* We have to actually move the cursor */
231 SetCursorPos( PosX, PosY );
236 if ( dwFlags & MOUSEEVENTF_MOVE )
238 hardware_event( WM_MOUSEMOVE,
239 keyState, 0L, PosX, PosY, time, extra );
241 if ( dwFlags & (!SwappedButtons? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_RIGHTDOWN) )
243 MouseButtonsStates[0] = AsyncMouseButtonsStates[0] = TRUE;
244 hardware_event( WM_LBUTTONDOWN,
245 keyState, 0L, PosX, PosY, time, extra );
247 if ( dwFlags & (!SwappedButtons? MOUSEEVENTF_LEFTUP : MOUSEEVENTF_RIGHTUP) )
249 MouseButtonsStates[0] = FALSE;
250 hardware_event( WM_LBUTTONUP,
251 keyState, 0L, PosX, PosY, time, extra );
253 if ( dwFlags & (!SwappedButtons? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_LEFTDOWN) )
255 MouseButtonsStates[2] = AsyncMouseButtonsStates[2] = TRUE;
256 hardware_event( WM_RBUTTONDOWN,
257 keyState, 0L, PosX, PosY, time, extra );
259 if ( dwFlags & (!SwappedButtons? MOUSEEVENTF_RIGHTUP : MOUSEEVENTF_LEFTUP) )
261 MouseButtonsStates[2] = FALSE;
262 hardware_event( WM_RBUTTONUP,
263 keyState, 0L, PosX, PosY, time, extra );
265 if ( dwFlags & MOUSEEVENTF_MIDDLEDOWN )
267 MouseButtonsStates[1] = AsyncMouseButtonsStates[1] = TRUE;
268 hardware_event( WM_MBUTTONDOWN,
269 keyState, 0L, PosX, PosY, time, extra );
271 if ( dwFlags & MOUSEEVENTF_MIDDLEUP )
273 MouseButtonsStates[1] = FALSE;
274 hardware_event( WM_MBUTTONUP,
275 keyState, 0L, PosX, PosY, time, extra );
279 /***********************************************************************
280 * WIN16_mouse_event (USER.299)
282 void WINAPI WIN16_mouse_event( CONTEXT86 *context )
284 mouse_event( AX_reg(context), BX_reg(context), CX_reg(context),
285 DX_reg(context), MAKELONG(SI_reg(context), DI_reg(context)) );
288 /***********************************************************************
289 * GetMouseEventProc (USER.337)
291 FARPROC16 WINAPI GetMouseEventProc16(void)
293 HMODULE16 hmodule = GetModuleHandle16("USER");
294 return NE_GetEntryPoint( hmodule, NE_GetOrdinal( hmodule, "mouse_event" ));
298 /**********************************************************************
299 * EnableHardwareInput (USER.331)
301 BOOL16 WINAPI EnableHardwareInput16(BOOL16 bEnable)
303 BOOL16 bOldState = InputEnabled;
304 FIXME_(event)("(%d) - stub\n", bEnable);
305 InputEnabled = bEnable;
306 return bOldState;
310 /***********************************************************************
311 * SwapMouseButton16 (USER.186)
313 BOOL16 WINAPI SwapMouseButton16( BOOL16 fSwap )
315 BOOL16 ret = SwappedButtons;
316 SwappedButtons = fSwap;
317 return ret;
321 /***********************************************************************
322 * SwapMouseButton32 (USER32.537)
324 BOOL WINAPI SwapMouseButton( BOOL fSwap )
326 BOOL ret = SwappedButtons;
327 SwappedButtons = fSwap;
328 return ret;
331 /**********************************************************************
332 * EVENT_Capture
334 * We need this to be able to generate double click messages
335 * when menu code captures mouse in the window without CS_DBLCLK style.
337 HWND EVENT_Capture(HWND hwnd, INT16 ht)
339 HWND capturePrev = 0, captureWnd = 0;
340 MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
341 WND* wndPtr = 0;
342 INT16 captureHT = 0;
344 /* Get the messageQ for the current thread */
345 if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
347 WARN_(win)("\tCurrent message queue not found. Exiting!\n" );
348 goto CLEANUP;
351 /* Get the current capture window from the perQ data of the current message Q */
352 capturePrev = PERQDATA_GetCaptureWnd( pCurMsgQ->pQData );
354 if (!hwnd)
356 captureWnd = 0L;
357 captureHT = 0;
359 else
361 wndPtr = WIN_FindWndPtr( hwnd );
362 if (wndPtr)
364 TRACE_(win)("(0x%04x)\n", hwnd );
365 captureWnd = hwnd;
366 captureHT = ht;
370 /* Update the perQ capture window and send messages */
371 if( capturePrev != captureWnd )
373 if (wndPtr)
375 /* Retrieve the message queue associated with this window */
376 pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
377 if ( !pMsgQ )
379 WARN_(win)("\tMessage queue not found. Exiting!\n" );
380 goto CLEANUP;
383 /* Make sure that message queue for the window we are setting capture to
384 * shares the same perQ data as the current threads message queue.
386 if ( pCurMsgQ->pQData != pMsgQ->pQData )
387 goto CLEANUP;
390 PERQDATA_SetCaptureWnd( pCurMsgQ->pQData, captureWnd );
391 PERQDATA_SetCaptureInfo( pCurMsgQ->pQData, captureHT );
393 if( capturePrev )
395 WND* wndPtr = WIN_FindWndPtr( capturePrev );
396 if( wndPtr && (wndPtr->flags & WIN_ISWIN32) )
397 SendMessageA( capturePrev, WM_CAPTURECHANGED, 0L, hwnd);
398 WIN_ReleaseWndPtr(wndPtr);
402 CLEANUP:
403 /* Unlock the queues before returning */
404 if ( pMsgQ )
405 QUEUE_Unlock( pMsgQ );
406 if ( pCurMsgQ )
407 QUEUE_Unlock( pCurMsgQ );
409 WIN_ReleaseWndPtr(wndPtr);
410 return capturePrev;
414 /**********************************************************************
415 * SetCapture16 (USER.18)
417 HWND16 WINAPI SetCapture16( HWND16 hwnd )
419 return (HWND16)EVENT_Capture( hwnd, HTCLIENT );
423 /**********************************************************************
424 * SetCapture32 (USER32.464)
426 HWND WINAPI SetCapture( HWND hwnd )
428 return EVENT_Capture( hwnd, HTCLIENT );
432 /**********************************************************************
433 * ReleaseCapture (USER.19) (USER32.439)
435 BOOL WINAPI ReleaseCapture(void)
437 return (EVENT_Capture( 0, 0 ) != 0);
441 /**********************************************************************
442 * GetCapture16 (USER.236)
444 HWND16 WINAPI GetCapture16(void)
446 return (HWND16)GetCapture();
449 /**********************************************************************
450 * GetCapture32 (USER32.208)
452 HWND WINAPI GetCapture(void)
454 MESSAGEQUEUE *pCurMsgQ = 0;
455 HWND hwndCapture = 0;
457 /* Get the messageQ for the current thread */
458 if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
460 TRACE_(win)("GetCapture32: Current message queue not found. Exiting!\n" );
461 return 0;
464 /* Get the current capture window from the perQ data of the current message Q */
465 hwndCapture = PERQDATA_GetCaptureWnd( pCurMsgQ->pQData );
467 QUEUE_Unlock( pCurMsgQ );
468 return hwndCapture;
471 /**********************************************************************
472 * GetKeyState (USER.106)
474 INT16 WINAPI GetKeyState16(INT16 vkey)
476 return GetKeyState(vkey);
479 /**********************************************************************
480 * GetKeyState (USER32.249)
482 * An application calls the GetKeyState function in response to a
483 * keyboard-input message. This function retrieves the state of the key
484 * at the time the input message was generated. (SDK 3.1 Vol 2. p 390)
486 SHORT WINAPI GetKeyState(INT vkey)
488 INT retval;
490 switch (vkey)
492 case VK_LBUTTON : /* VK_LBUTTON is 1 */
493 retval = MouseButtonsStates[0] ? 0x8000 : 0;
494 break;
495 case VK_MBUTTON : /* VK_MBUTTON is 4 */
496 retval = MouseButtonsStates[1] ? 0x8000 : 0;
497 break;
498 case VK_RBUTTON : /* VK_RBUTTON is 2 */
499 retval = MouseButtonsStates[2] ? 0x8000 : 0;
500 break;
501 default :
502 if (vkey >= 'a' && vkey <= 'z')
503 vkey += 'A' - 'a';
504 retval = ( (WORD)(QueueKeyStateTable[vkey] & 0x80) << 8 ) |
505 (WORD)(QueueKeyStateTable[vkey] & 0x01);
507 /* TRACE(key, "(0x%x) -> %x\n", vkey, retval); */
508 return retval;
511 /**********************************************************************
512 * GetKeyboardState (USER.222)(USER32.254)
514 * An application calls the GetKeyboardState function in response to a
515 * keyboard-input message. This function retrieves the state of the keyboard
516 * at the time the input message was generated. (SDK 3.1 Vol 2. p 387)
518 BOOL WINAPI GetKeyboardState(LPBYTE lpKeyState)
520 TRACE_(key)("(%p)\n", lpKeyState);
521 if (lpKeyState != NULL) {
522 QueueKeyStateTable[VK_LBUTTON] = MouseButtonsStates[0] ? 0x80 : 0;
523 QueueKeyStateTable[VK_MBUTTON] = MouseButtonsStates[1] ? 0x80 : 0;
524 QueueKeyStateTable[VK_RBUTTON] = MouseButtonsStates[2] ? 0x80 : 0;
525 memcpy(lpKeyState, QueueKeyStateTable, 256);
528 return TRUE;
531 /**********************************************************************
532 * SetKeyboardState (USER.223)(USER32.484)
534 BOOL WINAPI SetKeyboardState(LPBYTE lpKeyState)
536 TRACE_(key)("(%p)\n", lpKeyState);
537 if (lpKeyState != NULL) {
538 memcpy(QueueKeyStateTable, lpKeyState, 256);
539 MouseButtonsStates[0] = (QueueKeyStateTable[VK_LBUTTON] != 0);
540 MouseButtonsStates[1] = (QueueKeyStateTable[VK_MBUTTON] != 0);
541 MouseButtonsStates[2] = (QueueKeyStateTable[VK_RBUTTON] != 0);
544 return TRUE;
547 /**********************************************************************
548 * GetAsyncKeyState32 (USER32.207)
550 * Determine if a key is or was pressed. retval has high-order
551 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
552 * been pressed.
554 * This uses the variable AsyncMouseButtonsStates and
555 * AsyncKeyStateTable (set in event.c) which have the mouse button
556 * number or key number (whichever is applicable) set to true if the
557 * mouse or key had been depressed since the last call to
558 * GetAsyncKeyState.
560 WORD WINAPI GetAsyncKeyState(INT nKey)
562 short retval;
564 switch (nKey) {
565 case VK_LBUTTON:
566 retval = (AsyncMouseButtonsStates[0] ? 0x0001 : 0) |
567 (MouseButtonsStates[0] ? 0x8000 : 0);
568 break;
569 case VK_MBUTTON:
570 retval = (AsyncMouseButtonsStates[1] ? 0x0001 : 0) |
571 (MouseButtonsStates[1] ? 0x8000 : 0);
572 break;
573 case VK_RBUTTON:
574 retval = (AsyncMouseButtonsStates[2] ? 0x0001 : 0) |
575 (MouseButtonsStates[2] ? 0x8000 : 0);
576 break;
577 default:
578 retval = AsyncKeyStateTable[nKey] |
579 ((InputKeyStateTable[nKey] & 0x80) ? 0x8000 : 0);
580 break;
583 /* all states to false */
584 memset( AsyncMouseButtonsStates, 0, sizeof(AsyncMouseButtonsStates) );
585 memset( AsyncKeyStateTable, 0, sizeof(AsyncKeyStateTable) );
587 TRACE_(key)("(%x) -> %x\n", nKey, retval);
588 return retval;
591 /**********************************************************************
592 * GetAsyncKeyState16 (USER.249)
594 WORD WINAPI GetAsyncKeyState16(INT16 nKey)
596 return GetAsyncKeyState(nKey);
599 /***********************************************************************
600 * IsUserIdle (USER.333)
602 BOOL16 WINAPI IsUserIdle16(void)
604 if ( GetAsyncKeyState( VK_LBUTTON ) & 0x8000 )
605 return FALSE;
607 if ( GetAsyncKeyState( VK_RBUTTON ) & 0x8000 )
608 return FALSE;
610 if ( GetAsyncKeyState( VK_MBUTTON ) & 0x8000 )
611 return FALSE;
613 /* Should check for screen saver activation here ... */
615 return TRUE;
618 /**********************************************************************
619 * KBD_translate_accelerator
621 * FIXME: should send some WM_INITMENU or/and WM_INITMENUPOPUP -messages
623 static BOOL KBD_translate_accelerator(HWND hWnd,LPMSG msg,
624 BYTE fVirt,WORD key,WORD cmd)
626 BOOL sendmsg = FALSE;
628 if(msg->wParam == key)
630 if (msg->message == WM_CHAR) {
631 if ( !(fVirt & FALT) && !(fVirt & FVIRTKEY) )
633 TRACE_(accel)("found accel for WM_CHAR: ('%c')\n",
634 msg->wParam&0xff);
635 sendmsg=TRUE;
637 } else {
638 if(fVirt & FVIRTKEY) {
639 INT mask = 0;
640 TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
641 msg->wParam,0xff & HIWORD(msg->lParam));
642 if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
643 if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
644 if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
645 if(mask == (fVirt & (FSHIFT | FCONTROL | FALT)))
646 sendmsg=TRUE;
647 else
648 TRACE_(accel)(", but incorrect SHIFT/CTRL/ALT-state\n");
650 else
652 if (!(msg->lParam & 0x01000000)) /* no special_key */
654 if ((fVirt & FALT) && (msg->lParam & 0x20000000))
655 { /* ^^ ALT pressed */
656 TRACE_(accel)("found accel for Alt-%c\n", msg->wParam&0xff);
657 sendmsg=TRUE;
663 if (sendmsg) /* found an accelerator, but send a message... ? */
665 INT16 iSysStat,iStat,mesg=0;
666 HMENU16 hMenu;
668 if (msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP)
669 mesg=1;
670 else
671 if (GetCapture())
672 mesg=2;
673 else
674 if (!IsWindowEnabled(hWnd))
675 mesg=3;
676 else
678 WND* wndPtr = WIN_FindWndPtr(hWnd);
680 hMenu = (wndPtr->dwStyle & WS_CHILD) ? 0 : (HMENU)wndPtr->wIDmenu;
681 iSysStat = (wndPtr->hSysMenu) ? GetMenuState(GetSubMenu16(wndPtr->hSysMenu, 0),
682 cmd, MF_BYCOMMAND) : -1 ;
683 iStat = (hMenu) ? GetMenuState(hMenu,
684 cmd, MF_BYCOMMAND) : -1 ;
686 WIN_ReleaseWndPtr(wndPtr);
688 if (iSysStat!=-1)
690 if (iSysStat & (MF_DISABLED|MF_GRAYED))
691 mesg=4;
692 else
693 mesg=WM_SYSCOMMAND;
695 else
697 if (iStat!=-1)
699 if (IsIconic(hWnd))
700 mesg=5;
701 else
703 if (iStat & (MF_DISABLED|MF_GRAYED))
704 mesg=6;
705 else
706 mesg=WM_COMMAND;
709 else
710 mesg=WM_COMMAND;
713 if ( mesg==WM_COMMAND || mesg==WM_SYSCOMMAND )
715 TRACE_(accel)(", sending %s, wParam=%0x\n",
716 mesg==WM_COMMAND ? "WM_COMMAND" : "WM_SYSCOMMAND",
717 cmd);
718 SendMessageA(hWnd, mesg, cmd, 0x00010000L);
720 else
722 /* some reasons for NOT sending the WM_{SYS}COMMAND message:
723 * #0: unknown (please report!)
724 * #1: for WM_KEYUP,WM_SYSKEYUP
725 * #2: mouse is captured
726 * #3: window is disabled
727 * #4: it's a disabled system menu option
728 * #5: it's a menu option, but window is iconic
729 * #6: it's a menu option, but disabled
731 TRACE_(accel)(", but won't send WM_{SYS}COMMAND, reason is #%d\n",mesg);
732 if(mesg==0)
733 ERR_(accel)(" unknown reason - please report!");
735 return TRUE;
738 return FALSE;
741 /**********************************************************************
742 * TranslateAccelerator32 (USER32.551)(USER32.552)(USER32.553)
744 INT WINAPI TranslateAccelerator(HWND hWnd, HACCEL hAccel, LPMSG msg)
746 /* YES, Accel16! */
747 LPACCEL16 lpAccelTbl;
748 int i;
750 if (msg == NULL)
752 WARN_(accel)("msg null; should hang here to be win compatible\n");
753 return 0;
755 if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(hAccel)))
757 WARN_(accel)("invalid accel handle=%x\n", hAccel);
758 return 0;
760 if ((msg->message != WM_KEYDOWN &&
761 msg->message != WM_KEYUP &&
762 msg->message != WM_SYSKEYDOWN &&
763 msg->message != WM_SYSKEYUP &&
764 msg->message != WM_CHAR)) return 0;
766 TRACE_(accel)("TranslateAccelerators hAccel=%04x, hWnd=%04x,"
767 "msg->hwnd=%04x, msg->message=%04x, wParam=%08x, lParam=%lx\n",
768 hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
770 i = 0;
773 if (KBD_translate_accelerator(hWnd,msg,lpAccelTbl[i].fVirt,
774 lpAccelTbl[i].key,lpAccelTbl[i].cmd))
775 return 1;
776 } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);
777 WARN_(accel)("couldn't translate accelerator key\n");
778 return 0;
781 /**********************************************************************
782 * TranslateAccelerator16 (USER.178)
784 INT16 WINAPI TranslateAccelerator16(HWND16 hWnd, HACCEL16 hAccel, LPMSG16 msg)
786 LPACCEL16 lpAccelTbl;
787 int i;
788 MSG msg32;
790 if (msg == NULL)
792 WARN_(accel)("msg null; should hang here to be win compatible\n");
793 return 0;
795 if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(hAccel)))
797 WARN_(accel)("invalid accel handle=%x\n", hAccel);
798 return 0;
800 if ((msg->message != WM_KEYDOWN &&
801 msg->message != WM_KEYUP &&
802 msg->message != WM_SYSKEYDOWN &&
803 msg->message != WM_SYSKEYUP &&
804 msg->message != WM_CHAR)) return 0;
806 TRACE_(accel)("TranslateAccelerators hAccel=%04x, hWnd=%04x,\
807 msg->hwnd=%04x, msg->message=%04x, wParam=%04x, lParam=%lx\n", hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
809 STRUCT32_MSG16to32(msg,&msg32);
811 i = 0;
814 if (KBD_translate_accelerator(hWnd,&msg32,lpAccelTbl[i].fVirt,
815 lpAccelTbl[i].key,lpAccelTbl[i].cmd))
816 return 1;
817 } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);
818 WARN_(accel)("couldn't translate accelerator key\n");
819 return 0;
823 /**********************************************************************
824 * VkKeyScanA (USER32.573)
826 WORD WINAPI VkKeyScanA(CHAR cChar)
828 return VkKeyScan16(cChar);
831 /******************************************************************************
832 * VkKeyScanW (USER32.576)
834 WORD WINAPI VkKeyScanW(WCHAR cChar)
836 return VkKeyScanA((CHAR)cChar); /* FIXME: check unicode */
839 /**********************************************************************
840 * VkKeyScanExA (USER32.574)
842 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
844 /* FIXME: complete workaround this is */
845 return VkKeyScan16(cChar);
848 /******************************************************************************
849 * VkKeyScanExW (USER32.575)
851 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
853 /* FIXME: complete workaround this is */
854 return VkKeyScanA((CHAR)cChar); /* FIXME: check unicode */
857 /******************************************************************************
858 * GetKeyboardType32 (USER32.255)
860 INT WINAPI GetKeyboardType(INT nTypeFlag)
862 return GetKeyboardType16(nTypeFlag);
865 /******************************************************************************
866 * MapVirtualKey32A (USER32.383)
868 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
870 return MapVirtualKey16(code,maptype);
873 /******************************************************************************
874 * MapVirtualKey32W (USER32.385)
876 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
878 return MapVirtualKey16(code,maptype);
881 /******************************************************************************
882 * MapVirtualKeyEx32A (USER32.384)
884 UINT WINAPI MapVirtualKeyEx32A(UINT code, UINT maptype, HKL hkl)
886 if (hkl)
887 FIXME_(keyboard)("(%d,%d,0x%08lx), hkl unhandled!\n",code,maptype,(DWORD)hkl);
888 return MapVirtualKey16(code,maptype);
891 /****************************************************************************
892 * GetKBCodePage32 (USER32.246)
894 UINT WINAPI GetKBCodePage(void)
896 return GetKBCodePage16();
899 /****************************************************************************
900 * GetKeyboardLayoutName16 (USER.477)
902 INT16 WINAPI GetKeyboardLayoutName16(LPSTR pwszKLID)
904 return GetKeyboardLayoutNameA(pwszKLID);
907 /***********************************************************************
908 * GetKeyboardLayout (USER32.250)
910 * FIXME: - device handle for keyboard layout defaulted to
911 * the language id. This is the way Windows default works.
912 * - the thread identifier (dwLayout) is also ignored.
914 HKL WINAPI GetKeyboardLayout(DWORD dwLayout)
916 HKL layout;
917 layout = GetSystemDefaultLCID(); /* FIXME */
918 layout |= (layout<<16); /* FIXME */
919 TRACE_(keyboard)("returning %08x\n",layout);
920 return layout;
923 /****************************************************************************
924 * GetKeyboardLayoutName32A (USER32.252)
926 INT WINAPI GetKeyboardLayoutNameA(LPSTR pwszKLID)
928 sprintf(pwszKLID, "%08x",GetKeyboardLayout(0));
929 return 1;
932 /****************************************************************************
933 * GetKeyboardLayoutName32W (USER32.253)
935 INT WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
937 LPSTR buf = HEAP_xalloc( GetProcessHeap(), 0, strlen("00000409")+1);
938 int res = GetKeyboardLayoutNameA(buf);
939 lstrcpyAtoW(pwszKLID,buf);
940 HeapFree( GetProcessHeap(), 0, buf );
941 return res;
944 /****************************************************************************
945 * GetKeyNameText32A (USER32.247)
947 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
949 return GetKeyNameText16(lParam,lpBuffer,nSize);
952 /****************************************************************************
953 * GetKeyNameText32W (USER32.248)
955 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
957 LPSTR buf = HEAP_xalloc( GetProcessHeap(), 0, nSize );
958 int res = GetKeyNameTextA(lParam,buf,nSize);
960 lstrcpynAtoW(lpBuffer,buf,nSize);
961 HeapFree( GetProcessHeap(), 0, buf );
962 return res;
965 /****************************************************************************
966 * ToAscii32 (USER32.546)
968 INT WINAPI ToAscii( UINT virtKey,UINT scanCode,LPBYTE lpKeyState,
969 LPWORD lpChar,UINT flags )
971 return ToAscii16(virtKey,scanCode,lpKeyState,lpChar,flags);
974 /****************************************************************************
975 * ToAscii32 (USER32.547)
977 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
978 LPWORD lpChar, UINT flags, HKL dwhkl )
980 /* FIXME: need true implementation */
981 return ToAscii16(virtKey,scanCode,lpKeyState,lpChar,flags);
984 /**********************************************************************
985 * ActivateKeyboardLayout32 (USER32.1)
987 * Call ignored. WINE supports only system default keyboard layout.
989 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
991 TRACE_(keyboard)("(%d, %d)\n", hLayout, flags);
992 ERR_(keyboard)("Only default system keyboard layout supported. Call ignored.\n");
993 return 0;
997 /***********************************************************************
998 * GetKeyboardLayoutList (USER32.251)
1000 * FIXME: Supports only the system default language and layout and
1001 * returns only 1 value.
1003 * Return number of values available if either input parm is
1004 * 0, per MS documentation.
1007 INT WINAPI GetKeyboardLayoutList(INT nBuff,HKL *layouts)
1009 TRACE_(keyboard)("(%d,%p)\n",nBuff,layouts);
1010 if (!nBuff || !layouts)
1011 return 1;
1012 if (layouts)
1013 layouts[0] = GetKeyboardLayout(0);
1014 return 1;
1018 /***********************************************************************
1019 * RegisterHotKey (USER32.433)
1021 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk) {
1022 FIXME_(keyboard)("(0x%08x,%d,0x%08x,%d): stub\n",hwnd,id,modifiers,vk);
1023 return TRUE;
1026 /***********************************************************************
1027 * UnregisterHotKey (USER32.565)
1029 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id) {
1030 FIXME_(keyboard)("(0x%08x,%d): stub\n",hwnd,id);
1031 return TRUE;
1035 /***********************************************************************
1036 * ToUnicode32 (USER32.548)
1038 INT WINAPI ToUnicode(
1039 UINT wVirtKey,
1040 UINT wScanCode,
1041 PBYTE lpKeyState,
1042 LPWSTR pwszBuff,
1043 INT cchBuff,
1044 UINT wFlags) {
1046 FIXME_(keyboard)(": stub\n");
1047 return 0;
1050 /***********************************************************************
1051 * LoadKeyboardLayout32A (USER32.367)
1052 * Call ignored. WINE supports only system default keyboard layout.
1054 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
1056 TRACE_(keyboard)("(%s, %d)\n", pwszKLID, Flags);
1057 ERR_(keyboard)("Only default system keyboard layout supported. Call ignored.\n");
1058 return 0;
1061 /***********************************************************************
1062 * LoadKeyboardLayout32W (USER32.368)
1064 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
1066 LPSTR buf = HEAP_xalloc( GetProcessHeap(), 0, strlen("00000409")+1);
1067 int res;
1068 lstrcpynWtoA(buf,pwszKLID,8);
1069 buf[8] = 0;
1070 res = LoadKeyboardLayoutA(buf, Flags);
1071 HeapFree( GetProcessHeap(), 0, buf );
1072 return res;