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