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