Release 20031118.
[wine/multimedia.git] / windows / input.c
blobed845daa79f99f19f39b34c08c7b945c0b1abca7
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <assert.h>
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "winnls.h"
39 #include "wine/winbase16.h"
40 #include "wine/winuser16.h"
41 #include "wine/server.h"
42 #include "win.h"
43 #include "message.h"
44 #include "winternl.h"
45 #include "wine/debug.h"
46 #include "winerror.h"
48 WINE_DECLARE_DEBUG_CHANNEL(key);
49 WINE_DECLARE_DEBUG_CHANNEL(keyboard);
50 WINE_DECLARE_DEBUG_CHANNEL(win);
51 WINE_DEFAULT_DEBUG_CHANNEL(event);
53 static BOOL InputEnabled = TRUE;
54 static BOOL SwappedButtons;
56 BYTE InputKeyStateTable[256];
57 BYTE AsyncKeyStateTable[256];
58 BYTE TrackSysKey = 0; /* determine whether ALT key up will cause a WM_SYSKEYUP
59 or a WM_KEYUP message */
61 /* Storage for the USER-maintained mouse positions */
62 static DWORD PosX, PosY;
64 typedef union
66 struct
68 unsigned long count : 16;
69 unsigned long code : 8;
70 unsigned long extended : 1;
71 unsigned long unused : 2;
72 unsigned long win_internal : 2;
73 unsigned long context : 1;
74 unsigned long previous : 1;
75 unsigned long transition : 1;
76 } lp1;
77 unsigned long lp2;
78 } KEYLP;
81 /***********************************************************************
82 * get_key_state
84 static WORD get_key_state(void)
86 WORD ret = 0;
88 if (SwappedButtons)
90 if (InputKeyStateTable[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
91 if (InputKeyStateTable[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
93 else
95 if (InputKeyStateTable[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
96 if (InputKeyStateTable[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
98 if (InputKeyStateTable[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
99 if (InputKeyStateTable[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
100 if (InputKeyStateTable[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
101 if (InputKeyStateTable[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
102 if (InputKeyStateTable[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
103 return ret;
107 /***********************************************************************
108 * queue_hardware_message
110 * Add a message to the hardware queue.
111 * Note: the position is relative to the desktop window.
113 static void queue_hardware_message( UINT message, HWND hwnd, WPARAM wParam, LPARAM lParam,
114 int xPos, int yPos, DWORD time, ULONG_PTR extraInfo )
116 SERVER_START_REQ( send_message )
118 req->id = GetCurrentThreadId();
119 req->type = MSG_HARDWARE;
120 req->flags = 0;
121 req->win = hwnd;
122 req->msg = message;
123 req->wparam = wParam;
124 req->lparam = lParam;
125 req->x = xPos;
126 req->y = yPos;
127 req->time = time;
128 req->info = extraInfo;
129 req->timeout = -1;
130 req->callback = NULL;
131 wine_server_call( req );
133 SERVER_END_REQ;
137 /***********************************************************************
138 * queue_kbd_event
140 * Put a keyboard event into a thread queue
142 static void queue_kbd_event( const KEYBDINPUT *ki, UINT injected_flags )
144 UINT message;
145 KEYLP keylp;
146 KBDLLHOOKSTRUCT hook;
148 keylp.lp2 = 0;
149 keylp.lp1.count = 1;
150 keylp.lp1.code = ki->wScan;
151 keylp.lp1.extended = (ki->dwFlags & KEYEVENTF_EXTENDEDKEY) != 0;
152 keylp.lp1.win_internal = 0; /* this has something to do with dialogs,
153 * don't remember where I read it - AK */
154 /* it's '1' under windows, when a dialog box appears
155 * and you press one of the underlined keys - DF*/
157 /* note that there is a test for all this */
158 if (ki->dwFlags & KEYEVENTF_KEYUP )
160 message = WM_KEYUP;
161 if( (InputKeyStateTable[VK_MENU] & 0x80) && (
162 (ki->wVk == VK_MENU) || (ki->wVk == VK_CONTROL) ||
163 !(InputKeyStateTable[VK_CONTROL] & 0x80))) {
164 if( TrackSysKey == VK_MENU || /* <ALT>-down/<ALT>-up sequence */
165 (ki->wVk != VK_MENU)) /* <ALT>-down...<something else>-up */
166 message = WM_SYSKEYUP;
167 TrackSysKey = 0;
169 InputKeyStateTable[ki->wVk] &= ~0x80;
170 keylp.lp1.previous = 1;
171 keylp.lp1.transition = 1;
173 else
175 keylp.lp1.previous = (InputKeyStateTable[ki->wVk] & 0x80) != 0;
176 keylp.lp1.transition = 0;
177 if (!(InputKeyStateTable[ki->wVk] & 0x80)) InputKeyStateTable[ki->wVk] ^= 0x01;
178 InputKeyStateTable[ki->wVk] |= 0x80;
179 AsyncKeyStateTable[ki->wVk] |= 0x80;
181 message = WM_KEYDOWN;
182 if( (InputKeyStateTable[VK_MENU] & 0x80) &&
183 !(InputKeyStateTable[VK_CONTROL] & 0x80)) {
184 message = WM_SYSKEYDOWN;
185 TrackSysKey = ki->wVk;
189 keylp.lp1.context = (InputKeyStateTable[VK_MENU] & 0x80) != 0; /* 1 if alt */
191 TRACE_(key)(" wParam=%04x, lParam=%08lx, InputKeyState=%x\n",
192 ki->wVk, keylp.lp2, InputKeyStateTable[ki->wVk] );
194 hook.vkCode = ki->wVk;
195 hook.scanCode = ki->wScan;
196 hook.flags = (keylp.lp2 >> 24) | injected_flags;
197 hook.time = ki->time;
198 hook.dwExtraInfo = ki->dwExtraInfo;
199 if (!HOOK_CallHooks( WH_KEYBOARD_LL, HC_ACTION, message, (LPARAM)&hook, TRUE ))
200 queue_hardware_message( message, 0, ki->wVk, keylp.lp2,
201 PosX, PosY, ki->time, ki->dwExtraInfo );
205 /***********************************************************************
206 * queue_raw_mouse_message
208 static void queue_raw_mouse_message( UINT message, UINT flags, INT x, INT y, const MOUSEINPUT *mi )
210 MSLLHOOKSTRUCT hook;
212 hook.pt.x = x;
213 hook.pt.y = y;
214 hook.mouseData = MAKELONG( 0, mi->mouseData );
215 hook.flags = flags;
216 hook.time = mi->time;
217 hook.dwExtraInfo = mi->dwExtraInfo;
219 if (!HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE ))
220 queue_hardware_message( message, (HWND)mi->dwExtraInfo /*FIXME*/,
221 MAKEWPARAM( get_key_state(), mi->mouseData ),
222 0, x, y, mi->time, mi->dwExtraInfo );
226 /***********************************************************************
227 * queue_mouse_event
229 static void queue_mouse_event( const MOUSEINPUT *mi, UINT flags )
231 if (mi->dwFlags & MOUSEEVENTF_ABSOLUTE)
233 PosX = (mi->dx * GetSystemMetrics(SM_CXSCREEN)) >> 16;
234 PosY = (mi->dy * GetSystemMetrics(SM_CYSCREEN)) >> 16;
236 else if (mi->dwFlags & MOUSEEVENTF_MOVE)
238 int width = GetSystemMetrics(SM_CXSCREEN);
239 int height = GetSystemMetrics(SM_CYSCREEN);
240 long posX = (long) PosX, posY = (long) PosY;
241 int accel[3];
242 int accelMult;
244 /* dx and dy can be negative numbers for relative movements */
245 SystemParametersInfoA(SPI_GETMOUSE, 0, accel, 0);
247 accelMult = 1;
248 if (mi->dx > accel[0] && accel[2] != 0)
250 accelMult = 2;
251 if ((mi->dx > accel[1]) && (accel[2] == 2))
253 accelMult = 4;
256 posX += (long)mi->dx * accelMult;
258 accelMult = 1;
259 if (mi->dy > accel[0] && accel[2] != 0)
261 accelMult = 2;
262 if ((mi->dy > accel[1]) && (accel[2] == 2))
264 accelMult = 4;
267 posY += (long)mi->dy * accelMult;
269 /* Clip to the current screen size */
270 if (posX < 0) PosX = 0;
271 else if (posX >= width) PosX = width - 1;
272 else PosX = posX;
274 if (posY < 0) PosY = 0;
275 else if (posY >= height) PosY = height - 1;
276 else PosY = posY;
279 if (mi->dwFlags & MOUSEEVENTF_MOVE)
281 queue_raw_mouse_message( WM_MOUSEMOVE, flags, PosX, PosY, mi );
283 if (mi->dwFlags & MOUSEEVENTF_LEFTDOWN)
285 InputKeyStateTable[VK_LBUTTON] |= 0x80;
286 AsyncKeyStateTable[VK_LBUTTON] |= 0x80;
287 queue_raw_mouse_message( SwappedButtons ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
288 flags, PosX, PosY, mi );
290 if (mi->dwFlags & MOUSEEVENTF_LEFTUP)
292 InputKeyStateTable[VK_LBUTTON] &= ~0x80;
293 queue_raw_mouse_message( SwappedButtons ? WM_RBUTTONUP : WM_LBUTTONUP,
294 flags, PosX, PosY, mi );
296 if (mi->dwFlags & MOUSEEVENTF_RIGHTDOWN)
298 InputKeyStateTable[VK_RBUTTON] |= 0x80;
299 AsyncKeyStateTable[VK_RBUTTON] |= 0x80;
300 queue_raw_mouse_message( SwappedButtons ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
301 flags, PosX, PosY, mi );
303 if (mi->dwFlags & MOUSEEVENTF_RIGHTUP)
305 InputKeyStateTable[VK_RBUTTON] &= ~0x80;
306 queue_raw_mouse_message( SwappedButtons ? WM_LBUTTONUP : WM_RBUTTONUP,
307 flags, PosX, PosY, mi );
309 if (mi->dwFlags & MOUSEEVENTF_MIDDLEDOWN)
311 InputKeyStateTable[VK_MBUTTON] |= 0x80;
312 AsyncKeyStateTable[VK_MBUTTON] |= 0x80;
313 queue_raw_mouse_message( WM_MBUTTONDOWN, flags, PosX, PosY, mi );
315 if (mi->dwFlags & MOUSEEVENTF_MIDDLEUP)
317 InputKeyStateTable[VK_MBUTTON] &= ~0x80;
318 queue_raw_mouse_message( WM_MBUTTONUP, flags, PosX, PosY, mi );
320 if (mi->dwFlags & MOUSEEVENTF_WHEEL)
322 queue_raw_mouse_message( WM_MOUSEWHEEL, flags, PosX, PosY, mi );
324 if (flags & LLMHF_INJECTED) /* we have to actually move the cursor */
325 SetCursorPos( PosX, PosY );
329 /***********************************************************************
330 * SendInput (USER32.@)
332 UINT WINAPI SendInput( UINT count, LPINPUT inputs, int size )
334 UINT i;
336 if (!InputEnabled) return 0;
338 for (i = 0; i < count; i++, inputs++)
340 switch(inputs->type)
342 case INPUT_MOUSE:
343 queue_mouse_event( &inputs->u.mi, LLMHF_INJECTED );
344 break;
345 case WINE_INTERNAL_INPUT_MOUSE:
346 queue_mouse_event( &inputs->u.mi, 0 );
347 break;
348 case INPUT_KEYBOARD:
349 queue_kbd_event( &inputs->u.ki, LLKHF_INJECTED );
350 break;
351 case WINE_INTERNAL_INPUT_KEYBOARD:
352 queue_kbd_event( &inputs->u.ki, 0 );
353 break;
354 case INPUT_HARDWARE:
355 FIXME( "INPUT_HARDWARE not supported\n" );
356 break;
359 return count;
363 /***********************************************************************
364 * keybd_event (USER32.@)
366 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
367 DWORD dwFlags, ULONG_PTR dwExtraInfo )
369 INPUT input;
371 input.type = INPUT_KEYBOARD;
372 input.u.ki.wVk = bVk;
373 input.u.ki.wScan = bScan;
374 input.u.ki.dwFlags = dwFlags;
375 input.u.ki.time = GetTickCount();
376 input.u.ki.dwExtraInfo = dwExtraInfo;
377 SendInput( 1, &input, sizeof(input) );
381 /***********************************************************************
382 * keybd_event (USER.289)
384 void WINAPI keybd_event16( CONTEXT86 *context )
386 DWORD dwFlags = 0;
388 if (HIBYTE(context->Eax) & 0x80) dwFlags |= KEYEVENTF_KEYUP;
389 if (HIBYTE(context->Ebx) & 0x01) dwFlags |= KEYEVENTF_EXTENDEDKEY;
391 keybd_event( LOBYTE(context->Eax), LOBYTE(context->Ebx),
392 dwFlags, MAKELONG(LOWORD(context->Esi), LOWORD(context->Edi)) );
396 /***********************************************************************
397 * mouse_event (USER32.@)
399 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
400 DWORD dwData, ULONG_PTR dwExtraInfo )
402 INPUT input;
404 input.type = INPUT_MOUSE;
405 input.u.mi.dx = dx;
406 input.u.mi.dy = dy;
407 input.u.mi.mouseData = dwData;
408 input.u.mi.dwFlags = dwFlags;
409 input.u.mi.time = GetCurrentTime();
410 input.u.mi.dwExtraInfo = dwExtraInfo;
411 SendInput( 1, &input, sizeof(input) );
415 /***********************************************************************
416 * mouse_event (USER.299)
418 void WINAPI mouse_event16( CONTEXT86 *context )
420 mouse_event( LOWORD(context->Eax), LOWORD(context->Ebx), LOWORD(context->Ecx),
421 LOWORD(context->Edx), MAKELONG(context->Esi, context->Edi) );
424 /***********************************************************************
425 * GetMouseEventProc (USER.337)
427 FARPROC16 WINAPI GetMouseEventProc16(void)
429 HMODULE16 hmodule = GetModuleHandle16("USER");
430 return GetProcAddress16( hmodule, "mouse_event" );
434 /**********************************************************************
435 * EnableHardwareInput (USER.331)
437 BOOL16 WINAPI EnableHardwareInput16(BOOL16 bEnable)
439 BOOL16 bOldState = InputEnabled;
440 FIXME_(event)("(%d) - stub\n", bEnable);
441 InputEnabled = bEnable;
442 return bOldState;
446 /***********************************************************************
447 * SwapMouseButton (USER.186)
449 BOOL16 WINAPI SwapMouseButton16( BOOL16 fSwap )
451 BOOL16 ret = SwappedButtons;
452 SwappedButtons = fSwap;
453 return ret;
457 /***********************************************************************
458 * SwapMouseButton (USER32.@)
460 BOOL WINAPI SwapMouseButton( BOOL fSwap )
462 BOOL ret = SwappedButtons;
463 SwappedButtons = fSwap;
464 return ret;
468 /***********************************************************************
469 * GetCursorPos (USER.17)
471 BOOL16 WINAPI GetCursorPos16( POINT16 *pt )
473 POINT pos;
474 if (!pt) return 0;
475 GetCursorPos(&pos);
476 pt->x = pos.x;
477 pt->y = pos.y;
478 return 1;
482 /***********************************************************************
483 * GetCursorPos (USER32.@)
485 BOOL WINAPI GetCursorPos( POINT *pt )
487 if (!pt) return 0;
488 pt->x = PosX;
489 pt->y = PosY;
490 if (USER_Driver.pGetCursorPos) USER_Driver.pGetCursorPos( pt );
491 return 1;
495 /***********************************************************************
496 * GetCursorInfo (USER32.@)
498 BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
500 MESSAGEQUEUE *queue = QUEUE_Current();
502 if (!pci) return 0;
503 if (queue->cursor_count >= 0) pci->flags = CURSOR_SHOWING;
504 else pci->flags = 0;
505 GetCursorPos(&pci->ptScreenPos);
506 return 1;
510 /***********************************************************************
511 * SetCursorPos (USER.70)
513 void WINAPI SetCursorPos16( INT16 x, INT16 y )
515 SetCursorPos( x, y );
519 /***********************************************************************
520 * SetCursorPos (USER32.@)
522 BOOL WINAPI SetCursorPos( INT x, INT y )
524 if (USER_Driver.pSetCursorPos) USER_Driver.pSetCursorPos( x, y );
525 PosX = x;
526 PosY = y;
527 return TRUE;
531 /**********************************************************************
532 * SetCapture (USER32.@)
534 HWND WINAPI SetCapture( HWND hwnd )
536 HWND previous = 0;
538 SERVER_START_REQ( set_capture_window )
540 req->handle = hwnd;
541 req->flags = 0;
542 if (!wine_server_call_err( req ))
544 previous = reply->previous;
545 hwnd = reply->full_handle;
548 SERVER_END_REQ;
550 if (previous && previous != hwnd)
551 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
552 return previous;
556 /**********************************************************************
557 * ReleaseCapture (USER32.@)
559 BOOL WINAPI ReleaseCapture(void)
561 return (SetCapture(0) != 0);
565 /**********************************************************************
566 * GetCapture (USER32.@)
568 HWND WINAPI GetCapture(void)
570 HWND ret = 0;
572 SERVER_START_REQ( get_thread_input )
574 req->tid = GetCurrentThreadId();
575 if (!wine_server_call_err( req )) ret = reply->capture;
577 SERVER_END_REQ;
578 return ret;
582 /**********************************************************************
583 * GetAsyncKeyState (USER32.@)
585 * Determine if a key is or was pressed. retval has high-order
586 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
587 * been pressed.
589 * This uses the variable AsyncMouseButtonsStates and
590 * AsyncKeyStateTable (set in event.c) which have the mouse button
591 * number or key number (whichever is applicable) set to true if the
592 * mouse or key had been depressed since the last call to
593 * GetAsyncKeyState.
595 SHORT WINAPI GetAsyncKeyState(INT nKey)
597 SHORT retval = ((AsyncKeyStateTable[nKey] & 0x80) ? 0x0001 : 0) |
598 ((InputKeyStateTable[nKey] & 0x80) ? 0x8000 : 0);
599 AsyncKeyStateTable[nKey] = 0;
600 TRACE_(key)("(%x) -> %x\n", nKey, retval);
601 return retval;
604 /**********************************************************************
605 * GetAsyncKeyState (USER.249)
607 INT16 WINAPI GetAsyncKeyState16(INT16 nKey)
609 return GetAsyncKeyState(nKey);
612 /***********************************************************************
613 * IsUserIdle (USER.333)
615 BOOL16 WINAPI IsUserIdle16(void)
617 if ( GetAsyncKeyState( VK_LBUTTON ) & 0x8000 )
618 return FALSE;
620 if ( GetAsyncKeyState( VK_RBUTTON ) & 0x8000 )
621 return FALSE;
623 if ( GetAsyncKeyState( VK_MBUTTON ) & 0x8000 )
624 return FALSE;
626 /* Should check for screen saver activation here ... */
628 return TRUE;
631 /**********************************************************************
632 * VkKeyScanA (USER32.@)
634 * VkKeyScan translates an ANSI character to a virtual-key and shift code
635 * for the current keyboard.
636 * high-order byte yields :
637 * 0 Unshifted
638 * 1 Shift
639 * 2 Ctrl
640 * 3-5 Shift-key combinations that are not used for characters
641 * 6 Ctrl-Alt
642 * 7 Ctrl-Alt-Shift
643 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
644 * FIXME : works ok except for dead chars :
645 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
646 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
648 SHORT WINAPI VkKeyScanA(CHAR cChar)
650 WCHAR wChar;
652 if (IsDBCSLeadByte(cChar)) return -1;
654 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
655 return VkKeyScanW(wChar);
658 /******************************************************************************
659 * VkKeyScanW (USER32.@)
661 SHORT WINAPI VkKeyScanW(WCHAR cChar)
663 return VkKeyScanExW(cChar, GetKeyboardLayout(0));
666 /**********************************************************************
667 * VkKeyScanExA (USER32.@)
669 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
671 WCHAR wChar;
673 if (IsDBCSLeadByte(cChar)) return -1;
675 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
676 return VkKeyScanExW(wChar, dwhkl);
679 /******************************************************************************
680 * VkKeyScanExW (USER32.@)
682 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
684 if (USER_Driver.pVkKeyScanEx)
685 return USER_Driver.pVkKeyScanEx(cChar, dwhkl);
686 return -1;
689 /******************************************************************************
690 * GetKeyboardType (USER32.@)
692 INT WINAPI GetKeyboardType(INT nTypeFlag)
694 TRACE_(keyboard)("(%d)\n", nTypeFlag);
695 switch(nTypeFlag)
697 case 0: /* Keyboard type */
698 return 4; /* AT-101 */
699 case 1: /* Keyboard Subtype */
700 return 0; /* There are no defined subtypes */
701 case 2: /* Number of F-keys */
702 return 12; /* We're doing an 101 for now, so return 12 F-keys */
703 default:
704 WARN_(keyboard)("Unknown type\n");
705 return 0; /* The book says 0 here, so 0 */
709 /******************************************************************************
710 * MapVirtualKeyA (USER32.@)
712 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
714 return MapVirtualKeyExA( code, maptype, GetKeyboardLayout(0) );
717 /******************************************************************************
718 * MapVirtualKeyW (USER32.@)
720 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
722 return MapVirtualKeyExW(code, maptype, GetKeyboardLayout(0));
725 /******************************************************************************
726 * MapVirtualKeyExA (USER32.@)
728 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
730 return MapVirtualKeyExW(code, maptype, hkl);
733 /******************************************************************************
734 * MapVirtualKeyExW (USER32.@)
736 UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
738 TRACE_(keyboard)("(%d, %d, %p)\n", code, maptype, hkl);
740 if (USER_Driver.pMapVirtualKeyEx)
741 return USER_Driver.pMapVirtualKeyEx(code, maptype, hkl);
742 return 0;
745 /****************************************************************************
746 * GetKBCodePage (USER32.@)
748 UINT WINAPI GetKBCodePage(void)
750 return GetOEMCP();
753 /****************************************************************************
754 * GetKeyboardLayoutName (USER.477)
756 INT16 WINAPI GetKeyboardLayoutName16(LPSTR pwszKLID)
758 return GetKeyboardLayoutNameA(pwszKLID);
761 /***********************************************************************
762 * GetKeyboardLayout (USER32.@)
764 * - device handle for keyboard layout defaulted to
765 * the language id. This is the way Windows default works.
766 * - the thread identifier (dwLayout) is also ignored.
768 HKL WINAPI GetKeyboardLayout(DWORD dwLayout)
770 if (USER_Driver.pGetKeyboardLayout)
771 return USER_Driver.pGetKeyboardLayout(dwLayout);
772 return 0;
775 /****************************************************************************
776 * GetKeyboardLayoutNameA (USER32.@)
778 BOOL WINAPI GetKeyboardLayoutNameA(LPSTR pszKLID)
780 WCHAR buf[KL_NAMELENGTH];
782 if (GetKeyboardLayoutNameW(buf))
783 return WideCharToMultiByte( CP_ACP, 0, buf, -1, pszKLID, KL_NAMELENGTH, NULL, NULL ) != 0;
784 return FALSE;
787 /****************************************************************************
788 * GetKeyboardLayoutNameW (USER32.@)
790 BOOL WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
792 if (USER_Driver.pGetKeyboardLayoutName)
793 return USER_Driver.pGetKeyboardLayoutName(pwszKLID);
794 return FALSE;
797 /****************************************************************************
798 * GetKeyNameTextA (USER32.@)
800 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
802 WCHAR buf[256];
803 INT ret;
805 if (!GetKeyNameTextW(lParam, buf, 256))
806 return 0;
807 ret = WideCharToMultiByte(CP_ACP, 0, buf, -1, lpBuffer, nSize, NULL, NULL);
808 if (!ret && nSize)
810 ret = nSize - 1;
811 lpBuffer[ret] = 0;
813 return ret;
816 /****************************************************************************
817 * GetKeyNameTextW (USER32.@)
819 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
821 if (USER_Driver.pGetKeyNameText)
822 return USER_Driver.pGetKeyNameText( lParam, lpBuffer, nSize );
823 return 0;
826 /****************************************************************************
827 * ToUnicode (USER32.@)
829 INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
830 LPWSTR lpwStr, int size, UINT flags)
832 return ToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, GetKeyboardLayout(0));
835 /****************************************************************************
836 * ToUnicodeEx (USER32.@)
838 INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
839 LPWSTR lpwStr, int size, UINT flags, HKL hkl)
841 if (USER_Driver.pToUnicodeEx)
842 return USER_Driver.pToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, hkl);
843 return 0;
846 /****************************************************************************
847 * ToAscii (USER32.@)
849 INT WINAPI ToAscii( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
850 LPWORD lpChar, UINT flags )
852 return ToAsciiEx(virtKey, scanCode, lpKeyState, lpChar, flags, GetKeyboardLayout(0));
855 /****************************************************************************
856 * ToAsciiEx (USER32.@)
858 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
859 LPWORD lpChar, UINT flags, HKL dwhkl )
861 WCHAR uni_chars[2];
862 INT ret, n_ret;
864 ret = ToUnicodeEx(virtKey, scanCode, lpKeyState, uni_chars, 2, flags, dwhkl);
865 if (ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
866 else n_ret = ret;
867 WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
868 return ret;
871 /**********************************************************************
872 * ActivateKeyboardLayout (USER32.@)
874 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
876 TRACE_(keyboard)("(%p, %d)\n", hLayout, flags);
878 if (USER_Driver.pActivateKeyboardLayout)
879 return USER_Driver.pActivateKeyboardLayout(hLayout, flags);
880 return 0;
884 /***********************************************************************
885 * GetKeyboardLayoutList (USER32.@)
887 * Return number of values available if either input parm is
888 * 0, per MS documentation.
890 UINT WINAPI GetKeyboardLayoutList(INT nBuff, HKL *layouts)
892 TRACE_(keyboard)("(%d,%p)\n",nBuff,layouts);
894 if (USER_Driver.pGetKeyboardLayoutList)
895 return USER_Driver.pGetKeyboardLayoutList(nBuff, layouts);
896 return 0;
900 /***********************************************************************
901 * RegisterHotKey (USER32.@)
903 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk) {
904 FIXME_(keyboard)("(%p,%d,0x%08x,%d): stub\n",hwnd,id,modifiers,vk);
905 return TRUE;
908 /***********************************************************************
909 * UnregisterHotKey (USER32.@)
911 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id) {
912 FIXME_(keyboard)("(%p,%d): stub\n",hwnd,id);
913 return TRUE;
916 /***********************************************************************
917 * LoadKeyboardLayoutW (USER32.@)
919 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
921 TRACE_(keyboard)("(%s, %d)\n", debugstr_w(pwszKLID), Flags);
923 if (USER_Driver.pLoadKeyboardLayout)
924 return USER_Driver.pLoadKeyboardLayout(pwszKLID, Flags);
925 return 0;
928 /***********************************************************************
929 * LoadKeyboardLayoutA (USER32.@)
931 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
933 HKL ret;
934 UNICODE_STRING pwszKLIDW;
936 if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
937 else pwszKLIDW.Buffer = NULL;
939 ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
940 RtlFreeUnicodeString(&pwszKLIDW);
941 return ret;
945 /***********************************************************************
946 * UnloadKeyboardLayout (USER32.@)
948 BOOL WINAPI UnloadKeyboardLayout(HKL hkl)
950 TRACE_(keyboard)("(%p)\n", hkl);
952 if (USER_Driver.pUnloadKeyboardLayout)
953 return USER_Driver.pUnloadKeyboardLayout(hkl);
954 return 0;
957 typedef struct __TRACKINGLIST {
958 TRACKMOUSEEVENT tme;
959 POINT pos; /* center of hover rectangle */
960 INT iHoverTime; /* elapsed time the cursor has been inside of the hover rect */
961 } _TRACKINGLIST;
963 static _TRACKINGLIST TrackingList[10];
964 static int iTrackMax = 0;
965 static UINT_PTR timer;
966 static const INT iTimerInterval = 50; /* msec for timer interval */
968 /* FIXME: need to implement WM_NCMOUSELEAVE and WM_NCMOUSEHOVER for */
969 /* TrackMouseEventProc and _TrackMouseEvent */
970 static void CALLBACK TrackMouseEventProc(HWND hwndUnused, UINT uMsg, UINT_PTR idEvent,
971 DWORD dwTime)
973 int i = 0;
974 POINT pos;
975 POINT posClient;
976 HWND hwnd;
977 INT hoverwidth = 0, hoverheight = 0;
979 GetCursorPos(&pos);
980 hwnd = WindowFromPoint(pos);
982 SystemParametersInfoA(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
983 SystemParametersInfoA(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
985 /* loop through tracking events we are processing */
986 while (i < iTrackMax) {
987 /* see if this tracking event is looking for TME_LEAVE and that the */
988 /* mouse has left the window */
989 if ((TrackingList[i].tme.dwFlags & TME_LEAVE) &&
990 (TrackingList[i].tme.hwndTrack != hwnd)) {
991 PostMessageA(TrackingList[i].tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
993 /* remove the TME_LEAVE flag */
994 TrackingList[i].tme.dwFlags ^= TME_LEAVE;
997 /* see if we are tracking hovering for this hwnd */
998 if(TrackingList[i].tme.dwFlags & TME_HOVER) {
999 /* add the timer interval to the hovering time */
1000 TrackingList[i].iHoverTime+=iTimerInterval;
1002 /* has the cursor moved outside the rectangle centered around pos? */
1003 if((abs(pos.x - TrackingList[i].pos.x) > (hoverwidth / 2.0))
1004 || (abs(pos.y - TrackingList[i].pos.y) > (hoverheight / 2.0)))
1006 /* record this new position as the current position and reset */
1007 /* the iHoverTime variable to 0 */
1008 TrackingList[i].pos = pos;
1009 TrackingList[i].iHoverTime = 0;
1012 /* has the mouse hovered long enough? */
1013 if(TrackingList[i].iHoverTime <= TrackingList[i].tme.dwHoverTime)
1015 posClient.x = pos.x;
1016 posClient.y = pos.y;
1017 ScreenToClient(hwnd, &posClient);
1018 PostMessageW(TrackingList[i].tme.hwndTrack, WM_MOUSEHOVER,
1019 get_key_state(), MAKELPARAM( posClient.x, posClient.y ));
1021 /* stop tracking mouse hover */
1022 TrackingList[i].tme.dwFlags ^= TME_HOVER;
1026 /* see if we are still tracking TME_HOVER or TME_LEAVE for this entry */
1027 if((TrackingList[i].tme.dwFlags & TME_HOVER) ||
1028 (TrackingList[i].tme.dwFlags & TME_LEAVE)) {
1029 i++;
1030 } else { /* remove this entry from the tracking list */
1031 TrackingList[i] = TrackingList[--iTrackMax];
1035 /* stop the timer if the tracking list is empty */
1036 if(iTrackMax == 0) {
1037 KillTimer(0, timer);
1038 timer = 0;
1043 /***********************************************************************
1044 * TrackMouseEvent [USER32]
1046 * Requests notification of mouse events
1048 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
1049 * to the hwnd specified in the ptme structure. After the event message
1050 * is posted to the hwnd, the entry in the queue is removed.
1052 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
1053 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
1054 * immediately and the TME_LEAVE flag being ignored.
1056 * PARAMS
1057 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
1059 * RETURNS
1060 * Success: non-zero
1061 * Failure: zero
1065 BOOL WINAPI
1066 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
1068 DWORD flags = 0;
1069 int i = 0;
1070 BOOL cancel = 0, hover = 0, leave = 0, query = 0;
1071 HWND hwnd;
1072 POINT pos;
1074 pos.x = 0;
1075 pos.y = 0;
1077 TRACE("%lx, %lx, %p, %lx\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
1079 if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
1080 WARN("wrong TRACKMOUSEEVENT size from app\n");
1081 SetLastError(ERROR_INVALID_PARAMETER); /* FIXME not sure if this is correct */
1082 return FALSE;
1085 flags = ptme->dwFlags;
1087 /* if HOVER_DEFAULT was specified replace this with the systems current value */
1088 if(ptme->dwHoverTime == HOVER_DEFAULT)
1089 SystemParametersInfoA(SPI_GETMOUSEHOVERTIME, 0, &(ptme->dwHoverTime), 0);
1091 GetCursorPos(&pos);
1092 hwnd = WindowFromPoint(pos);
1094 if ( flags & TME_CANCEL ) {
1095 flags &= ~ TME_CANCEL;
1096 cancel = 1;
1099 if ( flags & TME_HOVER ) {
1100 flags &= ~ TME_HOVER;
1101 hover = 1;
1104 if ( flags & TME_LEAVE ) {
1105 flags &= ~ TME_LEAVE;
1106 leave = 1;
1109 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
1110 if ( flags & TME_QUERY ) {
1111 flags &= ~ TME_QUERY;
1112 query = 1;
1113 i = 0;
1115 /* Find the tracking list entry with the matching hwnd */
1116 while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
1117 i++;
1120 /* hwnd found, fill in the ptme struct */
1121 if(i < iTrackMax)
1122 *ptme = TrackingList[i].tme;
1123 else
1124 ptme->dwFlags = 0;
1126 return TRUE; /* return here, TME_QUERY is retrieving information */
1129 if ( flags )
1130 FIXME("Unknown flag(s) %08lx\n", flags );
1132 if(cancel) {
1133 /* find a matching hwnd if one exists */
1134 i = 0;
1136 while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
1137 i++;
1140 if(i < iTrackMax) {
1141 TrackingList[i].tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
1143 /* if we aren't tracking on hover or leave remove this entry */
1144 if(!((TrackingList[i].tme.dwFlags & TME_HOVER) ||
1145 (TrackingList[i].tme.dwFlags & TME_LEAVE)))
1147 TrackingList[i] = TrackingList[--iTrackMax];
1149 if(iTrackMax == 0) {
1150 KillTimer(0, timer);
1151 timer = 0;
1155 } else {
1156 /* see if hwndTrack isn't the current window */
1157 if(ptme->hwndTrack != hwnd) {
1158 if(leave) {
1159 PostMessageA(ptme->hwndTrack, WM_MOUSELEAVE, 0, 0);
1161 } else {
1162 /* See if this hwnd is already being tracked and update the tracking flags */
1163 for(i = 0; i < iTrackMax; i++) {
1164 if(TrackingList[i].tme.hwndTrack == ptme->hwndTrack) {
1165 if(hover) {
1166 TrackingList[i].tme.dwFlags |= TME_HOVER;
1167 TrackingList[i].tme.dwHoverTime = ptme->dwHoverTime;
1170 if(leave)
1171 TrackingList[i].tme.dwFlags |= TME_LEAVE;
1173 /* reset iHoverTime as per winapi specs */
1174 TrackingList[i].iHoverTime = 0;
1176 return TRUE;
1180 /* if the tracking list is full return FALSE */
1181 if (iTrackMax == sizeof (TrackingList) / sizeof(*TrackingList)) {
1182 return FALSE;
1185 /* Adding new mouse event to the tracking list */
1186 TrackingList[iTrackMax].tme = *ptme;
1188 /* Initialize HoverInfo variables even if not hover tracking */
1189 TrackingList[iTrackMax].iHoverTime = 0;
1190 TrackingList[iTrackMax].pos = pos;
1192 iTrackMax++;
1194 if (!timer) {
1195 timer = SetTimer(0, 0, iTimerInterval, TrackMouseEventProc);
1200 return TRUE;