kernel32/tests: Also test wrong architecture with matching 32/64 bitness.
[wine.git] / dlls / user32 / input.c
blobce4b13f926e13b92063dec792feb4c5096a5b78e
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 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;
147 else
149 int accel[3];
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])
158 input->u.mi.dx *= 2;
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])
163 input->u.mi.dy *= 2;
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 )
174 UINT i;
175 NTSTATUS status;
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 );
188 if (status)
190 SetLastError( RtlNtStatusToDosError(status) );
191 break;
195 return i;
199 /***********************************************************************
200 * keybd_event (USER32.@)
202 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
203 DWORD dwFlags, ULONG_PTR dwExtraInfo )
205 INPUT input;
207 input.type = INPUT_KEYBOARD;
208 input.u.ki.wVk = bVk;
209 input.u.ki.wScan = bScan;
210 input.u.ki.dwFlags = dwFlags;
211 input.u.ki.time = 0;
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 )
223 INPUT input;
225 input.type = INPUT_MOUSE;
226 input.u.mi.dx = dx;
227 input.u.mi.dy = dy;
228 input.u.mi.mouseData = dwData;
229 input.u.mi.dwFlags = dwFlags;
230 input.u.mi.time = 0;
231 input.u.mi.dwExtraInfo = dwExtraInfo;
232 SendInput( 1, &input, sizeof(input) );
236 /***********************************************************************
237 * GetCursorPos (USER32.@)
239 BOOL WINAPI DECLSPEC_HOTPATCH GetCursorPos( POINT *pt )
241 BOOL ret;
242 DWORD last_change;
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;
255 SERVER_END_REQ;
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 );
259 return ret;
263 /***********************************************************************
264 * GetCursorInfo (USER32.@)
266 BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
268 BOOL ret;
270 if (!pci) return FALSE;
272 SERVER_START_REQ( get_thread_input )
274 req->tid = 0;
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;
281 SERVER_END_REQ;
282 GetCursorPos(&pci->ptScreenPos);
283 return ret;
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 )
302 BOOL ret;
303 INT prev_x, prev_y, new_x, new_y;
305 SERVER_START_REQ( set_cursor )
307 req->flags = SET_CURSOR_POS;
308 req->x = x;
309 req->y = y;
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;
318 SERVER_END_REQ;
319 if (ret && (prev_x != new_x || prev_y != new_y)) USER_Driver->pSetCursorPos( new_x, new_y );
320 return ret;
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 )
338 HWND previous = 0;
340 set_capture_window( hwnd, 0, &previous );
341 return 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 );
355 return ret;
359 /**********************************************************************
360 * GetCapture (USER32.@)
362 HWND WINAPI GetCapture(void)
364 HWND ret = 0;
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 );
371 SERVER_END_REQ;
372 return ret;
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
387 * been pressed.
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;
393 BYTE prev_key_state;
394 SHORT ret;
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 */
408 return 0;
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;
416 ret = 0;
417 SERVER_START_REQ( get_key_state )
419 req->tid = 0;
420 req->key = key;
421 if (key_state_info)
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;
430 if (key_state_info)
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;
443 SERVER_END_REQ;
445 return ret;
449 /***********************************************************************
450 * GetQueueStatus (USER32.@)
452 DWORD WINAPI GetQueueStatus( UINT flags )
454 DWORD ret;
456 if (flags & ~(QS_ALLINPUT | QS_ALLPOSTMESSAGE | QS_SMRESULT))
458 SetLastError( ERROR_INVALID_FLAGS );
459 return 0;
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 );
470 SERVER_END_REQ;
471 return ret;
475 /***********************************************************************
476 * GetInputState (USER32.@)
478 BOOL WINAPI GetInputState(void)
480 DWORD ret;
482 check_for_events( QS_INPUT );
484 SERVER_START_REQ( get_queue_status )
486 req->clear_bits = 0;
487 wine_server_call( req );
488 ret = reply->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
490 SERVER_END_REQ;
491 return ret;
495 /******************************************************************
496 * GetLastInputInfo (USER32.@)
498 BOOL WINAPI GetLastInputInfo(PLASTINPUTINFO plii)
500 BOOL ret;
502 TRACE("%p\n", plii);
504 if (plii->cbSize != sizeof (*plii) )
506 SetLastError(ERROR_INVALID_PARAMETER);
507 return FALSE;
510 SERVER_START_REQ( get_last_input_time )
512 ret = !wine_server_call_err( req );
513 if (ret)
514 plii->dwTime = reply->time;
516 SERVER_END_REQ;
517 return ret;
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);
531 return ~0U;
534 if (!device_count)
536 SetLastError(ERROR_NOACCESS);
537 return ~0U;
540 if (!devices)
542 *device_count = 2;
543 return 0;
546 if (*device_count < 2)
548 SetLastError(ERROR_INSUFFICIENT_BUFFER);
549 *device_count = 2;
550 return ~0U;
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;
558 return 2;
562 /******************************************************************
563 * RegisterRawInputDevices (USER32.@)
565 BOOL WINAPI DECLSPEC_HOTPATCH RegisterRawInputDevices(RAWINPUTDEVICE *devices, UINT device_count, UINT size)
567 struct rawinput_device *d;
568 BOOL ret;
569 UINT i;
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);
576 return FALSE;
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 );
600 SERVER_END_REQ;
602 HeapFree( GetProcessHeap(), 0, d );
604 return ret;
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;
614 UINT s;
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);
622 return ~0U;
625 switch (command)
627 case RID_INPUT:
628 s = ri->header.dwSize;
629 break;
630 case RID_HEADER:
631 s = sizeof(RAWINPUTHEADER);
632 break;
633 default:
634 return ~0U;
637 if (!data)
639 *data_size = s;
640 return 0;
643 if (*data_size < s) return ~0U;
644 memcpy(data, ri, s);
645 return s;
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);
656 return 0;
660 /******************************************************************
661 * GetRawInputDeviceInfoA (USER32.@)
663 UINT WINAPI GetRawInputDeviceInfoA(HANDLE device, UINT command, void *data, UINT *data_size)
665 UINT ret;
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);
673 return ret;
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;
689 UINT s;
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;
695 switch (command)
697 case RIDI_DEVICENAME:
698 if (device == WINE_MOUSE_HANDLE)
700 s = sizeof(mouse_name);
701 name = mouse_name;
703 else
705 s = sizeof(keyboard_name);
706 name = keyboard_name;
708 break;
709 case RIDI_DEVICEINFO:
710 s = sizeof(*info);
711 break;
712 default:
713 return ~0U;
716 if (!data)
718 *data_size = s;
719 return 0;
722 if (*data_size < s)
724 *data_size = s;
725 return ~0U;
728 if (command == RIDI_DEVICENAME)
730 memcpy(data, name, s);
731 return s;
734 info = data;
735 info->cbSize = sizeof(*info);
736 if (device == WINE_MOUSE_HANDLE)
738 info->dwType = RIM_TYPEMOUSE;
739 info->u.mouse = mouse_info;
741 else
743 info->dwType = RIM_TYPEKEYBOARD;
744 info->u.keyboard = keyboard_info;
746 return s;
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);
757 return 0;
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);
768 return 0;
772 /**********************************************************************
773 * AttachThreadInput (USER32.@)
775 * Attaches the input processing mechanism of one thread to that of
776 * another thread.
778 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
780 BOOL ret;
782 SERVER_START_REQ( attach_thread_input )
784 req->tid_from = from;
785 req->tid_to = to;
786 req->attach = attach;
787 ret = !wine_server_call_err( req );
789 SERVER_END_REQ;
790 return ret;
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)
803 SHORT retval = 0;
805 SERVER_START_REQ( get_key_state )
807 req->tid = GetCurrentThreadId();
808 req->key = vkey;
809 if (!wine_server_call( req )) retval = (signed char)reply->state;
811 SERVER_END_REQ;
812 TRACE("key (0x%x) -> %x\n", vkey, retval);
813 return retval;
817 /**********************************************************************
818 * GetKeyboardState (USER32.@)
820 BOOL WINAPI DECLSPEC_HOTPATCH GetKeyboardState( LPBYTE state )
822 BOOL ret;
824 TRACE("(%p)\n", state);
826 memset( state, 0, 256 );
827 SERVER_START_REQ( get_key_state )
829 req->tid = GetCurrentThreadId();
830 req->key = -1;
831 wine_server_set_reply( req, state, 256 );
832 ret = !wine_server_call_err( req );
834 SERVER_END_REQ;
835 return ret;
839 /**********************************************************************
840 * SetKeyboardState (USER32.@)
842 BOOL WINAPI SetKeyboardState( LPBYTE state )
844 BOOL ret;
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 );
852 SERVER_END_REQ;
853 return ret;
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 :
863 * 0 Unshifted
864 * 1 Shift
865 * 2 Ctrl
866 * 3-5 Shift-key combinations that are not used for characters
867 * 6 Ctrl-Alt
868 * 7 Ctrl-Alt-Shift
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)
876 WCHAR wChar;
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)
897 WCHAR wChar;
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 )
918 WCHAR wchr;
919 DWORD vkey, scan;
920 char oem_char = LOBYTE( oem );
922 if (!OemToCharBuffW( &oem_char, &wchr, 1 ))
923 return -1;
925 vkey = VkKeyScanW( wchr );
926 scan = MapVirtualKeyW( LOBYTE( vkey ), MAPVK_VK_TO_VSC );
927 if (!scan) return -1;
929 vkey &= 0xff00;
930 vkey <<= 8;
931 return vkey | scan;
934 /******************************************************************************
935 * GetKeyboardType (USER32.@)
937 INT WINAPI GetKeyboardType(INT nTypeFlag)
939 TRACE_(keyboard)("(%d)\n", nTypeFlag);
940 switch(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 */
948 default:
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)
975 UINT ret;
977 ret = MapVirtualKeyExW( code, maptype, hkl );
978 if (maptype == MAPVK_VK_TO_CHAR)
980 BYTE ch = 0;
981 WCHAR wch = ret;
983 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL );
984 ret = ch;
986 return ret;
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)
1004 return GetOEMCP();
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;
1028 return FALSE;
1031 /****************************************************************************
1032 * GetKeyboardLayoutNameW (USER32.@)
1034 BOOL WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
1036 if (!pwszKLID)
1038 SetLastError(ERROR_NOACCESS);
1039 return FALSE;
1041 return USER_Driver->pGetKeyboardLayoutName(pwszKLID);
1044 /****************************************************************************
1045 * GetKeyNameTextA (USER32.@)
1047 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
1049 WCHAR buf[256];
1050 INT ret;
1052 if (!nSize || !GetKeyNameTextW(lParam, buf, 256))
1054 lpBuffer[0] = 0;
1055 return 0;
1057 ret = WideCharToMultiByte(CP_ACP, 0, buf, -1, lpBuffer, nSize, NULL, NULL);
1058 if (!ret && nSize)
1060 ret = nSize - 1;
1061 lpBuffer[ret] = 0;
1063 else ret--;
1065 return ret;
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 )
1111 WCHAR uni_chars[2];
1112 INT ret, n_ret;
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 */
1116 else n_ret = ret;
1117 WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
1118 return ret;
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);
1139 return FALSE;
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)
1161 BOOL ret;
1162 int replaced=0;
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))
1168 return FALSE;
1170 SERVER_START_REQ( register_hotkey )
1172 req->window = wine_server_user_handle( hwnd );
1173 req->id = id;
1174 req->flags = modifiers;
1175 req->vkey = vk;
1176 if ((ret = !wine_server_call_err( req )))
1178 replaced = reply->replaced;
1179 modifiers = reply->flags;
1180 vk = reply->vkey;
1183 SERVER_END_REQ;
1185 if (ret && replaced)
1186 USER_Driver->pUnregisterHotKey(hwnd, modifiers, vk);
1188 return ret;
1191 /***********************************************************************
1192 * UnregisterHotKey (USER32.@)
1194 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id)
1196 BOOL ret;
1197 UINT modifiers, vk;
1199 TRACE_(keyboard)("(%p,%d)\n",hwnd,id);
1201 SERVER_START_REQ( unregister_hotkey )
1203 req->window = wine_server_user_handle( hwnd );
1204 req->id = id;
1205 if ((ret = !wine_server_call_err( req )))
1207 modifiers = reply->flags;
1208 vk = reply->vkey;
1211 SERVER_END_REQ;
1213 if (ret)
1214 USER_Driver->pUnregisterHotKey(hwnd, modifiers, vk);
1216 return ret;
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)
1234 HKL ret;
1235 UNICODE_STRING pwszKLIDW;
1237 if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
1238 else pwszKLIDW.Buffer = NULL;
1240 ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
1241 RtlFreeUnicodeString(&pwszKLIDW);
1242 return ret;
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 */
1259 } _TRACKINGLIST;
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);
1271 else
1272 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
1274 /* remove the TME_LEAVE flag */
1275 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1277 else
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;
1288 else
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,
1301 DWORD dwTime)
1303 POINT pos;
1304 INT hoverwidth = 0, hoverheight = 0, hittest;
1306 TRACE("hwnd %p, msg %04x, id %04lx, time %u\n", hwnd, uMsg, idEvent, dwTime);
1308 GetCursorPos(&pos);
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;
1343 else
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 ));
1353 else
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);
1369 timer = 0;
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.
1390 * PARAMS
1391 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
1393 * RETURNS
1394 * Success: non-zero
1395 * Failure: zero
1399 BOOL WINAPI
1400 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
1402 HWND hwnd;
1403 POINT pos;
1404 DWORD hover_time;
1405 INT hittest;
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);
1412 return FALSE;
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);
1428 return FALSE;
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);
1438 GetCursorPos(&pos);
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);
1455 timer = 0;
1456 tracking_info.tme.hwndTrack = 0;
1457 tracking_info.tme.dwFlags = 0;
1458 tracking_info.tme.dwHoverTime = 0;
1461 } else {
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);
1468 if (timer)
1470 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1471 timer = 0;
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);
1490 return TRUE;
1493 /***********************************************************************
1494 * GetMouseMovePointsEx [USER32]
1496 * RETURNS
1497 * Success: count of point set in the buffer
1498 * Failure: -1
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);
1504 return -1;
1507 if(!ptin || (!ptout && count)) {
1508 SetLastError(ERROR_NOACCESS);
1509 return -1;
1512 FIXME("(%d %p %p %d %d) stub\n", size, ptin, ptout, count, res);
1514 SetLastError(ERROR_POINT_NOT_FOUND);
1515 return -1;