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