user32: Implement SPI_[SG]ETCARETWIDTH.
[wine] / dlls / user / input.c
1 /*
2  * USER Input processing
3  *
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
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <assert.h>
34
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"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(key);
49 WINE_DECLARE_DEBUG_CHANNEL(keyboard);
50
51
52 /***********************************************************************
53  *           get_key_state
54  */
55 static WORD get_key_state(void)
56 {
57     WORD ret = 0;
58
59     if (GetSystemMetrics( SM_SWAPBUTTON ))
60     {
61         if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_LBUTTON;
62         if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_RBUTTON;
63     }
64     else
65     {
66         if (GetAsyncKeyState(VK_LBUTTON) & 0x80) ret |= MK_LBUTTON;
67         if (GetAsyncKeyState(VK_RBUTTON) & 0x80) ret |= MK_RBUTTON;
68     }
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;
75 }
76
77
78 /***********************************************************************
79  *              SendInput  (USER32.@)
80  */
81 UINT WINAPI SendInput( UINT count, LPINPUT inputs, int size )
82 {
83     return USER_Driver->pSendInput( count, inputs, size );
84 }
85
86
87 /***********************************************************************
88  *              keybd_event (USER32.@)
89  */
90 void WINAPI keybd_event( BYTE bVk, BYTE bScan,
91                          DWORD dwFlags, ULONG_PTR dwExtraInfo )
92 {
93     INPUT input;
94
95     input.type = INPUT_KEYBOARD;
96     input.u.ki.wVk = bVk;
97     input.u.ki.wScan = bScan;
98     input.u.ki.dwFlags = dwFlags;
99     input.u.ki.time = GetTickCount();
100     input.u.ki.dwExtraInfo = dwExtraInfo;
101     SendInput( 1, &input, sizeof(input) );
102 }
103
104
105 /***********************************************************************
106  *              mouse_event (USER32.@)
107  */
108 void WINAPI mouse_event( DWORD dwFlags, DWORD dx, DWORD dy,
109                          DWORD dwData, ULONG_PTR dwExtraInfo )
110 {
111     INPUT input;
112
113     input.type = INPUT_MOUSE;
114     input.u.mi.dx = dx;
115     input.u.mi.dy = dy;
116     input.u.mi.mouseData = dwData;
117     input.u.mi.dwFlags = dwFlags;
118     input.u.mi.time = GetCurrentTime();
119     input.u.mi.dwExtraInfo = dwExtraInfo;
120     SendInput( 1, &input, sizeof(input) );
121 }
122
123
124 /***********************************************************************
125  *              GetCursorPos (USER32.@)
126  */
127 BOOL WINAPI GetCursorPos( POINT *pt )
128 {
129     if (!pt) return FALSE;
130     return USER_Driver->pGetCursorPos( pt );
131 }
132
133
134 /***********************************************************************
135  *              GetCursorInfo (USER32.@)
136  */
137 BOOL WINAPI GetCursorInfo( PCURSORINFO pci )
138 {
139     if (!pci) return 0;
140     if (get_user_thread_info()->cursor_count >= 0) pci->flags = CURSOR_SHOWING;
141     else pci->flags = 0;
142     GetCursorPos(&pci->ptScreenPos);
143     return 1;
144 }
145
146
147 /***********************************************************************
148  *              SetCursorPos (USER32.@)
149  */
150 BOOL WINAPI SetCursorPos( INT x, INT y )
151 {
152     return USER_Driver->pSetCursorPos( x, y );
153 }
154
155
156 /**********************************************************************
157  *              SetCapture (USER32.@)
158  */
159 HWND WINAPI SetCapture( HWND hwnd )
160 {
161     HWND previous = 0;
162
163     SERVER_START_REQ( set_capture_window )
164     {
165         req->handle = hwnd;
166         req->flags  = 0;
167         if (!wine_server_call_err( req ))
168         {
169             previous = reply->previous;
170             hwnd = reply->full_handle;
171         }
172     }
173     SERVER_END_REQ;
174
175     if (previous && previous != hwnd)
176         SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
177     return previous;
178 }
179
180
181 /**********************************************************************
182  *              ReleaseCapture (USER32.@)
183  */
184 BOOL WINAPI ReleaseCapture(void)
185 {
186     BOOL ret;
187     HWND previous = 0;
188
189     SERVER_START_REQ( set_capture_window )
190     {
191         req->handle = 0;
192         req->flags  = 0;
193         if ((ret = !wine_server_call_err( req ))) previous = reply->previous;
194     }
195     SERVER_END_REQ;
196
197     if (previous) SendMessageW( previous, WM_CAPTURECHANGED, 0, 0 );
198
199     /* Somebody may have missed some mouse movements */
200     mouse_event( MOUSEEVENTF_MOVE, 0, 0, 0, 0 );
201
202     return ret;
203 }
204
205
206 /**********************************************************************
207  *              GetCapture (USER32.@)
208  */
209 HWND WINAPI GetCapture(void)
210 {
211     HWND ret = 0;
212
213     SERVER_START_REQ( get_thread_input )
214     {
215         req->tid = GetCurrentThreadId();
216         if (!wine_server_call_err( req )) ret = reply->capture;
217     }
218     SERVER_END_REQ;
219     return ret;
220 }
221
222
223 /**********************************************************************
224  *              GetAsyncKeyState (USER32.@)
225  *
226  *      Determine if a key is or was pressed.  retval has high-order
227  * bit set to 1 if currently pressed, low-order bit set to 1 if key has
228  * been pressed.
229  */
230 SHORT WINAPI GetAsyncKeyState(INT nKey)
231 {
232     return USER_Driver->pGetAsyncKeyState( nKey );
233 }
234
235
236 /***********************************************************************
237  *              GetQueueStatus (USER32.@)
238  */
239 DWORD WINAPI GetQueueStatus( UINT flags )
240 {
241     DWORD ret = 0;
242
243     if (flags & ~(QS_ALLINPUT | QS_ALLPOSTMESSAGE | QS_SMRESULT))
244     {
245         SetLastError( ERROR_INVALID_FLAGS );
246         return 0;
247     }
248
249     /* check for pending X events */
250     USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, flags, 0 );
251
252     SERVER_START_REQ( get_queue_status )
253     {
254         req->clear = 1;
255         wine_server_call( req );
256         ret = MAKELONG( reply->changed_bits & flags, reply->wake_bits & flags );
257     }
258     SERVER_END_REQ;
259     return ret;
260 }
261
262
263 /***********************************************************************
264  *              GetInputState   (USER32.@)
265  */
266 BOOL WINAPI GetInputState(void)
267 {
268     DWORD ret = 0;
269
270     /* check for pending X events */
271     USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_INPUT, 0 );
272
273     SERVER_START_REQ( get_queue_status )
274     {
275         req->clear = 0;
276         wine_server_call( req );
277         ret = reply->wake_bits & (QS_KEY | QS_MOUSEBUTTON);
278     }
279     SERVER_END_REQ;
280     return ret;
281 }
282
283
284 /******************************************************************
285  *              GetLastInputInfo (USER32.@)
286  */
287 BOOL WINAPI GetLastInputInfo(PLASTINPUTINFO plii)
288 {
289     BOOL ret;
290
291     TRACE("%p\n", plii);
292
293     if (plii->cbSize != sizeof (*plii) )
294     {
295         SetLastError(ERROR_INVALID_PARAMETER);
296         return FALSE;
297     }
298
299     SERVER_START_REQ( get_last_input_time )
300     {
301         ret = !wine_server_call_err( req );
302         if (ret)
303             plii->dwTime = reply->time;
304     }
305     SERVER_END_REQ;
306     return ret;
307 }
308
309
310 /**********************************************************************
311  *              AttachThreadInput (USER32.@)
312  *
313  * Attaches the input processing mechanism of one thread to that of
314  * another thread.
315  */
316 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
317 {
318     BOOL ret;
319
320     SERVER_START_REQ( attach_thread_input )
321     {
322         req->tid_from = from;
323         req->tid_to   = to;
324         req->attach   = attach;
325         ret = !wine_server_call_err( req );
326     }
327     SERVER_END_REQ;
328     return ret;
329 }
330
331
332 /**********************************************************************
333  *              GetKeyState (USER32.@)
334  *
335  * An application calls the GetKeyState function in response to a
336  * keyboard-input message.  This function retrieves the state of the key
337  * at the time the input message was generated.
338  */
339 SHORT WINAPI GetKeyState(INT vkey)
340 {
341     SHORT retval = 0;
342
343     SERVER_START_REQ( get_key_state )
344     {
345         req->tid = GetCurrentThreadId();
346         req->key = vkey;
347         if (!wine_server_call( req )) retval = (signed char)reply->state;
348     }
349     SERVER_END_REQ;
350     TRACE("key (0x%x) -> %x\n", vkey, retval);
351     return retval;
352 }
353
354
355 /**********************************************************************
356  *              GetKeyboardState (USER32.@)
357  */
358 BOOL WINAPI GetKeyboardState( LPBYTE state )
359 {
360     BOOL ret;
361
362     TRACE("(%p)\n", state);
363
364     memset( state, 0, 256 );
365     SERVER_START_REQ( get_key_state )
366     {
367         req->tid = GetCurrentThreadId();
368         req->key = -1;
369         wine_server_set_reply( req, state, 256 );
370         ret = !wine_server_call_err( req );
371     }
372     SERVER_END_REQ;
373     return ret;
374 }
375
376
377 /**********************************************************************
378  *              SetKeyboardState (USER32.@)
379  */
380 BOOL WINAPI SetKeyboardState( LPBYTE state )
381 {
382     BOOL ret;
383
384     TRACE("(%p)\n", state);
385
386     SERVER_START_REQ( set_key_state )
387     {
388         req->tid = GetCurrentThreadId();
389         wine_server_add_data( req, state, 256 );
390         ret = !wine_server_call_err( req );
391     }
392     SERVER_END_REQ;
393     return ret;
394 }
395
396
397 /**********************************************************************
398  *              VkKeyScanA (USER32.@)
399  *
400  * VkKeyScan translates an ANSI character to a virtual-key and shift code
401  * for the current keyboard.
402  * high-order byte yields :
403  *      0       Unshifted
404  *      1       Shift
405  *      2       Ctrl
406  *      3-5     Shift-key combinations that are not used for characters
407  *      6       Ctrl-Alt
408  *      7       Ctrl-Alt-Shift
409  *      I.e. :  Shift = 1, Ctrl = 2, Alt = 4.
410  * FIXME : works ok except for dead chars :
411  * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
412  * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
413  */
414 SHORT WINAPI VkKeyScanA(CHAR cChar)
415 {
416     WCHAR wChar;
417
418     if (IsDBCSLeadByte(cChar)) return -1;
419
420     MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
421     return VkKeyScanW(wChar);
422 }
423
424 /******************************************************************************
425  *              VkKeyScanW (USER32.@)
426  */
427 SHORT WINAPI VkKeyScanW(WCHAR cChar)
428 {
429     return VkKeyScanExW(cChar, GetKeyboardLayout(0));
430 }
431
432 /**********************************************************************
433  *              VkKeyScanExA (USER32.@)
434  */
435 WORD WINAPI VkKeyScanExA(CHAR cChar, HKL dwhkl)
436 {
437     WCHAR wChar;
438
439     if (IsDBCSLeadByte(cChar)) return -1;
440
441     MultiByteToWideChar(CP_ACP, 0, &cChar, 1, &wChar, 1);
442     return VkKeyScanExW(wChar, dwhkl);
443 }
444
445 /******************************************************************************
446  *              VkKeyScanExW (USER32.@)
447  */
448 WORD WINAPI VkKeyScanExW(WCHAR cChar, HKL dwhkl)
449 {
450     return USER_Driver->pVkKeyScanEx(cChar, dwhkl);
451 }
452
453 /**********************************************************************
454  *              OemKeyScan (USER32.@)
455  */
456 DWORD WINAPI OemKeyScan(WORD wOemChar)
457 {
458     TRACE("(%d)\n", wOemChar);
459     return wOemChar;
460 }
461
462 /******************************************************************************
463  *              GetKeyboardType (USER32.@)
464  */
465 INT WINAPI GetKeyboardType(INT nTypeFlag)
466 {
467     TRACE_(keyboard)("(%d)\n", nTypeFlag);
468     switch(nTypeFlag)
469     {
470     case 0:      /* Keyboard type */
471         return 4;    /* AT-101 */
472     case 1:      /* Keyboard Subtype */
473         return 0;    /* There are no defined subtypes */
474     case 2:      /* Number of F-keys */
475         return 12;   /* We're doing an 101 for now, so return 12 F-keys */
476     default:
477         WARN_(keyboard)("Unknown type\n");
478         return 0;    /* The book says 0 here, so 0 */
479     }
480 }
481
482 /******************************************************************************
483  *              MapVirtualKeyA (USER32.@)
484  */
485 UINT WINAPI MapVirtualKeyA(UINT code, UINT maptype)
486 {
487     return MapVirtualKeyExA( code, maptype, GetKeyboardLayout(0) );
488 }
489
490 /******************************************************************************
491  *              MapVirtualKeyW (USER32.@)
492  */
493 UINT WINAPI MapVirtualKeyW(UINT code, UINT maptype)
494 {
495     return MapVirtualKeyExW(code, maptype, GetKeyboardLayout(0));
496 }
497
498 /******************************************************************************
499  *              MapVirtualKeyExA (USER32.@)
500  */
501 UINT WINAPI MapVirtualKeyExA(UINT code, UINT maptype, HKL hkl)
502 {
503     return MapVirtualKeyExW(code, maptype, hkl);
504 }
505
506 /******************************************************************************
507  *              MapVirtualKeyExW (USER32.@)
508  */
509 UINT WINAPI MapVirtualKeyExW(UINT code, UINT maptype, HKL hkl)
510 {
511     TRACE_(keyboard)("(%d, %d, %p)\n", code, maptype, hkl);
512
513     return USER_Driver->pMapVirtualKeyEx(code, maptype, hkl);
514 }
515
516 /****************************************************************************
517  *              GetKBCodePage (USER32.@)
518  */
519 UINT WINAPI GetKBCodePage(void)
520 {
521     return GetOEMCP();
522 }
523
524 /***********************************************************************
525  *              GetKeyboardLayout (USER32.@)
526  *
527  *        - device handle for keyboard layout defaulted to
528  *          the language id. This is the way Windows default works.
529  *        - the thread identifier (dwLayout) is also ignored.
530  */
531 HKL WINAPI GetKeyboardLayout(DWORD dwLayout)
532 {
533     return USER_Driver->pGetKeyboardLayout(dwLayout);
534 }
535
536 /****************************************************************************
537  *              GetKeyboardLayoutNameA (USER32.@)
538  */
539 BOOL WINAPI GetKeyboardLayoutNameA(LPSTR pszKLID)
540 {
541     WCHAR buf[KL_NAMELENGTH];
542
543     if (GetKeyboardLayoutNameW(buf))
544         return WideCharToMultiByte( CP_ACP, 0, buf, -1, pszKLID, KL_NAMELENGTH, NULL, NULL ) != 0;
545     return FALSE;
546 }
547
548 /****************************************************************************
549  *              GetKeyboardLayoutNameW (USER32.@)
550  */
551 BOOL WINAPI GetKeyboardLayoutNameW(LPWSTR pwszKLID)
552 {
553     return USER_Driver->pGetKeyboardLayoutName(pwszKLID);
554 }
555
556 /****************************************************************************
557  *              GetKeyNameTextA (USER32.@)
558  */
559 INT WINAPI GetKeyNameTextA(LONG lParam, LPSTR lpBuffer, INT nSize)
560 {
561     WCHAR buf[256];
562     INT ret;
563
564     if (!GetKeyNameTextW(lParam, buf, 256))
565         return 0;
566     ret = WideCharToMultiByte(CP_ACP, 0, buf, -1, lpBuffer, nSize, NULL, NULL);
567     if (!ret && nSize)
568     {
569         ret = nSize - 1;
570         lpBuffer[ret] = 0;
571     }
572     return ret;
573 }
574
575 /****************************************************************************
576  *              GetKeyNameTextW (USER32.@)
577  */
578 INT WINAPI GetKeyNameTextW(LONG lParam, LPWSTR lpBuffer, INT nSize)
579 {
580     return USER_Driver->pGetKeyNameText( lParam, lpBuffer, nSize );
581 }
582
583 /****************************************************************************
584  *              ToUnicode (USER32.@)
585  */
586 INT WINAPI ToUnicode(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
587                      LPWSTR lpwStr, int size, UINT flags)
588 {
589     return ToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, GetKeyboardLayout(0));
590 }
591
592 /****************************************************************************
593  *              ToUnicodeEx (USER32.@)
594  */
595 INT WINAPI ToUnicodeEx(UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
596                        LPWSTR lpwStr, int size, UINT flags, HKL hkl)
597 {
598     return USER_Driver->pToUnicodeEx(virtKey, scanCode, lpKeyState, lpwStr, size, flags, hkl);
599 }
600
601 /****************************************************************************
602  *              ToAscii (USER32.@)
603  */
604 INT WINAPI ToAscii( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
605                     LPWORD lpChar, UINT flags )
606 {
607     return ToAsciiEx(virtKey, scanCode, lpKeyState, lpChar, flags, GetKeyboardLayout(0));
608 }
609
610 /****************************************************************************
611  *              ToAsciiEx (USER32.@)
612  */
613 INT WINAPI ToAsciiEx( UINT virtKey, UINT scanCode, LPBYTE lpKeyState,
614                       LPWORD lpChar, UINT flags, HKL dwhkl )
615 {
616     WCHAR uni_chars[2];
617     INT ret, n_ret;
618
619     ret = ToUnicodeEx(virtKey, scanCode, lpKeyState, uni_chars, 2, flags, dwhkl);
620     if (ret < 0) n_ret = 1; /* FIXME: make ToUnicode return 2 for dead chars */
621     else n_ret = ret;
622     WideCharToMultiByte(CP_ACP, 0, uni_chars, n_ret, (LPSTR)lpChar, 2, NULL, NULL);
623     return ret;
624 }
625
626 /**********************************************************************
627  *              ActivateKeyboardLayout (USER32.@)
628  */
629 HKL WINAPI ActivateKeyboardLayout(HKL hLayout, UINT flags)
630 {
631     TRACE_(keyboard)("(%p, %d)\n", hLayout, flags);
632
633     return USER_Driver->pActivateKeyboardLayout(hLayout, flags);
634 }
635
636
637 /***********************************************************************
638  *              GetKeyboardLayoutList (USER32.@)
639  *
640  * Return number of values available if either input parm is
641  *  0, per MS documentation.
642  */
643 UINT WINAPI GetKeyboardLayoutList(INT nBuff, HKL *layouts)
644 {
645     TRACE_(keyboard)("(%d,%p)\n",nBuff,layouts);
646
647     return USER_Driver->pGetKeyboardLayoutList(nBuff, layouts);
648 }
649
650
651 /***********************************************************************
652  *              RegisterHotKey (USER32.@)
653  */
654 BOOL WINAPI RegisterHotKey(HWND hwnd,INT id,UINT modifiers,UINT vk)
655 {
656     FIXME_(keyboard)("(%p,%d,0x%08x,%d): stub\n",hwnd,id,modifiers,vk);
657     return TRUE;
658 }
659
660 /***********************************************************************
661  *              UnregisterHotKey (USER32.@)
662  */
663 BOOL WINAPI UnregisterHotKey(HWND hwnd,INT id)
664 {
665     FIXME_(keyboard)("(%p,%d): stub\n",hwnd,id);
666     return TRUE;
667 }
668
669 /***********************************************************************
670  *              LoadKeyboardLayoutW (USER32.@)
671  */
672 HKL WINAPI LoadKeyboardLayoutW(LPCWSTR pwszKLID, UINT Flags)
673 {
674     TRACE_(keyboard)("(%s, %d)\n", debugstr_w(pwszKLID), Flags);
675
676     return USER_Driver->pLoadKeyboardLayout(pwszKLID, Flags);
677 }
678
679 /***********************************************************************
680  *              LoadKeyboardLayoutA (USER32.@)
681  */
682 HKL WINAPI LoadKeyboardLayoutA(LPCSTR pwszKLID, UINT Flags)
683 {
684     HKL ret;
685     UNICODE_STRING pwszKLIDW;
686
687     if (pwszKLID) RtlCreateUnicodeStringFromAsciiz(&pwszKLIDW, pwszKLID);
688     else pwszKLIDW.Buffer = NULL;
689
690     ret = LoadKeyboardLayoutW(pwszKLIDW.Buffer, Flags);
691     RtlFreeUnicodeString(&pwszKLIDW);
692     return ret;
693 }
694
695
696 /***********************************************************************
697  *              UnloadKeyboardLayout (USER32.@)
698  */
699 BOOL WINAPI UnloadKeyboardLayout(HKL hkl)
700 {
701     TRACE_(keyboard)("(%p)\n", hkl);
702
703     return USER_Driver->pUnloadKeyboardLayout(hkl);
704 }
705
706 typedef struct __TRACKINGLIST {
707     TRACKMOUSEEVENT tme;
708     POINT pos; /* center of hover rectangle */
709     INT iHoverTime; /* elapsed time the cursor has been inside of the hover rect */
710 } _TRACKINGLIST;
711
712 static _TRACKINGLIST TrackingList[10];
713 static int iTrackMax = 0;
714 static UINT_PTR timer;
715 static const INT iTimerInterval = 50; /* msec for timer interval */
716
717 static void CALLBACK TrackMouseEventProc(HWND hwndUnused, UINT uMsg, UINT_PTR idEvent,
718     DWORD dwTime)
719 {
720     int i = 0;
721     POINT pos;
722     POINT posClient;
723     HWND hwnd;
724     INT nonclient;
725     INT hoverwidth = 0, hoverheight = 0;
726     RECT client;
727
728     GetCursorPos(&pos);
729     hwnd = WindowFromPoint(pos);
730
731     SystemParametersInfoA(SPI_GETMOUSEHOVERWIDTH, 0, &hoverwidth, 0);
732     SystemParametersInfoA(SPI_GETMOUSEHOVERHEIGHT, 0, &hoverheight, 0);
733
734     /* loop through tracking events we are processing */
735     while (i < iTrackMax) {
736         if (TrackingList[i].tme.dwFlags & TME_NONCLIENT) {
737             nonclient = 1;
738         }
739         else {
740             nonclient = 0;
741         }
742
743         /* see if this tracking event is looking for TME_LEAVE and that the */
744         /* mouse has left the window */
745         if (TrackingList[i].tme.dwFlags & TME_LEAVE) {
746             if (TrackingList[i].tme.hwndTrack != hwnd) {
747                 if (nonclient) {
748                     PostMessageA(TrackingList[i].tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
749                 }
750                 else {
751                     PostMessageA(TrackingList[i].tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
752                 }
753
754                 /* remove the TME_LEAVE flag */
755                 TrackingList[i].tme.dwFlags ^= TME_LEAVE;
756             }
757             else {
758                 GetClientRect(hwnd, &client);
759                 MapWindowPoints(hwnd, NULL, (LPPOINT)&client, 2);
760                 if(PtInRect(&client, pos)) {
761                     if (nonclient) {
762                         PostMessageA(TrackingList[i].tme.hwndTrack, WM_NCMOUSELEAVE, 0, 0);
763                         /* remove the TME_LEAVE flag */
764                         TrackingList[i].tme.dwFlags ^= TME_LEAVE;
765                     }
766                 }
767                 else {
768                     if (!nonclient) {
769                         PostMessageA(TrackingList[i].tme.hwndTrack, WM_MOUSELEAVE, 0, 0);
770                         /* remove the TME_LEAVE flag */
771                         TrackingList[i].tme.dwFlags ^= TME_LEAVE;
772                     }
773                 }
774             }
775         }
776
777         /* see if we are tracking hovering for this hwnd */
778         if(TrackingList[i].tme.dwFlags & TME_HOVER) {
779             /* add the timer interval to the hovering time */
780             TrackingList[i].iHoverTime+=iTimerInterval;
781
782             /* has the cursor moved outside the rectangle centered around pos? */
783             if((abs(pos.x - TrackingList[i].pos.x) > (hoverwidth / 2.0))
784               || (abs(pos.y - TrackingList[i].pos.y) > (hoverheight / 2.0)))
785             {
786                 /* record this new position as the current position and reset */
787                 /* the iHoverTime variable to 0 */
788                 TrackingList[i].pos = pos;
789                 TrackingList[i].iHoverTime = 0;
790             }
791
792             /* has the mouse hovered long enough? */
793             if(TrackingList[i].iHoverTime <= TrackingList[i].tme.dwHoverTime)
794             {
795                 posClient.x = pos.x;
796                 posClient.y = pos.y;
797                 ScreenToClient(hwnd, &posClient);
798                 if (nonclient) {
799                     PostMessageW(TrackingList[i].tme.hwndTrack, WM_NCMOUSEHOVER,
800                                 get_key_state(), MAKELPARAM( posClient.x, posClient.y ));
801                 }
802                 else {
803                     PostMessageW(TrackingList[i].tme.hwndTrack, WM_MOUSEHOVER,
804                                 get_key_state(), MAKELPARAM( posClient.x, posClient.y ));
805                 }
806
807                 /* stop tracking mouse hover */
808                 TrackingList[i].tme.dwFlags ^= TME_HOVER;
809             }
810         }
811
812         /* see if we are still tracking TME_HOVER or TME_LEAVE for this entry */
813         if((TrackingList[i].tme.dwFlags & TME_HOVER) ||
814            (TrackingList[i].tme.dwFlags & TME_LEAVE)) {
815             i++;
816         } else { /* remove this entry from the tracking list */
817             TrackingList[i] = TrackingList[--iTrackMax];
818         }
819     }
820
821     /* stop the timer if the tracking list is empty */
822     if(iTrackMax == 0) {
823         KillTimer(0, timer);
824         timer = 0;
825     }
826 }
827
828
829 /***********************************************************************
830  * TrackMouseEvent [USER32]
831  *
832  * Requests notification of mouse events
833  *
834  * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
835  * to the hwnd specified in the ptme structure.  After the event message
836  * is posted to the hwnd, the entry in the queue is removed.
837  *
838  * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
839  * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
840  * immediately and the TME_LEAVE flag being ignored.
841  *
842  * PARAMS
843  *     ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
844  *
845  * RETURNS
846  *     Success: non-zero
847  *     Failure: zero
848  *
849  */
850
851 BOOL WINAPI
852 TrackMouseEvent (TRACKMOUSEEVENT *ptme)
853 {
854     DWORD flags = 0;
855     int i = 0;
856     BOOL cancel = 0, hover = 0, leave = 0, query = 0, nonclient = 0, inclient = 0;
857     HWND hwnd;
858     POINT pos;
859     RECT client;
860
861
862     pos.x = 0;
863     pos.y = 0;
864     SetRectEmpty(&client);
865
866     TRACE("%lx, %lx, %p, %lx\n", ptme->cbSize, ptme->dwFlags, ptme->hwndTrack, ptme->dwHoverTime);
867
868     if (ptme->cbSize != sizeof(TRACKMOUSEEVENT)) {
869         WARN("wrong TRACKMOUSEEVENT size from app\n");
870         SetLastError(ERROR_INVALID_PARAMETER); /* FIXME not sure if this is correct */
871         return FALSE;
872     }
873
874     flags = ptme->dwFlags;
875
876     /* if HOVER_DEFAULT was specified replace this with the systems current value */
877     if(ptme->dwHoverTime == HOVER_DEFAULT)
878         SystemParametersInfoA(SPI_GETMOUSEHOVERTIME, 0, &(ptme->dwHoverTime), 0);
879
880     GetCursorPos(&pos);
881     hwnd = WindowFromPoint(pos);
882
883     if ( flags & TME_CANCEL ) {
884         flags &= ~ TME_CANCEL;
885         cancel = 1;
886     }
887
888     if ( flags & TME_HOVER  ) {
889         flags &= ~ TME_HOVER;
890         hover = 1;
891     }
892
893     if ( flags & TME_LEAVE ) {
894         flags &= ~ TME_LEAVE;
895         leave = 1;
896     }
897
898     if ( flags & TME_NONCLIENT ) {
899         flags &= ~ TME_NONCLIENT;
900         nonclient = 1;
901     }
902
903     /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
904     if ( flags & TME_QUERY ) {
905         flags &= ~ TME_QUERY;
906         query = 1;
907         i = 0;
908
909         /* Find the tracking list entry with the matching hwnd */
910         while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
911             i++;
912         }
913
914         /* hwnd found, fill in the ptme struct */
915         if(i < iTrackMax)
916             *ptme = TrackingList[i].tme;
917         else
918             ptme->dwFlags = 0;
919
920         return TRUE; /* return here, TME_QUERY is retrieving information */
921     }
922
923     if ( flags )
924         FIXME("Unknown flag(s) %08lx\n", flags );
925
926     if(cancel) {
927         /* find a matching hwnd if one exists */
928         i = 0;
929
930         while((i < iTrackMax) && (TrackingList[i].tme.hwndTrack != ptme->hwndTrack)) {
931           i++;
932         }
933
934         if(i < iTrackMax) {
935             TrackingList[i].tme.dwFlags &= ~(ptme->dwFlags & ~TME_CANCEL);
936
937             /* if we aren't tracking on hover or leave remove this entry */
938             if(!((TrackingList[i].tme.dwFlags & TME_HOVER) ||
939                  (TrackingList[i].tme.dwFlags & TME_LEAVE)))
940             {
941                 TrackingList[i] = TrackingList[--iTrackMax];
942
943                 if(iTrackMax == 0) {
944                     KillTimer(0, timer);
945                     timer = 0;
946                 }
947             }
948         }
949     } else {
950         /* see if hwndTrack isn't the current window */
951         if(ptme->hwndTrack != hwnd) {
952             if(leave) {
953                 if(nonclient) {
954                     PostMessageA(ptme->hwndTrack, WM_NCMOUSELEAVE, 0, 0);
955                 }
956                 else {
957                     PostMessageA(ptme->hwndTrack, WM_MOUSELEAVE, 0, 0);
958                 }
959             }
960         } else {
961             GetClientRect(ptme->hwndTrack, &client);
962             MapWindowPoints(ptme->hwndTrack, NULL, (LPPOINT)&client, 2);
963             if(PtInRect(&client, pos)) {
964                 inclient = 1;
965             }
966             if(nonclient && inclient) {
967                 PostMessageA(ptme->hwndTrack, WM_NCMOUSELEAVE, 0, 0);
968                 return TRUE;
969             }
970             else if(!nonclient && !inclient) {
971                 PostMessageA(ptme->hwndTrack, WM_MOUSELEAVE, 0, 0);
972                 return TRUE;
973             }
974
975             /* See if this hwnd is already being tracked and update the tracking flags */
976             for(i = 0; i < iTrackMax; i++) {
977                 if(TrackingList[i].tme.hwndTrack == ptme->hwndTrack) {
978                     TrackingList[i].tme.dwFlags = 0;
979
980                     if(hover) {
981                         TrackingList[i].tme.dwFlags |= TME_HOVER;
982                         TrackingList[i].tme.dwHoverTime = ptme->dwHoverTime;
983                     }
984
985                     if(leave)
986                         TrackingList[i].tme.dwFlags |= TME_LEAVE;
987
988                     if(nonclient)
989                         TrackingList[i].tme.dwFlags |= TME_NONCLIENT;
990
991                     /* reset iHoverTime as per winapi specs */
992                     TrackingList[i].iHoverTime = 0;
993
994                     return TRUE;
995                 }
996             }
997
998             /* if the tracking list is full return FALSE */
999             if (iTrackMax == sizeof (TrackingList) / sizeof(*TrackingList)) {
1000                 return FALSE;
1001             }
1002
1003             /* Adding new mouse event to the tracking list */
1004             TrackingList[iTrackMax].tme = *ptme;
1005
1006             /* Initialize HoverInfo variables even if not hover tracking */
1007             TrackingList[iTrackMax].iHoverTime = 0;
1008             TrackingList[iTrackMax].pos = pos;
1009
1010             iTrackMax++;
1011
1012             if (!timer) {
1013                 timer = SetTimer(0, 0, iTimerInterval, TrackMouseEventProc);
1014             }
1015         }
1016     }
1017
1018     return TRUE;
1019 }