- improve WsControl error checking
[wine.git] / windows / input.c
blobbfefcb4addaa909a03c77fb320811ba20186999f
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 <stdio.h>
28 #include <ctype.h>
29 #include <assert.h>
31 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wingdi.h"
36 #include "winuser.h"
37 #include "winnls.h"
38 #include "wine/winbase16.h"
39 #include "wine/winuser16.h"
40 #include "wine/server.h"
41 #include "win.h"
42 #include "message.h"
43 #include "winternl.h"
44 #include "wine/debug.h"
45 #include "winerror.h"
47 WINE_DECLARE_DEBUG_CHANNEL(key);
48 WINE_DECLARE_DEBUG_CHANNEL(keyboard);
49 WINE_DECLARE_DEBUG_CHANNEL(win);
50 WINE_DEFAULT_DEBUG_CHANNEL(event);
52 static BOOL InputEnabled = TRUE;
53 static BOOL SwappedButtons;
55 BYTE InputKeyStateTable[256];
56 BYTE AsyncKeyStateTable[256];
58 /* Storage for the USER-maintained mouse positions */
59 static DWORD PosX, PosY;
61 typedef union
63 struct
65 unsigned long count : 16;
66 unsigned long code : 8;
67 unsigned long extended : 1;
68 unsigned long unused : 2;
69 unsigned long win_internal : 2;
70 unsigned long context : 1;
71 unsigned long previous : 1;
72 unsigned long transition : 1;
73 } lp1;
74 unsigned long lp2;
75 } KEYLP;
78 /***********************************************************************
79 * get_key_state
81 static WORD get_key_state(void)
83 WORD ret = 0;
85 if (SwappedButtons)
87 if (InputKeyStateTable[VK_RBUTTON] & 0x80) ret |= MK_LBUTTON;
88 if (InputKeyStateTable[VK_LBUTTON] & 0x80) ret |= MK_RBUTTON;
90 else
92 if (InputKeyStateTable[VK_LBUTTON] & 0x80) ret |= MK_LBUTTON;
93 if (InputKeyStateTable[VK_RBUTTON] & 0x80) ret |= MK_RBUTTON;
95 if (InputKeyStateTable[VK_MBUTTON] & 0x80) ret |= MK_MBUTTON;
96 if (InputKeyStateTable[VK_SHIFT] & 0x80) ret |= MK_SHIFT;
97 if (InputKeyStateTable[VK_CONTROL] & 0x80) ret |= MK_CONTROL;
98 if (InputKeyStateTable[VK_XBUTTON1] & 0x80) ret |= MK_XBUTTON1;
99 if (InputKeyStateTable[VK_XBUTTON2] & 0x80) ret |= MK_XBUTTON2;
100 return ret;
104 /***********************************************************************
105 * queue_hardware_message
107 * Add a message to the hardware queue.
108 * Note: the position is relative to the desktop window.
110 static void queue_hardware_message( UINT message, HWND hwnd, WPARAM wParam, LPARAM lParam,
111 int xPos, int yPos, DWORD time, ULONG_PTR extraInfo )
113 SERVER_START_REQ( send_message )
115 req->id = GetCurrentThreadId();
116 req->type = MSG_HARDWARE;
117 req->flags = 0;
118 req->win = hwnd;
119 req->msg = message;
120 req->wparam = wParam;
121 req->lparam = lParam;
122 req->x = xPos;
123 req->y = yPos;
124 req->time = time;
125 req->info = extraInfo;
126 req->timeout = -1;
127 req->callback = NULL;
128 wine_server_call( req );
130 SERVER_END_REQ;
134 /***********************************************************************
135 * queue_kbd_event
137 * Put a keyboard event into a thread queue
139 static void queue_kbd_event( const KEYBDINPUT *ki, UINT injected_flags )
141 UINT message;
142 KEYLP keylp;
143 KBDLLHOOKSTRUCT hook;
145 keylp.lp2 = 0;
146 keylp.lp1.count = 1;
147 keylp.lp1.code = ki->wScan;
148 keylp.lp1.extended = (ki->dwFlags & KEYEVENTF_EXTENDEDKEY) != 0;
149 keylp.lp1.win_internal = 0; /* this has something to do with dialogs,
150 * don't remember where I read it - AK */
151 /* it's '1' under windows, when a dialog box appears
152 * and you press one of the underlined keys - DF*/
154 if (ki->dwFlags & KEYEVENTF_KEYUP )
156 BOOL sysKey = (InputKeyStateTable[VK_MENU] & 0x80) &&
157 !(InputKeyStateTable[VK_CONTROL] & 0x80);
158 InputKeyStateTable[ki->wVk] &= ~0x80;
159 keylp.lp1.previous = 1;
160 keylp.lp1.transition = 1;
161 message = sysKey ? WM_SYSKEYUP : WM_KEYUP;
163 else
165 keylp.lp1.previous = (InputKeyStateTable[ki->wVk] & 0x80) != 0;
166 keylp.lp1.transition = 0;
167 if (!(InputKeyStateTable[ki->wVk] & 0x80)) InputKeyStateTable[ki->wVk] ^= 0x01;
168 InputKeyStateTable[ki->wVk] |= 0x80;
169 AsyncKeyStateTable[ki->wVk] |= 0x80;
171 message = (InputKeyStateTable[VK_MENU] & 0x80) && !(InputKeyStateTable[VK_CONTROL] & 0x80)
172 ? WM_SYSKEYDOWN : WM_KEYDOWN;
175 keylp.lp1.context = (InputKeyStateTable[VK_MENU] & 0x80) != 0; /* 1 if alt */
177 TRACE_(key)(" wParam=%04x, lParam=%08lx, InputKeyState=%x\n",
178 ki->wVk, keylp.lp2, InputKeyStateTable[ki->wVk] );
180 hook.vkCode = ki->wVk;
181 hook.scanCode = ki->wScan;
182 hook.flags = (keylp.lp2 >> 24) | injected_flags;
183 hook.time = ki->time;
184 hook.dwExtraInfo = ki->dwExtraInfo;
185 if (!HOOK_CallHooks( WH_KEYBOARD_LL, HC_ACTION, message, (LPARAM)&hook, TRUE ))
186 queue_hardware_message( message, 0, ki->wVk, keylp.lp2,
187 PosX, PosY, ki->time, ki->dwExtraInfo );
191 /***********************************************************************
192 * queue_raw_mouse_message
194 static void queue_raw_mouse_message( UINT message, UINT flags, INT x, INT y, const MOUSEINPUT *mi )
196 MSLLHOOKSTRUCT hook;
198 hook.pt.x = x;
199 hook.pt.y = y;
200 hook.mouseData = MAKELONG( 0, mi->mouseData );
201 hook.flags = flags;
202 hook.time = mi->time;
203 hook.dwExtraInfo = mi->dwExtraInfo;
205 if (!HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, message, (LPARAM)&hook, TRUE ))
206 queue_hardware_message( message, (HWND)mi->dwExtraInfo /*FIXME*/,
207 MAKEWPARAM( get_key_state(), mi->mouseData ),
208 0, x, y, mi->time, mi->dwExtraInfo );
212 /***********************************************************************
213 * queue_mouse_event
215 static void queue_mouse_event( const MOUSEINPUT *mi, UINT flags )
217 if (mi->dwFlags & MOUSEEVENTF_ABSOLUTE)
219 PosX = (mi->dx * GetSystemMetrics(SM_CXSCREEN)) >> 16;
220 PosY = (mi->dy * GetSystemMetrics(SM_CYSCREEN)) >> 16;
222 else if (mi->dwFlags & MOUSEEVENTF_MOVE)
224 int width = GetSystemMetrics(SM_CXSCREEN);
225 int height = GetSystemMetrics(SM_CYSCREEN);
226 long posX = (long) PosX, posY = (long) PosY;
227 int accel[3];
228 int accelMult;
230 /* dx and dy can be negative numbers for relative movements */
231 SystemParametersInfoA(SPI_GETMOUSE, 0, accel, 0);
233 accelMult = 1;
234 if (mi->dx > accel[0] && accel[2] != 0)
236 accelMult = 2;
237 if ((mi->dx > accel[1]) && (accel[2] == 2))
239 accelMult = 4;
242 posX += (long)mi->dx * accelMult;
244 accelMult = 1;
245 if (mi->dy > accel[0] && accel[2] != 0)
247 accelMult = 2;
248 if ((mi->dy > accel[1]) && (accel[2] == 2))
250 accelMult = 4;
253 posY += (long)mi->dy * accelMult;
255 /* Clip to the current screen size */
256 if (posX < 0) PosX = 0;
257 else if (posX >= width) PosX = width - 1;
258 else PosX = posX;
260 if (posY < 0) PosY = 0;
261 else if (posY >= height) PosY = height - 1;
262 else PosY = posY;
265 if (mi->dwFlags & MOUSEEVENTF_MOVE)
267 queue_raw_mouse_message( WM_MOUSEMOVE, flags, PosX, PosY, mi );
269 if (mi->dwFlags & MOUSEEVENTF_LEFTDOWN)
271 InputKeyStateTable[VK_LBUTTON] |= 0x80;
272 AsyncKeyStateTable[VK_LBUTTON] |= 0x80;
273 queue_raw_mouse_message( SwappedButtons ? WM_RBUTTONDOWN : WM_LBUTTONDOWN,
274 flags, PosX, PosY, mi );
276 if (mi->dwFlags & MOUSEEVENTF_LEFTUP)
278 InputKeyStateTable[VK_LBUTTON] &= ~0x80;
279 queue_raw_mouse_message( SwappedButtons ? WM_RBUTTONUP : WM_LBUTTONUP,
280 flags, PosX, PosY, mi );
282 if (mi->dwFlags & MOUSEEVENTF_RIGHTDOWN)
284 InputKeyStateTable[VK_RBUTTON] |= 0x80;
285 AsyncKeyStateTable[VK_RBUTTON] |= 0x80;
286 queue_raw_mouse_message( SwappedButtons ? WM_LBUTTONDOWN : WM_RBUTTONDOWN,
287 flags, PosX, PosY, mi );
289 if (mi->dwFlags & MOUSEEVENTF_RIGHTUP)
291 InputKeyStateTable[VK_RBUTTON] &= ~0x80;
292 queue_raw_mouse_message( SwappedButtons ? WM_LBUTTONUP : WM_RBUTTONUP,
293 flags, PosX, PosY, mi );
295 if (mi->dwFlags & MOUSEEVENTF_MIDDLEDOWN)
297 InputKeyStateTable[VK_MBUTTON] |= 0x80;
298 AsyncKeyStateTable[VK_MBUTTON] |= 0x80;
299 queue_raw_mouse_message( WM_MBUTTONDOWN, flags, PosX, PosY, mi );
301 if (mi->dwFlags & MOUSEEVENTF_MIDDLEUP)
303 InputKeyStateTable[VK_MBUTTON] &= ~0x80;
304 queue_raw_mouse_message( WM_MBUTTONUP, flags, PosX, PosY, mi );
306 if (mi->dwFlags & MOUSEEVENTF_WHEEL)
308 queue_raw_mouse_message( WM_MOUSEWHEEL, flags, PosX, PosY, mi );
310 if (flags & LLMHF_INJECTED) /* we have to actually move the cursor */
311 SetCursorPos( PosX, PosY );
315 /***********************************************************************
316 * SendInput (USER32.@)
318 UINT WINAPI SendInput( UINT count, LPINPUT inputs, int size )
320 UINT i;
322 if (!InputEnabled) return 0;
324 for (i = 0; i < count; i++, inputs++)
326 switch(inputs->type)
328 case INPUT_MOUSE:
329 queue_mouse_event( &inputs->u.mi, LLMHF_INJECTED );
330 break;
331 case WINE_INTERNAL_INPUT_MOUSE:
332 queue_mouse_event( &inputs->u.mi, 0 );
333 break;
334 case INPUT_KEYBOARD:
335 queue_kbd_event( &inputs->u.ki, LLKHF_INJECTED );
336 break;
337 case WINE_INTERNAL_INPUT_KEYBOARD:
338 queue_kbd_event( &inputs->u.ki, 0 );
339 break;
340 case INPUT_HARDWARE:
341 FIXME( "INPUT_HARDWARE not supported\n" );
342 break;
345 return count;
349 /***********************************************************************
350 * keybd_event (USER32.@)
352 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
353 DWORD dwFlags, ULONG_PTR dwExtraInfo )
355 INPUT input;
357 input.type = INPUT_KEYBOARD;
358 input.u.ki.wVk = bVk;
359 input.u.ki.wScan = bScan;
360 input.u.ki.dwFlags = dwFlags;
361 input.u.ki.time = GetTickCount();
362 input.u.ki.dwExtraInfo = dwExtraInfo;
363 SendInput( 1, &input, sizeof(input) );
367 /***********************************************************************
368 * keybd_event (USER.289)
370 void WINAPI keybd_event16( CONTEXT86 *context )
372 DWORD dwFlags = 0;
374 if (HIBYTE(context->Eax) & 0x80) dwFlags |= KEYEVENTF_KEYUP;
375 if (HIBYTE(context->Ebx) & 0x01) dwFlags |= KEYEVENTF_EXTENDEDKEY;
377 keybd_event( LOBYTE(context->Eax), LOBYTE(context->Ebx),
378 dwFlags, MAKELONG(LOWORD(context->Esi), LOWORD(context->Edi)) );
382 /***********************************************************************
383 * mouse_event (USER32.@)
385 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
386 DWORD dwData, ULONG_PTR dwExtraInfo )
388 INPUT input;
390 input.type = INPUT_MOUSE;
391 input.u.mi.dx = dx;
392 input.u.mi.dy = dy;
393 input.u.mi.mouseData = dwData;
394 input.u.mi.dwFlags = dwFlags;
395 input.u.mi.time = GetCurrentTime();
396 input.u.mi.dwExtraInfo = dwExtraInfo;
397 SendInput( 1, &input, sizeof(input) );
401 /***********************************************************************
402 * mouse_event (USER.299)
404 void WINAPI mouse_event16( CONTEXT86 *context )
406 mouse_event( LOWORD(context->Eax), LOWORD(context->Ebx), LOWORD(context->Ecx),
407 LOWORD(context->Edx), MAKELONG(context->Esi, context->Edi) );
410 /***********************************************************************
411 * GetMouseEventProc (USER.337)
413 FARPROC16 WINAPI GetMouseEventProc16(void)
415 HMODULE16 hmodule = GetModuleHandle16("USER");
416 return GetProcAddress16( hmodule, "mouse_event" );
420 /**********************************************************************
421 * EnableHardwareInput (USER.331)
423 BOOL16 WINAPI EnableHardwareInput16(BOOL16 bEnable)
425 BOOL16 bOldState = InputEnabled;
426 FIXME_(event)("(%d) - stub\n", bEnable);
427 InputEnabled = bEnable;
428 return bOldState;
432 /***********************************************************************
433 * SwapMouseButton (USER.186)
435 BOOL16 WINAPI SwapMouseButton16( BOOL16 fSwap )
437 BOOL16 ret = SwappedButtons;
438 SwappedButtons = fSwap;
439 return ret;
443 /***********************************************************************
444 * SwapMouseButton (USER32.@)
446 BOOL WINAPI SwapMouseButton( BOOL fSwap )
448 BOOL ret = SwappedButtons;
449 SwappedButtons = fSwap;
450 return ret;
454 /***********************************************************************
455 * GetCursorPos (USER.17)
457 BOOL16 WINAPI GetCursorPos16( POINT16 *pt )
459 POINT pos;
460 if (!pt) return 0;
461 GetCursorPos(&pos);
462 pt->x = pos.x;
463 pt->y = pos.y;
464 return 1;
468 /***********************************************************************
469 * GetCursorPos (USER32.@)
471 BOOL WINAPI GetCursorPos( POINT *pt )
473 if (!pt) return 0;
474 pt->x = PosX;
475 pt->y = PosY;
476 if (USER_Driver.pGetCursorPos) USER_Driver.pGetCursorPos( pt );
477 return 1;
481 /***********************************************************************
482 * GetCursorInfo (USER32.@)
484 BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
486 MESSAGEQUEUE *queue = QUEUE_Current();
488 if (!pci) return 0;
489 if (queue->cursor_count >= 0) pci->flags = CURSOR_SHOWING;
490 else pci->flags = 0;
491 GetCursorPos(&pci->ptScreenPos);
492 return 1;
496 /***********************************************************************
497 * SetCursorPos (USER.70)
499 void WINAPI SetCursorPos16( INT16 x, INT16 y )
501 SetCursorPos( x, y );
505 /***********************************************************************
506 * SetCursorPos (USER32.@)
508 BOOL WINAPI SetCursorPos( INT x, INT y )
510 if (USER_Driver.pSetCursorPos) USER_Driver.pSetCursorPos( x, y );
511 PosX = x;
512 PosY = y;
513 return TRUE;
517 /**********************************************************************
518 * SetCapture (USER32.@)
520 HWND WINAPI SetCapture( HWND hwnd )
522 HWND previous = 0;
524 SERVER_START_REQ( set_capture_window )
526 req->handle = hwnd;
527 req->flags = 0;
528 if (!wine_server_call_err( req ))
530 previous = reply->previous;
531 hwnd = reply->full_handle;
534 SERVER_END_REQ;
536 if (previous && previous != hwnd)
537 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
538 return previous;
542 /**********************************************************************
543 * ReleaseCapture (USER32.@)
545 BOOL WINAPI ReleaseCapture(void)
547 return (SetCapture(0) != 0);
551 /**********************************************************************
552 * GetCapture (USER32.@)
554 HWND WINAPI GetCapture(void)
556 HWND ret = 0;
558 SERVER_START_REQ( get_thread_input )
560 req->tid = GetCurrentThreadId();
561 if (!wine_server_call_err( req )) ret = reply->capture;
563 SERVER_END_REQ;
564 return ret;
568 /**********************************************************************
569 * GetAsyncKeyState (USER32.@)
571 * Determine if a key is or was pressed. retval has high-order
572 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
573 * been pressed.
575 * This uses the variable AsyncMouseButtonsStates and
576 * AsyncKeyStateTable (set in event.c) which have the mouse button
577 * number or key number (whichever is applicable) set to true if the
578 * mouse or key had been depressed since the last call to
579 * GetAsyncKeyState.
581 SHORT WINAPI GetAsyncKeyState(INT nKey)
583 SHORT retval = ((AsyncKeyStateTable[nKey] & 0x80) ? 0x0001 : 0) |
584 ((InputKeyStateTable[nKey] & 0x80) ? 0x8000 : 0);
585 AsyncKeyStateTable[nKey] = 0;
586 TRACE_(key)("(%x) -> %x\n", nKey, retval);
587 return retval;
590 /**********************************************************************
591 * GetAsyncKeyState (USER.249)
593 INT16 WINAPI GetAsyncKeyState16(INT16 nKey)
595 return GetAsyncKeyState(nKey);
598 /***********************************************************************
599 * IsUserIdle (USER.333)
601 BOOL16 WINAPI IsUserIdle16(void)
603 if ( GetAsyncKeyState( VK_LBUTTON ) & 0x8000 )
604 return FALSE;
606 if ( GetAsyncKeyState( VK_RBUTTON ) & 0x8000 )
607 return FALSE;
609 if ( GetAsyncKeyState( VK_MBUTTON ) & 0x8000 )
610 return FALSE;
612 /* Should check for screen saver activation here ... */
614 return TRUE;
617 /**********************************************************************
618 * VkKeyScanA (USER32.@)
620 * VkKeyScan translates an ANSI character to a virtual-key and shift code
621 * for the current keyboard.
622 * high-order byte yields :
623 * 0 Unshifted
624 * 1 Shift
625 * 2 Ctrl
626 * 3-5 Shift-key combinations that are not used for characters
627 * 6 Ctrl-Alt
628 * 7 Ctrl-Alt-Shift
629 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
630 * FIXME : works ok except for dead chars :
631 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
632 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
634 WORD WINAPI VkKeyScanA(CHAR cChar)
636 return USER_Driver.pVkKeyScan( cChar );
639 /******************************************************************************
640 * VkKeyScanW (USER32.@)
642 WORD WINAPI VkKeyScanW(WCHAR cChar)
644 return VkKeyScanA((CHAR)cChar); /* FIXME: check unicode */
647 /**********************************************************************
648 * VkKeyScanExA (USER32.@)
650 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
652 /* FIXME: complete workaround this is */
653 return VkKeyScanA(cChar);
656 /******************************************************************************
657 * VkKeyScanExW (USER32.@)
659 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
661 /* FIXME: complete workaround this is */
662 return VkKeyScanA((CHAR)cChar); /* FIXME: check unicode */
665 /******************************************************************************
666 * GetKeyboardType (USER32.@)
668 INT WINAPI GetKeyboardType(INT nTypeFlag)
670 TRACE_(keyboard)("(%d)\n", nTypeFlag);
671 switch(nTypeFlag)
673 case 0: /* Keyboard type */
674 return 4; /* AT-101 */
675 case 1: /* Keyboard Subtype */
676 return 0; /* There are no defined subtypes */
677 case 2: /* Number of F-keys */
678 return 12; /* We're doing an 101 for now, so return 12 F-keys */
679 default:
680 WARN_(keyboard)("Unknown type\n");
681 return 0; /* The book says 0 here, so 0 */
685 /******************************************************************************
686 * MapVirtualKeyA (USER32.@)
688 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
690 return USER_Driver.pMapVirtualKey( code, maptype );
693 /******************************************************************************
694 * MapVirtualKeyW (USER32.@)
696 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
698 return MapVirtualKeyA(code,maptype);
701 /******************************************************************************
702 * MapVirtualKeyExA (USER32.@)
704 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
706 if (hkl)
707 FIXME_(keyboard)("(%d,%d,0x%08lx), hkl unhandled!\n",code,maptype,(DWORD)hkl);
708 return MapVirtualKeyA(code,maptype);
711 /******************************************************************************
712 * MapVirtualKeyExW (USER32.@)
714 UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
716 if (hkl)
717 FIXME_(keyboard)("(%d,%d,0x%08lx), hkl unhandled!\n",code,maptype,(DWORD)hkl);
718 return MapVirtualKeyA(code,maptype);
721 /****************************************************************************
722 * GetKBCodePage (USER32.@)
724 UINT WINAPI GetKBCodePage(void)
726 return GetOEMCP();
729 /****************************************************************************
730 * GetKeyboardLayoutName (USER.477)
732 INT16 WINAPI GetKeyboardLayoutName16(LPSTR pwszKLID)
734 return GetKeyboardLayoutNameA(pwszKLID);
737 /***********************************************************************
738 * GetKeyboardLayout (USER32.@)
740 * FIXME: - device handle for keyboard layout defaulted to
741 * the language id. This is the way Windows default works.
742 * - the thread identifier (dwLayout) is also ignored.
744 HKL WINAPI GetKeyboardLayout(DWORD dwLayout)
746 UINT layout;
747 layout = GetSystemDefaultLCID(); /* FIXME */
748 layout |= (layout<<16); /* FIXME */
749 TRACE_(keyboard)("returning %08x\n",layout);
750 return (HKL)layout;
753 /****************************************************************************
754 * GetKeyboardLayoutNameA (USER32.@)
756 INT WINAPI GetKeyboardLayoutNameA(LPSTR pwszKLID)
758 sprintf(pwszKLID, "%p",GetKeyboardLayout(0));
759 return 1;
762 /****************************************************************************
763 * GetKeyboardLayoutNameW (USER32.@)
765 INT WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
767 char buf[KL_NAMELENGTH];
768 int res = GetKeyboardLayoutNameA(buf);
769 MultiByteToWideChar( CP_ACP, 0, buf, -1, pwszKLID, KL_NAMELENGTH );
770 return res;
773 /****************************************************************************
774 * GetKeyNameTextA (USER32.@)
776 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
778 return USER_Driver.pGetKeyNameText( lParam, lpBuffer, nSize );
781 /****************************************************************************
782 * GetKeyNameTextW (USER32.@)
784 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
786 int res;
787 LPSTR buf = HeapAlloc( GetProcessHeap(), 0, nSize );
788 if(buf == NULL) return 0; /* FIXME: is this the correct failure value?*/
789 res = GetKeyNameTextA(lParam,buf,nSize);
791 if (nSize > 0 && !MultiByteToWideChar( CP_ACP, 0, buf, -1, lpBuffer, nSize ))
792 lpBuffer[nSize-1] = 0;
793 HeapFree( GetProcessHeap(), 0, buf );
794 return res;
797 /****************************************************************************
798 * ToUnicode (USER32.@)
800 INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
801 LPWSTR lpwStr, int size, UINT flags)
803 return USER_Driver.pToUnicode(virtKey, scanCode, lpKeyState, lpwStr, size, flags);
806 /****************************************************************************
807 * ToUnicodeEx (USER32.@)
809 INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
810 LPWSTR lpwStr, int size, UINT flags, HKL hkl)
812 /* FIXME: need true implementation */
813 return ToUnicode(virtKey, scanCode, lpKeyState, lpwStr, size, flags);
816 /****************************************************************************
817 * ToAscii (USER32.@)
819 INT WINAPI ToAscii( UINT virtKey,UINT scanCode,LPBYTE lpKeyState,
820 LPWORD lpChar,UINT flags )
822 WCHAR uni_chars[2];
823 INT ret, n_ret;
825 ret = ToUnicode(virtKey, scanCode, lpKeyState, uni_chars, 2, flags);
826 if(ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
827 else n_ret = ret;
828 WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
829 return ret;
832 /****************************************************************************
833 * ToAsciiEx (USER32.@)
835 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
836 LPWORD lpChar, UINT flags, HKL dwhkl )
838 /* FIXME: need true implementation */
839 return ToAscii(virtKey, scanCode, lpKeyState, lpChar, flags);
842 /**********************************************************************
843 * ActivateKeyboardLayout (USER32.@)
845 * Call ignored. WINE supports only system default keyboard layout.
847 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
849 TRACE_(keyboard)("(%p, %d)\n", hLayout, flags);
850 ERR_(keyboard)("Only default system keyboard layout supported. Call ignored.\n");
851 return 0;
855 /***********************************************************************
856 * GetKeyboardLayoutList (USER32.@)
858 * FIXME: Supports only the system default language and layout and
859 * returns only 1 value.
861 * Return number of values available if either input parm is
862 * 0, per MS documentation.
865 INT WINAPI GetKeyboardLayoutList(INT nBuff,HKL *layouts)
867 TRACE_(keyboard)("(%d,%p)\n",nBuff,layouts);
868 if (!nBuff || !layouts)
869 return 1;
870 if (layouts)
871 layouts[0] = GetKeyboardLayout(0);
872 return 1;
876 /***********************************************************************
877 * RegisterHotKey (USER32.@)
879 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk) {
880 FIXME_(keyboard)("(%p,%d,0x%08x,%d): stub\n",hwnd,id,modifiers,vk);
881 return TRUE;
884 /***********************************************************************
885 * UnregisterHotKey (USER32.@)
887 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id) {
888 FIXME_(keyboard)("(%p,%d): stub\n",hwnd,id);
889 return TRUE;
892 /***********************************************************************
893 * LoadKeyboardLayoutW (USER32.@)
894 * Call ignored. WINE supports only system default keyboard layout.
896 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
898 TRACE_(keyboard)("(%s, %d)\n", debugstr_w(pwszKLID), Flags);
899 ERR_(keyboard)("Only default system keyboard layout supported. Call ignored.\n");
900 return 0;
903 /***********************************************************************
904 * LoadKeyboardLayoutA (USER32.@)
906 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
908 HKL ret;
909 UNICODE_STRING pwszKLIDW;
911 if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
912 else pwszKLIDW.Buffer = NULL;
914 ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
915 RtlFreeUnicodeString(&pwszKLIDW);
916 return ret;
920 typedef struct __TRACKINGLIST {
921 TRACKMOUSEEVENT tme;
922 POINT pos; /* center of hover rectangle */
923 INT iHoverTime; /* elapsed time the cursor has been inside of the hover rect */
924 } _TRACKINGLIST;
926 static _TRACKINGLIST TrackingList[10];
927 static int iTrackMax = 0;
928 static UINT_PTR timer;
929 static const INT iTimerInterval = 50; /* msec for timer interval */
931 /* FIXME: need to implement WM_NCMOUSELEAVE and WM_NCMOUSEHOVER for */
932 /* TrackMouseEventProc and _TrackMouseEvent */
933 static void CALLBACK TrackMouseEventProc(HWND hwndUnused, UINT uMsg, UINT_PTR idEvent,
934 DWORD dwTime)
936 int i = 0;
937 POINT pos;
938 POINT posClient;
939 HWND hwnd;
940 INT hoverwidth = 0, hoverheight = 0;
942 GetCursorPos(&pos);
943 hwnd = WindowFromPoint(pos);
945 SystemParametersInfoA(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
946 SystemParametersInfoA(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
948 /* loop through tracking events we are processing */
949 while (i < iTrackMax) {
950 /* see if this tracking event is looking for TME_LEAVE and that the */
951 /* mouse has left the window */
952 if ((TrackingList[i].tme.dwFlags & TME_LEAVE) &&
953 (TrackingList[i].tme.hwndTrack != hwnd)) {
954 PostMessageA(TrackingList[i].tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
956 /* remove the TME_LEAVE flag */
957 TrackingList[i].tme.dwFlags ^= TME_LEAVE;
960 /* see if we are tracking hovering for this hwnd */
961 if(TrackingList[i].tme.dwFlags & TME_HOVER) {
962 /* add the timer interval to the hovering time */
963 TrackingList[i].iHoverTime+=iTimerInterval;
965 /* has the cursor moved outside the rectangle centered around pos? */
966 if((abs(pos.x - TrackingList[i].pos.x) > (hoverwidth / 2.0))
967 || (abs(pos.y - TrackingList[i].pos.y) > (hoverheight / 2.0)))
969 /* record this new position as the current position and reset */
970 /* the iHoverTime variable to 0 */
971 TrackingList[i].pos = pos;
972 TrackingList[i].iHoverTime = 0;
975 /* has the mouse hovered long enough? */
976 if(TrackingList[i].iHoverTime <= TrackingList[i].tme.dwHoverTime)
978 posClient.x = pos.x;
979 posClient.y = pos.y;
980 ScreenToClient(hwnd, &posClient);
981 PostMessageW(TrackingList[i].tme.hwndTrack, WM_MOUSEHOVER,
982 get_key_state(), MAKELPARAM( posClient.x, posClient.y ));
984 /* stop tracking mouse hover */
985 TrackingList[i].tme.dwFlags ^= TME_HOVER;
989 /* see if we are still tracking TME_HOVER or TME_LEAVE for this entry */
990 if((TrackingList[i].tme.dwFlags & TME_HOVER) ||
991 (TrackingList[i].tme.dwFlags & TME_LEAVE)) {
992 i++;
993 } else { /* remove this entry from the tracking list */
994 TrackingList[i] = TrackingList[--iTrackMax];
998 /* stop the timer if the tracking list is empty */
999 if(iTrackMax == 0) {
1000 KillTimer(0, timer);
1001 timer = 0;
1006 /***********************************************************************
1007 * TrackMouseEvent [USER32]
1009 * Requests notification of mouse events
1011 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
1012 * to the hwnd specified in the ptme structure. After the event message
1013 * is posted to the hwnd, the entry in the queue is removed.
1015 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
1016 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
1017 * immediately and the TME_LEAVE flag being ignored.
1019 * PARAMS
1020 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
1022 * RETURNS
1023 * Success: non-zero
1024 * Failure: zero
1028 BOOL WINAPI
1029 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
1031 DWORD flags = 0;
1032 int i = 0;
1033 BOOL cancel = 0, hover = 0, leave = 0, query = 0;
1034 HWND hwnd;
1035 POINT pos;
1037 pos.x = 0;
1038 pos.y = 0;
1040 TRACE("%lx, %lx, %p, %lx\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
1042 if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
1043 WARN("wrong TRACKMOUSEEVENT size from app\n");
1044 SetLastError(ERROR_INVALID_PARAMETER); /* FIXME not sure if this is correct */
1045 return FALSE;
1048 flags = ptme->dwFlags;
1050 /* if HOVER_DEFAULT was specified replace this with the systems current value */
1051 if(ptme->dwHoverTime == HOVER_DEFAULT)
1052 SystemParametersInfoA(SPI_GETMOUSEHOVERTIME, 0, &(ptme->dwHoverTime), 0);
1054 GetCursorPos(&pos);
1055 hwnd = WindowFromPoint(pos);
1057 if ( flags & TME_CANCEL ) {
1058 flags &= ~ TME_CANCEL;
1059 cancel = 1;
1062 if ( flags & TME_HOVER ) {
1063 flags &= ~ TME_HOVER;
1064 hover = 1;
1067 if ( flags & TME_LEAVE ) {
1068 flags &= ~ TME_LEAVE;
1069 leave = 1;
1072 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
1073 if ( flags & TME_QUERY ) {
1074 flags &= ~ TME_QUERY;
1075 query = 1;
1076 i = 0;
1078 /* Find the tracking list entry with the matching hwnd */
1079 while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
1080 i++;
1083 /* hwnd found, fill in the ptme struct */
1084 if(i < iTrackMax)
1085 *ptme = TrackingList[i].tme;
1086 else
1087 ptme->dwFlags = 0;
1089 return TRUE; /* return here, TME_QUERY is retrieving information */
1092 if ( flags )
1093 FIXME("Unknown flag(s) %08lx\n", flags );
1095 if(cancel) {
1096 /* find a matching hwnd if one exists */
1097 i = 0;
1099 while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
1100 i++;
1103 if(i < iTrackMax) {
1104 TrackingList[i].tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
1106 /* if we aren't tracking on hover or leave remove this entry */
1107 if(!((TrackingList[i].tme.dwFlags & TME_HOVER) ||
1108 (TrackingList[i].tme.dwFlags & TME_LEAVE)))
1110 TrackingList[i] = TrackingList[--iTrackMax];
1112 if(iTrackMax == 0) {
1113 KillTimer(0, timer);
1114 timer = 0;
1118 } else {
1119 /* see if hwndTrack isn't the current window */
1120 if(ptme->hwndTrack != hwnd) {
1121 if(leave) {
1122 PostMessageA(ptme->hwndTrack, WM_MOUSELEAVE, 0, 0);
1124 } else {
1125 /* See if this hwnd is already being tracked and update the tracking flags */
1126 for(i = 0; i < iTrackMax; i++) {
1127 if(TrackingList[i].tme.hwndTrack == ptme->hwndTrack) {
1128 if(hover) {
1129 TrackingList[i].tme.dwFlags |= TME_HOVER;
1130 TrackingList[i].tme.dwHoverTime = ptme->dwHoverTime;
1133 if(leave)
1134 TrackingList[i].tme.dwFlags |= TME_LEAVE;
1136 /* reset iHoverTime as per winapi specs */
1137 TrackingList[i].iHoverTime = 0;
1139 return TRUE;
1143 /* if the tracking list is full return FALSE */
1144 if (iTrackMax == sizeof (TrackingList) / sizeof(*TrackingList)) {
1145 return FALSE;
1148 /* Adding new mouse event to the tracking list */
1149 TrackingList[iTrackMax].tme = *ptme;
1151 /* Initialize HoverInfo variables even if not hover tracking */
1152 TrackingList[iTrackMax].iHoverTime = 0;
1153 TrackingList[iTrackMax].pos = pos;
1155 iTrackMax++;
1157 if (!timer) {
1158 timer = SetTimer(0, 0, iTimerInterval, TrackMouseEventProc);
1163 return TRUE;