Fix some DPA functions so they pass the new tests.
[wine/multimedia.git] / dlls / user / input.c
blobff6401ec912969de111408adcab64daf090bc039
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 "config.h"
26 #include "wine/port.h"
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <assert.h>
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41 #include "winnls.h"
42 #include "wine/server.h"
43 #include "user_private.h"
44 #include "winternl.h"
45 #include "wine/debug.h"
46 #include "winerror.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(key);
49 WINE_DECLARE_DEBUG_CHANNEL(keyboard);
52 /***********************************************************************
53 * get_key_state
55 static WORD get_key_state(void)
57 WORD ret = 0;
59 if (GetSystemMetrics( SM_SWAPBUTTON ))
61 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_LBUTTON;
62 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_RBUTTON;
64 else
66 if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_LBUTTON;
67 if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_RBUTTON;
69 if (GetAsyncKeyState(VK_MBUTTON) & 0x80) ret |= MK_MBUTTON;
70 if (GetAsyncKeyState(VK_SHIFT) & 0x80) ret |= MK_SHIFT;
71 if (GetAsyncKeyState(VK_CONTROL) & 0x80) ret |= MK_CONTROL;
72 if (GetAsyncKeyState(VK_XBUTTON1) & 0x80) ret |= MK_XBUTTON1;
73 if (GetAsyncKeyState(VK_XBUTTON2) & 0x80) ret |= MK_XBUTTON2;
74 return ret;
78 /***********************************************************************
79 * SendInput (USER32.@)
81 UINT WINAPI SendInput( UINT count, LPINPUT inputs, int size )
83 if (USER_Driver.pSendInput) return USER_Driver.pSendInput( count, inputs, size );
84 return 0;
88 /***********************************************************************
89 * keybd_event (USER32.@)
91 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
92 DWORD dwFlags, ULONG_PTR dwExtraInfo )
94 INPUT input;
96 input.type = INPUT_KEYBOARD;
97 input.u.ki.wVk = bVk;
98 input.u.ki.wScan = bScan;
99 input.u.ki.dwFlags = dwFlags;
100 input.u.ki.time = GetTickCount();
101 input.u.ki.dwExtraInfo = dwExtraInfo;
102 SendInput( 1, &input, sizeof(input) );
106 /***********************************************************************
107 * mouse_event (USER32.@)
109 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
110 DWORD dwData, ULONG_PTR dwExtraInfo )
112 INPUT input;
114 input.type = INPUT_MOUSE;
115 input.u.mi.dx = dx;
116 input.u.mi.dy = dy;
117 input.u.mi.mouseData = dwData;
118 input.u.mi.dwFlags = dwFlags;
119 input.u.mi.time = GetCurrentTime();
120 input.u.mi.dwExtraInfo = dwExtraInfo;
121 SendInput( 1, &input, sizeof(input) );
125 /***********************************************************************
126 * GetCursorPos (USER32.@)
128 BOOL WINAPI GetCursorPos( POINT *pt )
130 if (pt && USER_Driver.pGetCursorPos) return USER_Driver.pGetCursorPos( pt );
131 return FALSE;
135 /***********************************************************************
136 * GetCursorInfo (USER32.@)
138 BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
140 if (!pci) return 0;
141 if (get_user_thread_info()->cursor_count >= 0) pci->flags = CURSOR_SHOWING;
142 else pci->flags = 0;
143 GetCursorPos(&pci->ptScreenPos);
144 return 1;
148 /***********************************************************************
149 * SetCursorPos (USER32.@)
151 BOOL WINAPI SetCursorPos( INT x, INT y )
153 if (USER_Driver.pSetCursorPos) return USER_Driver.pSetCursorPos( x, y );
154 return FALSE;
158 /**********************************************************************
159 * SetCapture (USER32.@)
161 HWND WINAPI SetCapture( HWND hwnd )
163 HWND previous = 0;
165 SERVER_START_REQ( set_capture_window )
167 req->handle = hwnd;
168 req->flags = 0;
169 if (!wine_server_call_err( req ))
171 previous = reply->previous;
172 hwnd = reply->full_handle;
175 SERVER_END_REQ;
177 if (previous && previous != hwnd)
178 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
179 return previous;
183 /**********************************************************************
184 * ReleaseCapture (USER32.@)
186 BOOL WINAPI ReleaseCapture(void)
188 return (SetCapture(0) != 0);
192 /**********************************************************************
193 * GetCapture (USER32.@)
195 HWND WINAPI GetCapture(void)
197 HWND ret = 0;
199 SERVER_START_REQ( get_thread_input )
201 req->tid = GetCurrentThreadId();
202 if (!wine_server_call_err( req )) ret = reply->capture;
204 SERVER_END_REQ;
205 return ret;
209 /**********************************************************************
210 * GetAsyncKeyState (USER32.@)
212 * Determine if a key is or was pressed. retval has high-order
213 * bit set to 1 if currently pressed, low-order bit set to 1 if key has
214 * been pressed.
216 SHORT WINAPI GetAsyncKeyState(INT nKey)
218 if (USER_Driver.pGetAsyncKeyState) return USER_Driver.pGetAsyncKeyState( nKey );
219 return 0;
223 /***********************************************************************
224 * GetQueueStatus (USER32.@)
226 DWORD WINAPI GetQueueStatus( UINT flags )
228 DWORD ret = 0;
230 /* check for pending X events */
231 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
232 USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
234 SERVER_START_REQ( get_queue_status )
236 req->clear = 1;
237 wine_server_call( req );
238 ret = MAKELONG( reply->changed_bits & flags, reply->wake_bits & flags );
240 SERVER_END_REQ;
241 return ret;
245 /***********************************************************************
246 * GetInputState (USER32.@)
248 BOOL WINAPI GetInputState(void)
250 DWORD ret = 0;
252 /* check for pending X events */
253 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
254 USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_INPUT, 0 );
256 SERVER_START_REQ( get_queue_status )
258 req->clear = 0;
259 wine_server_call( req );
260 ret = reply->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
262 SERVER_END_REQ;
263 return ret;
267 /******************************************************************
268 * GetLastInputInfo (USER32.@)
270 BOOL WINAPI GetLastInputInfo(PLASTINPUTINFO plii)
272 BOOL ret;
274 TRACE("%p\n", plii);
276 if (plii->cbSize != sizeof (*plii) )
278 SetLastError(ERROR_INVALID_PARAMETER);
279 return FALSE;
282 SERVER_START_REQ( get_last_input_time )
284 ret = !wine_server_call_err( req );
285 if (ret)
286 plii->dwTime = reply->time;
288 SERVER_END_REQ;
289 return ret;
293 /**********************************************************************
294 * AttachThreadInput (USER32.@)
296 * Attaches the input processing mechanism of one thread to that of
297 * another thread.
299 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
301 BOOL ret;
303 SERVER_START_REQ( attach_thread_input )
305 req->tid_from = from;
306 req->tid_to = to;
307 req->attach = attach;
308 ret = !wine_server_call_err( req );
310 SERVER_END_REQ;
311 return ret;
315 /**********************************************************************
316 * GetKeyState (USER32.@)
318 * An application calls the GetKeyState function in response to a
319 * keyboard-input message. This function retrieves the state of the key
320 * at the time the input message was generated.
322 SHORT WINAPI GetKeyState(INT vkey)
324 SHORT retval = 0;
326 SERVER_START_REQ( get_key_state )
328 req->tid = GetCurrentThreadId();
329 req->key = vkey;
330 if (!wine_server_call( req )) retval = (signed char)reply->state;
332 SERVER_END_REQ;
333 TRACE("key (0x%x) -> %x\n", vkey, retval);
334 return retval;
338 /**********************************************************************
339 * GetKeyboardState (USER32.@)
341 BOOL WINAPI GetKeyboardState( LPBYTE state )
343 BOOL ret;
345 TRACE("(%p)\n", state);
347 memset( state, 0, 256 );
348 SERVER_START_REQ( get_key_state )
350 req->tid = GetCurrentThreadId();
351 req->key = -1;
352 wine_server_set_reply( req, state, 256 );
353 ret = !wine_server_call_err( req );
355 SERVER_END_REQ;
356 return ret;
360 /**********************************************************************
361 * SetKeyboardState (USER32.@)
363 BOOL WINAPI SetKeyboardState( LPBYTE state )
365 BOOL ret;
367 TRACE("(%p)\n", state);
369 SERVER_START_REQ( set_key_state )
371 req->tid = GetCurrentThreadId();
372 wine_server_add_data( req, state, 256 );
373 ret = !wine_server_call_err( req );
375 SERVER_END_REQ;
376 return ret;
380 /**********************************************************************
381 * VkKeyScanA (USER32.@)
383 * VkKeyScan translates an ANSI character to a virtual-key and shift code
384 * for the current keyboard.
385 * high-order byte yields :
386 * 0 Unshifted
387 * 1 Shift
388 * 2 Ctrl
389 * 3-5 Shift-key combinations that are not used for characters
390 * 6 Ctrl-Alt
391 * 7 Ctrl-Alt-Shift
392 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
393 * FIXME : works ok except for dead chars :
394 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
395 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
397 SHORT WINAPI VkKeyScanA(CHAR cChar)
399 WCHAR wChar;
401 if (IsDBCSLeadByte(cChar)) return -1;
403 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
404 return VkKeyScanW(wChar);
407 /******************************************************************************
408 * VkKeyScanW (USER32.@)
410 SHORT WINAPI VkKeyScanW(WCHAR cChar)
412 return VkKeyScanExW(cChar, GetKeyboardLayout(0));
415 /**********************************************************************
416 * VkKeyScanExA (USER32.@)
418 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
420 WCHAR wChar;
422 if (IsDBCSLeadByte(cChar)) return -1;
424 MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
425 return VkKeyScanExW(wChar, dwhkl);
428 /******************************************************************************
429 * VkKeyScanExW (USER32.@)
431 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
433 if (USER_Driver.pVkKeyScanEx)
434 return USER_Driver.pVkKeyScanEx(cChar, dwhkl);
435 return -1;
438 /**********************************************************************
439 * OemKeyScan (USER32.@)
441 DWORD WINAPI OemKeyScan(WORD wOemChar)
443 TRACE("(%d)\n", wOemChar);
444 return wOemChar;
447 /******************************************************************************
448 * GetKeyboardType (USER32.@)
450 INT WINAPI GetKeyboardType(INT nTypeFlag)
452 TRACE_(keyboard)("(%d)\n", nTypeFlag);
453 switch(nTypeFlag)
455 case 0: /* Keyboard type */
456 return 4; /* AT-101 */
457 case 1: /* Keyboard Subtype */
458 return 0; /* There are no defined subtypes */
459 case 2: /* Number of F-keys */
460 return 12; /* We're doing an 101 for now, so return 12 F-keys */
461 default:
462 WARN_(keyboard)("Unknown type\n");
463 return 0; /* The book says 0 here, so 0 */
467 /******************************************************************************
468 * MapVirtualKeyA (USER32.@)
470 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
472 return MapVirtualKeyExA( code, maptype, GetKeyboardLayout(0) );
475 /******************************************************************************
476 * MapVirtualKeyW (USER32.@)
478 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
480 return MapVirtualKeyExW(code, maptype, GetKeyboardLayout(0));
483 /******************************************************************************
484 * MapVirtualKeyExA (USER32.@)
486 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
488 return MapVirtualKeyExW(code, maptype, hkl);
491 /******************************************************************************
492 * MapVirtualKeyExW (USER32.@)
494 UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
496 TRACE_(keyboard)("(%d, %d, %p)\n", code, maptype, hkl);
498 if (USER_Driver.pMapVirtualKeyEx)
499 return USER_Driver.pMapVirtualKeyEx(code, maptype, hkl);
500 return 0;
503 /****************************************************************************
504 * GetKBCodePage (USER32.@)
506 UINT WINAPI GetKBCodePage(void)
508 return GetOEMCP();
511 /***********************************************************************
512 * GetKeyboardLayout (USER32.@)
514 * - device handle for keyboard layout defaulted to
515 * the language id. This is the way Windows default works.
516 * - the thread identifier (dwLayout) is also ignored.
518 HKL WINAPI GetKeyboardLayout(DWORD dwLayout)
520 if (USER_Driver.pGetKeyboardLayout)
521 return USER_Driver.pGetKeyboardLayout(dwLayout);
522 return 0;
525 /****************************************************************************
526 * GetKeyboardLayoutNameA (USER32.@)
528 BOOL WINAPI GetKeyboardLayoutNameA(LPSTR pszKLID)
530 WCHAR buf[KL_NAMELENGTH];
532 if (GetKeyboardLayoutNameW(buf))
533 return WideCharToMultiByte( CP_ACP, 0, buf, -1, pszKLID, KL_NAMELENGTH, NULL, NULL ) != 0;
534 return FALSE;
537 /****************************************************************************
538 * GetKeyboardLayoutNameW (USER32.@)
540 BOOL WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
542 if (USER_Driver.pGetKeyboardLayoutName)
543 return USER_Driver.pGetKeyboardLayoutName(pwszKLID);
544 return FALSE;
547 /****************************************************************************
548 * GetKeyNameTextA (USER32.@)
550 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
552 WCHAR buf[256];
553 INT ret;
555 if (!GetKeyNameTextW(lParam, buf, 256))
556 return 0;
557 ret = WideCharToMultiByte(CP_ACP, 0, buf, -1, lpBuffer, nSize, NULL, NULL);
558 if (!ret && nSize)
560 ret = nSize - 1;
561 lpBuffer[ret] = 0;
563 return ret;
566 /****************************************************************************
567 * GetKeyNameTextW (USER32.@)
569 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
571 if (USER_Driver.pGetKeyNameText)
572 return USER_Driver.pGetKeyNameText( lParam, lpBuffer, nSize );
573 return 0;
576 /****************************************************************************
577 * ToUnicode (USER32.@)
579 INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
580 LPWSTR lpwStr, int size, UINT flags)
582 return ToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, GetKeyboardLayout(0));
585 /****************************************************************************
586 * ToUnicodeEx (USER32.@)
588 INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
589 LPWSTR lpwStr, int size, UINT flags, HKL hkl)
591 if (USER_Driver.pToUnicodeEx)
592 return USER_Driver.pToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, hkl);
593 return 0;
596 /****************************************************************************
597 * ToAscii (USER32.@)
599 INT WINAPI ToAscii( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
600 LPWORD lpChar, UINT flags )
602 return ToAsciiEx(virtKey, scanCode, lpKeyState, lpChar, flags, GetKeyboardLayout(0));
605 /****************************************************************************
606 * ToAsciiEx (USER32.@)
608 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
609 LPWORD lpChar, UINT flags, HKL dwhkl )
611 WCHAR uni_chars[2];
612 INT ret, n_ret;
614 ret = ToUnicodeEx(virtKey, scanCode, lpKeyState, uni_chars, 2, flags, dwhkl);
615 if (ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
616 else n_ret = ret;
617 WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
618 return ret;
621 /**********************************************************************
622 * ActivateKeyboardLayout (USER32.@)
624 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
626 TRACE_(keyboard)("(%p, %d)\n", hLayout, flags);
628 if (USER_Driver.pActivateKeyboardLayout)
629 return USER_Driver.pActivateKeyboardLayout(hLayout, flags);
630 return 0;
634 /***********************************************************************
635 * GetKeyboardLayoutList (USER32.@)
637 * Return number of values available if either input parm is
638 * 0, per MS documentation.
640 UINT WINAPI GetKeyboardLayoutList(INT nBuff, HKL *layouts)
642 TRACE_(keyboard)("(%d,%p)\n",nBuff,layouts);
644 if (USER_Driver.pGetKeyboardLayoutList)
645 return USER_Driver.pGetKeyboardLayoutList(nBuff, layouts);
646 return 0;
650 /***********************************************************************
651 * RegisterHotKey (USER32.@)
653 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk)
655 FIXME_(keyboard)("(%p,%d,0x%08x,%d): stub\n",hwnd,id,modifiers,vk);
656 return TRUE;
659 /***********************************************************************
660 * UnregisterHotKey (USER32.@)
662 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id)
664 FIXME_(keyboard)("(%p,%d): stub\n",hwnd,id);
665 return TRUE;
668 /***********************************************************************
669 * LoadKeyboardLayoutW (USER32.@)
671 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
673 TRACE_(keyboard)("(%s, %d)\n", debugstr_w(pwszKLID), Flags);
675 if (USER_Driver.pLoadKeyboardLayout)
676 return USER_Driver.pLoadKeyboardLayout(pwszKLID, Flags);
677 return 0;
680 /***********************************************************************
681 * LoadKeyboardLayoutA (USER32.@)
683 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
685 HKL ret;
686 UNICODE_STRING pwszKLIDW;
688 if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
689 else pwszKLIDW.Buffer = NULL;
691 ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
692 RtlFreeUnicodeString(&pwszKLIDW);
693 return ret;
697 /***********************************************************************
698 * UnloadKeyboardLayout (USER32.@)
700 BOOL WINAPI UnloadKeyboardLayout(HKL hkl)
702 TRACE_(keyboard)("(%p)\n", hkl);
704 if (USER_Driver.pUnloadKeyboardLayout)
705 return USER_Driver.pUnloadKeyboardLayout(hkl);
706 return 0;
709 typedef struct __TRACKINGLIST {
710 TRACKMOUSEEVENT tme;
711 POINT pos; /* center of hover rectangle */
712 INT iHoverTime; /* elapsed time the cursor has been inside of the hover rect */
713 } _TRACKINGLIST;
715 static _TRACKINGLIST TrackingList[10];
716 static int iTrackMax = 0;
717 static UINT_PTR timer;
718 static const INT iTimerInterval = 50; /* msec for timer interval */
720 static void CALLBACK TrackMouseEventProc(HWND hwndUnused, UINT uMsg, UINT_PTR idEvent,
721 DWORD dwTime)
723 int i = 0;
724 POINT pos;
725 POINT posClient;
726 HWND hwnd;
727 INT nonclient;
728 INT hoverwidth = 0, hoverheight = 0;
729 RECT client;
731 GetCursorPos(&pos);
732 hwnd = WindowFromPoint(pos);
734 SystemParametersInfoA(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
735 SystemParametersInfoA(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
737 /* loop through tracking events we are processing */
738 while (i < iTrackMax) {
739 if (TrackingList[i].tme.dwFlags & TME_NONCLIENT) {
740 nonclient = 1;
742 else {
743 nonclient = 0;
746 /* see if this tracking event is looking for TME_LEAVE and that the */
747 /* mouse has left the window */
748 if (TrackingList[i].tme.dwFlags & TME_LEAVE) {
749 if (TrackingList[i].tme.hwndTrack != hwnd) {
750 if (nonclient) {
751 PostMessageA(TrackingList[i].tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
753 else {
754 PostMessageA(TrackingList[i].tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
757 /* remove the TME_LEAVE flag */
758 TrackingList[i].tme.dwFlags ^= TME_LEAVE;
760 else {
761 GetClientRect(hwnd, &client);
762 MapWindowPoints(hwnd, NULL, (LPPOINT)&client, 2);
763 if(PtInRect(&client, pos)) {
764 if (nonclient) {
765 PostMessageA(TrackingList[i].tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
766 /* remove the TME_LEAVE flag */
767 TrackingList[i].tme.dwFlags ^= TME_LEAVE;
770 else {
771 if (!nonclient) {
772 PostMessageA(TrackingList[i].tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
773 /* remove the TME_LEAVE flag */
774 TrackingList[i].tme.dwFlags ^= TME_LEAVE;
780 /* see if we are tracking hovering for this hwnd */
781 if(TrackingList[i].tme.dwFlags & TME_HOVER) {
782 /* add the timer interval to the hovering time */
783 TrackingList[i].iHoverTime+=iTimerInterval;
785 /* has the cursor moved outside the rectangle centered around pos? */
786 if((abs(pos.x - TrackingList[i].pos.x) > (hoverwidth / 2.0))
787 || (abs(pos.y - TrackingList[i].pos.y) > (hoverheight / 2.0)))
789 /* record this new position as the current position and reset */
790 /* the iHoverTime variable to 0 */
791 TrackingList[i].pos = pos;
792 TrackingList[i].iHoverTime = 0;
795 /* has the mouse hovered long enough? */
796 if(TrackingList[i].iHoverTime <= TrackingList[i].tme.dwHoverTime)
798 posClient.x = pos.x;
799 posClient.y = pos.y;
800 ScreenToClient(hwnd, &posClient);
801 if (nonclient) {
802 PostMessageW(TrackingList[i].tme.hwndTrack, WM_NCMOUSEHOVER,
803 get_key_state(), MAKELPARAM( posClient.x, posClient.y ));
805 else {
806 PostMessageW(TrackingList[i].tme.hwndTrack, WM_MOUSEHOVER,
807 get_key_state(), MAKELPARAM( posClient.x, posClient.y ));
810 /* stop tracking mouse hover */
811 TrackingList[i].tme.dwFlags ^= TME_HOVER;
815 /* see if we are still tracking TME_HOVER or TME_LEAVE for this entry */
816 if((TrackingList[i].tme.dwFlags & TME_HOVER) ||
817 (TrackingList[i].tme.dwFlags & TME_LEAVE)) {
818 i++;
819 } else { /* remove this entry from the tracking list */
820 TrackingList[i] = TrackingList[--iTrackMax];
824 /* stop the timer if the tracking list is empty */
825 if(iTrackMax == 0) {
826 KillTimer(0, timer);
827 timer = 0;
832 /***********************************************************************
833 * TrackMouseEvent [USER32]
835 * Requests notification of mouse events
837 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
838 * to the hwnd specified in the ptme structure. After the event message
839 * is posted to the hwnd, the entry in the queue is removed.
841 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
842 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
843 * immediately and the TME_LEAVE flag being ignored.
845 * PARAMS
846 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
848 * RETURNS
849 * Success: non-zero
850 * Failure: zero
854 BOOL WINAPI
855 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
857 DWORD flags = 0;
858 int i = 0;
859 BOOL cancel = 0, hover = 0, leave = 0, query = 0, nonclient = 0, inclient = 0;
860 HWND hwnd;
861 POINT pos;
862 RECT client;
865 pos.x = 0;
866 pos.y = 0;
867 SetRectEmpty(&client);
869 TRACE("%lx, %lx, %p, %lx\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
871 if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
872 WARN("wrong TRACKMOUSEEVENT size from app\n");
873 SetLastError(ERROR_INVALID_PARAMETER); /* FIXME not sure if this is correct */
874 return FALSE;
877 flags = ptme->dwFlags;
879 /* if HOVER_DEFAULT was specified replace this with the systems current value */
880 if(ptme->dwHoverTime == HOVER_DEFAULT)
881 SystemParametersInfoA(SPI_GETMOUSEHOVERTIME, 0, &(ptme->dwHoverTime), 0);
883 GetCursorPos(&pos);
884 hwnd = WindowFromPoint(pos);
886 if ( flags & TME_CANCEL ) {
887 flags &= ~ TME_CANCEL;
888 cancel = 1;
891 if ( flags & TME_HOVER ) {
892 flags &= ~ TME_HOVER;
893 hover = 1;
896 if ( flags & TME_LEAVE ) {
897 flags &= ~ TME_LEAVE;
898 leave = 1;
901 if ( flags & TME_NONCLIENT ) {
902 flags &= ~ TME_NONCLIENT;
903 nonclient = 1;
906 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
907 if ( flags & TME_QUERY ) {
908 flags &= ~ TME_QUERY;
909 query = 1;
910 i = 0;
912 /* Find the tracking list entry with the matching hwnd */
913 while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
914 i++;
917 /* hwnd found, fill in the ptme struct */
918 if(i < iTrackMax)
919 *ptme = TrackingList[i].tme;
920 else
921 ptme->dwFlags = 0;
923 return TRUE; /* return here, TME_QUERY is retrieving information */
926 if ( flags )
927 FIXME("Unknown flag(s) %08lx\n", flags );
929 if(cancel) {
930 /* find a matching hwnd if one exists */
931 i = 0;
933 while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
934 i++;
937 if(i < iTrackMax) {
938 TrackingList[i].tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
940 /* if we aren't tracking on hover or leave remove this entry */
941 if(!((TrackingList[i].tme.dwFlags & TME_HOVER) ||
942 (TrackingList[i].tme.dwFlags & TME_LEAVE)))
944 TrackingList[i] = TrackingList[--iTrackMax];
946 if(iTrackMax == 0) {
947 KillTimer(0, timer);
948 timer = 0;
952 } else {
953 /* see if hwndTrack isn't the current window */
954 if(ptme->hwndTrack != hwnd) {
955 if(leave) {
956 if(nonclient) {
957 PostMessageA(ptme->hwndTrack, WM_NCMOUSELEAVE, 0, 0);
959 else {
960 PostMessageA(ptme->hwndTrack, WM_MOUSELEAVE, 0, 0);
963 } else {
964 GetClientRect(ptme->hwndTrack, &client);
965 MapWindowPoints(ptme->hwndTrack, NULL, (LPPOINT)&client, 2);
966 if(PtInRect(&client, pos)) {
967 inclient = 1;
969 if(nonclient && inclient) {
970 PostMessageA(ptme->hwndTrack, WM_NCMOUSELEAVE, 0, 0);
971 return TRUE;
973 else if(!nonclient && !inclient) {
974 PostMessageA(ptme->hwndTrack, WM_MOUSELEAVE, 0, 0);
975 return TRUE;
978 /* See if this hwnd is already being tracked and update the tracking flags */
979 for(i = 0; i < iTrackMax; i++) {
980 if(TrackingList[i].tme.hwndTrack == ptme->hwndTrack) {
981 TrackingList[i].tme.dwFlags = 0;
983 if(hover) {
984 TrackingList[i].tme.dwFlags |= TME_HOVER;
985 TrackingList[i].tme.dwHoverTime = ptme->dwHoverTime;
988 if(leave)
989 TrackingList[i].tme.dwFlags |= TME_LEAVE;
991 if(nonclient)
992 TrackingList[i].tme.dwFlags |= TME_NONCLIENT;
994 /* reset iHoverTime as per winapi specs */
995 TrackingList[i].iHoverTime = 0;
997 return TRUE;
1001 /* if the tracking list is full return FALSE */
1002 if (iTrackMax == sizeof (TrackingList) / sizeof(*TrackingList)) {
1003 return FALSE;
1006 /* Adding new mouse event to the tracking list */
1007 TrackingList[iTrackMax].tme = *ptme;
1009 /* Initialize HoverInfo variables even if not hover tracking */
1010 TrackingList[iTrackMax].iHoverTime = 0;
1011 TrackingList[iTrackMax].pos = pos;
1013 iTrackMax++;
1015 if (!timer) {
1016 timer = SetTimer(0, 0, iTimerInterval, TrackMouseEventProc);
1021 return TRUE;