Removed #include of wingdi.h and windef.h from winuser.h (and resolved
[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 "module.h"
31 #include "debugtools.h"
32 #include "struct32.h"
33 #include "winerror.h"
34 #include "task.h"
35
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);
41
42 static BOOL InputEnabled = TRUE;
43 BOOL SwappedButtons = FALSE;
44
45 BOOL MouseButtonsStates[3];
46 BOOL AsyncMouseButtonsStates[3];
47 BYTE InputKeyStateTable[256];
48 BYTE QueueKeyStateTable[256];
49 BYTE AsyncKeyStateTable[256];
50
51 /* Storage for the USER-maintained mouse positions */
52 DWORD PosX, PosY;
53
54 typedef union
55 {
56     struct
57     {
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;
69
70 /***********************************************************************
71  *           keybd_event   (USER32.583)
72  */
73 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
74                          DWORD dwFlags, DWORD dwExtraInfo )
75 {
76     DWORD time, extra;
77     WORD message;
78     KEYLP keylp;
79     keylp.lp2 = 0;
80
81     if (!InputEnabled) return;
82
83     /*
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 ...).
88      */
89     if (   !IsBadReadPtr( (LPVOID)dwExtraInfo, sizeof(WINE_KEYBDEVENT) )
90         && ((WINE_KEYBDEVENT *)dwExtraInfo)->magic == WINE_KEYBDEVENT_MAGIC )
91     {
92         WINE_KEYBDEVENT *wke = (WINE_KEYBDEVENT *)dwExtraInfo;
93         time = wke->time;
94         extra = 0;
95     }
96     else
97     {
98         time = GetTickCount();
99         extra = dwExtraInfo;
100     }
101
102
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*/
110
111     if ( dwFlags & KEYEVENTF_KEYUP )
112     {
113         BOOL sysKey = (InputKeyStateTable[VK_MENU] & 0x80)
114                 && !(InputKeyStateTable[VK_CONTROL] & 0x80)
115                 && !(dwFlags & KEYEVENTF_WINE_FORCEEXTENDED); /* for Alt from AltGr */
116
117         InputKeyStateTable[bVk] &= ~0x80;
118         keylp.lp1.previous = 1;
119         keylp.lp1.transition = 1;
120         message = sysKey ? WM_SYSKEYUP : WM_KEYUP;
121     }
122     else
123     {
124         keylp.lp1.previous = (InputKeyStateTable[bVk] & 0x80) != 0;
125         keylp.lp1.transition = 0;
126
127         if (!(InputKeyStateTable[bVk] & 0x80))
128             InputKeyStateTable[bVk] ^= 0x01;
129         InputKeyStateTable[bVk] |= 0x80;
130
131         message = (InputKeyStateTable[VK_MENU] & 0x80)
132               && !(InputKeyStateTable[VK_CONTROL] & 0x80)
133               ? WM_SYSKEYDOWN : WM_KEYDOWN;
134     }
135
136     if ( message == WM_SYSKEYDOWN || message == WM_SYSKEYUP )
137         keylp.lp1.context = (InputKeyStateTable[VK_MENU] & 0x80) != 0; /* 1 if alt */
138
139
140     TRACE_(key)("            wParam=%04X, lParam=%08lX\n", bVk, keylp.lp2 );
141     TRACE_(key)("            InputKeyState=%X\n", InputKeyStateTable[bVk] );
142
143     hardware_event( message, bVk, keylp.lp2, PosX, PosY, time, extra );
144 }
145
146 /***********************************************************************
147  *           WIN16_keybd_event   (USER.289)
148  */
149 void WINAPI WIN16_keybd_event( CONTEXT86 *context )
150 {
151     DWORD dwFlags = 0;
152
153     if (AH_reg(context) & 0x80) dwFlags |= KEYEVENTF_KEYUP;
154     if (BH_reg(context) & 1   ) dwFlags |= KEYEVENTF_EXTENDEDKEY;
155
156     keybd_event( AL_reg(context), BL_reg(context),
157                  dwFlags, MAKELONG(SI_reg(context), DI_reg(context)) );
158 }
159
160 /***********************************************************************
161  *           mouse_event   (USER32.584)
162  */
163 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
164                          DWORD cButtons, DWORD dwExtraInfo )
165 {
166     DWORD time, extra;
167     DWORD keyState;
168     
169     if (!InputEnabled) return;
170
171     if ( dwFlags & MOUSEEVENTF_MOVE )
172       {
173         if ( dwFlags & MOUSEEVENTF_ABSOLUTE )
174           {
175             PosX = (dx * GetSystemMetrics(SM_CXSCREEN)) >> 16;
176             PosY = (dy * GetSystemMetrics(SM_CYSCREEN)) >> 16;
177           }
178         else
179           {
180             int width  = GetSystemMetrics(SM_CXSCREEN);
181             int height = GetSystemMetrics(SM_CYSCREEN);
182             long posX = (long) PosX, posY = (long) PosY;
183
184             /* dx and dy can be negative numbers for relative movements */
185             posX += (long) dx;
186             posY += (long) dy;
187
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;
192
193             if (posY < 0) PosY = 0;
194             else if (posY >= height) PosY = height - 1;
195             else PosY = posY;
196           }
197       }
198
199     /*
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 ...).
204      */
205     if (   !IsBadReadPtr( (LPVOID)dwExtraInfo, sizeof(WINE_MOUSEEVENT) )
206         && ((WINE_MOUSEEVENT *)dwExtraInfo)->magic == WINE_MOUSEEVENT_MAGIC )
207     {
208         WINE_MOUSEEVENT *wme = (WINE_MOUSEEVENT *)dwExtraInfo;
209         time = wme->time;
210         extra = (DWORD)wme->hWnd;
211         keyState = wme->keyState;
212         
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);
220         }
221     }
222     else
223     {
224         time = GetTickCount();
225         extra = dwExtraInfo;
226         keyState = GET_KEYSTATE();
227         
228         if ( dwFlags & MOUSEEVENTF_MOVE )
229           {
230             /* We have to actually move the cursor */
231             SetCursorPos( PosX, PosY );
232           }
233     }
234
235
236     if ( dwFlags & MOUSEEVENTF_MOVE )
237     {
238         hardware_event( WM_MOUSEMOVE,
239                         keyState, 0L, PosX, PosY, time, extra );
240     }
241     if ( dwFlags & (!SwappedButtons? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_RIGHTDOWN) )
242     {
243         MouseButtonsStates[0] = AsyncMouseButtonsStates[0] = TRUE;
244         hardware_event( WM_LBUTTONDOWN,
245                         keyState, 0L, PosX, PosY, time, extra );
246     }
247     if ( dwFlags & (!SwappedButtons? MOUSEEVENTF_LEFTUP : MOUSEEVENTF_RIGHTUP) )
248     {
249         MouseButtonsStates[0] = FALSE;
250         hardware_event( WM_LBUTTONUP,
251                         keyState, 0L, PosX, PosY, time, extra );
252     }
253     if ( dwFlags & (!SwappedButtons? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_LEFTDOWN) )
254     {
255         MouseButtonsStates[2] = AsyncMouseButtonsStates[2] = TRUE;
256         hardware_event( WM_RBUTTONDOWN,
257                         keyState, 0L, PosX, PosY, time, extra );
258     }
259     if ( dwFlags & (!SwappedButtons? MOUSEEVENTF_RIGHTUP : MOUSEEVENTF_LEFTUP) )
260     {
261         MouseButtonsStates[2] = FALSE;
262         hardware_event( WM_RBUTTONUP,
263                         keyState, 0L, PosX, PosY, time, extra );
264     }
265     if ( dwFlags & MOUSEEVENTF_MIDDLEDOWN )
266     {
267         MouseButtonsStates[1] = AsyncMouseButtonsStates[1] = TRUE;
268         hardware_event( WM_MBUTTONDOWN,
269                         keyState, 0L, PosX, PosY, time, extra );
270     }
271     if ( dwFlags & MOUSEEVENTF_MIDDLEUP )
272     {
273         MouseButtonsStates[1] = FALSE;
274         hardware_event( WM_MBUTTONUP,
275                         keyState, 0L, PosX, PosY, time, extra );
276     }
277 }
278
279 /***********************************************************************
280  *           WIN16_mouse_event   (USER.299)
281  */
282 void WINAPI WIN16_mouse_event( CONTEXT86 *context )
283 {
284     mouse_event( AX_reg(context), BX_reg(context), CX_reg(context),
285                  DX_reg(context), MAKELONG(SI_reg(context), DI_reg(context)) );
286 }
287
288 /***********************************************************************
289  *           GetMouseEventProc   (USER.337)
290  */
291 FARPROC16 WINAPI GetMouseEventProc16(void)
292 {
293     HMODULE16 hmodule = GetModuleHandle16("USER");
294     return NE_GetEntryPoint( hmodule, NE_GetOrdinal( hmodule, "mouse_event" ));
295 }
296
297
298 /**********************************************************************
299  *                      EnableHardwareInput   (USER.331)
300  */
301 BOOL16 WINAPI EnableHardwareInput16(BOOL16 bEnable)
302 {
303   BOOL16 bOldState = InputEnabled;
304   FIXME_(event)("(%d) - stub\n", bEnable);
305   InputEnabled = bEnable;
306   return bOldState;
307 }
308
309
310 /***********************************************************************
311  *           SwapMouseButton16   (USER.186)
312  */
313 BOOL16 WINAPI SwapMouseButton16( BOOL16 fSwap )
314 {
315     BOOL16 ret = SwappedButtons;
316     SwappedButtons = fSwap;
317     return ret;
318 }
319
320
321 /***********************************************************************
322  *           SwapMouseButton32   (USER32.537)
323  */
324 BOOL WINAPI SwapMouseButton( BOOL fSwap )
325 {
326     BOOL ret = SwappedButtons;
327     SwappedButtons = fSwap;
328     return ret;
329 }
330
331 /**********************************************************************
332  *              EVENT_Capture
333  *
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.
336  */
337 HWND EVENT_Capture(HWND hwnd, INT16 ht)
338 {
339     HWND capturePrev = 0, captureWnd = 0;
340     MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
341     WND* wndPtr = 0;
342     INT16 captureHT = 0;
343
344     /* Get the messageQ for the current thread */
345     if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
346     {
347         WARN_(win)("\tCurrent message queue not found. Exiting!\n" );
348         goto CLEANUP;
349     }
350     
351     /* Get the current capture window from the perQ data of the current message Q */
352     capturePrev = PERQDATA_GetCaptureWnd( pCurMsgQ->pQData );
353
354     if (!hwnd)
355     {
356         captureWnd = 0L;
357         captureHT = 0;
358     }
359     else
360     {
361         wndPtr = WIN_FindWndPtr( hwnd );
362         if (wndPtr)
363         {
364             TRACE_(win)("(0x%04x)\n", hwnd );
365             captureWnd   = hwnd;
366             captureHT    = ht;
367         }
368     }
369
370     /* Update the perQ capture window and send messages */
371     if( capturePrev != captureWnd )
372     {
373         if (wndPtr)
374         {
375             /* Retrieve the message queue associated with this window */
376             pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
377             if ( !pMsgQ )
378             {
379                 WARN_(win)("\tMessage queue not found. Exiting!\n" );
380                 goto CLEANUP;
381             }
382     
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.
385              */
386             if ( pCurMsgQ->pQData != pMsgQ->pQData )
387                 goto CLEANUP;
388         }
389
390         PERQDATA_SetCaptureWnd( pCurMsgQ->pQData, captureWnd );
391         PERQDATA_SetCaptureInfo( pCurMsgQ->pQData, captureHT );
392         
393         if( capturePrev )
394     {
395         WND* wndPtr = WIN_FindWndPtr( capturePrev );
396         if( wndPtr && (wndPtr->flags & WIN_ISWIN32) )
397             SendMessageA( capturePrev, WM_CAPTURECHANGED, 0L, hwnd);
398             WIN_ReleaseWndPtr(wndPtr);
399     }
400 }
401
402 CLEANUP:
403     /* Unlock the queues before returning */
404     if ( pMsgQ )
405         QUEUE_Unlock( pMsgQ );
406     if ( pCurMsgQ )
407         QUEUE_Unlock( pCurMsgQ );
408     
409     WIN_ReleaseWndPtr(wndPtr);
410     return capturePrev;
411 }
412
413
414 /**********************************************************************
415  *              SetCapture16   (USER.18)
416  */
417 HWND16 WINAPI SetCapture16( HWND16 hwnd )
418 {
419     return (HWND16)EVENT_Capture( hwnd, HTCLIENT );
420 }
421
422
423 /**********************************************************************
424  *              SetCapture32   (USER32.464)
425  */
426 HWND WINAPI SetCapture( HWND hwnd )
427 {
428     return EVENT_Capture( hwnd, HTCLIENT );
429 }
430
431
432 /**********************************************************************
433  *              ReleaseCapture   (USER.19) (USER32.439)
434  */
435 BOOL WINAPI ReleaseCapture(void)
436 {
437     return (EVENT_Capture( 0, 0 ) != 0);
438 }
439
440
441 /**********************************************************************
442  *              GetCapture16   (USER.236)
443  */
444 HWND16 WINAPI GetCapture16(void)
445 {
446     return (HWND16)GetCapture();
447 }
448
449 /**********************************************************************
450  *              GetCapture32   (USER32.208)
451  */
452 HWND WINAPI GetCapture(void)
453 {
454     MESSAGEQUEUE *pCurMsgQ = 0;
455     HWND hwndCapture = 0;
456
457     /* Get the messageQ for the current thread */
458     if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
459 {
460         TRACE_(win)("GetCapture32:  Current message queue not found. Exiting!\n" );
461         return 0;
462     }
463     
464     /* Get the current capture window from the perQ data of the current message Q */
465     hwndCapture = PERQDATA_GetCaptureWnd( pCurMsgQ->pQData );
466
467     QUEUE_Unlock( pCurMsgQ );
468     return hwndCapture;
469 }
470
471 /**********************************************************************
472  *           GetKeyState      (USER.106)
473  */
474 INT16 WINAPI GetKeyState16(INT16 vkey)
475 {
476     return GetKeyState(vkey);
477 }
478
479 /**********************************************************************
480  *           GetKeyState      (USER32.249)
481  *
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)
485  */
486 SHORT WINAPI GetKeyState(INT vkey)
487 {
488     INT retval;
489
490     switch (vkey)
491         {
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);
506         }
507     /* TRACE(key, "(0x%x) -> %x\n", vkey, retval); */
508     return retval;
509 }
510
511 /**********************************************************************
512  *           GetKeyboardState      (USER.222)(USER32.254)
513  *
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)
517  */
518 BOOL WINAPI GetKeyboardState(LPBYTE lpKeyState)
519 {
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);
526     }
527
528     return TRUE;
529 }
530
531 /**********************************************************************
532  *          SetKeyboardState      (USER.223)(USER32.484)
533  */
534 BOOL WINAPI SetKeyboardState(LPBYTE lpKeyState)
535 {
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);
542     }
543
544     return TRUE;
545 }
546
547 /**********************************************************************
548  *           GetAsyncKeyState32      (USER32.207)
549  *
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.
553  *
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.
559  */
560 WORD WINAPI GetAsyncKeyState(INT nKey)
561 {
562     short retval;       
563
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;
581     }
582
583     /* all states to false */
584     memset( AsyncMouseButtonsStates, 0, sizeof(AsyncMouseButtonsStates) );
585     memset( AsyncKeyStateTable, 0, sizeof(AsyncKeyStateTable) );
586
587     TRACE_(key)("(%x) -> %x\n", nKey, retval);
588     return retval;
589 }
590
591 /**********************************************************************
592  *            GetAsyncKeyState16        (USER.249)
593  */
594 WORD WINAPI GetAsyncKeyState16(INT16 nKey)
595 {
596     return GetAsyncKeyState(nKey);
597 }
598
599 /***********************************************************************
600  *              IsUserIdle      (USER.333)
601  */
602 BOOL16 WINAPI IsUserIdle16(void)
603 {
604     if ( GetAsyncKeyState( VK_LBUTTON ) & 0x8000 )
605         return FALSE;
606
607     if ( GetAsyncKeyState( VK_RBUTTON ) & 0x8000 )
608         return FALSE;
609
610     if ( GetAsyncKeyState( VK_MBUTTON ) & 0x8000 )
611         return FALSE;
612
613     /* Should check for screen saver activation here ... */
614
615     return TRUE;
616 }
617
618 /**********************************************************************
619  *           KBD_translate_accelerator
620  *
621  * FIXME: should send some WM_INITMENU or/and WM_INITMENUPOPUP  -messages
622  */
623 static BOOL KBD_translate_accelerator(HWND hWnd,LPMSG msg,
624                                         BYTE fVirt,WORD key,WORD cmd)
625 {
626     BOOL        sendmsg = FALSE;
627
628     if(msg->wParam == key) 
629     {
630         if (msg->message == WM_CHAR) {
631         if ( !(fVirt & FALT) && !(fVirt & FVIRTKEY) )
632         {
633           TRACE_(accel)("found accel for WM_CHAR: ('%c')\n",
634                         msg->wParam&0xff);
635           sendmsg=TRUE;
636         }  
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");
649        }
650        else
651        {
652          if (!(msg->lParam & 0x01000000))  /* no special_key */
653          {
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;           
658            } 
659          } 
660        }
661       } 
662
663       if (sendmsg)      /* found an accelerator, but send a message... ? */
664       {
665         INT16  iSysStat,iStat,mesg=0;
666         HMENU16 hMenu;
667         
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
677           {
678             WND* wndPtr = WIN_FindWndPtr(hWnd);
679
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 ;
685
686             WIN_ReleaseWndPtr(wndPtr);
687             
688             if (iSysStat!=-1)
689             {
690               if (iSysStat & (MF_DISABLED|MF_GRAYED))
691                 mesg=4;
692               else
693                 mesg=WM_SYSCOMMAND;
694             }
695             else
696             {
697               if (iStat!=-1)
698               {
699                 if (IsIconic(hWnd))
700                   mesg=5;
701                 else
702                 {
703                  if (iStat & (MF_DISABLED|MF_GRAYED))
704                    mesg=6;
705                  else
706                    mesg=WM_COMMAND;  
707                 }   
708               }
709               else
710                mesg=WM_COMMAND;  
711             }
712           }
713           if ( mesg==WM_COMMAND || mesg==WM_SYSCOMMAND )
714           {
715               TRACE_(accel)(", sending %s, wParam=%0x\n",
716                   mesg==WM_COMMAND ? "WM_COMMAND" : "WM_SYSCOMMAND",
717                   cmd);
718               SendMessageA(hWnd, mesg, cmd, 0x00010000L);
719           }
720           else
721           {
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
730             */
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!");
734           }          
735           return TRUE;         
736       }
737     }
738     return FALSE;
739 }
740
741 /**********************************************************************
742  *      TranslateAccelerator32      (USER32.551)(USER32.552)(USER32.553)
743  */
744 INT WINAPI TranslateAccelerator(HWND hWnd, HACCEL hAccel, LPMSG msg)
745 {
746     /* YES, Accel16! */
747     LPACCEL16   lpAccelTbl;
748     int         i;
749
750     if (msg == NULL)
751        {
752           WARN_(accel)("msg null; should hang here to be win compatible\n");
753           return 0;
754        }
755     if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(hAccel)))
756        {
757           WARN_(accel)("invalid accel handle=%x\n", hAccel);
758           return 0;
759        }
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;
765
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);
769
770     i = 0;
771     do
772     {
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;
779 }
780
781 /**********************************************************************
782  *           TranslateAccelerator16      (USER.178)
783  */     
784 INT16 WINAPI TranslateAccelerator16(HWND16 hWnd, HACCEL16 hAccel, LPMSG16 msg)
785 {
786     LPACCEL16   lpAccelTbl;
787     int         i;
788     MSG msg32;
789     
790     if (msg == NULL)
791        {
792           WARN_(accel)("msg null; should hang here to be win compatible\n");
793           return 0;
794        }
795     if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(hAccel)))
796        {
797           WARN_(accel)("invalid accel handle=%x\n", hAccel);
798           return 0;
799        }
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;
805
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);
808
809     STRUCT32_MSG16to32(msg,&msg32);
810
811     i = 0;
812     do
813     {
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;
820 }
821
822
823 /**********************************************************************
824  *           VkKeyScanA      (USER32.573)
825  */
826 WORD WINAPI VkKeyScanA(CHAR cChar)
827 {
828         return VkKeyScan16(cChar);
829 }
830
831 /******************************************************************************
832  *      VkKeyScanW      (USER32.576)
833  */
834 WORD WINAPI VkKeyScanW(WCHAR cChar)
835 {
836         return VkKeyScanA((CHAR)cChar); /* FIXME: check unicode */
837 }
838
839 /**********************************************************************
840  *           VkKeyScanExA      (USER32.574)
841  */
842 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
843 {
844                                 /* FIXME: complete workaround this is */
845                                 return VkKeyScan16(cChar);
846 }
847
848 /******************************************************************************
849  *      VkKeyScanExW      (USER32.575)
850  */
851 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
852 {
853                                 /* FIXME: complete workaround this is */
854                                 return VkKeyScanA((CHAR)cChar); /* FIXME: check unicode */
855 }
856  
857 /******************************************************************************
858  *      GetKeyboardType32      (USER32.255)
859  */
860 INT WINAPI GetKeyboardType(INT nTypeFlag)
861 {
862   return GetKeyboardType16(nTypeFlag);
863 }
864
865 /******************************************************************************
866  *      MapVirtualKey32A      (USER32.383)
867  */
868 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
869 {
870     return MapVirtualKey16(code,maptype);
871 }
872
873 /******************************************************************************
874  *      MapVirtualKey32W      (USER32.385)
875  */
876 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
877 {
878     return MapVirtualKey16(code,maptype);
879 }
880
881 /******************************************************************************
882  *      MapVirtualKeyEx32A      (USER32.384)
883  */
884 UINT WINAPI MapVirtualKeyEx32A(UINT code, UINT maptype, HKL hkl)
885 {
886     if (hkl)
887         FIXME_(keyboard)("(%d,%d,0x%08lx), hkl unhandled!\n",code,maptype,(DWORD)hkl);
888     return MapVirtualKey16(code,maptype);
889 }
890
891 /****************************************************************************
892  *      GetKBCodePage32   (USER32.246)
893  */
894 UINT WINAPI GetKBCodePage(void)
895 {
896     return GetKBCodePage16();
897 }
898
899 /****************************************************************************
900  *      GetKeyboardLayoutName16   (USER.477)
901  */
902 INT16 WINAPI GetKeyboardLayoutName16(LPSTR pwszKLID)
903 {
904         return GetKeyboardLayoutNameA(pwszKLID);
905 }
906
907 /***********************************************************************
908  *           GetKeyboardLayout                  (USER32.250)
909  *
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.
913  */
914 HKL WINAPI GetKeyboardLayout(DWORD dwLayout)
915 {
916         HKL layout;
917         layout = GetSystemDefaultLCID(); /* FIXME */
918         layout |= (layout<<16);          /* FIXME */
919         TRACE_(keyboard)("returning %08x\n",layout);
920         return layout;
921 }
922
923 /****************************************************************************
924  *      GetKeyboardLayoutName32A   (USER32.252)
925  */
926 INT WINAPI GetKeyboardLayoutNameA(LPSTR pwszKLID)
927 {
928         sprintf(pwszKLID, "%08x",GetKeyboardLayout(0));
929         return 1;
930 }
931
932 /****************************************************************************
933  *      GetKeyboardLayoutName32W   (USER32.253)
934  */
935 INT WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
936 {
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;
942 }
943
944 /****************************************************************************
945  *      GetKeyNameText32A   (USER32.247)
946  */
947 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
948 {
949         return GetKeyNameText16(lParam,lpBuffer,nSize);
950 }
951
952 /****************************************************************************
953  *      GetKeyNameText32W   (USER32.248)
954  */
955 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
956 {
957         LPSTR buf = HEAP_xalloc( GetProcessHeap(), 0, nSize );
958         int res = GetKeyNameTextA(lParam,buf,nSize);
959
960         lstrcpynAtoW(lpBuffer,buf,nSize);
961         HeapFree( GetProcessHeap(), 0, buf );
962         return res;
963 }
964
965 /****************************************************************************
966  *      ToAscii32      (USER32.546)
967  */
968 INT WINAPI ToAscii( UINT virtKey,UINT scanCode,LPBYTE lpKeyState,
969                         LPWORD lpChar,UINT flags )
970 {
971     return ToAscii16(virtKey,scanCode,lpKeyState,lpChar,flags);
972 }
973
974 /****************************************************************************
975  *      ToAscii32      (USER32.547)
976  */
977 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
978                       LPWORD lpChar, UINT flags, HKL dwhkl )
979 {
980                 /* FIXME: need true implementation */
981     return ToAscii16(virtKey,scanCode,lpKeyState,lpChar,flags);
982 }
983
984 /**********************************************************************
985  *           ActivateKeyboardLayout32      (USER32.1)
986  *
987  * Call ignored. WINE supports only system default keyboard layout.
988  */
989 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
990 {
991     TRACE_(keyboard)("(%d, %d)\n", hLayout, flags);
992     ERR_(keyboard)("Only default system keyboard layout supported. Call ignored.\n");
993     return 0;
994 }
995
996
997 /***********************************************************************
998  *           GetKeyboardLayoutList              (USER32.251)
999  *
1000  * FIXME: Supports only the system default language and layout and 
1001  *          returns only 1 value.
1002  *
1003  * Return number of values available if either input parm is 
1004  *  0, per MS documentation.
1005  *
1006  */
1007 INT WINAPI GetKeyboardLayoutList(INT nBuff,HKL *layouts)
1008 {
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;
1015 }
1016
1017
1018 /***********************************************************************
1019  *           RegisterHotKey                     (USER32.433)
1020  */
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;
1024 }
1025
1026 /***********************************************************************
1027  *           UnregisterHotKey                   (USER32.565)
1028  */
1029 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id) {
1030         FIXME_(keyboard)("(0x%08x,%d): stub\n",hwnd,id);
1031         return TRUE;
1032 }
1033
1034
1035 /***********************************************************************
1036  *           ToUnicode32                       (USER32.548)
1037  */
1038 INT WINAPI ToUnicode(
1039   UINT wVirtKey,
1040   UINT wScanCode,
1041   PBYTE  lpKeyState,
1042   LPWSTR pwszBuff,
1043   INT    cchBuff,
1044   UINT wFlags) {
1045
1046        FIXME_(keyboard)(": stub\n");
1047        return 0;
1048 }
1049
1050 /***********************************************************************
1051  *           LoadKeyboardLayout32A                (USER32.367)
1052  * Call ignored. WINE supports only system default keyboard layout.
1053  */
1054 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
1055 {
1056     TRACE_(keyboard)("(%s, %d)\n", pwszKLID, Flags);
1057     ERR_(keyboard)("Only default system keyboard layout supported. Call ignored.\n");
1058   return 0; 
1059 }
1060
1061 /***********************************************************************
1062  *           LoadKeyboardLayout32W                (USER32.368)
1063  */
1064 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
1065 {
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;
1073 }