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