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