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