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