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