wined3d: Replace wined3d_surface_update_desc() with wined3d_texture_update_desc().
[wine.git] / dlls / user32 / input.c
blobec81e60a305d58b3b3a255e7008b2c959fdffea4
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
36 #define NONAMELESSSTRUCT
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);
56 /***********************************************************************
57 * get_key_state
59 static WORD get_key_state(void)
61 WORD ret = 0;
63 if (GetSystemMetrics( SM_SWAPBUTTON ))
65 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_LBUTTON;
66 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_RBUTTON;
68 else
70 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_LBUTTON;
71 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_RBUTTON;
73 if (GetAsyncKeyState(VK_MBUTTON) & 0x80) ret |= MK_MBUTTON;
74 if (GetAsyncKeyState(VK_SHIFT) & 0x80) ret |= MK_SHIFT;
75 if (GetAsyncKeyState(VK_CONTROL) & 0x80) ret |= MK_CONTROL;
76 if (GetAsyncKeyState(VK_XBUTTON1) & 0x80) ret |= MK_XBUTTON1;
77 if (GetAsyncKeyState(VK_XBUTTON2) & 0x80) ret |= MK_XBUTTON2;
78 return ret;
82 /**********************************************************************
83 * set_capture_window
85 BOOL set_capture_window( HWND hwnd, UINT gui_flags, HWND *prev_ret )
87 HWND previous = 0;
88 UINT flags = 0;
89 BOOL ret;
91 if (gui_flags & GUI_INMENUMODE) flags |= CAPTURE_MENU;
92 if (gui_flags & GUI_INMOVESIZE) flags |= CAPTURE_MOVESIZE;
94 SERVER_START_REQ( set_capture_window )
96 req->handle = wine_server_user_handle( hwnd );
97 req->flags = flags;
98 if ((ret = !wine_server_call_err( req )))
100 previous = wine_server_ptr_handle( reply->previous );
101 hwnd = wine_server_ptr_handle( reply->full_handle );
104 SERVER_END_REQ;
106 if (ret)
108 USER_Driver->pSetCapture( hwnd, gui_flags );
110 if (previous && previous != hwnd)
111 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
113 if (prev_ret) *prev_ret = previous;
115 return ret;
119 /***********************************************************************
120 * __wine_send_input (USER32.@)
122 * Internal SendInput function to allow the graphics driver to inject real events.
124 BOOL CDECL __wine_send_input( HWND hwnd, const INPUT *input )
126 NTSTATUS status = send_hardware_message( hwnd, input, 0 );
127 if (status) SetLastError( RtlNtStatusToDosError(status) );
128 return !status;
132 /***********************************************************************
133 * update_mouse_coords
135 * Helper for SendInput.
137 static void update_mouse_coords( INPUT *input )
139 if (!(input->u.mi.dwFlags & MOUSEEVENTF_MOVE)) return;
141 if (input->u.mi.dwFlags & MOUSEEVENTF_ABSOLUTE)
143 input->u.mi.dx = (input->u.mi.dx * GetSystemMetrics( SM_CXSCREEN )) >> 16;
144 input->u.mi.dy = (input->u.mi.dy * GetSystemMetrics( SM_CYSCREEN )) >> 16;
146 else
148 int accel[3];
150 /* dx and dy can be negative numbers for relative movements */
151 SystemParametersInfoW(SPI_GETMOUSE, 0, accel, 0);
153 if (!accel[2]) return;
155 if (abs(input->u.mi.dx) > accel[0])
157 input->u.mi.dx *= 2;
158 if ((abs(input->u.mi.dx) > accel[1]) && (accel[2] == 2)) input->u.mi.dx *= 2;
160 if (abs(input->u.mi.dy) > accel[0])
162 input->u.mi.dy *= 2;
163 if ((abs(input->u.mi.dy) > accel[1]) && (accel[2] == 2)) input->u.mi.dy *= 2;
168 /***********************************************************************
169 * SendInput (USER32.@)
171 UINT WINAPI SendInput( UINT count, LPINPUT inputs, int size )
173 UINT i;
174 NTSTATUS status;
176 for (i = 0; i < count; i++)
178 if (inputs[i].type == INPUT_MOUSE)
180 /* we need to update the coordinates to what the server expects */
181 INPUT input = inputs[i];
182 update_mouse_coords( &input );
183 status = send_hardware_message( 0, &input, SEND_HWMSG_INJECTED );
185 else status = send_hardware_message( 0, &inputs[i], SEND_HWMSG_INJECTED );
187 if (status)
189 SetLastError( RtlNtStatusToDosError(status) );
190 break;
194 return i;
198 /***********************************************************************
199 * keybd_event (USER32.@)
201 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
202 DWORD dwFlags, ULONG_PTR dwExtraInfo )
204 INPUT input;
206 input.type = INPUT_KEYBOARD;
207 input.u.ki.wVk = bVk;
208 input.u.ki.wScan = bScan;
209 input.u.ki.dwFlags = dwFlags;
210 input.u.ki.time = 0;
211 input.u.ki.dwExtraInfo = dwExtraInfo;
212 SendInput( 1, &input, sizeof(input) );
216 /***********************************************************************
217 * mouse_event (USER32.@)
219 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
220 DWORD dwData, ULONG_PTR dwExtraInfo )
222 INPUT input;
224 input.type = INPUT_MOUSE;
225 input.u.mi.dx = dx;
226 input.u.mi.dy = dy;
227 input.u.mi.mouseData = dwData;
228 input.u.mi.dwFlags = dwFlags;
229 input.u.mi.time = 0;
230 input.u.mi.dwExtraInfo = dwExtraInfo;
231 SendInput( 1, &input, sizeof(input) );
235 /***********************************************************************
236 * GetCursorPos (USER32.@)
238 BOOL WINAPI DECLSPEC_HOTPATCH GetCursorPos( POINT *pt )
240 BOOL ret;
241 DWORD last_change;
243 if (!pt) return FALSE;
245 SERVER_START_REQ( set_cursor )
247 if ((ret = !wine_server_call( req )))
249 pt->x = reply->new_x;
250 pt->y = reply->new_y;
251 last_change = reply->last_change;
254 SERVER_END_REQ;
256 /* query new position from graphics driver if we haven't updated recently */
257 if (ret && GetTickCount() - last_change > 100) ret = USER_Driver->pGetCursorPos( pt );
258 return ret;
262 /***********************************************************************
263 * GetCursorInfo (USER32.@)
265 BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
267 BOOL ret;
269 if (!pci) return FALSE;
271 SERVER_START_REQ( get_thread_input )
273 req->tid = 0;
274 if ((ret = !wine_server_call( req )))
276 pci->hCursor = wine_server_ptr_handle( reply->cursor );
277 pci->flags = (reply->show_count >= 0) ? CURSOR_SHOWING : 0;
280 SERVER_END_REQ;
281 GetCursorPos(&pci->ptScreenPos);
282 return ret;
286 /***********************************************************************
287 * SetCursorPos (USER32.@)
289 BOOL WINAPI DECLSPEC_HOTPATCH SetCursorPos( INT x, INT y )
291 BOOL ret;
292 INT prev_x, prev_y, new_x, new_y;
294 SERVER_START_REQ( set_cursor )
296 req->flags = SET_CURSOR_POS;
297 req->x = x;
298 req->y = y;
299 if ((ret = !wine_server_call( req )))
301 prev_x = reply->prev_x;
302 prev_y = reply->prev_y;
303 new_x = reply->new_x;
304 new_y = reply->new_y;
307 SERVER_END_REQ;
308 if (ret && (prev_x != new_x || prev_y != new_y)) USER_Driver->pSetCursorPos( new_x, new_y );
309 return ret;
313 /**********************************************************************
314 * SetCapture (USER32.@)
316 HWND WINAPI DECLSPEC_HOTPATCH SetCapture( HWND hwnd )
318 HWND previous = 0;
320 set_capture_window( hwnd, 0, &previous );
321 return previous;
325 /**********************************************************************
326 * ReleaseCapture (USER32.@)
328 BOOL WINAPI DECLSPEC_HOTPATCH ReleaseCapture(void)
330 BOOL ret = set_capture_window( 0, 0, NULL );
332 /* Somebody may have missed some mouse movements */
333 if (ret) mouse_event( MOUSEEVENTF_MOVE, 0, 0, 0, 0 );
335 return ret;
339 /**********************************************************************
340 * GetCapture (USER32.@)
342 HWND WINAPI GetCapture(void)
344 HWND ret = 0;
346 SERVER_START_REQ( get_thread_input )
348 req->tid = GetCurrentThreadId();
349 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->capture );
351 SERVER_END_REQ;
352 return ret;
356 static void check_for_events( UINT flags )
358 if (USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, flags, 0 ) == WAIT_TIMEOUT)
359 flush_window_surfaces( TRUE );
362 /**********************************************************************
363 * GetAsyncKeyState (USER32.@)
365 * Determine if a key is or was pressed. retval has high-order
366 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
367 * been pressed.
369 SHORT WINAPI DECLSPEC_HOTPATCH GetAsyncKeyState( INT key )
371 struct user_thread_info *thread_info = get_user_thread_info();
372 SHORT ret;
374 if (key < 0 || key >= 256) return 0;
376 check_for_events( QS_INPUT );
378 if ((ret = USER_Driver->pGetAsyncKeyState( key )) == -1)
380 if (thread_info->key_state &&
381 !(thread_info->key_state[key] & 0xc0) &&
382 GetTickCount() - thread_info->key_state_time < 50)
383 return 0;
385 if (!thread_info->key_state) thread_info->key_state = HeapAlloc( GetProcessHeap(), 0, 256 );
387 ret = 0;
388 SERVER_START_REQ( get_key_state )
390 req->tid = 0;
391 req->key = key;
392 if (thread_info->key_state) wine_server_set_reply( req, thread_info->key_state, 256 );
393 if (!wine_server_call( req ))
395 if (reply->state & 0x40) ret |= 0x0001;
396 if (reply->state & 0x80) ret |= 0x8000;
397 thread_info->key_state_time = GetTickCount();
400 SERVER_END_REQ;
402 return ret;
406 /***********************************************************************
407 * GetQueueStatus (USER32.@)
409 DWORD WINAPI GetQueueStatus( UINT flags )
411 DWORD ret;
413 if (flags & ~(QS_ALLINPUT | QS_ALLPOSTMESSAGE | QS_SMRESULT))
415 SetLastError( ERROR_INVALID_FLAGS );
416 return 0;
419 check_for_events( flags );
421 SERVER_START_REQ( get_queue_status )
423 req->clear = 1;
424 wine_server_call( req );
425 ret = MAKELONG( reply->changed_bits & flags, reply->wake_bits & flags );
427 SERVER_END_REQ;
428 return ret;
432 /***********************************************************************
433 * GetInputState (USER32.@)
435 BOOL WINAPI GetInputState(void)
437 DWORD ret;
439 check_for_events( QS_INPUT );
441 SERVER_START_REQ( get_queue_status )
443 req->clear = 0;
444 wine_server_call( req );
445 ret = reply->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
447 SERVER_END_REQ;
448 return ret;
452 /******************************************************************
453 * GetLastInputInfo (USER32.@)
455 BOOL WINAPI GetLastInputInfo(PLASTINPUTINFO plii)
457 BOOL ret;
459 TRACE("%p\n", plii);
461 if (plii->cbSize != sizeof (*plii) )
463 SetLastError(ERROR_INVALID_PARAMETER);
464 return FALSE;
467 SERVER_START_REQ( get_last_input_time )
469 ret = !wine_server_call_err( req );
470 if (ret)
471 plii->dwTime = reply->time;
473 SERVER_END_REQ;
474 return ret;
478 /******************************************************************
479 * GetRawInputDeviceList (USER32.@)
481 UINT WINAPI GetRawInputDeviceList(RAWINPUTDEVICELIST *devices, UINT *device_count, UINT size)
483 TRACE("devices %p, device_count %p, size %u.\n", devices, device_count, size);
485 if (size != sizeof(*devices) || !device_count) return ~0U;
487 if (!devices)
489 *device_count = 2;
490 return 0;
493 if (*device_count < 2)
495 *device_count = 2;
496 return ~0U;
499 devices[0].hDevice = WINE_MOUSE_HANDLE;
500 devices[0].dwType = RIM_TYPEMOUSE;
501 devices[1].hDevice = WINE_KEYBOARD_HANDLE;
502 devices[1].dwType = RIM_TYPEKEYBOARD;
504 return 2;
508 /******************************************************************
509 * RegisterRawInputDevices (USER32.@)
511 BOOL WINAPI DECLSPEC_HOTPATCH RegisterRawInputDevices(RAWINPUTDEVICE *devices, UINT device_count, UINT size)
513 struct rawinput_device *d;
514 BOOL ret;
515 UINT i;
517 TRACE("devices %p, device_count %u, size %u.\n", devices, device_count, size);
519 if (size != sizeof(*devices))
521 WARN("Invalid structure size %u.\n", size);
522 return FALSE;
525 if (!(d = HeapAlloc( GetProcessHeap(), 0, device_count * sizeof(*d) ))) return FALSE;
527 for (i = 0; i < device_count; ++i)
529 TRACE("device %u: page %#x, usage %#x, flags %#x, target %p.\n",
530 i, devices[i].usUsagePage, devices[i].usUsage,
531 devices[i].dwFlags, devices[i].hwndTarget);
532 if (devices[i].dwFlags & ~RIDEV_REMOVE)
533 FIXME("Unhandled flags %#x for device %u.\n", devices[i].dwFlags, i);
535 d[i].usage_page = devices[i].usUsagePage;
536 d[i].usage = devices[i].usUsage;
537 d[i].flags = devices[i].dwFlags;
538 d[i].target = wine_server_user_handle( devices[i].hwndTarget );
541 SERVER_START_REQ( update_rawinput_devices )
543 wine_server_add_data( req, d, device_count * sizeof(*d) );
544 ret = !wine_server_call( req );
546 SERVER_END_REQ;
548 HeapFree( GetProcessHeap(), 0, d );
550 return ret;
554 /******************************************************************
555 * GetRawInputData (USER32.@)
557 UINT WINAPI GetRawInputData(HRAWINPUT rawinput, UINT command, void *data, UINT *data_size, UINT header_size)
559 RAWINPUT *ri = (RAWINPUT *)rawinput;
560 UINT s;
562 TRACE("rawinput %p, command %#x, data %p, data_size %p, header_size %u.\n",
563 rawinput, command, data, data_size, header_size);
565 if (header_size != sizeof(RAWINPUTHEADER))
567 WARN("Invalid structure size %u.\n", header_size);
568 return ~0U;
571 switch (command)
573 case RID_INPUT:
574 s = ri->header.dwSize;
575 break;
576 case RID_HEADER:
577 s = sizeof(RAWINPUTHEADER);
578 break;
579 default:
580 return ~0U;
583 if (!data)
585 *data_size = s;
586 return 0;
589 if (*data_size < s) return ~0U;
590 memcpy(data, ri, s);
591 return s;
595 /******************************************************************
596 * GetRawInputBuffer (USER32.@)
598 UINT WINAPI DECLSPEC_HOTPATCH GetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader)
600 FIXME("(pData=%p, pcbSize=%p, cbSizeHeader=%d) stub!\n", pData, pcbSize, cbSizeHeader);
602 return 0;
606 /******************************************************************
607 * GetRawInputDeviceInfoA (USER32.@)
609 UINT WINAPI GetRawInputDeviceInfoA(HANDLE device, UINT command, void *data, UINT *data_size)
611 UINT ret;
613 TRACE("device %p, command %u, data %p, data_size %p.\n", device, command, data, data_size);
615 ret = GetRawInputDeviceInfoW(device, command, data, data_size);
616 if (command == RIDI_DEVICENAME && ret && ret != ~0U)
617 ret = WideCharToMultiByte(CP_ACP, 0, data, -1, data, *data_size, NULL, NULL);
619 return ret;
623 /******************************************************************
624 * GetRawInputDeviceInfoW (USER32.@)
626 UINT WINAPI GetRawInputDeviceInfoW(HANDLE device, UINT command, void *data, UINT *data_size)
628 /* FIXME: Most of this is made up. */
629 static const WCHAR keyboard_name[] = {'\\','\\','?','\\','W','I','N','E','_','K','E','Y','B','O','A','R','D',0};
630 static const WCHAR mouse_name[] = {'\\','\\','?','\\','W','I','N','E','_','M','O','U','S','E',0};
631 static const RID_DEVICE_INFO_KEYBOARD keyboard_info = {0, 0, 1, 12, 3, 101};
632 static const RID_DEVICE_INFO_MOUSE mouse_info = {1, 5, 0, FALSE};
633 const WCHAR *name = NULL;
634 RID_DEVICE_INFO *info;
635 UINT s;
637 TRACE("device %p, command %u, data %p, data_size %p.\n", device, command, data, data_size);
639 if (!data_size || (device != WINE_MOUSE_HANDLE && device != WINE_KEYBOARD_HANDLE)) return ~0U;
641 switch (command)
643 case RIDI_DEVICENAME:
644 if (device == WINE_MOUSE_HANDLE)
646 s = sizeof(mouse_name);
647 name = mouse_name;
649 else
651 s = sizeof(keyboard_name);
652 name = keyboard_name;
654 break;
655 case RIDI_DEVICEINFO:
656 s = sizeof(*info);
657 break;
658 default:
659 return ~0U;
662 if (!data)
664 *data_size = s;
665 return 0;
668 if (*data_size < s)
670 *data_size = s;
671 return ~0U;
674 if (command == RIDI_DEVICENAME)
676 memcpy(data, name, s);
677 return s;
680 info = data;
681 info->cbSize = sizeof(*info);
682 if (device == WINE_MOUSE_HANDLE)
684 info->dwType = RIM_TYPEMOUSE;
685 info->u.mouse = mouse_info;
687 else
689 info->dwType = RIM_TYPEKEYBOARD;
690 info->u.keyboard = keyboard_info;
692 return s;
696 /******************************************************************
697 * GetRegisteredRawInputDevices (USER32.@)
699 UINT WINAPI DECLSPEC_HOTPATCH GetRegisteredRawInputDevices(PRAWINPUTDEVICE pRawInputDevices, PUINT puiNumDevices, UINT cbSize)
701 FIXME("(pRawInputDevices=%p, puiNumDevices=%p, cbSize=%d) stub!\n", pRawInputDevices, puiNumDevices, cbSize);
703 return 0;
707 /******************************************************************
708 * DefRawInputProc (USER32.@)
710 LRESULT WINAPI DefRawInputProc(PRAWINPUT *paRawInput, INT nInput, UINT cbSizeHeader)
712 FIXME("(paRawInput=%p, nInput=%d, cbSizeHeader=%d) stub!\n", *paRawInput, nInput, cbSizeHeader);
714 return 0;
718 /**********************************************************************
719 * AttachThreadInput (USER32.@)
721 * Attaches the input processing mechanism of one thread to that of
722 * another thread.
724 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
726 BOOL ret;
728 SERVER_START_REQ( attach_thread_input )
730 req->tid_from = from;
731 req->tid_to = to;
732 req->attach = attach;
733 ret = !wine_server_call_err( req );
735 SERVER_END_REQ;
736 return ret;
740 /**********************************************************************
741 * GetKeyState (USER32.@)
743 * An application calls the GetKeyState function in response to a
744 * keyboard-input message. This function retrieves the state of the key
745 * at the time the input message was generated.
747 SHORT WINAPI DECLSPEC_HOTPATCH GetKeyState(INT vkey)
749 SHORT retval = 0;
751 SERVER_START_REQ( get_key_state )
753 req->tid = GetCurrentThreadId();
754 req->key = vkey;
755 if (!wine_server_call( req )) retval = (signed char)reply->state;
757 SERVER_END_REQ;
758 TRACE("key (0x%x) -> %x\n", vkey, retval);
759 return retval;
763 /**********************************************************************
764 * GetKeyboardState (USER32.@)
766 BOOL WINAPI DECLSPEC_HOTPATCH GetKeyboardState( LPBYTE state )
768 BOOL ret;
770 TRACE("(%p)\n", state);
772 memset( state, 0, 256 );
773 SERVER_START_REQ( get_key_state )
775 req->tid = GetCurrentThreadId();
776 req->key = -1;
777 wine_server_set_reply( req, state, 256 );
778 ret = !wine_server_call_err( req );
780 SERVER_END_REQ;
781 return ret;
785 /**********************************************************************
786 * SetKeyboardState (USER32.@)
788 BOOL WINAPI SetKeyboardState( LPBYTE state )
790 BOOL ret;
792 SERVER_START_REQ( set_key_state )
794 req->tid = GetCurrentThreadId();
795 wine_server_add_data( req, state, 256 );
796 ret = !wine_server_call_err( req );
798 SERVER_END_REQ;
799 return ret;
803 /**********************************************************************
804 * VkKeyScanA (USER32.@)
806 * VkKeyScan translates an ANSI character to a virtual-key and shift code
807 * for the current keyboard.
808 * high-order byte yields :
809 * 0 Unshifted
810 * 1 Shift
811 * 2 Ctrl
812 * 3-5 Shift-key combinations that are not used for characters
813 * 6 Ctrl-Alt
814 * 7 Ctrl-Alt-Shift
815 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
816 * FIXME : works ok except for dead chars :
817 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
818 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
820 SHORT WINAPI VkKeyScanA(CHAR cChar)
822 WCHAR wChar;
824 if (IsDBCSLeadByte(cChar)) return -1;
826 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
827 return VkKeyScanW(wChar);
830 /******************************************************************************
831 * VkKeyScanW (USER32.@)
833 SHORT WINAPI VkKeyScanW(WCHAR cChar)
835 return VkKeyScanExW(cChar, GetKeyboardLayout(0));
838 /**********************************************************************
839 * VkKeyScanExA (USER32.@)
841 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
843 WCHAR wChar;
845 if (IsDBCSLeadByte(cChar)) return -1;
847 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
848 return VkKeyScanExW(wChar, dwhkl);
851 /******************************************************************************
852 * VkKeyScanExW (USER32.@)
854 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
856 return USER_Driver->pVkKeyScanEx(cChar, dwhkl);
859 /**********************************************************************
860 * OemKeyScan (USER32.@)
862 DWORD WINAPI OemKeyScan(WORD wOemChar)
864 return wOemChar;
867 /******************************************************************************
868 * GetKeyboardType (USER32.@)
870 INT WINAPI GetKeyboardType(INT nTypeFlag)
872 TRACE_(keyboard)("(%d)\n", nTypeFlag);
873 switch(nTypeFlag)
875 case 0: /* Keyboard type */
876 return 4; /* AT-101 */
877 case 1: /* Keyboard Subtype */
878 return 0; /* There are no defined subtypes */
879 case 2: /* Number of F-keys */
880 return 12; /* We're doing an 101 for now, so return 12 F-keys */
881 default:
882 WARN_(keyboard)("Unknown type\n");
883 return 0; /* The book says 0 here, so 0 */
887 /******************************************************************************
888 * MapVirtualKeyA (USER32.@)
890 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
892 return MapVirtualKeyExA( code, maptype, GetKeyboardLayout(0) );
895 /******************************************************************************
896 * MapVirtualKeyW (USER32.@)
898 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
900 return MapVirtualKeyExW(code, maptype, GetKeyboardLayout(0));
903 /******************************************************************************
904 * MapVirtualKeyExA (USER32.@)
906 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
908 UINT ret;
910 ret = MapVirtualKeyExW( code, maptype, hkl );
911 if (maptype == MAPVK_VK_TO_CHAR)
913 BYTE ch = 0;
914 WCHAR wch = ret;
916 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL );
917 ret = ch;
919 return ret;
922 /******************************************************************************
923 * MapVirtualKeyExW (USER32.@)
925 UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
927 TRACE_(keyboard)("(%X, %d, %p)\n", code, maptype, hkl);
929 return USER_Driver->pMapVirtualKeyEx(code, maptype, hkl);
932 /****************************************************************************
933 * GetKBCodePage (USER32.@)
935 UINT WINAPI GetKBCodePage(void)
937 return GetOEMCP();
940 /***********************************************************************
941 * GetKeyboardLayout (USER32.@)
943 * - device handle for keyboard layout defaulted to
944 * the language id. This is the way Windows default works.
945 * - the thread identifier is also ignored.
947 HKL WINAPI GetKeyboardLayout(DWORD thread_id)
949 return USER_Driver->pGetKeyboardLayout(thread_id);
952 /****************************************************************************
953 * GetKeyboardLayoutNameA (USER32.@)
955 BOOL WINAPI GetKeyboardLayoutNameA(LPSTR pszKLID)
957 WCHAR buf[KL_NAMELENGTH];
959 if (GetKeyboardLayoutNameW(buf))
960 return WideCharToMultiByte( CP_ACP, 0, buf, -1, pszKLID, KL_NAMELENGTH, NULL, NULL ) != 0;
961 return FALSE;
964 /****************************************************************************
965 * GetKeyboardLayoutNameW (USER32.@)
967 BOOL WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
969 return USER_Driver->pGetKeyboardLayoutName(pwszKLID);
972 /****************************************************************************
973 * GetKeyNameTextA (USER32.@)
975 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
977 WCHAR buf[256];
978 INT ret;
980 if (!nSize || !GetKeyNameTextW(lParam, buf, 256))
982 lpBuffer[0] = 0;
983 return 0;
985 ret = WideCharToMultiByte(CP_ACP, 0, buf, -1, lpBuffer, nSize, NULL, NULL);
986 if (!ret && nSize)
988 ret = nSize - 1;
989 lpBuffer[ret] = 0;
991 else ret--;
993 return ret;
996 /****************************************************************************
997 * GetKeyNameTextW (USER32.@)
999 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
1001 if (!lpBuffer || !nSize) return 0;
1002 return USER_Driver->pGetKeyNameText( lParam, lpBuffer, nSize );
1005 /****************************************************************************
1006 * ToUnicode (USER32.@)
1008 INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
1009 LPWSTR lpwStr, int size, UINT flags)
1011 return ToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, GetKeyboardLayout(0));
1014 /****************************************************************************
1015 * ToUnicodeEx (USER32.@)
1017 INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
1018 LPWSTR lpwStr, int size, UINT flags, HKL hkl)
1020 return USER_Driver->pToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, hkl);
1023 /****************************************************************************
1024 * ToAscii (USER32.@)
1026 INT WINAPI ToAscii( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
1027 LPWORD lpChar, UINT flags )
1029 return ToAsciiEx(virtKey, scanCode, lpKeyState, lpChar, flags, GetKeyboardLayout(0));
1032 /****************************************************************************
1033 * ToAsciiEx (USER32.@)
1035 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
1036 LPWORD lpChar, UINT flags, HKL dwhkl )
1038 WCHAR uni_chars[2];
1039 INT ret, n_ret;
1041 ret = ToUnicodeEx(virtKey, scanCode, lpKeyState, uni_chars, 2, flags, dwhkl);
1042 if (ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
1043 else n_ret = ret;
1044 WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
1045 return ret;
1048 /**********************************************************************
1049 * ActivateKeyboardLayout (USER32.@)
1051 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
1053 TRACE_(keyboard)("(%p, %d)\n", hLayout, flags);
1055 return USER_Driver->pActivateKeyboardLayout(hLayout, flags);
1058 /**********************************************************************
1059 * BlockInput (USER32.@)
1061 BOOL WINAPI BlockInput(BOOL fBlockIt)
1063 FIXME_(keyboard)("(%d): stub\n", fBlockIt);
1064 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1066 return FALSE;
1069 /***********************************************************************
1070 * GetKeyboardLayoutList (USER32.@)
1072 * Return number of values available if either input parm is
1073 * 0, per MS documentation.
1075 UINT WINAPI GetKeyboardLayoutList(INT nBuff, HKL *layouts)
1077 TRACE_(keyboard)( "(%d, %p)\n", nBuff, layouts );
1079 return USER_Driver->pGetKeyboardLayoutList( nBuff, layouts );
1083 /***********************************************************************
1084 * RegisterHotKey (USER32.@)
1086 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk)
1088 BOOL ret;
1089 int replaced=0;
1091 TRACE_(keyboard)("(%p,%d,0x%08x,%X)\n",hwnd,id,modifiers,vk);
1093 if ((hwnd == NULL || WIN_IsCurrentThread(hwnd)) &&
1094 !USER_Driver->pRegisterHotKey(hwnd, modifiers, vk))
1095 return FALSE;
1097 SERVER_START_REQ( register_hotkey )
1099 req->window = wine_server_user_handle( hwnd );
1100 req->id = id;
1101 req->flags = modifiers;
1102 req->vkey = vk;
1103 if ((ret = !wine_server_call_err( req )))
1105 replaced = reply->replaced;
1106 modifiers = reply->flags;
1107 vk = reply->vkey;
1110 SERVER_END_REQ;
1112 if (ret && replaced)
1113 USER_Driver->pUnregisterHotKey(hwnd, modifiers, vk);
1115 return ret;
1118 /***********************************************************************
1119 * UnregisterHotKey (USER32.@)
1121 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id)
1123 BOOL ret;
1124 UINT modifiers, vk;
1126 TRACE_(keyboard)("(%p,%d)\n",hwnd,id);
1128 SERVER_START_REQ( unregister_hotkey )
1130 req->window = wine_server_user_handle( hwnd );
1131 req->id = id;
1132 if ((ret = !wine_server_call_err( req )))
1134 modifiers = reply->flags;
1135 vk = reply->vkey;
1138 SERVER_END_REQ;
1140 if (ret)
1141 USER_Driver->pUnregisterHotKey(hwnd, modifiers, vk);
1143 return ret;
1146 /***********************************************************************
1147 * LoadKeyboardLayoutW (USER32.@)
1149 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
1151 TRACE_(keyboard)("(%s, %d)\n", debugstr_w(pwszKLID), Flags);
1153 return USER_Driver->pLoadKeyboardLayout(pwszKLID, Flags);
1156 /***********************************************************************
1157 * LoadKeyboardLayoutA (USER32.@)
1159 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
1161 HKL ret;
1162 UNICODE_STRING pwszKLIDW;
1164 if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
1165 else pwszKLIDW.Buffer = NULL;
1167 ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
1168 RtlFreeUnicodeString(&pwszKLIDW);
1169 return ret;
1173 /***********************************************************************
1174 * UnloadKeyboardLayout (USER32.@)
1176 BOOL WINAPI UnloadKeyboardLayout(HKL hkl)
1178 TRACE_(keyboard)("(%p)\n", hkl);
1180 return USER_Driver->pUnloadKeyboardLayout(hkl);
1183 typedef struct __TRACKINGLIST {
1184 TRACKMOUSEEVENT tme;
1185 POINT pos; /* center of hover rectangle */
1186 } _TRACKINGLIST;
1188 /* FIXME: move tracking stuff into a per thread data */
1189 static _TRACKINGLIST tracking_info;
1190 static UINT_PTR timer;
1192 static void check_mouse_leave(HWND hwnd, int hittest)
1194 if (tracking_info.tme.hwndTrack != hwnd)
1196 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1197 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
1198 else
1199 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
1201 /* remove the TME_LEAVE flag */
1202 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1204 else
1206 if (hittest == HTCLIENT)
1208 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1210 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
1211 /* remove the TME_LEAVE flag */
1212 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1215 else
1217 if (!(tracking_info.tme.dwFlags & TME_NONCLIENT))
1219 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
1220 /* remove the TME_LEAVE flag */
1221 tracking_info.tme.dwFlags &= ~TME_LEAVE;
1227 static void CALLBACK TrackMouseEventProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent,
1228 DWORD dwTime)
1230 POINT pos;
1231 INT hoverwidth = 0, hoverheight = 0, hittest;
1233 TRACE("hwnd %p, msg %04x, id %04lx, time %u\n", hwnd, uMsg, idEvent, dwTime);
1235 GetCursorPos(&pos);
1236 hwnd = WINPOS_WindowFromPoint(hwnd, pos, &hittest);
1238 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
1240 SystemParametersInfoW(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
1241 SystemParametersInfoW(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
1243 TRACE("tracked pos %s, current pos %s, hover width %d, hover height %d\n",
1244 wine_dbgstr_point(&tracking_info.pos), wine_dbgstr_point(&pos),
1245 hoverwidth, hoverheight);
1247 /* see if this tracking event is looking for TME_LEAVE and that the */
1248 /* mouse has left the window */
1249 if (tracking_info.tme.dwFlags & TME_LEAVE)
1251 check_mouse_leave(hwnd, hittest);
1254 if (tracking_info.tme.hwndTrack != hwnd)
1256 /* mouse is gone, stop tracking mouse hover */
1257 tracking_info.tme.dwFlags &= ~TME_HOVER;
1260 /* see if we are tracking hovering for this hwnd */
1261 if (tracking_info.tme.dwFlags & TME_HOVER)
1263 /* has the cursor moved outside the rectangle centered around pos? */
1264 if ((abs(pos.x - tracking_info.pos.x) > (hoverwidth / 2)) ||
1265 (abs(pos.y - tracking_info.pos.y) > (hoverheight / 2)))
1267 /* record this new position as the current position */
1268 tracking_info.pos = pos;
1270 else
1272 if (hittest == HTCLIENT)
1274 ScreenToClient(hwnd, &pos);
1275 TRACE("client cursor pos %s\n", wine_dbgstr_point(&pos));
1277 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSEHOVER,
1278 get_key_state(), MAKELPARAM( pos.x, pos.y ));
1280 else
1282 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1283 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSEHOVER,
1284 hittest, MAKELPARAM( pos.x, pos.y ));
1287 /* stop tracking mouse hover */
1288 tracking_info.tme.dwFlags &= ~TME_HOVER;
1292 /* stop the timer if the tracking list is empty */
1293 if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
1295 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1296 timer = 0;
1297 tracking_info.tme.hwndTrack = 0;
1298 tracking_info.tme.dwFlags = 0;
1299 tracking_info.tme.dwHoverTime = 0;
1304 /***********************************************************************
1305 * TrackMouseEvent [USER32]
1307 * Requests notification of mouse events
1309 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
1310 * to the hwnd specified in the ptme structure. After the event message
1311 * is posted to the hwnd, the entry in the queue is removed.
1313 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
1314 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
1315 * immediately and the TME_LEAVE flag being ignored.
1317 * PARAMS
1318 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
1320 * RETURNS
1321 * Success: non-zero
1322 * Failure: zero
1326 BOOL WINAPI
1327 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
1329 HWND hwnd;
1330 POINT pos;
1331 DWORD hover_time;
1332 INT hittest;
1334 TRACE("%x, %x, %p, %u\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
1336 if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
1337 WARN("wrong TRACKMOUSEEVENT size from app\n");
1338 SetLastError(ERROR_INVALID_PARAMETER);
1339 return FALSE;
1342 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
1343 if (ptme->dwFlags & TME_QUERY )
1345 *ptme = tracking_info.tme;
1346 /* set cbSize in the case it's not initialized yet */
1347 ptme->cbSize = sizeof(TRACKMOUSEEVENT);
1349 return TRUE; /* return here, TME_QUERY is retrieving information */
1352 if (!IsWindow(ptme->hwndTrack))
1354 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
1355 return FALSE;
1358 hover_time = (ptme->dwFlags & TME_HOVER) ? ptme->dwHoverTime : HOVER_DEFAULT;
1360 /* if HOVER_DEFAULT was specified replace this with the system's current value.
1361 * TME_LEAVE doesn't need to specify hover time so use default */
1362 if (hover_time == HOVER_DEFAULT || hover_time == 0)
1363 SystemParametersInfoW(SPI_GETMOUSEHOVERTIME, 0, &hover_time, 0);
1365 GetCursorPos(&pos);
1366 hwnd = WINPOS_WindowFromPoint(ptme->hwndTrack, pos, &hittest);
1367 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
1369 if (ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT))
1370 FIXME("Unknown flag(s) %08x\n", ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT));
1372 if (ptme->dwFlags & TME_CANCEL)
1374 if (tracking_info.tme.hwndTrack == ptme->hwndTrack)
1376 tracking_info.tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
1378 /* if we aren't tracking on hover or leave remove this entry */
1379 if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
1381 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1382 timer = 0;
1383 tracking_info.tme.hwndTrack = 0;
1384 tracking_info.tme.dwFlags = 0;
1385 tracking_info.tme.dwHoverTime = 0;
1388 } else {
1389 /* In our implementation it's possible that another window will receive a
1390 * WM_MOUSEMOVE and call TrackMouseEvent before TrackMouseEventProc is
1391 * called. In such a situation post the WM_MOUSELEAVE now */
1392 if (tracking_info.tme.dwFlags & TME_LEAVE && tracking_info.tme.hwndTrack != NULL)
1393 check_mouse_leave(hwnd, hittest);
1395 if (timer)
1397 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1398 timer = 0;
1399 tracking_info.tme.hwndTrack = 0;
1400 tracking_info.tme.dwFlags = 0;
1401 tracking_info.tme.dwHoverTime = 0;
1404 if (ptme->hwndTrack == hwnd)
1406 /* Adding new mouse event to the tracking list */
1407 tracking_info.tme = *ptme;
1408 tracking_info.tme.dwHoverTime = hover_time;
1410 /* Initialize HoverInfo variables even if not hover tracking */
1411 tracking_info.pos = pos;
1413 timer = SetSystemTimer(tracking_info.tme.hwndTrack, (UINT_PTR)&tracking_info.tme, hover_time, TrackMouseEventProc);
1417 return TRUE;
1420 /***********************************************************************
1421 * GetMouseMovePointsEx [USER32]
1423 * RETURNS
1424 * Success: count of point set in the buffer
1425 * Failure: -1
1427 int WINAPI GetMouseMovePointsEx(UINT size, LPMOUSEMOVEPOINT ptin, LPMOUSEMOVEPOINT ptout, int count, DWORD res) {
1429 if((size != sizeof(MOUSEMOVEPOINT)) || (count < 0) || (count > 64)) {
1430 SetLastError(ERROR_INVALID_PARAMETER);
1431 return -1;
1434 if(!ptin || (!ptout && count)) {
1435 SetLastError(ERROR_NOACCESS);
1436 return -1;
1439 FIXME("(%d %p %p %d %d) stub\n", size, ptin, ptout, count, res);
1441 SetLastError(ERROR_POINT_NOT_FOUND);
1442 return -1;