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
26 #include "wine/port.h"
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
38 #define WIN32_NO_STATUS
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);
56 /***********************************************************************
59 static WORD get_key_state(void)
63 if (GetSystemMetrics( SM_SWAPBUTTON ))
65 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_LBUTTON;
66 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_RBUTTON;
70 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_LBUTTON;
71 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_RBUTTON;
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;
82 /**********************************************************************
85 BOOL set_capture_window( HWND hwnd, UINT gui_flags, HWND *prev_ret )
91 if (gui_flags & GUI_INMENUMODE) flags |= CAPTURE_MENU;
92 if (gui_flags & GUI_INMOVESIZE) flags |= CAPTURE_MOVESIZE;
94 SERVER_START_REQ( set_capture_window )
96 req->handle = wine_server_user_handle( hwnd );
98 if ((ret = !wine_server_call_err( req )))
100 previous = wine_server_ptr_handle( reply->previous );
101 hwnd = wine_server_ptr_handle( reply->full_handle );
108 USER_Driver->pSetCapture( hwnd, gui_flags );
110 if (previous && previous != hwnd)
111 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
113 if (prev_ret) *prev_ret = previous;
119 /***********************************************************************
120 * __wine_send_input (USER32.@)
122 * Internal SendInput function to allow the graphics driver to inject real events.
124 BOOL CDECL __wine_send_input( HWND hwnd, const INPUT *input )
126 NTSTATUS status = send_hardware_message( hwnd, input, 0 );
127 if (status) SetLastError( RtlNtStatusToDosError(status) );
132 /***********************************************************************
133 * update_mouse_coords
135 * Helper for SendInput.
137 static void update_mouse_coords( INPUT *input )
139 if (!(input->u.mi.dwFlags & MOUSEEVENTF_MOVE)) return;
141 if (input->u.mi.dwFlags & MOUSEEVENTF_ABSOLUTE)
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;
150 /* dx and dy can be negative numbers for relative movements */
151 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
153 if (!accel[2]) return;
155 if (abs(input->u.mi.dx) > accel[0])
158 if ((abs(input->u.mi.dx) > accel[1]) && (accel[2] == 2)) input->u.mi.dx *= 2;
160 if (abs(input->u.mi.dy) > accel[0])
163 if ((abs(input->u.mi.dy) > accel[1]) && (accel[2] == 2)) input->u.mi.dy *= 2;
168 /***********************************************************************
169 * SendInput (USER32.@)
171 UINT WINAPI SendInput( UINT count, LPINPUT inputs, int size )
176 for (i = 0; i < count; i++)
178 if (inputs[i].type == INPUT_MOUSE)
180 /* we need to update the coordinates to what the server expects */
181 INPUT input = inputs[i];
182 update_mouse_coords( &input );
183 status = send_hardware_message( 0, &input, SEND_HWMSG_INJECTED );
185 else status = send_hardware_message( 0, &inputs[i], SEND_HWMSG_INJECTED );
189 SetLastError( RtlNtStatusToDosError(status) );
198 /***********************************************************************
199 * keybd_event (USER32.@)
201 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
202 DWORD dwFlags, ULONG_PTR dwExtraInfo )
206 input.type = INPUT_KEYBOARD;
207 input.u.ki.wVk = bVk;
208 input.u.ki.wScan = bScan;
209 input.u.ki.dwFlags = dwFlags;
211 input.u.ki.dwExtraInfo = dwExtraInfo;
212 SendInput( 1, &input, sizeof(input) );
216 /***********************************************************************
217 * mouse_event (USER32.@)
219 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
220 DWORD dwData, ULONG_PTR dwExtraInfo )
224 input.type = INPUT_MOUSE;
227 input.u.mi.mouseData = dwData;
228 input.u.mi.dwFlags = dwFlags;
230 input.u.mi.dwExtraInfo = dwExtraInfo;
231 SendInput( 1, &input, sizeof(input) );
235 /***********************************************************************
236 * GetCursorPos (USER32.@)
238 BOOL WINAPI DECLSPEC_HOTPATCH GetCursorPos( POINT *pt )
243 if (!pt) return FALSE;
245 SERVER_START_REQ( set_cursor )
247 if ((ret = !wine_server_call( req )))
249 pt->x = reply->new_x;
250 pt->y = reply->new_y;
251 last_change = reply->last_change;
256 /* query new position from graphics driver if we haven't updated recently */
257 if (ret && GetTickCount() - last_change > 100) ret = USER_Driver->pGetCursorPos( pt );
262 /***********************************************************************
263 * GetCursorInfo (USER32.@)
265 BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
271 SERVER_START_REQ( get_thread_input )
274 if ((ret = !wine_server_call( req )))
276 pci->hCursor = wine_server_ptr_handle( reply->cursor );
277 pci->flags = (reply->show_count >= 0) ? CURSOR_SHOWING : 0;
281 GetCursorPos(&pci->ptScreenPos);
286 /***********************************************************************
287 * SetCursorPos (USER32.@)
289 BOOL WINAPI DECLSPEC_HOTPATCH SetCursorPos( INT x, INT y )
292 INT prev_x, prev_y, new_x, new_y;
294 SERVER_START_REQ( set_cursor )
296 req->flags = SET_CURSOR_POS;
299 if ((ret = !wine_server_call( req )))
301 prev_x = reply->prev_x;
302 prev_y = reply->prev_y;
303 new_x = reply->new_x;
304 new_y = reply->new_y;
308 if (ret && (prev_x != new_x || prev_y != new_y)) USER_Driver->pSetCursorPos( new_x, new_y );
313 /**********************************************************************
314 * SetCapture (USER32.@)
316 HWND WINAPI DECLSPEC_HOTPATCH SetCapture( HWND hwnd )
320 set_capture_window( hwnd, 0, &previous );
325 /**********************************************************************
326 * ReleaseCapture (USER32.@)
328 BOOL WINAPI DECLSPEC_HOTPATCH ReleaseCapture(void)
330 BOOL ret = set_capture_window( 0, 0, NULL );
332 /* Somebody may have missed some mouse movements */
333 if (ret) mouse_event( MOUSEEVENTF_MOVE, 0, 0, 0, 0 );
339 /**********************************************************************
340 * GetCapture (USER32.@)
342 HWND WINAPI GetCapture(void)
346 SERVER_START_REQ( get_thread_input )
348 req->tid = GetCurrentThreadId();
349 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->capture );
356 static void check_for_events( UINT flags )
358 if (USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, flags, 0 ) == WAIT_TIMEOUT)
359 flush_window_surfaces( TRUE );
362 /**********************************************************************
363 * GetAsyncKeyState (USER32.@)
365 * Determine if a key is or was pressed. retval has high-order
366 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
369 SHORT WINAPI DECLSPEC_HOTPATCH GetAsyncKeyState( INT key )
371 struct user_thread_info *thread_info = get_user_thread_info();
374 if (key < 0 || key >= 256) return 0;
376 check_for_events( QS_INPUT );
378 if ((ret = USER_Driver->pGetAsyncKeyState( key )) == -1)
380 if (thread_info->key_state &&
381 !(thread_info->key_state[key] & 0xc0) &&
382 GetTickCount() - thread_info->key_state_time < 50)
385 if (!thread_info->key_state) thread_info->key_state = HeapAlloc( GetProcessHeap(), 0, 256 );
388 SERVER_START_REQ( get_key_state )
392 if (thread_info->key_state) wine_server_set_reply( req, thread_info->key_state, 256 );
393 if (!wine_server_call( req ))
395 if (reply->state & 0x40) ret |= 0x0001;
396 if (reply->state & 0x80) ret |= 0x8000;
397 thread_info->key_state_time = GetTickCount();
406 /***********************************************************************
407 * GetQueueStatus (USER32.@)
409 DWORD WINAPI GetQueueStatus( UINT flags )
413 if (flags & ~(QS_ALLINPUT | QS_ALLPOSTMESSAGE | QS_SMRESULT))
415 SetLastError( ERROR_INVALID_FLAGS );
419 check_for_events( flags );
421 SERVER_START_REQ( get_queue_status )
424 wine_server_call( req );
425 ret = MAKELONG( reply->changed_bits & flags, reply->wake_bits & flags );
432 /***********************************************************************
433 * GetInputState (USER32.@)
435 BOOL WINAPI GetInputState(void)
439 check_for_events( QS_INPUT );
441 SERVER_START_REQ( get_queue_status )
444 wine_server_call( req );
445 ret = reply->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
452 /******************************************************************
453 * GetLastInputInfo (USER32.@)
455 BOOL WINAPI GetLastInputInfo(PLASTINPUTINFO plii)
461 if (plii->cbSize != sizeof (*plii) )
463 SetLastError(ERROR_INVALID_PARAMETER);
467 SERVER_START_REQ( get_last_input_time )
469 ret = !wine_server_call_err( req );
471 plii->dwTime = reply->time;
478 /******************************************************************
479 * GetRawInputDeviceList (USER32.@)
481 UINT WINAPI GetRawInputDeviceList(RAWINPUTDEVICELIST *devices, UINT *device_count, UINT size)
483 TRACE("devices %p, device_count %p, size %u.\n", devices, device_count, size);
485 if (size != sizeof(*devices) || !device_count) return ~0U;
493 if (*device_count < 2)
499 devices[0].hDevice = WINE_MOUSE_HANDLE;
500 devices[0].dwType = RIM_TYPEMOUSE;
501 devices[1].hDevice = WINE_KEYBOARD_HANDLE;
502 devices[1].dwType = RIM_TYPEKEYBOARD;
508 /******************************************************************
509 * RegisterRawInputDevices (USER32.@)
511 BOOL WINAPI DECLSPEC_HOTPATCH RegisterRawInputDevices(RAWINPUTDEVICE *devices, UINT device_count, UINT size)
513 struct rawinput_device *d;
517 TRACE("devices %p, device_count %u, size %u.\n", devices, device_count, size);
519 if (size != sizeof(*devices))
521 WARN("Invalid structure size %u.\n", size);
525 if (!(d = HeapAlloc( GetProcessHeap(), 0, device_count * sizeof(*d) ))) return FALSE;
527 for (i = 0; i < device_count; ++i)
529 TRACE("device %u: page %#x, usage %#x, flags %#x, target %p.\n",
530 i, devices[i].usUsagePage, devices[i].usUsage,
531 devices[i].dwFlags, devices[i].hwndTarget);
532 if (devices[i].dwFlags & ~RIDEV_REMOVE)
533 FIXME("Unhandled flags %#x for device %u.\n", devices[i].dwFlags, i);
535 d[i].usage_page = devices[i].usUsagePage;
536 d[i].usage = devices[i].usUsage;
537 d[i].flags = devices[i].dwFlags;
538 d[i].target = wine_server_user_handle( devices[i].hwndTarget );
541 SERVER_START_REQ( update_rawinput_devices )
543 wine_server_add_data( req, d, device_count * sizeof(*d) );
544 ret = !wine_server_call( req );
548 HeapFree( GetProcessHeap(), 0, d );
554 /******************************************************************
555 * GetRawInputData (USER32.@)
557 UINT WINAPI GetRawInputData(HRAWINPUT rawinput, UINT command, void *data, UINT *data_size, UINT header_size)
559 RAWINPUT *ri = (RAWINPUT *)rawinput;
562 TRACE("rawinput %p, command %#x, data %p, data_size %p, header_size %u.\n",
563 rawinput, command, data, data_size, header_size);
565 if (header_size != sizeof(RAWINPUTHEADER))
567 WARN("Invalid structure size %u.\n", header_size);
574 s = ri->header.dwSize;
577 s = sizeof(RAWINPUTHEADER);
589 if (*data_size < s) return ~0U;
595 /******************************************************************
596 * GetRawInputBuffer (USER32.@)
598 UINT WINAPI DECLSPEC_HOTPATCH GetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader)
600 FIXME("(pData=%p, pcbSize=%p, cbSizeHeader=%d) stub!\n", pData, pcbSize, cbSizeHeader);
606 /******************************************************************
607 * GetRawInputDeviceInfoA (USER32.@)
609 UINT WINAPI GetRawInputDeviceInfoA(HANDLE device, UINT command, void *data, UINT *data_size)
613 TRACE("device %p, command %u, data %p, data_size %p.\n", device, command, data, data_size);
615 ret = GetRawInputDeviceInfoW(device, command, data, data_size);
616 if (command == RIDI_DEVICENAME && ret && ret != ~0U)
617 ret = WideCharToMultiByte(CP_ACP, 0, data, -1, data, *data_size, NULL, NULL);
623 /******************************************************************
624 * GetRawInputDeviceInfoW (USER32.@)
626 UINT WINAPI GetRawInputDeviceInfoW(HANDLE device, UINT command, void *data, UINT *data_size)
628 /* FIXME: Most of this is made up. */
629 static const WCHAR keyboard_name[] = {'\\','\\','?','\\','W','I','N','E','_','K','E','Y','B','O','A','R','D',0};
630 static const WCHAR mouse_name[] = {'\\','\\','?','\\','W','I','N','E','_','M','O','U','S','E',0};
631 static const RID_DEVICE_INFO_KEYBOARD keyboard_info = {0, 0, 1, 12, 3, 101};
632 static const RID_DEVICE_INFO_MOUSE mouse_info = {1, 5, 0, FALSE};
633 const WCHAR *name = NULL;
634 RID_DEVICE_INFO *info;
637 TRACE("device %p, command %u, data %p, data_size %p.\n", device, command, data, data_size);
639 if (!data_size || (device != WINE_MOUSE_HANDLE && device != WINE_KEYBOARD_HANDLE)) return ~0U;
643 case RIDI_DEVICENAME:
644 if (device == WINE_MOUSE_HANDLE)
646 s = sizeof(mouse_name);
651 s = sizeof(keyboard_name);
652 name = keyboard_name;
655 case RIDI_DEVICEINFO:
674 if (command == RIDI_DEVICENAME)
676 memcpy(data, name, s);
681 info->cbSize = sizeof(*info);
682 if (device == WINE_MOUSE_HANDLE)
684 info->dwType = RIM_TYPEMOUSE;
685 info->u.mouse = mouse_info;
689 info->dwType = RIM_TYPEKEYBOARD;
690 info->u.keyboard = keyboard_info;
696 /******************************************************************
697 * GetRegisteredRawInputDevices (USER32.@)
699 UINT WINAPI GetRegisteredRawInputDevices(PRAWINPUTDEVICE pRawInputDevices, PUINT puiNumDevices, UINT cbSize)
701 FIXME("(pRawInputDevices=%p, puiNumDevices=%p, cbSize=%d) stub!\n", pRawInputDevices, puiNumDevices, cbSize);
707 /******************************************************************
708 * DefRawInputProc (USER32.@)
710 LRESULT WINAPI DefRawInputProc(PRAWINPUT *paRawInput, INT nInput, UINT cbSizeHeader)
712 FIXME("(paRawInput=%p, nInput=%d, cbSizeHeader=%d) stub!\n", *paRawInput, nInput, cbSizeHeader);
718 /**********************************************************************
719 * AttachThreadInput (USER32.@)
721 * Attaches the input processing mechanism of one thread to that of
724 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
728 SERVER_START_REQ( attach_thread_input )
730 req->tid_from = from;
732 req->attach = attach;
733 ret = !wine_server_call_err( req );
740 /**********************************************************************
741 * GetKeyState (USER32.@)
743 * An application calls the GetKeyState function in response to a
744 * keyboard-input message. This function retrieves the state of the key
745 * at the time the input message was generated.
747 SHORT WINAPI DECLSPEC_HOTPATCH GetKeyState(INT vkey)
751 SERVER_START_REQ( get_key_state )
753 req->tid = GetCurrentThreadId();
755 if (!wine_server_call( req )) retval = (signed char)reply->state;
758 TRACE("key (0x%x) -> %x\n", vkey, retval);
763 /**********************************************************************
764 * GetKeyboardState (USER32.@)
766 BOOL WINAPI DECLSPEC_HOTPATCH GetKeyboardState( LPBYTE state )
770 TRACE("(%p)\n", state);
772 memset( state, 0, 256 );
773 SERVER_START_REQ( get_key_state )
775 req->tid = GetCurrentThreadId();
777 wine_server_set_reply( req, state, 256 );
778 ret = !wine_server_call_err( req );
785 /**********************************************************************
786 * SetKeyboardState (USER32.@)
788 BOOL WINAPI SetKeyboardState( LPBYTE state )
792 SERVER_START_REQ( set_key_state )
794 req->tid = GetCurrentThreadId();
795 wine_server_add_data( req, state, 256 );
796 ret = !wine_server_call_err( req );
803 /**********************************************************************
804 * VkKeyScanA (USER32.@)
806 * VkKeyScan translates an ANSI character to a virtual-key and shift code
807 * for the current keyboard.
808 * high-order byte yields :
812 * 3-5 Shift-key combinations that are not used for characters
815 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
816 * FIXME : works ok except for dead chars :
817 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
818 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
820 SHORT WINAPI VkKeyScanA(CHAR cChar)
824 if (IsDBCSLeadByte(cChar)) return -1;
826 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
827 return VkKeyScanW(wChar);
830 /******************************************************************************
831 * VkKeyScanW (USER32.@)
833 SHORT WINAPI VkKeyScanW(WCHAR cChar)
835 return VkKeyScanExW(cChar, GetKeyboardLayout(0));
838 /**********************************************************************
839 * VkKeyScanExA (USER32.@)
841 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
845 if (IsDBCSLeadByte(cChar)) return -1;
847 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
848 return VkKeyScanExW(wChar, dwhkl);
851 /******************************************************************************
852 * VkKeyScanExW (USER32.@)
854 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
856 return USER_Driver->pVkKeyScanEx(cChar, dwhkl);
859 /**********************************************************************
860 * OemKeyScan (USER32.@)
862 DWORD WINAPI OemKeyScan(WORD wOemChar)
867 /******************************************************************************
868 * GetKeyboardType (USER32.@)
870 INT WINAPI GetKeyboardType(INT nTypeFlag)
872 TRACE_(keyboard)("(%d)\n", nTypeFlag);
875 case 0: /* Keyboard type */
876 return 4; /* AT-101 */
877 case 1: /* Keyboard Subtype */
878 return 0; /* There are no defined subtypes */
879 case 2: /* Number of F-keys */
880 return 12; /* We're doing an 101 for now, so return 12 F-keys */
882 WARN_(keyboard)("Unknown type\n");
883 return 0; /* The book says 0 here, so 0 */
887 /******************************************************************************
888 * MapVirtualKeyA (USER32.@)
890 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
892 return MapVirtualKeyExA( code, maptype, GetKeyboardLayout(0) );
895 /******************************************************************************
896 * MapVirtualKeyW (USER32.@)
898 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
900 return MapVirtualKeyExW(code, maptype, GetKeyboardLayout(0));
903 /******************************************************************************
904 * MapVirtualKeyExA (USER32.@)
906 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
910 ret = MapVirtualKeyExW( code, maptype, hkl );
911 if (maptype == MAPVK_VK_TO_CHAR)
916 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL );
922 /******************************************************************************
923 * MapVirtualKeyExW (USER32.@)
925 UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
927 TRACE_(keyboard)("(%X, %d, %p)\n", code, maptype, hkl);
929 return USER_Driver->pMapVirtualKeyEx(code, maptype, hkl);
932 /****************************************************************************
933 * GetKBCodePage (USER32.@)
935 UINT WINAPI GetKBCodePage(void)
940 /***********************************************************************
941 * GetKeyboardLayout (USER32.@)
943 * - device handle for keyboard layout defaulted to
944 * the language id. This is the way Windows default works.
945 * - the thread identifier is also ignored.
947 HKL WINAPI GetKeyboardLayout(DWORD thread_id)
949 return USER_Driver->pGetKeyboardLayout(thread_id);
952 /****************************************************************************
953 * GetKeyboardLayoutNameA (USER32.@)
955 BOOL WINAPI GetKeyboardLayoutNameA(LPSTR pszKLID)
957 WCHAR buf[KL_NAMELENGTH];
959 if (GetKeyboardLayoutNameW(buf))
960 return WideCharToMultiByte( CP_ACP, 0, buf, -1, pszKLID, KL_NAMELENGTH, NULL, NULL ) != 0;
964 /****************************************************************************
965 * GetKeyboardLayoutNameW (USER32.@)
967 BOOL WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
969 return USER_Driver->pGetKeyboardLayoutName(pwszKLID);
972 /****************************************************************************
973 * GetKeyNameTextA (USER32.@)
975 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
980 if (!nSize || !GetKeyNameTextW(lParam, buf, 256))
985 ret = WideCharToMultiByte(CP_ACP, 0, buf, -1, lpBuffer, nSize, NULL, NULL);
996 /****************************************************************************
997 * GetKeyNameTextW (USER32.@)
999 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
1001 if (!lpBuffer || !nSize) return 0;
1002 return USER_Driver->pGetKeyNameText( lParam, lpBuffer, nSize );
1005 /****************************************************************************
1006 * ToUnicode (USER32.@)
1008 INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
1009 LPWSTR lpwStr, int size, UINT flags)
1011 return ToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, GetKeyboardLayout(0));
1014 /****************************************************************************
1015 * ToUnicodeEx (USER32.@)
1017 INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
1018 LPWSTR lpwStr, int size, UINT flags, HKL hkl)
1020 return USER_Driver->pToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, hkl);
1023 /****************************************************************************
1024 * ToAscii (USER32.@)
1026 INT WINAPI ToAscii( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
1027 LPWORD lpChar, UINT flags )
1029 return ToAsciiEx(virtKey, scanCode, lpKeyState, lpChar, flags, GetKeyboardLayout(0));
1032 /****************************************************************************
1033 * ToAsciiEx (USER32.@)
1035 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
1036 LPWORD lpChar, UINT flags, HKL dwhkl )
1041 ret = ToUnicodeEx(virtKey, scanCode, lpKeyState, uni_chars, 2, flags, dwhkl);
1042 if (ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
1044 WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
1048 /**********************************************************************
1049 * ActivateKeyboardLayout (USER32.@)
1051 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
1053 TRACE_(keyboard)("(%p, %d)\n", hLayout, flags);
1055 return USER_Driver->pActivateKeyboardLayout(hLayout, flags);
1058 /**********************************************************************
1059 * BlockInput (USER32.@)
1061 BOOL WINAPI BlockInput(BOOL fBlockIt)
1063 FIXME_(keyboard)("(%d): stub\n", fBlockIt);
1064 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1069 /***********************************************************************
1070 * GetKeyboardLayoutList (USER32.@)
1072 * Return number of values available if either input parm is
1073 * 0, per MS documentation.
1075 UINT WINAPI GetKeyboardLayoutList(INT nBuff, HKL *layouts)
1080 ULONG_PTR baselayout;
1082 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};
1084 TRACE_(keyboard)("(%d,%p)\n",nBuff,layouts);
1086 baselayout = GetUserDefaultLCID();
1087 langid = PRIMARYLANGID(LANGIDFROMLCID(baselayout));
1088 if (langid == LANG_CHINESE || langid == LANG_JAPANESE || langid == LANG_KOREAN)
1089 baselayout |= 0xe001 << 16; /* IME */
1091 baselayout |= baselayout << 16;
1093 /* Enumerate the Registry */
1094 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,szKeyboardReg,&hKeyKeyboard);
1095 if (rc == ERROR_SUCCESS)
1100 rc = RegEnumKeyW(hKeyKeyboard, count, szKeyName, 9);
1101 if (rc == ERROR_SUCCESS)
1103 layout = (HKL)(ULONG_PTR)strtoulW(szKeyName,NULL,16);
1104 if (baselayout != 0 && layout == (HKL)baselayout)
1105 baselayout = 0; /* found in the registry do not add again */
1106 if (nBuff && layouts)
1108 if (count >= nBuff ) break;
1109 layouts[count] = layout;
1113 } while (rc == ERROR_SUCCESS);
1114 RegCloseKey(hKeyKeyboard);
1117 /* make sure our base layout is on the list */
1118 if (baselayout != 0)
1120 if (nBuff && layouts)
1124 layouts[count] = (HKL)baselayout;
1136 /***********************************************************************
1137 * RegisterHotKey (USER32.@)
1139 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk)
1144 TRACE_(keyboard)("(%p,%d,0x%08x,%X)\n",hwnd,id,modifiers,vk);
1146 if ((hwnd == NULL || WIN_IsCurrentThread(hwnd)) &&
1147 !USER_Driver->pRegisterHotKey(hwnd, modifiers, vk))
1150 SERVER_START_REQ( register_hotkey )
1152 req->window = wine_server_user_handle( hwnd );
1154 req->flags = modifiers;
1156 if ((ret = !wine_server_call_err( req )))
1158 replaced = reply->replaced;
1159 modifiers = reply->flags;
1165 if (ret && replaced)
1166 USER_Driver->pUnregisterHotKey(hwnd, modifiers, vk);
1171 /***********************************************************************
1172 * UnregisterHotKey (USER32.@)
1174 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id)
1179 TRACE_(keyboard)("(%p,%d)\n",hwnd,id);
1181 SERVER_START_REQ( unregister_hotkey )
1183 req->window = wine_server_user_handle( hwnd );
1185 if ((ret = !wine_server_call_err( req )))
1187 modifiers = reply->flags;
1194 USER_Driver->pUnregisterHotKey(hwnd, modifiers, vk);
1199 /***********************************************************************
1200 * LoadKeyboardLayoutW (USER32.@)
1202 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
1204 TRACE_(keyboard)("(%s, %d)\n", debugstr_w(pwszKLID), Flags);
1206 return USER_Driver->pLoadKeyboardLayout(pwszKLID, Flags);
1209 /***********************************************************************
1210 * LoadKeyboardLayoutA (USER32.@)
1212 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
1215 UNICODE_STRING pwszKLIDW;
1217 if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
1218 else pwszKLIDW.Buffer = NULL;
1220 ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
1221 RtlFreeUnicodeString(&pwszKLIDW);
1226 /***********************************************************************
1227 * UnloadKeyboardLayout (USER32.@)
1229 BOOL WINAPI UnloadKeyboardLayout(HKL hkl)
1231 TRACE_(keyboard)("(%p)\n", hkl);
1233 return USER_Driver->pUnloadKeyboardLayout(hkl);
1236 typedef struct __TRACKINGLIST {
1237 TRACKMOUSEEVENT tme;
1238 POINT pos; /* center of hover rectangle */
1241 /* FIXME: move tracking stuff into a per thread data */
1242 static _TRACKINGLIST tracking_info;
1243 static UINT_PTR timer;
1245 static void check_mouse_leave(HWND hwnd, int hittest)
1247 if (tracking_info.tme.hwndTrack != hwnd)
1249 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1250 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
1252 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
1254 /* remove the TME_LEAVE flag */
1255 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1259 if (hittest == HTCLIENT)
1261 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1263 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
1264 /* remove the TME_LEAVE flag */
1265 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1270 if (!(tracking_info.tme.dwFlags & TME_NONCLIENT))
1272 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
1273 /* remove the TME_LEAVE flag */
1274 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1280 static void CALLBACK TrackMouseEventProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent,
1284 INT hoverwidth = 0, hoverheight = 0, hittest;
1286 TRACE("hwnd %p, msg %04x, id %04lx, time %u\n", hwnd, uMsg, idEvent, dwTime);
1289 hwnd = WINPOS_WindowFromPoint(hwnd, pos, &hittest);
1291 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
1293 SystemParametersInfoW(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
1294 SystemParametersInfoW(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
1296 TRACE("tracked pos %s, current pos %s, hover width %d, hover height %d\n",
1297 wine_dbgstr_point(&tracking_info.pos), wine_dbgstr_point(&pos),
1298 hoverwidth, hoverheight);
1300 /* see if this tracking event is looking for TME_LEAVE and that the */
1301 /* mouse has left the window */
1302 if (tracking_info.tme.dwFlags & TME_LEAVE)
1304 check_mouse_leave(hwnd, hittest);
1307 if (tracking_info.tme.hwndTrack != hwnd)
1309 /* mouse is gone, stop tracking mouse hover */
1310 tracking_info.tme.dwFlags &= ~TME_HOVER;
1313 /* see if we are tracking hovering for this hwnd */
1314 if (tracking_info.tme.dwFlags & TME_HOVER)
1316 /* has the cursor moved outside the rectangle centered around pos? */
1317 if ((abs(pos.x - tracking_info.pos.x) > (hoverwidth / 2)) ||
1318 (abs(pos.y - tracking_info.pos.y) > (hoverheight / 2)))
1320 /* record this new position as the current position */
1321 tracking_info.pos = pos;
1325 if (hittest == HTCLIENT)
1327 ScreenToClient(hwnd, &pos);
1328 TRACE("client cursor pos %s\n", wine_dbgstr_point(&pos));
1330 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSEHOVER,
1331 get_key_state(), MAKELPARAM( pos.x, pos.y ));
1335 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1336 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSEHOVER,
1337 hittest, MAKELPARAM( pos.x, pos.y ));
1340 /* stop tracking mouse hover */
1341 tracking_info.tme.dwFlags &= ~TME_HOVER;
1345 /* stop the timer if the tracking list is empty */
1346 if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
1348 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1350 tracking_info.tme.hwndTrack = 0;
1351 tracking_info.tme.dwFlags = 0;
1352 tracking_info.tme.dwHoverTime = 0;
1357 /***********************************************************************
1358 * TrackMouseEvent [USER32]
1360 * Requests notification of mouse events
1362 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
1363 * to the hwnd specified in the ptme structure. After the event message
1364 * is posted to the hwnd, the entry in the queue is removed.
1366 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
1367 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
1368 * immediately and the TME_LEAVE flag being ignored.
1371 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
1380 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
1387 TRACE("%x, %x, %p, %u\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
1389 if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
1390 WARN("wrong TRACKMOUSEEVENT size from app\n");
1391 SetLastError(ERROR_INVALID_PARAMETER);
1395 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
1396 if (ptme->dwFlags & TME_QUERY )
1398 *ptme = tracking_info.tme;
1399 /* set cbSize in the case it's not initialized yet */
1400 ptme->cbSize = sizeof(TRACKMOUSEEVENT);
1402 return TRUE; /* return here, TME_QUERY is retrieving information */
1405 if (!IsWindow(ptme->hwndTrack))
1407 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
1411 hover_time = (ptme->dwFlags & TME_HOVER) ? ptme->dwHoverTime : HOVER_DEFAULT;
1413 /* if HOVER_DEFAULT was specified replace this with the system's current value.
1414 * TME_LEAVE doesn't need to specify hover time so use default */
1415 if (hover_time == HOVER_DEFAULT || hover_time == 0)
1416 SystemParametersInfoW(SPI_GETMOUSEHOVERTIME, 0, &hover_time, 0);
1419 hwnd = WINPOS_WindowFromPoint(ptme->hwndTrack, pos, &hittest);
1420 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
1422 if (ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT))
1423 FIXME("Unknown flag(s) %08x\n", ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT));
1425 if (ptme->dwFlags & TME_CANCEL)
1427 if (tracking_info.tme.hwndTrack == ptme->hwndTrack)
1429 tracking_info.tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
1431 /* if we aren't tracking on hover or leave remove this entry */
1432 if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
1434 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1436 tracking_info.tme.hwndTrack = 0;
1437 tracking_info.tme.dwFlags = 0;
1438 tracking_info.tme.dwHoverTime = 0;
1442 /* In our implementation it's possible that another window will receive a
1443 * WM_MOUSEMOVE and call TrackMouseEvent before TrackMouseEventProc is
1444 * called. In such a situation post the WM_MOUSELEAVE now */
1445 if (tracking_info.tme.dwFlags & TME_LEAVE && tracking_info.tme.hwndTrack != NULL)
1446 check_mouse_leave(hwnd, hittest);
1450 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1452 tracking_info.tme.hwndTrack = 0;
1453 tracking_info.tme.dwFlags = 0;
1454 tracking_info.tme.dwHoverTime = 0;
1457 if (ptme->hwndTrack == hwnd)
1459 /* Adding new mouse event to the tracking list */
1460 tracking_info.tme = *ptme;
1461 tracking_info.tme.dwHoverTime = hover_time;
1463 /* Initialize HoverInfo variables even if not hover tracking */
1464 tracking_info.pos = pos;
1466 timer = SetSystemTimer(tracking_info.tme.hwndTrack, (UINT_PTR)&tracking_info.tme, hover_time, TrackMouseEventProc);
1473 /***********************************************************************
1474 * GetMouseMovePointsEx [USER32]
1477 * Success: count of point set in the buffer
1480 int WINAPI GetMouseMovePointsEx(UINT size, LPMOUSEMOVEPOINT ptin, LPMOUSEMOVEPOINT ptout, int count, DWORD res) {
1482 if((size != sizeof(MOUSEMOVEPOINT)) || (count < 0) || (count > 64)) {
1483 SetLastError(ERROR_INVALID_PARAMETER);
1487 if(!ptin || (!ptout && count)) {
1488 SetLastError(ERROR_NOACCESS);
1492 FIXME("(%d %p %p %d %d) stub\n", size, ptin, ptout, count, res);
1494 SetLastError(ERROR_POINT_NOT_FOUND);