Fixed ShowWindowAsync.
[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  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <assert.h>
30
31 #include "windef.h"
32 #include "winnls.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "wine/winbase16.h"
37 #include "wine/winuser16.h"
38 #include "wine/server.h"
39 #include "win.h"
40 #include "input.h"
41 #include "message.h"
42 #include "queue.h"
43 #include "wine/debug.h"
44 #include "winerror.h"
45
46 WINE_DECLARE_DEBUG_CHANNEL(key);
47 WINE_DECLARE_DEBUG_CHANNEL(keyboard);
48 WINE_DECLARE_DEBUG_CHANNEL(win);
49 WINE_DEFAULT_DEBUG_CHANNEL(event);
50
51 static BOOL InputEnabled = TRUE;
52 static BOOL SwappedButtons;
53
54 BYTE InputKeyStateTable[256];
55 BYTE AsyncKeyStateTable[256];
56
57 /* Storage for the USER-maintained mouse positions */
58 static DWORD PosX, PosY;
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 /***********************************************************************
78  *           get_key_state
79  */
80 static WORD get_key_state(void)
81 {
82     WORD ret = 0;
83
84     if (SwappedButtons)
85     {
86         if (InputKeyStateTable[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
87         if (InputKeyStateTable[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
88     }
89     else
90     {
91         if (InputKeyStateTable[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
92         if (InputKeyStateTable[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
93     }
94     if (InputKeyStateTable[VK_MBUTTON] & 0x80)  ret |= MK_MBUTTON;
95     if (InputKeyStateTable[VK_SHIFT] & 0x80)    ret |= MK_SHIFT;
96     if (InputKeyStateTable[VK_CONTROL] & 0x80)  ret |= MK_CONTROL;
97     if (InputKeyStateTable[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
98     if (InputKeyStateTable[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
99     return ret;
100 }
101
102
103 /***********************************************************************
104  *           queue_raw_hardware_message
105  *
106  * Add a message to the raw hardware queue.
107  * Note: the position is relative to the desktop window.
108  */
109 static void queue_raw_hardware_message( UINT message, WPARAM wParam, LPARAM lParam,
110                                         int xPos, int yPos, DWORD time, ULONG_PTR extraInfo )
111 {
112     SERVER_START_REQ( send_message )
113     {
114         req->id     = GetCurrentThreadId();
115         req->type   = MSG_HARDWARE_RAW;
116         req->win    = 0;
117         req->msg    = message;
118         req->wparam = wParam;
119         req->lparam = lParam;
120         req->x      = xPos;
121         req->y      = yPos;
122         req->time   = time;
123         req->info   = extraInfo;
124         req->timeout = 0;
125         wine_server_call( req );
126     }
127     SERVER_END_REQ;
128 }
129
130
131 /***********************************************************************
132  *           queue_kbd_event
133  *
134  * Put a keyboard event into a thread queue
135  */
136 static void queue_kbd_event( const KEYBDINPUT *ki, UINT injected_flags )
137 {
138     UINT message;
139     KEYLP keylp;
140     KBDLLHOOKSTRUCT hook;
141
142     keylp.lp2 = 0;
143     keylp.lp1.count = 1;
144     keylp.lp1.code = ki->wScan;
145     keylp.lp1.extended = (ki->dwFlags & KEYEVENTF_EXTENDEDKEY) != 0;
146     keylp.lp1.win_internal = 0; /* this has something to do with dialogs,
147                                 * don't remember where I read it - AK */
148                                 /* it's '1' under windows, when a dialog box appears
149                                  * and you press one of the underlined keys - DF*/
150
151     if (ki->dwFlags & KEYEVENTF_KEYUP )
152     {
153         BOOL sysKey = (InputKeyStateTable[VK_MENU] & 0x80) &&
154                       !(InputKeyStateTable[VK_CONTROL] & 0x80);
155         InputKeyStateTable[ki->wVk] &= ~0x80;
156         keylp.lp1.previous = 1;
157         keylp.lp1.transition = 1;
158         message = sysKey ? WM_SYSKEYUP : WM_KEYUP;
159     }
160     else
161     {
162         keylp.lp1.previous = (InputKeyStateTable[ki->wVk] & 0x80) != 0;
163         keylp.lp1.transition = 0;
164         if (!(InputKeyStateTable[ki->wVk] & 0x80)) InputKeyStateTable[ki->wVk] ^= 0x01;
165         InputKeyStateTable[ki->wVk] |= 0x80;
166         AsyncKeyStateTable[ki->wVk] |= 0x80;
167
168         message = (InputKeyStateTable[VK_MENU] & 0x80) && !(InputKeyStateTable[VK_CONTROL] & 0x80)
169               ? WM_SYSKEYDOWN : WM_KEYDOWN;
170     }
171
172     if (message == WM_SYSKEYDOWN || message == WM_SYSKEYUP )
173         keylp.lp1.context = (InputKeyStateTable[VK_MENU] & 0x80) != 0; /* 1 if alt */
174
175     TRACE_(key)(" wParam=%04x, lParam=%08lx, InputKeyState=%x\n",
176                 ki->wVk, keylp.lp2, InputKeyStateTable[ki->wVk] );
177
178     hook.vkCode      = ki->wVk;
179     hook.scanCode    = ki->wScan;
180     hook.flags       = (keylp.lp2 >> 24) | injected_flags;
181     hook.time        = ki->time;
182     hook.dwExtraInfo = ki->dwExtraInfo;
183     if (!HOOK_CallHooks( WH_KEYBOARD_LL, HC_ACTION, message, (LPARAM)&hook, TRUE ))
184         queue_raw_hardware_message( message, ki->wVk, keylp.lp2,
185                                     PosX, PosY, ki->time, ki->dwExtraInfo );
186 }
187
188
189 /***********************************************************************
190  *           queue_raw_mouse_message
191  */
192 static void queue_raw_mouse_message( UINT message, UINT flags, INT x, INT y, const MOUSEINPUT *mi )
193 {
194     MSLLHOOKSTRUCT hook;
195
196     hook.pt.x        = x;
197     hook.pt.y        = y;
198     hook.mouseData   = MAKELONG( 0, mi->mouseData );
199     hook.flags       = flags;
200     hook.time        = mi->time;
201     hook.dwExtraInfo = mi->dwExtraInfo;
202
203     if (!HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE ))
204         queue_raw_hardware_message( message, MAKEWPARAM( get_key_state(), mi->mouseData ),
205                                     0, x, y, mi->time, mi->dwExtraInfo );
206 }
207
208
209 /***********************************************************************
210  *              queue_mouse_event
211  */
212 static void queue_mouse_event( const MOUSEINPUT *mi, UINT flags )
213 {
214     if (mi->dwFlags & MOUSEEVENTF_ABSOLUTE)
215     {
216         PosX = (mi->dx * GetSystemMetrics(SM_CXSCREEN)) >> 16;
217         PosY = (mi->dy * GetSystemMetrics(SM_CYSCREEN)) >> 16;
218     }
219     else if (mi->dwFlags & MOUSEEVENTF_MOVE)
220     {
221         int width  = GetSystemMetrics(SM_CXSCREEN);
222         int height = GetSystemMetrics(SM_CYSCREEN);
223         long posX = (long) PosX, posY = (long) PosY;
224         int accel[3];
225         int accelMult;
226
227         /* dx and dy can be negative numbers for relative movements */
228         SystemParametersInfoA(SPI_GETMOUSE, 0, accel, 0);
229
230         accelMult = 1;
231         if (mi->dx > accel[0] && accel[2] != 0)
232         {
233             accelMult = 2;
234             if ((mi->dx > accel[1]) && (accel[2] == 2))
235             {
236                 accelMult = 4;
237             }
238         }
239         posX += (long)mi->dx * accelMult;
240
241         accelMult = 1;
242         if (mi->dy > accel[0] && accel[2] != 0)
243         {
244             accelMult = 2;
245             if ((mi->dy > accel[1]) && (accel[2] == 2))
246             {
247                 accelMult = 4;
248             }
249         }
250         posY += (long)mi->dy * accelMult;
251
252         /* Clip to the current screen size */
253         if (posX < 0) PosX = 0;
254         else if (posX >= width) PosX = width - 1;
255         else PosX = posX;
256
257         if (posY < 0) PosY = 0;
258         else if (posY >= height) PosY = height - 1;
259         else PosY = posY;
260     }
261
262     if (mi->dwFlags & MOUSEEVENTF_MOVE)
263     {
264         queue_raw_mouse_message( WM_MOUSEMOVE, flags, PosX, PosY, mi );
265     }
266     if (mi->dwFlags & MOUSEEVENTF_LEFTDOWN)
267     {
268         InputKeyStateTable[VK_LBUTTON] |= 0x80;
269         AsyncKeyStateTable[VK_LBUTTON] |= 0x80;
270         queue_raw_mouse_message( SwappedButtons ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
271                                  flags, PosX, PosY, mi );
272     }
273     if (mi->dwFlags & MOUSEEVENTF_LEFTUP)
274     {
275         InputKeyStateTable[VK_LBUTTON] &= ~0x80;
276         queue_raw_mouse_message( SwappedButtons ? WM_RBUTTONUP : WM_LBUTTONUP,
277                                  flags, PosX, PosY, mi );
278     }
279     if (mi->dwFlags & MOUSEEVENTF_RIGHTDOWN)
280     {
281         InputKeyStateTable[VK_RBUTTON] |= 0x80;
282         AsyncKeyStateTable[VK_RBUTTON] |= 0x80;
283         queue_raw_mouse_message( SwappedButtons ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
284                                  flags, PosX, PosY, mi );
285     }
286     if (mi->dwFlags & MOUSEEVENTF_RIGHTUP)
287     {
288         InputKeyStateTable[VK_RBUTTON] &= ~0x80;
289         queue_raw_mouse_message( SwappedButtons ? WM_LBUTTONUP : WM_RBUTTONUP,
290                                  flags, PosX, PosY, mi );
291     }
292     if (mi->dwFlags & MOUSEEVENTF_MIDDLEDOWN)
293     {
294         InputKeyStateTable[VK_MBUTTON] |= 0x80;
295         AsyncKeyStateTable[VK_MBUTTON] |= 0x80;
296         queue_raw_mouse_message( WM_MBUTTONDOWN, flags, PosX, PosY, mi );
297     }
298     if (mi->dwFlags & MOUSEEVENTF_MIDDLEUP)
299     {
300         InputKeyStateTable[VK_MBUTTON] &= ~0x80;
301         queue_raw_mouse_message( WM_MBUTTONUP, flags, PosX, PosY, mi );
302     }
303     if (mi->dwFlags & MOUSEEVENTF_WHEEL)
304     {
305         queue_raw_mouse_message( WM_MOUSEWHEEL, flags, PosX, PosY, mi );
306     }
307     if (flags & LLMHF_INJECTED)  /* we have to actually move the cursor */
308         SetCursorPos( PosX, PosY );
309 }
310
311
312 /***********************************************************************
313  *              SendInput  (USER32.@)
314  */
315 UINT WINAPI SendInput( UINT count, LPINPUT inputs, int size )
316 {
317     UINT i;
318
319     if (!InputEnabled) return 0;
320
321     for (i = 0; i < count; i++, inputs++)
322     {
323         switch(inputs->type)
324         {
325         case INPUT_MOUSE:
326             queue_mouse_event( &inputs->u.mi, LLMHF_INJECTED );
327             break;
328         case WINE_INTERNAL_INPUT_MOUSE:
329             queue_mouse_event( &inputs->u.mi, 0 );
330             break;
331         case INPUT_KEYBOARD:
332             queue_kbd_event( &inputs->u.ki, LLKHF_INJECTED );
333             break;
334         case WINE_INTERNAL_INPUT_KEYBOARD:
335             queue_kbd_event( &inputs->u.ki, 0 );
336             break;
337         case INPUT_HARDWARE:
338             FIXME( "INPUT_HARDWARE not supported\n" );
339             break;
340         }
341     }
342     return count;
343 }
344
345
346 /***********************************************************************
347  *              keybd_event (USER32.@)
348  */
349 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
350                          DWORD dwFlags, DWORD dwExtraInfo )
351 {
352     INPUT input;
353
354     input.type = INPUT_KEYBOARD;
355     input.u.ki.wVk = bVk;
356     input.u.ki.wScan = bScan;
357     input.u.ki.dwFlags = dwFlags;
358     input.u.ki.time = GetTickCount();
359     input.u.ki.dwExtraInfo = dwExtraInfo;
360     SendInput( 1, &input, sizeof(input) );
361 }
362
363
364 /***********************************************************************
365  *              keybd_event (USER.289)
366  */
367 void WINAPI keybd_event16( CONTEXT86 *context )
368 {
369     DWORD dwFlags = 0;
370
371     if (HIBYTE(context->Eax) & 0x80) dwFlags |= KEYEVENTF_KEYUP;
372     if (HIBYTE(context->Ebx) & 0x01) dwFlags |= KEYEVENTF_EXTENDEDKEY;
373
374     keybd_event( LOBYTE(context->Eax), LOBYTE(context->Ebx),
375                  dwFlags, MAKELONG(LOWORD(context->Esi), LOWORD(context->Edi)) );
376 }
377
378
379 /***********************************************************************
380  *              mouse_event (USER32.@)
381  */
382 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
383                          DWORD dwData, DWORD dwExtraInfo )
384 {
385     INPUT input;
386
387     input.type = INPUT_MOUSE;
388     input.u.mi.dx = dx;
389     input.u.mi.dy = dy;
390     input.u.mi.mouseData = dwData;
391     input.u.mi.dwFlags = dwFlags;
392     input.u.mi.time = GetCurrentTime();
393     input.u.mi.dwExtraInfo = dwExtraInfo;
394     SendInput( 1, &input, sizeof(input) );
395 }
396
397
398 /***********************************************************************
399  *              mouse_event (USER.299)
400  */
401 void WINAPI mouse_event16( CONTEXT86 *context )
402 {
403     mouse_event( LOWORD(context->Eax), LOWORD(context->Ebx), LOWORD(context->Ecx),
404                  LOWORD(context->Edx), MAKELONG(context->Esi, context->Edi) );
405 }
406
407 /***********************************************************************
408  *              GetMouseEventProc (USER.337)
409  */
410 FARPROC16 WINAPI GetMouseEventProc16(void)
411 {
412     HMODULE16 hmodule = GetModuleHandle16("USER");
413     return GetProcAddress16( hmodule, "mouse_event" );
414 }
415
416
417 /**********************************************************************
418  *              EnableHardwareInput (USER.331)
419  */
420 BOOL16 WINAPI EnableHardwareInput16(BOOL16 bEnable)
421 {
422   BOOL16 bOldState = InputEnabled;
423   FIXME_(event)("(%d) - stub\n", bEnable);
424   InputEnabled = bEnable;
425   return bOldState;
426 }
427
428
429 /***********************************************************************
430  *              SwapMouseButton (USER.186)
431  */
432 BOOL16 WINAPI SwapMouseButton16( BOOL16 fSwap )
433 {
434     BOOL16 ret = SwappedButtons;
435     SwappedButtons = fSwap;
436     return ret;
437 }
438
439
440 /***********************************************************************
441  *              SwapMouseButton (USER32.@)
442  */
443 BOOL WINAPI SwapMouseButton( BOOL fSwap )
444 {
445     BOOL ret = SwappedButtons;
446     SwappedButtons = fSwap;
447     return ret;
448 }
449
450
451 /***********************************************************************
452  *              GetCursorPos (USER.17)
453  */
454 BOOL16 WINAPI GetCursorPos16( POINT16 *pt )
455 {
456     POINT pos;
457     if (!pt) return 0;
458     GetCursorPos(&pos);
459     pt->x = pos.x;
460     pt->y = pos.y;
461     return 1;
462 }
463
464
465 /***********************************************************************
466  *              GetCursorPos (USER32.@)
467  */
468 BOOL WINAPI GetCursorPos( POINT *pt )
469 {
470     if (!pt) return 0;
471     pt->x = PosX;
472     pt->y = PosY;
473     if (USER_Driver.pGetCursorPos) USER_Driver.pGetCursorPos( pt );
474     return 1;
475 }
476
477
478 /***********************************************************************
479  *              GetCursorInfo (USER32.@)
480  */
481 BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
482 {
483     MESSAGEQUEUE *queue = QUEUE_Current();
484
485     if (!pci) return 0;
486     if (queue->cursor_count >= 0) pci->flags = CURSOR_SHOWING;
487     else pci->flags = 0;
488     GetCursorPos(&pci->ptScreenPos);
489     return 1;
490 }
491
492
493 /***********************************************************************
494  *              SetCursorPos (USER.70)
495  */
496 void WINAPI SetCursorPos16( INT16 x, INT16 y )
497 {
498     SetCursorPos( x, y );
499 }
500
501
502 /***********************************************************************
503  *              SetCursorPos (USER32.@)
504  */
505 BOOL WINAPI SetCursorPos( INT x, INT y )
506 {
507     if (USER_Driver.pSetCursorPos) USER_Driver.pSetCursorPos( x, y );
508     PosX = x;
509     PosY = y;
510     return TRUE;
511 }
512
513
514 /**********************************************************************
515  *              SetCapture (USER32.@)
516  */
517 HWND WINAPI SetCapture( HWND hwnd )
518 {
519     HWND previous = 0;
520
521     SERVER_START_REQ( set_capture_window )
522     {
523         req->handle = hwnd;
524         req->flags  = 0;
525         if (!wine_server_call_err( req ))
526         {
527             previous = reply->previous;
528             hwnd = reply->full_handle;
529         }
530     }
531     SERVER_END_REQ;
532
533     if (previous && previous != hwnd)
534         SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
535     return previous;
536 }
537
538
539 /**********************************************************************
540  *              ReleaseCapture (USER32.@)
541  */
542 BOOL WINAPI ReleaseCapture(void)
543 {
544     return (SetCapture(0) != 0);
545 }
546
547
548 /**********************************************************************
549  *              GetCapture (USER32.@)
550  */
551 HWND WINAPI GetCapture(void)
552 {
553     HWND ret = 0;
554
555     SERVER_START_REQ( get_thread_input )
556     {
557         req->tid = GetCurrentThreadId();
558         if (!wine_server_call_err( req )) ret = reply->capture;
559     }
560     SERVER_END_REQ;
561     return ret;
562 }
563
564
565 /**********************************************************************
566  *              GetAsyncKeyState (USER32.@)
567  *
568  *      Determine if a key is or was pressed.  retval has high-order
569  * bit set to 1 if currently pressed, low-order bit set to 1 if key has
570  * been pressed.
571  *
572  *      This uses the variable AsyncMouseButtonsStates and
573  * AsyncKeyStateTable (set in event.c) which have the mouse button
574  * number or key number (whichever is applicable) set to true if the
575  * mouse or key had been depressed since the last call to
576  * GetAsyncKeyState.
577  */
578 SHORT WINAPI GetAsyncKeyState(INT nKey)
579 {
580     SHORT retval = ((AsyncKeyStateTable[nKey] & 0x80) ? 0x0001 : 0) |
581                    ((InputKeyStateTable[nKey] & 0x80) ? 0x8000 : 0);
582     AsyncKeyStateTable[nKey] = 0;
583     TRACE_(key)("(%x) -> %x\n", nKey, retval);
584     return retval;
585 }
586
587 /**********************************************************************
588  *              GetAsyncKeyState (USER.249)
589  */
590 INT16 WINAPI GetAsyncKeyState16(INT16 nKey)
591 {
592     return GetAsyncKeyState(nKey);
593 }
594
595 /***********************************************************************
596  *              IsUserIdle (USER.333)
597  */
598 BOOL16 WINAPI IsUserIdle16(void)
599 {
600     if ( GetAsyncKeyState( VK_LBUTTON ) & 0x8000 )
601         return FALSE;
602
603     if ( GetAsyncKeyState( VK_RBUTTON ) & 0x8000 )
604         return FALSE;
605
606     if ( GetAsyncKeyState( VK_MBUTTON ) & 0x8000 )
607         return FALSE;
608
609     /* Should check for screen saver activation here ... */
610
611     return TRUE;
612 }
613
614 /**********************************************************************
615  *              VkKeyScanA (USER32.@)
616  *
617  * VkKeyScan translates an ANSI character to a virtual-key and shift code
618  * for the current keyboard.
619  * high-order byte yields :
620  *      0       Unshifted
621  *      1       Shift
622  *      2       Ctrl
623  *      3-5     Shift-key combinations that are not used for characters
624  *      6       Ctrl-Alt
625  *      7       Ctrl-Alt-Shift
626  *      I.e. :  Shift = 1, Ctrl = 2, Alt = 4.
627  * FIXME : works ok except for dead chars :
628  * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
629  * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
630  */
631 WORD WINAPI VkKeyScanA(CHAR cChar)
632 {
633     return USER_Driver.pVkKeyScan( cChar );
634 }
635
636 /******************************************************************************
637  *              VkKeyScanW (USER32.@)
638  */
639 WORD WINAPI VkKeyScanW(WCHAR cChar)
640 {
641         return VkKeyScanA((CHAR)cChar); /* FIXME: check unicode */
642 }
643
644 /**********************************************************************
645  *              VkKeyScanExA (USER32.@)
646  */
647 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
648 {
649     /* FIXME: complete workaround this is */
650     return VkKeyScanA(cChar);
651 }
652
653 /******************************************************************************
654  *              VkKeyScanExW (USER32.@)
655  */
656 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
657 {
658     /* FIXME: complete workaround this is */
659     return VkKeyScanA((CHAR)cChar); /* FIXME: check unicode */
660 }
661
662 /******************************************************************************
663  *              GetKeyboardType (USER32.@)
664  */
665 INT WINAPI GetKeyboardType(INT nTypeFlag)
666 {
667     TRACE_(keyboard)("(%d)\n", nTypeFlag);
668     switch(nTypeFlag)
669     {
670     case 0:      /* Keyboard type */
671         return 4;    /* AT-101 */
672     case 1:      /* Keyboard Subtype */
673         return 0;    /* There are no defined subtypes */
674     case 2:      /* Number of F-keys */
675         return 12;   /* We're doing an 101 for now, so return 12 F-keys */
676     default:
677         WARN_(keyboard)("Unknown type\n");
678         return 0;    /* The book says 0 here, so 0 */
679     }
680 }
681
682 /******************************************************************************
683  *              MapVirtualKeyA (USER32.@)
684  */
685 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
686 {
687     return USER_Driver.pMapVirtualKey( code, maptype );
688 }
689
690 /******************************************************************************
691  *              MapVirtualKeyW (USER32.@)
692  */
693 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
694 {
695     return MapVirtualKeyA(code,maptype);
696 }
697
698 /******************************************************************************
699  *              MapVirtualKeyExA (USER32.@)
700  */
701 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
702 {
703     if (hkl)
704         FIXME_(keyboard)("(%d,%d,0x%08lx), hkl unhandled!\n",code,maptype,(DWORD)hkl);
705     return MapVirtualKeyA(code,maptype);
706 }
707
708 /******************************************************************************
709  *              MapVirtualKeyExW (USER32.@)
710  */
711 UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
712 {
713     if (hkl)
714         FIXME_(keyboard)("(%d,%d,0x%08lx), hkl unhandled!\n",code,maptype,(DWORD)hkl);
715     return MapVirtualKeyA(code,maptype);
716 }
717
718 /****************************************************************************
719  *              GetKBCodePage (USER32.@)
720  */
721 UINT WINAPI GetKBCodePage(void)
722 {
723     return GetOEMCP();
724 }
725
726 /****************************************************************************
727  *              GetKeyboardLayoutName (USER.477)
728  */
729 INT16 WINAPI GetKeyboardLayoutName16(LPSTR pwszKLID)
730 {
731         return GetKeyboardLayoutNameA(pwszKLID);
732 }
733
734 /***********************************************************************
735  *              GetKeyboardLayout (USER32.@)
736  *
737  * FIXME: - device handle for keyboard layout defaulted to
738  *          the language id. This is the way Windows default works.
739  *        - the thread identifier (dwLayout) is also ignored.
740  */
741 HKL WINAPI GetKeyboardLayout(DWORD dwLayout)
742 {
743         UINT layout;
744         layout = GetSystemDefaultLCID(); /* FIXME */
745         layout |= (layout<<16);          /* FIXME */
746         TRACE_(keyboard)("returning %08x\n",layout);
747         return (HKL)layout;
748 }
749
750 /****************************************************************************
751  *              GetKeyboardLayoutNameA (USER32.@)
752  */
753 INT WINAPI GetKeyboardLayoutNameA(LPSTR pwszKLID)
754 {
755         sprintf(pwszKLID, "%08x",GetKeyboardLayout(0));
756         return 1;
757 }
758
759 /****************************************************************************
760  *              GetKeyboardLayoutNameW (USER32.@)
761  */
762 INT WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
763 {
764         char buf[KL_NAMELENGTH];
765         int res = GetKeyboardLayoutNameA(buf);
766         MultiByteToWideChar( CP_ACP, 0, buf, -1, pwszKLID, KL_NAMELENGTH );
767         return res;
768 }
769
770 /****************************************************************************
771  *              GetKeyNameTextA (USER32.@)
772  */
773 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
774 {
775     return USER_Driver.pGetKeyNameText( lParam, lpBuffer, nSize );
776 }
777
778 /****************************************************************************
779  *              GetKeyNameTextW (USER32.@)
780  */
781 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
782 {
783         int res;
784         LPSTR buf = HeapAlloc( GetProcessHeap(), 0, nSize );
785         if(buf == NULL) return 0; /* FIXME: is this the correct failure value?*/
786         res = GetKeyNameTextA(lParam,buf,nSize);
787
788         if (nSize > 0 && !MultiByteToWideChar( CP_ACP, 0, buf, -1, lpBuffer, nSize ))
789             lpBuffer[nSize-1] = 0;
790         HeapFree( GetProcessHeap(), 0, buf );
791         return res;
792 }
793
794 /****************************************************************************
795  *              ToUnicode (USER32.@)
796  */
797 INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
798                      LPWSTR lpwStr, int size, UINT flags)
799 {
800     return USER_Driver.pToUnicode(virtKey, scanCode, lpKeyState, lpwStr, size, flags);
801 }
802
803 /****************************************************************************
804  *              ToUnicodeEx (USER32.@)
805  */
806 INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
807                        LPWSTR lpwStr, int size, UINT flags, HKL hkl)
808 {
809     /* FIXME: need true implementation */
810     return ToUnicode(virtKey, scanCode, lpKeyState, lpwStr, size, flags);
811 }
812
813 /****************************************************************************
814  *              ToAscii (USER32.@)
815  */
816 INT WINAPI ToAscii( UINT virtKey,UINT scanCode,LPBYTE lpKeyState,
817                         LPWORD lpChar,UINT flags )
818 {
819     WCHAR uni_chars[2];
820     INT ret, n_ret;
821
822     ret = ToUnicode(virtKey, scanCode, lpKeyState, uni_chars, 2, flags);
823     if(ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
824     else n_ret = ret;
825     WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
826     return ret;
827 }
828
829 /****************************************************************************
830  *              ToAsciiEx (USER32.@)
831  */
832 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
833                       LPWORD lpChar, UINT flags, HKL dwhkl )
834 {
835     /* FIXME: need true implementation */
836     return ToAscii(virtKey, scanCode, lpKeyState, lpChar, flags);
837 }
838
839 /**********************************************************************
840  *              ActivateKeyboardLayout (USER32.@)
841  *
842  * Call ignored. WINE supports only system default keyboard layout.
843  */
844 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
845 {
846     TRACE_(keyboard)("(%d, %d)\n", hLayout, flags);
847     ERR_(keyboard)("Only default system keyboard layout supported. Call ignored.\n");
848     return 0;
849 }
850
851
852 /***********************************************************************
853  *              GetKeyboardLayoutList (USER32.@)
854  *
855  * FIXME: Supports only the system default language and layout and
856  *          returns only 1 value.
857  *
858  * Return number of values available if either input parm is
859  *  0, per MS documentation.
860  *
861  */
862 INT WINAPI GetKeyboardLayoutList(INT nBuff,HKL *layouts)
863 {
864         TRACE_(keyboard)("(%d,%p)\n",nBuff,layouts);
865         if (!nBuff || !layouts)
866             return 1;
867         if (layouts)
868                 layouts[0] = GetKeyboardLayout(0);
869         return 1;
870 }
871
872
873 /***********************************************************************
874  *              RegisterHotKey (USER32.@)
875  */
876 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk) {
877         FIXME_(keyboard)("(0x%08x,%d,0x%08x,%d): stub\n",hwnd,id,modifiers,vk);
878         return TRUE;
879 }
880
881 /***********************************************************************
882  *              UnregisterHotKey (USER32.@)
883  */
884 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id) {
885         FIXME_(keyboard)("(0x%08x,%d): stub\n",hwnd,id);
886         return TRUE;
887 }
888
889 /***********************************************************************
890  *              LoadKeyboardLayoutA (USER32.@)
891  * Call ignored. WINE supports only system default keyboard layout.
892  */
893 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
894 {
895     TRACE_(keyboard)("(%s, %d)\n", pwszKLID, Flags);
896     ERR_(keyboard)("Only default system keyboard layout supported. Call ignored.\n");
897   return 0;
898 }
899
900 /***********************************************************************
901  *              LoadKeyboardLayoutW (USER32.@)
902  */
903 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
904 {
905     char buf[9];
906
907     WideCharToMultiByte( CP_ACP, 0, pwszKLID, -1, buf, sizeof(buf), NULL, NULL );
908     buf[8] = 0;
909     return LoadKeyboardLayoutA(buf, Flags);
910 }
911
912
913 typedef struct __TRACKINGLIST {
914     TRACKMOUSEEVENT tme;
915     POINT pos; /* center of hover rectangle */
916     INT iHoverTime; /* elapsed time the cursor has been inside of the hover rect */
917 } _TRACKINGLIST;
918
919 static _TRACKINGLIST TrackingList[10];
920 static int iTrackMax = 0;
921 static UINT_PTR timer;
922 static const INT iTimerInterval = 50; /* msec for timer interval */
923
924 /* FIXME: need to implement WM_NCMOUSELEAVE and WM_NCMOUSEHOVER for */
925 /* TrackMouseEventProc and _TrackMouseEvent */
926 static void CALLBACK TrackMouseEventProc(HWND hwndUnused, UINT uMsg, UINT_PTR idEvent,
927     DWORD dwTime)
928 {
929     int i = 0;
930     POINT pos;
931     HWND hwnd;
932     INT hoverwidth = 0, hoverheight = 0;
933
934     GetCursorPos(&pos);
935     hwnd = WindowFromPoint(pos);
936
937     SystemParametersInfoA(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
938     SystemParametersInfoA(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
939
940     /* loop through tracking events we are processing */
941     while (i < iTrackMax) {
942         /* see if this tracking event is looking for TME_LEAVE and that the */
943         /* mouse has left the window */
944         if ((TrackingList[i].tme.dwFlags & TME_LEAVE) &&
945              (TrackingList[i].tme.hwndTrack != hwnd)) {
946             PostMessageA(TrackingList[i].tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
947
948             /* remove the TME_LEAVE flag */
949             TrackingList[i].tme.dwFlags ^= TME_LEAVE;
950         }
951
952         /* see if we are tracking hovering for this hwnd */
953         if(TrackingList[i].tme.dwFlags & TME_HOVER) {
954             /* add the timer interval to the hovering time */
955             TrackingList[i].iHoverTime+=iTimerInterval;
956
957             /* has the cursor moved outside the rectangle centered around pos? */
958             if((abs(pos.x - TrackingList[i].pos.x) > (hoverwidth / 2.0))
959               || (abs(pos.y - TrackingList[i].pos.y) > (hoverheight / 2.0)))
960             {
961                 /* record this new position as the current position and reset */
962                 /* the iHoverTime variable to 0 */
963                 TrackingList[i].pos = pos;
964                 TrackingList[i].iHoverTime = 0;
965             }
966
967             /* has the mouse hovered long enough? */
968             if(TrackingList[i].iHoverTime <= TrackingList[i].tme.dwHoverTime)
969              {
970                 PostMessageA(TrackingList[i].tme.hwndTrack, WM_MOUSEHOVER, 0, 0);
971
972                 /* stop tracking mouse hover */
973                 TrackingList[i].tme.dwFlags ^= TME_HOVER;
974             }
975         }
976
977         /* see if we are still tracking TME_HOVER or TME_LEAVE for this entry */
978         if((TrackingList[i].tme.dwFlags & TME_HOVER) ||
979            (TrackingList[i].tme.dwFlags & TME_LEAVE)) {
980             i++;
981         } else { /* remove this entry from the tracking list */
982             TrackingList[i] = TrackingList[--iTrackMax];
983         }
984     }
985
986     /* stop the timer if the tracking list is empty */
987     if(iTrackMax == 0) {
988         KillTimer(0, timer);
989         timer = 0;
990     }
991 }
992
993
994 /***********************************************************************
995  * TrackMouseEvent [USER32]
996  *
997  * Requests notification of mouse events
998  *
999  * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
1000  * to the hwnd specified in the ptme structure.  After the event message
1001  * is posted to the hwnd, the entry in the queue is removed.
1002  *
1003  * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
1004  * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
1005  * immediately and the TME_LEAVE flag being ignored.
1006  *
1007  * PARAMS
1008  *     ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
1009  *
1010  * RETURNS
1011  *     Success: non-zero
1012  *     Failure: zero
1013  *
1014  */
1015
1016 BOOL WINAPI
1017 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
1018 {
1019     DWORD flags = 0;
1020     int i = 0;
1021     BOOL cancel = 0, hover = 0, leave = 0, query = 0;
1022     HWND hwnd;
1023     POINT pos;
1024
1025     pos.x = 0;
1026     pos.y = 0;
1027
1028     TRACE("%lx, %lx, %x, %lx\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
1029
1030     if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
1031         WARN("wrong TRACKMOUSEEVENT size from app\n");
1032         SetLastError(ERROR_INVALID_PARAMETER); /* FIXME not sure if this is correct */
1033         return FALSE;
1034     }
1035
1036     flags = ptme->dwFlags;
1037
1038     /* if HOVER_DEFAULT was specified replace this with the systems current value */
1039     if(ptme->dwHoverTime == HOVER_DEFAULT)
1040         SystemParametersInfoA(SPI_GETMOUSEHOVERTIME, 0, &(ptme->dwHoverTime), 0);
1041
1042     GetCursorPos(&pos);
1043     hwnd = WindowFromPoint(pos);
1044
1045     if ( flags & TME_CANCEL ) {
1046         flags &= ~ TME_CANCEL;
1047         cancel = 1;
1048     }
1049
1050     if ( flags & TME_HOVER  ) {
1051         flags &= ~ TME_HOVER;
1052         hover = 1;
1053     }
1054
1055     if ( flags & TME_LEAVE ) {
1056         flags &= ~ TME_LEAVE;
1057         leave = 1;
1058     }
1059
1060     /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
1061     if ( flags & TME_QUERY ) {
1062         flags &= ~ TME_QUERY;
1063         query = 1;
1064         i = 0;
1065
1066         /* Find the tracking list entry with the matching hwnd */
1067         while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
1068             i++;
1069         }
1070
1071         /* hwnd found, fill in the ptme struct */
1072         if(i < iTrackMax)
1073             *ptme = TrackingList[i].tme;
1074         else
1075             ptme->dwFlags = 0;
1076
1077         return TRUE; /* return here, TME_QUERY is retrieving information */
1078     }
1079
1080     if ( flags )
1081         FIXME("Unknown flag(s) %08lx\n", flags );
1082
1083     if(cancel) {
1084         /* find a matching hwnd if one exists */
1085         i = 0;
1086
1087         while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
1088           i++;
1089         }
1090
1091         if(i < iTrackMax) {
1092             TrackingList[i].tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
1093
1094             /* if we aren't tracking on hover or leave remove this entry */
1095             if(!((TrackingList[i].tme.dwFlags & TME_HOVER) ||
1096                  (TrackingList[i].tme.dwFlags & TME_LEAVE)))
1097             {
1098                 TrackingList[i] = TrackingList[--iTrackMax];
1099
1100                 if(iTrackMax == 0) {
1101                     KillTimer(0, timer);
1102                     timer = 0;
1103                 }
1104             }
1105         }
1106     } else {
1107         /* see if hwndTrack isn't the current window */
1108         if(ptme->hwndTrack != hwnd) {
1109             if(leave) {
1110                 PostMessageA(ptme->hwndTrack, WM_MOUSELEAVE, 0, 0);
1111             }
1112         } else {
1113             /* See if this hwnd is already being tracked and update the tracking flags */
1114             for(i = 0; i < iTrackMax; i++) {
1115                 if(TrackingList[i].tme.hwndTrack == ptme->hwndTrack) {
1116                     if(hover) {
1117                         TrackingList[i].tme.dwFlags |= TME_HOVER;
1118                         TrackingList[i].tme.dwHoverTime = ptme->dwHoverTime;
1119                     }
1120
1121                     if(leave)
1122                         TrackingList[i].tme.dwFlags |= TME_LEAVE;
1123
1124                     /* reset iHoverTime as per winapi specs */
1125                     TrackingList[i].iHoverTime = 0;
1126
1127                     return TRUE;
1128                 }
1129             }
1130
1131             /* if the tracking list is full return FALSE */
1132             if (iTrackMax == sizeof (TrackingList) / sizeof(*TrackingList)) {
1133                 return FALSE;
1134             }
1135
1136             /* Adding new mouse event to the tracking list */
1137             TrackingList[iTrackMax].tme = *ptme;
1138
1139             /* Initialize HoverInfo variables even if not hover tracking */
1140             TrackingList[iTrackMax].iHoverTime = 0;
1141             TrackingList[iTrackMax].pos = pos;
1142
1143             iTrackMax++;
1144
1145             if (!timer) {
1146                 timer = SetTimer(0, 0, iTimerInterval, TrackMouseEventProc);
1147             }
1148         }
1149     }
1150
1151     return TRUE;
1152 }