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