user32: Also ignore dynamic DPI awareness changes when DPI scaling is disabled.
[wine.git] / dlls / user32 / input.c
blob8b2ae805aa7419eccc3c7a0c3af54b2f9328a23b
1 /*
2 * USER Input processing
4 * Copyright 1993 Bob Amstadt
5 * Copyright 1996 Albrecht Kleine
6 * Copyright 1997 David Faure
7 * Copyright 1998 Morten Welinder
8 * Copyright 1998 Ulrich Weigand
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <assert.h>
35 #define NONAMELESSUNION
37 #include "ntstatus.h"
38 #define WIN32_NO_STATUS
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "winternl.h"
45 #include "winerror.h"
46 #include "win.h"
47 #include "user_private.h"
48 #include "wine/server.h"
49 #include "wine/debug.h"
50 #include "wine/unicode.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(win);
53 WINE_DECLARE_DEBUG_CHANNEL(keyboard);
55 INT global_key_state_counter = 0;
57 /***********************************************************************
58 * get_key_state
60 static WORD get_key_state(void)
62 WORD ret = 0;
64 if (GetSystemMetrics( SM_SWAPBUTTON ))
66 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_LBUTTON;
67 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_RBUTTON;
69 else
71 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_LBUTTON;
72 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_RBUTTON;
74 if (GetAsyncKeyState(VK_MBUTTON) & 0x80) ret |= MK_MBUTTON;
75 if (GetAsyncKeyState(VK_SHIFT) & 0x80) ret |= MK_SHIFT;
76 if (GetAsyncKeyState(VK_CONTROL) & 0x80) ret |= MK_CONTROL;
77 if (GetAsyncKeyState(VK_XBUTTON1) & 0x80) ret |= MK_XBUTTON1;
78 if (GetAsyncKeyState(VK_XBUTTON2) & 0x80) ret |= MK_XBUTTON2;
79 return ret;
83 /**********************************************************************
84 * set_capture_window
86 BOOL set_capture_window( HWND hwnd, UINT gui_flags, HWND *prev_ret )
88 HWND previous = 0;
89 UINT flags = 0;
90 BOOL ret;
92 if (gui_flags & GUI_INMENUMODE) flags |= CAPTURE_MENU;
93 if (gui_flags & GUI_INMOVESIZE) flags |= CAPTURE_MOVESIZE;
95 SERVER_START_REQ( set_capture_window )
97 req->handle = wine_server_user_handle( hwnd );
98 req->flags = flags;
99 if ((ret = !wine_server_call_err( req )))
101 previous = wine_server_ptr_handle( reply->previous );
102 hwnd = wine_server_ptr_handle( reply->full_handle );
105 SERVER_END_REQ;
107 if (ret)
109 USER_Driver->pSetCapture( hwnd, gui_flags );
111 if (previous)
112 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
114 if (prev_ret) *prev_ret = previous;
116 return ret;
120 /***********************************************************************
121 * __wine_send_input (USER32.@)
123 * Internal SendInput function to allow the graphics driver to inject real events.
125 BOOL CDECL __wine_send_input( HWND hwnd, const INPUT *input )
127 NTSTATUS status = send_hardware_message( hwnd, input, 0 );
128 if (status) SetLastError( RtlNtStatusToDosError(status) );
129 return !status;
133 /***********************************************************************
134 * update_mouse_coords
136 * Helper for SendInput.
138 static void update_mouse_coords( INPUT *input )
140 if (!(input->u.mi.dwFlags & MOUSEEVENTF_MOVE)) return;
142 if (input->u.mi.dwFlags & MOUSEEVENTF_ABSOLUTE)
144 DPI_AWARENESS_CONTEXT context = SetThreadDpiAwarenessContext( DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE );
145 if (input->u.mi.dwFlags & MOUSEEVENTF_VIRTUALDESK)
147 RECT rc = get_virtual_screen_rect();
148 input->u.mi.dx = rc.left + ((input->u.mi.dx * (rc.right - rc.left)) >> 16);
149 input->u.mi.dy = rc.top + ((input->u.mi.dy * (rc.bottom - rc.top)) >> 16);
151 else
153 input->u.mi.dx = (input->u.mi.dx * GetSystemMetrics( SM_CXSCREEN )) >> 16;
154 input->u.mi.dy = (input->u.mi.dy * GetSystemMetrics( SM_CYSCREEN )) >> 16;
156 SetThreadDpiAwarenessContext( context );
158 else
160 int accel[3];
162 /* dx and dy can be negative numbers for relative movements */
163 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
165 if (!accel[2]) return;
167 if (abs(input->u.mi.dx) > accel[0])
169 input->u.mi.dx *= 2;
170 if ((abs(input->u.mi.dx) > accel[1]) && (accel[2] == 2)) input->u.mi.dx *= 2;
172 if (abs(input->u.mi.dy) > accel[0])
174 input->u.mi.dy *= 2;
175 if ((abs(input->u.mi.dy) > accel[1]) && (accel[2] == 2)) input->u.mi.dy *= 2;
180 /***********************************************************************
181 * SendInput (USER32.@)
183 UINT WINAPI SendInput( UINT count, LPINPUT inputs, int size )
185 UINT i;
186 NTSTATUS status;
188 for (i = 0; i < count; i++)
190 if (inputs[i].type == INPUT_MOUSE)
192 /* we need to update the coordinates to what the server expects */
193 INPUT input = inputs[i];
194 update_mouse_coords( &input );
195 status = send_hardware_message( 0, &input, SEND_HWMSG_INJECTED );
197 else status = send_hardware_message( 0, &inputs[i], SEND_HWMSG_INJECTED );
199 if (status)
201 SetLastError( RtlNtStatusToDosError(status) );
202 break;
206 return i;
210 /***********************************************************************
211 * keybd_event (USER32.@)
213 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
214 DWORD dwFlags, ULONG_PTR dwExtraInfo )
216 INPUT input;
218 input.type = INPUT_KEYBOARD;
219 input.u.ki.wVk = bVk;
220 input.u.ki.wScan = bScan;
221 input.u.ki.dwFlags = dwFlags;
222 input.u.ki.time = 0;
223 input.u.ki.dwExtraInfo = dwExtraInfo;
224 SendInput( 1, &input, sizeof(input) );
228 /***********************************************************************
229 * mouse_event (USER32.@)
231 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
232 DWORD dwData, ULONG_PTR dwExtraInfo )
234 INPUT input;
236 input.type = INPUT_MOUSE;
237 input.u.mi.dx = dx;
238 input.u.mi.dy = dy;
239 input.u.mi.mouseData = dwData;
240 input.u.mi.dwFlags = dwFlags;
241 input.u.mi.time = 0;
242 input.u.mi.dwExtraInfo = dwExtraInfo;
243 SendInput( 1, &input, sizeof(input) );
247 /***********************************************************************
248 * GetCursorPos (USER32.@)
250 BOOL WINAPI DECLSPEC_HOTPATCH GetCursorPos( POINT *pt )
252 BOOL ret;
253 DWORD last_change;
254 UINT dpi;
256 if (!pt) return FALSE;
258 SERVER_START_REQ( set_cursor )
260 if ((ret = !wine_server_call( req )))
262 pt->x = reply->new_x;
263 pt->y = reply->new_y;
264 last_change = reply->last_change;
267 SERVER_END_REQ;
269 /* query new position from graphics driver if we haven't updated recently */
270 if (ret && GetTickCount() - last_change > 100) ret = USER_Driver->pGetCursorPos( pt );
271 if (ret && (dpi = get_thread_dpi()))
273 DPI_AWARENESS_CONTEXT context;
274 context = SetThreadDpiAwarenessContext( DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE );
275 *pt = map_dpi_point( *pt, get_monitor_dpi( MonitorFromPoint( *pt, MONITOR_DEFAULTTOPRIMARY )), dpi );
276 SetThreadDpiAwarenessContext( context );
278 return ret;
282 /***********************************************************************
283 * GetCursorInfo (USER32.@)
285 BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
287 BOOL ret;
289 if (!pci) return FALSE;
291 SERVER_START_REQ( get_thread_input )
293 req->tid = 0;
294 if ((ret = !wine_server_call( req )))
296 pci->hCursor = wine_server_ptr_handle( reply->cursor );
297 pci->flags = (reply->show_count >= 0) ? CURSOR_SHOWING : 0;
300 SERVER_END_REQ;
301 GetCursorPos(&pci->ptScreenPos);
302 return ret;
306 /***********************************************************************
307 * SetCursorPos (USER32.@)
309 BOOL WINAPI DECLSPEC_HOTPATCH SetCursorPos( INT x, INT y )
311 POINT pt = { x, y };
312 BOOL ret;
313 INT prev_x, prev_y, new_x, new_y;
314 UINT dpi;
316 if ((dpi = get_thread_dpi()))
317 pt = map_dpi_point( pt, dpi, get_monitor_dpi( MonitorFromPoint( pt, MONITOR_DEFAULTTOPRIMARY )));
319 SERVER_START_REQ( set_cursor )
321 req->flags = SET_CURSOR_POS;
322 req->x = pt.x;
323 req->y = pt.y;
324 if ((ret = !wine_server_call( req )))
326 prev_x = reply->prev_x;
327 prev_y = reply->prev_y;
328 new_x = reply->new_x;
329 new_y = reply->new_y;
332 SERVER_END_REQ;
333 if (ret && (prev_x != new_x || prev_y != new_y)) USER_Driver->pSetCursorPos( new_x, new_y );
334 return ret;
337 /**********************************************************************
338 * SetCapture (USER32.@)
340 HWND WINAPI DECLSPEC_HOTPATCH SetCapture( HWND hwnd )
342 HWND previous = 0;
344 set_capture_window( hwnd, 0, &previous );
345 return previous;
349 /**********************************************************************
350 * ReleaseCapture (USER32.@)
352 BOOL WINAPI DECLSPEC_HOTPATCH ReleaseCapture(void)
354 BOOL ret = set_capture_window( 0, 0, NULL );
356 /* Somebody may have missed some mouse movements */
357 if (ret) mouse_event( MOUSEEVENTF_MOVE, 0, 0, 0, 0 );
359 return ret;
363 /**********************************************************************
364 * GetCapture (USER32.@)
366 HWND WINAPI GetCapture(void)
368 HWND ret = 0;
370 SERVER_START_REQ( get_thread_input )
372 req->tid = GetCurrentThreadId();
373 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->capture );
375 SERVER_END_REQ;
376 return ret;
380 static void check_for_events( UINT flags )
382 if (USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, flags, 0 ) == WAIT_TIMEOUT)
383 flush_window_surfaces( TRUE );
386 /**********************************************************************
387 * GetAsyncKeyState (USER32.@)
389 * Determine if a key is or was pressed. retval has high-order
390 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
391 * been pressed.
393 SHORT WINAPI DECLSPEC_HOTPATCH GetAsyncKeyState( INT key )
395 struct user_key_state_info *key_state_info = get_user_thread_info()->key_state;
396 INT counter = global_key_state_counter;
397 BYTE prev_key_state;
398 SHORT ret;
400 if (key < 0 || key >= 256) return 0;
402 check_for_events( QS_INPUT );
404 if ((ret = USER_Driver->pGetAsyncKeyState( key )) == -1)
406 if (key_state_info &&
407 !(key_state_info->state[key] & 0xc0) &&
408 key_state_info->counter == counter &&
409 GetTickCount() - key_state_info->time < 50)
411 /* use cached value */
412 return 0;
414 else if (!key_state_info)
416 key_state_info = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*key_state_info) );
417 get_user_thread_info()->key_state = key_state_info;
420 ret = 0;
421 SERVER_START_REQ( get_key_state )
423 req->tid = 0;
424 req->key = key;
425 if (key_state_info)
427 prev_key_state = key_state_info->state[key];
428 wine_server_set_reply( req, key_state_info->state, sizeof(key_state_info->state) );
430 if (!wine_server_call( req ))
432 if (reply->state & 0x40) ret |= 0x0001;
433 if (reply->state & 0x80) ret |= 0x8000;
434 if (key_state_info)
436 /* force refreshing the key state cache - some multithreaded programs
437 * (like Adobe Photoshop CS5) expect that changes to the async key state
438 * are also immediately available in other threads. */
439 if (prev_key_state != key_state_info->state[key])
440 counter = interlocked_xchg_add( &global_key_state_counter, 1 ) + 1;
442 key_state_info->time = GetTickCount();
443 key_state_info->counter = counter;
447 SERVER_END_REQ;
449 return ret;
453 /***********************************************************************
454 * GetQueueStatus (USER32.@)
456 DWORD WINAPI GetQueueStatus( UINT flags )
458 DWORD ret;
460 if (flags & ~(QS_ALLINPUT | QS_ALLPOSTMESSAGE | QS_SMRESULT))
462 SetLastError( ERROR_INVALID_FLAGS );
463 return 0;
466 check_for_events( flags );
468 SERVER_START_REQ( get_queue_status )
470 req->clear_bits = flags;
471 wine_server_call( req );
472 ret = MAKELONG( reply->changed_bits & flags, reply->wake_bits & flags );
474 SERVER_END_REQ;
475 return ret;
479 /***********************************************************************
480 * GetInputState (USER32.@)
482 BOOL WINAPI GetInputState(void)
484 DWORD ret;
486 check_for_events( QS_INPUT );
488 SERVER_START_REQ( get_queue_status )
490 req->clear_bits = 0;
491 wine_server_call( req );
492 ret = reply->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
494 SERVER_END_REQ;
495 return ret;
499 /******************************************************************
500 * GetLastInputInfo (USER32.@)
502 BOOL WINAPI GetLastInputInfo(PLASTINPUTINFO plii)
504 BOOL ret;
506 TRACE("%p\n", plii);
508 if (plii->cbSize != sizeof (*plii) )
510 SetLastError(ERROR_INVALID_PARAMETER);
511 return FALSE;
514 SERVER_START_REQ( get_last_input_time )
516 ret = !wine_server_call_err( req );
517 if (ret)
518 plii->dwTime = reply->time;
520 SERVER_END_REQ;
521 return ret;
525 /**********************************************************************
526 * AttachThreadInput (USER32.@)
528 * Attaches the input processing mechanism of one thread to that of
529 * another thread.
531 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
533 BOOL ret;
535 SERVER_START_REQ( attach_thread_input )
537 req->tid_from = from;
538 req->tid_to = to;
539 req->attach = attach;
540 ret = !wine_server_call_err( req );
542 SERVER_END_REQ;
543 return ret;
547 /**********************************************************************
548 * GetKeyState (USER32.@)
550 * An application calls the GetKeyState function in response to a
551 * keyboard-input message. This function retrieves the state of the key
552 * at the time the input message was generated.
554 SHORT WINAPI DECLSPEC_HOTPATCH GetKeyState(INT vkey)
556 SHORT retval = 0;
558 SERVER_START_REQ( get_key_state )
560 req->tid = GetCurrentThreadId();
561 req->key = vkey;
562 if (!wine_server_call( req )) retval = (signed char)reply->state;
564 SERVER_END_REQ;
565 TRACE("key (0x%x) -> %x\n", vkey, retval);
566 return retval;
570 /**********************************************************************
571 * GetKeyboardState (USER32.@)
573 BOOL WINAPI DECLSPEC_HOTPATCH GetKeyboardState( LPBYTE state )
575 BOOL ret;
577 TRACE("(%p)\n", state);
579 memset( state, 0, 256 );
580 SERVER_START_REQ( get_key_state )
582 req->tid = GetCurrentThreadId();
583 req->key = -1;
584 wine_server_set_reply( req, state, 256 );
585 ret = !wine_server_call_err( req );
587 SERVER_END_REQ;
588 return ret;
592 /**********************************************************************
593 * SetKeyboardState (USER32.@)
595 BOOL WINAPI SetKeyboardState( LPBYTE state )
597 BOOL ret;
599 SERVER_START_REQ( set_key_state )
601 req->tid = GetCurrentThreadId();
602 wine_server_add_data( req, state, 256 );
603 ret = !wine_server_call_err( req );
605 SERVER_END_REQ;
606 return ret;
610 /**********************************************************************
611 * VkKeyScanA (USER32.@)
613 * VkKeyScan translates an ANSI character to a virtual-key and shift code
614 * for the current keyboard.
615 * high-order byte yields :
616 * 0 Unshifted
617 * 1 Shift
618 * 2 Ctrl
619 * 3-5 Shift-key combinations that are not used for characters
620 * 6 Ctrl-Alt
621 * 7 Ctrl-Alt-Shift
622 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
623 * FIXME : works ok except for dead chars :
624 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
625 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
627 SHORT WINAPI VkKeyScanA(CHAR cChar)
629 WCHAR wChar;
631 if (IsDBCSLeadByte(cChar)) return -1;
633 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
634 return VkKeyScanW(wChar);
637 /******************************************************************************
638 * VkKeyScanW (USER32.@)
640 SHORT WINAPI VkKeyScanW(WCHAR cChar)
642 return VkKeyScanExW(cChar, GetKeyboardLayout(0));
645 /**********************************************************************
646 * VkKeyScanExA (USER32.@)
648 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
650 WCHAR wChar;
652 if (IsDBCSLeadByte(cChar)) return -1;
654 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
655 return VkKeyScanExW(wChar, dwhkl);
658 /******************************************************************************
659 * VkKeyScanExW (USER32.@)
661 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
663 return USER_Driver->pVkKeyScanEx(cChar, dwhkl);
666 /**********************************************************************
667 * OemKeyScan (USER32.@)
669 DWORD WINAPI OemKeyScan( WORD oem )
671 WCHAR wchr;
672 DWORD vkey, scan;
673 char oem_char = LOBYTE( oem );
675 if (!OemToCharBuffW( &oem_char, &wchr, 1 ))
676 return -1;
678 vkey = VkKeyScanW( wchr );
679 scan = MapVirtualKeyW( LOBYTE( vkey ), MAPVK_VK_TO_VSC );
680 if (!scan) return -1;
682 vkey &= 0xff00;
683 vkey <<= 8;
684 return vkey | scan;
687 /******************************************************************************
688 * GetKeyboardType (USER32.@)
690 INT WINAPI GetKeyboardType(INT nTypeFlag)
692 TRACE_(keyboard)("(%d)\n", nTypeFlag);
693 if (LOWORD(GetKeyboardLayout(0)) == MAKELANGID(LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN))
695 /* scan code for `_', the key left of r-shift, in Japanese 106 keyboard */
696 const UINT JP106_VSC_USCORE = 0x73;
698 switch(nTypeFlag)
700 case 0: /* Keyboard type */
701 return 7; /* Japanese keyboard */
702 case 1: /* Keyboard Subtype */
703 /* Test keyboard mappings to detect Japanese keyboard */
704 if (MapVirtualKeyW(VK_OEM_102, MAPVK_VK_TO_VSC) == JP106_VSC_USCORE
705 && MapVirtualKeyW(JP106_VSC_USCORE, MAPVK_VSC_TO_VK) == VK_OEM_102)
706 return 2; /* Japanese 106 */
707 else
708 return 0; /* AT-101 */
709 case 2: /* Number of F-keys */
710 return 12; /* It has 12 F-keys */
713 else
715 switch(nTypeFlag)
717 case 0: /* Keyboard type */
718 return 4; /* AT-101 */
719 case 1: /* Keyboard Subtype */
720 return 0; /* There are no defined subtypes */
721 case 2: /* Number of F-keys */
722 return 12; /* We're doing an 101 for now, so return 12 F-keys */
725 WARN_(keyboard)("Unknown type\n");
726 return 0; /* The book says 0 here, so 0 */
729 /******************************************************************************
730 * MapVirtualKeyA (USER32.@)
732 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
734 return MapVirtualKeyExA( code, maptype, GetKeyboardLayout(0) );
737 /******************************************************************************
738 * MapVirtualKeyW (USER32.@)
740 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
742 return MapVirtualKeyExW(code, maptype, GetKeyboardLayout(0));
745 /******************************************************************************
746 * MapVirtualKeyExA (USER32.@)
748 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
750 UINT ret;
752 ret = MapVirtualKeyExW( code, maptype, hkl );
753 if (maptype == MAPVK_VK_TO_CHAR)
755 BYTE ch = 0;
756 WCHAR wch = ret;
758 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL );
759 ret = ch;
761 return ret;
764 /******************************************************************************
765 * MapVirtualKeyExW (USER32.@)
767 UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
769 TRACE_(keyboard)("(%X, %d, %p)\n", code, maptype, hkl);
771 return USER_Driver->pMapVirtualKeyEx(code, maptype, hkl);
774 /****************************************************************************
775 * GetKBCodePage (USER32.@)
777 UINT WINAPI GetKBCodePage(void)
779 return GetOEMCP();
782 /***********************************************************************
783 * GetKeyboardLayout (USER32.@)
785 * - device handle for keyboard layout defaulted to
786 * the language id. This is the way Windows default works.
787 * - the thread identifier is also ignored.
789 HKL WINAPI GetKeyboardLayout(DWORD thread_id)
791 return USER_Driver->pGetKeyboardLayout(thread_id);
794 /****************************************************************************
795 * GetKeyboardLayoutNameA (USER32.@)
797 BOOL WINAPI GetKeyboardLayoutNameA(LPSTR pszKLID)
799 WCHAR buf[KL_NAMELENGTH];
801 if (GetKeyboardLayoutNameW(buf))
802 return WideCharToMultiByte( CP_ACP, 0, buf, -1, pszKLID, KL_NAMELENGTH, NULL, NULL ) != 0;
803 return FALSE;
806 /****************************************************************************
807 * GetKeyboardLayoutNameW (USER32.@)
809 BOOL WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
811 if (!pwszKLID)
813 SetLastError(ERROR_NOACCESS);
814 return FALSE;
816 return USER_Driver->pGetKeyboardLayoutName(pwszKLID);
819 /****************************************************************************
820 * GetKeyNameTextA (USER32.@)
822 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
824 WCHAR buf[256];
825 INT ret;
827 if (!nSize || !GetKeyNameTextW(lParam, buf, 256))
829 lpBuffer[0] = 0;
830 return 0;
832 ret = WideCharToMultiByte(CP_ACP, 0, buf, -1, lpBuffer, nSize, NULL, NULL);
833 if (!ret && nSize)
835 ret = nSize - 1;
836 lpBuffer[ret] = 0;
838 else ret--;
840 return ret;
843 /****************************************************************************
844 * GetKeyNameTextW (USER32.@)
846 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
848 if (!lpBuffer || !nSize) return 0;
849 return USER_Driver->pGetKeyNameText( lParam, lpBuffer, nSize );
852 /****************************************************************************
853 * ToUnicode (USER32.@)
855 INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
856 LPWSTR lpwStr, int size, UINT flags)
858 return ToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, GetKeyboardLayout(0));
861 /****************************************************************************
862 * ToUnicodeEx (USER32.@)
864 INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
865 LPWSTR lpwStr, int size, UINT flags, HKL hkl)
867 if (!lpKeyState) return 0;
868 return USER_Driver->pToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, hkl);
871 /****************************************************************************
872 * ToAscii (USER32.@)
874 INT WINAPI ToAscii( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
875 LPWORD lpChar, UINT flags )
877 return ToAsciiEx(virtKey, scanCode, lpKeyState, lpChar, flags, GetKeyboardLayout(0));
880 /****************************************************************************
881 * ToAsciiEx (USER32.@)
883 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
884 LPWORD lpChar, UINT flags, HKL dwhkl )
886 WCHAR uni_chars[2];
887 INT ret, n_ret;
889 ret = ToUnicodeEx(virtKey, scanCode, lpKeyState, uni_chars, 2, flags, dwhkl);
890 if (ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
891 else n_ret = ret;
892 WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
893 return ret;
896 /**********************************************************************
897 * ActivateKeyboardLayout (USER32.@)
899 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
901 TRACE_(keyboard)("(%p, %d)\n", hLayout, flags);
903 return USER_Driver->pActivateKeyboardLayout(hLayout, flags);
906 /**********************************************************************
907 * BlockInput (USER32.@)
909 BOOL WINAPI BlockInput(BOOL fBlockIt)
911 FIXME_(keyboard)("(%d): stub\n", fBlockIt);
912 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
914 return FALSE;
917 /***********************************************************************
918 * GetKeyboardLayoutList (USER32.@)
920 * Return number of values available if either input parm is
921 * 0, per MS documentation.
923 UINT WINAPI GetKeyboardLayoutList(INT nBuff, HKL *layouts)
925 TRACE_(keyboard)( "(%d, %p)\n", nBuff, layouts );
927 return USER_Driver->pGetKeyboardLayoutList( nBuff, layouts );
931 /***********************************************************************
932 * RegisterHotKey (USER32.@)
934 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk)
936 BOOL ret;
937 int replaced=0;
939 TRACE_(keyboard)("(%p,%d,0x%08x,%X)\n",hwnd,id,modifiers,vk);
941 if ((hwnd == NULL || WIN_IsCurrentThread(hwnd)) &&
942 !USER_Driver->pRegisterHotKey(hwnd, modifiers, vk))
943 return FALSE;
945 SERVER_START_REQ( register_hotkey )
947 req->window = wine_server_user_handle( hwnd );
948 req->id = id;
949 req->flags = modifiers;
950 req->vkey = vk;
951 if ((ret = !wine_server_call_err( req )))
953 replaced = reply->replaced;
954 modifiers = reply->flags;
955 vk = reply->vkey;
958 SERVER_END_REQ;
960 if (ret && replaced)
961 USER_Driver->pUnregisterHotKey(hwnd, modifiers, vk);
963 return ret;
966 /***********************************************************************
967 * UnregisterHotKey (USER32.@)
969 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id)
971 BOOL ret;
972 UINT modifiers, vk;
974 TRACE_(keyboard)("(%p,%d)\n",hwnd,id);
976 SERVER_START_REQ( unregister_hotkey )
978 req->window = wine_server_user_handle( hwnd );
979 req->id = id;
980 if ((ret = !wine_server_call_err( req )))
982 modifiers = reply->flags;
983 vk = reply->vkey;
986 SERVER_END_REQ;
988 if (ret)
989 USER_Driver->pUnregisterHotKey(hwnd, modifiers, vk);
991 return ret;
994 /***********************************************************************
995 * LoadKeyboardLayoutW (USER32.@)
997 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
999 TRACE_(keyboard)("(%s, %d)\n", debugstr_w(pwszKLID), Flags);
1001 return USER_Driver->pLoadKeyboardLayout(pwszKLID, Flags);
1004 /***********************************************************************
1005 * LoadKeyboardLayoutA (USER32.@)
1007 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
1009 HKL ret;
1010 UNICODE_STRING pwszKLIDW;
1012 if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
1013 else pwszKLIDW.Buffer = NULL;
1015 ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
1016 RtlFreeUnicodeString(&pwszKLIDW);
1017 return ret;
1021 /***********************************************************************
1022 * UnloadKeyboardLayout (USER32.@)
1024 BOOL WINAPI UnloadKeyboardLayout(HKL hkl)
1026 TRACE_(keyboard)("(%p)\n", hkl);
1028 return USER_Driver->pUnloadKeyboardLayout(hkl);
1031 typedef struct __TRACKINGLIST {
1032 TRACKMOUSEEVENT tme;
1033 POINT pos; /* center of hover rectangle */
1034 } _TRACKINGLIST;
1036 /* FIXME: move tracking stuff into a per thread data */
1037 static _TRACKINGLIST tracking_info;
1038 static UINT_PTR timer;
1040 static void check_mouse_leave(HWND hwnd, int hittest)
1042 if (tracking_info.tme.hwndTrack != hwnd)
1044 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1045 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
1046 else
1047 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
1049 /* remove the TME_LEAVE flag */
1050 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1052 else
1054 if (hittest == HTCLIENT)
1056 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1058 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
1059 /* remove the TME_LEAVE flag */
1060 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1063 else
1065 if (!(tracking_info.tme.dwFlags & TME_NONCLIENT))
1067 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
1068 /* remove the TME_LEAVE flag */
1069 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1075 static void CALLBACK TrackMouseEventProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent,
1076 DWORD dwTime)
1078 POINT pos;
1079 INT hoverwidth = 0, hoverheight = 0, hittest;
1081 TRACE("hwnd %p, msg %04x, id %04lx, time %u\n", hwnd, uMsg, idEvent, dwTime);
1083 GetCursorPos(&pos);
1084 hwnd = WINPOS_WindowFromPoint(hwnd, pos, &hittest);
1086 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
1088 SystemParametersInfoW(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
1089 SystemParametersInfoW(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
1091 TRACE("tracked pos %s, current pos %s, hover width %d, hover height %d\n",
1092 wine_dbgstr_point(&tracking_info.pos), wine_dbgstr_point(&pos),
1093 hoverwidth, hoverheight);
1095 /* see if this tracking event is looking for TME_LEAVE and that the */
1096 /* mouse has left the window */
1097 if (tracking_info.tme.dwFlags & TME_LEAVE)
1099 check_mouse_leave(hwnd, hittest);
1102 if (tracking_info.tme.hwndTrack != hwnd)
1104 /* mouse is gone, stop tracking mouse hover */
1105 tracking_info.tme.dwFlags &= ~TME_HOVER;
1108 /* see if we are tracking hovering for this hwnd */
1109 if (tracking_info.tme.dwFlags & TME_HOVER)
1111 /* has the cursor moved outside the rectangle centered around pos? */
1112 if ((abs(pos.x - tracking_info.pos.x) > (hoverwidth / 2)) ||
1113 (abs(pos.y - tracking_info.pos.y) > (hoverheight / 2)))
1115 /* record this new position as the current position */
1116 tracking_info.pos = pos;
1118 else
1120 if (hittest == HTCLIENT)
1122 ScreenToClient(hwnd, &pos);
1123 TRACE("client cursor pos %s\n", wine_dbgstr_point(&pos));
1125 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSEHOVER,
1126 get_key_state(), MAKELPARAM( pos.x, pos.y ));
1128 else
1130 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1131 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSEHOVER,
1132 hittest, MAKELPARAM( pos.x, pos.y ));
1135 /* stop tracking mouse hover */
1136 tracking_info.tme.dwFlags &= ~TME_HOVER;
1140 /* stop the timer if the tracking list is empty */
1141 if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
1143 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1144 timer = 0;
1145 tracking_info.tme.hwndTrack = 0;
1146 tracking_info.tme.dwFlags = 0;
1147 tracking_info.tme.dwHoverTime = 0;
1152 /***********************************************************************
1153 * TrackMouseEvent [USER32]
1155 * Requests notification of mouse events
1157 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
1158 * to the hwnd specified in the ptme structure. After the event message
1159 * is posted to the hwnd, the entry in the queue is removed.
1161 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
1162 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
1163 * immediately and the TME_LEAVE flag being ignored.
1165 * PARAMS
1166 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
1168 * RETURNS
1169 * Success: non-zero
1170 * Failure: zero
1174 BOOL WINAPI
1175 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
1177 HWND hwnd;
1178 POINT pos;
1179 DWORD hover_time;
1180 INT hittest;
1182 TRACE("%x, %x, %p, %u\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
1184 if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
1185 WARN("wrong TRACKMOUSEEVENT size from app\n");
1186 SetLastError(ERROR_INVALID_PARAMETER);
1187 return FALSE;
1190 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
1191 if (ptme->dwFlags & TME_QUERY )
1193 *ptme = tracking_info.tme;
1194 /* set cbSize in the case it's not initialized yet */
1195 ptme->cbSize = sizeof(TRACKMOUSEEVENT);
1197 return TRUE; /* return here, TME_QUERY is retrieving information */
1200 if (!IsWindow(ptme->hwndTrack))
1202 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
1203 return FALSE;
1206 hover_time = (ptme->dwFlags & TME_HOVER) ? ptme->dwHoverTime : HOVER_DEFAULT;
1208 /* if HOVER_DEFAULT was specified replace this with the system's current value.
1209 * TME_LEAVE doesn't need to specify hover time so use default */
1210 if (hover_time == HOVER_DEFAULT || hover_time == 0)
1211 SystemParametersInfoW(SPI_GETMOUSEHOVERTIME, 0, &hover_time, 0);
1213 GetCursorPos(&pos);
1214 hwnd = WINPOS_WindowFromPoint(ptme->hwndTrack, pos, &hittest);
1215 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
1217 if (ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT))
1218 FIXME("Unknown flag(s) %08x\n", ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT));
1220 if (ptme->dwFlags & TME_CANCEL)
1222 if (tracking_info.tme.hwndTrack == ptme->hwndTrack)
1224 tracking_info.tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
1226 /* if we aren't tracking on hover or leave remove this entry */
1227 if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
1229 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1230 timer = 0;
1231 tracking_info.tme.hwndTrack = 0;
1232 tracking_info.tme.dwFlags = 0;
1233 tracking_info.tme.dwHoverTime = 0;
1236 } else {
1237 /* In our implementation it's possible that another window will receive a
1238 * WM_MOUSEMOVE and call TrackMouseEvent before TrackMouseEventProc is
1239 * called. In such a situation post the WM_MOUSELEAVE now */
1240 if (tracking_info.tme.dwFlags & TME_LEAVE && tracking_info.tme.hwndTrack != NULL)
1241 check_mouse_leave(hwnd, hittest);
1243 if (timer)
1245 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1246 timer = 0;
1247 tracking_info.tme.hwndTrack = 0;
1248 tracking_info.tme.dwFlags = 0;
1249 tracking_info.tme.dwHoverTime = 0;
1252 if (ptme->hwndTrack == hwnd)
1254 /* Adding new mouse event to the tracking list */
1255 tracking_info.tme = *ptme;
1256 tracking_info.tme.dwHoverTime = hover_time;
1258 /* Initialize HoverInfo variables even if not hover tracking */
1259 tracking_info.pos = pos;
1261 timer = SetSystemTimer(tracking_info.tme.hwndTrack, (UINT_PTR)&tracking_info.tme, hover_time, TrackMouseEventProc);
1265 return TRUE;
1268 /***********************************************************************
1269 * GetMouseMovePointsEx [USER32]
1271 * RETURNS
1272 * Success: count of point set in the buffer
1273 * Failure: -1
1275 int WINAPI GetMouseMovePointsEx(UINT size, LPMOUSEMOVEPOINT ptin, LPMOUSEMOVEPOINT ptout, int count, DWORD res) {
1277 if((size != sizeof(MOUSEMOVEPOINT)) || (count < 0) || (count > 64)) {
1278 SetLastError(ERROR_INVALID_PARAMETER);
1279 return -1;
1282 if(!ptin || (!ptout && count)) {
1283 SetLastError(ERROR_NOACCESS);
1284 return -1;
1287 FIXME("(%d %p %p %d %d) stub\n", size, ptin, ptout, count, res);
1289 SetLastError(ERROR_POINT_NOT_FOUND);
1290 return -1;
1293 /***********************************************************************
1294 * EnableMouseInPointer (USER32.@)
1296 BOOL WINAPI EnableMouseInPointer(BOOL enable)
1298 FIXME("(%#x) stub\n", enable);
1300 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1301 return FALSE;