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