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