user32: Implement GetMouseMovePointsEx().
[wine.git] / dlls / user32 / input.c
blobe06f8b4413ebff158615fd9677a25708bff1d2a5
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 <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <assert.h>
32 #define NONAMELESSUNION
34 #include "ntstatus.h"
35 #define WIN32_NO_STATUS
36 #include "windef.h"
37 #include "winbase.h"
38 #include "wingdi.h"
39 #include "winuser.h"
40 #include "winnls.h"
41 #include "winternl.h"
42 #include "winerror.h"
43 #include "win.h"
44 #include "user_private.h"
45 #include "dbt.h"
46 #include "wine/server.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(win);
50 WINE_DECLARE_DEBUG_CHANNEL(keyboard);
52 INT global_key_state_counter = 0;
54 /***********************************************************************
55 * get_key_state
57 static WORD get_key_state(void)
59 WORD ret = 0;
61 if (GetSystemMetrics( SM_SWAPBUTTON ))
63 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_LBUTTON;
64 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_RBUTTON;
66 else
68 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_LBUTTON;
69 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_RBUTTON;
71 if (GetAsyncKeyState(VK_MBUTTON) & 0x80) ret |= MK_MBUTTON;
72 if (GetAsyncKeyState(VK_SHIFT) & 0x80) ret |= MK_SHIFT;
73 if (GetAsyncKeyState(VK_CONTROL) & 0x80) ret |= MK_CONTROL;
74 if (GetAsyncKeyState(VK_XBUTTON1) & 0x80) ret |= MK_XBUTTON1;
75 if (GetAsyncKeyState(VK_XBUTTON2) & 0x80) ret |= MK_XBUTTON2;
76 return ret;
80 /**********************************************************************
81 * set_capture_window
83 BOOL set_capture_window( HWND hwnd, UINT gui_flags, HWND *prev_ret )
85 HWND previous = 0;
86 UINT flags = 0;
87 BOOL ret;
89 if (gui_flags & GUI_INMENUMODE) flags |= CAPTURE_MENU;
90 if (gui_flags & GUI_INMOVESIZE) flags |= CAPTURE_MOVESIZE;
92 SERVER_START_REQ( set_capture_window )
94 req->handle = wine_server_user_handle( hwnd );
95 req->flags = flags;
96 if ((ret = !wine_server_call_err( req )))
98 previous = wine_server_ptr_handle( reply->previous );
99 hwnd = wine_server_ptr_handle( reply->full_handle );
102 SERVER_END_REQ;
104 if (ret)
106 USER_Driver->pSetCapture( hwnd, gui_flags );
108 if (previous)
109 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
111 if (prev_ret) *prev_ret = previous;
113 return ret;
117 /***********************************************************************
118 * __wine_send_input (USER32.@)
120 * Internal SendInput function to allow the graphics driver to inject real events.
122 BOOL CDECL __wine_send_input( HWND hwnd, const INPUT *input )
124 NTSTATUS status = send_hardware_message( hwnd, input, 0 );
125 if (status) SetLastError( RtlNtStatusToDosError(status) );
126 return !status;
130 /***********************************************************************
131 * update_mouse_coords
133 * Helper for SendInput.
135 static void update_mouse_coords( INPUT *input )
137 if (!(input->u.mi.dwFlags & MOUSEEVENTF_MOVE)) return;
139 if (input->u.mi.dwFlags & MOUSEEVENTF_ABSOLUTE)
141 DPI_AWARENESS_CONTEXT context = SetThreadDpiAwarenessContext( DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE );
142 if (input->u.mi.dwFlags & MOUSEEVENTF_VIRTUALDESK)
144 RECT rc = get_virtual_screen_rect();
145 input->u.mi.dx = rc.left + ((input->u.mi.dx * (rc.right - rc.left)) >> 16);
146 input->u.mi.dy = rc.top + ((input->u.mi.dy * (rc.bottom - rc.top)) >> 16);
148 else
150 input->u.mi.dx = (input->u.mi.dx * GetSystemMetrics( SM_CXSCREEN )) >> 16;
151 input->u.mi.dy = (input->u.mi.dy * GetSystemMetrics( SM_CYSCREEN )) >> 16;
153 SetThreadDpiAwarenessContext( context );
155 else
157 int accel[3];
159 /* dx and dy can be negative numbers for relative movements */
160 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
162 if (!accel[2]) return;
164 if (abs(input->u.mi.dx) > accel[0])
166 input->u.mi.dx *= 2;
167 if ((abs(input->u.mi.dx) > accel[1]) && (accel[2] == 2)) input->u.mi.dx *= 2;
169 if (abs(input->u.mi.dy) > accel[0])
171 input->u.mi.dy *= 2;
172 if ((abs(input->u.mi.dy) > accel[1]) && (accel[2] == 2)) input->u.mi.dy *= 2;
177 /***********************************************************************
178 * SendInput (USER32.@)
180 UINT WINAPI SendInput( UINT count, LPINPUT inputs, int size )
182 UINT i;
183 NTSTATUS status;
185 for (i = 0; i < count; i++)
187 if (inputs[i].type == INPUT_MOUSE)
189 /* we need to update the coordinates to what the server expects */
190 INPUT input = inputs[i];
191 update_mouse_coords( &input );
192 status = send_hardware_message( 0, &input, SEND_HWMSG_INJECTED );
194 else status = send_hardware_message( 0, &inputs[i], SEND_HWMSG_INJECTED );
196 if (status)
198 SetLastError( RtlNtStatusToDosError(status) );
199 break;
203 return i;
207 /***********************************************************************
208 * keybd_event (USER32.@)
210 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
211 DWORD dwFlags, ULONG_PTR dwExtraInfo )
213 INPUT input;
215 input.type = INPUT_KEYBOARD;
216 input.u.ki.wVk = bVk;
217 input.u.ki.wScan = bScan;
218 input.u.ki.dwFlags = dwFlags;
219 input.u.ki.time = 0;
220 input.u.ki.dwExtraInfo = dwExtraInfo;
221 SendInput( 1, &input, sizeof(input) );
225 /***********************************************************************
226 * mouse_event (USER32.@)
228 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
229 DWORD dwData, ULONG_PTR dwExtraInfo )
231 INPUT input;
233 input.type = INPUT_MOUSE;
234 input.u.mi.dx = dx;
235 input.u.mi.dy = dy;
236 input.u.mi.mouseData = dwData;
237 input.u.mi.dwFlags = dwFlags;
238 input.u.mi.time = 0;
239 input.u.mi.dwExtraInfo = dwExtraInfo;
240 SendInput( 1, &input, sizeof(input) );
244 /***********************************************************************
245 * GetCursorPos (USER32.@)
247 BOOL WINAPI DECLSPEC_HOTPATCH GetCursorPos( POINT *pt )
249 BOOL ret;
250 DWORD last_change;
251 UINT dpi;
253 if (!pt) return FALSE;
255 SERVER_START_REQ( set_cursor )
257 if ((ret = !wine_server_call( req )))
259 pt->x = reply->new_x;
260 pt->y = reply->new_y;
261 last_change = reply->last_change;
264 SERVER_END_REQ;
266 /* query new position from graphics driver if we haven't updated recently */
267 if (ret && GetTickCount() - last_change > 100) ret = USER_Driver->pGetCursorPos( pt );
268 if (ret && (dpi = get_thread_dpi()))
270 DPI_AWARENESS_CONTEXT context;
271 context = SetThreadDpiAwarenessContext( DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE );
272 *pt = map_dpi_point( *pt, get_monitor_dpi( MonitorFromPoint( *pt, MONITOR_DEFAULTTOPRIMARY )), dpi );
273 SetThreadDpiAwarenessContext( context );
275 return ret;
279 /***********************************************************************
280 * GetCursorInfo (USER32.@)
282 BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
284 BOOL ret;
286 if (!pci) return FALSE;
288 SERVER_START_REQ( get_thread_input )
290 req->tid = 0;
291 if ((ret = !wine_server_call( req )))
293 pci->hCursor = wine_server_ptr_handle( reply->cursor );
294 pci->flags = (reply->show_count >= 0) ? CURSOR_SHOWING : 0;
297 SERVER_END_REQ;
298 GetCursorPos(&pci->ptScreenPos);
299 return ret;
303 /***********************************************************************
304 * SetCursorPos (USER32.@)
306 BOOL WINAPI DECLSPEC_HOTPATCH SetCursorPos( INT x, INT y )
308 POINT pt = { x, y };
309 BOOL ret;
310 INT prev_x, prev_y, new_x, new_y;
311 UINT dpi;
313 if ((dpi = get_thread_dpi()))
314 pt = map_dpi_point( pt, dpi, get_monitor_dpi( MonitorFromPoint( pt, MONITOR_DEFAULTTOPRIMARY )));
316 SERVER_START_REQ( set_cursor )
318 req->flags = SET_CURSOR_POS;
319 req->x = pt.x;
320 req->y = pt.y;
321 if ((ret = !wine_server_call( req )))
323 prev_x = reply->prev_x;
324 prev_y = reply->prev_y;
325 new_x = reply->new_x;
326 new_y = reply->new_y;
329 SERVER_END_REQ;
330 if (ret && (prev_x != new_x || prev_y != new_y)) USER_Driver->pSetCursorPos( new_x, new_y );
331 return ret;
334 /**********************************************************************
335 * SetCapture (USER32.@)
337 HWND WINAPI DECLSPEC_HOTPATCH SetCapture( HWND hwnd )
339 HWND previous = 0;
341 set_capture_window( hwnd, 0, &previous );
342 return previous;
346 /**********************************************************************
347 * ReleaseCapture (USER32.@)
349 BOOL WINAPI DECLSPEC_HOTPATCH ReleaseCapture(void)
351 BOOL ret = set_capture_window( 0, 0, NULL );
353 /* Somebody may have missed some mouse movements */
354 if (ret) mouse_event( MOUSEEVENTF_MOVE, 0, 0, 0, 0 );
356 return ret;
360 /**********************************************************************
361 * GetCapture (USER32.@)
363 HWND WINAPI GetCapture(void)
365 HWND ret = 0;
367 SERVER_START_REQ( get_thread_input )
369 req->tid = GetCurrentThreadId();
370 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->capture );
372 SERVER_END_REQ;
373 return ret;
377 static void check_for_events( UINT flags )
379 if (USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, flags, 0 ) == WAIT_TIMEOUT)
380 flush_window_surfaces( TRUE );
383 /**********************************************************************
384 * GetAsyncKeyState (USER32.@)
386 * Determine if a key is or was pressed. retval has high-order
387 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
388 * been pressed.
390 SHORT WINAPI DECLSPEC_HOTPATCH GetAsyncKeyState( INT key )
392 struct user_key_state_info *key_state_info = get_user_thread_info()->key_state;
393 INT counter = global_key_state_counter;
394 BYTE prev_key_state;
395 SHORT ret;
397 if (key < 0 || key >= 256) return 0;
399 check_for_events( QS_INPUT );
401 if (key_state_info && !(key_state_info->state[key] & 0xc0) &&
402 key_state_info->counter == counter && GetTickCount() - key_state_info->time < 50)
404 /* use cached value */
405 return 0;
407 else if (!key_state_info)
409 key_state_info = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*key_state_info) );
410 get_user_thread_info()->key_state = key_state_info;
413 ret = 0;
414 SERVER_START_REQ( get_key_state )
416 req->tid = 0;
417 req->key = key;
418 if (key_state_info)
420 prev_key_state = key_state_info->state[key];
421 wine_server_set_reply( req, key_state_info->state, sizeof(key_state_info->state) );
423 if (!wine_server_call( req ))
425 if (reply->state & 0x40) ret |= 0x0001;
426 if (reply->state & 0x80) ret |= 0x8000;
427 if (key_state_info)
429 /* force refreshing the key state cache - some multithreaded programs
430 * (like Adobe Photoshop CS5) expect that changes to the async key state
431 * are also immediately available in other threads. */
432 if (prev_key_state != key_state_info->state[key])
433 counter = InterlockedIncrement( &global_key_state_counter );
435 key_state_info->time = GetTickCount();
436 key_state_info->counter = counter;
440 SERVER_END_REQ;
442 return ret;
446 /***********************************************************************
447 * GetQueueStatus (USER32.@)
449 DWORD WINAPI GetQueueStatus( UINT flags )
451 DWORD ret;
453 if (flags & ~(QS_ALLINPUT | QS_ALLPOSTMESSAGE | QS_SMRESULT))
455 SetLastError( ERROR_INVALID_FLAGS );
456 return 0;
459 check_for_events( flags );
461 SERVER_START_REQ( get_queue_status )
463 req->clear_bits = flags;
464 wine_server_call( req );
465 ret = MAKELONG( reply->changed_bits & flags, reply->wake_bits & flags );
467 SERVER_END_REQ;
468 return ret;
472 /***********************************************************************
473 * GetInputState (USER32.@)
475 BOOL WINAPI GetInputState(void)
477 DWORD ret;
479 check_for_events( QS_INPUT );
481 SERVER_START_REQ( get_queue_status )
483 req->clear_bits = 0;
484 wine_server_call( req );
485 ret = reply->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
487 SERVER_END_REQ;
488 return ret;
492 /******************************************************************
493 * GetLastInputInfo (USER32.@)
495 BOOL WINAPI GetLastInputInfo(PLASTINPUTINFO plii)
497 BOOL ret;
499 TRACE("%p\n", plii);
501 if (plii->cbSize != sizeof (*plii) )
503 SetLastError(ERROR_INVALID_PARAMETER);
504 return FALSE;
507 SERVER_START_REQ( get_last_input_time )
509 ret = !wine_server_call_err( req );
510 if (ret)
511 plii->dwTime = reply->time;
513 SERVER_END_REQ;
514 return ret;
518 /**********************************************************************
519 * AttachThreadInput (USER32.@)
521 * Attaches the input processing mechanism of one thread to that of
522 * another thread.
524 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
526 BOOL ret;
528 SERVER_START_REQ( attach_thread_input )
530 req->tid_from = from;
531 req->tid_to = to;
532 req->attach = attach;
533 ret = !wine_server_call_err( req );
535 SERVER_END_REQ;
536 return ret;
540 /**********************************************************************
541 * GetKeyState (USER32.@)
543 * An application calls the GetKeyState function in response to a
544 * keyboard-input message. This function retrieves the state of the key
545 * at the time the input message was generated.
547 SHORT WINAPI DECLSPEC_HOTPATCH GetKeyState(INT vkey)
549 SHORT retval = 0;
551 SERVER_START_REQ( get_key_state )
553 req->tid = GetCurrentThreadId();
554 req->key = vkey;
555 if (!wine_server_call( req )) retval = (signed char)(reply->state & 0x81);
557 SERVER_END_REQ;
558 TRACE("key (0x%x) -> %x\n", vkey, retval);
559 return retval;
563 /**********************************************************************
564 * GetKeyboardState (USER32.@)
566 BOOL WINAPI DECLSPEC_HOTPATCH GetKeyboardState( LPBYTE state )
568 BOOL ret;
569 UINT i;
571 TRACE("(%p)\n", state);
573 memset( state, 0, 256 );
574 SERVER_START_REQ( get_key_state )
576 req->tid = GetCurrentThreadId();
577 req->key = -1;
578 wine_server_set_reply( req, state, 256 );
579 ret = !wine_server_call_err( req );
580 for (i = 0; i < 256; i++) state[i] &= 0x81;
582 SERVER_END_REQ;
583 return ret;
587 /**********************************************************************
588 * SetKeyboardState (USER32.@)
590 BOOL WINAPI SetKeyboardState( LPBYTE state )
592 BOOL ret;
594 SERVER_START_REQ( set_key_state )
596 req->tid = GetCurrentThreadId();
597 wine_server_add_data( req, state, 256 );
598 ret = !wine_server_call_err( req );
600 SERVER_END_REQ;
601 return ret;
605 /**********************************************************************
606 * VkKeyScanA (USER32.@)
608 * VkKeyScan translates an ANSI character to a virtual-key and shift code
609 * for the current keyboard.
610 * high-order byte yields :
611 * 0 Unshifted
612 * 1 Shift
613 * 2 Ctrl
614 * 3-5 Shift-key combinations that are not used for characters
615 * 6 Ctrl-Alt
616 * 7 Ctrl-Alt-Shift
617 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
618 * FIXME : works ok except for dead chars :
619 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
620 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
622 SHORT WINAPI VkKeyScanA(CHAR cChar)
624 WCHAR wChar;
626 if (IsDBCSLeadByte(cChar)) return -1;
628 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
629 return VkKeyScanW(wChar);
632 /******************************************************************************
633 * VkKeyScanW (USER32.@)
635 SHORT WINAPI VkKeyScanW(WCHAR cChar)
637 return VkKeyScanExW(cChar, GetKeyboardLayout(0));
640 /**********************************************************************
641 * VkKeyScanExA (USER32.@)
643 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
645 WCHAR wChar;
647 if (IsDBCSLeadByte(cChar)) return -1;
649 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
650 return VkKeyScanExW(wChar, dwhkl);
653 /******************************************************************************
654 * VkKeyScanExW (USER32.@)
656 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
658 return USER_Driver->pVkKeyScanEx(cChar, dwhkl);
661 /**********************************************************************
662 * OemKeyScan (USER32.@)
664 DWORD WINAPI OemKeyScan( WORD oem )
666 WCHAR wchr;
667 DWORD vkey, scan;
668 char oem_char = LOBYTE( oem );
670 if (!OemToCharBuffW( &oem_char, &wchr, 1 ))
671 return -1;
673 vkey = VkKeyScanW( wchr );
674 scan = MapVirtualKeyW( LOBYTE( vkey ), MAPVK_VK_TO_VSC );
675 if (!scan) return -1;
677 vkey &= 0xff00;
678 vkey <<= 8;
679 return vkey | scan;
682 /******************************************************************************
683 * GetKeyboardType (USER32.@)
685 INT WINAPI GetKeyboardType(INT nTypeFlag)
687 TRACE_(keyboard)("(%d)\n", nTypeFlag);
688 if (LOWORD(GetKeyboardLayout(0)) == MAKELANGID(LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN))
690 /* scan code for `_', the key left of r-shift, in Japanese 106 keyboard */
691 const UINT JP106_VSC_USCORE = 0x73;
693 switch(nTypeFlag)
695 case 0: /* Keyboard type */
696 return 7; /* Japanese keyboard */
697 case 1: /* Keyboard Subtype */
698 /* Test keyboard mappings to detect Japanese keyboard */
699 if (MapVirtualKeyW(VK_OEM_102, MAPVK_VK_TO_VSC) == JP106_VSC_USCORE
700 && MapVirtualKeyW(JP106_VSC_USCORE, MAPVK_VSC_TO_VK) == VK_OEM_102)
701 return 2; /* Japanese 106 */
702 else
703 return 0; /* AT-101 */
704 case 2: /* Number of F-keys */
705 return 12; /* It has 12 F-keys */
708 else
710 switch(nTypeFlag)
712 case 0: /* Keyboard type */
713 return 4; /* AT-101 */
714 case 1: /* Keyboard Subtype */
715 return 0; /* There are no defined subtypes */
716 case 2: /* Number of F-keys */
717 return 12; /* We're doing an 101 for now, so return 12 F-keys */
720 WARN_(keyboard)("Unknown type\n");
721 return 0; /* The book says 0 here, so 0 */
724 /******************************************************************************
725 * MapVirtualKeyA (USER32.@)
727 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
729 return MapVirtualKeyExA( code, maptype, GetKeyboardLayout(0) );
732 /******************************************************************************
733 * MapVirtualKeyW (USER32.@)
735 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
737 return MapVirtualKeyExW(code, maptype, GetKeyboardLayout(0));
740 /******************************************************************************
741 * MapVirtualKeyExA (USER32.@)
743 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
745 UINT ret;
747 ret = MapVirtualKeyExW( code, maptype, hkl );
748 if (maptype == MAPVK_VK_TO_CHAR)
750 BYTE ch = 0;
751 WCHAR wch = ret;
753 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL );
754 ret = ch;
756 return ret;
759 /******************************************************************************
760 * MapVirtualKeyExW (USER32.@)
762 UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
764 TRACE_(keyboard)("(%X, %d, %p)\n", code, maptype, hkl);
766 return USER_Driver->pMapVirtualKeyEx(code, maptype, hkl);
769 /****************************************************************************
770 * GetKBCodePage (USER32.@)
772 UINT WINAPI GetKBCodePage(void)
774 return GetOEMCP();
777 /***********************************************************************
778 * GetKeyboardLayout (USER32.@)
780 * - device handle for keyboard layout defaulted to
781 * the language id. This is the way Windows default works.
782 * - the thread identifier is also ignored.
784 HKL WINAPI GetKeyboardLayout(DWORD thread_id)
786 return USER_Driver->pGetKeyboardLayout(thread_id);
789 /****************************************************************************
790 * GetKeyboardLayoutNameA (USER32.@)
792 BOOL WINAPI GetKeyboardLayoutNameA(LPSTR pszKLID)
794 WCHAR buf[KL_NAMELENGTH];
796 if (GetKeyboardLayoutNameW(buf))
797 return WideCharToMultiByte( CP_ACP, 0, buf, -1, pszKLID, KL_NAMELENGTH, NULL, NULL ) != 0;
798 return FALSE;
801 /****************************************************************************
802 * GetKeyboardLayoutNameW (USER32.@)
804 BOOL WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
806 if (!pwszKLID)
808 SetLastError(ERROR_NOACCESS);
809 return FALSE;
811 return USER_Driver->pGetKeyboardLayoutName(pwszKLID);
814 /****************************************************************************
815 * GetKeyNameTextA (USER32.@)
817 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
819 WCHAR buf[256];
820 INT ret;
822 if (!nSize || !GetKeyNameTextW(lParam, buf, 256))
824 lpBuffer[0] = 0;
825 return 0;
827 ret = WideCharToMultiByte(CP_ACP, 0, buf, -1, lpBuffer, nSize, NULL, NULL);
828 if (!ret && nSize)
830 ret = nSize - 1;
831 lpBuffer[ret] = 0;
833 else ret--;
835 return ret;
838 /****************************************************************************
839 * GetKeyNameTextW (USER32.@)
841 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
843 if (!lpBuffer || !nSize) return 0;
844 return USER_Driver->pGetKeyNameText( lParam, lpBuffer, nSize );
847 /****************************************************************************
848 * ToUnicode (USER32.@)
850 INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
851 LPWSTR lpwStr, int size, UINT flags)
853 return ToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, GetKeyboardLayout(0));
856 /****************************************************************************
857 * ToUnicodeEx (USER32.@)
859 INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
860 LPWSTR lpwStr, int size, UINT flags, HKL hkl)
862 if (!lpKeyState) return 0;
863 return USER_Driver->pToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, hkl);
866 /****************************************************************************
867 * ToAscii (USER32.@)
869 INT WINAPI ToAscii( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
870 LPWORD lpChar, UINT flags )
872 return ToAsciiEx(virtKey, scanCode, lpKeyState, lpChar, flags, GetKeyboardLayout(0));
875 /****************************************************************************
876 * ToAsciiEx (USER32.@)
878 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
879 LPWORD lpChar, UINT flags, HKL dwhkl )
881 WCHAR uni_chars[2];
882 INT ret, n_ret;
884 ret = ToUnicodeEx(virtKey, scanCode, lpKeyState, uni_chars, 2, flags, dwhkl);
885 if (ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
886 else n_ret = ret;
887 WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
888 return ret;
891 /**********************************************************************
892 * ActivateKeyboardLayout (USER32.@)
894 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
896 TRACE_(keyboard)("(%p, %d)\n", hLayout, flags);
898 return USER_Driver->pActivateKeyboardLayout(hLayout, flags);
901 /**********************************************************************
902 * BlockInput (USER32.@)
904 BOOL WINAPI BlockInput(BOOL fBlockIt)
906 FIXME_(keyboard)("(%d): stub\n", fBlockIt);
907 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
909 return FALSE;
912 /***********************************************************************
913 * GetKeyboardLayoutList (USER32.@)
915 * Return number of values available if either input parm is
916 * 0, per MS documentation.
918 UINT WINAPI GetKeyboardLayoutList(INT nBuff, HKL *layouts)
920 TRACE_(keyboard)( "(%d, %p)\n", nBuff, layouts );
922 return USER_Driver->pGetKeyboardLayoutList( nBuff, layouts );
926 /***********************************************************************
927 * RegisterHotKey (USER32.@)
929 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk)
931 BOOL ret;
932 int replaced=0;
934 TRACE_(keyboard)("(%p,%d,0x%08x,%X)\n",hwnd,id,modifiers,vk);
936 if ((hwnd == NULL || WIN_IsCurrentThread(hwnd)) &&
937 !USER_Driver->pRegisterHotKey(hwnd, modifiers, vk))
938 return FALSE;
940 SERVER_START_REQ( register_hotkey )
942 req->window = wine_server_user_handle( hwnd );
943 req->id = id;
944 req->flags = modifiers;
945 req->vkey = vk;
946 if ((ret = !wine_server_call_err( req )))
948 replaced = reply->replaced;
949 modifiers = reply->flags;
950 vk = reply->vkey;
953 SERVER_END_REQ;
955 if (ret && replaced)
956 USER_Driver->pUnregisterHotKey(hwnd, modifiers, vk);
958 return ret;
961 /***********************************************************************
962 * UnregisterHotKey (USER32.@)
964 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id)
966 BOOL ret;
967 UINT modifiers, vk;
969 TRACE_(keyboard)("(%p,%d)\n",hwnd,id);
971 SERVER_START_REQ( unregister_hotkey )
973 req->window = wine_server_user_handle( hwnd );
974 req->id = id;
975 if ((ret = !wine_server_call_err( req )))
977 modifiers = reply->flags;
978 vk = reply->vkey;
981 SERVER_END_REQ;
983 if (ret)
984 USER_Driver->pUnregisterHotKey(hwnd, modifiers, vk);
986 return ret;
989 /***********************************************************************
990 * LoadKeyboardLayoutW (USER32.@)
992 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
994 TRACE_(keyboard)("(%s, %d)\n", debugstr_w(pwszKLID), Flags);
996 return USER_Driver->pLoadKeyboardLayout(pwszKLID, Flags);
999 /***********************************************************************
1000 * LoadKeyboardLayoutA (USER32.@)
1002 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
1004 HKL ret;
1005 UNICODE_STRING pwszKLIDW;
1007 if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
1008 else pwszKLIDW.Buffer = NULL;
1010 ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
1011 RtlFreeUnicodeString(&pwszKLIDW);
1012 return ret;
1016 /***********************************************************************
1017 * UnloadKeyboardLayout (USER32.@)
1019 BOOL WINAPI UnloadKeyboardLayout(HKL hkl)
1021 TRACE_(keyboard)("(%p)\n", hkl);
1023 return USER_Driver->pUnloadKeyboardLayout(hkl);
1026 typedef struct __TRACKINGLIST {
1027 TRACKMOUSEEVENT tme;
1028 POINT pos; /* center of hover rectangle */
1029 } _TRACKINGLIST;
1031 /* FIXME: move tracking stuff into a per thread data */
1032 static _TRACKINGLIST tracking_info;
1033 static UINT_PTR timer;
1035 static void check_mouse_leave(HWND hwnd, int hittest)
1037 if (tracking_info.tme.hwndTrack != hwnd)
1039 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1040 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
1041 else
1042 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
1044 /* remove the TME_LEAVE flag */
1045 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1047 else
1049 if (hittest == HTCLIENT)
1051 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1053 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
1054 /* remove the TME_LEAVE flag */
1055 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1058 else
1060 if (!(tracking_info.tme.dwFlags & TME_NONCLIENT))
1062 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
1063 /* remove the TME_LEAVE flag */
1064 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1070 static void CALLBACK TrackMouseEventProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent,
1071 DWORD dwTime)
1073 POINT pos;
1074 INT hoverwidth = 0, hoverheight = 0, hittest;
1076 TRACE("hwnd %p, msg %04x, id %04lx, time %u\n", hwnd, uMsg, idEvent, dwTime);
1078 GetCursorPos(&pos);
1079 hwnd = WINPOS_WindowFromPoint(hwnd, pos, &hittest);
1081 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
1083 SystemParametersInfoW(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
1084 SystemParametersInfoW(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
1086 TRACE("tracked pos %s, current pos %s, hover width %d, hover height %d\n",
1087 wine_dbgstr_point(&tracking_info.pos), wine_dbgstr_point(&pos),
1088 hoverwidth, hoverheight);
1090 /* see if this tracking event is looking for TME_LEAVE and that the */
1091 /* mouse has left the window */
1092 if (tracking_info.tme.dwFlags & TME_LEAVE)
1094 check_mouse_leave(hwnd, hittest);
1097 if (tracking_info.tme.hwndTrack != hwnd)
1099 /* mouse is gone, stop tracking mouse hover */
1100 tracking_info.tme.dwFlags &= ~TME_HOVER;
1103 /* see if we are tracking hovering for this hwnd */
1104 if (tracking_info.tme.dwFlags & TME_HOVER)
1106 /* has the cursor moved outside the rectangle centered around pos? */
1107 if ((abs(pos.x - tracking_info.pos.x) > (hoverwidth / 2)) ||
1108 (abs(pos.y - tracking_info.pos.y) > (hoverheight / 2)))
1110 /* record this new position as the current position */
1111 tracking_info.pos = pos;
1113 else
1115 if (hittest == HTCLIENT)
1117 ScreenToClient(hwnd, &pos);
1118 TRACE("client cursor pos %s\n", wine_dbgstr_point(&pos));
1120 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSEHOVER,
1121 get_key_state(), MAKELPARAM( pos.x, pos.y ));
1123 else
1125 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1126 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSEHOVER,
1127 hittest, MAKELPARAM( pos.x, pos.y ));
1130 /* stop tracking mouse hover */
1131 tracking_info.tme.dwFlags &= ~TME_HOVER;
1135 /* stop the timer if the tracking list is empty */
1136 if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
1138 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1139 timer = 0;
1140 tracking_info.tme.hwndTrack = 0;
1141 tracking_info.tme.dwFlags = 0;
1142 tracking_info.tme.dwHoverTime = 0;
1147 /***********************************************************************
1148 * TrackMouseEvent [USER32]
1150 * Requests notification of mouse events
1152 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
1153 * to the hwnd specified in the ptme structure. After the event message
1154 * is posted to the hwnd, the entry in the queue is removed.
1156 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
1157 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
1158 * immediately and the TME_LEAVE flag being ignored.
1160 * PARAMS
1161 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
1163 * RETURNS
1164 * Success: non-zero
1165 * Failure: zero
1169 BOOL WINAPI
1170 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
1172 HWND hwnd;
1173 POINT pos;
1174 DWORD hover_time;
1175 INT hittest;
1177 TRACE("%x, %x, %p, %u\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
1179 if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
1180 WARN("wrong TRACKMOUSEEVENT size from app\n");
1181 SetLastError(ERROR_INVALID_PARAMETER);
1182 return FALSE;
1185 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
1186 if (ptme->dwFlags & TME_QUERY )
1188 *ptme = tracking_info.tme;
1189 /* set cbSize in the case it's not initialized yet */
1190 ptme->cbSize = sizeof(TRACKMOUSEEVENT);
1192 return TRUE; /* return here, TME_QUERY is retrieving information */
1195 if (!IsWindow(ptme->hwndTrack))
1197 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
1198 return FALSE;
1201 hover_time = (ptme->dwFlags & TME_HOVER) ? ptme->dwHoverTime : HOVER_DEFAULT;
1203 /* if HOVER_DEFAULT was specified replace this with the system's current value.
1204 * TME_LEAVE doesn't need to specify hover time so use default */
1205 if (hover_time == HOVER_DEFAULT || hover_time == 0)
1206 SystemParametersInfoW(SPI_GETMOUSEHOVERTIME, 0, &hover_time, 0);
1208 GetCursorPos(&pos);
1209 hwnd = WINPOS_WindowFromPoint(ptme->hwndTrack, pos, &hittest);
1210 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
1212 if (ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT))
1213 FIXME("Unknown flag(s) %08x\n", ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT));
1215 if (ptme->dwFlags & TME_CANCEL)
1217 if (tracking_info.tme.hwndTrack == ptme->hwndTrack)
1219 tracking_info.tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
1221 /* if we aren't tracking on hover or leave remove this entry */
1222 if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
1224 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1225 timer = 0;
1226 tracking_info.tme.hwndTrack = 0;
1227 tracking_info.tme.dwFlags = 0;
1228 tracking_info.tme.dwHoverTime = 0;
1231 } else {
1232 /* In our implementation it's possible that another window will receive a
1233 * WM_MOUSEMOVE and call TrackMouseEvent before TrackMouseEventProc is
1234 * called. In such a situation post the WM_MOUSELEAVE now */
1235 if (tracking_info.tme.dwFlags & TME_LEAVE && tracking_info.tme.hwndTrack != NULL)
1236 check_mouse_leave(hwnd, hittest);
1238 if (timer)
1240 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1241 timer = 0;
1242 tracking_info.tme.hwndTrack = 0;
1243 tracking_info.tme.dwFlags = 0;
1244 tracking_info.tme.dwHoverTime = 0;
1247 if (ptme->hwndTrack == hwnd)
1249 /* Adding new mouse event to the tracking list */
1250 tracking_info.tme = *ptme;
1251 tracking_info.tme.dwHoverTime = hover_time;
1253 /* Initialize HoverInfo variables even if not hover tracking */
1254 tracking_info.pos = pos;
1256 timer = SetSystemTimer(tracking_info.tme.hwndTrack, (UINT_PTR)&tracking_info.tme, hover_time, TrackMouseEventProc);
1260 return TRUE;
1263 /***********************************************************************
1264 * GetMouseMovePointsEx [USER32]
1266 * RETURNS
1267 * Success: count of point set in the buffer
1268 * Failure: -1
1270 int WINAPI GetMouseMovePointsEx( UINT size, LPMOUSEMOVEPOINT ptin, LPMOUSEMOVEPOINT ptout, int count, DWORD resolution )
1272 cursor_pos_t *pos, positions[64];
1273 int copied;
1274 unsigned int i;
1277 TRACE( "%d, %p, %p, %d, %d\n", size, ptin, ptout, count, resolution );
1279 if ((size != sizeof(MOUSEMOVEPOINT)) || (count < 0) || (count > ARRAY_SIZE( positions )))
1281 SetLastError( ERROR_INVALID_PARAMETER );
1282 return -1;
1285 if (!ptin || (!ptout && count))
1287 SetLastError( ERROR_NOACCESS );
1288 return -1;
1291 if (resolution != GMMP_USE_DISPLAY_POINTS)
1293 FIXME( "only GMMP_USE_DISPLAY_POINTS is supported for now\n" );
1294 SetLastError( ERROR_POINT_NOT_FOUND );
1295 return -1;
1298 SERVER_START_REQ( get_cursor_history )
1300 wine_server_set_reply( req, &positions, sizeof(positions) );
1301 if (wine_server_call_err( req )) return -1;
1303 SERVER_END_REQ;
1305 for (i = 0; i < ARRAY_SIZE( positions ); i++)
1307 pos = &positions[i];
1308 if (ptin->x == pos->x && ptin->y == pos->y && (!ptin->time || ptin->time == pos->time))
1309 break;
1312 if (i == ARRAY_SIZE( positions ))
1314 SetLastError( ERROR_POINT_NOT_FOUND );
1315 return -1;
1318 for (copied = 0; copied < count && i < ARRAY_SIZE( positions ); copied++, i++)
1320 pos = &positions[i];
1321 ptout[copied].x = pos->x;
1322 ptout[copied].y = pos->y;
1323 ptout[copied].time = pos->time;
1324 ptout[copied].dwExtraInfo = pos->info;
1327 return copied;
1330 /***********************************************************************
1331 * EnableMouseInPointer (USER32.@)
1333 BOOL WINAPI EnableMouseInPointer(BOOL enable)
1335 FIXME("(%#x) stub\n", enable);
1337 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1338 return FALSE;
1341 static DWORD CALLBACK devnotify_window_callback(HANDLE handle, DWORD flags, DEV_BROADCAST_HDR *header)
1343 SendMessageTimeoutW(handle, WM_DEVICECHANGE, flags, (LPARAM)header, SMTO_ABORTIFHUNG, 2000, NULL);
1344 return 0;
1347 static DWORD CALLBACK devnotify_service_callback(HANDLE handle, DWORD flags, DEV_BROADCAST_HDR *header)
1349 FIXME("Support for service handles is not yet implemented!\n");
1350 return 0;
1353 struct device_notification_details
1355 DWORD (CALLBACK *cb)(HANDLE handle, DWORD flags, DEV_BROADCAST_HDR *header);
1356 HANDLE handle;
1359 extern HDEVNOTIFY WINAPI I_ScRegisterDeviceNotification( struct device_notification_details *details,
1360 void *filter, DWORD flags );
1361 extern BOOL WINAPI I_ScUnregisterDeviceNotification( HDEVNOTIFY handle );
1363 /***********************************************************************
1364 * RegisterDeviceNotificationA (USER32.@)
1366 * See RegisterDeviceNotificationW.
1368 HDEVNOTIFY WINAPI RegisterDeviceNotificationA(HANDLE hRecipient, LPVOID pNotificationFilter, DWORD dwFlags)
1370 TRACE("(hwnd=%p, filter=%p,flags=0x%08x)\n",
1371 hRecipient,pNotificationFilter,dwFlags);
1372 if (pNotificationFilter)
1373 FIXME("The notification filter will requires an A->W when filter support is implemented\n");
1374 return RegisterDeviceNotificationW(hRecipient, pNotificationFilter, dwFlags);
1377 /***********************************************************************
1378 * RegisterDeviceNotificationW (USER32.@)
1380 HDEVNOTIFY WINAPI RegisterDeviceNotificationW( HANDLE handle, void *filter, DWORD flags )
1382 struct device_notification_details details;
1384 TRACE("handle %p, filter %p, flags %#x\n", handle, filter, flags);
1386 if (flags & ~(DEVICE_NOTIFY_SERVICE_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES))
1387 FIXME("unhandled flags %#x\n", flags);
1389 details.handle = handle;
1391 if (flags & DEVICE_NOTIFY_SERVICE_HANDLE)
1392 details.cb = devnotify_service_callback;
1393 else
1394 details.cb = devnotify_window_callback;
1396 return I_ScRegisterDeviceNotification( &details, filter, 0 );
1399 /***********************************************************************
1400 * UnregisterDeviceNotification (USER32.@)
1402 BOOL WINAPI UnregisterDeviceNotification( HDEVNOTIFY handle )
1404 TRACE("%p\n", handle);
1406 return I_ScUnregisterDeviceNotification( handle );