Add missing message to winerr_enu.mc.
[wine] / windows / input.c
1 /*
2  * USER Input processing
3  *
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  *
10  */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <ctype.h>
16 #include <assert.h>
17
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 "queue.h"
31 #include "module.h"
32 #include "debugtools.h"
33 #include "struct32.h"
34 #include "winerror.h"
35 #include "task.h"
36
37 DECLARE_DEBUG_CHANNEL(accel);
38 DECLARE_DEBUG_CHANNEL(event);
39 DECLARE_DEBUG_CHANNEL(key);
40 DECLARE_DEBUG_CHANNEL(keyboard);
41 DECLARE_DEBUG_CHANNEL(win);
42
43 static BOOL InputEnabled = TRUE;
44 BOOL SwappedButtons = FALSE;
45
46 BOOL MouseButtonsStates[3];
47 BOOL AsyncMouseButtonsStates[3];
48 BYTE InputKeyStateTable[256];
49 BYTE QueueKeyStateTable[256];
50 BYTE AsyncKeyStateTable[256];
51
52 /* Storage for the USER-maintained mouse positions */
53 DWORD PosX, PosY;
54
55 typedef union
56 {
57     struct
58     {
59         unsigned long count : 16;
60         unsigned long code : 8;
61         unsigned long extended : 1;
62         unsigned long unused : 2;
63         unsigned long win_internal : 2;
64         unsigned long context : 1;
65         unsigned long previous : 1;
66         unsigned long transition : 1;
67     } lp1;
68     unsigned long lp2;
69 } KEYLP;
70
71 /***********************************************************************
72  *           keybd_event   (USER32.583)
73  */
74 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
75                          DWORD dwFlags, DWORD dwExtraInfo )
76 {
77     DWORD time, extra;
78     WORD message;
79     KEYLP keylp;
80     keylp.lp2 = 0;
81
82     if (!InputEnabled) return;
83
84     /*
85      * If we are called by the Wine keyboard driver, use the additional
86      * info pointed to by the dwExtraInfo argument.
87      * Otherwise, we need to determine that info ourselves (probably
88      * less accurate, but we can't help that ...).
89      */
90     if (   !IsBadReadPtr( (LPVOID)dwExtraInfo, sizeof(WINE_KEYBDEVENT) )
91         && ((WINE_KEYBDEVENT *)dwExtraInfo)->magic == WINE_KEYBDEVENT_MAGIC )
92     {
93         WINE_KEYBDEVENT *wke = (WINE_KEYBDEVENT *)dwExtraInfo;
94         time = wke->time;
95         extra = 0;
96     }
97     else
98     {
99         time = GetTickCount();
100         extra = dwExtraInfo;
101     }
102
103
104     keylp.lp1.count = 1;
105     keylp.lp1.code = bScan;
106     keylp.lp1.extended = (dwFlags & KEYEVENTF_EXTENDEDKEY) != 0;
107     keylp.lp1.win_internal = 0; /* this has something to do with dialogs,
108                                 * don't remember where I read it - AK */
109                                 /* it's '1' under windows, when a dialog box appears
110                                  * and you press one of the underlined keys - DF*/
111
112     if ( dwFlags & KEYEVENTF_KEYUP )
113     {
114         BOOL sysKey = (InputKeyStateTable[VK_MENU] & 0x80)
115                 && !(InputKeyStateTable[VK_CONTROL] & 0x80)
116                 && !(dwFlags & KEYEVENTF_WINE_FORCEEXTENDED); /* for Alt from AltGr */
117
118         InputKeyStateTable[bVk] &= ~0x80;
119         keylp.lp1.previous = 1;
120         keylp.lp1.transition = 1;
121         message = sysKey ? WM_SYSKEYUP : WM_KEYUP;
122     }
123     else
124     {
125         keylp.lp1.previous = (InputKeyStateTable[bVk] & 0x80) != 0;
126         keylp.lp1.transition = 0;
127
128         if (!(InputKeyStateTable[bVk] & 0x80))
129             InputKeyStateTable[bVk] ^= 0x01;
130         InputKeyStateTable[bVk] |= 0x80;
131
132         message = (InputKeyStateTable[VK_MENU] & 0x80)
133               && !(InputKeyStateTable[VK_CONTROL] & 0x80)
134               ? WM_SYSKEYDOWN : WM_KEYDOWN;
135     }
136
137     if ( message == WM_SYSKEYDOWN || message == WM_SYSKEYUP )
138         keylp.lp1.context = (InputKeyStateTable[VK_MENU] & 0x80) != 0; /* 1 if alt */
139
140
141     TRACE_(key)("            wParam=%04X, lParam=%08lX\n", bVk, keylp.lp2 );
142     TRACE_(key)("            InputKeyState=%X\n", InputKeyStateTable[bVk] );
143
144     hardware_event( message, bVk, keylp.lp2, PosX, PosY, time, extra );
145 }
146
147 /***********************************************************************
148  *           WIN16_keybd_event   (USER.289)
149  */
150 void WINAPI WIN16_keybd_event( CONTEXT86 *context )
151 {
152     DWORD dwFlags = 0;
153
154     if (AH_reg(context) & 0x80) dwFlags |= KEYEVENTF_KEYUP;
155     if (BH_reg(context) & 1   ) dwFlags |= KEYEVENTF_EXTENDEDKEY;
156
157     keybd_event( AL_reg(context), BL_reg(context),
158                  dwFlags, MAKELONG(SI_reg(context), DI_reg(context)) );
159 }
160
161 /***********************************************************************
162  *           mouse_event   (USER32.584)
163  */
164 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
165                          DWORD cButtons, DWORD dwExtraInfo )
166 {
167     DWORD time, extra;
168     DWORD keyState;
169     
170     if (!InputEnabled) return;
171
172     if ( dwFlags & MOUSEEVENTF_MOVE )
173       {
174         if ( dwFlags & MOUSEEVENTF_ABSOLUTE )
175           {
176             PosX = (dx * GetSystemMetrics(SM_CXSCREEN)) >> 16;
177             PosY = (dy * GetSystemMetrics(SM_CYSCREEN)) >> 16;
178           }
179         else
180           {
181             int width  = GetSystemMetrics(SM_CXSCREEN);
182             int height = GetSystemMetrics(SM_CYSCREEN);
183             long posX = (long) PosX, posY = (long) PosY;
184
185             /* dx and dy can be negative numbers for relative movements */
186             posX += (long) dx;
187             posY += (long) dy;
188
189             /* Clip to the current screen size */
190             if (posX < 0) PosX = 0;
191             else if (posX >= width) PosX = width - 1;
192             else PosX = posX;
193
194             if (posY < 0) PosY = 0;
195             else if (posY >= height) PosY = height - 1;
196             else PosY = posY;
197           }
198       }
199
200     /*
201      * If we are called by the Wine mouse driver, use the additional
202      * info pointed to by the dwExtraInfo argument.
203      * Otherwise, we need to determine that info ourselves (probably
204      * less accurate, but we can't help that ...).
205      */
206     if (   !IsBadReadPtr( (LPVOID)dwExtraInfo, sizeof(WINE_MOUSEEVENT) )
207         && ((WINE_MOUSEEVENT *)dwExtraInfo)->magic == WINE_MOUSEEVENT_MAGIC )
208     {
209         WINE_MOUSEEVENT *wme = (WINE_MOUSEEVENT *)dwExtraInfo;
210         time = wme->time;
211         extra = (DWORD)wme->hWnd;
212         keyState = wme->keyState;
213         
214         if (keyState != GET_KEYSTATE()) {
215           /* We need to update the keystate with what X provides us */
216           MouseButtonsStates[SwappedButtons ? 2 : 0] = (keyState & MK_LBUTTON ? TRUE : FALSE);
217           MouseButtonsStates[SwappedButtons ? 0 : 2] = (keyState & MK_RBUTTON ? TRUE : FALSE);
218           MouseButtonsStates[1]                      = (keyState & MK_MBUTTON ? TRUE : FALSE);
219           InputKeyStateTable[VK_SHIFT]               = (keyState & MK_SHIFT   ? 0x80 : 0);
220           InputKeyStateTable[VK_CONTROL]             = (keyState & MK_CONTROL ? 0x80 : 0);
221         }
222     }
223     else
224     {
225         time = GetTickCount();
226         extra = dwExtraInfo;
227         keyState = GET_KEYSTATE();
228         
229         if ( dwFlags & MOUSEEVENTF_MOVE )
230           {
231             /* We have to actually move the cursor */
232             SetCursorPos( PosX, PosY );
233           }
234     }
235
236
237     if ( dwFlags & MOUSEEVENTF_MOVE )
238     {
239         hardware_event( WM_MOUSEMOVE,
240                         keyState, 0L, PosX, PosY, time, extra );
241     }
242     if ( dwFlags & (!SwappedButtons? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_RIGHTDOWN) )
243     {
244         MouseButtonsStates[0] = AsyncMouseButtonsStates[0] = TRUE;
245         hardware_event( WM_LBUTTONDOWN,
246                         keyState, 0L, PosX, PosY, time, extra );
247     }
248     if ( dwFlags & (!SwappedButtons? MOUSEEVENTF_LEFTUP : MOUSEEVENTF_RIGHTUP) )
249     {
250         MouseButtonsStates[0] = FALSE;
251         hardware_event( WM_LBUTTONUP,
252                         keyState, 0L, PosX, PosY, time, extra );
253     }
254     if ( dwFlags & (!SwappedButtons? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_LEFTDOWN) )
255     {
256         MouseButtonsStates[2] = AsyncMouseButtonsStates[2] = TRUE;
257         hardware_event( WM_RBUTTONDOWN,
258                         keyState, 0L, PosX, PosY, time, extra );
259     }
260     if ( dwFlags & (!SwappedButtons? MOUSEEVENTF_RIGHTUP : MOUSEEVENTF_LEFTUP) )
261     {
262         MouseButtonsStates[2] = FALSE;
263         hardware_event( WM_RBUTTONUP,
264                         keyState, 0L, PosX, PosY, time, extra );
265     }
266     if ( dwFlags & MOUSEEVENTF_MIDDLEDOWN )
267     {
268         MouseButtonsStates[1] = AsyncMouseButtonsStates[1] = TRUE;
269         hardware_event( WM_MBUTTONDOWN,
270                         keyState, 0L, PosX, PosY, time, extra );
271     }
272     if ( dwFlags & MOUSEEVENTF_MIDDLEUP )
273     {
274         MouseButtonsStates[1] = FALSE;
275         hardware_event( WM_MBUTTONUP,
276                         keyState, 0L, PosX, PosY, time, extra );
277     }
278     if ( dwFlags & MOUSEEVENTF_WHEEL )
279     {
280         hardware_event( WM_MOUSEWHEEL,
281                         keyState, 0L, PosX, PosY, time, extra );
282     }
283 }
284
285 /***********************************************************************
286  *           WIN16_mouse_event   (USER.299)
287  */
288 void WINAPI WIN16_mouse_event( CONTEXT86 *context )
289 {
290     mouse_event( AX_reg(context), BX_reg(context), CX_reg(context),
291                  DX_reg(context), MAKELONG(SI_reg(context), DI_reg(context)) );
292 }
293
294 /***********************************************************************
295  *           GetMouseEventProc   (USER.337)
296  */
297 FARPROC16 WINAPI GetMouseEventProc16(void)
298 {
299     HMODULE16 hmodule = GetModuleHandle16("USER");
300     return NE_GetEntryPoint( hmodule, NE_GetOrdinal( hmodule, "mouse_event" ));
301 }
302
303
304 /**********************************************************************
305  *                      EnableHardwareInput   (USER.331)
306  */
307 BOOL16 WINAPI EnableHardwareInput16(BOOL16 bEnable)
308 {
309   BOOL16 bOldState = InputEnabled;
310   FIXME_(event)("(%d) - stub\n", bEnable);
311   InputEnabled = bEnable;
312   return bOldState;
313 }
314
315
316 /***********************************************************************
317  *           SwapMouseButton16   (USER.186)
318  */
319 BOOL16 WINAPI SwapMouseButton16( BOOL16 fSwap )
320 {
321     BOOL16 ret = SwappedButtons;
322     SwappedButtons = fSwap;
323     return ret;
324 }
325
326
327 /***********************************************************************
328  *           SwapMouseButton   (USER32.537)
329  */
330 BOOL WINAPI SwapMouseButton( BOOL fSwap )
331 {
332     BOOL ret = SwappedButtons;
333     SwappedButtons = fSwap;
334     return ret;
335 }
336
337 /**********************************************************************
338  *              EVENT_Capture
339  *
340  * We need this to be able to generate double click messages
341  * when menu code captures mouse in the window without CS_DBLCLK style.
342  */
343 HWND EVENT_Capture(HWND hwnd, INT16 ht)
344 {
345     HWND capturePrev = 0, captureWnd = 0;
346     MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
347     WND* wndPtr = 0;
348     INT16 captureHT = 0;
349
350     /* Get the messageQ for the current thread */
351     if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
352     {
353         WARN_(win)("\tCurrent message queue not found. Exiting!\n" );
354         goto CLEANUP;
355     }
356     
357     /* Get the current capture window from the perQ data of the current message Q */
358     capturePrev = PERQDATA_GetCaptureWnd( pCurMsgQ->pQData );
359
360     if (!hwnd)
361     {
362         captureWnd = 0L;
363         captureHT = 0;
364     }
365     else
366     {
367         wndPtr = WIN_FindWndPtr( hwnd );
368         if (wndPtr)
369         {
370             TRACE_(win)("(0x%04x)\n", hwnd );
371             captureWnd   = hwnd;
372             captureHT    = ht;
373         }
374     }
375
376     /* Update the perQ capture window and send messages */
377     if( capturePrev != captureWnd )
378     {
379         if (wndPtr)
380         {
381             /* Retrieve the message queue associated with this window */
382             pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
383             if ( !pMsgQ )
384             {
385                 WARN_(win)("\tMessage queue not found. Exiting!\n" );
386                 goto CLEANUP;
387             }
388     
389             /* Make sure that message queue for the window we are setting capture to
390              * shares the same perQ data as the current threads message queue.
391              */
392             if ( pCurMsgQ->pQData != pMsgQ->pQData )
393                 goto CLEANUP;
394         }
395
396         PERQDATA_SetCaptureWnd( pCurMsgQ->pQData, captureWnd );
397         PERQDATA_SetCaptureInfo( pCurMsgQ->pQData, captureHT );
398         
399         if( capturePrev )
400     {
401         WND* xwndPtr = WIN_FindWndPtr( capturePrev );
402         if( xwndPtr && (xwndPtr->flags & WIN_ISWIN32) )
403             SendMessageA( capturePrev, WM_CAPTURECHANGED, 0L, hwnd);
404         WIN_ReleaseWndPtr(xwndPtr);
405     }
406 }
407
408 CLEANUP:
409     /* Unlock the queues before returning */
410     if ( pMsgQ )
411         QUEUE_Unlock( pMsgQ );
412     if ( pCurMsgQ )
413         QUEUE_Unlock( pCurMsgQ );
414     
415     WIN_ReleaseWndPtr(wndPtr);
416     return capturePrev;
417 }
418
419
420 /**********************************************************************
421  *              SetCapture16   (USER.18)
422  */
423 HWND16 WINAPI SetCapture16( HWND16 hwnd )
424 {
425     return (HWND16)EVENT_Capture( hwnd, HTCLIENT );
426 }
427
428
429 /**********************************************************************
430  *              SetCapture   (USER32.464)
431  */
432 HWND WINAPI SetCapture( HWND hwnd )
433 {
434     return EVENT_Capture( hwnd, HTCLIENT );
435 }
436
437
438 /**********************************************************************
439  *              ReleaseCapture   (USER.19) (USER32.439)
440  */
441 BOOL WINAPI ReleaseCapture(void)
442 {
443     return (EVENT_Capture( 0, 0 ) != 0);
444 }
445
446
447 /**********************************************************************
448  *              GetCapture16   (USER.236)
449  */
450 HWND16 WINAPI GetCapture16(void)
451 {
452     return (HWND16)GetCapture();
453 }
454
455 /**********************************************************************
456  *              GetCapture   (USER32.208)
457  */
458 HWND WINAPI GetCapture(void)
459 {
460     MESSAGEQUEUE *pCurMsgQ = 0;
461     HWND hwndCapture = 0;
462
463     /* Get the messageQ for the current thread */
464     if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
465 {
466         TRACE_(win)("GetCapture32:  Current message queue not found. Exiting!\n" );
467         return 0;
468     }
469     
470     /* Get the current capture window from the perQ data of the current message Q */
471     hwndCapture = PERQDATA_GetCaptureWnd( pCurMsgQ->pQData );
472
473     QUEUE_Unlock( pCurMsgQ );
474     return hwndCapture;
475 }
476
477 /**********************************************************************
478  *           GetKeyState      (USER.106)
479  */
480 INT16 WINAPI GetKeyState16(INT16 vkey)
481 {
482     return GetKeyState(vkey);
483 }
484
485 /**********************************************************************
486  *           GetKeyState      (USER32.249)
487  *
488  * An application calls the GetKeyState function in response to a
489  * keyboard-input message.  This function retrieves the state of the key
490  * at the time the input message was generated.  (SDK 3.1 Vol 2. p 390)
491  */
492 SHORT WINAPI GetKeyState(INT vkey)
493 {
494     INT retval;
495
496     switch (vkey)
497         {
498         case VK_LBUTTON : /* VK_LBUTTON is 1 */
499             retval = MouseButtonsStates[0] ? 0x8000 : 0;
500             break;
501         case VK_MBUTTON : /* VK_MBUTTON is 4 */
502             retval = MouseButtonsStates[1] ? 0x8000 : 0;
503             break;
504         case VK_RBUTTON : /* VK_RBUTTON is 2 */
505             retval = MouseButtonsStates[2] ? 0x8000 : 0;
506             break;
507         default :
508             if (vkey >= 'a' && vkey <= 'z')
509                 vkey += 'A' - 'a';
510             retval = ( (WORD)(QueueKeyStateTable[vkey] & 0x80) << 8 ) |
511                        (WORD)(QueueKeyStateTable[vkey] & 0x01);
512         }
513     /* TRACE(key, "(0x%x) -> %x\n", vkey, retval); */
514     return retval;
515 }
516
517 /**********************************************************************
518  *           GetKeyboardState      (USER.222)(USER32.254)
519  *
520  * An application calls the GetKeyboardState function in response to a
521  * keyboard-input message.  This function retrieves the state of the keyboard
522  * at the time the input message was generated.  (SDK 3.1 Vol 2. p 387)
523  */
524 BOOL WINAPI GetKeyboardState(LPBYTE lpKeyState)
525 {
526     TRACE_(key)("(%p)\n", lpKeyState);
527     if (lpKeyState != NULL) {
528         QueueKeyStateTable[VK_LBUTTON] = MouseButtonsStates[0] ? 0x80 : 0;
529         QueueKeyStateTable[VK_MBUTTON] = MouseButtonsStates[1] ? 0x80 : 0;
530         QueueKeyStateTable[VK_RBUTTON] = MouseButtonsStates[2] ? 0x80 : 0;
531         memcpy(lpKeyState, QueueKeyStateTable, 256);
532     }
533
534     return TRUE;
535 }
536
537 /**********************************************************************
538  *          SetKeyboardState      (USER.223)(USER32.484)
539  */
540 BOOL WINAPI SetKeyboardState(LPBYTE lpKeyState)
541 {
542     TRACE_(key)("(%p)\n", lpKeyState);
543     if (lpKeyState != NULL) {
544         memcpy(QueueKeyStateTable, lpKeyState, 256);
545         MouseButtonsStates[0] = (QueueKeyStateTable[VK_LBUTTON] != 0);
546         MouseButtonsStates[1] = (QueueKeyStateTable[VK_MBUTTON] != 0);
547         MouseButtonsStates[2] = (QueueKeyStateTable[VK_RBUTTON] != 0);
548     }
549
550     return TRUE;
551 }
552
553 /**********************************************************************
554  *           GetAsyncKeyState      (USER32.207)
555  *
556  *      Determine if a key is or was pressed.  retval has high-order 
557  * bit set to 1 if currently pressed, low-order bit set to 1 if key has
558  * been pressed.
559  *
560  *      This uses the variable AsyncMouseButtonsStates and
561  * AsyncKeyStateTable (set in event.c) which have the mouse button
562  * number or key number (whichever is applicable) set to true if the
563  * mouse or key had been depressed since the last call to 
564  * GetAsyncKeyState.
565  */
566 WORD WINAPI GetAsyncKeyState(INT nKey)
567 {
568     short retval;       
569
570     switch (nKey) {
571      case VK_LBUTTON:
572         retval = (AsyncMouseButtonsStates[0] ? 0x0001 : 0) | 
573                  (MouseButtonsStates[0] ? 0x8000 : 0);
574         break;
575      case VK_MBUTTON:
576         retval = (AsyncMouseButtonsStates[1] ? 0x0001 : 0) | 
577                  (MouseButtonsStates[1] ? 0x8000 : 0);
578         break;
579      case VK_RBUTTON:
580         retval = (AsyncMouseButtonsStates[2] ? 0x0001 : 0) | 
581                  (MouseButtonsStates[2] ? 0x8000 : 0);
582         break;
583      default:
584         retval = AsyncKeyStateTable[nKey] | 
585                 ((InputKeyStateTable[nKey] & 0x80) ? 0x8000 : 0); 
586         break;
587     }
588
589     /* all states to false */
590     memset( AsyncMouseButtonsStates, 0, sizeof(AsyncMouseButtonsStates) );
591     memset( AsyncKeyStateTable, 0, sizeof(AsyncKeyStateTable) );
592
593     TRACE_(key)("(%x) -> %x\n", nKey, retval);
594     return retval;
595 }
596
597 /**********************************************************************
598  *            GetAsyncKeyState16        (USER.249)
599  */
600 WORD WINAPI GetAsyncKeyState16(INT16 nKey)
601 {
602     return GetAsyncKeyState(nKey);
603 }
604
605 /***********************************************************************
606  *              IsUserIdle      (USER.333)
607  */
608 BOOL16 WINAPI IsUserIdle16(void)
609 {
610     if ( GetAsyncKeyState( VK_LBUTTON ) & 0x8000 )
611         return FALSE;
612
613     if ( GetAsyncKeyState( VK_RBUTTON ) & 0x8000 )
614         return FALSE;
615
616     if ( GetAsyncKeyState( VK_MBUTTON ) & 0x8000 )
617         return FALSE;
618
619     /* Should check for screen saver activation here ... */
620
621     return TRUE;
622 }
623
624 /**********************************************************************
625  *           KBD_translate_accelerator
626  *
627  * FIXME: should send some WM_INITMENU or/and WM_INITMENUPOPUP  -messages
628  */
629 static BOOL KBD_translate_accelerator(HWND hWnd,LPMSG msg,
630                                         BYTE fVirt,WORD key,WORD cmd)
631 {
632     BOOL        sendmsg = FALSE;
633
634     if(msg->wParam == key) 
635     {
636         if (msg->message == WM_CHAR) {
637         if ( !(fVirt & FALT) && !(fVirt & FVIRTKEY) )
638         {
639           TRACE_(accel)("found accel for WM_CHAR: ('%c')\n",
640                         msg->wParam&0xff);
641           sendmsg=TRUE;
642         }  
643       } else {
644        if(fVirt & FVIRTKEY) {
645         INT mask = 0;
646         TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
647                                msg->wParam,0xff & HIWORD(msg->lParam));                
648         if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
649         if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
650         if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
651         if(mask == (fVirt & (FSHIFT | FCONTROL | FALT)))
652           sendmsg=TRUE;                     
653         else
654           TRACE_(accel)(", but incorrect SHIFT/CTRL/ALT-state\n");
655        }
656        else
657        {
658          if (!(msg->lParam & 0x01000000))  /* no special_key */
659          {
660            if ((fVirt & FALT) && (msg->lParam & 0x20000000))
661            {                                                   /* ^^ ALT pressed */
662             TRACE_(accel)("found accel for Alt-%c\n", msg->wParam&0xff);
663             sendmsg=TRUE;           
664            } 
665          } 
666        }
667       } 
668
669       if (sendmsg)      /* found an accelerator, but send a message... ? */
670       {
671         INT16  iSysStat,iStat,mesg=0;
672         HMENU16 hMenu;
673         
674         if (msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP)
675           mesg=1;
676         else 
677          if (GetCapture())
678            mesg=2;
679          else
680           if (!IsWindowEnabled(hWnd))
681             mesg=3;
682           else
683           {
684             WND* wndPtr = WIN_FindWndPtr(hWnd);
685
686             hMenu = (wndPtr->dwStyle & WS_CHILD) ? 0 : (HMENU)wndPtr->wIDmenu;
687             iSysStat = (wndPtr->hSysMenu) ? GetMenuState(GetSubMenu16(wndPtr->hSysMenu, 0),
688                                             cmd, MF_BYCOMMAND) : -1 ;
689             iStat = (hMenu) ? GetMenuState(hMenu,
690                                             cmd, MF_BYCOMMAND) : -1 ;
691
692             WIN_ReleaseWndPtr(wndPtr);
693             
694             if (iSysStat!=-1)
695             {
696               if (iSysStat & (MF_DISABLED|MF_GRAYED))
697                 mesg=4;
698               else
699                 mesg=WM_SYSCOMMAND;
700             }
701             else
702             {
703               if (iStat!=-1)
704               {
705                 if (IsIconic(hWnd))
706                   mesg=5;
707                 else
708                 {
709                  if (iStat & (MF_DISABLED|MF_GRAYED))
710                    mesg=6;
711                  else
712                    mesg=WM_COMMAND;  
713                 }   
714               }
715               else
716                mesg=WM_COMMAND;  
717             }
718           }
719           if( mesg==WM_COMMAND ) {
720               TRACE_(accel)(", sending WM_COMMAND, wParam=%0x\n", 0x10000 | cmd);
721               SendMessageA(hWnd, mesg, 0x10000 | cmd, 0L);
722           } else if( mesg==WM_SYSCOMMAND ) {
723               TRACE_(accel)(", sending WM_SYSCOMMAND, wParam=%0x\n", cmd);
724               SendMessageA(hWnd, mesg, cmd, 0x00010000L);
725           }
726           else
727           {
728            /*  some reasons for NOT sending the WM_{SYS}COMMAND message: 
729             *   #0: unknown (please report!)
730             *   #1: for WM_KEYUP,WM_SYSKEYUP
731             *   #2: mouse is captured
732             *   #3: window is disabled 
733             *   #4: it's a disabled system menu option
734             *   #5: it's a menu option, but window is iconic
735             *   #6: it's a menu option, but disabled
736             */
737             TRACE_(accel)(", but won't send WM_{SYS}COMMAND, reason is #%d\n",mesg);
738             if(mesg==0)
739               ERR_(accel)(" unknown reason - please report!");
740           }          
741           return TRUE;         
742       }
743     }
744     return FALSE;
745 }
746
747 /**********************************************************************
748  *      TranslateAccelerator      (USER32.551)(USER32.552)(USER32.553)
749  */
750 INT WINAPI TranslateAccelerator(HWND hWnd, HACCEL hAccel, LPMSG msg)
751 {
752     /* YES, Accel16! */
753     LPACCEL16   lpAccelTbl;
754     int         i;
755
756     if (msg == NULL)
757        {
758           WARN_(accel)("msg null; should hang here to be win compatible\n");
759           return 0;
760        }
761     if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(hAccel)))
762        {
763           WARN_(accel)("invalid accel handle=%x\n", hAccel);
764           return 0;
765        }
766     if ((msg->message != WM_KEYDOWN &&
767          msg->message != WM_KEYUP &&
768          msg->message != WM_SYSKEYDOWN &&
769          msg->message != WM_SYSKEYUP &&
770          msg->message != WM_CHAR)) return 0;
771
772     TRACE_(accel)("TranslateAccelerators hAccel=%04x, hWnd=%04x,"
773           "msg->hwnd=%04x, msg->message=%04x, wParam=%08x, lParam=%lx\n",
774           hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
775
776     i = 0;
777     do
778     {
779         if (KBD_translate_accelerator(hWnd,msg,lpAccelTbl[i].fVirt,
780                                       lpAccelTbl[i].key,lpAccelTbl[i].cmd))
781                 return 1;
782     } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);
783     WARN_(accel)("couldn't translate accelerator key\n");
784     return 0;
785 }
786
787 /**********************************************************************
788  *           TranslateAccelerator16      (USER.178)
789  */     
790 INT16 WINAPI TranslateAccelerator16(HWND16 hWnd, HACCEL16 hAccel, LPMSG16 msg)
791 {
792     LPACCEL16   lpAccelTbl;
793     int         i;
794     MSG msg32;
795     
796     if (msg == NULL)
797        {
798           WARN_(accel)("msg null; should hang here to be win compatible\n");
799           return 0;
800        }
801     if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(hAccel)))
802        {
803           WARN_(accel)("invalid accel handle=%x\n", hAccel);
804           return 0;
805        }
806     if ((msg->message != WM_KEYDOWN &&
807          msg->message != WM_KEYUP &&
808          msg->message != WM_SYSKEYDOWN &&
809          msg->message != WM_SYSKEYUP &&
810          msg->message != WM_CHAR)) return 0;
811
812     TRACE_(accel)("TranslateAccelerators hAccel=%04x, hWnd=%04x,\
813 msg->hwnd=%04x, msg->message=%04x, wParam=%04x, lParam=%lx\n", hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
814
815     STRUCT32_MSG16to32(msg,&msg32);
816
817     i = 0;
818     do
819     {
820         if (KBD_translate_accelerator(hWnd,&msg32,lpAccelTbl[i].fVirt,
821                                       lpAccelTbl[i].key,lpAccelTbl[i].cmd))
822                 return 1;
823     } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);
824     WARN_(accel)("couldn't translate accelerator key\n");
825     return 0;
826 }
827
828
829 /**********************************************************************
830  *           VkKeyScanA      (USER32.573)
831  */
832 WORD WINAPI VkKeyScanA(CHAR cChar)
833 {
834         return VkKeyScan16(cChar);
835 }
836
837 /******************************************************************************
838  *      VkKeyScanW      (USER32.576)
839  */
840 WORD WINAPI VkKeyScanW(WCHAR cChar)
841 {
842         return VkKeyScanA((CHAR)cChar); /* FIXME: check unicode */
843 }
844
845 /**********************************************************************
846  *           VkKeyScanExA      (USER32.574)
847  */
848 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
849 {
850                                 /* FIXME: complete workaround this is */
851                                 return VkKeyScan16(cChar);
852 }
853
854 /******************************************************************************
855  *      VkKeyScanExW      (USER32.575)
856  */
857 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
858 {
859                                 /* FIXME: complete workaround this is */
860                                 return VkKeyScanA((CHAR)cChar); /* FIXME: check unicode */
861 }
862  
863 /******************************************************************************
864  *      GetKeyboardType      (USER32.255)
865  */
866 INT WINAPI GetKeyboardType(INT nTypeFlag)
867 {
868   return GetKeyboardType16(nTypeFlag);
869 }
870
871 /******************************************************************************
872  *      MapVirtualKeyA      (USER32.383)
873  */
874 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
875 {
876     return MapVirtualKey16(code,maptype);
877 }
878
879 /******************************************************************************
880  *      MapVirtualKeyW      (USER32.385)
881  */
882 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
883 {
884     return MapVirtualKey16(code,maptype);
885 }
886
887 /******************************************************************************
888  *      MapVirtualKeyExA      (USER32.384)
889  */
890 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
891 {
892     if (hkl)
893         FIXME_(keyboard)("(%d,%d,0x%08lx), hkl unhandled!\n",code,maptype,(DWORD)hkl);
894     return MapVirtualKey16(code,maptype);
895 }
896
897 /****************************************************************************
898  *      GetKBCodePage   (USER32.246)
899  */
900 UINT WINAPI GetKBCodePage(void)
901 {
902     return GetKBCodePage16();
903 }
904
905 /****************************************************************************
906  *      GetKeyboardLayoutName16   (USER.477)
907  */
908 INT16 WINAPI GetKeyboardLayoutName16(LPSTR pwszKLID)
909 {
910         return GetKeyboardLayoutNameA(pwszKLID);
911 }
912
913 /***********************************************************************
914  *           GetKeyboardLayout                  (USER32.250)
915  *
916  * FIXME: - device handle for keyboard layout defaulted to 
917  *          the language id. This is the way Windows default works.
918  *        - the thread identifier (dwLayout) is also ignored.
919  */
920 HKL WINAPI GetKeyboardLayout(DWORD dwLayout)
921 {
922         HKL layout;
923         layout = GetSystemDefaultLCID(); /* FIXME */
924         layout |= (layout<<16);          /* FIXME */
925         TRACE_(keyboard)("returning %08x\n",layout);
926         return layout;
927 }
928
929 /****************************************************************************
930  *      GetKeyboardLayoutNameA   (USER32.252)
931  */
932 INT WINAPI GetKeyboardLayoutNameA(LPSTR pwszKLID)
933 {
934         sprintf(pwszKLID, "%08x",GetKeyboardLayout(0));
935         return 1;
936 }
937
938 /****************************************************************************
939  *      GetKeyboardLayoutNameW   (USER32.253)
940  */
941 INT WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
942 {
943         char buf[9];
944         int res = GetKeyboardLayoutNameA(buf);
945         lstrcpyAtoW(pwszKLID,buf);
946         return res;
947 }
948
949 /****************************************************************************
950  *      GetKeyNameTextA   (USER32.247)
951  */
952 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
953 {
954         return GetKeyNameText16(lParam,lpBuffer,nSize);
955 }
956
957 /****************************************************************************
958  *      GetKeyNameTextW   (USER32.248)
959  */
960 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
961 {
962         int res;
963         LPSTR buf = HeapAlloc( GetProcessHeap(), 0, nSize );
964         if(buf == NULL) return 0; /* FIXME: is this the correct failure value?*/
965         res = GetKeyNameTextA(lParam,buf,nSize);
966
967         lstrcpynAtoW(lpBuffer,buf,nSize);
968         HeapFree( GetProcessHeap(), 0, buf );
969         return res;
970 }
971
972 /****************************************************************************
973  *      ToAscii      (USER32.546)
974  */
975 INT WINAPI ToAscii( UINT virtKey,UINT scanCode,LPBYTE lpKeyState,
976                         LPWORD lpChar,UINT flags )
977 {
978     return ToAscii16(virtKey,scanCode,lpKeyState,lpChar,flags);
979 }
980
981 /****************************************************************************
982  *      ToAsciiEx      (USER32.547)
983  */
984 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
985                       LPWORD lpChar, UINT flags, HKL dwhkl )
986 {
987                 /* FIXME: need true implementation */
988     return ToAscii16(virtKey,scanCode,lpKeyState,lpChar,flags);
989 }
990
991 /**********************************************************************
992  *           ActivateKeyboardLayout      (USER32.1)
993  *
994  * Call ignored. WINE supports only system default keyboard layout.
995  */
996 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
997 {
998     TRACE_(keyboard)("(%d, %d)\n", hLayout, flags);
999     ERR_(keyboard)("Only default system keyboard layout supported. Call ignored.\n");
1000     return 0;
1001 }
1002
1003
1004 /***********************************************************************
1005  *           GetKeyboardLayoutList              (USER32.251)
1006  *
1007  * FIXME: Supports only the system default language and layout and 
1008  *          returns only 1 value.
1009  *
1010  * Return number of values available if either input parm is 
1011  *  0, per MS documentation.
1012  *
1013  */
1014 INT WINAPI GetKeyboardLayoutList(INT nBuff,HKL *layouts)
1015 {
1016         TRACE_(keyboard)("(%d,%p)\n",nBuff,layouts);
1017         if (!nBuff || !layouts)
1018             return 1;
1019         if (layouts)
1020                 layouts[0] = GetKeyboardLayout(0);
1021         return 1;
1022 }
1023
1024
1025 /***********************************************************************
1026  *           RegisterHotKey                     (USER32.433)
1027  */
1028 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk) {
1029         FIXME_(keyboard)("(0x%08x,%d,0x%08x,%d): stub\n",hwnd,id,modifiers,vk);
1030         return TRUE;
1031 }
1032
1033 /***********************************************************************
1034  *           UnregisterHotKey                   (USER32.565)
1035  */
1036 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id) {
1037         FIXME_(keyboard)("(0x%08x,%d): stub\n",hwnd,id);
1038         return TRUE;
1039 }
1040
1041
1042 /***********************************************************************
1043  *           ToUnicode                       (USER32.548)
1044  */
1045 INT WINAPI ToUnicode(
1046   UINT wVirtKey,
1047   UINT wScanCode,
1048   PBYTE  lpKeyState,
1049   LPWSTR pwszBuff,
1050   INT    cchBuff,
1051   UINT wFlags) {
1052
1053        FIXME_(keyboard)(": stub\n");
1054        return 0;
1055 }
1056
1057 /***********************************************************************
1058  *           LoadKeyboardLayoutA                (USER32.367)
1059  * Call ignored. WINE supports only system default keyboard layout.
1060  */
1061 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
1062 {
1063     TRACE_(keyboard)("(%s, %d)\n", pwszKLID, Flags);
1064     ERR_(keyboard)("Only default system keyboard layout supported. Call ignored.\n");
1065   return 0; 
1066 }
1067
1068 /***********************************************************************
1069  *           LoadKeyboardLayoutW                (USER32.368)
1070  */
1071 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
1072 {
1073     char buf[9];
1074     
1075     lstrcpynWtoA(buf,pwszKLID,8);
1076     buf[8] = 0;
1077     return LoadKeyboardLayoutA(buf, Flags);
1078 }