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