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