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
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
);
55 INT global_key_state_counter
= 0;
57 /***********************************************************************
60 static WORD
get_key_state(void)
64 if (GetSystemMetrics( SM_SWAPBUTTON
))
66 if (GetAsyncKeyState(VK_RBUTTON
) & 0x80) ret
|= MK_LBUTTON
;
67 if (GetAsyncKeyState(VK_LBUTTON
) & 0x80) ret
|= MK_RBUTTON
;
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
;
83 /**********************************************************************
86 BOOL
set_capture_window( HWND hwnd
, UINT gui_flags
, HWND
*prev_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
);
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
);
109 USER_Driver
->pSetCapture( hwnd
, gui_flags
);
112 SendMessageW( previous
, WM_CAPTURECHANGED
, 0, (LPARAM
)hwnd
);
114 if (prev_ret
) *prev_ret
= previous
;
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
) );
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;
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])
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])
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
)
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
);
190 SetLastError( RtlNtStatusToDosError(status
) );
199 /***********************************************************************
200 * keybd_event (USER32.@)
202 void WINAPI
keybd_event( BYTE bVk
, BYTE bScan
,
203 DWORD dwFlags
, ULONG_PTR dwExtraInfo
)
207 input
.type
= INPUT_KEYBOARD
;
208 input
.u
.ki
.wVk
= bVk
;
209 input
.u
.ki
.wScan
= bScan
;
210 input
.u
.ki
.dwFlags
= dwFlags
;
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
)
225 input
.type
= INPUT_MOUSE
;
228 input
.u
.mi
.mouseData
= dwData
;
229 input
.u
.mi
.dwFlags
= dwFlags
;
231 input
.u
.mi
.dwExtraInfo
= dwExtraInfo
;
232 SendInput( 1, &input
, sizeof(input
) );
236 /***********************************************************************
237 * GetCursorPos (USER32.@)
239 BOOL WINAPI DECLSPEC_HOTPATCH
GetCursorPos( POINT
*pt
)
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
;
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
);
263 /***********************************************************************
264 * GetCursorInfo (USER32.@)
266 BOOL WINAPI
GetCursorInfo( PCURSORINFO pci
)
270 if (!pci
) return FALSE
;
272 SERVER_START_REQ( get_thread_input
)
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;
282 GetCursorPos(&pci
->ptScreenPos
);
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
)
303 INT prev_x
, prev_y
, new_x
, new_y
;
305 SERVER_START_REQ( set_cursor
)
307 req
->flags
= SET_CURSOR_POS
;
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
;
319 if (ret
&& (prev_x
!= new_x
|| prev_y
!= new_y
)) USER_Driver
->pSetCursorPos( new_x
, new_y
);
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
)
340 set_capture_window( hwnd
, 0, &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 );
359 /**********************************************************************
360 * GetCapture (USER32.@)
362 HWND WINAPI
GetCapture(void)
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
);
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
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
;
396 if (key
< 0 || key
>= 256) return 0;
398 check_for_events( QS_INPUT
);
400 if ((ret
= USER_Driver
->pGetAsyncKeyState( key
)) == -1)
402 if (key_state_info
&&
403 !(key_state_info
->state
[key
] & 0xc0) &&
404 key_state_info
->counter
== counter
&&
405 GetTickCount() - key_state_info
->time
< 50)
407 /* use cached value */
410 else if (!key_state_info
)
412 key_state_info
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*key_state_info
) );
413 get_user_thread_info()->key_state
= key_state_info
;
417 SERVER_START_REQ( get_key_state
)
423 prev_key_state
= key_state_info
->state
[key
];
424 wine_server_set_reply( req
, key_state_info
->state
, sizeof(key_state_info
->state
) );
426 if (!wine_server_call( req
))
428 if (reply
->state
& 0x40) ret
|= 0x0001;
429 if (reply
->state
& 0x80) ret
|= 0x8000;
432 /* force refreshing the key state cache - some multithreaded programs
433 * (like Adobe Photoshop CS5) expect that changes to the async key state
434 * are also immediately available in other threads. */
435 if (prev_key_state
!= key_state_info
->state
[key
])
436 counter
= interlocked_xchg_add( &global_key_state_counter
, 1 ) + 1;
438 key_state_info
->time
= GetTickCount();
439 key_state_info
->counter
= counter
;
449 /***********************************************************************
450 * GetQueueStatus (USER32.@)
452 DWORD WINAPI
GetQueueStatus( UINT flags
)
456 if (flags
& ~(QS_ALLINPUT
| QS_ALLPOSTMESSAGE
| QS_SMRESULT
))
458 SetLastError( ERROR_INVALID_FLAGS
);
462 check_for_events( flags
);
464 SERVER_START_REQ( get_queue_status
)
466 req
->clear_bits
= flags
;
467 wine_server_call( req
);
468 ret
= MAKELONG( reply
->changed_bits
& flags
, reply
->wake_bits
& flags
);
475 /***********************************************************************
476 * GetInputState (USER32.@)
478 BOOL WINAPI
GetInputState(void)
482 check_for_events( QS_INPUT
);
484 SERVER_START_REQ( get_queue_status
)
487 wine_server_call( req
);
488 ret
= reply
->wake_bits
& (QS_KEY
| QS_MOUSEBUTTON
);
495 /******************************************************************
496 * GetLastInputInfo (USER32.@)
498 BOOL WINAPI
GetLastInputInfo(PLASTINPUTINFO plii
)
504 if (plii
->cbSize
!= sizeof (*plii
) )
506 SetLastError(ERROR_INVALID_PARAMETER
);
510 SERVER_START_REQ( get_last_input_time
)
512 ret
= !wine_server_call_err( req
);
514 plii
->dwTime
= reply
->time
;
521 /******************************************************************
522 * GetRawInputDeviceList (USER32.@)
524 UINT WINAPI
GetRawInputDeviceList(RAWINPUTDEVICELIST
*devices
, UINT
*device_count
, UINT size
)
526 TRACE("devices %p, device_count %p, size %u.\n", devices
, device_count
, size
);
528 if (size
!= sizeof(*devices
))
530 SetLastError(ERROR_INVALID_PARAMETER
);
536 SetLastError(ERROR_NOACCESS
);
546 if (*device_count
< 2)
548 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
553 devices
[0].hDevice
= WINE_MOUSE_HANDLE
;
554 devices
[0].dwType
= RIM_TYPEMOUSE
;
555 devices
[1].hDevice
= WINE_KEYBOARD_HANDLE
;
556 devices
[1].dwType
= RIM_TYPEKEYBOARD
;
562 /******************************************************************
563 * RegisterRawInputDevices (USER32.@)
565 BOOL WINAPI DECLSPEC_HOTPATCH
RegisterRawInputDevices(RAWINPUTDEVICE
*devices
, UINT device_count
, UINT size
)
567 struct rawinput_device
*d
;
571 TRACE("devices %p, device_count %u, size %u.\n", devices
, device_count
, size
);
573 if (size
!= sizeof(*devices
))
575 WARN("Invalid structure size %u.\n", size
);
579 if (!(d
= HeapAlloc( GetProcessHeap(), 0, device_count
* sizeof(*d
) ))) return FALSE
;
581 for (i
= 0; i
< device_count
; ++i
)
583 TRACE("device %u: page %#x, usage %#x, flags %#x, target %p.\n",
584 i
, devices
[i
].usUsagePage
, devices
[i
].usUsage
,
585 devices
[i
].dwFlags
, devices
[i
].hwndTarget
);
586 if (devices
[i
].dwFlags
& ~RIDEV_REMOVE
)
587 FIXME("Unhandled flags %#x for device %u.\n", devices
[i
].dwFlags
, i
);
589 d
[i
].usage_page
= devices
[i
].usUsagePage
;
590 d
[i
].usage
= devices
[i
].usUsage
;
591 d
[i
].flags
= devices
[i
].dwFlags
;
592 d
[i
].target
= wine_server_user_handle( devices
[i
].hwndTarget
);
595 SERVER_START_REQ( update_rawinput_devices
)
597 wine_server_add_data( req
, d
, device_count
* sizeof(*d
) );
598 ret
= !wine_server_call( req
);
602 HeapFree( GetProcessHeap(), 0, d
);
608 /******************************************************************
609 * GetRawInputData (USER32.@)
611 UINT WINAPI
GetRawInputData(HRAWINPUT rawinput
, UINT command
, void *data
, UINT
*data_size
, UINT header_size
)
613 RAWINPUT
*ri
= (RAWINPUT
*)rawinput
;
616 TRACE("rawinput %p, command %#x, data %p, data_size %p, header_size %u.\n",
617 rawinput
, command
, data
, data_size
, header_size
);
619 if (header_size
!= sizeof(RAWINPUTHEADER
))
621 WARN("Invalid structure size %u.\n", header_size
);
628 s
= ri
->header
.dwSize
;
631 s
= sizeof(RAWINPUTHEADER
);
643 if (*data_size
< s
) return ~0U;
649 /******************************************************************
650 * GetRawInputBuffer (USER32.@)
652 UINT WINAPI DECLSPEC_HOTPATCH
GetRawInputBuffer(PRAWINPUT pData
, PUINT pcbSize
, UINT cbSizeHeader
)
654 FIXME("(pData=%p, pcbSize=%p, cbSizeHeader=%d) stub!\n", pData
, pcbSize
, cbSizeHeader
);
660 /******************************************************************
661 * GetRawInputDeviceInfoA (USER32.@)
663 UINT WINAPI
GetRawInputDeviceInfoA(HANDLE device
, UINT command
, void *data
, UINT
*data_size
)
667 TRACE("device %p, command %u, data %p, data_size %p.\n", device
, command
, data
, data_size
);
669 ret
= GetRawInputDeviceInfoW(device
, command
, data
, data_size
);
670 if (command
== RIDI_DEVICENAME
&& ret
&& ret
!= ~0U)
671 ret
= WideCharToMultiByte(CP_ACP
, 0, data
, -1, data
, *data_size
, NULL
, NULL
);
677 /******************************************************************
678 * GetRawInputDeviceInfoW (USER32.@)
680 UINT WINAPI
GetRawInputDeviceInfoW(HANDLE device
, UINT command
, void *data
, UINT
*data_size
)
682 /* FIXME: Most of this is made up. */
683 static const WCHAR keyboard_name
[] = {'\\','\\','?','\\','W','I','N','E','_','K','E','Y','B','O','A','R','D',0};
684 static const WCHAR mouse_name
[] = {'\\','\\','?','\\','W','I','N','E','_','M','O','U','S','E',0};
685 static const RID_DEVICE_INFO_KEYBOARD keyboard_info
= {0, 0, 1, 12, 3, 101};
686 static const RID_DEVICE_INFO_MOUSE mouse_info
= {1, 5, 0, FALSE
};
687 const WCHAR
*name
= NULL
;
688 RID_DEVICE_INFO
*info
;
691 TRACE("device %p, command %u, data %p, data_size %p.\n", device
, command
, data
, data_size
);
693 if (!data_size
|| (device
!= WINE_MOUSE_HANDLE
&& device
!= WINE_KEYBOARD_HANDLE
)) return ~0U;
697 case RIDI_DEVICENAME
:
698 if (device
== WINE_MOUSE_HANDLE
)
700 s
= sizeof(mouse_name
);
705 s
= sizeof(keyboard_name
);
706 name
= keyboard_name
;
709 case RIDI_DEVICEINFO
:
728 if (command
== RIDI_DEVICENAME
)
730 memcpy(data
, name
, s
);
735 info
->cbSize
= sizeof(*info
);
736 if (device
== WINE_MOUSE_HANDLE
)
738 info
->dwType
= RIM_TYPEMOUSE
;
739 info
->u
.mouse
= mouse_info
;
743 info
->dwType
= RIM_TYPEKEYBOARD
;
744 info
->u
.keyboard
= keyboard_info
;
750 /******************************************************************
751 * GetRegisteredRawInputDevices (USER32.@)
753 UINT WINAPI DECLSPEC_HOTPATCH
GetRegisteredRawInputDevices(PRAWINPUTDEVICE pRawInputDevices
, PUINT puiNumDevices
, UINT cbSize
)
755 FIXME("(pRawInputDevices=%p, puiNumDevices=%p, cbSize=%d) stub!\n", pRawInputDevices
, puiNumDevices
, cbSize
);
761 /******************************************************************
762 * DefRawInputProc (USER32.@)
764 LRESULT WINAPI
DefRawInputProc(PRAWINPUT
*paRawInput
, INT nInput
, UINT cbSizeHeader
)
766 FIXME("(paRawInput=%p, nInput=%d, cbSizeHeader=%d) stub!\n", *paRawInput
, nInput
, cbSizeHeader
);
772 /**********************************************************************
773 * AttachThreadInput (USER32.@)
775 * Attaches the input processing mechanism of one thread to that of
778 BOOL WINAPI
AttachThreadInput( DWORD from
, DWORD to
, BOOL attach
)
782 SERVER_START_REQ( attach_thread_input
)
784 req
->tid_from
= from
;
786 req
->attach
= attach
;
787 ret
= !wine_server_call_err( req
);
794 /**********************************************************************
795 * GetKeyState (USER32.@)
797 * An application calls the GetKeyState function in response to a
798 * keyboard-input message. This function retrieves the state of the key
799 * at the time the input message was generated.
801 SHORT WINAPI DECLSPEC_HOTPATCH
GetKeyState(INT vkey
)
805 SERVER_START_REQ( get_key_state
)
807 req
->tid
= GetCurrentThreadId();
809 if (!wine_server_call( req
)) retval
= (signed char)reply
->state
;
812 TRACE("key (0x%x) -> %x\n", vkey
, retval
);
817 /**********************************************************************
818 * GetKeyboardState (USER32.@)
820 BOOL WINAPI DECLSPEC_HOTPATCH
GetKeyboardState( LPBYTE state
)
824 TRACE("(%p)\n", state
);
826 memset( state
, 0, 256 );
827 SERVER_START_REQ( get_key_state
)
829 req
->tid
= GetCurrentThreadId();
831 wine_server_set_reply( req
, state
, 256 );
832 ret
= !wine_server_call_err( req
);
839 /**********************************************************************
840 * SetKeyboardState (USER32.@)
842 BOOL WINAPI
SetKeyboardState( LPBYTE state
)
846 SERVER_START_REQ( set_key_state
)
848 req
->tid
= GetCurrentThreadId();
849 wine_server_add_data( req
, state
, 256 );
850 ret
= !wine_server_call_err( req
);
857 /**********************************************************************
858 * VkKeyScanA (USER32.@)
860 * VkKeyScan translates an ANSI character to a virtual-key and shift code
861 * for the current keyboard.
862 * high-order byte yields :
866 * 3-5 Shift-key combinations that are not used for characters
869 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
870 * FIXME : works ok except for dead chars :
871 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
872 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
874 SHORT WINAPI
VkKeyScanA(CHAR cChar
)
878 if (IsDBCSLeadByte(cChar
)) return -1;
880 MultiByteToWideChar(CP_ACP
, 0, &cChar
, 1, &wChar
, 1);
881 return VkKeyScanW(wChar
);
884 /******************************************************************************
885 * VkKeyScanW (USER32.@)
887 SHORT WINAPI
VkKeyScanW(WCHAR cChar
)
889 return VkKeyScanExW(cChar
, GetKeyboardLayout(0));
892 /**********************************************************************
893 * VkKeyScanExA (USER32.@)
895 WORD WINAPI
VkKeyScanExA(CHAR cChar
, HKL dwhkl
)
899 if (IsDBCSLeadByte(cChar
)) return -1;
901 MultiByteToWideChar(CP_ACP
, 0, &cChar
, 1, &wChar
, 1);
902 return VkKeyScanExW(wChar
, dwhkl
);
905 /******************************************************************************
906 * VkKeyScanExW (USER32.@)
908 WORD WINAPI
VkKeyScanExW(WCHAR cChar
, HKL dwhkl
)
910 return USER_Driver
->pVkKeyScanEx(cChar
, dwhkl
);
913 /**********************************************************************
914 * OemKeyScan (USER32.@)
916 DWORD WINAPI
OemKeyScan( WORD oem
)
920 char oem_char
= LOBYTE( oem
);
922 if (!OemToCharBuffW( &oem_char
, &wchr
, 1 ))
925 vkey
= VkKeyScanW( wchr
);
926 scan
= MapVirtualKeyW( LOBYTE( vkey
), MAPVK_VK_TO_VSC
);
927 if (!scan
) return -1;
934 /******************************************************************************
935 * GetKeyboardType (USER32.@)
937 INT WINAPI
GetKeyboardType(INT nTypeFlag
)
939 TRACE_(keyboard
)("(%d)\n", nTypeFlag
);
942 case 0: /* Keyboard type */
943 return 4; /* AT-101 */
944 case 1: /* Keyboard Subtype */
945 return 0; /* There are no defined subtypes */
946 case 2: /* Number of F-keys */
947 return 12; /* We're doing an 101 for now, so return 12 F-keys */
949 WARN_(keyboard
)("Unknown type\n");
950 return 0; /* The book says 0 here, so 0 */
954 /******************************************************************************
955 * MapVirtualKeyA (USER32.@)
957 UINT WINAPI
MapVirtualKeyA(UINT code
, UINT maptype
)
959 return MapVirtualKeyExA( code
, maptype
, GetKeyboardLayout(0) );
962 /******************************************************************************
963 * MapVirtualKeyW (USER32.@)
965 UINT WINAPI
MapVirtualKeyW(UINT code
, UINT maptype
)
967 return MapVirtualKeyExW(code
, maptype
, GetKeyboardLayout(0));
970 /******************************************************************************
971 * MapVirtualKeyExA (USER32.@)
973 UINT WINAPI
MapVirtualKeyExA(UINT code
, UINT maptype
, HKL hkl
)
977 ret
= MapVirtualKeyExW( code
, maptype
, hkl
);
978 if (maptype
== MAPVK_VK_TO_CHAR
)
983 WideCharToMultiByte( CP_ACP
, 0, &wch
, 1, (LPSTR
)&ch
, 1, NULL
, NULL
);
989 /******************************************************************************
990 * MapVirtualKeyExW (USER32.@)
992 UINT WINAPI
MapVirtualKeyExW(UINT code
, UINT maptype
, HKL hkl
)
994 TRACE_(keyboard
)("(%X, %d, %p)\n", code
, maptype
, hkl
);
996 return USER_Driver
->pMapVirtualKeyEx(code
, maptype
, hkl
);
999 /****************************************************************************
1000 * GetKBCodePage (USER32.@)
1002 UINT WINAPI
GetKBCodePage(void)
1007 /***********************************************************************
1008 * GetKeyboardLayout (USER32.@)
1010 * - device handle for keyboard layout defaulted to
1011 * the language id. This is the way Windows default works.
1012 * - the thread identifier is also ignored.
1014 HKL WINAPI
GetKeyboardLayout(DWORD thread_id
)
1016 return USER_Driver
->pGetKeyboardLayout(thread_id
);
1019 /****************************************************************************
1020 * GetKeyboardLayoutNameA (USER32.@)
1022 BOOL WINAPI
GetKeyboardLayoutNameA(LPSTR pszKLID
)
1024 WCHAR buf
[KL_NAMELENGTH
];
1026 if (GetKeyboardLayoutNameW(buf
))
1027 return WideCharToMultiByte( CP_ACP
, 0, buf
, -1, pszKLID
, KL_NAMELENGTH
, NULL
, NULL
) != 0;
1031 /****************************************************************************
1032 * GetKeyboardLayoutNameW (USER32.@)
1034 BOOL WINAPI
GetKeyboardLayoutNameW(LPWSTR pwszKLID
)
1038 SetLastError(ERROR_NOACCESS
);
1041 return USER_Driver
->pGetKeyboardLayoutName(pwszKLID
);
1044 /****************************************************************************
1045 * GetKeyNameTextA (USER32.@)
1047 INT WINAPI
GetKeyNameTextA(LONG lParam
, LPSTR lpBuffer
, INT nSize
)
1052 if (!nSize
|| !GetKeyNameTextW(lParam
, buf
, 256))
1057 ret
= WideCharToMultiByte(CP_ACP
, 0, buf
, -1, lpBuffer
, nSize
, NULL
, NULL
);
1068 /****************************************************************************
1069 * GetKeyNameTextW (USER32.@)
1071 INT WINAPI
GetKeyNameTextW(LONG lParam
, LPWSTR lpBuffer
, INT nSize
)
1073 if (!lpBuffer
|| !nSize
) return 0;
1074 return USER_Driver
->pGetKeyNameText( lParam
, lpBuffer
, nSize
);
1077 /****************************************************************************
1078 * ToUnicode (USER32.@)
1080 INT WINAPI
ToUnicode(UINT virtKey
, UINT scanCode
, const BYTE
*lpKeyState
,
1081 LPWSTR lpwStr
, int size
, UINT flags
)
1083 return ToUnicodeEx(virtKey
, scanCode
, lpKeyState
, lpwStr
, size
, flags
, GetKeyboardLayout(0));
1086 /****************************************************************************
1087 * ToUnicodeEx (USER32.@)
1089 INT WINAPI
ToUnicodeEx(UINT virtKey
, UINT scanCode
, const BYTE
*lpKeyState
,
1090 LPWSTR lpwStr
, int size
, UINT flags
, HKL hkl
)
1092 if (!lpKeyState
) return 0;
1093 return USER_Driver
->pToUnicodeEx(virtKey
, scanCode
, lpKeyState
, lpwStr
, size
, flags
, hkl
);
1096 /****************************************************************************
1097 * ToAscii (USER32.@)
1099 INT WINAPI
ToAscii( UINT virtKey
, UINT scanCode
, const BYTE
*lpKeyState
,
1100 LPWORD lpChar
, UINT flags
)
1102 return ToAsciiEx(virtKey
, scanCode
, lpKeyState
, lpChar
, flags
, GetKeyboardLayout(0));
1105 /****************************************************************************
1106 * ToAsciiEx (USER32.@)
1108 INT WINAPI
ToAsciiEx( UINT virtKey
, UINT scanCode
, const BYTE
*lpKeyState
,
1109 LPWORD lpChar
, UINT flags
, HKL dwhkl
)
1114 ret
= ToUnicodeEx(virtKey
, scanCode
, lpKeyState
, uni_chars
, 2, flags
, dwhkl
);
1115 if (ret
< 0) n_ret
= 1; /* FIXME: make ToUnicode return 2 for dead chars */
1117 WideCharToMultiByte(CP_ACP
, 0, uni_chars
, n_ret
, (LPSTR
)lpChar
, 2, NULL
, NULL
);
1121 /**********************************************************************
1122 * ActivateKeyboardLayout (USER32.@)
1124 HKL WINAPI
ActivateKeyboardLayout(HKL hLayout
, UINT flags
)
1126 TRACE_(keyboard
)("(%p, %d)\n", hLayout
, flags
);
1128 return USER_Driver
->pActivateKeyboardLayout(hLayout
, flags
);
1131 /**********************************************************************
1132 * BlockInput (USER32.@)
1134 BOOL WINAPI
BlockInput(BOOL fBlockIt
)
1136 FIXME_(keyboard
)("(%d): stub\n", fBlockIt
);
1137 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1142 /***********************************************************************
1143 * GetKeyboardLayoutList (USER32.@)
1145 * Return number of values available if either input parm is
1146 * 0, per MS documentation.
1148 UINT WINAPI
GetKeyboardLayoutList(INT nBuff
, HKL
*layouts
)
1150 TRACE_(keyboard
)( "(%d, %p)\n", nBuff
, layouts
);
1152 return USER_Driver
->pGetKeyboardLayoutList( nBuff
, layouts
);
1156 /***********************************************************************
1157 * RegisterHotKey (USER32.@)
1159 BOOL WINAPI
RegisterHotKey(HWND hwnd
,INT id
,UINT modifiers
,UINT vk
)
1164 TRACE_(keyboard
)("(%p,%d,0x%08x,%X)\n",hwnd
,id
,modifiers
,vk
);
1166 if ((hwnd
== NULL
|| WIN_IsCurrentThread(hwnd
)) &&
1167 !USER_Driver
->pRegisterHotKey(hwnd
, modifiers
, vk
))
1170 SERVER_START_REQ( register_hotkey
)
1172 req
->window
= wine_server_user_handle( hwnd
);
1174 req
->flags
= modifiers
;
1176 if ((ret
= !wine_server_call_err( req
)))
1178 replaced
= reply
->replaced
;
1179 modifiers
= reply
->flags
;
1185 if (ret
&& replaced
)
1186 USER_Driver
->pUnregisterHotKey(hwnd
, modifiers
, vk
);
1191 /***********************************************************************
1192 * UnregisterHotKey (USER32.@)
1194 BOOL WINAPI
UnregisterHotKey(HWND hwnd
,INT id
)
1199 TRACE_(keyboard
)("(%p,%d)\n",hwnd
,id
);
1201 SERVER_START_REQ( unregister_hotkey
)
1203 req
->window
= wine_server_user_handle( hwnd
);
1205 if ((ret
= !wine_server_call_err( req
)))
1207 modifiers
= reply
->flags
;
1214 USER_Driver
->pUnregisterHotKey(hwnd
, modifiers
, vk
);
1219 /***********************************************************************
1220 * LoadKeyboardLayoutW (USER32.@)
1222 HKL WINAPI
LoadKeyboardLayoutW(LPCWSTR pwszKLID
, UINT Flags
)
1224 TRACE_(keyboard
)("(%s, %d)\n", debugstr_w(pwszKLID
), Flags
);
1226 return USER_Driver
->pLoadKeyboardLayout(pwszKLID
, Flags
);
1229 /***********************************************************************
1230 * LoadKeyboardLayoutA (USER32.@)
1232 HKL WINAPI
LoadKeyboardLayoutA(LPCSTR pwszKLID
, UINT Flags
)
1235 UNICODE_STRING pwszKLIDW
;
1237 if (pwszKLID
) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW
, pwszKLID
);
1238 else pwszKLIDW
.Buffer
= NULL
;
1240 ret
= LoadKeyboardLayoutW(pwszKLIDW
.Buffer
, Flags
);
1241 RtlFreeUnicodeString(&pwszKLIDW
);
1246 /***********************************************************************
1247 * UnloadKeyboardLayout (USER32.@)
1249 BOOL WINAPI
UnloadKeyboardLayout(HKL hkl
)
1251 TRACE_(keyboard
)("(%p)\n", hkl
);
1253 return USER_Driver
->pUnloadKeyboardLayout(hkl
);
1256 typedef struct __TRACKINGLIST
{
1257 TRACKMOUSEEVENT tme
;
1258 POINT pos
; /* center of hover rectangle */
1261 /* FIXME: move tracking stuff into a per thread data */
1262 static _TRACKINGLIST tracking_info
;
1263 static UINT_PTR timer
;
1265 static void check_mouse_leave(HWND hwnd
, int hittest
)
1267 if (tracking_info
.tme
.hwndTrack
!= hwnd
)
1269 if (tracking_info
.tme
.dwFlags
& TME_NONCLIENT
)
1270 PostMessageW(tracking_info
.tme
.hwndTrack
, WM_NCMOUSELEAVE
, 0, 0);
1272 PostMessageW(tracking_info
.tme
.hwndTrack
, WM_MOUSELEAVE
, 0, 0);
1274 /* remove the TME_LEAVE flag */
1275 tracking_info
.tme
.dwFlags
&= ~TME_LEAVE
;
1279 if (hittest
== HTCLIENT
)
1281 if (tracking_info
.tme
.dwFlags
& TME_NONCLIENT
)
1283 PostMessageW(tracking_info
.tme
.hwndTrack
, WM_NCMOUSELEAVE
, 0, 0);
1284 /* remove the TME_LEAVE flag */
1285 tracking_info
.tme
.dwFlags
&= ~TME_LEAVE
;
1290 if (!(tracking_info
.tme
.dwFlags
& TME_NONCLIENT
))
1292 PostMessageW(tracking_info
.tme
.hwndTrack
, WM_MOUSELEAVE
, 0, 0);
1293 /* remove the TME_LEAVE flag */
1294 tracking_info
.tme
.dwFlags
&= ~TME_LEAVE
;
1300 static void CALLBACK
TrackMouseEventProc(HWND hwnd
, UINT uMsg
, UINT_PTR idEvent
,
1304 INT hoverwidth
= 0, hoverheight
= 0, hittest
;
1306 TRACE("hwnd %p, msg %04x, id %04lx, time %u\n", hwnd
, uMsg
, idEvent
, dwTime
);
1309 hwnd
= WINPOS_WindowFromPoint(hwnd
, pos
, &hittest
);
1311 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos
), hwnd
, hittest
);
1313 SystemParametersInfoW(SPI_GETMOUSEHOVERWIDTH
, 0, &hoverwidth
, 0);
1314 SystemParametersInfoW(SPI_GETMOUSEHOVERHEIGHT
, 0, &hoverheight
, 0);
1316 TRACE("tracked pos %s, current pos %s, hover width %d, hover height %d\n",
1317 wine_dbgstr_point(&tracking_info
.pos
), wine_dbgstr_point(&pos
),
1318 hoverwidth
, hoverheight
);
1320 /* see if this tracking event is looking for TME_LEAVE and that the */
1321 /* mouse has left the window */
1322 if (tracking_info
.tme
.dwFlags
& TME_LEAVE
)
1324 check_mouse_leave(hwnd
, hittest
);
1327 if (tracking_info
.tme
.hwndTrack
!= hwnd
)
1329 /* mouse is gone, stop tracking mouse hover */
1330 tracking_info
.tme
.dwFlags
&= ~TME_HOVER
;
1333 /* see if we are tracking hovering for this hwnd */
1334 if (tracking_info
.tme
.dwFlags
& TME_HOVER
)
1336 /* has the cursor moved outside the rectangle centered around pos? */
1337 if ((abs(pos
.x
- tracking_info
.pos
.x
) > (hoverwidth
/ 2)) ||
1338 (abs(pos
.y
- tracking_info
.pos
.y
) > (hoverheight
/ 2)))
1340 /* record this new position as the current position */
1341 tracking_info
.pos
= pos
;
1345 if (hittest
== HTCLIENT
)
1347 ScreenToClient(hwnd
, &pos
);
1348 TRACE("client cursor pos %s\n", wine_dbgstr_point(&pos
));
1350 PostMessageW(tracking_info
.tme
.hwndTrack
, WM_MOUSEHOVER
,
1351 get_key_state(), MAKELPARAM( pos
.x
, pos
.y
));
1355 if (tracking_info
.tme
.dwFlags
& TME_NONCLIENT
)
1356 PostMessageW(tracking_info
.tme
.hwndTrack
, WM_NCMOUSEHOVER
,
1357 hittest
, MAKELPARAM( pos
.x
, pos
.y
));
1360 /* stop tracking mouse hover */
1361 tracking_info
.tme
.dwFlags
&= ~TME_HOVER
;
1365 /* stop the timer if the tracking list is empty */
1366 if (!(tracking_info
.tme
.dwFlags
& (TME_HOVER
| TME_LEAVE
)))
1368 KillSystemTimer(tracking_info
.tme
.hwndTrack
, timer
);
1370 tracking_info
.tme
.hwndTrack
= 0;
1371 tracking_info
.tme
.dwFlags
= 0;
1372 tracking_info
.tme
.dwHoverTime
= 0;
1377 /***********************************************************************
1378 * TrackMouseEvent [USER32]
1380 * Requests notification of mouse events
1382 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
1383 * to the hwnd specified in the ptme structure. After the event message
1384 * is posted to the hwnd, the entry in the queue is removed.
1386 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
1387 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
1388 * immediately and the TME_LEAVE flag being ignored.
1391 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
1400 TrackMouseEvent (TRACKMOUSEEVENT
*ptme
)
1407 TRACE("%x, %x, %p, %u\n", ptme
->cbSize
, ptme
->dwFlags
, ptme
->hwndTrack
, ptme
->dwHoverTime
);
1409 if (ptme
->cbSize
!= sizeof(TRACKMOUSEEVENT
)) {
1410 WARN("wrong TRACKMOUSEEVENT size from app\n");
1411 SetLastError(ERROR_INVALID_PARAMETER
);
1415 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
1416 if (ptme
->dwFlags
& TME_QUERY
)
1418 *ptme
= tracking_info
.tme
;
1419 /* set cbSize in the case it's not initialized yet */
1420 ptme
->cbSize
= sizeof(TRACKMOUSEEVENT
);
1422 return TRUE
; /* return here, TME_QUERY is retrieving information */
1425 if (!IsWindow(ptme
->hwndTrack
))
1427 SetLastError(ERROR_INVALID_WINDOW_HANDLE
);
1431 hover_time
= (ptme
->dwFlags
& TME_HOVER
) ? ptme
->dwHoverTime
: HOVER_DEFAULT
;
1433 /* if HOVER_DEFAULT was specified replace this with the system's current value.
1434 * TME_LEAVE doesn't need to specify hover time so use default */
1435 if (hover_time
== HOVER_DEFAULT
|| hover_time
== 0)
1436 SystemParametersInfoW(SPI_GETMOUSEHOVERTIME
, 0, &hover_time
, 0);
1439 hwnd
= WINPOS_WindowFromPoint(ptme
->hwndTrack
, pos
, &hittest
);
1440 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos
), hwnd
, hittest
);
1442 if (ptme
->dwFlags
& ~(TME_CANCEL
| TME_HOVER
| TME_LEAVE
| TME_NONCLIENT
))
1443 FIXME("Unknown flag(s) %08x\n", ptme
->dwFlags
& ~(TME_CANCEL
| TME_HOVER
| TME_LEAVE
| TME_NONCLIENT
));
1445 if (ptme
->dwFlags
& TME_CANCEL
)
1447 if (tracking_info
.tme
.hwndTrack
== ptme
->hwndTrack
)
1449 tracking_info
.tme
.dwFlags
&= ~(ptme
->dwFlags
& ~TME_CANCEL
);
1451 /* if we aren't tracking on hover or leave remove this entry */
1452 if (!(tracking_info
.tme
.dwFlags
& (TME_HOVER
| TME_LEAVE
)))
1454 KillSystemTimer(tracking_info
.tme
.hwndTrack
, timer
);
1456 tracking_info
.tme
.hwndTrack
= 0;
1457 tracking_info
.tme
.dwFlags
= 0;
1458 tracking_info
.tme
.dwHoverTime
= 0;
1462 /* In our implementation it's possible that another window will receive a
1463 * WM_MOUSEMOVE and call TrackMouseEvent before TrackMouseEventProc is
1464 * called. In such a situation post the WM_MOUSELEAVE now */
1465 if (tracking_info
.tme
.dwFlags
& TME_LEAVE
&& tracking_info
.tme
.hwndTrack
!= NULL
)
1466 check_mouse_leave(hwnd
, hittest
);
1470 KillSystemTimer(tracking_info
.tme
.hwndTrack
, timer
);
1472 tracking_info
.tme
.hwndTrack
= 0;
1473 tracking_info
.tme
.dwFlags
= 0;
1474 tracking_info
.tme
.dwHoverTime
= 0;
1477 if (ptme
->hwndTrack
== hwnd
)
1479 /* Adding new mouse event to the tracking list */
1480 tracking_info
.tme
= *ptme
;
1481 tracking_info
.tme
.dwHoverTime
= hover_time
;
1483 /* Initialize HoverInfo variables even if not hover tracking */
1484 tracking_info
.pos
= pos
;
1486 timer
= SetSystemTimer(tracking_info
.tme
.hwndTrack
, (UINT_PTR
)&tracking_info
.tme
, hover_time
, TrackMouseEventProc
);
1493 /***********************************************************************
1494 * GetMouseMovePointsEx [USER32]
1497 * Success: count of point set in the buffer
1500 int WINAPI
GetMouseMovePointsEx(UINT size
, LPMOUSEMOVEPOINT ptin
, LPMOUSEMOVEPOINT ptout
, int count
, DWORD res
) {
1502 if((size
!= sizeof(MOUSEMOVEPOINT
)) || (count
< 0) || (count
> 64)) {
1503 SetLastError(ERROR_INVALID_PARAMETER
);
1507 if(!ptin
|| (!ptout
&& count
)) {
1508 SetLastError(ERROR_NOACCESS
);
1512 FIXME("(%d %p %p %d %d) stub\n", size
, ptin
, ptout
, count
, res
);
1514 SetLastError(ERROR_POINT_NOT_FOUND
);