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