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(PRAWINPUTDEVICELIST pRawInputDeviceList, PUINT puiNumDevices, UINT cbSize)
483 FIXME("(pRawInputDeviceList=%p, puiNumDevices=%p, cbSize=%d) stub!\n", pRawInputDeviceList, puiNumDevices, cbSize);
485 if(pRawInputDeviceList)
486 memset(pRawInputDeviceList, 0, sizeof *pRawInputDeviceList);
492 /******************************************************************
493 * RegisterRawInputDevices (USER32.@)
495 BOOL WINAPI DECLSPEC_HOTPATCH RegisterRawInputDevices(RAWINPUTDEVICE *devices, UINT device_count, UINT size)
497 struct rawinput_device *d;
501 TRACE("devices %p, device_count %u, size %u.\n", devices, device_count, size);
503 if (size != sizeof(*devices))
505 WARN("Invalid structure size %u.\n", size);
509 if (!(d = HeapAlloc( GetProcessHeap(), 0, device_count * sizeof(*d) ))) return FALSE;
511 for (i = 0; i < device_count; ++i)
513 TRACE("device %u: page %#x, usage %#x, flags %#x, target %p.\n",
514 i, devices[i].usUsagePage, devices[i].usUsage,
515 devices[i].dwFlags, devices[i].hwndTarget);
516 if (devices[i].dwFlags & ~RIDEV_REMOVE)
517 FIXME("Unhandled flags %#x for device %u.\n", devices[i].dwFlags, i);
519 d[i].usage_page = devices[i].usUsagePage;
520 d[i].usage = devices[i].usUsage;
521 d[i].flags = devices[i].dwFlags;
522 d[i].target = wine_server_user_handle( devices[i].hwndTarget );
525 SERVER_START_REQ( update_rawinput_devices )
527 wine_server_add_data( req, d, device_count * sizeof(*d) );
528 ret = !wine_server_call( req );
532 HeapFree( GetProcessHeap(), 0, d );
538 /******************************************************************
539 * GetRawInputData (USER32.@)
541 UINT WINAPI GetRawInputData(HRAWINPUT rawinput, UINT command, void *data, UINT *data_size, UINT header_size)
543 RAWINPUT *ri = (RAWINPUT *)rawinput;
546 TRACE("rawinput %p, command %#x, data %p, data_size %p, header_size %u.\n",
547 rawinput, command, data, data_size, header_size);
549 if (header_size != sizeof(RAWINPUTHEADER))
551 WARN("Invalid structure size %u.\n", header_size);
558 s = ri->header.dwSize;
561 s = sizeof(RAWINPUTHEADER);
573 if (*data_size < s) return ~0U;
579 /******************************************************************
580 * GetRawInputBuffer (USER32.@)
582 UINT WINAPI DECLSPEC_HOTPATCH GetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader)
584 FIXME("(pData=%p, pcbSize=%p, cbSizeHeader=%d) stub!\n", pData, pcbSize, cbSizeHeader);
590 /******************************************************************
591 * GetRawInputDeviceInfoA (USER32.@)
593 UINT WINAPI GetRawInputDeviceInfoA(HANDLE hDevice, UINT uiCommand, LPVOID pData, PUINT pcbSize)
595 FIXME("(hDevice=%p, uiCommand=%d, pData=%p, pcbSize=%p) stub!\n", hDevice, uiCommand, pData, pcbSize);
601 /******************************************************************
602 * GetRawInputDeviceInfoW (USER32.@)
604 UINT WINAPI GetRawInputDeviceInfoW(HANDLE hDevice, UINT uiCommand, LPVOID pData, PUINT pcbSize)
606 FIXME("(hDevice=%p, uiCommand=%d, pData=%p, pcbSize=%p) stub!\n", hDevice, uiCommand, pData, pcbSize);
612 /******************************************************************
613 * GetRegisteredRawInputDevices (USER32.@)
615 UINT WINAPI GetRegisteredRawInputDevices(PRAWINPUTDEVICE pRawInputDevices, PUINT puiNumDevices, UINT cbSize)
617 FIXME("(pRawInputDevices=%p, puiNumDevices=%p, cbSize=%d) stub!\n", pRawInputDevices, puiNumDevices, cbSize);
623 /******************************************************************
624 * DefRawInputProc (USER32.@)
626 LRESULT WINAPI DefRawInputProc(PRAWINPUT *paRawInput, INT nInput, UINT cbSizeHeader)
628 FIXME("(paRawInput=%p, nInput=%d, cbSizeHeader=%d) stub!\n", *paRawInput, nInput, cbSizeHeader);
634 /**********************************************************************
635 * AttachThreadInput (USER32.@)
637 * Attaches the input processing mechanism of one thread to that of
640 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
644 SERVER_START_REQ( attach_thread_input )
646 req->tid_from = from;
648 req->attach = attach;
649 ret = !wine_server_call_err( req );
656 /**********************************************************************
657 * GetKeyState (USER32.@)
659 * An application calls the GetKeyState function in response to a
660 * keyboard-input message. This function retrieves the state of the key
661 * at the time the input message was generated.
663 SHORT WINAPI DECLSPEC_HOTPATCH GetKeyState(INT vkey)
667 SERVER_START_REQ( get_key_state )
669 req->tid = GetCurrentThreadId();
671 if (!wine_server_call( req )) retval = (signed char)reply->state;
674 TRACE("key (0x%x) -> %x\n", vkey, retval);
679 /**********************************************************************
680 * GetKeyboardState (USER32.@)
682 BOOL WINAPI DECLSPEC_HOTPATCH GetKeyboardState( LPBYTE state )
686 TRACE("(%p)\n", state);
688 memset( state, 0, 256 );
689 SERVER_START_REQ( get_key_state )
691 req->tid = GetCurrentThreadId();
693 wine_server_set_reply( req, state, 256 );
694 ret = !wine_server_call_err( req );
701 /**********************************************************************
702 * SetKeyboardState (USER32.@)
704 BOOL WINAPI SetKeyboardState( LPBYTE state )
708 SERVER_START_REQ( set_key_state )
710 req->tid = GetCurrentThreadId();
711 wine_server_add_data( req, state, 256 );
712 ret = !wine_server_call_err( req );
719 /**********************************************************************
720 * VkKeyScanA (USER32.@)
722 * VkKeyScan translates an ANSI character to a virtual-key and shift code
723 * for the current keyboard.
724 * high-order byte yields :
728 * 3-5 Shift-key combinations that are not used for characters
731 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
732 * FIXME : works ok except for dead chars :
733 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
734 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
736 SHORT WINAPI VkKeyScanA(CHAR cChar)
740 if (IsDBCSLeadByte(cChar)) return -1;
742 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
743 return VkKeyScanW(wChar);
746 /******************************************************************************
747 * VkKeyScanW (USER32.@)
749 SHORT WINAPI VkKeyScanW(WCHAR cChar)
751 return VkKeyScanExW(cChar, GetKeyboardLayout(0));
754 /**********************************************************************
755 * VkKeyScanExA (USER32.@)
757 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
761 if (IsDBCSLeadByte(cChar)) return -1;
763 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
764 return VkKeyScanExW(wChar, dwhkl);
767 /******************************************************************************
768 * VkKeyScanExW (USER32.@)
770 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
772 return USER_Driver->pVkKeyScanEx(cChar, dwhkl);
775 /**********************************************************************
776 * OemKeyScan (USER32.@)
778 DWORD WINAPI OemKeyScan(WORD wOemChar)
783 /******************************************************************************
784 * GetKeyboardType (USER32.@)
786 INT WINAPI GetKeyboardType(INT nTypeFlag)
788 TRACE_(keyboard)("(%d)\n", nTypeFlag);
791 case 0: /* Keyboard type */
792 return 4; /* AT-101 */
793 case 1: /* Keyboard Subtype */
794 return 0; /* There are no defined subtypes */
795 case 2: /* Number of F-keys */
796 return 12; /* We're doing an 101 for now, so return 12 F-keys */
798 WARN_(keyboard)("Unknown type\n");
799 return 0; /* The book says 0 here, so 0 */
803 /******************************************************************************
804 * MapVirtualKeyA (USER32.@)
806 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
808 return MapVirtualKeyExA( code, maptype, GetKeyboardLayout(0) );
811 /******************************************************************************
812 * MapVirtualKeyW (USER32.@)
814 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
816 return MapVirtualKeyExW(code, maptype, GetKeyboardLayout(0));
819 /******************************************************************************
820 * MapVirtualKeyExA (USER32.@)
822 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
826 ret = MapVirtualKeyExW( code, maptype, hkl );
827 if (maptype == MAPVK_VK_TO_CHAR)
832 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL );
838 /******************************************************************************
839 * MapVirtualKeyExW (USER32.@)
841 UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
843 TRACE_(keyboard)("(%X, %d, %p)\n", code, maptype, hkl);
845 return USER_Driver->pMapVirtualKeyEx(code, maptype, hkl);
848 /****************************************************************************
849 * GetKBCodePage (USER32.@)
851 UINT WINAPI GetKBCodePage(void)
856 /***********************************************************************
857 * GetKeyboardLayout (USER32.@)
859 * - device handle for keyboard layout defaulted to
860 * the language id. This is the way Windows default works.
861 * - the thread identifier is also ignored.
863 HKL WINAPI GetKeyboardLayout(DWORD thread_id)
865 return USER_Driver->pGetKeyboardLayout(thread_id);
868 /****************************************************************************
869 * GetKeyboardLayoutNameA (USER32.@)
871 BOOL WINAPI GetKeyboardLayoutNameA(LPSTR pszKLID)
873 WCHAR buf[KL_NAMELENGTH];
875 if (GetKeyboardLayoutNameW(buf))
876 return WideCharToMultiByte( CP_ACP, 0, buf, -1, pszKLID, KL_NAMELENGTH, NULL, NULL ) != 0;
880 /****************************************************************************
881 * GetKeyboardLayoutNameW (USER32.@)
883 BOOL WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
885 return USER_Driver->pGetKeyboardLayoutName(pwszKLID);
888 /****************************************************************************
889 * GetKeyNameTextA (USER32.@)
891 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
896 if (!nSize || !GetKeyNameTextW(lParam, buf, 256))
901 ret = WideCharToMultiByte(CP_ACP, 0, buf, -1, lpBuffer, nSize, NULL, NULL);
912 /****************************************************************************
913 * GetKeyNameTextW (USER32.@)
915 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
917 if (!lpBuffer || !nSize) return 0;
918 return USER_Driver->pGetKeyNameText( lParam, lpBuffer, nSize );
921 /****************************************************************************
922 * ToUnicode (USER32.@)
924 INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
925 LPWSTR lpwStr, int size, UINT flags)
927 return ToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, GetKeyboardLayout(0));
930 /****************************************************************************
931 * ToUnicodeEx (USER32.@)
933 INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
934 LPWSTR lpwStr, int size, UINT flags, HKL hkl)
936 return USER_Driver->pToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, hkl);
939 /****************************************************************************
942 INT WINAPI ToAscii( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
943 LPWORD lpChar, UINT flags )
945 return ToAsciiEx(virtKey, scanCode, lpKeyState, lpChar, flags, GetKeyboardLayout(0));
948 /****************************************************************************
949 * ToAsciiEx (USER32.@)
951 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
952 LPWORD lpChar, UINT flags, HKL dwhkl )
957 ret = ToUnicodeEx(virtKey, scanCode, lpKeyState, uni_chars, 2, flags, dwhkl);
958 if (ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
960 WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
964 /**********************************************************************
965 * ActivateKeyboardLayout (USER32.@)
967 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
969 TRACE_(keyboard)("(%p, %d)\n", hLayout, flags);
971 return USER_Driver->pActivateKeyboardLayout(hLayout, flags);
974 /**********************************************************************
975 * BlockInput (USER32.@)
977 BOOL WINAPI BlockInput(BOOL fBlockIt)
979 FIXME_(keyboard)("(%d): stub\n", fBlockIt);
980 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
985 /***********************************************************************
986 * GetKeyboardLayoutList (USER32.@)
988 * Return number of values available if either input parm is
989 * 0, per MS documentation.
991 UINT WINAPI GetKeyboardLayoutList(INT nBuff, HKL *layouts)
996 ULONG_PTR baselayout;
998 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};
1000 TRACE_(keyboard)("(%d,%p)\n",nBuff,layouts);
1002 baselayout = GetUserDefaultLCID();
1003 langid = PRIMARYLANGID(LANGIDFROMLCID(baselayout));
1004 if (langid == LANG_CHINESE || langid == LANG_JAPANESE || langid == LANG_KOREAN)
1005 baselayout |= 0xe001 << 16; /* IME */
1007 baselayout |= baselayout << 16;
1009 /* Enumerate the Registry */
1010 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,szKeyboardReg,&hKeyKeyboard);
1011 if (rc == ERROR_SUCCESS)
1016 rc = RegEnumKeyW(hKeyKeyboard, count, szKeyName, 9);
1017 if (rc == ERROR_SUCCESS)
1019 layout = (HKL)(ULONG_PTR)strtoulW(szKeyName,NULL,16);
1020 if (baselayout != 0 && layout == (HKL)baselayout)
1021 baselayout = 0; /* found in the registry do not add again */
1022 if (nBuff && layouts)
1024 if (count >= nBuff ) break;
1025 layouts[count] = layout;
1029 } while (rc == ERROR_SUCCESS);
1030 RegCloseKey(hKeyKeyboard);
1033 /* make sure our base layout is on the list */
1034 if (baselayout != 0)
1036 if (nBuff && layouts)
1040 layouts[count] = (HKL)baselayout;
1052 /***********************************************************************
1053 * RegisterHotKey (USER32.@)
1055 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk)
1060 TRACE_(keyboard)("(%p,%d,0x%08x,%X)\n",hwnd,id,modifiers,vk);
1062 if ((hwnd == NULL || WIN_IsCurrentThread(hwnd)) &&
1063 !USER_Driver->pRegisterHotKey(hwnd, modifiers, vk))
1066 SERVER_START_REQ( register_hotkey )
1068 req->window = wine_server_user_handle( hwnd );
1070 req->flags = modifiers;
1072 if ((ret = !wine_server_call_err( req )))
1074 replaced = reply->replaced;
1075 modifiers = reply->flags;
1081 if (ret && replaced)
1082 USER_Driver->pUnregisterHotKey(hwnd, modifiers, vk);
1087 /***********************************************************************
1088 * UnregisterHotKey (USER32.@)
1090 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id)
1095 TRACE_(keyboard)("(%p,%d)\n",hwnd,id);
1097 SERVER_START_REQ( unregister_hotkey )
1099 req->window = wine_server_user_handle( hwnd );
1101 if ((ret = !wine_server_call_err( req )))
1103 modifiers = reply->flags;
1110 USER_Driver->pUnregisterHotKey(hwnd, modifiers, vk);
1115 /***********************************************************************
1116 * LoadKeyboardLayoutW (USER32.@)
1118 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
1120 TRACE_(keyboard)("(%s, %d)\n", debugstr_w(pwszKLID), Flags);
1122 return USER_Driver->pLoadKeyboardLayout(pwszKLID, Flags);
1125 /***********************************************************************
1126 * LoadKeyboardLayoutA (USER32.@)
1128 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
1131 UNICODE_STRING pwszKLIDW;
1133 if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
1134 else pwszKLIDW.Buffer = NULL;
1136 ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
1137 RtlFreeUnicodeString(&pwszKLIDW);
1142 /***********************************************************************
1143 * UnloadKeyboardLayout (USER32.@)
1145 BOOL WINAPI UnloadKeyboardLayout(HKL hkl)
1147 TRACE_(keyboard)("(%p)\n", hkl);
1149 return USER_Driver->pUnloadKeyboardLayout(hkl);
1152 typedef struct __TRACKINGLIST {
1153 TRACKMOUSEEVENT tme;
1154 POINT pos; /* center of hover rectangle */
1157 /* FIXME: move tracking stuff into a per thread data */
1158 static _TRACKINGLIST tracking_info;
1159 static UINT_PTR timer;
1161 static void check_mouse_leave(HWND hwnd, int hittest)
1163 if (tracking_info.tme.hwndTrack != hwnd)
1165 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1166 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
1168 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
1170 /* remove the TME_LEAVE flag */
1171 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1175 if (hittest == HTCLIENT)
1177 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1179 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
1180 /* remove the TME_LEAVE flag */
1181 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1186 if (!(tracking_info.tme.dwFlags & TME_NONCLIENT))
1188 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
1189 /* remove the TME_LEAVE flag */
1190 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1196 static void CALLBACK TrackMouseEventProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent,
1200 INT hoverwidth = 0, hoverheight = 0, hittest;
1202 TRACE("hwnd %p, msg %04x, id %04lx, time %u\n", hwnd, uMsg, idEvent, dwTime);
1205 hwnd = WINPOS_WindowFromPoint(hwnd, pos, &hittest);
1207 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
1209 SystemParametersInfoW(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
1210 SystemParametersInfoW(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
1212 TRACE("tracked pos %s, current pos %s, hover width %d, hover height %d\n",
1213 wine_dbgstr_point(&tracking_info.pos), wine_dbgstr_point(&pos),
1214 hoverwidth, hoverheight);
1216 /* see if this tracking event is looking for TME_LEAVE and that the */
1217 /* mouse has left the window */
1218 if (tracking_info.tme.dwFlags & TME_LEAVE)
1220 check_mouse_leave(hwnd, hittest);
1223 if (tracking_info.tme.hwndTrack != hwnd)
1225 /* mouse is gone, stop tracking mouse hover */
1226 tracking_info.tme.dwFlags &= ~TME_HOVER;
1229 /* see if we are tracking hovering for this hwnd */
1230 if (tracking_info.tme.dwFlags & TME_HOVER)
1232 /* has the cursor moved outside the rectangle centered around pos? */
1233 if ((abs(pos.x - tracking_info.pos.x) > (hoverwidth / 2)) ||
1234 (abs(pos.y - tracking_info.pos.y) > (hoverheight / 2)))
1236 /* record this new position as the current position */
1237 tracking_info.pos = pos;
1241 if (hittest == HTCLIENT)
1243 ScreenToClient(hwnd, &pos);
1244 TRACE("client cursor pos %s\n", wine_dbgstr_point(&pos));
1246 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSEHOVER,
1247 get_key_state(), MAKELPARAM( pos.x, pos.y ));
1251 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1252 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSEHOVER,
1253 hittest, MAKELPARAM( pos.x, pos.y ));
1256 /* stop tracking mouse hover */
1257 tracking_info.tme.dwFlags &= ~TME_HOVER;
1261 /* stop the timer if the tracking list is empty */
1262 if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
1264 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1266 tracking_info.tme.hwndTrack = 0;
1267 tracking_info.tme.dwFlags = 0;
1268 tracking_info.tme.dwHoverTime = 0;
1273 /***********************************************************************
1274 * TrackMouseEvent [USER32]
1276 * Requests notification of mouse events
1278 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
1279 * to the hwnd specified in the ptme structure. After the event message
1280 * is posted to the hwnd, the entry in the queue is removed.
1282 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
1283 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
1284 * immediately and the TME_LEAVE flag being ignored.
1287 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
1296 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
1303 TRACE("%x, %x, %p, %u\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
1305 if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
1306 WARN("wrong TRACKMOUSEEVENT size from app\n");
1307 SetLastError(ERROR_INVALID_PARAMETER);
1311 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
1312 if (ptme->dwFlags & TME_QUERY )
1314 *ptme = tracking_info.tme;
1315 /* set cbSize in the case it's not initialized yet */
1316 ptme->cbSize = sizeof(TRACKMOUSEEVENT);
1318 return TRUE; /* return here, TME_QUERY is retrieving information */
1321 if (!IsWindow(ptme->hwndTrack))
1323 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
1327 hover_time = (ptme->dwFlags & TME_HOVER) ? ptme->dwHoverTime : HOVER_DEFAULT;
1329 /* if HOVER_DEFAULT was specified replace this with the system's current value.
1330 * TME_LEAVE doesn't need to specify hover time so use default */
1331 if (hover_time == HOVER_DEFAULT || hover_time == 0)
1332 SystemParametersInfoW(SPI_GETMOUSEHOVERTIME, 0, &hover_time, 0);
1335 hwnd = WINPOS_WindowFromPoint(ptme->hwndTrack, pos, &hittest);
1336 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
1338 if (ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT))
1339 FIXME("Unknown flag(s) %08x\n", ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT));
1341 if (ptme->dwFlags & TME_CANCEL)
1343 if (tracking_info.tme.hwndTrack == ptme->hwndTrack)
1345 tracking_info.tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
1347 /* if we aren't tracking on hover or leave remove this entry */
1348 if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
1350 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1352 tracking_info.tme.hwndTrack = 0;
1353 tracking_info.tme.dwFlags = 0;
1354 tracking_info.tme.dwHoverTime = 0;
1358 /* In our implementation it's possible that another window will receive a
1359 * WM_MOUSEMOVE and call TrackMouseEvent before TrackMouseEventProc is
1360 * called. In such a situation post the WM_MOUSELEAVE now */
1361 if (tracking_info.tme.dwFlags & TME_LEAVE && tracking_info.tme.hwndTrack != NULL)
1362 check_mouse_leave(hwnd, hittest);
1366 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1368 tracking_info.tme.hwndTrack = 0;
1369 tracking_info.tme.dwFlags = 0;
1370 tracking_info.tme.dwHoverTime = 0;
1373 if (ptme->hwndTrack == hwnd)
1375 /* Adding new mouse event to the tracking list */
1376 tracking_info.tme = *ptme;
1377 tracking_info.tme.dwHoverTime = hover_time;
1379 /* Initialize HoverInfo variables even if not hover tracking */
1380 tracking_info.pos = pos;
1382 timer = SetSystemTimer(tracking_info.tme.hwndTrack, (UINT_PTR)&tracking_info.tme, hover_time, TrackMouseEventProc);
1389 /***********************************************************************
1390 * GetMouseMovePointsEx [USER32]
1393 * Success: count of point set in the buffer
1396 int WINAPI GetMouseMovePointsEx(UINT size, LPMOUSEMOVEPOINT ptin, LPMOUSEMOVEPOINT ptout, int count, DWORD res) {
1398 if((size != sizeof(MOUSEMOVEPOINT)) || (count < 0) || (count > 64)) {
1399 SetLastError(ERROR_INVALID_PARAMETER);
1403 if(!ptin || (!ptout && count)) {
1404 SetLastError(ERROR_NOACCESS);
1408 FIXME("(%d %p %p %d %d) stub\n", size, ptin, ptout, count, res);
1410 SetLastError(ERROR_POINT_NOT_FOUND);