mmdevapi: Fix compiler warnings with flag -Wcast-qual.
[wine.git] / dlls / user32 / input.c
blobf14590d1cbc6866c27176e05594261bbac09b16d
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 "windef.h"
38 #include "winbase.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41 #include "winnls.h"
42 #include "winternl.h"
43 #include "winerror.h"
44 #include "win.h"
45 #include "user_private.h"
46 #include "wine/server.h"
47 #include "wine/debug.h"
48 #include "wine/unicode.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(win);
51 WINE_DECLARE_DEBUG_CHANNEL(keyboard);
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 && previous != hwnd)
109 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
111 if (prev_ret) *prev_ret = previous;
113 return ret;
117 /***********************************************************************
118 * SendInput (USER32.@)
120 UINT WINAPI SendInput( UINT count, LPINPUT inputs, int size )
122 if (TRACE_ON(win))
124 UINT i;
126 for (i = 0; i < count; i++)
128 switch(inputs[i].type)
130 case INPUT_MOUSE:
131 TRACE("mouse: dx %d, dy %d, data %x, flags %x, time %u, info %lx\n",
132 inputs[i].u.mi.dx, inputs[i].u.mi.dy, inputs[i].u.mi.mouseData,
133 inputs[i].u.mi.dwFlags, inputs[i].u.mi.time, inputs[i].u.mi.dwExtraInfo);
134 break;
136 case INPUT_KEYBOARD:
137 TRACE("keyboard: vk %X, scan %x, flags %x, time %u, info %lx\n",
138 inputs[i].u.ki.wVk, inputs[i].u.ki.wScan, inputs[i].u.ki.dwFlags,
139 inputs[i].u.ki.time, inputs[i].u.ki.dwExtraInfo);
140 break;
142 case INPUT_HARDWARE:
143 TRACE("hardware: msg %d, wParamL %x, wParamH %x\n",
144 inputs[i].u.hi.uMsg, inputs[i].u.hi.wParamL, inputs[i].u.hi.wParamH);
145 break;
147 default:
148 FIXME("unknown input type %u\n", inputs[i].type);
149 break;
154 return USER_Driver->pSendInput( count, inputs, size );
158 /***********************************************************************
159 * keybd_event (USER32.@)
161 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
162 DWORD dwFlags, ULONG_PTR dwExtraInfo )
164 INPUT input;
166 input.type = INPUT_KEYBOARD;
167 input.u.ki.wVk = bVk;
168 input.u.ki.wScan = bScan;
169 input.u.ki.dwFlags = dwFlags;
170 input.u.ki.time = GetTickCount();
171 input.u.ki.dwExtraInfo = dwExtraInfo;
172 SendInput( 1, &input, sizeof(input) );
176 /***********************************************************************
177 * mouse_event (USER32.@)
179 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
180 DWORD dwData, ULONG_PTR dwExtraInfo )
182 INPUT input;
184 input.type = INPUT_MOUSE;
185 input.u.mi.dx = dx;
186 input.u.mi.dy = dy;
187 input.u.mi.mouseData = dwData;
188 input.u.mi.dwFlags = dwFlags;
189 input.u.mi.time = GetCurrentTime();
190 input.u.mi.dwExtraInfo = dwExtraInfo;
191 SendInput( 1, &input, sizeof(input) );
195 /***********************************************************************
196 * GetCursorPos (USER32.@)
198 BOOL WINAPI DECLSPEC_HOTPATCH GetCursorPos( POINT *pt )
200 if (!pt) return FALSE;
201 return USER_Driver->pGetCursorPos( pt );
205 /***********************************************************************
206 * GetCursorInfo (USER32.@)
208 BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
210 if (!pci) return 0;
211 pci->hCursor = LoadCursorW( 0, (LPCWSTR)IDC_ARROW );
212 pci->flags = CURSOR_SHOWING;
213 GetCursorPos(&pci->ptScreenPos);
214 return 1;
218 /***********************************************************************
219 * SetCursorPos (USER32.@)
221 BOOL WINAPI DECLSPEC_HOTPATCH SetCursorPos( INT x, INT y )
223 return USER_Driver->pSetCursorPos( x, y );
227 /**********************************************************************
228 * SetCapture (USER32.@)
230 HWND WINAPI DECLSPEC_HOTPATCH SetCapture( HWND hwnd )
232 HWND previous = 0;
234 set_capture_window( hwnd, 0, &previous );
235 return previous;
239 /**********************************************************************
240 * ReleaseCapture (USER32.@)
242 BOOL WINAPI DECLSPEC_HOTPATCH ReleaseCapture(void)
244 BOOL ret = set_capture_window( 0, 0, NULL );
246 /* Somebody may have missed some mouse movements */
247 if (ret) mouse_event( MOUSEEVENTF_MOVE, 0, 0, 0, 0 );
249 return ret;
253 /**********************************************************************
254 * GetCapture (USER32.@)
256 HWND WINAPI GetCapture(void)
258 HWND ret = 0;
260 SERVER_START_REQ( get_thread_input )
262 req->tid = GetCurrentThreadId();
263 if (!wine_server_call_err( req )) ret = wine_server_ptr_handle( reply->capture );
265 SERVER_END_REQ;
266 return ret;
270 /**********************************************************************
271 * GetAsyncKeyState (USER32.@)
273 * Determine if a key is or was pressed. retval has high-order
274 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
275 * been pressed.
277 SHORT WINAPI DECLSPEC_HOTPATCH GetAsyncKeyState(INT nKey)
279 if (nKey < 0 || nKey > 256)
280 return 0;
281 return USER_Driver->pGetAsyncKeyState( nKey );
285 /***********************************************************************
286 * GetQueueStatus (USER32.@)
288 DWORD WINAPI GetQueueStatus( UINT flags )
290 DWORD ret = 0;
292 if (flags & ~(QS_ALLINPUT | QS_ALLPOSTMESSAGE | QS_SMRESULT))
294 SetLastError( ERROR_INVALID_FLAGS );
295 return 0;
298 /* check for pending X events */
299 USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, flags, 0 );
301 SERVER_START_REQ( get_queue_status )
303 req->clear = 1;
304 wine_server_call( req );
305 ret = MAKELONG( reply->changed_bits & flags, reply->wake_bits & flags );
307 SERVER_END_REQ;
308 return ret;
312 /***********************************************************************
313 * GetInputState (USER32.@)
315 BOOL WINAPI GetInputState(void)
317 DWORD ret = 0;
319 /* check for pending X events */
320 USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_INPUT, 0 );
322 SERVER_START_REQ( get_queue_status )
324 req->clear = 0;
325 wine_server_call( req );
326 ret = reply->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
328 SERVER_END_REQ;
329 return ret;
333 /******************************************************************
334 * GetLastInputInfo (USER32.@)
336 BOOL WINAPI GetLastInputInfo(PLASTINPUTINFO plii)
338 BOOL ret;
340 TRACE("%p\n", plii);
342 if (plii->cbSize != sizeof (*plii) )
344 SetLastError(ERROR_INVALID_PARAMETER);
345 return FALSE;
348 SERVER_START_REQ( get_last_input_time )
350 ret = !wine_server_call_err( req );
351 if (ret)
352 plii->dwTime = reply->time;
354 SERVER_END_REQ;
355 return ret;
359 /******************************************************************
360 * GetRawInputDeviceList (USER32.@)
362 UINT WINAPI GetRawInputDeviceList(PRAWINPUTDEVICELIST pRawInputDeviceList, PUINT puiNumDevices, UINT cbSize)
364 FIXME("(pRawInputDeviceList=%p, puiNumDevices=%p, cbSize=%d) stub!\n", pRawInputDeviceList, puiNumDevices, cbSize);
366 if(pRawInputDeviceList)
367 memset(pRawInputDeviceList, 0, sizeof *pRawInputDeviceList);
368 *puiNumDevices = 0;
369 return 0;
373 /******************************************************************
374 * RegisterRawInputDevices (USER32.@)
376 BOOL WINAPI DECLSPEC_HOTPATCH RegisterRawInputDevices(PRAWINPUTDEVICE pRawInputDevices, UINT uiNumDevices, UINT cbSize)
378 FIXME("(pRawInputDevices=%p, uiNumDevices=%d, cbSize=%d) stub!\n", pRawInputDevices, uiNumDevices, cbSize);
380 return TRUE;
384 /******************************************************************
385 * GetRawInputData (USER32.@)
387 UINT WINAPI GetRawInputData(HRAWINPUT hRawInput, UINT uiCommand, LPVOID pData, PUINT pcbSize, UINT cbSizeHeader)
389 FIXME("(hRawInput=%p, uiCommand=%d, pData=%p, pcbSize=%p, cbSizeHeader=%d) stub!\n",
390 hRawInput, uiCommand, pData, pcbSize, cbSizeHeader);
392 return 0;
396 /******************************************************************
397 * GetRawInputBuffer (USER32.@)
399 UINT WINAPI DECLSPEC_HOTPATCH GetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader)
401 FIXME("(pData=%p, pcbSize=%p, cbSizeHeader=%d) stub!\n", pData, pcbSize, cbSizeHeader);
403 return 0;
407 /******************************************************************
408 * GetRawInputDeviceInfoA (USER32.@)
410 UINT WINAPI GetRawInputDeviceInfoA(HANDLE hDevice, UINT uiCommand, LPVOID pData, PUINT pcbSize)
412 FIXME("(hDevice=%p, uiCommand=%d, pData=%p, pcbSize=%p) stub!\n", hDevice, uiCommand, pData, pcbSize);
414 return 0;
418 /******************************************************************
419 * GetRawInputDeviceInfoW (USER32.@)
421 UINT WINAPI GetRawInputDeviceInfoW(HANDLE hDevice, UINT uiCommand, LPVOID pData, PUINT pcbSize)
423 FIXME("(hDevice=%p, uiCommand=%d, pData=%p, pcbSize=%p) stub!\n", hDevice, uiCommand, pData, pcbSize);
425 return 0;
429 /******************************************************************
430 * GetRegisteredRawInputDevices (USER32.@)
432 UINT WINAPI GetRegisteredRawInputDevices(PRAWINPUTDEVICE pRawInputDevices, PUINT puiNumDevices, UINT cbSize)
434 FIXME("(pRawInputDevices=%p, puiNumDevices=%p, cbSize=%d) stub!\n", pRawInputDevices, puiNumDevices, cbSize);
436 return 0;
440 /******************************************************************
441 * DefRawInputProc (USER32.@)
443 LRESULT WINAPI DefRawInputProc(PRAWINPUT *paRawInput, INT nInput, UINT cbSizeHeader)
445 FIXME("(paRawInput=%p, nInput=%d, cbSizeHeader=%d) stub!\n", *paRawInput, nInput, cbSizeHeader);
447 return 0;
451 /**********************************************************************
452 * AttachThreadInput (USER32.@)
454 * Attaches the input processing mechanism of one thread to that of
455 * another thread.
457 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
459 BOOL ret;
461 SERVER_START_REQ( attach_thread_input )
463 req->tid_from = from;
464 req->tid_to = to;
465 req->attach = attach;
466 ret = !wine_server_call_err( req );
468 SERVER_END_REQ;
469 return ret;
473 /**********************************************************************
474 * GetKeyState (USER32.@)
476 * An application calls the GetKeyState function in response to a
477 * keyboard-input message. This function retrieves the state of the key
478 * at the time the input message was generated.
480 SHORT WINAPI DECLSPEC_HOTPATCH GetKeyState(INT vkey)
482 SHORT retval = 0;
484 SERVER_START_REQ( get_key_state )
486 req->tid = GetCurrentThreadId();
487 req->key = vkey;
488 if (!wine_server_call( req )) retval = (signed char)reply->state;
490 SERVER_END_REQ;
491 TRACE("key (0x%x) -> %x\n", vkey, retval);
492 return retval;
496 /**********************************************************************
497 * GetKeyboardState (USER32.@)
499 BOOL WINAPI DECLSPEC_HOTPATCH GetKeyboardState( LPBYTE state )
501 BOOL ret;
503 TRACE("(%p)\n", state);
505 memset( state, 0, 256 );
506 SERVER_START_REQ( get_key_state )
508 req->tid = GetCurrentThreadId();
509 req->key = -1;
510 wine_server_set_reply( req, state, 256 );
511 ret = !wine_server_call_err( req );
513 SERVER_END_REQ;
514 return ret;
518 /**********************************************************************
519 * SetKeyboardState (USER32.@)
521 BOOL WINAPI SetKeyboardState( LPBYTE state )
523 BOOL ret;
525 SERVER_START_REQ( set_key_state )
527 req->tid = GetCurrentThreadId();
528 wine_server_add_data( req, state, 256 );
529 ret = !wine_server_call_err( req );
531 SERVER_END_REQ;
532 return ret;
536 /**********************************************************************
537 * VkKeyScanA (USER32.@)
539 * VkKeyScan translates an ANSI character to a virtual-key and shift code
540 * for the current keyboard.
541 * high-order byte yields :
542 * 0 Unshifted
543 * 1 Shift
544 * 2 Ctrl
545 * 3-5 Shift-key combinations that are not used for characters
546 * 6 Ctrl-Alt
547 * 7 Ctrl-Alt-Shift
548 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
549 * FIXME : works ok except for dead chars :
550 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
551 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
553 SHORT WINAPI VkKeyScanA(CHAR cChar)
555 WCHAR wChar;
557 if (IsDBCSLeadByte(cChar)) return -1;
559 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
560 return VkKeyScanW(wChar);
563 /******************************************************************************
564 * VkKeyScanW (USER32.@)
566 SHORT WINAPI VkKeyScanW(WCHAR cChar)
568 return VkKeyScanExW(cChar, GetKeyboardLayout(0));
571 /**********************************************************************
572 * VkKeyScanExA (USER32.@)
574 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
576 WCHAR wChar;
578 if (IsDBCSLeadByte(cChar)) return -1;
580 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
581 return VkKeyScanExW(wChar, dwhkl);
584 /******************************************************************************
585 * VkKeyScanExW (USER32.@)
587 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
589 return USER_Driver->pVkKeyScanEx(cChar, dwhkl);
592 /**********************************************************************
593 * OemKeyScan (USER32.@)
595 DWORD WINAPI OemKeyScan(WORD wOemChar)
597 return wOemChar;
600 /******************************************************************************
601 * GetKeyboardType (USER32.@)
603 INT WINAPI GetKeyboardType(INT nTypeFlag)
605 TRACE_(keyboard)("(%d)\n", nTypeFlag);
606 switch(nTypeFlag)
608 case 0: /* Keyboard type */
609 return 4; /* AT-101 */
610 case 1: /* Keyboard Subtype */
611 return 0; /* There are no defined subtypes */
612 case 2: /* Number of F-keys */
613 return 12; /* We're doing an 101 for now, so return 12 F-keys */
614 default:
615 WARN_(keyboard)("Unknown type\n");
616 return 0; /* The book says 0 here, so 0 */
620 /******************************************************************************
621 * MapVirtualKeyA (USER32.@)
623 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
625 return MapVirtualKeyExA( code, maptype, GetKeyboardLayout(0) );
628 /******************************************************************************
629 * MapVirtualKeyW (USER32.@)
631 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
633 return MapVirtualKeyExW(code, maptype, GetKeyboardLayout(0));
636 /******************************************************************************
637 * MapVirtualKeyExA (USER32.@)
639 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
641 UINT ret;
643 ret = MapVirtualKeyExW( code, maptype, hkl );
644 if (maptype == MAPVK_VK_TO_CHAR)
646 BYTE ch = 0;
647 WCHAR wch = ret;
649 WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)&ch, 1, NULL, NULL );
650 ret = ch;
652 return ret;
655 /******************************************************************************
656 * MapVirtualKeyExW (USER32.@)
658 UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
660 TRACE_(keyboard)("(%X, %d, %p)\n", code, maptype, hkl);
662 return USER_Driver->pMapVirtualKeyEx(code, maptype, hkl);
665 /****************************************************************************
666 * GetKBCodePage (USER32.@)
668 UINT WINAPI GetKBCodePage(void)
670 return GetOEMCP();
673 /***********************************************************************
674 * GetKeyboardLayout (USER32.@)
676 * - device handle for keyboard layout defaulted to
677 * the language id. This is the way Windows default works.
678 * - the thread identifier (dwLayout) is also ignored.
680 HKL WINAPI GetKeyboardLayout(DWORD dwLayout)
682 return USER_Driver->pGetKeyboardLayout(dwLayout);
685 /****************************************************************************
686 * GetKeyboardLayoutNameA (USER32.@)
688 BOOL WINAPI GetKeyboardLayoutNameA(LPSTR pszKLID)
690 WCHAR buf[KL_NAMELENGTH];
692 if (GetKeyboardLayoutNameW(buf))
693 return WideCharToMultiByte( CP_ACP, 0, buf, -1, pszKLID, KL_NAMELENGTH, NULL, NULL ) != 0;
694 return FALSE;
697 /****************************************************************************
698 * GetKeyboardLayoutNameW (USER32.@)
700 BOOL WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
702 return USER_Driver->pGetKeyboardLayoutName(pwszKLID);
705 /****************************************************************************
706 * GetKeyNameTextA (USER32.@)
708 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
710 WCHAR buf[256];
711 INT ret;
713 if (!GetKeyNameTextW(lParam, buf, 256))
715 lpBuffer[0] = 0;
716 return 0;
718 ret = WideCharToMultiByte(CP_ACP, 0, buf, -1, lpBuffer, nSize, NULL, NULL);
719 if (!ret && nSize)
721 ret = nSize - 1;
722 lpBuffer[ret] = 0;
724 return ret;
727 /****************************************************************************
728 * GetKeyNameTextW (USER32.@)
730 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
732 return USER_Driver->pGetKeyNameText( lParam, lpBuffer, nSize );
735 /****************************************************************************
736 * ToUnicode (USER32.@)
738 INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
739 LPWSTR lpwStr, int size, UINT flags)
741 return ToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, GetKeyboardLayout(0));
744 /****************************************************************************
745 * ToUnicodeEx (USER32.@)
747 INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
748 LPWSTR lpwStr, int size, UINT flags, HKL hkl)
750 return USER_Driver->pToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, hkl);
753 /****************************************************************************
754 * ToAscii (USER32.@)
756 INT WINAPI ToAscii( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
757 LPWORD lpChar, UINT flags )
759 return ToAsciiEx(virtKey, scanCode, lpKeyState, lpChar, flags, GetKeyboardLayout(0));
762 /****************************************************************************
763 * ToAsciiEx (USER32.@)
765 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
766 LPWORD lpChar, UINT flags, HKL dwhkl )
768 WCHAR uni_chars[2];
769 INT ret, n_ret;
771 ret = ToUnicodeEx(virtKey, scanCode, lpKeyState, uni_chars, 2, flags, dwhkl);
772 if (ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
773 else n_ret = ret;
774 WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
775 return ret;
778 /**********************************************************************
779 * ActivateKeyboardLayout (USER32.@)
781 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
783 TRACE_(keyboard)("(%p, %d)\n", hLayout, flags);
785 return USER_Driver->pActivateKeyboardLayout(hLayout, flags);
788 /**********************************************************************
789 * BlockInput (USER32.@)
791 BOOL WINAPI BlockInput(BOOL fBlockIt)
793 FIXME_(keyboard)("(%d): stub\n", fBlockIt);
794 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
796 return FALSE;
799 /***********************************************************************
800 * GetKeyboardLayoutList (USER32.@)
802 * Return number of values available if either input parm is
803 * 0, per MS documentation.
805 UINT WINAPI GetKeyboardLayoutList(INT nBuff, HKL *layouts)
807 HKEY hKeyKeyboard;
808 DWORD rc;
809 INT count = 0;
810 ULONG_PTR baselayout;
811 LANGID langid;
812 static const WCHAR szKeyboardReg[] = {'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\','C','o','n','t','r','o','l','\\','K','e','y','b','o','a','r','d',' ','L','a','y','o','u','t','s',0};
814 TRACE_(keyboard)("(%d,%p)\n",nBuff,layouts);
816 baselayout = GetUserDefaultLCID();
817 langid = PRIMARYLANGID(LANGIDFROMLCID(baselayout));
818 if (langid == LANG_CHINESE || langid == LANG_JAPANESE || langid == LANG_KOREAN)
819 baselayout |= 0xe001 << 16; /* IME */
820 else
821 baselayout |= baselayout << 16;
823 /* Enumerate the Registry */
824 rc = RegOpenKeyW(HKEY_LOCAL_MACHINE,szKeyboardReg,&hKeyKeyboard);
825 if (rc == ERROR_SUCCESS)
827 do {
828 WCHAR szKeyName[9];
829 HKL layout;
830 rc = RegEnumKeyW(hKeyKeyboard, count, szKeyName, 9);
831 if (rc == ERROR_SUCCESS)
833 layout = (HKL)strtoulW(szKeyName,NULL,16);
834 if (baselayout != 0 && layout == (HKL)baselayout)
835 baselayout = 0; /* found in the registry do not add again */
836 if (nBuff && layouts)
838 if (count >= nBuff ) break;
839 layouts[count] = layout;
841 count ++;
843 } while (rc == ERROR_SUCCESS);
844 RegCloseKey(hKeyKeyboard);
847 /* make sure our base layout is on the list */
848 if (baselayout != 0)
850 if (nBuff && layouts)
852 if (count < nBuff)
854 layouts[count] = (HKL)baselayout;
855 count++;
858 else
859 count++;
862 return count;
866 /***********************************************************************
867 * RegisterHotKey (USER32.@)
869 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk)
871 static int once;
872 if (!once++) FIXME_(keyboard)("(%p,%d,0x%08x,%X): stub\n",hwnd,id,modifiers,vk);
873 return TRUE;
876 /***********************************************************************
877 * UnregisterHotKey (USER32.@)
879 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id)
881 static int once;
882 if (!once++) FIXME_(keyboard)("(%p,%d): stub\n",hwnd,id);
883 return TRUE;
886 /***********************************************************************
887 * LoadKeyboardLayoutW (USER32.@)
889 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
891 TRACE_(keyboard)("(%s, %d)\n", debugstr_w(pwszKLID), Flags);
893 return USER_Driver->pLoadKeyboardLayout(pwszKLID, Flags);
896 /***********************************************************************
897 * LoadKeyboardLayoutA (USER32.@)
899 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
901 HKL ret;
902 UNICODE_STRING pwszKLIDW;
904 if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
905 else pwszKLIDW.Buffer = NULL;
907 ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
908 RtlFreeUnicodeString(&pwszKLIDW);
909 return ret;
913 /***********************************************************************
914 * UnloadKeyboardLayout (USER32.@)
916 BOOL WINAPI UnloadKeyboardLayout(HKL hkl)
918 TRACE_(keyboard)("(%p)\n", hkl);
920 return USER_Driver->pUnloadKeyboardLayout(hkl);
923 typedef struct __TRACKINGLIST {
924 TRACKMOUSEEVENT tme;
925 POINT pos; /* center of hover rectangle */
926 } _TRACKINGLIST;
928 /* FIXME: move tracking stuff into a per thread data */
929 static _TRACKINGLIST tracking_info;
930 static UINT_PTR timer;
932 static void check_mouse_leave(HWND hwnd, int hittest)
934 if (tracking_info.tme.hwndTrack != hwnd)
936 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
937 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
938 else
939 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
941 /* remove the TME_LEAVE flag */
942 tracking_info.tme.dwFlags &= ~TME_LEAVE;
944 else
946 if (hittest == HTCLIENT)
948 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
950 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
951 /* remove the TME_LEAVE flag */
952 tracking_info.tme.dwFlags &= ~TME_LEAVE;
955 else
957 if (!(tracking_info.tme.dwFlags & TME_NONCLIENT))
959 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
960 /* remove the TME_LEAVE flag */
961 tracking_info.tme.dwFlags &= ~TME_LEAVE;
967 static void CALLBACK TrackMouseEventProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent,
968 DWORD dwTime)
970 POINT pos;
971 INT hoverwidth = 0, hoverheight = 0, hittest;
973 TRACE("hwnd %p, msg %04x, id %04lx, time %u\n", hwnd, uMsg, idEvent, dwTime);
975 GetCursorPos(&pos);
976 hwnd = WINPOS_WindowFromPoint(hwnd, pos, &hittest);
978 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
980 SystemParametersInfoW(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
981 SystemParametersInfoW(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
983 TRACE("tracked pos %s, current pos %s, hover width %d, hover height %d\n",
984 wine_dbgstr_point(&tracking_info.pos), wine_dbgstr_point(&pos),
985 hoverwidth, hoverheight);
987 /* see if this tracking event is looking for TME_LEAVE and that the */
988 /* mouse has left the window */
989 if (tracking_info.tme.dwFlags & TME_LEAVE)
991 check_mouse_leave(hwnd, hittest);
994 if (tracking_info.tme.hwndTrack != hwnd)
996 /* mouse is gone, stop tracking mouse hover */
997 tracking_info.tme.dwFlags &= ~TME_HOVER;
1000 /* see if we are tracking hovering for this hwnd */
1001 if (tracking_info.tme.dwFlags & TME_HOVER)
1003 /* has the cursor moved outside the rectangle centered around pos? */
1004 if ((abs(pos.x - tracking_info.pos.x) > (hoverwidth / 2)) ||
1005 (abs(pos.y - tracking_info.pos.y) > (hoverheight / 2)))
1007 /* record this new position as the current position */
1008 tracking_info.pos = pos;
1010 else
1012 if (hittest == HTCLIENT)
1014 ScreenToClient(hwnd, &pos);
1015 TRACE("client cursor pos %s\n", wine_dbgstr_point(&pos));
1017 PostMessageW(tracking_info.tme.hwndTrack, WM_MOUSEHOVER,
1018 get_key_state(), MAKELPARAM( pos.x, pos.y ));
1020 else
1022 if (tracking_info.tme.dwFlags & TME_NONCLIENT)
1023 PostMessageW(tracking_info.tme.hwndTrack, WM_NCMOUSEHOVER,
1024 hittest, MAKELPARAM( pos.x, pos.y ));
1027 /* stop tracking mouse hover */
1028 tracking_info.tme.dwFlags &= ~TME_HOVER;
1032 /* stop the timer if the tracking list is empty */
1033 if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
1035 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1036 timer = 0;
1037 tracking_info.tme.hwndTrack = 0;
1038 tracking_info.tme.dwFlags = 0;
1039 tracking_info.tme.dwHoverTime = 0;
1044 /***********************************************************************
1045 * TrackMouseEvent [USER32]
1047 * Requests notification of mouse events
1049 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
1050 * to the hwnd specified in the ptme structure. After the event message
1051 * is posted to the hwnd, the entry in the queue is removed.
1053 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
1054 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
1055 * immediately and the TME_LEAVE flag being ignored.
1057 * PARAMS
1058 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
1060 * RETURNS
1061 * Success: non-zero
1062 * Failure: zero
1066 BOOL WINAPI
1067 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
1069 HWND hwnd;
1070 POINT pos;
1071 DWORD hover_time;
1072 INT hittest;
1074 TRACE("%x, %x, %p, %u\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
1076 if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
1077 WARN("wrong TRACKMOUSEEVENT size from app\n");
1078 SetLastError(ERROR_INVALID_PARAMETER);
1079 return FALSE;
1082 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
1083 if (ptme->dwFlags & TME_QUERY )
1085 *ptme = tracking_info.tme;
1086 /* set cbSize in the case it's not initialized yet */
1087 ptme->cbSize = sizeof(TRACKMOUSEEVENT);
1089 return TRUE; /* return here, TME_QUERY is retrieving information */
1092 if (!IsWindow(ptme->hwndTrack))
1094 SetLastError(ERROR_INVALID_WINDOW_HANDLE);
1095 return FALSE;
1098 hover_time = ptme->dwHoverTime;
1100 /* if HOVER_DEFAULT was specified replace this with the systems current value.
1101 * TME_LEAVE doesn't need to specify hover time so use default */
1102 if (hover_time == HOVER_DEFAULT || hover_time == 0 || !(ptme->dwHoverTime&TME_HOVER))
1103 SystemParametersInfoW(SPI_GETMOUSEHOVERTIME, 0, &hover_time, 0);
1105 GetCursorPos(&pos);
1106 hwnd = WINPOS_WindowFromPoint(ptme->hwndTrack, pos, &hittest);
1107 TRACE("point %s hwnd %p hittest %d\n", wine_dbgstr_point(&pos), hwnd, hittest);
1109 if (ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT))
1110 FIXME("Unknown flag(s) %08x\n", ptme->dwFlags & ~(TME_CANCEL | TME_HOVER | TME_LEAVE | TME_NONCLIENT));
1112 if (ptme->dwFlags & TME_CANCEL)
1114 if (tracking_info.tme.hwndTrack == ptme->hwndTrack)
1116 tracking_info.tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
1118 /* if we aren't tracking on hover or leave remove this entry */
1119 if (!(tracking_info.tme.dwFlags & (TME_HOVER | TME_LEAVE)))
1121 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1122 timer = 0;
1123 tracking_info.tme.hwndTrack = 0;
1124 tracking_info.tme.dwFlags = 0;
1125 tracking_info.tme.dwHoverTime = 0;
1128 } else {
1129 /* In our implementation it's possible that another window will receive a
1130 * WM_MOUSEMOVE and call TrackMouseEvent before TrackMouseEventProc is
1131 * called. In such a situation post the WM_MOUSELEAVE now */
1132 if (tracking_info.tme.dwFlags & TME_LEAVE && tracking_info.tme.hwndTrack != NULL)
1133 check_mouse_leave(hwnd, hittest);
1135 if (timer)
1137 KillSystemTimer(tracking_info.tme.hwndTrack, timer);
1138 timer = 0;
1139 tracking_info.tme.hwndTrack = 0;
1140 tracking_info.tme.dwFlags = 0;
1141 tracking_info.tme.dwHoverTime = 0;
1144 if (ptme->hwndTrack == hwnd)
1146 /* Adding new mouse event to the tracking list */
1147 tracking_info.tme = *ptme;
1148 tracking_info.tme.dwHoverTime = hover_time;
1150 /* Initialize HoverInfo variables even if not hover tracking */
1151 tracking_info.pos = pos;
1153 timer = SetSystemTimer(tracking_info.tme.hwndTrack, (UINT_PTR)&tracking_info.tme, hover_time, TrackMouseEventProc);
1157 return TRUE;
1160 /***********************************************************************
1161 * GetMouseMovePointsEx [USER32]
1163 * RETURNS
1164 * Success: count of point set in the buffer
1165 * Failure: -1
1167 int WINAPI GetMouseMovePointsEx(UINT size, LPMOUSEMOVEPOINT ptin, LPMOUSEMOVEPOINT ptout, int count, DWORD res) {
1169 if((size != sizeof(MOUSEMOVEPOINT)) || (count < 0) || (count > 64)) {
1170 SetLastError(ERROR_INVALID_PARAMETER);
1171 return -1;
1174 if(!ptin || (!ptout && count)) {
1175 SetLastError(ERROR_NOACCESS);
1176 return -1;
1179 FIXME("(%d %p %p %d %d) stub\n", size, ptin, ptout, count, res);
1181 SetLastError(ERROR_POINT_NOT_FOUND);
1182 return -1;