shell32/tests: GetLongPathNameA is not implemented on win95.
[wine] / dlls / user32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <assert.h>
34
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41 #include "winnls.h"
42 #include "winternl.h"
43 #include "winerror.h"
44 #include "win.h"
45 #include "user_private.h"
46 #include "wine/server.h"
47 #include "wine/debug.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(win);
50 WINE_DECLARE_DEBUG_CHANNEL(keyboard);
51
52
53 /***********************************************************************
54  *           get_key_state
55  */
56 static WORD get_key_state(void)
57 {
58     WORD ret = 0;
59
60     if (GetSystemMetrics( SM_SWAPBUTTON ))
61     {
62         if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_LBUTTON;
63         if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_RBUTTON;
64     }
65     else
66     {
67         if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_LBUTTON;
68         if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_RBUTTON;
69     }
70     if (GetAsyncKeyState(VK_MBUTTON) & 0x80)  ret |= MK_MBUTTON;
71     if (GetAsyncKeyState(VK_SHIFT) & 0x80)    ret |= MK_SHIFT;
72     if (GetAsyncKeyState(VK_CONTROL) & 0x80)  ret |= MK_CONTROL;
73     if (GetAsyncKeyState(VK_XBUTTON1) & 0x80) ret |= MK_XBUTTON1;
74     if (GetAsyncKeyState(VK_XBUTTON2) & 0x80) ret |= MK_XBUTTON2;
75     return ret;
76 }
77
78
79 /***********************************************************************
80  *              SendInput  (USER32.@)
81  */
82 UINT WINAPI SendInput( UINT count, LPINPUT inputs, int size )
83 {
84     if (TRACE_ON(win))
85     {
86         UINT i;
87
88         for (i = 0; i < count; i++)
89         {
90             switch(inputs[i].type)
91             {
92             case INPUT_MOUSE:
93                 TRACE("mouse: dx %d, dy %d, data %x, flags %x, time %u, info %lx\n",
94                       inputs[i].u.mi.dx, inputs[i].u.mi.dy, inputs[i].u.mi.mouseData,
95                       inputs[i].u.mi.dwFlags, inputs[i].u.mi.time, inputs[i].u.mi.dwExtraInfo);
96                 break;
97
98             case INPUT_KEYBOARD:
99                 TRACE("keyboard: vk %x, scan %x, flags %x, time %u, info %lx\n",
100                       inputs[i].u.ki.wVk, inputs[i].u.ki.wScan, inputs[i].u.ki.dwFlags,
101                       inputs[i].u.ki.time, inputs[i].u.ki.dwExtraInfo);
102                 break;
103
104             case INPUT_HARDWARE:
105                 TRACE("hardware: msg %d, wParamL %x, wParamH %x\n",
106                       inputs[i].u.hi.uMsg, inputs[i].u.hi.wParamL, inputs[i].u.hi.wParamH);
107                 break;
108
109             default:
110                 FIXME("unknown input type %u\n", inputs[i].type);
111                 break;
112             }
113         }
114     }
115
116     return USER_Driver->pSendInput( count, inputs, size );
117 }
118
119
120 /***********************************************************************
121  *              keybd_event (USER32.@)
122  */
123 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
124                          DWORD dwFlags, ULONG_PTR dwExtraInfo )
125 {
126     INPUT input;
127
128     input.type = INPUT_KEYBOARD;
129     input.u.ki.wVk = bVk;
130     input.u.ki.wScan = bScan;
131     input.u.ki.dwFlags = dwFlags;
132     input.u.ki.time = GetTickCount();
133     input.u.ki.dwExtraInfo = dwExtraInfo;
134     SendInput( 1, &input, sizeof(input) );
135 }
136
137
138 /***********************************************************************
139  *              mouse_event (USER32.@)
140  */
141 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
142                          DWORD dwData, ULONG_PTR dwExtraInfo )
143 {
144     INPUT input;
145
146     input.type = INPUT_MOUSE;
147     input.u.mi.dx = dx;
148     input.u.mi.dy = dy;
149     input.u.mi.mouseData = dwData;
150     input.u.mi.dwFlags = dwFlags;
151     input.u.mi.time = GetCurrentTime();
152     input.u.mi.dwExtraInfo = dwExtraInfo;
153     SendInput( 1, &input, sizeof(input) );
154 }
155
156
157 /***********************************************************************
158  *              GetCursorPos (USER32.@)
159  */
160 BOOL WINAPI GetCursorPos( POINT *pt )
161 {
162     if (!pt) return FALSE;
163     return USER_Driver->pGetCursorPos( pt );
164 }
165
166
167 /***********************************************************************
168  *              GetCursorInfo (USER32.@)
169  */
170 BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
171 {
172     if (!pci) return 0;
173     if (get_user_thread_info()->cursor_count >= 0) pci->flags = CURSOR_SHOWING;
174     else pci->flags = 0;
175     GetCursorPos(&pci->ptScreenPos);
176     return 1;
177 }
178
179
180 /***********************************************************************
181  *              SetCursorPos (USER32.@)
182  */
183 BOOL WINAPI SetCursorPos( INT x, INT y )
184 {
185     return USER_Driver->pSetCursorPos( x, y );
186 }
187
188
189 /**********************************************************************
190  *              SetCapture (USER32.@)
191  */
192 HWND WINAPI SetCapture( HWND hwnd )
193 {
194     HWND previous = 0;
195
196     SERVER_START_REQ( set_capture_window )
197     {
198         req->handle = hwnd;
199         req->flags  = 0;
200         if (!wine_server_call_err( req ))
201         {
202             previous = reply->previous;
203             hwnd = reply->full_handle;
204         }
205     }
206     SERVER_END_REQ;
207
208     if (previous && previous != hwnd)
209         SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
210     return previous;
211 }
212
213
214 /**********************************************************************
215  *              ReleaseCapture (USER32.@)
216  */
217 BOOL WINAPI ReleaseCapture(void)
218 {
219     BOOL ret;
220     HWND previous = 0;
221
222     SERVER_START_REQ( set_capture_window )
223     {
224         req->handle = 0;
225         req->flags  = 0;
226         if ((ret = !wine_server_call_err( req ))) previous = reply->previous;
227     }
228     SERVER_END_REQ;
229
230     if (previous) SendMessageW( previous, WM_CAPTURECHANGED, 0, 0 );
231
232     /* Somebody may have missed some mouse movements */
233     mouse_event( MOUSEEVENTF_MOVE, 0, 0, 0, 0 );
234
235     return ret;
236 }
237
238
239 /**********************************************************************
240  *              GetCapture (USER32.@)
241  */
242 HWND WINAPI GetCapture(void)
243 {
244     HWND ret = 0;
245
246     SERVER_START_REQ( get_thread_input )
247     {
248         req->tid = GetCurrentThreadId();
249         if (!wine_server_call_err( req )) ret = reply->capture;
250     }
251     SERVER_END_REQ;
252     return ret;
253 }
254
255
256 /**********************************************************************
257  *              GetAsyncKeyState (USER32.@)
258  *
259  *      Determine if a key is or was pressed.  retval has high-order
260  * bit set to 1 if currently pressed, low-order bit set to 1 if key has
261  * been pressed.
262  */
263 SHORT WINAPI GetAsyncKeyState(INT nKey)
264 {
265     return USER_Driver->pGetAsyncKeyState( nKey );
266 }
267
268
269 /***********************************************************************
270  *              GetQueueStatus (USER32.@)
271  */
272 DWORD WINAPI GetQueueStatus( UINT flags )
273 {
274     DWORD ret = 0;
275
276     if (flags & ~(QS_ALLINPUT | QS_ALLPOSTMESSAGE | QS_SMRESULT))
277     {
278         SetLastError( ERROR_INVALID_FLAGS );
279         return 0;
280     }
281
282     /* check for pending X events */
283     USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, flags, 0 );
284
285     SERVER_START_REQ( get_queue_status )
286     {
287         req->clear = 1;
288         wine_server_call( req );
289         ret = MAKELONG( reply->changed_bits & flags, reply->wake_bits & flags );
290     }
291     SERVER_END_REQ;
292     return ret;
293 }
294
295
296 /***********************************************************************
297  *              GetInputState   (USER32.@)
298  */
299 BOOL WINAPI GetInputState(void)
300 {
301     DWORD ret = 0;
302
303     /* check for pending X events */
304     USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_INPUT, 0 );
305
306     SERVER_START_REQ( get_queue_status )
307     {
308         req->clear = 0;
309         wine_server_call( req );
310         ret = reply->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
311     }
312     SERVER_END_REQ;
313     return ret;
314 }
315
316
317 /******************************************************************
318  *              GetLastInputInfo (USER32.@)
319  */
320 BOOL WINAPI GetLastInputInfo(PLASTINPUTINFO plii)
321 {
322     BOOL ret;
323
324     TRACE("%p\n", plii);
325
326     if (plii->cbSize != sizeof (*plii) )
327     {
328         SetLastError(ERROR_INVALID_PARAMETER);
329         return FALSE;
330     }
331
332     SERVER_START_REQ( get_last_input_time )
333     {
334         ret = !wine_server_call_err( req );
335         if (ret)
336             plii->dwTime = reply->time;
337     }
338     SERVER_END_REQ;
339     return ret;
340 }
341
342
343 /******************************************************************
344 *               GetRawInputDeviceList (USER32.@)
345 */
346 UINT WINAPI GetRawInputDeviceList(PRAWINPUTDEVICELIST pRawInputDeviceList, PUINT puiNumDevices, UINT cbSize)
347 {
348     FIXME("(pRawInputDeviceList=%p, puiNumDevices=%p, cbSize=%d) stub!\n", pRawInputDeviceList, puiNumDevices, cbSize);
349
350     if(pRawInputDeviceList)
351         memset(pRawInputDeviceList, 0, sizeof *pRawInputDeviceList);
352     *puiNumDevices = 0;
353     return 0;
354 }
355
356
357 /******************************************************************
358 *               RegisterRawInputDevices (USER32.@)
359 */
360 BOOL WINAPI RegisterRawInputDevices(PRAWINPUTDEVICE pRawInputDevices, UINT uiNumDevices, UINT cbSize)
361 {
362     FIXME("(pRawInputDevices=%p, uiNumDevices=%d, cbSize=%d) stub!\n", pRawInputDevices, uiNumDevices, cbSize);
363
364     return TRUE;
365 }
366
367
368 /******************************************************************
369 *               GetRawInputData (USER32.@)
370 */
371 UINT WINAPI GetRawInputData(HRAWINPUT hRawInput, UINT uiCommand, LPVOID pData, PUINT pcbSize, UINT cbSizeHeader)
372 {
373     FIXME("(hRawInput=%p, uiCommand=%d, pData=%p, pcbSize=%p, cbSizeHeader=%d) stub!\n",
374             hRawInput, uiCommand, pData, pcbSize, cbSizeHeader);
375
376     return 0;
377 }
378
379
380 /******************************************************************
381 *               GetRawInputBuffer (USER32.@)
382 */
383 UINT WINAPI GetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader)
384 {
385     FIXME("(pData=%p, pcbSize=%p, cbSizeHeader=%d) stub!\n", pData, pcbSize, cbSizeHeader);
386
387     return 0;
388 }
389
390
391 /******************************************************************
392 *               GetRawInputDeviceInfoA (USER32.@)
393 */
394 UINT WINAPI GetRawInputDeviceInfoA(HANDLE hDevice, UINT uiCommand, LPVOID pData, PUINT pcbSize)
395 {
396     FIXME("(hDevice=%p, uiCommand=%d, pData=%p, pcbSize=%p) stub!\n", hDevice, uiCommand, pData, pcbSize);
397
398     return 0;
399 }
400
401
402 /******************************************************************
403 *               GetRawInputDeviceInfoW (USER32.@)
404 */
405 UINT WINAPI GetRawInputDeviceInfoW(HANDLE hDevice, UINT uiCommand, LPVOID pData, PUINT pcbSize)
406 {
407     FIXME("(hDevice=%p, uiCommand=%d, pData=%p, pcbSize=%p) stub!\n", hDevice, uiCommand, pData, pcbSize);
408
409     return 0;
410 }
411
412
413 /******************************************************************
414 *               GetRegisteredRawInputDevices (USER32.@)
415 */
416 UINT WINAPI GetRegisteredRawInputDevices(PRAWINPUTDEVICE pRawInputDevices, PUINT puiNumDevices, UINT cbSize)
417 {
418     FIXME("(pRawInputDevices=%p, puiNumDevices=%p, cbSize=%d) stub!\n", pRawInputDevices, puiNumDevices, cbSize);
419
420     return 0;
421 }
422
423
424 /******************************************************************
425 *               DefRawInputProc (USER32.@)
426 */
427 LRESULT WINAPI DefRawInputProc(PRAWINPUT *paRawInput, INT nInput, UINT cbSizeHeader)
428 {
429     FIXME("(paRawInput=%p, nInput=%d, cbSizeHeader=%d) stub!\n", *paRawInput, nInput, cbSizeHeader);
430
431     return 0;
432 }
433
434
435 /**********************************************************************
436  *              AttachThreadInput (USER32.@)
437  *
438  * Attaches the input processing mechanism of one thread to that of
439  * another thread.
440  */
441 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
442 {
443     BOOL ret;
444
445     SERVER_START_REQ( attach_thread_input )
446     {
447         req->tid_from = from;
448         req->tid_to   = to;
449         req->attach   = attach;
450         ret = !wine_server_call_err( req );
451     }
452     SERVER_END_REQ;
453     return ret;
454 }
455
456
457 /**********************************************************************
458  *              GetKeyState (USER32.@)
459  *
460  * An application calls the GetKeyState function in response to a
461  * keyboard-input message.  This function retrieves the state of the key
462  * at the time the input message was generated.
463  */
464 SHORT WINAPI GetKeyState(INT vkey)
465 {
466     SHORT retval = 0;
467
468     SERVER_START_REQ( get_key_state )
469     {
470         req->tid = GetCurrentThreadId();
471         req->key = vkey;
472         if (!wine_server_call( req )) retval = (signed char)reply->state;
473     }
474     SERVER_END_REQ;
475     TRACE("key (0x%x) -> %x\n", vkey, retval);
476     return retval;
477 }
478
479
480 /**********************************************************************
481  *              GetKeyboardState (USER32.@)
482  */
483 BOOL WINAPI GetKeyboardState( LPBYTE state )
484 {
485     BOOL ret;
486
487     TRACE("(%p)\n", state);
488
489     memset( state, 0, 256 );
490     SERVER_START_REQ( get_key_state )
491     {
492         req->tid = GetCurrentThreadId();
493         req->key = -1;
494         wine_server_set_reply( req, state, 256 );
495         ret = !wine_server_call_err( req );
496     }
497     SERVER_END_REQ;
498     return ret;
499 }
500
501
502 /**********************************************************************
503  *              SetKeyboardState (USER32.@)
504  */
505 BOOL WINAPI SetKeyboardState( LPBYTE state )
506 {
507     BOOL ret;
508
509     SERVER_START_REQ( set_key_state )
510     {
511         req->tid = GetCurrentThreadId();
512         wine_server_add_data( req, state, 256 );
513         ret = !wine_server_call_err( req );
514     }
515     SERVER_END_REQ;
516     return ret;
517 }
518
519
520 /**********************************************************************
521  *              VkKeyScanA (USER32.@)
522  *
523  * VkKeyScan translates an ANSI character to a virtual-key and shift code
524  * for the current keyboard.
525  * high-order byte yields :
526  *      0       Unshifted
527  *      1       Shift
528  *      2       Ctrl
529  *      3-5     Shift-key combinations that are not used for characters
530  *      6       Ctrl-Alt
531  *      7       Ctrl-Alt-Shift
532  *      I.e. :  Shift = 1, Ctrl = 2, Alt = 4.
533  * FIXME : works ok except for dead chars :
534  * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
535  * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
536  */
537 SHORT WINAPI VkKeyScanA(CHAR cChar)
538 {
539     WCHAR wChar;
540
541     if (IsDBCSLeadByte(cChar)) return -1;
542
543     MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
544     return VkKeyScanW(wChar);
545 }
546
547 /******************************************************************************
548  *              VkKeyScanW (USER32.@)
549  */
550 SHORT WINAPI VkKeyScanW(WCHAR cChar)
551 {
552     return VkKeyScanExW(cChar, GetKeyboardLayout(0));
553 }
554
555 /**********************************************************************
556  *              VkKeyScanExA (USER32.@)
557  */
558 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
559 {
560     WCHAR wChar;
561
562     if (IsDBCSLeadByte(cChar)) return -1;
563
564     MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
565     return VkKeyScanExW(wChar, dwhkl);
566 }
567
568 /******************************************************************************
569  *              VkKeyScanExW (USER32.@)
570  */
571 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
572 {
573     return USER_Driver->pVkKeyScanEx(cChar, dwhkl);
574 }
575
576 /**********************************************************************
577  *              OemKeyScan (USER32.@)
578  */
579 DWORD WINAPI OemKeyScan(WORD wOemChar)
580 {
581     return wOemChar;
582 }
583
584 /******************************************************************************
585  *              GetKeyboardType (USER32.@)
586  */
587 INT WINAPI GetKeyboardType(INT nTypeFlag)
588 {
589     TRACE_(keyboard)("(%d)\n", nTypeFlag);
590     switch(nTypeFlag)
591     {
592     case 0:      /* Keyboard type */
593         return 4;    /* AT-101 */
594     case 1:      /* Keyboard Subtype */
595         return 0;    /* There are no defined subtypes */
596     case 2:      /* Number of F-keys */
597         return 12;   /* We're doing an 101 for now, so return 12 F-keys */
598     default:
599         WARN_(keyboard)("Unknown type\n");
600         return 0;    /* The book says 0 here, so 0 */
601     }
602 }
603
604 /******************************************************************************
605  *              MapVirtualKeyA (USER32.@)
606  */
607 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
608 {
609     return MapVirtualKeyExA( code, maptype, GetKeyboardLayout(0) );
610 }
611
612 /******************************************************************************
613  *              MapVirtualKeyW (USER32.@)
614  */
615 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
616 {
617     return MapVirtualKeyExW(code, maptype, GetKeyboardLayout(0));
618 }
619
620 /******************************************************************************
621  *              MapVirtualKeyExA (USER32.@)
622  */
623 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
624 {
625     UINT ret;
626
627     ret = MapVirtualKeyExW( code, maptype, hkl );
628     if (maptype == MAPVK_VK_TO_CHAR)
629     {
630         BYTE ch = 0;
631         WCHAR wch = ret;
632
633         WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL );
634         ret = ch;
635     }
636     return ret;
637 }
638
639 /******************************************************************************
640  *              MapVirtualKeyExW (USER32.@)
641  */
642 UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
643 {
644     TRACE_(keyboard)("(%d, %d, %p)\n", code, maptype, hkl);
645
646     return USER_Driver->pMapVirtualKeyEx(code, maptype, hkl);
647 }
648
649 /****************************************************************************
650  *              GetKBCodePage (USER32.@)
651  */
652 UINT WINAPI GetKBCodePage(void)
653 {
654     return GetOEMCP();
655 }
656
657 /***********************************************************************
658  *              GetKeyboardLayout (USER32.@)
659  *
660  *        - device handle for keyboard layout defaulted to
661  *          the language id. This is the way Windows default works.
662  *        - the thread identifier (dwLayout) is also ignored.
663  */
664 HKL WINAPI GetKeyboardLayout(DWORD dwLayout)
665 {
666     return USER_Driver->pGetKeyboardLayout(dwLayout);
667 }
668
669 /****************************************************************************
670  *              GetKeyboardLayoutNameA (USER32.@)
671  */
672 BOOL WINAPI GetKeyboardLayoutNameA(LPSTR pszKLID)
673 {
674     WCHAR buf[KL_NAMELENGTH];
675
676     if (GetKeyboardLayoutNameW(buf))
677         return WideCharToMultiByte( CP_ACP, 0, buf, -1, pszKLID, KL_NAMELENGTH, NULL, NULL ) != 0;
678     return FALSE;
679 }
680
681 /****************************************************************************
682  *              GetKeyboardLayoutNameW (USER32.@)
683  */
684 BOOL WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
685 {
686     return USER_Driver->pGetKeyboardLayoutName(pwszKLID);
687 }
688
689 /****************************************************************************
690  *              GetKeyNameTextA (USER32.@)
691  */
692 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
693 {
694     WCHAR buf[256];
695     INT ret;
696
697     if (!GetKeyNameTextW(lParam, buf, 256))
698     {
699         lpBuffer[0] = 0;
700         return 0;
701     }
702     ret = WideCharToMultiByte(CP_ACP, 0, buf, -1, lpBuffer, nSize, NULL, NULL);
703     if (!ret && nSize)
704     {
705         ret = nSize - 1;
706         lpBuffer[ret] = 0;
707     }
708     return ret;
709 }
710
711 /****************************************************************************
712  *              GetKeyNameTextW (USER32.@)
713  */
714 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
715 {
716     return USER_Driver->pGetKeyNameText( lParam, lpBuffer, nSize );
717 }
718
719 /****************************************************************************
720  *              ToUnicode (USER32.@)
721  */
722 INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
723                      LPWSTR lpwStr, int size, UINT flags)
724 {
725     return ToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, GetKeyboardLayout(0));
726 }
727
728 /****************************************************************************
729  *              ToUnicodeEx (USER32.@)
730  */
731 INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
732                        LPWSTR lpwStr, int size, UINT flags, HKL hkl)
733 {
734     return USER_Driver->pToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, hkl);
735 }
736
737 /****************************************************************************
738  *              ToAscii (USER32.@)
739  */
740 INT WINAPI ToAscii( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
741                     LPWORD lpChar, UINT flags )
742 {
743     return ToAsciiEx(virtKey, scanCode, lpKeyState, lpChar, flags, GetKeyboardLayout(0));
744 }
745
746 /****************************************************************************
747  *              ToAsciiEx (USER32.@)
748  */
749 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
750                       LPWORD lpChar, UINT flags, HKL dwhkl )
751 {
752     WCHAR uni_chars[2];
753     INT ret, n_ret;
754
755     ret = ToUnicodeEx(virtKey, scanCode, lpKeyState, uni_chars, 2, flags, dwhkl);
756     if (ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
757     else n_ret = ret;
758     WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
759     return ret;
760 }
761
762 /**********************************************************************
763  *              ActivateKeyboardLayout (USER32.@)
764  */
765 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
766 {
767     TRACE_(keyboard)("(%p, %d)\n", hLayout, flags);
768
769     return USER_Driver->pActivateKeyboardLayout(hLayout, flags);
770 }
771
772 /**********************************************************************
773  *              BlockInput (USER32.@)
774  */
775 BOOL WINAPI BlockInput(BOOL fBlockIt)
776 {
777     FIXME_(keyboard)("(%d): stub\n", fBlockIt);
778     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
779
780     return FALSE;
781 }
782
783 /***********************************************************************
784  *              GetKeyboardLayoutList (USER32.@)
785  *
786  * Return number of values available if either input parm is
787  *  0, per MS documentation.
788  */
789 UINT WINAPI GetKeyboardLayoutList(INT nBuff, HKL *layouts)
790 {
791     TRACE_(keyboard)("(%d,%p)\n",nBuff,layouts);
792
793     return USER_Driver->pGetKeyboardLayoutList(nBuff, layouts);
794 }
795
796
797 /***********************************************************************
798  *              RegisterHotKey (USER32.@)
799  */
800 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk)
801 {
802     static int once;
803     if (!once++) FIXME_(keyboard)("(%p,%d,0x%08x,%d): stub\n",hwnd,id,modifiers,vk);
804     return TRUE;
805 }
806
807 /***********************************************************************
808  *              UnregisterHotKey (USER32.@)
809  */
810 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id)
811 {
812     static int once;
813     if (!once++) FIXME_(keyboard)("(%p,%d): stub\n",hwnd,id);
814     return TRUE;
815 }
816
817 /***********************************************************************
818  *              LoadKeyboardLayoutW (USER32.@)
819  */
820 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
821 {
822     TRACE_(keyboard)("(%s, %d)\n", debugstr_w(pwszKLID), Flags);
823
824     return USER_Driver->pLoadKeyboardLayout(pwszKLID, Flags);
825 }
826
827 /***********************************************************************
828  *              LoadKeyboardLayoutA (USER32.@)
829  */
830 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
831 {
832     HKL ret;
833     UNICODE_STRING pwszKLIDW;
834
835     if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
836     else pwszKLIDW.Buffer = NULL;
837
838     ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
839     RtlFreeUnicodeString(&pwszKLIDW);
840     return ret;
841 }
842
843
844 /***********************************************************************
845  *              UnloadKeyboardLayout (USER32.@)
846  */
847 BOOL WINAPI UnloadKeyboardLayout(HKL hkl)
848 {
849     TRACE_(keyboard)("(%p)\n", hkl);
850
851     return USER_Driver->pUnloadKeyboardLayout(hkl);
852 }
853
854 typedef struct __TRACKINGLIST {
855     TRACKMOUSEEVENT tme;
856     POINT pos; /* center of hover rectangle */
857 } _TRACKINGLIST;
858
859 /* FIXME: move tracking stuff into a per thread data */
860 static _TRACKINGLIST tracking_info;
861 static UINT_PTR timer;
862
863 static void check_mouse_leave(HWND hwnd, int hittest)
864 {
865     if (tracking_info.tme.hwndTrack != hwnd)
866     {
867         if (tracking_info.tme.dwFlags & TME_NONCLIENT)
868             PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
869         else
870             PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
871
872         /* remove the TME_LEAVE flag */
873         tracking_info.tme.dwFlags &= ~TME_LEAVE;
874     }
875     else
876     {
877         if (hittest == HTCLIENT)
878         {
879             if (tracking_info.tme.dwFlags & TME_NONCLIENT)
880             {
881                 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
882                 /* remove the TME_LEAVE flag */
883                 tracking_info.tme.dwFlags &= ~TME_LEAVE;
884             }
885         }
886         else
887         {
888             if (!(tracking_info.tme.dwFlags & TME_NONCLIENT))
889             {
890                 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
891                 /* remove the TME_LEAVE flag */
892                 tracking_info.tme.dwFlags &= ~TME_LEAVE;
893             }
894         }
895     }
896 }
897
898 static void CALLBACK TrackMouseEventProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent,
899                                          DWORD dwTime)
900 {
901     POINT pos;
902     INT hoverwidth = 0, hoverheight = 0, hittest;
903
904     TRACE("hwnd %p, msg %04x, id %04lx, time %u\n", hwnd, uMsg, idEvent, dwTime);
905
906     GetCursorPos(&pos);
907     hwnd = WINPOS_WindowFromPoint(hwnd, pos, &hittest);
908
909     TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
910
911     SystemParametersInfoW(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
912     SystemParametersInfoW(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
913
914     TRACE("tracked pos %s, current pos %s, hover width %d, hover height %d\n",
915            wine_dbgstr_point(&tracking_info.pos), wine_dbgstr_point(&pos),
916            hoverwidth, hoverheight);
917
918     /* see if this tracking event is looking for TME_LEAVE and that the */
919     /* mouse has left the window */
920     if (tracking_info.tme.dwFlags & TME_LEAVE)
921     {
922         check_mouse_leave(hwnd, hittest);
923     }
924
925     if (tracking_info.tme.hwndTrack != hwnd)
926     {
927         /* mouse is gone, stop tracking mouse hover */
928         tracking_info.tme.dwFlags &= ~TME_HOVER;
929     }
930
931     /* see if we are tracking hovering for this hwnd */
932     if (tracking_info.tme.dwFlags & TME_HOVER)
933     {
934         /* has the cursor moved outside the rectangle centered around pos? */
935         if ((abs(pos.x - tracking_info.pos.x) > (hoverwidth / 2)) ||
936             (abs(pos.y - tracking_info.pos.y) > (hoverheight / 2)))
937         {
938             /* record this new position as the current position */
939             tracking_info.pos = pos;
940         }
941         else
942         {
943             if (hittest == HTCLIENT)
944             {
945                 ScreenToClient(hwnd, &pos);
946                 TRACE("client cursor pos %s\n", wine_dbgstr_point(&pos));
947
948                 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSEHOVER,
949                              get_key_state(), MAKELPARAM( pos.x, pos.y ));
950             }
951             else
952             {
953                 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
954                     PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSEHOVER,
955                                  hittest, MAKELPARAM( pos.x, pos.y ));
956             }
957
958             /* stop tracking mouse hover */
959             tracking_info.tme.dwFlags &= ~TME_HOVER;
960         }
961     }
962
963     /* stop the timer if the tracking list is empty */
964     if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
965     {
966         KillSystemTimer(tracking_info.tme.hwndTrack, timer);
967         timer = 0;
968         tracking_info.tme.hwndTrack = 0;
969         tracking_info.tme.dwFlags = 0;
970         tracking_info.tme.dwHoverTime = 0;
971     }
972 }
973
974
975 /***********************************************************************
976  * TrackMouseEvent [USER32]
977  *
978  * Requests notification of mouse events
979  *
980  * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
981  * to the hwnd specified in the ptme structure.  After the event message
982  * is posted to the hwnd, the entry in the queue is removed.
983  *
984  * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
985  * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
986  * immediately and the TME_LEAVE flag being ignored.
987  *
988  * PARAMS
989  *     ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
990  *
991  * RETURNS
992  *     Success: non-zero
993  *     Failure: zero
994  *
995  */
996
997 BOOL WINAPI
998 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
999 {
1000     HWND hwnd;
1001     POINT pos;
1002     DWORD hover_time;
1003     INT hittest;
1004
1005     TRACE("%x, %x, %p, %u\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
1006
1007     if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
1008         WARN("wrong TRACKMOUSEEVENT size from app\n");
1009         SetLastError(ERROR_INVALID_PARAMETER);
1010         return FALSE;
1011     }
1012
1013     /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
1014     if (ptme->dwFlags & TME_QUERY )
1015     {
1016         *ptme = tracking_info.tme;
1017         /* set cbSize in the case it's not initialized yet */
1018         ptme->cbSize = sizeof(TRACKMOUSEEVENT);
1019
1020         return TRUE; /* return here, TME_QUERY is retrieving information */
1021     }
1022
1023     if (!IsWindow(ptme->hwndTrack))
1024     {
1025         SetLastError(ERROR_INVALID_WINDOW_HANDLE);
1026         return FALSE;
1027     }
1028
1029     hover_time = ptme->dwHoverTime;
1030
1031     /* if HOVER_DEFAULT was specified replace this with the systems current value.
1032      * TME_LEAVE doesn't need to specify hover time so use default */
1033     if (hover_time == HOVER_DEFAULT || hover_time == 0 || !(ptme->dwHoverTime&TME_HOVER))
1034         SystemParametersInfoW(SPI_GETMOUSEHOVERTIME, 0, &hover_time, 0);
1035
1036     GetCursorPos(&pos);
1037     hwnd = WINPOS_WindowFromPoint(ptme->hwndTrack, pos, &hittest);
1038     TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
1039
1040     if (ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT))
1041         FIXME("Unknown flag(s) %08x\n", ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT));
1042
1043     if (ptme->dwFlags & TME_CANCEL)
1044     {
1045         if (tracking_info.tme.hwndTrack == ptme->hwndTrack)
1046         {
1047             tracking_info.tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
1048
1049             /* if we aren't tracking on hover or leave remove this entry */
1050             if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
1051             {
1052                 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1053                 timer = 0;
1054                 tracking_info.tme.hwndTrack = 0;
1055                 tracking_info.tme.dwFlags = 0;
1056                 tracking_info.tme.dwHoverTime = 0;
1057             }
1058         }
1059     } else {
1060         /* In our implementation it's possible that another window will receive a
1061          * WM_MOUSEMOVE and call TrackMouseEvent before TrackMouseEventProc is
1062          * called. In such a situation post the WM_MOUSELEAVE now */
1063         if (tracking_info.tme.dwFlags & TME_LEAVE && tracking_info.tme.hwndTrack != NULL)
1064             check_mouse_leave(hwnd, hittest);
1065
1066         if (timer)
1067         {
1068             KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1069             timer = 0;
1070             tracking_info.tme.hwndTrack = 0;
1071             tracking_info.tme.dwFlags = 0;
1072             tracking_info.tme.dwHoverTime = 0;
1073         }
1074
1075         if (ptme->hwndTrack == hwnd)
1076         {
1077             /* Adding new mouse event to the tracking list */
1078             tracking_info.tme = *ptme;
1079             tracking_info.tme.dwHoverTime = hover_time;
1080
1081             /* Initialize HoverInfo variables even if not hover tracking */
1082             tracking_info.pos = pos;
1083
1084             timer = SetSystemTimer(tracking_info.tme.hwndTrack, (UINT_PTR)&tracking_info.tme, hover_time, TrackMouseEventProc);
1085         }
1086     }
1087
1088     return TRUE;
1089 }