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
9 * Copyright 2012 Henri Verbeet
10 * Copyright 2018 Zebediah Figura for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "user_private.h"
29 #include "wine/server.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(win
);
33 WINE_DECLARE_DEBUG_CHANNEL(keyboard
);
35 /***********************************************************************
36 * get_locale_kbd_layout
38 static HKL
get_locale_kbd_layout(void)
45 * layout = main_key_tab[kbd_layout].lcid;
47 * Winword uses return value of GetKeyboardLayout as a codepage
48 * to translate ANSI keyboard messages to unicode. But we have
49 * a problem with it: for instance Polish keyboard layout is
50 * identical to the US one, and therefore instead of the Polish
51 * locale id we return the US one.
54 layout
= GetUserDefaultLCID();
57 * Microsoft Office expects this value to be something specific
58 * for Japanese and Korean Windows with an IME the value is 0xe001
59 * We should probably check to see if an IME exists and if so then
60 * set this word properly.
62 langid
= PRIMARYLANGID( LANGIDFROMLCID( layout
) );
63 if (langid
== LANG_CHINESE
|| langid
== LANG_JAPANESE
|| langid
== LANG_KOREAN
)
64 layout
= MAKELONG( layout
, 0xe001 ); /* IME */
66 layout
= MAKELONG( layout
, layout
);
72 /***********************************************************************
73 * keybd_event (USER32.@)
75 void WINAPI
keybd_event( BYTE bVk
, BYTE bScan
,
76 DWORD dwFlags
, ULONG_PTR dwExtraInfo
)
80 input
.type
= INPUT_KEYBOARD
;
82 input
.ki
.wScan
= bScan
;
83 input
.ki
.dwFlags
= dwFlags
;
85 input
.ki
.dwExtraInfo
= dwExtraInfo
;
86 NtUserSendInput( 1, &input
, sizeof(input
) );
90 /***********************************************************************
91 * mouse_event (USER32.@)
93 void WINAPI
mouse_event( DWORD dwFlags
, DWORD dx
, DWORD dy
,
94 DWORD dwData
, ULONG_PTR dwExtraInfo
)
98 input
.type
= INPUT_MOUSE
;
101 input
.mi
.mouseData
= dwData
;
102 input
.mi
.dwFlags
= dwFlags
;
104 input
.mi
.dwExtraInfo
= dwExtraInfo
;
105 NtUserSendInput( 1, &input
, sizeof(input
) );
109 /***********************************************************************
110 * GetCursorPos (USER32.@)
112 BOOL WINAPI DECLSPEC_HOTPATCH
GetCursorPos( POINT
*pt
)
114 return NtUserGetCursorPos( pt
);
118 /**********************************************************************
119 * ReleaseCapture (USER32.@)
121 BOOL WINAPI DECLSPEC_HOTPATCH
ReleaseCapture(void)
123 return NtUserReleaseCapture();
127 /**********************************************************************
128 * GetCapture (USER32.@)
130 HWND WINAPI
GetCapture(void)
133 info
.cbSize
= sizeof(info
);
134 return NtUserGetGUIThreadInfo( GetCurrentThreadId(), &info
) ? info
.hwndCapture
: 0;
138 /*****************************************************************
139 * DestroyCaret (USER32.@)
141 BOOL WINAPI
DestroyCaret(void)
143 return NtUserDestroyCaret();
147 /*****************************************************************
148 * SetCaretPos (USER32.@)
150 BOOL WINAPI
SetCaretPos( int x
, int y
)
152 return NtUserSetCaretPos( x
, y
);
156 /*****************************************************************
157 * SetCaretBlinkTime (USER32.@)
159 BOOL WINAPI
SetCaretBlinkTime( unsigned int time
)
161 return NtUserSetCaretBlinkTime( time
);
165 /***********************************************************************
166 * GetInputState (USER32.@)
168 BOOL WINAPI
GetInputState(void)
170 return NtUserGetInputState();
174 /******************************************************************
175 * GetLastInputInfo (USER32.@)
177 BOOL WINAPI
GetLastInputInfo(PLASTINPUTINFO plii
)
183 if (plii
->cbSize
!= sizeof (*plii
) )
185 SetLastError(ERROR_INVALID_PARAMETER
);
189 SERVER_START_REQ( get_last_input_time
)
191 ret
= !wine_server_call_err( req
);
193 plii
->dwTime
= reply
->time
;
200 /**********************************************************************
201 * VkKeyScanA (USER32.@)
203 * VkKeyScan translates an ANSI character to a virtual-key and shift code
204 * for the current keyboard.
205 * high-order byte yields :
209 * 3-5 Shift-key combinations that are not used for characters
212 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
213 * FIXME : works ok except for dead chars :
214 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
215 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
217 SHORT WINAPI
VkKeyScanA(CHAR cChar
)
221 if (IsDBCSLeadByte(cChar
)) return -1;
223 MultiByteToWideChar(CP_ACP
, 0, &cChar
, 1, &wChar
, 1);
224 return VkKeyScanW(wChar
);
227 /******************************************************************************
228 * VkKeyScanW (USER32.@)
230 SHORT WINAPI
VkKeyScanW(WCHAR cChar
)
232 return NtUserVkKeyScanEx( cChar
, NtUserGetKeyboardLayout(0) );
235 /**********************************************************************
236 * VkKeyScanExA (USER32.@)
238 WORD WINAPI
VkKeyScanExA(CHAR cChar
, HKL dwhkl
)
242 if (IsDBCSLeadByte(cChar
)) return -1;
244 MultiByteToWideChar(CP_ACP
, 0, &cChar
, 1, &wChar
, 1);
245 return NtUserVkKeyScanEx( wChar
, dwhkl
);
248 /**********************************************************************
249 * OemKeyScan (USER32.@)
251 DWORD WINAPI
OemKeyScan( WORD oem
)
255 char oem_char
= LOBYTE( oem
);
257 if (!OemToCharBuffW( &oem_char
, &wchr
, 1 ))
260 vkey
= VkKeyScanW( wchr
);
261 scan
= MapVirtualKeyW( LOBYTE( vkey
), MAPVK_VK_TO_VSC
);
262 if (!scan
) return -1;
269 /******************************************************************************
270 * GetKeyboardType (USER32.@)
272 INT WINAPI
GetKeyboardType(INT nTypeFlag
)
274 TRACE_(keyboard
)("(%d)\n", nTypeFlag
);
275 if (LOWORD(NtUserGetKeyboardLayout(0)) == MAKELANGID(LANG_JAPANESE
, SUBLANG_JAPANESE_JAPAN
))
277 /* scan code for `_', the key left of r-shift, in Japanese 106 keyboard */
278 const UINT JP106_VSC_USCORE
= 0x73;
282 case 0: /* Keyboard type */
283 return 7; /* Japanese keyboard */
284 case 1: /* Keyboard Subtype */
285 /* Test keyboard mappings to detect Japanese keyboard */
286 if (MapVirtualKeyW(VK_OEM_102
, MAPVK_VK_TO_VSC
) == JP106_VSC_USCORE
287 && MapVirtualKeyW(JP106_VSC_USCORE
, MAPVK_VSC_TO_VK
) == VK_OEM_102
)
288 return 2; /* Japanese 106 */
290 return 0; /* AT-101 */
291 case 2: /* Number of F-keys */
292 return 12; /* It has 12 F-keys */
299 case 0: /* Keyboard type */
300 return 4; /* AT-101 */
301 case 1: /* Keyboard Subtype */
302 return 0; /* There are no defined subtypes */
303 case 2: /* Number of F-keys */
304 return 12; /* We're doing an 101 for now, so return 12 F-keys */
307 WARN_(keyboard
)("Unknown type\n");
308 return 0; /* The book says 0 here, so 0 */
311 /******************************************************************************
312 * MapVirtualKeyA (USER32.@)
314 UINT WINAPI
MapVirtualKeyA(UINT code
, UINT maptype
)
316 return MapVirtualKeyExA( code
, maptype
, NtUserGetKeyboardLayout(0) );
319 /******************************************************************************
320 * MapVirtualKeyW (USER32.@)
322 UINT WINAPI
MapVirtualKeyW(UINT code
, UINT maptype
)
324 return NtUserMapVirtualKeyEx( code
, maptype
, NtUserGetKeyboardLayout(0) );
327 /******************************************************************************
328 * MapVirtualKeyExA (USER32.@)
330 UINT WINAPI
MapVirtualKeyExA(UINT code
, UINT maptype
, HKL hkl
)
334 ret
= NtUserMapVirtualKeyEx( code
, maptype
, hkl
);
335 if (maptype
== MAPVK_VK_TO_CHAR
)
340 WideCharToMultiByte( CP_ACP
, 0, &wch
, 1, (LPSTR
)&ch
, 1, NULL
, NULL
);
346 /****************************************************************************
347 * GetKBCodePage (USER32.@)
349 UINT WINAPI
GetKBCodePage(void)
354 /****************************************************************************
355 * GetKeyboardLayoutNameA (USER32.@)
357 BOOL WINAPI
GetKeyboardLayoutNameA(LPSTR pszKLID
)
359 WCHAR buf
[KL_NAMELENGTH
];
361 if (NtUserGetKeyboardLayoutName( buf
))
362 return WideCharToMultiByte( CP_ACP
, 0, buf
, -1, pszKLID
, KL_NAMELENGTH
, NULL
, NULL
) != 0;
366 /****************************************************************************
367 * GetKeyNameTextA (USER32.@)
369 INT WINAPI
GetKeyNameTextA(LONG lParam
, LPSTR lpBuffer
, INT nSize
)
374 if (!nSize
|| !NtUserGetKeyNameText( lParam
, buf
, ARRAYSIZE(buf
) ))
379 ret
= WideCharToMultiByte(CP_ACP
, 0, buf
, -1, lpBuffer
, nSize
, NULL
, NULL
);
390 /****************************************************************************
391 * ToUnicode (USER32.@)
393 INT WINAPI
ToUnicode( UINT virt
, UINT scan
, const BYTE
*state
, LPWSTR str
, int size
, UINT flags
)
395 return NtUserToUnicodeEx( virt
, scan
, state
, str
, size
, flags
, NtUserGetKeyboardLayout(0) );
398 /****************************************************************************
401 INT WINAPI
ToAscii( UINT virtKey
, UINT scanCode
, const BYTE
*lpKeyState
,
402 LPWORD lpChar
, UINT flags
)
404 return ToAsciiEx( virtKey
, scanCode
, lpKeyState
, lpChar
, flags
,
405 NtUserGetKeyboardLayout(0) );
408 /****************************************************************************
409 * ToAsciiEx (USER32.@)
411 INT WINAPI
ToAsciiEx( UINT virtKey
, UINT scanCode
, const BYTE
*lpKeyState
,
412 LPWORD lpChar
, UINT flags
, HKL dwhkl
)
417 ret
= NtUserToUnicodeEx( virtKey
, scanCode
, lpKeyState
, uni_chars
, 2, flags
, dwhkl
);
418 if (ret
< 0) n_ret
= 1; /* FIXME: make ToUnicode return 2 for dead chars */
420 WideCharToMultiByte(CP_ACP
, 0, uni_chars
, n_ret
, (LPSTR
)lpChar
, 2, NULL
, NULL
);
424 /**********************************************************************
425 * BlockInput (USER32.@)
427 BOOL WINAPI
BlockInput(BOOL fBlockIt
)
429 FIXME_(keyboard
)("(%d): stub\n", fBlockIt
);
430 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
436 /***********************************************************************
437 * LoadKeyboardLayoutW (USER32.@)
439 HKL WINAPI
LoadKeyboardLayoutW( const WCHAR
*name
, UINT flags
)
441 WCHAR layout_path
[MAX_PATH
], value
[5];
442 DWORD value_size
, tmp
;
446 FIXME_(keyboard
)( "name %s, flags %x, semi-stub!\n", debugstr_w( name
), flags
);
448 tmp
= wcstoul( name
, NULL
, 16 );
449 if (HIWORD( tmp
)) layout
= UlongToHandle( tmp
);
450 else layout
= UlongToHandle( MAKELONG( LOWORD( tmp
), LOWORD( tmp
) ) );
452 wcscpy( layout_path
, L
"System\\CurrentControlSet\\Control\\Keyboard Layouts\\" );
453 wcscat( layout_path
, name
);
455 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE
, layout_path
, &hkey
))
457 value_size
= sizeof(value
);
458 if (!RegGetValueW( hkey
, NULL
, L
"Layout Id", RRF_RT_REG_SZ
, NULL
, (void *)&value
, &value_size
))
459 layout
= UlongToHandle( MAKELONG( LOWORD( tmp
), 0xf000 | (wcstoul( value
, NULL
, 16 ) & 0xfff) ) );
464 if ((flags
& KLF_ACTIVATE
) && NtUserActivateKeyboardLayout( layout
, 0 )) return layout
;
466 /* FIXME: semi-stub: returning default layout */
467 return get_locale_kbd_layout();
470 /***********************************************************************
471 * LoadKeyboardLayoutA (USER32.@)
473 HKL WINAPI
LoadKeyboardLayoutA(LPCSTR pwszKLID
, UINT Flags
)
476 UNICODE_STRING pwszKLIDW
;
478 if (pwszKLID
) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW
, pwszKLID
);
479 else pwszKLIDW
.Buffer
= NULL
;
481 ret
= LoadKeyboardLayoutW(pwszKLIDW
.Buffer
, Flags
);
482 RtlFreeUnicodeString(&pwszKLIDW
);
487 /***********************************************************************
488 * UnloadKeyboardLayout (USER32.@)
490 BOOL WINAPI
UnloadKeyboardLayout( HKL layout
)
492 FIXME_(keyboard
)( "layout %p, stub!\n", layout
);
493 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
498 /***********************************************************************
499 * EnableMouseInPointer (USER32.@)
501 BOOL WINAPI
EnableMouseInPointer(BOOL enable
)
503 FIXME("(%#x) stub\n", enable
);
505 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
509 static DWORD CALLBACK
devnotify_window_callback(HANDLE handle
, DWORD flags
, DEV_BROADCAST_HDR
*header
)
511 SendMessageTimeoutW(handle
, WM_DEVICECHANGE
, flags
, (LPARAM
)header
, SMTO_ABORTIFHUNG
, 2000, NULL
);
515 static DWORD CALLBACK
devnotify_service_callback(HANDLE handle
, DWORD flags
, DEV_BROADCAST_HDR
*header
)
517 FIXME("Support for service handles is not yet implemented!\n");
521 struct device_notification_details
523 DWORD (CALLBACK
*cb
)(HANDLE handle
, DWORD flags
, DEV_BROADCAST_HDR
*header
);
527 DEV_BROADCAST_HDR header
;
528 DEV_BROADCAST_DEVICEINTERFACE_W iface
;
532 extern HDEVNOTIFY WINAPI
I_ScRegisterDeviceNotification( struct device_notification_details
*details
,
533 void *filter
, DWORD flags
);
534 extern BOOL WINAPI
I_ScUnregisterDeviceNotification( HDEVNOTIFY handle
);
536 /***********************************************************************
537 * RegisterDeviceNotificationA (USER32.@)
539 * See RegisterDeviceNotificationW.
541 HDEVNOTIFY WINAPI
RegisterDeviceNotificationA( HANDLE handle
, void *filter
, DWORD flags
)
543 return RegisterDeviceNotificationW( handle
, filter
, flags
);
546 /***********************************************************************
547 * RegisterDeviceNotificationW (USER32.@)
549 HDEVNOTIFY WINAPI
RegisterDeviceNotificationW( HANDLE handle
, void *filter
, DWORD flags
)
551 struct device_notification_details details
;
552 DEV_BROADCAST_HDR
*header
= filter
;
554 TRACE("handle %p, filter %p, flags %#lx\n", handle
, filter
, flags
);
556 if (flags
& ~(DEVICE_NOTIFY_SERVICE_HANDLE
| DEVICE_NOTIFY_ALL_INTERFACE_CLASSES
))
558 SetLastError( ERROR_INVALID_PARAMETER
);
562 if (!(flags
& DEVICE_NOTIFY_SERVICE_HANDLE
) && !IsWindow( handle
))
564 SetLastError( ERROR_INVALID_PARAMETER
);
568 if (!header
) details
.filter
.header
.dbch_devicetype
= 0;
569 else if (header
->dbch_devicetype
== DBT_DEVTYP_DEVICEINTERFACE
)
571 DEV_BROADCAST_DEVICEINTERFACE_W
*iface
= (DEV_BROADCAST_DEVICEINTERFACE_W
*)header
;
572 details
.filter
.iface
= *iface
;
574 if (flags
& DEVICE_NOTIFY_ALL_INTERFACE_CLASSES
)
575 details
.filter
.iface
.dbcc_size
= offsetof( DEV_BROADCAST_DEVICEINTERFACE_W
, dbcc_classguid
);
577 details
.filter
.iface
.dbcc_size
= offsetof( DEV_BROADCAST_DEVICEINTERFACE_W
, dbcc_name
);
579 else if (header
->dbch_devicetype
== DBT_DEVTYP_HANDLE
)
581 FIXME( "DBT_DEVTYP_HANDLE filter type not implemented\n" );
582 details
.filter
.header
.dbch_devicetype
= 0;
586 SetLastError( ERROR_INVALID_DATA
);
590 details
.handle
= handle
;
592 if (flags
& DEVICE_NOTIFY_SERVICE_HANDLE
)
593 details
.cb
= devnotify_service_callback
;
595 details
.cb
= devnotify_window_callback
;
597 return I_ScRegisterDeviceNotification( &details
, filter
, 0 );
600 /***********************************************************************
601 * UnregisterDeviceNotification (USER32.@)
603 BOOL WINAPI
UnregisterDeviceNotification( HDEVNOTIFY handle
)
605 TRACE("%p\n", handle
);
607 return I_ScUnregisterDeviceNotification( handle
);
610 /***********************************************************************
611 * GetRawInputDeviceInfoA (USER32.@)
613 UINT WINAPI
GetRawInputDeviceInfoA( HANDLE device
, UINT command
, void *data
, UINT
*size
)
615 TRACE( "device %p, command %#x, data %p, size %p.\n", device
, command
, data
, size
);
617 /* RIDI_DEVICENAME size is in chars, not bytes */
618 if (command
== RIDI_DEVICENAME
)
623 if (!size
) return ~0U;
627 if (data
&& sizeW
> 0)
628 nameW
= HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR
) * sizeW
);
632 ret
= NtUserGetRawInputDeviceInfo( device
, command
, nameW
, &sizeW
);
634 if (ret
&& ret
!= ~0U)
635 WideCharToMultiByte( CP_ACP
, 0, nameW
, -1, data
, *size
, NULL
, NULL
);
639 HeapFree( GetProcessHeap(), 0, nameW
);
644 return NtUserGetRawInputDeviceInfo( device
, command
, data
, size
);
647 /***********************************************************************
648 * DefRawInputProc (USER32.@)
650 LRESULT WINAPI
DefRawInputProc( RAWINPUT
**data
, INT data_count
, UINT header_size
)
652 TRACE( "data %p, data_count %d, header_size %u.\n", data
, data_count
, header_size
);
654 return header_size
== sizeof(RAWINPUTHEADER
) ? 0 : -1;