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