Use unions instead of defines in async work requests.
[wine/wine64.git] / windows / input.c
blob953d188286e3e1611d8693245416d453b2f1eccb
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];
59 /* Storage for the USER-maintained mouse positions */
60 static DWORD PosX, PosY;
62 typedef union
64 struct
66 unsigned long count : 16;
67 unsigned long code : 8;
68 unsigned long extended : 1;
69 unsigned long unused : 2;
70 unsigned long win_internal : 2;
71 unsigned long context : 1;
72 unsigned long previous : 1;
73 unsigned long transition : 1;
74 } lp1;
75 unsigned long lp2;
76 } KEYLP;
79 /***********************************************************************
80 * get_key_state
82 static WORD get_key_state(void)
84 WORD ret = 0;
86 if (SwappedButtons)
88 if (InputKeyStateTable[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
89 if (InputKeyStateTable[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
91 else
93 if (InputKeyStateTable[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
94 if (InputKeyStateTable[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
96 if (InputKeyStateTable[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
97 if (InputKeyStateTable[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
98 if (InputKeyStateTable[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
99 if (InputKeyStateTable[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
100 if (InputKeyStateTable[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
101 return ret;
105 /***********************************************************************
106 * queue_hardware_message
108 * Add a message to the hardware queue.
109 * Note: the position is relative to the desktop window.
111 static void queue_hardware_message( UINT message, HWND hwnd, WPARAM wParam, LPARAM lParam,
112 int xPos, int yPos, DWORD time, ULONG_PTR extraInfo )
114 SERVER_START_REQ( send_message )
116 req->id = GetCurrentThreadId();
117 req->type = MSG_HARDWARE;
118 req->flags = 0;
119 req->win = hwnd;
120 req->msg = message;
121 req->wparam = wParam;
122 req->lparam = lParam;
123 req->x = xPos;
124 req->y = yPos;
125 req->time = time;
126 req->info = extraInfo;
127 req->timeout = -1;
128 req->callback = NULL;
129 wine_server_call( req );
131 SERVER_END_REQ;
135 /***********************************************************************
136 * queue_kbd_event
138 * Put a keyboard event into a thread queue
140 static void queue_kbd_event( const KEYBDINPUT *ki, UINT injected_flags )
142 UINT message;
143 KEYLP keylp;
144 KBDLLHOOKSTRUCT hook;
146 keylp.lp2 = 0;
147 keylp.lp1.count = 1;
148 keylp.lp1.code = ki->wScan;
149 keylp.lp1.extended = (ki->dwFlags & KEYEVENTF_EXTENDEDKEY) != 0;
150 keylp.lp1.win_internal = 0; /* this has something to do with dialogs,
151 * don't remember where I read it - AK */
152 /* it's '1' under windows, when a dialog box appears
153 * and you press one of the underlined keys - DF*/
155 if (ki->dwFlags & KEYEVENTF_KEYUP )
157 BOOL sysKey = (InputKeyStateTable[VK_MENU] & 0x80) &&
158 !(InputKeyStateTable[VK_CONTROL] & 0x80);
159 InputKeyStateTable[ki->wVk] &= ~0x80;
160 keylp.lp1.previous = 1;
161 keylp.lp1.transition = 1;
162 message = sysKey ? WM_SYSKEYUP : WM_KEYUP;
164 else
166 keylp.lp1.previous = (InputKeyStateTable[ki->wVk] & 0x80) != 0;
167 keylp.lp1.transition = 0;
168 if (!(InputKeyStateTable[ki->wVk] & 0x80)) InputKeyStateTable[ki->wVk] ^= 0x01;
169 InputKeyStateTable[ki->wVk] |= 0x80;
170 AsyncKeyStateTable[ki->wVk] |= 0x80;
172 message = (InputKeyStateTable[VK_MENU] & 0x80) && !(InputKeyStateTable[VK_CONTROL] & 0x80)
173 ? WM_SYSKEYDOWN : WM_KEYDOWN;
176 keylp.lp1.context = (InputKeyStateTable[VK_MENU] & 0x80) != 0; /* 1 if alt */
178 TRACE_(key)(" wParam=%04x, lParam=%08lx, InputKeyState=%x\n",
179 ki->wVk, keylp.lp2, InputKeyStateTable[ki->wVk] );
181 hook.vkCode = ki->wVk;
182 hook.scanCode = ki->wScan;
183 hook.flags = (keylp.lp2 >> 24) | injected_flags;
184 hook.time = ki->time;
185 hook.dwExtraInfo = ki->dwExtraInfo;
186 if (!HOOK_CallHooks( WH_KEYBOARD_LL, HC_ACTION, message, (LPARAM)&hook, TRUE ))
187 queue_hardware_message( message, 0, ki->wVk, keylp.lp2,
188 PosX, PosY, ki->time, ki->dwExtraInfo );
192 /***********************************************************************
193 * queue_raw_mouse_message
195 static void queue_raw_mouse_message( UINT message, UINT flags, INT x, INT y, const MOUSEINPUT *mi )
197 MSLLHOOKSTRUCT hook;
199 hook.pt.x = x;
200 hook.pt.y = y;
201 hook.mouseData = MAKELONG( 0, mi->mouseData );
202 hook.flags = flags;
203 hook.time = mi->time;
204 hook.dwExtraInfo = mi->dwExtraInfo;
206 if (!HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE ))
207 queue_hardware_message( message, (HWND)mi->dwExtraInfo /*FIXME*/,
208 MAKEWPARAM( get_key_state(), mi->mouseData ),
209 0, x, y, mi->time, mi->dwExtraInfo );
213 /***********************************************************************
214 * queue_mouse_event
216 static void queue_mouse_event( const MOUSEINPUT *mi, UINT flags )
218 if (mi->dwFlags & MOUSEEVENTF_ABSOLUTE)
220 PosX = (mi->dx * GetSystemMetrics(SM_CXSCREEN)) >> 16;
221 PosY = (mi->dy * GetSystemMetrics(SM_CYSCREEN)) >> 16;
223 else if (mi->dwFlags & MOUSEEVENTF_MOVE)
225 int width = GetSystemMetrics(SM_CXSCREEN);
226 int height = GetSystemMetrics(SM_CYSCREEN);
227 long posX = (long) PosX, posY = (long) PosY;
228 int accel[3];
229 int accelMult;
231 /* dx and dy can be negative numbers for relative movements */
232 SystemParametersInfoA(SPI_GETMOUSE, 0, accel, 0);
234 accelMult = 1;
235 if (mi->dx > accel[0] && accel[2] != 0)
237 accelMult = 2;
238 if ((mi->dx > accel[1]) && (accel[2] == 2))
240 accelMult = 4;
243 posX += (long)mi->dx * accelMult;
245 accelMult = 1;
246 if (mi->dy > accel[0] && accel[2] != 0)
248 accelMult = 2;
249 if ((mi->dy > accel[1]) && (accel[2] == 2))
251 accelMult = 4;
254 posY += (long)mi->dy * accelMult;
256 /* Clip to the current screen size */
257 if (posX < 0) PosX = 0;
258 else if (posX >= width) PosX = width - 1;
259 else PosX = posX;
261 if (posY < 0) PosY = 0;
262 else if (posY >= height) PosY = height - 1;
263 else PosY = posY;
266 if (mi->dwFlags & MOUSEEVENTF_MOVE)
268 queue_raw_mouse_message( WM_MOUSEMOVE, flags, PosX, PosY, mi );
270 if (mi->dwFlags & MOUSEEVENTF_LEFTDOWN)
272 InputKeyStateTable[VK_LBUTTON] |= 0x80;
273 AsyncKeyStateTable[VK_LBUTTON] |= 0x80;
274 queue_raw_mouse_message( SwappedButtons ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
275 flags, PosX, PosY, mi );
277 if (mi->dwFlags & MOUSEEVENTF_LEFTUP)
279 InputKeyStateTable[VK_LBUTTON] &= ~0x80;
280 queue_raw_mouse_message( SwappedButtons ? WM_RBUTTONUP : WM_LBUTTONUP,
281 flags, PosX, PosY, mi );
283 if (mi->dwFlags & MOUSEEVENTF_RIGHTDOWN)
285 InputKeyStateTable[VK_RBUTTON] |= 0x80;
286 AsyncKeyStateTable[VK_RBUTTON] |= 0x80;
287 queue_raw_mouse_message( SwappedButtons ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
288 flags, PosX, PosY, mi );
290 if (mi->dwFlags & MOUSEEVENTF_RIGHTUP)
292 InputKeyStateTable[VK_RBUTTON] &= ~0x80;
293 queue_raw_mouse_message( SwappedButtons ? WM_LBUTTONUP : WM_RBUTTONUP,
294 flags, PosX, PosY, mi );
296 if (mi->dwFlags & MOUSEEVENTF_MIDDLEDOWN)
298 InputKeyStateTable[VK_MBUTTON] |= 0x80;
299 AsyncKeyStateTable[VK_MBUTTON] |= 0x80;
300 queue_raw_mouse_message( WM_MBUTTONDOWN, flags, PosX, PosY, mi );
302 if (mi->dwFlags & MOUSEEVENTF_MIDDLEUP)
304 InputKeyStateTable[VK_MBUTTON] &= ~0x80;
305 queue_raw_mouse_message( WM_MBUTTONUP, flags, PosX, PosY, mi );
307 if (mi->dwFlags & MOUSEEVENTF_WHEEL)
309 queue_raw_mouse_message( WM_MOUSEWHEEL, flags, PosX, PosY, mi );
311 if (flags & LLMHF_INJECTED) /* we have to actually move the cursor */
312 SetCursorPos( PosX, PosY );
316 /***********************************************************************
317 * SendInput (USER32.@)
319 UINT WINAPI SendInput( UINT count, LPINPUT inputs, int size )
321 UINT i;
323 if (!InputEnabled) return 0;
325 for (i = 0; i < count; i++, inputs++)
327 switch(inputs->type)
329 case INPUT_MOUSE:
330 queue_mouse_event( &inputs->u.mi, LLMHF_INJECTED );
331 break;
332 case WINE_INTERNAL_INPUT_MOUSE:
333 queue_mouse_event( &inputs->u.mi, 0 );
334 break;
335 case INPUT_KEYBOARD:
336 queue_kbd_event( &inputs->u.ki, LLKHF_INJECTED );
337 break;
338 case WINE_INTERNAL_INPUT_KEYBOARD:
339 queue_kbd_event( &inputs->u.ki, 0 );
340 break;
341 case INPUT_HARDWARE:
342 FIXME( "INPUT_HARDWARE not supported\n" );
343 break;
346 return count;
350 /***********************************************************************
351 * keybd_event (USER32.@)
353 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
354 DWORD dwFlags, ULONG_PTR dwExtraInfo )
356 INPUT input;
358 input.type = INPUT_KEYBOARD;
359 input.u.ki.wVk = bVk;
360 input.u.ki.wScan = bScan;
361 input.u.ki.dwFlags = dwFlags;
362 input.u.ki.time = GetTickCount();
363 input.u.ki.dwExtraInfo = dwExtraInfo;
364 SendInput( 1, &input, sizeof(input) );
368 /***********************************************************************
369 * keybd_event (USER.289)
371 void WINAPI keybd_event16( CONTEXT86 *context )
373 DWORD dwFlags = 0;
375 if (HIBYTE(context->Eax) & 0x80) dwFlags |= KEYEVENTF_KEYUP;
376 if (HIBYTE(context->Ebx) & 0x01) dwFlags |= KEYEVENTF_EXTENDEDKEY;
378 keybd_event( LOBYTE(context->Eax), LOBYTE(context->Ebx),
379 dwFlags, MAKELONG(LOWORD(context->Esi), LOWORD(context->Edi)) );
383 /***********************************************************************
384 * mouse_event (USER32.@)
386 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
387 DWORD dwData, ULONG_PTR dwExtraInfo )
389 INPUT input;
391 input.type = INPUT_MOUSE;
392 input.u.mi.dx = dx;
393 input.u.mi.dy = dy;
394 input.u.mi.mouseData = dwData;
395 input.u.mi.dwFlags = dwFlags;
396 input.u.mi.time = GetCurrentTime();
397 input.u.mi.dwExtraInfo = dwExtraInfo;
398 SendInput( 1, &input, sizeof(input) );
402 /***********************************************************************
403 * mouse_event (USER.299)
405 void WINAPI mouse_event16( CONTEXT86 *context )
407 mouse_event( LOWORD(context->Eax), LOWORD(context->Ebx), LOWORD(context->Ecx),
408 LOWORD(context->Edx), MAKELONG(context->Esi, context->Edi) );
411 /***********************************************************************
412 * GetMouseEventProc (USER.337)
414 FARPROC16 WINAPI GetMouseEventProc16(void)
416 HMODULE16 hmodule = GetModuleHandle16("USER");
417 return GetProcAddress16( hmodule, "mouse_event" );
421 /**********************************************************************
422 * EnableHardwareInput (USER.331)
424 BOOL16 WINAPI EnableHardwareInput16(BOOL16 bEnable)
426 BOOL16 bOldState = InputEnabled;
427 FIXME_(event)("(%d) - stub\n", bEnable);
428 InputEnabled = bEnable;
429 return bOldState;
433 /***********************************************************************
434 * SwapMouseButton (USER.186)
436 BOOL16 WINAPI SwapMouseButton16( BOOL16 fSwap )
438 BOOL16 ret = SwappedButtons;
439 SwappedButtons = fSwap;
440 return ret;
444 /***********************************************************************
445 * SwapMouseButton (USER32.@)
447 BOOL WINAPI SwapMouseButton( BOOL fSwap )
449 BOOL ret = SwappedButtons;
450 SwappedButtons = fSwap;
451 return ret;
455 /***********************************************************************
456 * GetCursorPos (USER.17)
458 BOOL16 WINAPI GetCursorPos16( POINT16 *pt )
460 POINT pos;
461 if (!pt) return 0;
462 GetCursorPos(&pos);
463 pt->x = pos.x;
464 pt->y = pos.y;
465 return 1;
469 /***********************************************************************
470 * GetCursorPos (USER32.@)
472 BOOL WINAPI GetCursorPos( POINT *pt )
474 if (!pt) return 0;
475 pt->x = PosX;
476 pt->y = PosY;
477 if (USER_Driver.pGetCursorPos) USER_Driver.pGetCursorPos( pt );
478 return 1;
482 /***********************************************************************
483 * GetCursorInfo (USER32.@)
485 BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
487 MESSAGEQUEUE *queue = QUEUE_Current();
489 if (!pci) return 0;
490 if (queue->cursor_count >= 0) pci->flags = CURSOR_SHOWING;
491 else pci->flags = 0;
492 GetCursorPos(&pci->ptScreenPos);
493 return 1;
497 /***********************************************************************
498 * SetCursorPos (USER.70)
500 void WINAPI SetCursorPos16( INT16 x, INT16 y )
502 SetCursorPos( x, y );
506 /***********************************************************************
507 * SetCursorPos (USER32.@)
509 BOOL WINAPI SetCursorPos( INT x, INT y )
511 if (USER_Driver.pSetCursorPos) USER_Driver.pSetCursorPos( x, y );
512 PosX = x;
513 PosY = y;
514 return TRUE;
518 /**********************************************************************
519 * SetCapture (USER32.@)
521 HWND WINAPI SetCapture( HWND hwnd )
523 HWND previous = 0;
525 SERVER_START_REQ( set_capture_window )
527 req->handle = hwnd;
528 req->flags = 0;
529 if (!wine_server_call_err( req ))
531 previous = reply->previous;
532 hwnd = reply->full_handle;
535 SERVER_END_REQ;
537 if (previous && previous != hwnd)
538 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
539 return previous;
543 /**********************************************************************
544 * ReleaseCapture (USER32.@)
546 BOOL WINAPI ReleaseCapture(void)
548 return (SetCapture(0) != 0);
552 /**********************************************************************
553 * GetCapture (USER32.@)
555 HWND WINAPI GetCapture(void)
557 HWND ret = 0;
559 SERVER_START_REQ( get_thread_input )
561 req->tid = GetCurrentThreadId();
562 if (!wine_server_call_err( req )) ret = reply->capture;
564 SERVER_END_REQ;
565 return ret;
569 /**********************************************************************
570 * GetAsyncKeyState (USER32.@)
572 * Determine if a key is or was pressed. retval has high-order
573 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
574 * been pressed.
576 * This uses the variable AsyncMouseButtonsStates and
577 * AsyncKeyStateTable (set in event.c) which have the mouse button
578 * number or key number (whichever is applicable) set to true if the
579 * mouse or key had been depressed since the last call to
580 * GetAsyncKeyState.
582 SHORT WINAPI GetAsyncKeyState(INT nKey)
584 SHORT retval = ((AsyncKeyStateTable[nKey] & 0x80) ? 0x0001 : 0) |
585 ((InputKeyStateTable[nKey] & 0x80) ? 0x8000 : 0);
586 AsyncKeyStateTable[nKey] = 0;
587 TRACE_(key)("(%x) -> %x\n", nKey, retval);
588 return retval;
591 /**********************************************************************
592 * GetAsyncKeyState (USER.249)
594 INT16 WINAPI GetAsyncKeyState16(INT16 nKey)
596 return GetAsyncKeyState(nKey);
599 /***********************************************************************
600 * IsUserIdle (USER.333)
602 BOOL16 WINAPI IsUserIdle16(void)
604 if ( GetAsyncKeyState( VK_LBUTTON ) & 0x8000 )
605 return FALSE;
607 if ( GetAsyncKeyState( VK_RBUTTON ) & 0x8000 )
608 return FALSE;
610 if ( GetAsyncKeyState( VK_MBUTTON ) & 0x8000 )
611 return FALSE;
613 /* Should check for screen saver activation here ... */
615 return TRUE;
618 /**********************************************************************
619 * VkKeyScanA (USER32.@)
621 * VkKeyScan translates an ANSI character to a virtual-key and shift code
622 * for the current keyboard.
623 * high-order byte yields :
624 * 0 Unshifted
625 * 1 Shift
626 * 2 Ctrl
627 * 3-5 Shift-key combinations that are not used for characters
628 * 6 Ctrl-Alt
629 * 7 Ctrl-Alt-Shift
630 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
631 * FIXME : works ok except for dead chars :
632 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
633 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
635 WORD WINAPI VkKeyScanA(CHAR cChar)
637 return USER_Driver.pVkKeyScan( cChar );
640 /******************************************************************************
641 * VkKeyScanW (USER32.@)
643 WORD WINAPI VkKeyScanW(WCHAR cChar)
645 return VkKeyScanA((CHAR)cChar); /* FIXME: check unicode */
648 /**********************************************************************
649 * VkKeyScanExA (USER32.@)
651 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
653 /* FIXME: complete workaround this is */
654 return VkKeyScanA(cChar);
657 /******************************************************************************
658 * VkKeyScanExW (USER32.@)
660 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
662 /* FIXME: complete workaround this is */
663 return VkKeyScanA((CHAR)cChar); /* FIXME: check unicode */
666 /******************************************************************************
667 * GetKeyboardType (USER32.@)
669 INT WINAPI GetKeyboardType(INT nTypeFlag)
671 TRACE_(keyboard)("(%d)\n", nTypeFlag);
672 switch(nTypeFlag)
674 case 0: /* Keyboard type */
675 return 4; /* AT-101 */
676 case 1: /* Keyboard Subtype */
677 return 0; /* There are no defined subtypes */
678 case 2: /* Number of F-keys */
679 return 12; /* We're doing an 101 for now, so return 12 F-keys */
680 default:
681 WARN_(keyboard)("Unknown type\n");
682 return 0; /* The book says 0 here, so 0 */
686 /******************************************************************************
687 * MapVirtualKeyA (USER32.@)
689 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
691 return USER_Driver.pMapVirtualKey( code, maptype );
694 /******************************************************************************
695 * MapVirtualKeyW (USER32.@)
697 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
699 return MapVirtualKeyA(code,maptype);
702 /******************************************************************************
703 * MapVirtualKeyExA (USER32.@)
705 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
707 if (hkl)
708 FIXME_(keyboard)("(%d,%d,0x%08lx), hkl unhandled!\n",code,maptype,(DWORD)hkl);
709 return MapVirtualKeyA(code,maptype);
712 /******************************************************************************
713 * MapVirtualKeyExW (USER32.@)
715 UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
717 if (hkl)
718 FIXME_(keyboard)("(%d,%d,0x%08lx), hkl unhandled!\n",code,maptype,(DWORD)hkl);
719 return MapVirtualKeyA(code,maptype);
722 /****************************************************************************
723 * GetKBCodePage (USER32.@)
725 UINT WINAPI GetKBCodePage(void)
727 return GetOEMCP();
730 /****************************************************************************
731 * GetKeyboardLayoutName (USER.477)
733 INT16 WINAPI GetKeyboardLayoutName16(LPSTR pwszKLID)
735 return GetKeyboardLayoutNameA(pwszKLID);
738 /***********************************************************************
739 * GetKeyboardLayout (USER32.@)
741 * FIXME: - device handle for keyboard layout defaulted to
742 * the language id. This is the way Windows default works.
743 * - the thread identifier (dwLayout) is also ignored.
745 HKL WINAPI GetKeyboardLayout(DWORD dwLayout)
747 UINT layout;
748 layout = GetSystemDefaultLCID(); /* FIXME */
749 layout |= (layout<<16); /* FIXME */
750 TRACE_(keyboard)("returning %08x\n",layout);
751 return (HKL)layout;
754 /****************************************************************************
755 * GetKeyboardLayoutNameA (USER32.@)
757 INT WINAPI GetKeyboardLayoutNameA(LPSTR pwszKLID)
759 sprintf(pwszKLID, "%p",GetKeyboardLayout(0));
760 return 1;
763 /****************************************************************************
764 * GetKeyboardLayoutNameW (USER32.@)
766 INT WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
768 char buf[KL_NAMELENGTH];
769 int res = GetKeyboardLayoutNameA(buf);
770 MultiByteToWideChar( CP_ACP, 0, buf, -1, pwszKLID, KL_NAMELENGTH );
771 return res;
774 /****************************************************************************
775 * GetKeyNameTextA (USER32.@)
777 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
779 return USER_Driver.pGetKeyNameText( lParam, lpBuffer, nSize );
782 /****************************************************************************
783 * GetKeyNameTextW (USER32.@)
785 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
787 int res;
788 LPSTR buf = HeapAlloc( GetProcessHeap(), 0, nSize );
789 if(buf == NULL) return 0; /* FIXME: is this the correct failure value?*/
790 res = GetKeyNameTextA(lParam,buf,nSize);
792 if (nSize > 0 && !MultiByteToWideChar( CP_ACP, 0, buf, -1, lpBuffer, nSize ))
793 lpBuffer[nSize-1] = 0;
794 HeapFree( GetProcessHeap(), 0, buf );
795 return res;
798 /****************************************************************************
799 * ToUnicode (USER32.@)
801 INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
802 LPWSTR lpwStr, int size, UINT flags)
804 return USER_Driver.pToUnicode(virtKey, scanCode, lpKeyState, lpwStr, size, flags);
807 /****************************************************************************
808 * ToUnicodeEx (USER32.@)
810 INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
811 LPWSTR lpwStr, int size, UINT flags, HKL hkl)
813 /* FIXME: need true implementation */
814 return ToUnicode(virtKey, scanCode, lpKeyState, lpwStr, size, flags);
817 /****************************************************************************
818 * ToAscii (USER32.@)
820 INT WINAPI ToAscii( UINT virtKey,UINT scanCode,LPBYTE lpKeyState,
821 LPWORD lpChar,UINT flags )
823 WCHAR uni_chars[2];
824 INT ret, n_ret;
826 ret = ToUnicode(virtKey, scanCode, lpKeyState, uni_chars, 2, flags);
827 if(ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
828 else n_ret = ret;
829 WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
830 return ret;
833 /****************************************************************************
834 * ToAsciiEx (USER32.@)
836 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
837 LPWORD lpChar, UINT flags, HKL dwhkl )
839 /* FIXME: need true implementation */
840 return ToAscii(virtKey, scanCode, lpKeyState, lpChar, flags);
843 /**********************************************************************
844 * ActivateKeyboardLayout (USER32.@)
846 * Call ignored. WINE supports only system default keyboard layout.
848 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
850 TRACE_(keyboard)("(%p, %d)\n", hLayout, flags);
851 ERR_(keyboard)("Only default system keyboard layout supported. Call ignored.\n");
852 return 0;
856 /***********************************************************************
857 * GetKeyboardLayoutList (USER32.@)
859 * FIXME: Supports only the system default language and layout and
860 * returns only 1 value.
862 * Return number of values available if either input parm is
863 * 0, per MS documentation.
866 INT WINAPI GetKeyboardLayoutList(INT nBuff,HKL *layouts)
868 TRACE_(keyboard)("(%d,%p)\n",nBuff,layouts);
869 if (!nBuff || !layouts)
870 return 1;
871 if (layouts)
872 layouts[0] = GetKeyboardLayout(0);
873 return 1;
877 /***********************************************************************
878 * RegisterHotKey (USER32.@)
880 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk) {
881 FIXME_(keyboard)("(%p,%d,0x%08x,%d): stub\n",hwnd,id,modifiers,vk);
882 return TRUE;
885 /***********************************************************************
886 * UnregisterHotKey (USER32.@)
888 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id) {
889 FIXME_(keyboard)("(%p,%d): stub\n",hwnd,id);
890 return TRUE;
893 /***********************************************************************
894 * LoadKeyboardLayoutW (USER32.@)
895 * Call ignored. WINE supports only system default keyboard layout.
897 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
899 TRACE_(keyboard)("(%s, %d)\n", debugstr_w(pwszKLID), Flags);
900 ERR_(keyboard)("Only default system keyboard layout supported. Call ignored.\n");
901 return 0;
904 /***********************************************************************
905 * LoadKeyboardLayoutA (USER32.@)
907 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
909 HKL ret;
910 UNICODE_STRING pwszKLIDW;
912 if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
913 else pwszKLIDW.Buffer = NULL;
915 ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
916 RtlFreeUnicodeString(&pwszKLIDW);
917 return ret;
921 typedef struct __TRACKINGLIST {
922 TRACKMOUSEEVENT tme;
923 POINT pos; /* center of hover rectangle */
924 INT iHoverTime; /* elapsed time the cursor has been inside of the hover rect */
925 } _TRACKINGLIST;
927 static _TRACKINGLIST TrackingList[10];
928 static int iTrackMax = 0;
929 static UINT_PTR timer;
930 static const INT iTimerInterval = 50; /* msec for timer interval */
932 /* FIXME: need to implement WM_NCMOUSELEAVE and WM_NCMOUSEHOVER for */
933 /* TrackMouseEventProc and _TrackMouseEvent */
934 static void CALLBACK TrackMouseEventProc(HWND hwndUnused, UINT uMsg, UINT_PTR idEvent,
935 DWORD dwTime)
937 int i = 0;
938 POINT pos;
939 POINT posClient;
940 HWND hwnd;
941 INT hoverwidth = 0, hoverheight = 0;
943 GetCursorPos(&pos);
944 hwnd = WindowFromPoint(pos);
946 SystemParametersInfoA(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
947 SystemParametersInfoA(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
949 /* loop through tracking events we are processing */
950 while (i < iTrackMax) {
951 /* see if this tracking event is looking for TME_LEAVE and that the */
952 /* mouse has left the window */
953 if ((TrackingList[i].tme.dwFlags & TME_LEAVE) &&
954 (TrackingList[i].tme.hwndTrack != hwnd)) {
955 PostMessageA(TrackingList[i].tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
957 /* remove the TME_LEAVE flag */
958 TrackingList[i].tme.dwFlags ^= TME_LEAVE;
961 /* see if we are tracking hovering for this hwnd */
962 if(TrackingList[i].tme.dwFlags & TME_HOVER) {
963 /* add the timer interval to the hovering time */
964 TrackingList[i].iHoverTime+=iTimerInterval;
966 /* has the cursor moved outside the rectangle centered around pos? */
967 if((abs(pos.x - TrackingList[i].pos.x) > (hoverwidth / 2.0))
968 || (abs(pos.y - TrackingList[i].pos.y) > (hoverheight / 2.0)))
970 /* record this new position as the current position and reset */
971 /* the iHoverTime variable to 0 */
972 TrackingList[i].pos = pos;
973 TrackingList[i].iHoverTime = 0;
976 /* has the mouse hovered long enough? */
977 if(TrackingList[i].iHoverTime <= TrackingList[i].tme.dwHoverTime)
979 posClient.x = pos.x;
980 posClient.y = pos.y;
981 ScreenToClient(hwnd, &posClient);
982 PostMessageW(TrackingList[i].tme.hwndTrack, WM_MOUSEHOVER,
983 get_key_state(), MAKELPARAM( posClient.x, posClient.y ));
985 /* stop tracking mouse hover */
986 TrackingList[i].tme.dwFlags ^= TME_HOVER;
990 /* see if we are still tracking TME_HOVER or TME_LEAVE for this entry */
991 if((TrackingList[i].tme.dwFlags & TME_HOVER) ||
992 (TrackingList[i].tme.dwFlags & TME_LEAVE)) {
993 i++;
994 } else { /* remove this entry from the tracking list */
995 TrackingList[i] = TrackingList[--iTrackMax];
999 /* stop the timer if the tracking list is empty */
1000 if(iTrackMax == 0) {
1001 KillTimer(0, timer);
1002 timer = 0;
1007 /***********************************************************************
1008 * TrackMouseEvent [USER32]
1010 * Requests notification of mouse events
1012 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
1013 * to the hwnd specified in the ptme structure. After the event message
1014 * is posted to the hwnd, the entry in the queue is removed.
1016 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
1017 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
1018 * immediately and the TME_LEAVE flag being ignored.
1020 * PARAMS
1021 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
1023 * RETURNS
1024 * Success: non-zero
1025 * Failure: zero
1029 BOOL WINAPI
1030 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
1032 DWORD flags = 0;
1033 int i = 0;
1034 BOOL cancel = 0, hover = 0, leave = 0, query = 0;
1035 HWND hwnd;
1036 POINT pos;
1038 pos.x = 0;
1039 pos.y = 0;
1041 TRACE("%lx, %lx, %p, %lx\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
1043 if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
1044 WARN("wrong TRACKMOUSEEVENT size from app\n");
1045 SetLastError(ERROR_INVALID_PARAMETER); /* FIXME not sure if this is correct */
1046 return FALSE;
1049 flags = ptme->dwFlags;
1051 /* if HOVER_DEFAULT was specified replace this with the systems current value */
1052 if(ptme->dwHoverTime == HOVER_DEFAULT)
1053 SystemParametersInfoA(SPI_GETMOUSEHOVERTIME, 0, &(ptme->dwHoverTime), 0);
1055 GetCursorPos(&pos);
1056 hwnd = WindowFromPoint(pos);
1058 if ( flags & TME_CANCEL ) {
1059 flags &= ~ TME_CANCEL;
1060 cancel = 1;
1063 if ( flags & TME_HOVER ) {
1064 flags &= ~ TME_HOVER;
1065 hover = 1;
1068 if ( flags & TME_LEAVE ) {
1069 flags &= ~ TME_LEAVE;
1070 leave = 1;
1073 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
1074 if ( flags & TME_QUERY ) {
1075 flags &= ~ TME_QUERY;
1076 query = 1;
1077 i = 0;
1079 /* Find the tracking list entry with the matching hwnd */
1080 while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
1081 i++;
1084 /* hwnd found, fill in the ptme struct */
1085 if(i < iTrackMax)
1086 *ptme = TrackingList[i].tme;
1087 else
1088 ptme->dwFlags = 0;
1090 return TRUE; /* return here, TME_QUERY is retrieving information */
1093 if ( flags )
1094 FIXME("Unknown flag(s) %08lx\n", flags );
1096 if(cancel) {
1097 /* find a matching hwnd if one exists */
1098 i = 0;
1100 while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
1101 i++;
1104 if(i < iTrackMax) {
1105 TrackingList[i].tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
1107 /* if we aren't tracking on hover or leave remove this entry */
1108 if(!((TrackingList[i].tme.dwFlags & TME_HOVER) ||
1109 (TrackingList[i].tme.dwFlags & TME_LEAVE)))
1111 TrackingList[i] = TrackingList[--iTrackMax];
1113 if(iTrackMax == 0) {
1114 KillTimer(0, timer);
1115 timer = 0;
1119 } else {
1120 /* see if hwndTrack isn't the current window */
1121 if(ptme->hwndTrack != hwnd) {
1122 if(leave) {
1123 PostMessageA(ptme->hwndTrack, WM_MOUSELEAVE, 0, 0);
1125 } else {
1126 /* See if this hwnd is already being tracked and update the tracking flags */
1127 for(i = 0; i < iTrackMax; i++) {
1128 if(TrackingList[i].tme.hwndTrack == ptme->hwndTrack) {
1129 if(hover) {
1130 TrackingList[i].tme.dwFlags |= TME_HOVER;
1131 TrackingList[i].tme.dwHoverTime = ptme->dwHoverTime;
1134 if(leave)
1135 TrackingList[i].tme.dwFlags |= TME_LEAVE;
1137 /* reset iHoverTime as per winapi specs */
1138 TrackingList[i].iHoverTime = 0;
1140 return TRUE;
1144 /* if the tracking list is full return FALSE */
1145 if (iTrackMax == sizeof (TrackingList) / sizeof(*TrackingList)) {
1146 return FALSE;
1149 /* Adding new mouse event to the tracking list */
1150 TrackingList[iTrackMax].tme = *ptme;
1152 /* Initialize HoverInfo variables even if not hover tracking */
1153 TrackingList[iTrackMax].iHoverTime = 0;
1154 TrackingList[iTrackMax].pos = pos;
1156 iTrackMax++;
1158 if (!timer) {
1159 timer = SetTimer(0, 0, iTimerInterval, TrackMouseEventProc);
1164 return TRUE;