- Added/fixed some documentation reported by winapi_check
[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 }
279
280 /***********************************************************************
281  *           WIN16_mouse_event   (USER.299)
282  */
283 void WINAPI WIN16_mouse_event( CONTEXT86 *context )
284 {
285     mouse_event( AX_reg(context), BX_reg(context), CX_reg(context),
286                  DX_reg(context), MAKELONG(SI_reg(context), DI_reg(context)) );
287 }
288
289 /***********************************************************************
290  *           GetMouseEventProc   (USER.337)
291  */
292 FARPROC16 WINAPI GetMouseEventProc16(void)
293 {
294     HMODULE16 hmodule = GetModuleHandle16("USER");
295     return NE_GetEntryPoint( hmodule, NE_GetOrdinal( hmodule, "mouse_event" ));
296 }
297
298
299 /**********************************************************************
300  *                      EnableHardwareInput   (USER.331)
301  */
302 BOOL16 WINAPI EnableHardwareInput16(BOOL16 bEnable)
303 {
304   BOOL16 bOldState = InputEnabled;
305   FIXME_(event)("(%d) - stub\n", bEnable);
306   InputEnabled = bEnable;
307   return bOldState;
308 }
309
310
311 /***********************************************************************
312  *           SwapMouseButton16   (USER.186)
313  */
314 BOOL16 WINAPI SwapMouseButton16( BOOL16 fSwap )
315 {
316     BOOL16 ret = SwappedButtons;
317     SwappedButtons = fSwap;
318     return ret;
319 }
320
321
322 /***********************************************************************
323  *           SwapMouseButton   (USER32.537)
324  */
325 BOOL WINAPI SwapMouseButton( BOOL fSwap )
326 {
327     BOOL ret = SwappedButtons;
328     SwappedButtons = fSwap;
329     return ret;
330 }
331
332 /**********************************************************************
333  *              EVENT_Capture
334  *
335  * We need this to be able to generate double click messages
336  * when menu code captures mouse in the window without CS_DBLCLK style.
337  */
338 HWND EVENT_Capture(HWND hwnd, INT16 ht)
339 {
340     HWND capturePrev = 0, captureWnd = 0;
341     MESSAGEQUEUE *pMsgQ = 0, *pCurMsgQ = 0;
342     WND* wndPtr = 0;
343     INT16 captureHT = 0;
344
345     /* Get the messageQ for the current thread */
346     if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
347     {
348         WARN_(win)("\tCurrent message queue not found. Exiting!\n" );
349         goto CLEANUP;
350     }
351     
352     /* Get the current capture window from the perQ data of the current message Q */
353     capturePrev = PERQDATA_GetCaptureWnd( pCurMsgQ->pQData );
354
355     if (!hwnd)
356     {
357         captureWnd = 0L;
358         captureHT = 0;
359     }
360     else
361     {
362         wndPtr = WIN_FindWndPtr( hwnd );
363         if (wndPtr)
364         {
365             TRACE_(win)("(0x%04x)\n", hwnd );
366             captureWnd   = hwnd;
367             captureHT    = ht;
368         }
369     }
370
371     /* Update the perQ capture window and send messages */
372     if( capturePrev != captureWnd )
373     {
374         if (wndPtr)
375         {
376             /* Retrieve the message queue associated with this window */
377             pMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( wndPtr->hmemTaskQ );
378             if ( !pMsgQ )
379             {
380                 WARN_(win)("\tMessage queue not found. Exiting!\n" );
381                 goto CLEANUP;
382             }
383     
384             /* Make sure that message queue for the window we are setting capture to
385              * shares the same perQ data as the current threads message queue.
386              */
387             if ( pCurMsgQ->pQData != pMsgQ->pQData )
388                 goto CLEANUP;
389         }
390
391         PERQDATA_SetCaptureWnd( pCurMsgQ->pQData, captureWnd );
392         PERQDATA_SetCaptureInfo( pCurMsgQ->pQData, captureHT );
393         
394         if( capturePrev )
395     {
396         WND* wndPtr = WIN_FindWndPtr( capturePrev );
397         if( wndPtr && (wndPtr->flags & WIN_ISWIN32) )
398             SendMessageA( capturePrev, WM_CAPTURECHANGED, 0L, hwnd);
399             WIN_ReleaseWndPtr(wndPtr);
400     }
401 }
402
403 CLEANUP:
404     /* Unlock the queues before returning */
405     if ( pMsgQ )
406         QUEUE_Unlock( pMsgQ );
407     if ( pCurMsgQ )
408         QUEUE_Unlock( pCurMsgQ );
409     
410     WIN_ReleaseWndPtr(wndPtr);
411     return capturePrev;
412 }
413
414
415 /**********************************************************************
416  *              SetCapture16   (USER.18)
417  */
418 HWND16 WINAPI SetCapture16( HWND16 hwnd )
419 {
420     return (HWND16)EVENT_Capture( hwnd, HTCLIENT );
421 }
422
423
424 /**********************************************************************
425  *              SetCapture   (USER32.464)
426  */
427 HWND WINAPI SetCapture( HWND hwnd )
428 {
429     return EVENT_Capture( hwnd, HTCLIENT );
430 }
431
432
433 /**********************************************************************
434  *              ReleaseCapture   (USER.19) (USER32.439)
435  */
436 BOOL WINAPI ReleaseCapture(void)
437 {
438     return (EVENT_Capture( 0, 0 ) != 0);
439 }
440
441
442 /**********************************************************************
443  *              GetCapture16   (USER.236)
444  */
445 HWND16 WINAPI GetCapture16(void)
446 {
447     return (HWND16)GetCapture();
448 }
449
450 /**********************************************************************
451  *              GetCapture   (USER32.208)
452  */
453 HWND WINAPI GetCapture(void)
454 {
455     MESSAGEQUEUE *pCurMsgQ = 0;
456     HWND hwndCapture = 0;
457
458     /* Get the messageQ for the current thread */
459     if (!(pCurMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
460 {
461         TRACE_(win)("GetCapture32:  Current message queue not found. Exiting!\n" );
462         return 0;
463     }
464     
465     /* Get the current capture window from the perQ data of the current message Q */
466     hwndCapture = PERQDATA_GetCaptureWnd( pCurMsgQ->pQData );
467
468     QUEUE_Unlock( pCurMsgQ );
469     return hwndCapture;
470 }
471
472 /**********************************************************************
473  *           GetKeyState      (USER.106)
474  */
475 INT16 WINAPI GetKeyState16(INT16 vkey)
476 {
477     return GetKeyState(vkey);
478 }
479
480 /**********************************************************************
481  *           GetKeyState      (USER32.249)
482  *
483  * An application calls the GetKeyState function in response to a
484  * keyboard-input message.  This function retrieves the state of the key
485  * at the time the input message was generated.  (SDK 3.1 Vol 2. p 390)
486  */
487 SHORT WINAPI GetKeyState(INT vkey)
488 {
489     INT retval;
490
491     switch (vkey)
492         {
493         case VK_LBUTTON : /* VK_LBUTTON is 1 */
494             retval = MouseButtonsStates[0] ? 0x8000 : 0;
495             break;
496         case VK_MBUTTON : /* VK_MBUTTON is 4 */
497             retval = MouseButtonsStates[1] ? 0x8000 : 0;
498             break;
499         case VK_RBUTTON : /* VK_RBUTTON is 2 */
500             retval = MouseButtonsStates[2] ? 0x8000 : 0;
501             break;
502         default :
503             if (vkey >= 'a' && vkey <= 'z')
504                 vkey += 'A' - 'a';
505             retval = ( (WORD)(QueueKeyStateTable[vkey] & 0x80) << 8 ) |
506                        (WORD)(QueueKeyStateTable[vkey] & 0x01);
507         }
508     /* TRACE(key, "(0x%x) -> %x\n", vkey, retval); */
509     return retval;
510 }
511
512 /**********************************************************************
513  *           GetKeyboardState      (USER.222)(USER32.254)
514  *
515  * An application calls the GetKeyboardState function in response to a
516  * keyboard-input message.  This function retrieves the state of the keyboard
517  * at the time the input message was generated.  (SDK 3.1 Vol 2. p 387)
518  */
519 BOOL WINAPI GetKeyboardState(LPBYTE lpKeyState)
520 {
521     TRACE_(key)("(%p)\n", lpKeyState);
522     if (lpKeyState != NULL) {
523         QueueKeyStateTable[VK_LBUTTON] = MouseButtonsStates[0] ? 0x80 : 0;
524         QueueKeyStateTable[VK_MBUTTON] = MouseButtonsStates[1] ? 0x80 : 0;
525         QueueKeyStateTable[VK_RBUTTON] = MouseButtonsStates[2] ? 0x80 : 0;
526         memcpy(lpKeyState, QueueKeyStateTable, 256);
527     }
528
529     return TRUE;
530 }
531
532 /**********************************************************************
533  *          SetKeyboardState      (USER.223)(USER32.484)
534  */
535 BOOL WINAPI SetKeyboardState(LPBYTE lpKeyState)
536 {
537     TRACE_(key)("(%p)\n", lpKeyState);
538     if (lpKeyState != NULL) {
539         memcpy(QueueKeyStateTable, lpKeyState, 256);
540         MouseButtonsStates[0] = (QueueKeyStateTable[VK_LBUTTON] != 0);
541         MouseButtonsStates[1] = (QueueKeyStateTable[VK_MBUTTON] != 0);
542         MouseButtonsStates[2] = (QueueKeyStateTable[VK_RBUTTON] != 0);
543     }
544
545     return TRUE;
546 }
547
548 /**********************************************************************
549  *           GetAsyncKeyState      (USER32.207)
550  *
551  *      Determine if a key is or was pressed.  retval has high-order 
552  * bit set to 1 if currently pressed, low-order bit set to 1 if key has
553  * been pressed.
554  *
555  *      This uses the variable AsyncMouseButtonsStates and
556  * AsyncKeyStateTable (set in event.c) which have the mouse button
557  * number or key number (whichever is applicable) set to true if the
558  * mouse or key had been depressed since the last call to 
559  * GetAsyncKeyState.
560  */
561 WORD WINAPI GetAsyncKeyState(INT nKey)
562 {
563     short retval;       
564
565     switch (nKey) {
566      case VK_LBUTTON:
567         retval = (AsyncMouseButtonsStates[0] ? 0x0001 : 0) | 
568                  (MouseButtonsStates[0] ? 0x8000 : 0);
569         break;
570      case VK_MBUTTON:
571         retval = (AsyncMouseButtonsStates[1] ? 0x0001 : 0) | 
572                  (MouseButtonsStates[1] ? 0x8000 : 0);
573         break;
574      case VK_RBUTTON:
575         retval = (AsyncMouseButtonsStates[2] ? 0x0001 : 0) | 
576                  (MouseButtonsStates[2] ? 0x8000 : 0);
577         break;
578      default:
579         retval = AsyncKeyStateTable[nKey] | 
580                 ((InputKeyStateTable[nKey] & 0x80) ? 0x8000 : 0); 
581         break;
582     }
583
584     /* all states to false */
585     memset( AsyncMouseButtonsStates, 0, sizeof(AsyncMouseButtonsStates) );
586     memset( AsyncKeyStateTable, 0, sizeof(AsyncKeyStateTable) );
587
588     TRACE_(key)("(%x) -> %x\n", nKey, retval);
589     return retval;
590 }
591
592 /**********************************************************************
593  *            GetAsyncKeyState16        (USER.249)
594  */
595 WORD WINAPI GetAsyncKeyState16(INT16 nKey)
596 {
597     return GetAsyncKeyState(nKey);
598 }
599
600 /***********************************************************************
601  *              IsUserIdle      (USER.333)
602  */
603 BOOL16 WINAPI IsUserIdle16(void)
604 {
605     if ( GetAsyncKeyState( VK_LBUTTON ) & 0x8000 )
606         return FALSE;
607
608     if ( GetAsyncKeyState( VK_RBUTTON ) & 0x8000 )
609         return FALSE;
610
611     if ( GetAsyncKeyState( VK_MBUTTON ) & 0x8000 )
612         return FALSE;
613
614     /* Should check for screen saver activation here ... */
615
616     return TRUE;
617 }
618
619 /**********************************************************************
620  *           KBD_translate_accelerator
621  *
622  * FIXME: should send some WM_INITMENU or/and WM_INITMENUPOPUP  -messages
623  */
624 static BOOL KBD_translate_accelerator(HWND hWnd,LPMSG msg,
625                                         BYTE fVirt,WORD key,WORD cmd)
626 {
627     BOOL        sendmsg = FALSE;
628
629     if(msg->wParam == key) 
630     {
631         if (msg->message == WM_CHAR) {
632         if ( !(fVirt & FALT) && !(fVirt & FVIRTKEY) )
633         {
634           TRACE_(accel)("found accel for WM_CHAR: ('%c')\n",
635                         msg->wParam&0xff);
636           sendmsg=TRUE;
637         }  
638       } else {
639        if(fVirt & FVIRTKEY) {
640         INT mask = 0;
641         TRACE_(accel)("found accel for virt_key %04x (scan %04x)\n",
642                                msg->wParam,0xff & HIWORD(msg->lParam));                
643         if(GetKeyState(VK_SHIFT) & 0x8000) mask |= FSHIFT;
644         if(GetKeyState(VK_CONTROL) & 0x8000) mask |= FCONTROL;
645         if(GetKeyState(VK_MENU) & 0x8000) mask |= FALT;
646         if(mask == (fVirt & (FSHIFT | FCONTROL | FALT)))
647           sendmsg=TRUE;                     
648         else
649           TRACE_(accel)(", but incorrect SHIFT/CTRL/ALT-state\n");
650        }
651        else
652        {
653          if (!(msg->lParam & 0x01000000))  /* no special_key */
654          {
655            if ((fVirt & FALT) && (msg->lParam & 0x20000000))
656            {                                                   /* ^^ ALT pressed */
657             TRACE_(accel)("found accel for Alt-%c\n", msg->wParam&0xff);
658             sendmsg=TRUE;           
659            } 
660          } 
661        }
662       } 
663
664       if (sendmsg)      /* found an accelerator, but send a message... ? */
665       {
666         INT16  iSysStat,iStat,mesg=0;
667         HMENU16 hMenu;
668         
669         if (msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP)
670           mesg=1;
671         else 
672          if (GetCapture())
673            mesg=2;
674          else
675           if (!IsWindowEnabled(hWnd))
676             mesg=3;
677           else
678           {
679             WND* wndPtr = WIN_FindWndPtr(hWnd);
680
681             hMenu = (wndPtr->dwStyle & WS_CHILD) ? 0 : (HMENU)wndPtr->wIDmenu;
682             iSysStat = (wndPtr->hSysMenu) ? GetMenuState(GetSubMenu16(wndPtr->hSysMenu, 0),
683                                             cmd, MF_BYCOMMAND) : -1 ;
684             iStat = (hMenu) ? GetMenuState(hMenu,
685                                             cmd, MF_BYCOMMAND) : -1 ;
686
687             WIN_ReleaseWndPtr(wndPtr);
688             
689             if (iSysStat!=-1)
690             {
691               if (iSysStat & (MF_DISABLED|MF_GRAYED))
692                 mesg=4;
693               else
694                 mesg=WM_SYSCOMMAND;
695             }
696             else
697             {
698               if (iStat!=-1)
699               {
700                 if (IsIconic(hWnd))
701                   mesg=5;
702                 else
703                 {
704                  if (iStat & (MF_DISABLED|MF_GRAYED))
705                    mesg=6;
706                  else
707                    mesg=WM_COMMAND;  
708                 }   
709               }
710               else
711                mesg=WM_COMMAND;  
712             }
713           }
714           if( mesg==WM_COMMAND ) {
715               TRACE_(accel)(", sending WM_COMMAND, wParam=%0x\n", 0x10000 | cmd);
716               SendMessageA(hWnd, mesg, 0x10000 | cmd, 0L);
717           } else if( mesg==WM_SYSCOMMAND ) {
718               TRACE_(accel)(", sending WM_SYSCOMMAND, wParam=%0x\n", cmd);
719               SendMessageA(hWnd, mesg, cmd, 0x00010000L);
720           }
721           else
722           {
723            /*  some reasons for NOT sending the WM_{SYS}COMMAND message: 
724             *   #0: unknown (please report!)
725             *   #1: for WM_KEYUP,WM_SYSKEYUP
726             *   #2: mouse is captured
727             *   #3: window is disabled 
728             *   #4: it's a disabled system menu option
729             *   #5: it's a menu option, but window is iconic
730             *   #6: it's a menu option, but disabled
731             */
732             TRACE_(accel)(", but won't send WM_{SYS}COMMAND, reason is #%d\n",mesg);
733             if(mesg==0)
734               ERR_(accel)(" unknown reason - please report!");
735           }          
736           return TRUE;         
737       }
738     }
739     return FALSE;
740 }
741
742 /**********************************************************************
743  *      TranslateAccelerator      (USER32.551)(USER32.552)(USER32.553)
744  */
745 INT WINAPI TranslateAccelerator(HWND hWnd, HACCEL hAccel, LPMSG msg)
746 {
747     /* YES, Accel16! */
748     LPACCEL16   lpAccelTbl;
749     int         i;
750
751     if (msg == NULL)
752        {
753           WARN_(accel)("msg null; should hang here to be win compatible\n");
754           return 0;
755        }
756     if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(hAccel)))
757        {
758           WARN_(accel)("invalid accel handle=%x\n", hAccel);
759           return 0;
760        }
761     if ((msg->message != WM_KEYDOWN &&
762          msg->message != WM_KEYUP &&
763          msg->message != WM_SYSKEYDOWN &&
764          msg->message != WM_SYSKEYUP &&
765          msg->message != WM_CHAR)) return 0;
766
767     TRACE_(accel)("TranslateAccelerators hAccel=%04x, hWnd=%04x,"
768           "msg->hwnd=%04x, msg->message=%04x, wParam=%08x, lParam=%lx\n",
769           hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
770
771     i = 0;
772     do
773     {
774         if (KBD_translate_accelerator(hWnd,msg,lpAccelTbl[i].fVirt,
775                                       lpAccelTbl[i].key,lpAccelTbl[i].cmd))
776                 return 1;
777     } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);
778     WARN_(accel)("couldn't translate accelerator key\n");
779     return 0;
780 }
781
782 /**********************************************************************
783  *           TranslateAccelerator16      (USER.178)
784  */     
785 INT16 WINAPI TranslateAccelerator16(HWND16 hWnd, HACCEL16 hAccel, LPMSG16 msg)
786 {
787     LPACCEL16   lpAccelTbl;
788     int         i;
789     MSG msg32;
790     
791     if (msg == NULL)
792        {
793           WARN_(accel)("msg null; should hang here to be win compatible\n");
794           return 0;
795        }
796     if (!hAccel || !(lpAccelTbl = (LPACCEL16) LockResource16(hAccel)))
797        {
798           WARN_(accel)("invalid accel handle=%x\n", hAccel);
799           return 0;
800        }
801     if ((msg->message != WM_KEYDOWN &&
802          msg->message != WM_KEYUP &&
803          msg->message != WM_SYSKEYDOWN &&
804          msg->message != WM_SYSKEYUP &&
805          msg->message != WM_CHAR)) return 0;
806
807     TRACE_(accel)("TranslateAccelerators hAccel=%04x, hWnd=%04x,\
808 msg->hwnd=%04x, msg->message=%04x, wParam=%04x, lParam=%lx\n", hAccel,hWnd,msg->hwnd,msg->message,msg->wParam,msg->lParam);
809
810     STRUCT32_MSG16to32(msg,&msg32);
811
812     i = 0;
813     do
814     {
815         if (KBD_translate_accelerator(hWnd,&msg32,lpAccelTbl[i].fVirt,
816                                       lpAccelTbl[i].key,lpAccelTbl[i].cmd))
817                 return 1;
818     } while ((lpAccelTbl[i++].fVirt & 0x80) == 0);
819     WARN_(accel)("couldn't translate accelerator key\n");
820     return 0;
821 }
822
823
824 /**********************************************************************
825  *           VkKeyScanA      (USER32.573)
826  */
827 WORD WINAPI VkKeyScanA(CHAR cChar)
828 {
829         return VkKeyScan16(cChar);
830 }
831
832 /******************************************************************************
833  *      VkKeyScanW      (USER32.576)
834  */
835 WORD WINAPI VkKeyScanW(WCHAR cChar)
836 {
837         return VkKeyScanA((CHAR)cChar); /* FIXME: check unicode */
838 }
839
840 /**********************************************************************
841  *           VkKeyScanExA      (USER32.574)
842  */
843 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
844 {
845                                 /* FIXME: complete workaround this is */
846                                 return VkKeyScan16(cChar);
847 }
848
849 /******************************************************************************
850  *      VkKeyScanExW      (USER32.575)
851  */
852 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
853 {
854                                 /* FIXME: complete workaround this is */
855                                 return VkKeyScanA((CHAR)cChar); /* FIXME: check unicode */
856 }
857  
858 /******************************************************************************
859  *      GetKeyboardType      (USER32.255)
860  */
861 INT WINAPI GetKeyboardType(INT nTypeFlag)
862 {
863   return GetKeyboardType16(nTypeFlag);
864 }
865
866 /******************************************************************************
867  *      MapVirtualKeyA      (USER32.383)
868  */
869 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
870 {
871     return MapVirtualKey16(code,maptype);
872 }
873
874 /******************************************************************************
875  *      MapVirtualKeyW      (USER32.385)
876  */
877 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
878 {
879     return MapVirtualKey16(code,maptype);
880 }
881
882 /******************************************************************************
883  *      MapVirtualKeyExA      (USER32.384)
884  */
885 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
886 {
887     if (hkl)
888         FIXME_(keyboard)("(%d,%d,0x%08lx), hkl unhandled!\n",code,maptype,(DWORD)hkl);
889     return MapVirtualKey16(code,maptype);
890 }
891
892 /****************************************************************************
893  *      GetKBCodePage   (USER32.246)
894  */
895 UINT WINAPI GetKBCodePage(void)
896 {
897     return GetKBCodePage16();
898 }
899
900 /****************************************************************************
901  *      GetKeyboardLayoutName16   (USER.477)
902  */
903 INT16 WINAPI GetKeyboardLayoutName16(LPSTR pwszKLID)
904 {
905         return GetKeyboardLayoutNameA(pwszKLID);
906 }
907
908 /***********************************************************************
909  *           GetKeyboardLayout                  (USER32.250)
910  *
911  * FIXME: - device handle for keyboard layout defaulted to 
912  *          the language id. This is the way Windows default works.
913  *        - the thread identifier (dwLayout) is also ignored.
914  */
915 HKL WINAPI GetKeyboardLayout(DWORD dwLayout)
916 {
917         HKL layout;
918         layout = GetSystemDefaultLCID(); /* FIXME */
919         layout |= (layout<<16);          /* FIXME */
920         TRACE_(keyboard)("returning %08x\n",layout);
921         return layout;
922 }
923
924 /****************************************************************************
925  *      GetKeyboardLayoutNameA   (USER32.252)
926  */
927 INT WINAPI GetKeyboardLayoutNameA(LPSTR pwszKLID)
928 {
929         sprintf(pwszKLID, "%08x",GetKeyboardLayout(0));
930         return 1;
931 }
932
933 /****************************************************************************
934  *      GetKeyboardLayoutNameW   (USER32.253)
935  */
936 INT WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
937 {
938         LPSTR buf = HEAP_xalloc( GetProcessHeap(), 0, strlen("00000409")+1);
939         int res = GetKeyboardLayoutNameA(buf);
940         lstrcpyAtoW(pwszKLID,buf);
941         HeapFree( GetProcessHeap(), 0, buf );
942         return res;
943 }
944
945 /****************************************************************************
946  *      GetKeyNameTextA   (USER32.247)
947  */
948 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
949 {
950         return GetKeyNameText16(lParam,lpBuffer,nSize);
951 }
952
953 /****************************************************************************
954  *      GetKeyNameTextW   (USER32.248)
955  */
956 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
957 {
958         LPSTR buf = HEAP_xalloc( GetProcessHeap(), 0, nSize );
959         int res = GetKeyNameTextA(lParam,buf,nSize);
960
961         lstrcpynAtoW(lpBuffer,buf,nSize);
962         HeapFree( GetProcessHeap(), 0, buf );
963         return res;
964 }
965
966 /****************************************************************************
967  *      ToAscii      (USER32.546)
968  */
969 INT WINAPI ToAscii( UINT virtKey,UINT scanCode,LPBYTE lpKeyState,
970                         LPWORD lpChar,UINT flags )
971 {
972     return ToAscii16(virtKey,scanCode,lpKeyState,lpChar,flags);
973 }
974
975 /****************************************************************************
976  *      ToAsciiEx      (USER32.547)
977  */
978 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
979                       LPWORD lpChar, UINT flags, HKL dwhkl )
980 {
981                 /* FIXME: need true implementation */
982     return ToAscii16(virtKey,scanCode,lpKeyState,lpChar,flags);
983 }
984
985 /**********************************************************************
986  *           ActivateKeyboardLayout      (USER32.1)
987  *
988  * Call ignored. WINE supports only system default keyboard layout.
989  */
990 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
991 {
992     TRACE_(keyboard)("(%d, %d)\n", hLayout, flags);
993     ERR_(keyboard)("Only default system keyboard layout supported. Call ignored.\n");
994     return 0;
995 }
996
997
998 /***********************************************************************
999  *           GetKeyboardLayoutList              (USER32.251)
1000  *
1001  * FIXME: Supports only the system default language and layout and 
1002  *          returns only 1 value.
1003  *
1004  * Return number of values available if either input parm is 
1005  *  0, per MS documentation.
1006  *
1007  */
1008 INT WINAPI GetKeyboardLayoutList(INT nBuff,HKL *layouts)
1009 {
1010         TRACE_(keyboard)("(%d,%p)\n",nBuff,layouts);
1011         if (!nBuff || !layouts)
1012             return 1;
1013         if (layouts)
1014                 layouts[0] = GetKeyboardLayout(0);
1015         return 1;
1016 }
1017
1018
1019 /***********************************************************************
1020  *           RegisterHotKey                     (USER32.433)
1021  */
1022 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk) {
1023         FIXME_(keyboard)("(0x%08x,%d,0x%08x,%d): stub\n",hwnd,id,modifiers,vk);
1024         return TRUE;
1025 }
1026
1027 /***********************************************************************
1028  *           UnregisterHotKey                   (USER32.565)
1029  */
1030 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id) {
1031         FIXME_(keyboard)("(0x%08x,%d): stub\n",hwnd,id);
1032         return TRUE;
1033 }
1034
1035
1036 /***********************************************************************
1037  *           ToUnicode                       (USER32.548)
1038  */
1039 INT WINAPI ToUnicode(
1040   UINT wVirtKey,
1041   UINT wScanCode,
1042   PBYTE  lpKeyState,
1043   LPWSTR pwszBuff,
1044   INT    cchBuff,
1045   UINT wFlags) {
1046
1047        FIXME_(keyboard)(": stub\n");
1048        return 0;
1049 }
1050
1051 /***********************************************************************
1052  *           LoadKeyboardLayoutA                (USER32.367)
1053  * Call ignored. WINE supports only system default keyboard layout.
1054  */
1055 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
1056 {
1057     TRACE_(keyboard)("(%s, %d)\n", pwszKLID, Flags);
1058     ERR_(keyboard)("Only default system keyboard layout supported. Call ignored.\n");
1059   return 0; 
1060 }
1061
1062 /***********************************************************************
1063  *           LoadKeyboardLayoutW                (USER32.368)
1064  */
1065 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
1066 {
1067     LPSTR buf = HEAP_xalloc( GetProcessHeap(), 0, strlen("00000409")+1);
1068     int res;
1069     lstrcpynWtoA(buf,pwszKLID,8);
1070     buf[8] = 0;
1071     res = LoadKeyboardLayoutA(buf, Flags);
1072     HeapFree( GetProcessHeap(), 0, buf );
1073     return res;
1074 }