d2d1/tests: Fix stroke style object leak (Valgrind).
[wine.git] / dlls / user32 / input.c
blobc475b191ab38a687e7f9b27506cf38338805f019
1 /*
2 * USER Input processing
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
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.
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.
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <assert.h>
35 #define NONAMELESSUNION
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"
52 WINE_DEFAULT_DEBUG_CHANNEL(win);
53 WINE_DECLARE_DEBUG_CHANNEL(keyboard);
55 INT global_key_state_counter = 0;
57 /***********************************************************************
58 * get_key_state
60 static WORD get_key_state(void)
62 WORD ret = 0;
64 if (GetSystemMetrics( SM_SWAPBUTTON ))
66 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_LBUTTON;
67 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_RBUTTON;
69 else
71 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_LBUTTON;
72 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_RBUTTON;
74 if (GetAsyncKeyState(VK_MBUTTON) & 0x80) ret |= MK_MBUTTON;
75 if (GetAsyncKeyState(VK_SHIFT) & 0x80) ret |= MK_SHIFT;
76 if (GetAsyncKeyState(VK_CONTROL) & 0x80) ret |= MK_CONTROL;
77 if (GetAsyncKeyState(VK_XBUTTON1) & 0x80) ret |= MK_XBUTTON1;
78 if (GetAsyncKeyState(VK_XBUTTON2) & 0x80) ret |= MK_XBUTTON2;
79 return ret;
83 /**********************************************************************
84 * set_capture_window
86 BOOL set_capture_window( HWND hwnd, UINT gui_flags, HWND *prev_ret )
88 HWND previous = 0;
89 UINT flags = 0;
90 BOOL ret;
92 if (gui_flags & GUI_INMENUMODE) flags |= CAPTURE_MENU;
93 if (gui_flags & GUI_INMOVESIZE) flags |= CAPTURE_MOVESIZE;
95 SERVER_START_REQ( set_capture_window )
97 req->handle = wine_server_user_handle( hwnd );
98 req->flags = flags;
99 if ((ret = !wine_server_call_err( req )))
101 previous = wine_server_ptr_handle( reply->previous );
102 hwnd = wine_server_ptr_handle( reply->full_handle );
105 SERVER_END_REQ;
107 if (ret)
109 USER_Driver->pSetCapture( hwnd, gui_flags );
111 if (previous)
112 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
114 if (prev_ret) *prev_ret = previous;
116 return ret;
120 /***********************************************************************
121 * __wine_send_input (USER32.@)
123 * Internal SendInput function to allow the graphics driver to inject real events.
125 BOOL CDECL __wine_send_input( HWND hwnd, const INPUT *input )
127 NTSTATUS status = send_hardware_message( hwnd, input, 0 );
128 if (status) SetLastError( RtlNtStatusToDosError(status) );
129 return !status;
133 /***********************************************************************
134 * update_mouse_coords
136 * Helper for SendInput.
138 static void update_mouse_coords( INPUT *input )
140 if (!(input->u.mi.dwFlags & MOUSEEVENTF_MOVE)) return;
142 if (input->u.mi.dwFlags & MOUSEEVENTF_ABSOLUTE)
144 input->u.mi.dx = (input->u.mi.dx * GetSystemMetrics( SM_CXSCREEN )) >> 16;
145 input->u.mi.dy = (input->u.mi.dy * GetSystemMetrics( SM_CYSCREEN )) >> 16;
147 else
149 int accel[3];
151 /* dx and dy can be negative numbers for relative movements */
152 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
154 if (!accel[2]) return;
156 if (abs(input->u.mi.dx) > accel[0])
158 input->u.mi.dx *= 2;
159 if ((abs(input->u.mi.dx) > accel[1]) && (accel[2] == 2)) input->u.mi.dx *= 2;
161 if (abs(input->u.mi.dy) > accel[0])
163 input->u.mi.dy *= 2;
164 if ((abs(input->u.mi.dy) > accel[1]) && (accel[2] == 2)) input->u.mi.dy *= 2;
169 /***********************************************************************
170 * SendInput (USER32.@)
172 UINT WINAPI SendInput( UINT count, LPINPUT inputs, int size )
174 UINT i;
175 NTSTATUS status;
177 for (i = 0; i < count; i++)
179 if (inputs[i].type == INPUT_MOUSE)
181 /* we need to update the coordinates to what the server expects */
182 INPUT input = inputs[i];
183 update_mouse_coords( &input );
184 status = send_hardware_message( 0, &input, SEND_HWMSG_INJECTED );
186 else status = send_hardware_message( 0, &inputs[i], SEND_HWMSG_INJECTED );
188 if (status)
190 SetLastError( RtlNtStatusToDosError(status) );
191 break;
195 return i;
199 /***********************************************************************
200 * keybd_event (USER32.@)
202 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
203 DWORD dwFlags, ULONG_PTR dwExtraInfo )
205 INPUT input;
207 input.type = INPUT_KEYBOARD;
208 input.u.ki.wVk = bVk;
209 input.u.ki.wScan = bScan;
210 input.u.ki.dwFlags = dwFlags;
211 input.u.ki.time = 0;
212 input.u.ki.dwExtraInfo = dwExtraInfo;
213 SendInput( 1, &input, sizeof(input) );
217 /***********************************************************************
218 * mouse_event (USER32.@)
220 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
221 DWORD dwData, ULONG_PTR dwExtraInfo )
223 INPUT input;
225 input.type = INPUT_MOUSE;
226 input.u.mi.dx = dx;
227 input.u.mi.dy = dy;
228 input.u.mi.mouseData = dwData;
229 input.u.mi.dwFlags = dwFlags;
230 input.u.mi.time = 0;
231 input.u.mi.dwExtraInfo = dwExtraInfo;
232 SendInput( 1, &input, sizeof(input) );
236 /***********************************************************************
237 * GetCursorPos (USER32.@)
239 BOOL WINAPI DECLSPEC_HOTPATCH GetCursorPos( POINT *pt )
241 BOOL ret;
242 DWORD last_change;
244 if (!pt) return FALSE;
246 SERVER_START_REQ( set_cursor )
248 if ((ret = !wine_server_call( req )))
250 pt->x = reply->new_x;
251 pt->y = reply->new_y;
252 last_change = reply->last_change;
255 SERVER_END_REQ;
257 /* query new position from graphics driver if we haven't updated recently */
258 if (ret && GetTickCount() - last_change > 100) ret = USER_Driver->pGetCursorPos( pt );
259 return ret;
263 /***********************************************************************
264 * GetCursorInfo (USER32.@)
266 BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
268 BOOL ret;
270 if (!pci) return FALSE;
272 SERVER_START_REQ( get_thread_input )
274 req->tid = 0;
275 if ((ret = !wine_server_call( req )))
277 pci->hCursor = wine_server_ptr_handle( reply->cursor );
278 pci->flags = (reply->show_count >= 0) ? CURSOR_SHOWING : 0;
281 SERVER_END_REQ;
282 GetCursorPos(&pci->ptScreenPos);
283 return ret;
287 /***********************************************************************
288 * GetPhysicalCursorPos (USER32.@)
291 BOOL WINAPI GetPhysicalCursorPos(POINT *point)
293 FIXME("(%p) semi-stub: forwarding to GetCursorPos\n", point);
294 return GetCursorPos(point);
297 /***********************************************************************
298 * SetCursorPos (USER32.@)
300 BOOL WINAPI DECLSPEC_HOTPATCH SetCursorPos( INT x, INT y )
302 BOOL ret;
303 INT prev_x, prev_y, new_x, new_y;
305 SERVER_START_REQ( set_cursor )
307 req->flags = SET_CURSOR_POS;
308 req->x = x;
309 req->y = y;
310 if ((ret = !wine_server_call( req )))
312 prev_x = reply->prev_x;
313 prev_y = reply->prev_y;
314 new_x = reply->new_x;
315 new_y = reply->new_y;
318 SERVER_END_REQ;
319 if (ret && (prev_x != new_x || prev_y != new_y)) USER_Driver->pSetCursorPos( new_x, new_y );
320 return ret;
323 /***********************************************************************
324 * SetPhysicalCursorPos (USER32.@)
327 BOOL WINAPI SetPhysicalCursorPos(INT x, INT y)
329 FIXME("(%u %u) semi-stub: forwarding to SetCursorPos\n", x, y);
330 return SetCursorPos(x, y);
333 /**********************************************************************
334 * SetCapture (USER32.@)
336 HWND WINAPI DECLSPEC_HOTPATCH SetCapture( HWND hwnd )
338 HWND previous = 0;
340 set_capture_window( hwnd, 0, &previous );
341 return previous;
345 /**********************************************************************
346 * ReleaseCapture (USER32.@)
348 BOOL WINAPI DECLSPEC_HOTPATCH ReleaseCapture(void)
350 BOOL ret = set_capture_window( 0, 0, NULL );
352 /* Somebody may have missed some mouse movements */
353 if (ret) mouse_event( MOUSEEVENTF_MOVE, 0, 0, 0, 0 );
355 return ret;
359 /**********************************************************************
360 * GetCapture (USER32.@)
362 HWND WINAPI GetCapture(void)
364 HWND ret = 0;
366 SERVER_START_REQ( get_thread_input )
368 req->tid = GetCurrentThreadId();
369 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->capture );
371 SERVER_END_REQ;
372 return ret;
376 static void check_for_events( UINT flags )
378 if (USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, flags, 0 ) == WAIT_TIMEOUT)
379 flush_window_surfaces( TRUE );
382 /**********************************************************************
383 * GetAsyncKeyState (USER32.@)
385 * Determine if a key is or was pressed. retval has high-order
386 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
387 * been pressed.
389 SHORT WINAPI DECLSPEC_HOTPATCH GetAsyncKeyState( INT key )
391 struct user_key_state_info *key_state_info = get_user_thread_info()->key_state;
392 INT counter = global_key_state_counter;
393 SHORT ret;
395 if (key < 0 || key >= 256) return 0;
397 check_for_events( QS_INPUT );
399 if ((ret = USER_Driver->pGetAsyncKeyState( key )) == -1)
401 if (key_state_info &&
402 !(key_state_info->state[key] & 0xc0) &&
403 key_state_info->counter == counter &&
404 GetTickCount() - key_state_info->time < 50)
406 /* use cached value */
407 return 0;
409 else if (!key_state_info)
411 key_state_info = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*key_state_info) );
412 get_user_thread_info()->key_state = key_state_info;
415 ret = 0;
416 SERVER_START_REQ( get_key_state )
418 req->tid = 0;
419 req->key = key;
420 if (key_state_info) wine_server_set_reply( req, key_state_info->state,
421 sizeof(key_state_info->state) );
422 if (!wine_server_call( req ))
424 if (reply->state & 0x40) ret |= 0x0001;
425 if (reply->state & 0x80) ret |= 0x8000;
426 if (key_state_info)
428 key_state_info->time = GetTickCount();
429 key_state_info->counter = counter;
433 SERVER_END_REQ;
435 return ret;
439 /***********************************************************************
440 * GetQueueStatus (USER32.@)
442 DWORD WINAPI GetQueueStatus( UINT flags )
444 DWORD ret;
446 if (flags & ~(QS_ALLINPUT | QS_ALLPOSTMESSAGE | QS_SMRESULT))
448 SetLastError( ERROR_INVALID_FLAGS );
449 return 0;
452 check_for_events( flags );
454 SERVER_START_REQ( get_queue_status )
456 req->clear_bits = flags;
457 wine_server_call( req );
458 ret = MAKELONG( reply->changed_bits & flags, reply->wake_bits & flags );
460 SERVER_END_REQ;
461 return ret;
465 /***********************************************************************
466 * GetInputState (USER32.@)
468 BOOL WINAPI GetInputState(void)
470 DWORD ret;
472 check_for_events( QS_INPUT );
474 SERVER_START_REQ( get_queue_status )
476 req->clear_bits = 0;
477 wine_server_call( req );
478 ret = reply->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
480 SERVER_END_REQ;
481 return ret;
485 /******************************************************************
486 * GetLastInputInfo (USER32.@)
488 BOOL WINAPI GetLastInputInfo(PLASTINPUTINFO plii)
490 BOOL ret;
492 TRACE("%p\n", plii);
494 if (plii->cbSize != sizeof (*plii) )
496 SetLastError(ERROR_INVALID_PARAMETER);
497 return FALSE;
500 SERVER_START_REQ( get_last_input_time )
502 ret = !wine_server_call_err( req );
503 if (ret)
504 plii->dwTime = reply->time;
506 SERVER_END_REQ;
507 return ret;
511 /******************************************************************
512 * GetRawInputDeviceList (USER32.@)
514 UINT WINAPI GetRawInputDeviceList(RAWINPUTDEVICELIST *devices, UINT *device_count, UINT size)
516 TRACE("devices %p, device_count %p, size %u.\n", devices, device_count, size);
518 if (size != sizeof(*devices))
520 SetLastError(ERROR_INVALID_PARAMETER);
521 return ~0U;
524 if (!device_count)
526 SetLastError(ERROR_NOACCESS);
527 return ~0U;
530 if (!devices)
532 *device_count = 2;
533 return 0;
536 if (*device_count < 2)
538 SetLastError(ERROR_INSUFFICIENT_BUFFER);
539 *device_count = 2;
540 return ~0U;
543 devices[0].hDevice = WINE_MOUSE_HANDLE;
544 devices[0].dwType = RIM_TYPEMOUSE;
545 devices[1].hDevice = WINE_KEYBOARD_HANDLE;
546 devices[1].dwType = RIM_TYPEKEYBOARD;
548 return 2;
552 /******************************************************************
553 * RegisterRawInputDevices (USER32.@)
555 BOOL WINAPI DECLSPEC_HOTPATCH RegisterRawInputDevices(RAWINPUTDEVICE *devices, UINT device_count, UINT size)
557 struct rawinput_device *d;
558 BOOL ret;
559 UINT i;
561 TRACE("devices %p, device_count %u, size %u.\n", devices, device_count, size);
563 if (size != sizeof(*devices))
565 WARN("Invalid structure size %u.\n", size);
566 return FALSE;
569 if (!(d = HeapAlloc( GetProcessHeap(), 0, device_count * sizeof(*d) ))) return FALSE;
571 for (i = 0; i < device_count; ++i)
573 TRACE("device %u: page %#x, usage %#x, flags %#x, target %p.\n",
574 i, devices[i].usUsagePage, devices[i].usUsage,
575 devices[i].dwFlags, devices[i].hwndTarget);
576 if (devices[i].dwFlags & ~RIDEV_REMOVE)
577 FIXME("Unhandled flags %#x for device %u.\n", devices[i].dwFlags, i);
579 d[i].usage_page = devices[i].usUsagePage;
580 d[i].usage = devices[i].usUsage;
581 d[i].flags = devices[i].dwFlags;
582 d[i].target = wine_server_user_handle( devices[i].hwndTarget );
585 SERVER_START_REQ( update_rawinput_devices )
587 wine_server_add_data( req, d, device_count * sizeof(*d) );
588 ret = !wine_server_call( req );
590 SERVER_END_REQ;
592 HeapFree( GetProcessHeap(), 0, d );
594 return ret;
598 /******************************************************************
599 * GetRawInputData (USER32.@)
601 UINT WINAPI GetRawInputData(HRAWINPUT rawinput, UINT command, void *data, UINT *data_size, UINT header_size)
603 RAWINPUT *ri = (RAWINPUT *)rawinput;
604 UINT s;
606 TRACE("rawinput %p, command %#x, data %p, data_size %p, header_size %u.\n",
607 rawinput, command, data, data_size, header_size);
609 if (header_size != sizeof(RAWINPUTHEADER))
611 WARN("Invalid structure size %u.\n", header_size);
612 return ~0U;
615 switch (command)
617 case RID_INPUT:
618 s = ri->header.dwSize;
619 break;
620 case RID_HEADER:
621 s = sizeof(RAWINPUTHEADER);
622 break;
623 default:
624 return ~0U;
627 if (!data)
629 *data_size = s;
630 return 0;
633 if (*data_size < s) return ~0U;
634 memcpy(data, ri, s);
635 return s;
639 /******************************************************************
640 * GetRawInputBuffer (USER32.@)
642 UINT WINAPI DECLSPEC_HOTPATCH GetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader)
644 FIXME("(pData=%p, pcbSize=%p, cbSizeHeader=%d) stub!\n", pData, pcbSize, cbSizeHeader);
646 return 0;
650 /******************************************************************
651 * GetRawInputDeviceInfoA (USER32.@)
653 UINT WINAPI GetRawInputDeviceInfoA(HANDLE device, UINT command, void *data, UINT *data_size)
655 UINT ret;
657 TRACE("device %p, command %u, data %p, data_size %p.\n", device, command, data, data_size);
659 ret = GetRawInputDeviceInfoW(device, command, data, data_size);
660 if (command == RIDI_DEVICENAME && ret && ret != ~0U)
661 ret = WideCharToMultiByte(CP_ACP, 0, data, -1, data, *data_size, NULL, NULL);
663 return ret;
667 /******************************************************************
668 * GetRawInputDeviceInfoW (USER32.@)
670 UINT WINAPI GetRawInputDeviceInfoW(HANDLE device, UINT command, void *data, UINT *data_size)
672 /* FIXME: Most of this is made up. */
673 static const WCHAR keyboard_name[] = {'\\','\\','?','\\','W','I','N','E','_','K','E','Y','B','O','A','R','D',0};
674 static const WCHAR mouse_name[] = {'\\','\\','?','\\','W','I','N','E','_','M','O','U','S','E',0};
675 static const RID_DEVICE_INFO_KEYBOARD keyboard_info = {0, 0, 1, 12, 3, 101};
676 static const RID_DEVICE_INFO_MOUSE mouse_info = {1, 5, 0, FALSE};
677 const WCHAR *name = NULL;
678 RID_DEVICE_INFO *info;
679 UINT s;
681 TRACE("device %p, command %u, data %p, data_size %p.\n", device, command, data, data_size);
683 if (!data_size || (device != WINE_MOUSE_HANDLE && device != WINE_KEYBOARD_HANDLE)) return ~0U;
685 switch (command)
687 case RIDI_DEVICENAME:
688 if (device == WINE_MOUSE_HANDLE)
690 s = sizeof(mouse_name);
691 name = mouse_name;
693 else
695 s = sizeof(keyboard_name);
696 name = keyboard_name;
698 break;
699 case RIDI_DEVICEINFO:
700 s = sizeof(*info);
701 break;
702 default:
703 return ~0U;
706 if (!data)
708 *data_size = s;
709 return 0;
712 if (*data_size < s)
714 *data_size = s;
715 return ~0U;
718 if (command == RIDI_DEVICENAME)
720 memcpy(data, name, s);
721 return s;
724 info = data;
725 info->cbSize = sizeof(*info);
726 if (device == WINE_MOUSE_HANDLE)
728 info->dwType = RIM_TYPEMOUSE;
729 info->u.mouse = mouse_info;
731 else
733 info->dwType = RIM_TYPEKEYBOARD;
734 info->u.keyboard = keyboard_info;
736 return s;
740 /******************************************************************
741 * GetRegisteredRawInputDevices (USER32.@)
743 UINT WINAPI DECLSPEC_HOTPATCH GetRegisteredRawInputDevices(PRAWINPUTDEVICE pRawInputDevices, PUINT puiNumDevices, UINT cbSize)
745 FIXME("(pRawInputDevices=%p, puiNumDevices=%p, cbSize=%d) stub!\n", pRawInputDevices, puiNumDevices, cbSize);
747 return 0;
751 /******************************************************************
752 * DefRawInputProc (USER32.@)
754 LRESULT WINAPI DefRawInputProc(PRAWINPUT *paRawInput, INT nInput, UINT cbSizeHeader)
756 FIXME("(paRawInput=%p, nInput=%d, cbSizeHeader=%d) stub!\n", *paRawInput, nInput, cbSizeHeader);
758 return 0;
762 /**********************************************************************
763 * AttachThreadInput (USER32.@)
765 * Attaches the input processing mechanism of one thread to that of
766 * another thread.
768 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
770 BOOL ret;
772 SERVER_START_REQ( attach_thread_input )
774 req->tid_from = from;
775 req->tid_to = to;
776 req->attach = attach;
777 ret = !wine_server_call_err( req );
779 SERVER_END_REQ;
780 return ret;
784 /**********************************************************************
785 * GetKeyState (USER32.@)
787 * An application calls the GetKeyState function in response to a
788 * keyboard-input message. This function retrieves the state of the key
789 * at the time the input message was generated.
791 SHORT WINAPI DECLSPEC_HOTPATCH GetKeyState(INT vkey)
793 SHORT retval = 0;
795 SERVER_START_REQ( get_key_state )
797 req->tid = GetCurrentThreadId();
798 req->key = vkey;
799 if (!wine_server_call( req )) retval = (signed char)reply->state;
801 SERVER_END_REQ;
802 TRACE("key (0x%x) -> %x\n", vkey, retval);
803 return retval;
807 /**********************************************************************
808 * GetKeyboardState (USER32.@)
810 BOOL WINAPI DECLSPEC_HOTPATCH GetKeyboardState( LPBYTE state )
812 BOOL ret;
814 TRACE("(%p)\n", state);
816 memset( state, 0, 256 );
817 SERVER_START_REQ( get_key_state )
819 req->tid = GetCurrentThreadId();
820 req->key = -1;
821 wine_server_set_reply( req, state, 256 );
822 ret = !wine_server_call_err( req );
824 SERVER_END_REQ;
825 return ret;
829 /**********************************************************************
830 * SetKeyboardState (USER32.@)
832 BOOL WINAPI SetKeyboardState( LPBYTE state )
834 BOOL ret;
836 SERVER_START_REQ( set_key_state )
838 req->tid = GetCurrentThreadId();
839 wine_server_add_data( req, state, 256 );
840 ret = !wine_server_call_err( req );
842 SERVER_END_REQ;
843 return ret;
847 /**********************************************************************
848 * VkKeyScanA (USER32.@)
850 * VkKeyScan translates an ANSI character to a virtual-key and shift code
851 * for the current keyboard.
852 * high-order byte yields :
853 * 0 Unshifted
854 * 1 Shift
855 * 2 Ctrl
856 * 3-5 Shift-key combinations that are not used for characters
857 * 6 Ctrl-Alt
858 * 7 Ctrl-Alt-Shift
859 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
860 * FIXME : works ok except for dead chars :
861 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
862 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
864 SHORT WINAPI VkKeyScanA(CHAR cChar)
866 WCHAR wChar;
868 if (IsDBCSLeadByte(cChar)) return -1;
870 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
871 return VkKeyScanW(wChar);
874 /******************************************************************************
875 * VkKeyScanW (USER32.@)
877 SHORT WINAPI VkKeyScanW(WCHAR cChar)
879 return VkKeyScanExW(cChar, GetKeyboardLayout(0));
882 /**********************************************************************
883 * VkKeyScanExA (USER32.@)
885 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
887 WCHAR wChar;
889 if (IsDBCSLeadByte(cChar)) return -1;
891 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
892 return VkKeyScanExW(wChar, dwhkl);
895 /******************************************************************************
896 * VkKeyScanExW (USER32.@)
898 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
900 return USER_Driver->pVkKeyScanEx(cChar, dwhkl);
903 /**********************************************************************
904 * OemKeyScan (USER32.@)
906 DWORD WINAPI OemKeyScan( WORD oem )
908 WCHAR wchr;
909 DWORD vkey, scan;
910 char oem_char = LOBYTE( oem );
912 if (!OemToCharBuffW( &oem_char, &wchr, 1 ))
913 return -1;
915 vkey = VkKeyScanW( wchr );
916 scan = MapVirtualKeyW( LOBYTE( vkey ), MAPVK_VK_TO_VSC );
917 if (!scan) return -1;
919 vkey &= 0xff00;
920 vkey <<= 8;
921 return vkey | scan;
924 /******************************************************************************
925 * GetKeyboardType (USER32.@)
927 INT WINAPI GetKeyboardType(INT nTypeFlag)
929 TRACE_(keyboard)("(%d)\n", nTypeFlag);
930 switch(nTypeFlag)
932 case 0: /* Keyboard type */
933 return 4; /* AT-101 */
934 case 1: /* Keyboard Subtype */
935 return 0; /* There are no defined subtypes */
936 case 2: /* Number of F-keys */
937 return 12; /* We're doing an 101 for now, so return 12 F-keys */
938 default:
939 WARN_(keyboard)("Unknown type\n");
940 return 0; /* The book says 0 here, so 0 */
944 /******************************************************************************
945 * MapVirtualKeyA (USER32.@)
947 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
949 return MapVirtualKeyExA( code, maptype, GetKeyboardLayout(0) );
952 /******************************************************************************
953 * MapVirtualKeyW (USER32.@)
955 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
957 return MapVirtualKeyExW(code, maptype, GetKeyboardLayout(0));
960 /******************************************************************************
961 * MapVirtualKeyExA (USER32.@)
963 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
965 UINT ret;
967 ret = MapVirtualKeyExW( code, maptype, hkl );
968 if (maptype == MAPVK_VK_TO_CHAR)
970 BYTE ch = 0;
971 WCHAR wch = ret;
973 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL );
974 ret = ch;
976 return ret;
979 /******************************************************************************
980 * MapVirtualKeyExW (USER32.@)
982 UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
984 TRACE_(keyboard)("(%X, %d, %p)\n", code, maptype, hkl);
986 return USER_Driver->pMapVirtualKeyEx(code, maptype, hkl);
989 /****************************************************************************
990 * GetKBCodePage (USER32.@)
992 UINT WINAPI GetKBCodePage(void)
994 return GetOEMCP();
997 /***********************************************************************
998 * GetKeyboardLayout (USER32.@)
1000 * - device handle for keyboard layout defaulted to
1001 * the language id. This is the way Windows default works.
1002 * - the thread identifier is also ignored.
1004 HKL WINAPI GetKeyboardLayout(DWORD thread_id)
1006 return USER_Driver->pGetKeyboardLayout(thread_id);
1009 /****************************************************************************
1010 * GetKeyboardLayoutNameA (USER32.@)
1012 BOOL WINAPI GetKeyboardLayoutNameA(LPSTR pszKLID)
1014 WCHAR buf[KL_NAMELENGTH];
1016 if (GetKeyboardLayoutNameW(buf))
1017 return WideCharToMultiByte( CP_ACP, 0, buf, -1, pszKLID, KL_NAMELENGTH, NULL, NULL ) != 0;
1018 return FALSE;
1021 /****************************************************************************
1022 * GetKeyboardLayoutNameW (USER32.@)
1024 BOOL WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
1026 if (!pwszKLID)
1028 SetLastError(ERROR_NOACCESS);
1029 return FALSE;
1031 return USER_Driver->pGetKeyboardLayoutName(pwszKLID);
1034 /****************************************************************************
1035 * GetKeyNameTextA (USER32.@)
1037 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
1039 WCHAR buf[256];
1040 INT ret;
1042 if (!nSize || !GetKeyNameTextW(lParam, buf, 256))
1044 lpBuffer[0] = 0;
1045 return 0;
1047 ret = WideCharToMultiByte(CP_ACP, 0, buf, -1, lpBuffer, nSize, NULL, NULL);
1048 if (!ret && nSize)
1050 ret = nSize - 1;
1051 lpBuffer[ret] = 0;
1053 else ret--;
1055 return ret;
1058 /****************************************************************************
1059 * GetKeyNameTextW (USER32.@)
1061 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
1063 if (!lpBuffer || !nSize) return 0;
1064 return USER_Driver->pGetKeyNameText( lParam, lpBuffer, nSize );
1067 /****************************************************************************
1068 * ToUnicode (USER32.@)
1070 INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
1071 LPWSTR lpwStr, int size, UINT flags)
1073 return ToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, GetKeyboardLayout(0));
1076 /****************************************************************************
1077 * ToUnicodeEx (USER32.@)
1079 INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
1080 LPWSTR lpwStr, int size, UINT flags, HKL hkl)
1082 if (!lpKeyState) return 0;
1083 return USER_Driver->pToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, hkl);
1086 /****************************************************************************
1087 * ToAscii (USER32.@)
1089 INT WINAPI ToAscii( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
1090 LPWORD lpChar, UINT flags )
1092 return ToAsciiEx(virtKey, scanCode, lpKeyState, lpChar, flags, GetKeyboardLayout(0));
1095 /****************************************************************************
1096 * ToAsciiEx (USER32.@)
1098 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
1099 LPWORD lpChar, UINT flags, HKL dwhkl )
1101 WCHAR uni_chars[2];
1102 INT ret, n_ret;
1104 ret = ToUnicodeEx(virtKey, scanCode, lpKeyState, uni_chars, 2, flags, dwhkl);
1105 if (ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
1106 else n_ret = ret;
1107 WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
1108 return ret;
1111 /**********************************************************************
1112 * ActivateKeyboardLayout (USER32.@)
1114 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
1116 TRACE_(keyboard)("(%p, %d)\n", hLayout, flags);
1118 return USER_Driver->pActivateKeyboardLayout(hLayout, flags);
1121 /**********************************************************************
1122 * BlockInput (USER32.@)
1124 BOOL WINAPI BlockInput(BOOL fBlockIt)
1126 FIXME_(keyboard)("(%d): stub\n", fBlockIt);
1127 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1129 return FALSE;
1132 /***********************************************************************
1133 * GetKeyboardLayoutList (USER32.@)
1135 * Return number of values available if either input parm is
1136 * 0, per MS documentation.
1138 UINT WINAPI GetKeyboardLayoutList(INT nBuff, HKL *layouts)
1140 TRACE_(keyboard)( "(%d, %p)\n", nBuff, layouts );
1142 return USER_Driver->pGetKeyboardLayoutList( nBuff, layouts );
1146 /***********************************************************************
1147 * RegisterHotKey (USER32.@)
1149 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk)
1151 BOOL ret;
1152 int replaced=0;
1154 TRACE_(keyboard)("(%p,%d,0x%08x,%X)\n",hwnd,id,modifiers,vk);
1156 if ((hwnd == NULL || WIN_IsCurrentThread(hwnd)) &&
1157 !USER_Driver->pRegisterHotKey(hwnd, modifiers, vk))
1158 return FALSE;
1160 SERVER_START_REQ( register_hotkey )
1162 req->window = wine_server_user_handle( hwnd );
1163 req->id = id;
1164 req->flags = modifiers;
1165 req->vkey = vk;
1166 if ((ret = !wine_server_call_err( req )))
1168 replaced = reply->replaced;
1169 modifiers = reply->flags;
1170 vk = reply->vkey;
1173 SERVER_END_REQ;
1175 if (ret && replaced)
1176 USER_Driver->pUnregisterHotKey(hwnd, modifiers, vk);
1178 return ret;
1181 /***********************************************************************
1182 * UnregisterHotKey (USER32.@)
1184 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id)
1186 BOOL ret;
1187 UINT modifiers, vk;
1189 TRACE_(keyboard)("(%p,%d)\n",hwnd,id);
1191 SERVER_START_REQ( unregister_hotkey )
1193 req->window = wine_server_user_handle( hwnd );
1194 req->id = id;
1195 if ((ret = !wine_server_call_err( req )))
1197 modifiers = reply->flags;
1198 vk = reply->vkey;
1201 SERVER_END_REQ;
1203 if (ret)
1204 USER_Driver->pUnregisterHotKey(hwnd, modifiers, vk);
1206 return ret;
1209 /***********************************************************************
1210 * LoadKeyboardLayoutW (USER32.@)
1212 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
1214 TRACE_(keyboard)("(%s, %d)\n", debugstr_w(pwszKLID), Flags);
1216 return USER_Driver->pLoadKeyboardLayout(pwszKLID, Flags);
1219 /***********************************************************************
1220 * LoadKeyboardLayoutA (USER32.@)
1222 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
1224 HKL ret;
1225 UNICODE_STRING pwszKLIDW;
1227 if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
1228 else pwszKLIDW.Buffer = NULL;
1230 ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
1231 RtlFreeUnicodeString(&pwszKLIDW);
1232 return ret;
1236 /***********************************************************************
1237 * UnloadKeyboardLayout (USER32.@)
1239 BOOL WINAPI UnloadKeyboardLayout(HKL hkl)
1241 TRACE_(keyboard)("(%p)\n", hkl);
1243 return USER_Driver->pUnloadKeyboardLayout(hkl);
1246 typedef struct __TRACKINGLIST {
1247 TRACKMOUSEEVENT tme;
1248 POINT pos; /* center of hover rectangle */
1249 } _TRACKINGLIST;
1251 /* FIXME: move tracking stuff into a per thread data */
1252 static _TRACKINGLIST tracking_info;
1253 static UINT_PTR timer;
1255 static void check_mouse_leave(HWND hwnd, int hittest)
1257 if (tracking_info.tme.hwndTrack != hwnd)
1259 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1260 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
1261 else
1262 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
1264 /* remove the TME_LEAVE flag */
1265 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1267 else
1269 if (hittest == HTCLIENT)
1271 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1273 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
1274 /* remove the TME_LEAVE flag */
1275 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1278 else
1280 if (!(tracking_info.tme.dwFlags & TME_NONCLIENT))
1282 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
1283 /* remove the TME_LEAVE flag */
1284 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1290 static void CALLBACK TrackMouseEventProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent,
1291 DWORD dwTime)
1293 POINT pos;
1294 INT hoverwidth = 0, hoverheight = 0, hittest;
1296 TRACE("hwnd %p, msg %04x, id %04lx, time %u\n", hwnd, uMsg, idEvent, dwTime);
1298 GetCursorPos(&pos);
1299 hwnd = WINPOS_WindowFromPoint(hwnd, pos, &hittest);
1301 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
1303 SystemParametersInfoW(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
1304 SystemParametersInfoW(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
1306 TRACE("tracked pos %s, current pos %s, hover width %d, hover height %d\n",
1307 wine_dbgstr_point(&tracking_info.pos), wine_dbgstr_point(&pos),
1308 hoverwidth, hoverheight);
1310 /* see if this tracking event is looking for TME_LEAVE and that the */
1311 /* mouse has left the window */
1312 if (tracking_info.tme.dwFlags & TME_LEAVE)
1314 check_mouse_leave(hwnd, hittest);
1317 if (tracking_info.tme.hwndTrack != hwnd)
1319 /* mouse is gone, stop tracking mouse hover */
1320 tracking_info.tme.dwFlags &= ~TME_HOVER;
1323 /* see if we are tracking hovering for this hwnd */
1324 if (tracking_info.tme.dwFlags & TME_HOVER)
1326 /* has the cursor moved outside the rectangle centered around pos? */
1327 if ((abs(pos.x - tracking_info.pos.x) > (hoverwidth / 2)) ||
1328 (abs(pos.y - tracking_info.pos.y) > (hoverheight / 2)))
1330 /* record this new position as the current position */
1331 tracking_info.pos = pos;
1333 else
1335 if (hittest == HTCLIENT)
1337 ScreenToClient(hwnd, &pos);
1338 TRACE("client cursor pos %s\n", wine_dbgstr_point(&pos));
1340 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSEHOVER,
1341 get_key_state(), MAKELPARAM( pos.x, pos.y ));
1343 else
1345 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1346 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSEHOVER,
1347 hittest, MAKELPARAM( pos.x, pos.y ));
1350 /* stop tracking mouse hover */
1351 tracking_info.tme.dwFlags &= ~TME_HOVER;
1355 /* stop the timer if the tracking list is empty */
1356 if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
1358 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1359 timer = 0;
1360 tracking_info.tme.hwndTrack = 0;
1361 tracking_info.tme.dwFlags = 0;
1362 tracking_info.tme.dwHoverTime = 0;
1367 /***********************************************************************
1368 * TrackMouseEvent [USER32]
1370 * Requests notification of mouse events
1372 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
1373 * to the hwnd specified in the ptme structure. After the event message
1374 * is posted to the hwnd, the entry in the queue is removed.
1376 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
1377 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
1378 * immediately and the TME_LEAVE flag being ignored.
1380 * PARAMS
1381 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
1383 * RETURNS
1384 * Success: non-zero
1385 * Failure: zero
1389 BOOL WINAPI
1390 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
1392 HWND hwnd;
1393 POINT pos;
1394 DWORD hover_time;
1395 INT hittest;
1397 TRACE("%x, %x, %p, %u\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
1399 if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
1400 WARN("wrong TRACKMOUSEEVENT size from app\n");
1401 SetLastError(ERROR_INVALID_PARAMETER);
1402 return FALSE;
1405 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
1406 if (ptme->dwFlags & TME_QUERY )
1408 *ptme = tracking_info.tme;
1409 /* set cbSize in the case it's not initialized yet */
1410 ptme->cbSize = sizeof(TRACKMOUSEEVENT);
1412 return TRUE; /* return here, TME_QUERY is retrieving information */
1415 if (!IsWindow(ptme->hwndTrack))
1417 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
1418 return FALSE;
1421 hover_time = (ptme->dwFlags & TME_HOVER) ? ptme->dwHoverTime : HOVER_DEFAULT;
1423 /* if HOVER_DEFAULT was specified replace this with the system's current value.
1424 * TME_LEAVE doesn't need to specify hover time so use default */
1425 if (hover_time == HOVER_DEFAULT || hover_time == 0)
1426 SystemParametersInfoW(SPI_GETMOUSEHOVERTIME, 0, &hover_time, 0);
1428 GetCursorPos(&pos);
1429 hwnd = WINPOS_WindowFromPoint(ptme->hwndTrack, pos, &hittest);
1430 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
1432 if (ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT))
1433 FIXME("Unknown flag(s) %08x\n", ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT));
1435 if (ptme->dwFlags & TME_CANCEL)
1437 if (tracking_info.tme.hwndTrack == ptme->hwndTrack)
1439 tracking_info.tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
1441 /* if we aren't tracking on hover or leave remove this entry */
1442 if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
1444 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1445 timer = 0;
1446 tracking_info.tme.hwndTrack = 0;
1447 tracking_info.tme.dwFlags = 0;
1448 tracking_info.tme.dwHoverTime = 0;
1451 } else {
1452 /* In our implementation it's possible that another window will receive a
1453 * WM_MOUSEMOVE and call TrackMouseEvent before TrackMouseEventProc is
1454 * called. In such a situation post the WM_MOUSELEAVE now */
1455 if (tracking_info.tme.dwFlags & TME_LEAVE && tracking_info.tme.hwndTrack != NULL)
1456 check_mouse_leave(hwnd, hittest);
1458 if (timer)
1460 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1461 timer = 0;
1462 tracking_info.tme.hwndTrack = 0;
1463 tracking_info.tme.dwFlags = 0;
1464 tracking_info.tme.dwHoverTime = 0;
1467 if (ptme->hwndTrack == hwnd)
1469 /* Adding new mouse event to the tracking list */
1470 tracking_info.tme = *ptme;
1471 tracking_info.tme.dwHoverTime = hover_time;
1473 /* Initialize HoverInfo variables even if not hover tracking */
1474 tracking_info.pos = pos;
1476 timer = SetSystemTimer(tracking_info.tme.hwndTrack, (UINT_PTR)&tracking_info.tme, hover_time, TrackMouseEventProc);
1480 return TRUE;
1483 /***********************************************************************
1484 * GetMouseMovePointsEx [USER32]
1486 * RETURNS
1487 * Success: count of point set in the buffer
1488 * Failure: -1
1490 int WINAPI GetMouseMovePointsEx(UINT size, LPMOUSEMOVEPOINT ptin, LPMOUSEMOVEPOINT ptout, int count, DWORD res) {
1492 if((size != sizeof(MOUSEMOVEPOINT)) || (count < 0) || (count > 64)) {
1493 SetLastError(ERROR_INVALID_PARAMETER);
1494 return -1;
1497 if(!ptin || (!ptout && count)) {
1498 SetLastError(ERROR_NOACCESS);
1499 return -1;
1502 FIXME("(%d %p %p %d %d) stub\n", size, ptin, ptout, count, res);
1504 SetLastError(ERROR_POINT_NOT_FOUND);
1505 return -1;