Validate the parent with the child window client rectangle when the
[wine] / windows / message.c
1 /*
2  * Message queues related functions
3  *
4  * Copyright 1993, 1994 Alexandre Julliard
5  */
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include <sys/time.h>
11 #include <sys/types.h>
12
13 #include "wine/winbase16.h"
14 #include "message.h"
15 #include "winerror.h"
16 #include "win.h"
17 #include "heap.h"
18 #include "hook.h"
19 #include "input.h"
20 #include "spy.h"
21 #include "winpos.h"
22 #include "dde.h"
23 #include "queue.h"
24 #include "winproc.h"
25 #include "task.h"
26 #include "process.h"
27 #include "thread.h"
28 #include "options.h"
29 #include "struct32.h"
30 #include "debugtools.h"
31
32 DECLARE_DEBUG_CHANNEL(key)
33 DECLARE_DEBUG_CHANNEL(msg)
34 DECLARE_DEBUG_CHANNEL(sendmsg)
35
36 #define WM_NCMOUSEFIRST         WM_NCMOUSEMOVE
37 #define WM_NCMOUSELAST          WM_NCMBUTTONDBLCLK
38
39     
40 typedef enum { SYSQ_MSG_ABANDON, SYSQ_MSG_SKIP, 
41                SYSQ_MSG_ACCEPT, SYSQ_MSG_CONTINUE } SYSQ_STATUS;
42
43 extern HQUEUE16 hCursorQueue;                    /* queue.c */
44
45 DWORD MSG_WineStartTicks; /* Ticks at Wine startup */
46
47 static UINT doubleClickSpeed = 452;
48
49 /***********************************************************************
50  *           MSG_CheckFilter
51  */
52 static BOOL MSG_CheckFilter(DWORD uMsg, DWORD first, DWORD last)
53 {
54    if( first || last )
55        return (uMsg >= first && uMsg <= last);
56    return TRUE;
57 }
58
59 /***********************************************************************
60  *           MSG_SendParentNotify
61  *
62  * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
63  * the window has the WS_EX_NOPARENTNOTIFY style.
64  */
65 static void MSG_SendParentNotify(WND* wndPtr, WORD event, WORD idChild, LPARAM lValue)
66 {
67 #define lppt ((LPPOINT16)&lValue)
68
69     /* pt has to be in the client coordinates of the parent window */
70     WND *tmpWnd = WIN_LockWndPtr(wndPtr);
71
72     MapWindowPoints16( 0, tmpWnd->hwndSelf, lppt, 1 );
73     while (tmpWnd)
74     {
75         if (!(tmpWnd->dwStyle & WS_CHILD) || (tmpWnd->dwExStyle & WS_EX_NOPARENTNOTIFY))
76     {
77             WIN_ReleaseWndPtr(tmpWnd);
78             break;
79         }
80         lppt->x += tmpWnd->rectClient.left;
81         lppt->y += tmpWnd->rectClient.top;
82         WIN_UpdateWndPtr(&tmpWnd,tmpWnd->parent);
83         SendMessageA( tmpWnd->hwndSelf, WM_PARENTNOTIFY,
84                         MAKEWPARAM( event, idChild ), lValue );
85     }
86
87 #undef lppt
88 }
89
90
91 /***********************************************************************
92  *           MSG_TranslateMouseMsg
93  *
94  * Translate an mouse hardware event into a real mouse message.
95  *
96  * Returns:
97  *
98  *  SYSQ_MSG_ABANDON  - abandon the peek message loop
99  *  SYSQ_MSG_CONTINUE - leave the message in the queue and
100  *                      continue with the translation loop
101  *  SYSQ_MSG_ACCEPT   - proceed to process the translated message
102  */
103 static DWORD MSG_TranslateMouseMsg( HWND hTopWnd, DWORD first, DWORD last,
104                                     MSG *msg, BOOL remove, WND* pWndScope,
105                                     INT16 *pHitTest, POINT16 *pScreen_pt, BOOL *pmouseClick )
106 {
107     static DWORD   dblclk_time_limit = 0;
108     static UINT16     clk_message = 0;
109     static HWND16     clk_hwnd = 0;
110     static POINT16    clk_pos = { 0, 0 };
111
112     WND *pWnd;
113     HWND hWnd;
114     INT16 ht, hittest;
115     UINT message = msg->message;
116     POINT16 screen_pt, pt;
117     HANDLE16 hQ = GetFastQueue16();
118     MESSAGEQUEUE *queue = (MESSAGEQUEUE *)QUEUE_Lock(hQ);
119     BOOL mouseClick = ((message == WM_LBUTTONDOWN) ||
120                          (message == WM_RBUTTONDOWN) ||
121                          (message == WM_MBUTTONDOWN))?1:0;
122     DWORD retvalue;
123
124     /* Find the window to dispatch this mouse message to */
125
126     CONV_POINT32TO16( &msg->pt, &pt );
127     
128     hWnd = GetCapture();
129
130     /* If no capture HWND, find window which contains the mouse position.
131      * Also find the position of the cursor hot spot (hittest) */
132     if( !hWnd )
133     {
134         ht = hittest = WINPOS_WindowFromPoint( pWndScope, pt, &pWnd );
135         if( !pWnd ) pWnd = WIN_GetDesktop();
136         else WIN_LockWndPtr(pWnd);
137         hWnd = pWnd->hwndSelf;
138     } 
139     else 
140     {
141         ht = hittest = HTCLIENT;
142         pWnd = WIN_FindWndPtr(hWnd);
143         if (queue)
144             ht = PERQDATA_GetCaptureInfo( queue->pQData );
145     }
146
147     /* Save hittest for return */
148     *pHitTest = hittest;
149         
150         /* stop if not the right queue */
151
152     if (pWnd->hmemTaskQ != hQ)
153     {
154         /* Not for the current task */
155         if (queue) QUEUE_ClearWakeBit( queue, QS_MOUSE );
156         /* Wake up the other task */
157         QUEUE_Unlock( queue );
158         queue = (MESSAGEQUEUE *)QUEUE_Lock( pWnd->hmemTaskQ );
159         if (queue) QUEUE_SetWakeBit( queue, QS_MOUSE );
160
161         QUEUE_Unlock( queue );
162         retvalue = SYSQ_MSG_ABANDON;
163         goto END;
164     }
165
166         /* check if hWnd is within hWndScope */
167
168     if( hTopWnd && hWnd != hTopWnd )
169         if( !IsChild(hTopWnd, hWnd) )
170         {
171             QUEUE_Unlock( queue );
172             retvalue = SYSQ_MSG_CONTINUE;
173             goto END;
174         }
175
176     /* Was it a mouse click message */
177     if( mouseClick )
178     {
179         /* translate double clicks -
180          * note that ...MOUSEMOVEs can slip in between
181          * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
182
183         if(GetClassLongA(hWnd, GCL_STYLE) & CS_DBLCLKS || ht != HTCLIENT )
184         {
185            if ((message == clk_message) && (hWnd == clk_hwnd) &&
186                (msg->time - dblclk_time_limit < doubleClickSpeed) &&
187                (abs(msg->pt.x - clk_pos.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
188                (abs(msg->pt.y - clk_pos.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
189            {
190               message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
191               mouseClick++;   /* == 2 */
192            }
193         }
194     }
195     /* save mouse position */
196     screen_pt = pt;
197     *pScreen_pt = pt;
198
199     if (hittest != HTCLIENT)
200     {
201         message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
202         msg->wParam = hittest;
203     }
204     else
205         ScreenToClient16( hWnd, &pt );
206
207         /* check message filter */
208
209     if (!MSG_CheckFilter(message, first, last))
210     {
211         QUEUE_Unlock(queue);
212         retvalue = SYSQ_MSG_CONTINUE;
213         goto END;
214     }
215
216     /* Update global hCursorQueue */
217     hCursorQueue = queue->self;
218     
219     /* Update static double click conditions */
220     if( remove && mouseClick )
221     {
222         if( mouseClick == 1 )
223         {
224             /* set conditions */
225             dblclk_time_limit = msg->time;
226                clk_message = msg->message;
227                clk_hwnd = hWnd;
228                clk_pos = screen_pt;
229         } else 
230             /* got double click - zero them out */
231             dblclk_time_limit = clk_hwnd = 0;
232     }
233
234     QUEUE_Unlock(queue);
235
236     /* Update message params */
237     msg->hwnd    = hWnd;
238     msg->message = message;
239     msg->lParam  = MAKELONG( pt.x, pt.y );
240     retvalue = SYSQ_MSG_ACCEPT;
241
242 END:
243     WIN_ReleaseWndPtr(pWnd);
244
245     /* Return mouseclick flag */
246     *pmouseClick = mouseClick;
247     
248     return retvalue;
249 }
250
251
252 /***********************************************************************
253  *           MSG_ProcessMouseMsg
254  *
255  * Processes a translated mouse hardware event.
256  * The passed in msg structure should contain the updated hWnd, 
257  * lParam, wParam and message fields from MSG_TranslateMouseMsg.
258  *
259  * Returns:
260  *
261  *  SYSQ_MSG_SKIP     - Message should be skipped entirely (in this case
262  *                      HIWORD contains hit test code). Continue translating..
263  *  SYSQ_MSG_ACCEPT   - the translated message must be passed to the user
264  *                      MSG_PeekHardwareMsg should return TRUE.
265  */
266 static DWORD MSG_ProcessMouseMsg( MSG *msg, BOOL remove, INT16 hittest,
267                                   POINT16 screen_pt, BOOL mouseClick )
268 {
269     WND *pWnd;
270     HWND hWnd = msg->hwnd;
271     INT16 sendSC = (GetCapture() == 0);
272     UINT message = msg->message;
273     BOOL eatMsg = FALSE;
274     DWORD retvalue;
275
276     pWnd = WIN_FindWndPtr(hWnd);
277     
278         /* call WH_MOUSE */
279
280     if (HOOK_IsHooked( WH_MOUSE ))
281     { 
282         SYSQ_STATUS ret = 0;
283         MOUSEHOOKSTRUCT16 *hook = SEGPTR_NEW(MOUSEHOOKSTRUCT16);
284         if( hook )
285         {
286             hook->pt           = screen_pt;
287             hook->hwnd         = hWnd;
288             hook->wHitTestCode = hittest;
289             hook->dwExtraInfo  = 0;
290             ret = HOOK_CallHooks16( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
291                                     message, (LPARAM)SEGPTR_GET(hook) );
292             SEGPTR_FREE(hook);
293         }
294         if( ret )
295         {
296             retvalue = MAKELONG((INT16)SYSQ_MSG_SKIP, hittest);
297             goto END;
298     }
299     }
300
301
302     if ((hittest == HTERROR) || (hittest == HTNOWHERE)) 
303         eatMsg = sendSC = 1;
304     else if( remove && mouseClick )
305     {
306         HWND hwndTop = WIN_GetTopParent( hWnd );
307
308         if( sendSC )
309         {
310             /* Send the WM_PARENTNOTIFY,
311              * note that even for double/nonclient clicks
312              * notification message is still WM_L/M/RBUTTONDOWN.
313              */
314
315             MSG_SendParentNotify( pWnd, msg->message & 0xffff, 0, MAKELPARAM(screen_pt.x, screen_pt.y) );
316
317             /* Activate the window if needed */
318
319             if (hWnd != GetActiveWindow() && hWnd != GetDesktopWindow())
320             {
321                 LONG ret = SendMessageA( hWnd, WM_MOUSEACTIVATE, hwndTop,
322                                           MAKELONG( hittest, message ) );
323
324                 if ((ret == MA_ACTIVATEANDEAT) || (ret == MA_NOACTIVATEANDEAT))
325                          eatMsg = TRUE;
326
327                 if (((ret == MA_ACTIVATE) || (ret == MA_ACTIVATEANDEAT))
328                         && hwndTop != GetForegroundWindow() )
329                 {
330                       if (!WINPOS_SetActiveWindow( hwndTop, TRUE , TRUE ))
331                          eatMsg = TRUE;
332                 }
333             }
334         }
335     } else sendSC = (remove && sendSC);
336
337      /* Send the WM_SETCURSOR message */
338
339     if (sendSC)
340         SendMessageA( hWnd, WM_SETCURSOR, hWnd,
341                        MAKELONG( hittest, message ));
342     if (eatMsg)
343     {
344         retvalue = MAKELONG( (UINT16)SYSQ_MSG_SKIP, hittest);
345         goto END;
346     }
347
348     retvalue = SYSQ_MSG_ACCEPT;
349 END:
350     WIN_ReleaseWndPtr(pWnd);
351
352     return retvalue;
353 }
354
355
356 /***********************************************************************
357  *           MSG_TranslateKbdMsg
358  *
359  * Translate an keyboard hardware event into a real message.
360  */
361 static DWORD MSG_TranslateKbdMsg( HWND hTopWnd, DWORD first, DWORD last,
362                                   MSG *msg, BOOL remove )
363 {
364     WORD message = msg->message;
365     HWND hWnd = GetFocus();
366     WND *pWnd;
367
368       /* Should check Ctrl-Esc and PrintScreen here */
369
370     if (!hWnd)
371     {
372           /* Send the message to the active window instead,  */
373           /* translating messages to their WM_SYS equivalent */
374
375         hWnd = GetActiveWindow();
376
377         if( message < WM_SYSKEYDOWN )
378             message += WM_SYSKEYDOWN - WM_KEYDOWN;
379     }
380     pWnd = WIN_FindWndPtr( hWnd );
381     if (pWnd && (pWnd->hmemTaskQ != GetFastQueue16()))
382     {
383         /* Not for the current task */
384         MESSAGEQUEUE *queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() );
385         if (queue) QUEUE_ClearWakeBit( queue, QS_KEY );
386         QUEUE_Unlock( queue );
387         
388         /* Wake up the other task */
389         queue = (MESSAGEQUEUE *)QUEUE_Lock( pWnd->hmemTaskQ );
390         if (queue) QUEUE_SetWakeBit( queue, QS_KEY );
391         QUEUE_Unlock( queue );
392         WIN_ReleaseWndPtr(pWnd);
393         return SYSQ_MSG_ABANDON;
394     }
395     WIN_ReleaseWndPtr(pWnd);
396
397     if (hTopWnd && hWnd != hTopWnd)
398         if (!IsChild(hTopWnd, hWnd)) return SYSQ_MSG_CONTINUE;
399     if (!MSG_CheckFilter(message, first, last)) return SYSQ_MSG_CONTINUE;
400
401     msg->hwnd = hWnd;
402     msg->message = message;
403
404     return SYSQ_MSG_ACCEPT;
405 }
406
407
408 /***********************************************************************
409  *           MSG_ProcessKbdMsg
410  *
411  *  Processes a translated keyboard message
412  */
413 static DWORD MSG_ProcessKbdMsg( MSG *msg, BOOL remove )
414 {
415     return (HOOK_CallHooks16( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
416                               LOWORD (msg->wParam), msg->lParam )
417             ? SYSQ_MSG_SKIP : SYSQ_MSG_ACCEPT);
418 }
419
420
421 /***********************************************************************
422  *           MSG_JournalRecordMsg
423  *
424  * Build an EVENTMSG structure and call JOURNALRECORD hook
425  */
426 static void MSG_JournalRecordMsg( MSG *msg )
427 {
428     EVENTMSG *event = (EVENTMSG *) HeapAlloc(SystemHeap, 0, sizeof(EVENTMSG));
429     if (!event) return;
430     event->message = msg->message;
431     event->time = msg->time;
432     if ((msg->message >= WM_KEYFIRST) && (msg->message <= WM_KEYLAST))
433     {
434         event->paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
435         event->paramH = msg->lParam & 0x7FFF;  
436         if (HIWORD(msg->lParam) & 0x0100)
437             event->paramH |= 0x8000;               /* special_key - bit */
438         HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)event );
439     }
440     else if ((msg->message >= WM_MOUSEFIRST) && (msg->message <= WM_MOUSELAST))
441     {
442         event->paramL = LOWORD(msg->lParam);       /* X pos */
443         event->paramH = HIWORD(msg->lParam);       /* Y pos */ 
444         ClientToScreen16( msg->hwnd, (LPPOINT16)&event->paramL );
445         HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)event );
446     }
447     else if ((msg->message >= WM_NCMOUSEFIRST) &&
448              (msg->message <= WM_NCMOUSELAST))
449     {
450         event->paramL = LOWORD(msg->lParam);       /* X pos */
451         event->paramH = HIWORD(msg->lParam);       /* Y pos */ 
452         event->message += WM_MOUSEMOVE-WM_NCMOUSEMOVE;/* give no info about NC area */
453         HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)event );
454     }
455     
456     HeapFree(SystemHeap, 0, event);
457 }
458
459 /***********************************************************************
460  *          MSG_JournalPlayBackMsg
461  *
462  * Get an EVENTMSG struct via call JOURNALPLAYBACK hook function 
463  */
464 static int MSG_JournalPlayBackMsg(void)
465 {
466  EVENTMSG *tmpMsg;
467  long wtime,lParam,wParam;
468  WORD keyDown,i,result=0;
469
470  if ( HOOK_IsHooked( WH_JOURNALPLAYBACK ) )
471  {
472   tmpMsg = (EVENTMSG *) HeapAlloc(SystemHeap, 0, sizeof(EVENTMSG));
473   if (!tmpMsg) return result;
474   
475   wtime=HOOK_CallHooksA( WH_JOURNALPLAYBACK, HC_GETNEXT, 0,
476                            (LPARAM) tmpMsg );
477   /*  TRACE(msg,"Playback wait time =%ld\n",wtime); */
478   if (wtime<=0)
479   {
480    wtime=0;
481    if ((tmpMsg->message>= WM_KEYFIRST) && (tmpMsg->message <= WM_KEYLAST))
482    {
483      wParam=tmpMsg->paramL & 0xFF;
484      lParam=MAKELONG(tmpMsg->paramH&0x7ffff,tmpMsg->paramL>>8);
485      if (tmpMsg->message == WM_KEYDOWN || tmpMsg->message == WM_SYSKEYDOWN)
486      {
487        for (keyDown=i=0; i<256 && !keyDown; i++)
488           if (InputKeyStateTable[i] & 0x80)
489             keyDown++;
490        if (!keyDown)
491          lParam |= 0x40000000;       
492        AsyncKeyStateTable[wParam]=InputKeyStateTable[wParam] |= 0x80;
493      }  
494      else                                       /* WM_KEYUP, WM_SYSKEYUP */
495      {
496        lParam |= 0xC0000000;      
497        AsyncKeyStateTable[wParam]=InputKeyStateTable[wParam] &= ~0x80;
498      }
499      if (InputKeyStateTable[VK_MENU] & 0x80)
500        lParam |= 0x20000000;     
501      if (tmpMsg->paramH & 0x8000)              /*special_key bit*/
502        lParam |= 0x01000000;
503      hardware_event( tmpMsg->message & 0xffff, LOWORD(wParam), lParam,
504                      0, 0, tmpMsg->time, 0 );
505    }
506    else
507    {
508     if ((tmpMsg->message>= WM_MOUSEFIRST) && (tmpMsg->message <= WM_MOUSELAST))
509     {
510      switch (tmpMsg->message)
511      {
512       case WM_LBUTTONDOWN:
513           MouseButtonsStates[0]=AsyncMouseButtonsStates[0]=TRUE;break;
514       case WM_LBUTTONUP:
515           MouseButtonsStates[0]=AsyncMouseButtonsStates[0]=FALSE;break;
516       case WM_MBUTTONDOWN:
517           MouseButtonsStates[1]=AsyncMouseButtonsStates[1]=TRUE;break;
518       case WM_MBUTTONUP:
519           MouseButtonsStates[1]=AsyncMouseButtonsStates[1]=FALSE;break;
520       case WM_RBUTTONDOWN:
521           MouseButtonsStates[2]=AsyncMouseButtonsStates[2]=TRUE;break;
522       case WM_RBUTTONUP:
523           MouseButtonsStates[2]=AsyncMouseButtonsStates[2]=FALSE;break;      
524      }
525      AsyncKeyStateTable[VK_LBUTTON]= InputKeyStateTable[VK_LBUTTON] = MouseButtonsStates[0] ? 0x80 : 0;
526      AsyncKeyStateTable[VK_MBUTTON]= InputKeyStateTable[VK_MBUTTON] = MouseButtonsStates[1] ? 0x80 : 0;
527      AsyncKeyStateTable[VK_RBUTTON]= InputKeyStateTable[VK_RBUTTON] = MouseButtonsStates[2] ? 0x80 : 0;
528      SetCursorPos(tmpMsg->paramL,tmpMsg->paramH);
529      lParam=MAKELONG(tmpMsg->paramL,tmpMsg->paramH);
530      wParam=0;             
531      if (MouseButtonsStates[0]) wParam |= MK_LBUTTON;
532      if (MouseButtonsStates[1]) wParam |= MK_MBUTTON;
533      if (MouseButtonsStates[2]) wParam |= MK_RBUTTON;
534      hardware_event( tmpMsg->message & 0xffff, LOWORD (wParam), lParam,
535                      tmpMsg->paramL, tmpMsg->paramH, tmpMsg->time, 0 );
536     }
537    }
538    HOOK_CallHooksA( WH_JOURNALPLAYBACK, HC_SKIP, 0,
539                       (LPARAM) tmpMsg);
540   }
541   else
542   {
543       
544     if( tmpMsg->message == WM_QUEUESYNC )
545         if (HOOK_IsHooked( WH_CBT ))
546             HOOK_CallHooksA( WH_CBT, HCBT_QS, 0, 0L);
547
548     result= QS_MOUSE | QS_KEY; /* ? */
549   }
550   HeapFree(SystemHeap, 0, tmpMsg);
551  }
552  return result;
553
554
555 /***********************************************************************
556  *           MSG_PeekHardwareMsg
557  *
558  * Peek for a hardware message matching the hwnd and message filters.
559  */
560 static BOOL MSG_PeekHardwareMsg( MSG *msg, HWND hwnd, DWORD first, DWORD last,
561                                    BOOL remove )
562 {
563     /* FIXME: should deal with MSG32 instead of MSG16 */
564     
565     DWORD status = SYSQ_MSG_ACCEPT;
566     MESSAGEQUEUE *sysMsgQueue = QUEUE_GetSysQueue();
567     enum { MOUSE_MSG = 0, KEYBOARD_MSG, HARDWARE_MSG } msgType;
568     QMSG *nextqmsg, *qmsg = 0;
569     BOOL bRet = FALSE;
570
571     /* FIXME: there has to be a better way to do this */
572     joySendMessages();
573
574     EnterCriticalSection(&sysMsgQueue->cSection);
575
576     /* Loop through the Q and translate the message we wish to process
577      * while we own the lock. Based on the translation status (abandon/cont/accept)
578      * we then process the message accordingly
579      */
580
581     for ( qmsg = sysMsgQueue->firstMsg; qmsg; qmsg = nextqmsg )
582     {
583         INT16 hittest;
584         POINT16 screen_pt;
585         BOOL mouseClick;
586
587         *msg = qmsg->msg;
588
589         nextqmsg = qmsg->nextMsg;
590
591           /* Translate message */
592
593         if ((msg->message >= WM_MOUSEFIRST) && (msg->message <= WM_MOUSELAST))
594         {
595             HWND hWndScope = (HWND)qmsg->extraInfo;
596             WND *tmpWnd = (Options.managed && IsWindow(hWndScope) ) 
597                            ? WIN_FindWndPtr(hWndScope) : WIN_GetDesktop();
598
599             status = MSG_TranslateMouseMsg(hwnd, first, last, msg, remove, tmpWnd,
600                                            &hittest, &screen_pt, &mouseClick );
601             msgType = MOUSE_MSG;
602             
603             WIN_ReleaseWndPtr(tmpWnd);
604
605         }
606         else if ((msg->message >= WM_KEYFIRST) && (msg->message <= WM_KEYLAST))
607         {
608             status = MSG_TranslateKbdMsg(hwnd, first, last, msg, remove);
609             msgType = KEYBOARD_MSG;
610         }
611         else /* Non-standard hardware event */
612         {
613             HARDWAREHOOKSTRUCT16 *hook;
614             msgType = HARDWARE_MSG;
615             if ((hook = SEGPTR_NEW(HARDWAREHOOKSTRUCT16)))
616             {
617                 BOOL ret;
618                 hook->hWnd     = msg->hwnd;
619                 hook->wMessage = msg->message & 0xffff;
620                 hook->wParam   = LOWORD (msg->wParam);
621                 hook->lParam   = msg->lParam;
622                 ret = HOOK_CallHooks16( WH_HARDWARE,
623                                         remove ? HC_ACTION : HC_NOREMOVE,
624                                         0, (LPARAM)SEGPTR_GET(hook) );
625                 SEGPTR_FREE(hook);
626                 if (ret) 
627                 {
628                     QUEUE_RemoveMsg( sysMsgQueue, qmsg );
629                     continue;
630                 }
631                 status = SYSQ_MSG_ACCEPT; 
632             }
633         }
634
635         
636         switch (LOWORD(status))
637         {
638            case SYSQ_MSG_ACCEPT:
639            {
640                /* Remove the message from the system msg Q while it is still locked,
641                 * before accepting it */
642                if (remove)
643                {
644                    if (HOOK_IsHooked( WH_JOURNALRECORD )) MSG_JournalRecordMsg( msg );
645                    QUEUE_RemoveMsg( sysMsgQueue, qmsg );
646                }
647                /* Now actually process the message, after we unlock the system msg Q.
648                 * We should not hold on to the crst since SendMessage calls during processing 
649                 * will potentially cause callbacks to PeekMessage from the application.
650                 * If we're holding the crst and QUEUE_WaitBits is called with a
651                 * QS_SENDMESSAGE mask we will deadlock in hardware_event() when a
652                 * message is being posted to the Q.
653                 */
654                LeaveCriticalSection(&sysMsgQueue->cSection);
655                if( msgType == KEYBOARD_MSG )
656                    status = MSG_ProcessKbdMsg( msg, remove );
657                else if ( msgType == MOUSE_MSG )
658                    status = MSG_ProcessMouseMsg( msg, remove, hittest, screen_pt, mouseClick );
659
660                /* Reclaim the sys msg Q crst */
661                EnterCriticalSection(&sysMsgQueue->cSection);
662                
663                /* Pass the translated message to the user if it was accepted */
664                if (status == SYSQ_MSG_ACCEPT)
665                 break;
666
667                /* If not accepted, fall through into the SYSQ_MSG_SKIP case */
668            }
669                 
670            case SYSQ_MSG_SKIP:
671                 if (HOOK_IsHooked( WH_CBT ))
672                 {
673                    if( msgType == KEYBOARD_MSG )
674                        HOOK_CallHooks16( WH_CBT, HCBT_KEYSKIPPED, 
675                                          LOWORD (msg->wParam), msg->lParam );
676                    else if ( msgType == MOUSE_MSG )
677                    {
678                        MOUSEHOOKSTRUCT16 *hook = SEGPTR_NEW(MOUSEHOOKSTRUCT16);
679                        if (hook)
680                        {
681                            CONV_POINT32TO16( &msg->pt,&hook->pt );
682                            hook->hwnd         = msg->hwnd;
683                            hook->wHitTestCode = HIWORD(status);
684                            hook->dwExtraInfo  = 0;
685                            HOOK_CallHooks16( WH_CBT, HCBT_CLICKSKIPPED ,msg->message & 0xffff,
686                                           (LPARAM)SEGPTR_GET(hook) );
687                            SEGPTR_FREE(hook);
688                        }
689                    }
690                 }
691
692                 /* If the message was removed earlier set up nextqmsg so that we start 
693                  * at the top of the queue again.  We need to do this since our next pointer
694                  * could be invalid due to us unlocking the system message Q to process the message.
695                  * If not removed just refresh nextqmsg to point to the next msg.
696                  */
697                 if (remove)
698                     nextqmsg = sysMsgQueue->firstMsg;
699                 else
700                     nextqmsg = qmsg->nextMsg;
701
702                 continue;
703                 
704            case SYSQ_MSG_CONTINUE:
705                 continue;
706
707            case SYSQ_MSG_ABANDON: 
708                bRet = FALSE;
709                goto END;
710         }
711
712         bRet = TRUE;
713         goto END;
714     }
715
716 END:
717     LeaveCriticalSection(&sysMsgQueue->cSection);
718     return bRet;
719 }
720
721
722 /**********************************************************************
723  *           SetDoubleClickTime16   (USER.20)
724  */
725 void WINAPI SetDoubleClickTime16( UINT16 interval )
726 {
727     SetDoubleClickTime( interval );
728 }               
729
730
731 /**********************************************************************
732  *           SetDoubleClickTime32   (USER32.480)
733  */
734 BOOL WINAPI SetDoubleClickTime( UINT interval )
735 {
736     doubleClickSpeed = interval ? interval : 500;
737     return TRUE;
738 }               
739
740
741 /**********************************************************************
742  *           GetDoubleClickTime16   (USER.21)
743  */
744 UINT16 WINAPI GetDoubleClickTime16(void)
745 {
746     return doubleClickSpeed;
747 }               
748
749
750 /**********************************************************************
751  *           GetDoubleClickTime32   (USER32.239)
752  */
753 UINT WINAPI GetDoubleClickTime(void)
754 {
755     return doubleClickSpeed;
756 }               
757
758
759 /***********************************************************************
760  *           MSG_SendMessageInterThread
761  *
762  * Implementation of an inter-task SendMessage.
763  * Return values:
764  *    0 if error or timeout
765  *    1 if successflul
766  */
767 static LRESULT MSG_SendMessageInterThread( HQUEUE16 hDestQueue,
768                                            HWND hwnd, UINT msg,
769                                            WPARAM wParam, LPARAM lParam,
770                                            DWORD timeout, WORD flags,
771                                            LRESULT *pRes)
772 {
773     MESSAGEQUEUE *queue, *destQ;
774     SMSG         *smsg;
775     LRESULT      retVal = 1;
776     int          iWndsLocks;
777     
778     *pRes = 0;
779
780     if (IsTaskLocked16() || !IsWindow(hwnd))
781         return 0;
782
783     /* create a SMSG structure to hold SendMessage() parameters */
784     if (! (smsg = (SMSG *) HeapAlloc( SystemHeap, 0, sizeof(SMSG) )) )
785         return 0;
786     if (!(queue = (MESSAGEQUEUE*)QUEUE_Lock( GetFastQueue16() ))) return 0;
787
788     if (!(destQ = (MESSAGEQUEUE*)QUEUE_Lock( hDestQueue )))
789     {
790         QUEUE_Unlock( queue );
791         return 0;
792     }
793
794     TRACE_(sendmsg)("SM: %s [%04x] (%04x -> %04x)\n",
795                     SPY_GetMsgName(msg), msg, queue->self, hDestQueue );
796
797     /* fill up SMSG structure */
798     smsg->hWnd = hwnd;
799     smsg->msg = msg;
800     smsg->wParam = wParam;
801     smsg->lParam = lParam;
802     
803     smsg->lResult = 0;
804     smsg->hSrcQueue = GetFastQueue16();
805     smsg->hDstQueue = hDestQueue;
806     smsg->flags = flags;
807
808     /* add smsg struct in the processing SM list of the source queue */
809     QUEUE_AddSMSG(queue, SM_PROCESSING_LIST, smsg);
810
811     /* add smsg struct in the pending list of the destination queue */
812     if (QUEUE_AddSMSG(destQ, SM_PENDING_LIST, smsg) == FALSE)
813         return 0;
814
815     iWndsLocks = WIN_SuspendWndsLock();
816
817     /* force destination task to run next, if 16 bit threads */
818     if ( THREAD_IsWin16(NtCurrentTeb()) && THREAD_IsWin16(destQ->teb) )
819         DirectedYield16( destQ->teb->htask16 );
820
821     /* wait for the result, note that 16-bit apps almost always get out of
822      * DirectedYield() with SMSG_HAVE_RESULT flag already set */
823
824     while ( TRUE )
825     {
826         /*
827          * The sequence is crucial to avoid deadlock situations:
828          * - first, we clear the QS_SMRESULT bit
829          * - then, we check the SMSG_HAVE_RESULT bit
830          * - only if this isn't set, we enter the wait state.
831          *
832          * As the receiver first sets the SMSG_HAVE_RESULT and then wakes us,
833          * we are guaranteed that -should we now clear the QS_SMRESULT that
834          * was signalled already by the receiver- we will not start waiting.
835          */
836
837         if ( smsg->flags & SMSG_HAVE_RESULT )
838         {
839 got:
840              *pRes = smsg->lResult;
841              TRACE_(sendmsg)("smResult = %08x\n", (unsigned)*pRes );
842              break;
843         }
844
845         QUEUE_ClearWakeBit( queue, QS_SMRESULT );
846
847         if ( smsg->flags & SMSG_HAVE_RESULT )
848              goto got;
849
850         if(  QUEUE_WaitBits( QS_SMRESULT, timeout ) == 0 )
851         {
852              /* return with timeout */
853              SetLastError( 0 );
854              retVal = 0;
855              break;
856         }
857     }
858     WIN_RestoreWndsLock(iWndsLocks);
859
860     /* remove the smsg from the processingg list of the source queue */
861     QUEUE_RemoveSMSG( queue, SM_PROCESSING_LIST, smsg );
862
863     /* Note: the destination thread is in charge of removing the smsg from
864        the pending list */
865
866     /* In the case of an early reply (or a timeout), sender thread will
867        released the smsg structure if the receiver thread is done
868        (SMSG_RECEIVED set). If the receiver thread isn't done,
869        SMSG_RECEIVER_CLEANS_UP flag is set, and it will be the receiver
870        responsability to released smsg */
871         EnterCriticalSection( &queue->cSection );
872     
873         if (smsg->flags & SMSG_RECEIVED)
874             HeapFree(SystemHeap, 0, smsg);
875         else
876             smsg->flags |= SMSG_RECEIVER_CLEANS;
877         
878         LeaveCriticalSection( &queue->cSection );
879
880
881     QUEUE_Unlock( queue );
882     QUEUE_Unlock( destQ );
883     
884     TRACE_(sendmsg)("done!\n");
885     return retVal;
886 }
887
888
889 /***********************************************************************
890  *           ReplyMessage16   (USER.115)
891  */
892 void WINAPI ReplyMessage16( LRESULT result )
893 {
894     ReplyMessage( result );
895 }
896
897 /***********************************************************************
898  *           ReplyMessage   (USER.115)
899  */
900 BOOL WINAPI ReplyMessage( LRESULT result )
901 {
902     MESSAGEQUEUE *senderQ = 0;
903     MESSAGEQUEUE *queue = 0;
904     SMSG         *smsg;
905     BOOL       ret = FALSE;
906
907     if (!(queue = (MESSAGEQUEUE*)QUEUE_Lock( GetFastQueue16() )))
908         return FALSE;
909
910     TRACE_(sendmsg)("ReplyMessage, queue %04x\n", queue->self);
911
912
913     if (    !(smsg = queue->smWaiting)
914          || !(senderQ = QUEUE_Lock( smsg->hSrcQueue )) )
915         goto ReplyMessageEnd;
916
917     if ( !(smsg->flags & SMSG_ALREADY_REPLIED) )
918     {
919         /* This is the first reply, so pass result to sender */
920
921         TRACE_(sendmsg)("\trpm: smResult = %08lx\n", (long) result );
922
923         EnterCriticalSection(&senderQ->cSection);
924         
925         smsg->lResult = result;
926         smsg->flags |= SMSG_ALREADY_REPLIED;
927
928         /* check if it's an early reply (called by the application) or
929            a regular reply (called by ReceiveMessage) */
930         if ( !(smsg->flags & SMSG_SENDING_REPLY) )
931             smsg->flags |= SMSG_EARLY_REPLY;
932
933         smsg->flags |= SMSG_HAVE_RESULT;
934
935         LeaveCriticalSection(&senderQ->cSection);
936
937         /* tell the sending task that its reply is ready */
938         QUEUE_SetWakeBit( senderQ, QS_SMRESULT );
939
940         /* switch directly to sending task (16 bit thread only) */
941         if ( THREAD_IsWin16( NtCurrentTeb() ) && THREAD_IsWin16( senderQ->teb ) )
942             DirectedYield16( senderQ->teb->htask16 );
943
944         ret = TRUE;
945     }
946     
947     if (smsg->flags & SMSG_SENDING_REPLY)
948     {
949         /* remove msg from the waiting list, since this  is the last
950           ReplyMessage */
951         QUEUE_RemoveSMSG( queue, SM_WAITING_LIST, smsg );
952         
953         EnterCriticalSection(&senderQ->cSection);
954         
955         /* tell the sender we're all done with smsg structure */
956         smsg->flags |= SMSG_RECEIVED;
957
958         /* sender will set SMSG_RECEIVER_CLEANS_UP if it wants the
959          receiver to clean up smsg, it could only happens when there is
960          an early reply or a timeout */
961         if ( smsg->flags & SMSG_RECEIVER_CLEANS )
962         {
963             TRACE_(sendmsg)("Receiver cleans up!\n" );
964             HeapFree( SystemHeap, 0, smsg );
965         }
966         
967         LeaveCriticalSection(&senderQ->cSection);
968     }
969
970 ReplyMessageEnd:
971     if ( senderQ )
972     QUEUE_Unlock( senderQ );
973     if ( queue )
974     QUEUE_Unlock( queue );
975
976     return ret;
977 }
978
979 /***********************************************************************
980  *           MSG_PeekMessage
981  */
982 static BOOL MSG_PeekMessage( LPMSG msg, HWND hwnd, DWORD first, DWORD last,
983                                WORD flags, BOOL peek )
984 {
985     int mask;
986     MESSAGEQUEUE *msgQueue;
987     HQUEUE16 hQueue;
988     POINT16 pt16;
989     int iWndsLocks;
990
991     mask = QS_POSTMESSAGE | QS_SENDMESSAGE;  /* Always selected */
992     if (first || last)
993     {
994         if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
995         if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
996              ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
997         if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
998         if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
999         if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
1000     }
1001     else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
1002
1003     if (IsTaskLocked16()) flags |= PM_NOYIELD;
1004
1005     /* Never yield on Win32 threads */
1006     if (!THREAD_IsWin16(NtCurrentTeb())) flags |= PM_NOYIELD;
1007
1008     iWndsLocks = WIN_SuspendWndsLock();
1009
1010     while(1)
1011     {    
1012         QMSG *qmsg;
1013         
1014         hQueue   = GetFastQueue16();
1015         msgQueue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
1016         if (!msgQueue)
1017         {
1018             WIN_RestoreWndsLock(iWndsLocks);
1019             return FALSE;
1020         }
1021         msgQueue->changeBits = 0;
1022
1023         /* First handle a message put by SendMessage() */
1024
1025         while (msgQueue->wakeBits & QS_SENDMESSAGE)
1026             QUEUE_ReceiveMessage( msgQueue );
1027
1028         /* Now handle a WM_QUIT message */
1029
1030         if (msgQueue->wPostQMsg &&
1031            (!first || WM_QUIT >= first) && 
1032            (!last || WM_QUIT <= last) )
1033         {
1034             msg->hwnd    = hwnd;
1035             msg->message = WM_QUIT;
1036             msg->wParam  = msgQueue->wExitCode;
1037             msg->lParam  = 0;
1038             if (flags & PM_REMOVE) msgQueue->wPostQMsg = 0;
1039             break;
1040         }
1041     
1042         /* Now find a normal message */
1043
1044         if (((msgQueue->wakeBits & mask) & QS_POSTMESSAGE) &&
1045             ((qmsg = QUEUE_FindMsg( msgQueue, hwnd, first, last )) != 0))
1046         {
1047             *msg = qmsg->msg;
1048             
1049             msgQueue->GetMessageTimeVal      = msg->time;
1050             CONV_POINT32TO16(&msg->pt, &pt16);
1051             msgQueue->GetMessagePosVal       = *(DWORD *)&pt16;
1052             msgQueue->GetMessageExtraInfoVal = qmsg->extraInfo;
1053
1054             if (flags & PM_REMOVE) QUEUE_RemoveMsg( msgQueue, qmsg );
1055             break;
1056         }
1057
1058         msgQueue->changeBits |= MSG_JournalPlayBackMsg();
1059
1060         /* Now find a hardware event */
1061
1062         if (((msgQueue->wakeBits & mask) & (QS_MOUSE | QS_KEY)) &&
1063             MSG_PeekHardwareMsg( msg, hwnd, first, last, flags & PM_REMOVE ))
1064         {
1065             /* Got one */
1066             msgQueue->GetMessageTimeVal      = msg->time;
1067             CONV_POINT32TO16(&msg->pt, &pt16);
1068             msgQueue->GetMessagePosVal       = *(DWORD *)&pt16;
1069             msgQueue->GetMessageExtraInfoVal = 0;  /* Always 0 for now */
1070             break;
1071         }
1072
1073         /* Check again for SendMessage */
1074
1075         while (msgQueue->wakeBits & QS_SENDMESSAGE)
1076             QUEUE_ReceiveMessage( msgQueue );
1077
1078         /* Now find a WM_PAINT message */
1079
1080         if ((msgQueue->wakeBits & mask) & QS_PAINT)
1081         {
1082             WND* wndPtr;
1083             msg->hwnd = WIN_FindWinToRepaint( hwnd , hQueue );
1084             msg->message = WM_PAINT;
1085             msg->wParam = 0;
1086             msg->lParam = 0;
1087
1088             if ((wndPtr = WIN_FindWndPtr(msg->hwnd)))
1089             {
1090                 if( wndPtr->dwStyle & WS_MINIMIZE &&
1091                     (HICON) GetClassLongA(wndPtr->hwndSelf, GCL_HICON) )
1092                 {
1093                     msg->message = WM_PAINTICON;
1094                     msg->wParam = 1;
1095                 }
1096
1097                 if( !hwnd || msg->hwnd == hwnd || IsChild16(hwnd,msg->hwnd) )
1098                 {
1099                     if( wndPtr->flags & WIN_INTERNAL_PAINT && !wndPtr->hrgnUpdate)
1100                     {
1101                         wndPtr->flags &= ~WIN_INTERNAL_PAINT;
1102                         QUEUE_DecPaintCount( hQueue );
1103                     }
1104                     WIN_ReleaseWndPtr(wndPtr);
1105                     break;
1106                 }
1107                 WIN_ReleaseWndPtr(wndPtr);
1108             }
1109         }
1110
1111         /* Check for timer messages, but yield first */
1112
1113         if (!(flags & PM_NOYIELD))
1114         {
1115             UserYield16();
1116             while (msgQueue->wakeBits & QS_SENDMESSAGE)
1117                 QUEUE_ReceiveMessage( msgQueue );
1118         }
1119         if ((msgQueue->wakeBits & mask) & QS_TIMER)
1120         {
1121             if (TIMER_GetTimerMsg(msg, hwnd, hQueue, flags & PM_REMOVE)) break;
1122         }
1123
1124         if (peek)
1125         {
1126             if (!(flags & PM_NOYIELD)) UserYield16();
1127             
1128             QUEUE_Unlock( msgQueue );
1129             WIN_RestoreWndsLock(iWndsLocks);
1130             return FALSE;
1131         }
1132         msgQueue->wakeMask = mask;
1133         QUEUE_WaitBits( mask, INFINITE );
1134         QUEUE_Unlock( msgQueue );
1135     }
1136
1137     WIN_RestoreWndsLock(iWndsLocks);
1138     
1139     /* instead of unlocking queue for every break condition, all break
1140        condition will fall here */
1141     QUEUE_Unlock( msgQueue );
1142     
1143       /* We got a message */
1144     if (flags & PM_REMOVE)
1145     {
1146         WORD message = msg->message;
1147
1148         if (message == WM_KEYDOWN || message == WM_SYSKEYDOWN)
1149         {
1150             BYTE *p = &QueueKeyStateTable[msg->wParam & 0xff];
1151
1152             if (!(*p & 0x80))
1153                 *p ^= 0x01;
1154             *p |= 0x80;
1155         }
1156         else if (message == WM_KEYUP || message == WM_SYSKEYUP)
1157             QueueKeyStateTable[msg->wParam & 0xff] &= ~0x80;
1158     }
1159
1160     if (peek)
1161         return TRUE;
1162
1163     else
1164         return (msg->message != WM_QUIT);
1165 }
1166
1167 /***********************************************************************
1168  *           MSG_InternalGetMessage
1169  *
1170  * GetMessage() function for internal use. Behave like GetMessage(),
1171  * but also call message filters and optionally send WM_ENTERIDLE messages.
1172  * 'hwnd' must be the handle of the dialog or menu window.
1173  * 'code' is the message filter value (MSGF_??? codes).
1174  */
1175 BOOL MSG_InternalGetMessage( MSG *msg, HWND hwnd, HWND hwndOwner,
1176                                WPARAM code, WORD flags, BOOL sendIdle ) 
1177 {
1178     for (;;)
1179     {
1180         if (sendIdle)
1181         {
1182             if (!MSG_PeekMessage( msg, 0, 0, 0, flags, TRUE ))
1183             {
1184                   /* No message present -> send ENTERIDLE and wait */
1185                 if (IsWindow(hwndOwner))
1186                     SendMessageA( hwndOwner, WM_ENTERIDLE,
1187                                    code, (LPARAM)hwnd );
1188                 MSG_PeekMessage( msg, 0, 0, 0, flags, FALSE );
1189             }
1190         }
1191         else  /* Always wait for a message */
1192             MSG_PeekMessage( msg, 0, 0, 0, flags, FALSE );
1193
1194         /* Call message filters */
1195
1196         if (HOOK_IsHooked( WH_SYSMSGFILTER ) || HOOK_IsHooked( WH_MSGFILTER ))
1197         {
1198             MSG *pmsg = HeapAlloc( SystemHeap, 0, sizeof(MSG) );
1199             if (pmsg)
1200             {
1201                 BOOL ret;
1202                 *pmsg = *msg;
1203                 ret = (HOOK_CallHooksA( WH_SYSMSGFILTER, code, 0,
1204                                           (LPARAM) pmsg ) ||
1205                        HOOK_CallHooksA( WH_MSGFILTER, code, 0,
1206                                           (LPARAM) pmsg ));
1207                        
1208                 HeapFree( SystemHeap, 0, pmsg );
1209                 if (ret)
1210                 {
1211                     /* Message filtered -> remove it from the queue */
1212                     /* if it's still there. */
1213                     if (!(flags & PM_REMOVE))
1214                         MSG_PeekMessage( msg, 0, 0, 0, PM_REMOVE, TRUE );
1215                     continue;
1216                 }
1217             }
1218         }
1219
1220         return (msg->message != WM_QUIT);
1221     }
1222 }
1223
1224
1225 /***********************************************************************
1226  *           PeekMessage16   (USER.109)
1227  */
1228 BOOL16 WINAPI PeekMessage16( LPMSG16 lpmsg, HWND16 hwnd, UINT16 first,
1229                              UINT16 last, UINT16 flags )
1230 {
1231     MSG msg32;
1232     BOOL16 ret;
1233     ret = PeekMessageA(&msg32, hwnd, first, last, flags);
1234     STRUCT32_MSG32to16(&msg32, lpmsg);
1235     return ret;
1236 }
1237
1238 /***********************************************************************
1239  *         WIN16_PeekMessage32   (USER.819)
1240  */
1241 BOOL16 WINAPI PeekMessage32_16( LPMSG16_32 lpmsg16_32, HWND16 hwnd,
1242                UINT16 first, UINT16 last, UINT16 flags, BOOL16 wHaveParamHigh )
1243 {
1244     if (wHaveParamHigh == FALSE)
1245     {
1246         lpmsg16_32->wParamHigh = 0;
1247         return PeekMessage16(&(lpmsg16_32->msg), hwnd, first, last, flags);
1248     }
1249     else
1250     {
1251         MSG msg32;
1252         BOOL16 ret;
1253
1254         ret = (BOOL16)PeekMessageA(&msg32, (HWND)hwnd,
1255                                    (UINT)first, (UINT)last, (UINT)flags);
1256         lpmsg16_32->msg.hwnd    = msg32.hwnd;
1257         lpmsg16_32->msg.message = msg32.message;
1258         lpmsg16_32->msg.wParam  = LOWORD(msg32.wParam);
1259         lpmsg16_32->msg.lParam  = msg32.lParam;
1260         lpmsg16_32->msg.time    = msg32.time;
1261         lpmsg16_32->msg.pt.x    = (INT16)msg32.pt.x;
1262         lpmsg16_32->msg.pt.y    = (INT16)msg32.pt.y;
1263         lpmsg16_32->wParamHigh  = HIWORD(msg32.wParam);
1264         return ret;
1265     }
1266 }
1267
1268
1269 /***********************************************************************
1270  *         PeekMessageA
1271  */
1272 BOOL WINAPI PeekMessageA( LPMSG lpmsg, HWND hwnd,
1273                               UINT min,UINT max,UINT wRemoveMsg)
1274 {
1275     return MSG_PeekMessage( lpmsg, hwnd, min, max, wRemoveMsg, TRUE );
1276 }
1277
1278 /***********************************************************************
1279  *         PeekMessageW             Check queue for messages
1280  *
1281  * Checks for a message in the thread's queue, filtered as for
1282  * GetMessage(). Returns immediately whether a message is available
1283  * or not.
1284  *
1285  * Whether a retrieved message is removed from the queue is set by the
1286  * _wRemoveMsg_ flags, which should be one of the following values:
1287  *
1288  *    PM_NOREMOVE    Do not remove the message from the queue. 
1289  *
1290  *    PM_REMOVE      Remove the message from the queue.
1291  *
1292  * In addition, PM_NOYIELD may be combined into _wRemoveMsg_ to
1293  * request that the system not yield control during PeekMessage();
1294  * however applications may not rely on scheduling behavior.
1295  * 
1296  * RETURNS
1297  *
1298  *  Nonzero if a message is available and is retrieved, zero otherwise.
1299  *
1300  * CONFORMANCE
1301  *
1302  * ECMA-234, Win32
1303  *
1304  */
1305 BOOL WINAPI PeekMessageW( 
1306   LPMSG lpmsg,    /* buffer to receive message */
1307   HWND hwnd,      /* restrict to messages for hwnd */
1308   UINT min,       /* minimum message to receive */
1309   UINT max,       /* maximum message to receive */
1310   UINT wRemoveMsg /* removal flags */ 
1311
1312 {
1313         BOOL bRet = PeekMessageA(lpmsg,hwnd,min,max,wRemoveMsg);
1314         if (bRet)
1315             FIXME_(sendmsg)("(%s) unicode<->ascii\n", SPY_GetMsgName(lpmsg->message));
1316         return bRet;
1317 }
1318
1319 /***********************************************************************
1320  *           GetMessage16   (USER.108)
1321  */
1322 BOOL16 WINAPI GetMessage16( SEGPTR msg, HWND16 hwnd, UINT16 first, UINT16 last)
1323 {
1324     BOOL ret;
1325     MSG16 *lpmsg = (MSG16 *)PTR_SEG_TO_LIN(msg);
1326     MSG msg32;
1327         
1328     ret = GetMessageA( &msg32, hwnd, first, last );
1329
1330     STRUCT32_MSG32to16( &msg32, lpmsg );
1331
1332     TRACE_(msg)("message %04x, hwnd %04x, filter(%04x - %04x)\n", lpmsg->message,
1333                                                                  hwnd, first, last );
1334
1335     return ret;
1336 }
1337
1338 /***********************************************************************
1339  *          WIN16_GetMessage32   (USER.820)
1340  */
1341 BOOL16 WINAPI GetMessage32_16( SEGPTR msg16_32, HWND16 hWnd, UINT16 first,
1342                      UINT16 last, BOOL16 wHaveParamHigh )
1343 {
1344     MSG32_16 *lpmsg16_32 = (MSG32_16 *)PTR_SEG_TO_LIN(msg16_32);
1345
1346     if (wHaveParamHigh == FALSE) /* normal GetMessage16 call */
1347     {
1348
1349         lpmsg16_32->wParamHigh = 0; /* you never know... */
1350         /* WARNING: msg16_32->msg has to be the first variable in the struct */ 
1351         return GetMessage16(msg16_32, hWnd, first, last);
1352     }
1353     else
1354     {
1355         MSG msg32;
1356         BOOL16 ret;
1357
1358         ret = (BOOL16)GetMessageA(&msg32, hWnd, first, last);
1359         lpmsg16_32->msg.hwnd    = msg32.hwnd;
1360         lpmsg16_32->msg.message = msg32.message;
1361         lpmsg16_32->msg.wParam  = LOWORD(msg32.wParam);
1362         lpmsg16_32->msg.lParam  = msg32.lParam;
1363         lpmsg16_32->msg.time    = msg32.time;
1364         lpmsg16_32->msg.pt.x    = (INT16)msg32.pt.x;
1365         lpmsg16_32->msg.pt.y    = (INT16)msg32.pt.y;
1366         lpmsg16_32->wParamHigh  = HIWORD(msg32.wParam);
1367         return ret;
1368     }
1369 }
1370
1371 /***********************************************************************
1372  *          GetMessage32A   (USER32.270)
1373  */
1374 BOOL WINAPI GetMessageA(MSG* lpmsg,HWND hwnd,UINT min,UINT max)
1375 {
1376     MSG_PeekMessage( lpmsg, hwnd, min, max, PM_REMOVE, FALSE );
1377     
1378     TRACE_(msg)("message %04x, hwnd %04x, filter(%04x - %04x)\n", lpmsg->message,
1379           hwnd, min, max );
1380     
1381     HOOK_CallHooksA( WH_GETMESSAGE, HC_ACTION, 0, (LPARAM)lpmsg );
1382
1383     return (lpmsg->message != WM_QUIT);
1384 }
1385
1386 /***********************************************************************
1387  *          GetMessage32W   (USER32.274) Retrieve next message
1388  *
1389  * GetMessage retrieves the next event from the calling thread's
1390  * queue and deposits it in *lpmsg.
1391  *
1392  * If _hwnd_ is not NULL, only messages for window _hwnd_ and its
1393  * children as specified by IsChild() are retrieved. If _hwnd_ is NULL
1394  * all application messages are retrieved.
1395  *
1396  * _min_ and _max_ specify the range of messages of interest. If
1397  * min==max==0, no filtering is performed. Useful examples are
1398  * WM_KEYFIRST and WM_KEYLAST to retrieve keyboard input, and
1399  * WM_MOUSEFIRST and WM_MOUSELAST to retrieve mouse input.
1400  *
1401  * WM_PAINT messages are not removed from the queue; they remain until
1402  * processed. Other messages are removed from the queue.
1403  *
1404  * RETURNS
1405  *
1406  * -1 on error, 0 if message is WM_QUIT, nonzero otherwise.
1407  *
1408  * CONFORMANCE
1409  *
1410  * ECMA-234, Win32
1411  * 
1412  */
1413 BOOL WINAPI GetMessageW(
1414   MSG* lpmsg, /* buffer to receive message */
1415   HWND hwnd,  /* restrict to messages for hwnd */
1416   UINT min,   /* minimum message to receive */
1417   UINT max    /* maximum message to receive */
1418
1419 {
1420     BOOL bRet = GetMessageA(lpmsg, hwnd, min, max);
1421     if (bRet)
1422         FIXME_(sendmsg)("(%s) unicode<->ascii\n", SPY_GetMsgName(lpmsg->message));
1423     return bRet;
1424
1425 }
1426
1427
1428 /***********************************************************************
1429  *           PostMessage16   (USER.110)
1430  */
1431 BOOL16 WINAPI PostMessage16( HWND16 hwnd, UINT16 message, WPARAM16 wParam,
1432                              LPARAM lParam )
1433 {
1434     return (BOOL16) PostMessageA( hwnd, message, wParam, lParam );
1435 }
1436
1437
1438 /***********************************************************************
1439  *           PostMessage32A   (USER32.419)
1440  */
1441 BOOL WINAPI PostMessageA( HWND hwnd, UINT message, WPARAM wParam,
1442                               LPARAM lParam )
1443 {
1444     MSG       msg;
1445     WND         *wndPtr;
1446     BOOL      retvalue;
1447
1448     msg.hwnd    = hwnd;
1449     msg.message = message;
1450     msg.wParam  = wParam;
1451     msg.lParam  = lParam;
1452     msg.time    = GetTickCount();
1453     msg.pt.x    = 0;
1454     msg.pt.y    = 0;
1455
1456     if (hwnd == HWND_BROADCAST)
1457     {
1458         WND *pDesktop = WIN_GetDesktop();
1459         TRACE_(msg)("HWND_BROADCAST !\n");
1460         
1461         for (wndPtr=WIN_LockWndPtr(pDesktop->child); wndPtr; WIN_UpdateWndPtr(&wndPtr,wndPtr->next))
1462         {
1463             if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
1464             {
1465                 TRACE_(msg)("BROADCAST Message to hWnd=%04x m=%04X w=%04X l=%08lX !\n",
1466                             wndPtr->hwndSelf, message, wParam, lParam);
1467                 PostMessageA( wndPtr->hwndSelf, message, wParam, lParam );
1468             }
1469         }
1470         WIN_ReleaseDesktop();
1471         TRACE_(msg)("End of HWND_BROADCAST !\n");
1472         return TRUE;
1473     }
1474
1475     wndPtr = WIN_FindWndPtr( hwnd );
1476     if (!wndPtr || !wndPtr->hmemTaskQ)
1477     {
1478         retvalue = FALSE;
1479         goto END;
1480     }
1481
1482     retvalue = QUEUE_AddMsg( wndPtr->hmemTaskQ, &msg, 0 );
1483 END:
1484     WIN_ReleaseWndPtr(wndPtr);
1485     return retvalue;
1486 }
1487
1488
1489 /***********************************************************************
1490  *           PostMessage32W   (USER32.420)
1491  */
1492 BOOL WINAPI PostMessageW( HWND hwnd, UINT message, WPARAM wParam,
1493                               LPARAM lParam )
1494 {
1495   FIXME_(sendmsg)("(%s) unicode<->ascii\n", SPY_GetMsgName(message));
1496   return PostMessageA( hwnd, message, wParam, lParam );
1497 }
1498
1499
1500 /***********************************************************************
1501  *           PostAppMessage16   (USER.116)
1502  */
1503 BOOL16 WINAPI PostAppMessage16( HTASK16 hTask, UINT16 message, WPARAM16 wParam,
1504                                 LPARAM lParam )
1505 {
1506     MSG msg;
1507
1508     if (GetTaskQueue16(hTask) == 0) return FALSE;
1509     msg.hwnd    = 0;
1510     msg.message = message;
1511     msg.wParam  = wParam;
1512     msg.lParam  = lParam;
1513     msg.time    = GetTickCount();
1514     msg.pt.x    = 0;
1515     msg.pt.y    = 0;
1516
1517     return QUEUE_AddMsg( GetTaskQueue16(hTask), &msg, 0 );
1518 }
1519
1520 /************************************************************************
1521  *           MSG_CallWndProcHook32
1522  */
1523 static void  MSG_CallWndProcHook( LPMSG pmsg, BOOL bUnicode )
1524 {
1525    CWPSTRUCT cwp;
1526
1527    cwp.lParam = pmsg->lParam;
1528    cwp.wParam = pmsg->wParam;
1529    cwp.message = pmsg->message;
1530    cwp.hwnd = pmsg->hwnd;
1531
1532    if (bUnicode) HOOK_CallHooksW(WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp);
1533    else HOOK_CallHooksA( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1534
1535    pmsg->lParam = cwp.lParam;
1536    pmsg->wParam = cwp.wParam;
1537    pmsg->message = cwp.message;
1538    pmsg->hwnd = cwp.hwnd;
1539 }
1540
1541
1542 /***********************************************************************
1543  *           MSG_SendMessage
1544  *
1545  * return values: 0 if timeout occurs
1546  *                1 otherwise
1547  */
1548 static LRESULT MSG_SendMessage( HWND hwnd, UINT msg, WPARAM wParam,
1549                          LPARAM lParam, DWORD timeout, WORD flags,
1550                          LRESULT *pRes)
1551 {
1552     WND * wndPtr = 0;
1553     WND **list, **ppWnd;
1554     LRESULT ret = 1;
1555
1556     *pRes = 0;
1557
1558     if (hwnd == HWND_BROADCAST|| hwnd == HWND_TOPMOST)
1559     {
1560         *pRes = 1;
1561         
1562         if (!(list = WIN_BuildWinArray( WIN_GetDesktop(), 0, NULL )))
1563         {
1564             WIN_ReleaseDesktop();
1565             return 1;
1566         }
1567         WIN_ReleaseDesktop();
1568
1569         TRACE_(msg)("HWND_BROADCAST !\n");
1570         for (ppWnd = list; *ppWnd; ppWnd++)
1571         {
1572             WIN_UpdateWndPtr(&wndPtr,*ppWnd);
1573             if (!IsWindow(wndPtr->hwndSelf)) continue;
1574             if (wndPtr->dwStyle & WS_POPUP || wndPtr->dwStyle & WS_CAPTION)
1575             {
1576                 TRACE_(msg)("BROADCAST Message to hWnd=%04x m=%04X w=%04lX l=%08lX !\n",
1577                             wndPtr->hwndSelf, msg, (DWORD)wParam, lParam);
1578                 MSG_SendMessage( wndPtr->hwndSelf, msg, wParam, lParam,
1579                                timeout, flags, pRes);
1580             }
1581         }
1582         WIN_ReleaseWndPtr(wndPtr);
1583         WIN_ReleaseWinArray(list);
1584         TRACE_(msg)("End of HWND_BROADCAST !\n");
1585         return 1;
1586     }
1587
1588     if (HOOK_IsHooked( WH_CALLWNDPROC ))
1589     {
1590         if (flags & SMSG_UNICODE)
1591             MSG_CallWndProcHook( (LPMSG)&hwnd, TRUE);
1592         else if (flags & SMSG_WIN32)
1593             MSG_CallWndProcHook( (LPMSG)&hwnd, FALSE);
1594         else
1595         {
1596         LPCWPSTRUCT16 pmsg;
1597
1598         if ((pmsg = SEGPTR_NEW(CWPSTRUCT16)))
1599         {
1600                 pmsg->hwnd   = hwnd & 0xffff;
1601                 pmsg->message= msg & 0xffff;
1602                 pmsg->wParam = wParam & 0xffff;
1603             pmsg->lParam = lParam;
1604             HOOK_CallHooks16( WH_CALLWNDPROC, HC_ACTION, 1,
1605                               (LPARAM)SEGPTR_GET(pmsg) );
1606             hwnd   = pmsg->hwnd;
1607             msg    = pmsg->message;
1608             wParam = pmsg->wParam;
1609             lParam = pmsg->lParam;
1610             SEGPTR_FREE( pmsg );
1611         }
1612     }
1613     }
1614
1615     if (!(wndPtr = WIN_FindWndPtr( hwnd )))
1616     {
1617         WARN_(msg)("invalid hwnd %04x\n", hwnd );
1618         return 0;
1619     }
1620     if (QUEUE_IsExitingQueue(wndPtr->hmemTaskQ))
1621     {
1622         ret = 0;  /* Don't send anything if the task is dying */
1623         goto END;
1624     }
1625     if (flags & SMSG_WIN32)
1626         SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wParam, lParam );
1627     else
1628     SPY_EnterMessage( SPY_SENDMESSAGE16, hwnd, msg, wParam, lParam );
1629
1630     if (wndPtr->hmemTaskQ != GetFastQueue16())
1631         ret = MSG_SendMessageInterThread( wndPtr->hmemTaskQ, hwnd, msg,
1632                                           wParam, lParam, timeout, flags, pRes );
1633     else
1634     {
1635         /* Call the right CallWindowProc flavor */
1636         if (flags & SMSG_UNICODE)
1637             *pRes = CallWindowProcW( (WNDPROC)wndPtr->winproc,
1638                                      hwnd, msg, wParam, lParam );
1639         else if (flags & SMSG_WIN32)
1640             *pRes = CallWindowProcA( (WNDPROC)wndPtr->winproc,
1641                                 hwnd, msg, wParam, lParam );
1642         else
1643             *pRes = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
1644                                     (HWND16) hwnd, (UINT16) msg,
1645                                     (WPARAM16) wParam, lParam );
1646     }
1647
1648     if (flags & SMSG_WIN32)
1649         SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, ret );
1650     else
1651     SPY_ExitMessage( SPY_RESULT_OK16, hwnd, msg, ret );
1652 END:
1653     WIN_ReleaseWndPtr(wndPtr);
1654     return ret;
1655 }
1656
1657
1658 /***********************************************************************
1659  *           SendMessage16   (USER.111)
1660  */
1661 LRESULT WINAPI SendMessage16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
1662                               LPARAM lParam)
1663 {
1664     LRESULT res;
1665     MSG_SendMessage(hwnd, msg, wParam, lParam, INFINITE, 0, &res);
1666     return res;
1667 }
1668
1669
1670
1671 /**********************************************************************
1672  *           PostThreadMessage32A    (USER32.422)
1673  *
1674  * BUGS
1675  *
1676  *  Thread-local message queues are not supported.
1677  * 
1678  */
1679 BOOL WINAPI PostThreadMessageA(DWORD idThread , UINT message,
1680                                    WPARAM wParam, LPARAM lParam )
1681 {
1682     MSG msg;
1683     HQUEUE16 hQueue;
1684
1685     if ((hQueue = GetThreadQueue16(idThread)) == 0)
1686         return FALSE;
1687     
1688     msg.hwnd    = 0;
1689     msg.message = message;
1690     msg.wParam  = wParam;
1691     msg.lParam  = lParam;
1692     msg.time    = GetTickCount();
1693     msg.pt.x    = 0;
1694     msg.pt.y    = 0;
1695
1696     return QUEUE_AddMsg( hQueue, &msg, 0 );
1697 }
1698
1699 /**********************************************************************
1700  *           PostThreadMessage32W    (USER32.423)
1701  *
1702  * BUGS
1703  *
1704  *  Thread-local message queues are not supported.
1705  * 
1706  */
1707 BOOL WINAPI PostThreadMessageW(DWORD idThread , UINT message,
1708                                    WPARAM wParam, LPARAM lParam )
1709 {
1710    FIXME_(sendmsg)("(%s) unicode<->ascii\n",SPY_GetMsgName(message));
1711    return PostThreadMessageA(idThread, message, wParam, lParam);
1712 }
1713
1714 /***********************************************************************
1715  *           SendMessage32A   (USER32.454)
1716  */
1717 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wParam,
1718                                LPARAM lParam )
1719         {
1720     LRESULT res;
1721
1722     MSG_SendMessage(hwnd, msg, wParam, lParam, INFINITE,
1723                     SMSG_WIN32, &res);
1724
1725     return res;
1726 }
1727
1728
1729 /***********************************************************************
1730  *           SendMessage32W   (USER32.459)  Send Window Message
1731  *
1732  *  Sends a message to the window procedure of the specified window.
1733  *  SendMessage() will not return until the called window procedure
1734  *  either returns or calls ReplyMessage().
1735  *
1736  *  Use PostMessage() to send message and return immediately. A window
1737  *  procedure may use InSendMessage() to detect
1738  *  SendMessage()-originated messages.
1739  *
1740  *  Applications which communicate via HWND_BROADCAST may use
1741  *  RegisterWindowMessage() to obtain a unique message to avoid conflicts
1742  *  with other applications.
1743  *
1744  * CONFORMANCE
1745  * 
1746  *  ECMA-234, Win32 
1747  */
1748 LRESULT WINAPI SendMessageW( 
1749   HWND hwnd,    /* Window to send message to. If HWND_BROADCAST, 
1750                  the message will be sent to all top-level windows. */
1751
1752   UINT msg,      /* message */
1753   WPARAM wParam, /* message parameter */
1754   LPARAM lParam    /* additional message parameter */
1755 ) {
1756     LRESULT res;
1757
1758     MSG_SendMessage(hwnd, msg, wParam, lParam, INFINITE,
1759                     SMSG_WIN32 | SMSG_UNICODE, &res);
1760
1761     return res;
1762 }
1763
1764
1765 /***********************************************************************
1766  *           SendMessageTimeout16    (not a WINAPI)
1767  */
1768 LRESULT WINAPI SendMessageTimeout16( HWND16 hwnd, UINT16 msg, WPARAM16 wParam,
1769                                      LPARAM lParam, UINT16 flags,
1770                                      UINT16 timeout, LPWORD resultp)
1771 {
1772     LRESULT ret;
1773     LRESULT msgRet;
1774     
1775     /* FIXME: need support for SMTO_BLOCK */
1776     
1777     ret = MSG_SendMessage(hwnd, msg, wParam, lParam, timeout, 0, &msgRet);
1778     *resultp = (WORD) msgRet;
1779     return ret;
1780 }
1781
1782
1783 /***********************************************************************
1784  *           SendMessageTimeoutA   (USER32.457)
1785  */
1786 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wParam,
1787                                       LPARAM lParam, UINT flags,
1788                                       UINT timeout, LPDWORD resultp)
1789 {
1790     LRESULT ret;
1791     LRESULT msgRet;
1792
1793     /* FIXME: need support for SMTO_BLOCK */
1794     
1795     ret = MSG_SendMessage(hwnd, msg, wParam, lParam, timeout, SMSG_WIN32,
1796                           &msgRet);
1797
1798     *resultp = (DWORD) msgRet;
1799     return ret;
1800 }
1801
1802
1803 /***********************************************************************
1804  *           SendMessageTimeoutW   (USER32.458)
1805  */
1806 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wParam,
1807                                       LPARAM lParam, UINT flags,
1808                                       UINT timeout, LPDWORD resultp)
1809 {
1810     LRESULT ret;
1811     LRESULT msgRet;
1812     
1813     /* FIXME: need support for SMTO_BLOCK */
1814
1815     ret = MSG_SendMessage(hwnd, msg, wParam, lParam, timeout,
1816                           SMSG_WIN32 | SMSG_UNICODE, &msgRet);
1817     
1818     *resultp = (DWORD) msgRet;
1819     return ret;
1820 }
1821
1822
1823 /***********************************************************************
1824  *  WaitMessage    (USER.112) (USER32.578)  Suspend thread pending messages
1825  *
1826  * WaitMessage() suspends a thread until events appear in the thread's
1827  * queue.
1828  *
1829  * BUGS
1830  *
1831  * Is supposed to return BOOL under Win32.
1832  *
1833  * Thread-local message queues are not supported.
1834  *
1835  * CONFORMANCE
1836  *
1837  * ECMA-234, Win32
1838  * 
1839  */
1840 void WINAPI WaitMessage( void )
1841 {
1842     QUEUE_WaitBits( QS_ALLINPUT, INFINITE );
1843 }
1844
1845 /***********************************************************************
1846  *           MsgWaitForMultipleObjects    (USER32.400)
1847  */
1848 DWORD WINAPI MsgWaitForMultipleObjects( DWORD nCount, HANDLE *pHandles,
1849                                         BOOL fWaitAll, DWORD dwMilliseconds,
1850                                         DWORD dwWakeMask )
1851 {
1852     DWORD i;
1853     HANDLE handles[MAXIMUM_WAIT_OBJECTS];
1854     DWORD ret;
1855
1856     HQUEUE16 hQueue = GetFastQueue16();
1857     MESSAGEQUEUE *msgQueue = (MESSAGEQUEUE *)QUEUE_Lock( hQueue );
1858     if (!msgQueue) return WAIT_FAILED;
1859
1860     if (nCount > MAXIMUM_WAIT_OBJECTS-1)
1861     {
1862         SetLastError( ERROR_INVALID_PARAMETER );
1863         QUEUE_Unlock( msgQueue );
1864         return WAIT_FAILED;
1865     }
1866
1867     msgQueue->changeBits = 0;
1868     msgQueue->wakeMask = dwWakeMask;
1869
1870     if (THREAD_IsWin16(NtCurrentTeb()))
1871     {
1872       /*
1873        * This is a temporary solution to a big problem.
1874        * You see, the main thread of all Win32 programs is created as a 16 bit
1875        * task. This means that if you want on an event using Win32 synchronization
1876        * methods, the 16 bit scheduler is stopped and things might just stop happening.
1877        * This implements a semi-busy loop that checks the handles to wait on and
1878        * also the message queue. When either one is ready, the wait function returns.
1879        *
1880        * This will all go away when the real Win32 threads are implemented for all
1881        * the threads of an applications. Including the main thread.
1882        */
1883       DWORD curTime = GetCurrentTime();
1884
1885       do
1886       {
1887         /*
1888          * Check the handles in the list.
1889          */
1890         ret = WaitForMultipleObjects(nCount, pHandles, fWaitAll, 5L);
1891
1892         /*
1893          * If the handles have been triggered, return.
1894          */
1895         if (ret != WAIT_TIMEOUT)
1896           break;
1897
1898         /*
1899          * Then, let the 16 bit scheduler do it's thing.
1900          */
1901         Yield16();
1902
1903         /*
1904          * If a message matching the wait mask has arrived, return.
1905          */
1906         if (msgQueue->changeBits & dwWakeMask)
1907         {
1908           ret = nCount;
1909           break;
1910         }
1911
1912         /*
1913          * And continue doing this until we hit the timeout.
1914          */
1915       } while ((dwMilliseconds == INFINITE) || (GetCurrentTime()-curTime < dwMilliseconds) );
1916     }
1917     else
1918     {
1919     /* Add the thread event to the handle list */
1920       for (i = 0; i < nCount; i++)
1921         handles[i] = pHandles[i];
1922     handles[nCount] = msgQueue->hEvent;
1923
1924     ret = WaitForMultipleObjects( nCount+1, handles, fWaitAll, dwMilliseconds );
1925     } 
1926
1927     QUEUE_Unlock( msgQueue );
1928     
1929     return ret;
1930 }
1931
1932
1933
1934 struct accent_char
1935 {
1936     BYTE ac_accent;
1937     BYTE ac_char;
1938     BYTE ac_result;
1939 };
1940
1941 static const struct accent_char accent_chars[] =
1942 {
1943 /* A good idea should be to read /usr/X11/lib/X11/locale/iso8859-x/Compose */
1944     {'`', 'A', '\300'},  {'`', 'a', '\340'},
1945     {'\'', 'A', '\301'}, {'\'', 'a', '\341'},
1946     {'^', 'A', '\302'},  {'^', 'a', '\342'},
1947     {'~', 'A', '\303'},  {'~', 'a', '\343'},
1948     {'"', 'A', '\304'},  {'"', 'a', '\344'},
1949     {'O', 'A', '\305'},  {'o', 'a', '\345'},
1950     {'0', 'A', '\305'},  {'0', 'a', '\345'},
1951     {'A', 'A', '\305'},  {'a', 'a', '\345'},
1952     {'A', 'E', '\306'},  {'a', 'e', '\346'},
1953     {',', 'C', '\307'},  {',', 'c', '\347'},
1954     {'`', 'E', '\310'},  {'`', 'e', '\350'},
1955     {'\'', 'E', '\311'}, {'\'', 'e', '\351'},
1956     {'^', 'E', '\312'},  {'^', 'e', '\352'},
1957     {'"', 'E', '\313'},  {'"', 'e', '\353'},
1958     {'`', 'I', '\314'},  {'`', 'i', '\354'},
1959     {'\'', 'I', '\315'}, {'\'', 'i', '\355'},
1960     {'^', 'I', '\316'},  {'^', 'i', '\356'},
1961     {'"', 'I', '\317'},  {'"', 'i', '\357'},
1962     {'-', 'D', '\320'},  {'-', 'd', '\360'},
1963     {'~', 'N', '\321'},  {'~', 'n', '\361'},
1964     {'`', 'O', '\322'},  {'`', 'o', '\362'},
1965     {'\'', 'O', '\323'}, {'\'', 'o', '\363'},
1966     {'^', 'O', '\324'},  {'^', 'o', '\364'},
1967     {'~', 'O', '\325'},  {'~', 'o', '\365'},
1968     {'"', 'O', '\326'},  {'"', 'o', '\366'},
1969     {'/', 'O', '\330'},  {'/', 'o', '\370'},
1970     {'`', 'U', '\331'},  {'`', 'u', '\371'},
1971     {'\'', 'U', '\332'}, {'\'', 'u', '\372'},
1972     {'^', 'U', '\333'},  {'^', 'u', '\373'},
1973     {'"', 'U', '\334'},  {'"', 'u', '\374'},
1974     {'\'', 'Y', '\335'}, {'\'', 'y', '\375'},
1975     {'T', 'H', '\336'},  {'t', 'h', '\376'},
1976     {'s', 's', '\337'},  {'"', 'y', '\377'},
1977     {'s', 'z', '\337'},  {'i', 'j', '\377'},
1978         /* iso-8859-2 uses this */
1979     {'<', 'L', '\245'},  {'<', 'l', '\265'},    /* caron */
1980     {'<', 'S', '\251'},  {'<', 's', '\271'},
1981     {'<', 'T', '\253'},  {'<', 't', '\273'},
1982     {'<', 'Z', '\256'},  {'<', 'z', '\276'},
1983     {'<', 'C', '\310'},  {'<', 'c', '\350'},
1984     {'<', 'E', '\314'},  {'<', 'e', '\354'},
1985     {'<', 'D', '\317'},  {'<', 'd', '\357'},
1986     {'<', 'N', '\322'},  {'<', 'n', '\362'},
1987     {'<', 'R', '\330'},  {'<', 'r', '\370'},
1988     {';', 'A', '\241'},  {';', 'a', '\261'},    /* ogonek */
1989     {';', 'E', '\312'},  {';', 'e', '\332'},
1990     {'\'', 'Z', '\254'}, {'\'', 'z', '\274'},   /* acute */
1991     {'\'', 'R', '\300'}, {'\'', 'r', '\340'},
1992     {'\'', 'L', '\305'}, {'\'', 'l', '\345'},
1993     {'\'', 'C', '\306'}, {'\'', 'c', '\346'},
1994     {'\'', 'N', '\321'}, {'\'', 'n', '\361'},
1995 /*  collision whith S, from iso-8859-9 !!! */
1996     {',', 'S', '\252'},  {',', 's', '\272'},    /* cedilla */
1997     {',', 'T', '\336'},  {',', 't', '\376'},
1998     {'.', 'Z', '\257'},  {'.', 'z', '\277'},    /* dot above */
1999     {'/', 'L', '\243'},  {'/', 'l', '\263'},    /* slash */
2000     {'/', 'D', '\320'},  {'/', 'd', '\360'},
2001     {'(', 'A', '\303'},  {'(', 'a', '\343'},    /* breve */
2002     {'\275', 'O', '\325'}, {'\275', 'o', '\365'},       /* double acute */
2003     {'\275', 'U', '\334'}, {'\275', 'u', '\374'},
2004     {'0', 'U', '\332'},  {'0', 'u', '\372'},    /* ring above */
2005         /* iso-8859-3 uses this */
2006     {'/', 'H', '\241'},  {'/', 'h', '\261'},    /* slash */
2007     {'>', 'H', '\246'},  {'>', 'h', '\266'},    /* circumflex */
2008     {'>', 'J', '\254'},  {'>', 'j', '\274'},
2009     {'>', 'C', '\306'},  {'>', 'c', '\346'},
2010     {'>', 'G', '\330'},  {'>', 'g', '\370'},
2011     {'>', 'S', '\336'},  {'>', 's', '\376'},
2012 /*  collision whith G( from iso-8859-9 !!!   */
2013     {'(', 'G', '\253'},  {'(', 'g', '\273'},    /* breve */
2014     {'(', 'U', '\335'},  {'(', 'u', '\375'},
2015 /*  collision whith I. from iso-8859-3 !!!   */
2016     {'.', 'I', '\251'},  {'.', 'i', '\271'},    /* dot above */
2017     {'.', 'C', '\305'},  {'.', 'c', '\345'},
2018     {'.', 'G', '\325'},  {'.', 'g', '\365'},
2019         /* iso-8859-4 uses this */
2020     {',', 'R', '\243'},  {',', 'r', '\263'},    /* cedilla */
2021     {',', 'L', '\246'},  {',', 'l', '\266'},
2022     {',', 'G', '\253'},  {',', 'g', '\273'},
2023     {',', 'N', '\321'},  {',', 'n', '\361'},
2024     {',', 'K', '\323'},  {',', 'k', '\363'},
2025     {'~', 'I', '\245'},  {'~', 'i', '\265'},    /* tilde */
2026     {'-', 'E', '\252'},  {'-', 'e', '\272'},    /* macron */
2027     {'-', 'A', '\300'},  {'-', 'a', '\340'},
2028     {'-', 'I', '\317'},  {'-', 'i', '\357'},
2029     {'-', 'O', '\322'},  {'-', 'o', '\362'},
2030     {'-', 'U', '\336'},  {'-', 'u', '\376'},
2031     {'/', 'T', '\254'},  {'/', 't', '\274'},    /* slash */
2032     {'.', 'E', '\314'},  {'.', 'e', '\344'},    /* dot above */
2033     {';', 'I', '\307'},  {';', 'i', '\347'},    /* ogonek */
2034     {';', 'U', '\331'},  {';', 'u', '\371'},
2035         /* iso-8859-9 uses this */
2036         /* iso-8859-9 has really bad choosen G( S, and I. as they collide
2037          * whith the same letters on other iso-8859-x (that is they are on
2038          * different places :-( ), if you use turkish uncomment these and
2039          * comment out the lines in iso-8859-2 and iso-8859-3 sections
2040          * FIXME: should be dynamic according to chosen language
2041          *        if/when Wine has turkish support.  
2042          */ 
2043 /*  collision whith G( from iso-8859-3 !!!   */
2044 /*  {'(', 'G', '\320'},  {'(', 'g', '\360'}, */ /* breve */
2045 /*  collision whith S, from iso-8859-2 !!! */
2046 /*  {',', 'S', '\336'},  {',', 's', '\376'}, */ /* cedilla */
2047 /*  collision whith I. from iso-8859-3 !!!   */
2048 /*  {'.', 'I', '\335'},  {'.', 'i', '\375'}, */ /* dot above */
2049 };
2050
2051
2052 /***********************************************************************
2053  *           MSG_DoTranslateMessage
2054  *
2055  * Implementation of TranslateMessage.
2056  *
2057  * TranslateMessage translates virtual-key messages into character-messages,
2058  * as follows :
2059  * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
2060  * ditto replacing WM_* with WM_SYS*
2061  * This produces WM_CHAR messages only for keys mapped to ASCII characters
2062  * by the keyboard driver.
2063  */
2064 static BOOL MSG_DoTranslateMessage( UINT message, HWND hwnd,
2065                                       WPARAM wParam, LPARAM lParam )
2066 {
2067     static int dead_char;
2068     BYTE wp[2];
2069     
2070     if (message != WM_MOUSEMOVE && message != WM_TIMER)
2071         TRACE_(msg)("(%s, %04X, %08lX)\n",
2072                      SPY_GetMsgName(message), wParam, lParam );
2073     if(message >= WM_KEYFIRST && message <= WM_KEYLAST)
2074         TRACE_(key)("(%s, %04X, %08lX)\n",
2075                      SPY_GetMsgName(message), wParam, lParam );
2076
2077     if ((message != WM_KEYDOWN) && (message != WM_SYSKEYDOWN))  return FALSE;
2078
2079     TRACE_(key)("Translating key %04X, scancode %04X\n",
2080                  wParam, HIWORD(lParam) );
2081
2082     /* FIXME : should handle ToAscii yielding 2 */
2083     switch (ToAscii(wParam, HIWORD(lParam),
2084                       QueueKeyStateTable,(LPWORD)wp, 0)) 
2085     {
2086     case 1 :
2087         message = (message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
2088         /* Should dead chars handling go in ToAscii ? */
2089         if (dead_char)
2090         {
2091             int i;
2092
2093             if (wp[0] == ' ') wp[0] =  dead_char;
2094             if (dead_char == 0xa2) dead_char = '(';
2095             else if (dead_char == 0xa8) dead_char = '"';
2096             else if (dead_char == 0xb2) dead_char = ';';
2097             else if (dead_char == 0xb4) dead_char = '\'';
2098             else if (dead_char == 0xb7) dead_char = '<';
2099             else if (dead_char == 0xb8) dead_char = ',';
2100             else if (dead_char == 0xff) dead_char = '.';
2101             for (i = 0; i < sizeof(accent_chars)/sizeof(accent_chars[0]); i++)
2102                 if ((accent_chars[i].ac_accent == dead_char) &&
2103                     (accent_chars[i].ac_char == wp[0]))
2104                 {
2105                     wp[0] = accent_chars[i].ac_result;
2106                     break;
2107                 }
2108             dead_char = 0;
2109         }
2110         TRACE_(key)("1 -> PostMessage(%s)\n", SPY_GetMsgName(message));
2111         PostMessage16( hwnd, message, wp[0], lParam );
2112         return TRUE;
2113
2114     case -1 :
2115         message = (message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
2116         dead_char = wp[0];
2117         TRACE_(key)("-1 -> PostMessage(%s)\n",
2118                      SPY_GetMsgName(message));
2119         PostMessage16( hwnd, message, wp[0], lParam );
2120         return TRUE;
2121     }
2122     return FALSE;
2123 }
2124
2125
2126 /***********************************************************************
2127  *           TranslateMessage16   (USER.113)
2128  */
2129 BOOL16 WINAPI TranslateMessage16( const MSG16 *msg )
2130 {
2131     return MSG_DoTranslateMessage( msg->message, msg->hwnd,
2132                                    msg->wParam, msg->lParam );
2133 }
2134
2135
2136 /***********************************************************************
2137  *           WIN16_TranslateMessage32   (USER.821)
2138  */
2139 BOOL16 WINAPI TranslateMessage32_16( const MSG32_16 *msg, BOOL16 wHaveParamHigh )
2140 {
2141     WPARAM wParam;
2142
2143     if (wHaveParamHigh)
2144         wParam = MAKELONG(msg->msg.wParam, msg->wParamHigh);
2145     else
2146         wParam = (WPARAM)msg->msg.wParam;
2147
2148     return MSG_DoTranslateMessage( msg->msg.message, msg->msg.hwnd,
2149                                    wParam, msg->msg.lParam );
2150 }
2151
2152 /***********************************************************************
2153  *           TranslateMessage32   (USER32.556)
2154  */
2155 BOOL WINAPI TranslateMessage( const MSG *msg )
2156 {
2157     return MSG_DoTranslateMessage( msg->message, msg->hwnd,
2158                                    msg->wParam, msg->lParam );
2159 }
2160
2161
2162 /***********************************************************************
2163  *           DispatchMessage16   (USER.114)
2164  */
2165 LONG WINAPI DispatchMessage16( const MSG16* msg )
2166 {
2167     WND * wndPtr;
2168     LONG retval;
2169     int painting;
2170     
2171       /* Process timer messages */
2172     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
2173     {
2174         if (msg->lParam)
2175         {
2176             return CallWindowProc16( (WNDPROC16)msg->lParam, msg->hwnd,
2177                                    msg->message, msg->wParam, GetTickCount() );
2178         }
2179     }
2180
2181     if (!msg->hwnd) return 0;
2182     if (!(wndPtr = WIN_FindWndPtr( msg->hwnd ))) return 0;
2183     if (!wndPtr->winproc)
2184     {
2185         retval = 0;
2186         goto END;
2187     }
2188     painting = (msg->message == WM_PAINT);
2189     if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
2190
2191     SPY_EnterMessage( SPY_DISPATCHMESSAGE16, msg->hwnd, msg->message,
2192                       msg->wParam, msg->lParam );
2193     retval = CallWindowProc16( (WNDPROC16)wndPtr->winproc,
2194                                msg->hwnd, msg->message,
2195                                msg->wParam, msg->lParam );
2196     SPY_ExitMessage( SPY_RESULT_OK16, msg->hwnd, msg->message, retval );
2197
2198     WIN_ReleaseWndPtr(wndPtr);
2199     wndPtr = WIN_FindWndPtr(msg->hwnd);
2200     if (painting && wndPtr &&
2201         (wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate)
2202     {
2203         ERR_(msg)("BeginPaint not called on WM_PAINT for hwnd %04x!\n", 
2204             msg->hwnd);
2205         wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
2206         /* Validate the update region to avoid infinite WM_PAINT loop */
2207         ValidateRect( msg->hwnd, NULL );
2208     }
2209 END:
2210     WIN_ReleaseWndPtr(wndPtr);
2211     return retval;
2212 }
2213
2214
2215 /***********************************************************************
2216  *           WIN16_DispatchMessage32   (USER.822)
2217  */
2218 LONG WINAPI DispatchMessage32_16( const MSG32_16* lpmsg16_32, BOOL16 wHaveParamHigh )
2219 {
2220     if (wHaveParamHigh == FALSE)
2221         return DispatchMessage16(&(lpmsg16_32->msg));
2222     else
2223     {
2224         MSG msg;
2225
2226         msg.hwnd = lpmsg16_32->msg.hwnd;
2227         msg.message = lpmsg16_32->msg.message;
2228         msg.wParam = MAKELONG(lpmsg16_32->msg.wParam, lpmsg16_32->wParamHigh);
2229         msg.lParam = lpmsg16_32->msg.lParam;
2230         msg.time = lpmsg16_32->msg.time;
2231         msg.pt.x = (INT)lpmsg16_32->msg.pt.x;
2232         msg.pt.y = (INT)lpmsg16_32->msg.pt.y;
2233         return DispatchMessageA(&msg);
2234     }
2235 }
2236
2237 /***********************************************************************
2238  *           DispatchMessage32A   (USER32.141)
2239  */
2240 LONG WINAPI DispatchMessageA( const MSG* msg )
2241 {
2242     WND * wndPtr;
2243     LONG retval;
2244     int painting;
2245     
2246       /* Process timer messages */
2247     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
2248     {
2249         if (msg->lParam)
2250         {
2251 /*            HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
2252             return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
2253                                    msg->message, msg->wParam, GetTickCount() );
2254         }
2255     }
2256
2257     if (!msg->hwnd) return 0;
2258     if (!(wndPtr = WIN_FindWndPtr( msg->hwnd ))) return 0;
2259     if (!wndPtr->winproc)
2260     {
2261         retval = 0;
2262         goto END;
2263     }
2264     painting = (msg->message == WM_PAINT);
2265     if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
2266 /*    HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
2267
2268     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
2269                       msg->wParam, msg->lParam );
2270     retval = CallWindowProcA( (WNDPROC)wndPtr->winproc,
2271                                 msg->hwnd, msg->message,
2272                                 msg->wParam, msg->lParam );
2273     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval );
2274
2275     WIN_ReleaseWndPtr(wndPtr);
2276     wndPtr = WIN_FindWndPtr(msg->hwnd);
2277
2278     if (painting && wndPtr &&
2279         (wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate)
2280     {
2281         ERR_(msg)("BeginPaint not called on WM_PAINT for hwnd %04x!\n", 
2282             msg->hwnd);
2283         wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
2284         /* Validate the update region to avoid infinite WM_PAINT loop */
2285         ValidateRect( msg->hwnd, NULL );
2286     }
2287 END:
2288     WIN_ReleaseWndPtr(wndPtr);
2289     return retval;
2290 }
2291
2292
2293 /***********************************************************************
2294  *           DispatchMessage32W   (USER32.142)     Process Message
2295  *
2296  * Process the message specified in the structure *_msg_.
2297  *
2298  * If the lpMsg parameter points to a WM_TIMER message and the
2299  * parameter of the WM_TIMER message is not NULL, the lParam parameter
2300  * points to the function that is called instead of the window
2301  * procedure.
2302  *  
2303  * The message must be valid.
2304  *
2305  * RETURNS
2306  *
2307  *   DispatchMessage() returns the result of the window procedure invoked.
2308  *
2309  * CONFORMANCE
2310  *
2311  *   ECMA-234, Win32 
2312  *
2313  */
2314 LONG WINAPI DispatchMessageW( const MSG* msg )
2315 {
2316     WND * wndPtr;
2317     LONG retval;
2318     int painting;
2319     
2320       /* Process timer messages */
2321     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
2322     {
2323         if (msg->lParam)
2324         {
2325 /*            HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
2326             return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
2327                                    msg->message, msg->wParam, GetTickCount() );
2328         }
2329     }
2330
2331     if (!msg->hwnd) return 0;
2332     if (!(wndPtr = WIN_FindWndPtr( msg->hwnd ))) return 0;
2333     if (!wndPtr->winproc)
2334     {
2335         retval = 0;
2336         goto END;
2337     }
2338     painting = (msg->message == WM_PAINT);
2339     if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
2340 /*    HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
2341
2342     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
2343                       msg->wParam, msg->lParam );
2344     retval = CallWindowProcW( (WNDPROC)wndPtr->winproc,
2345                                 msg->hwnd, msg->message,
2346                                 msg->wParam, msg->lParam );
2347     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval );
2348
2349     WIN_ReleaseWndPtr(wndPtr);
2350     wndPtr = WIN_FindWndPtr(msg->hwnd);
2351
2352     if (painting && wndPtr &&
2353         (wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate)
2354     {
2355         ERR_(msg)("BeginPaint not called on WM_PAINT for hwnd %04x!\n", 
2356             msg->hwnd);
2357         wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
2358         /* Validate the update region to avoid infinite WM_PAINT loop */
2359         ValidateRect( msg->hwnd, NULL );
2360     }
2361 END:
2362     WIN_ReleaseWndPtr(wndPtr);
2363     return retval;
2364 }
2365
2366
2367 /***********************************************************************
2368  *           RegisterWindowMessage16   (USER.118)
2369  */
2370 WORD WINAPI RegisterWindowMessage16( SEGPTR str )
2371 {
2372     TRACE_(msg)("%08lx\n", (DWORD)str );
2373     return GlobalAddAtom16( str );
2374 }
2375
2376
2377 /***********************************************************************
2378  *           RegisterWindowMessage32A   (USER32.437)
2379  */
2380 WORD WINAPI RegisterWindowMessageA( LPCSTR str )
2381 {
2382     TRACE_(msg)("%s\n", str );
2383     return GlobalAddAtomA( str );
2384 }
2385
2386
2387 /***********************************************************************
2388  *           RegisterWindowMessage32W   (USER32.438)
2389  */
2390 WORD WINAPI RegisterWindowMessageW( LPCWSTR str )
2391 {
2392     TRACE_(msg)("%p\n", str );
2393     return GlobalAddAtomW( str );
2394 }
2395
2396
2397 /***********************************************************************
2398  *           GetTickCount   (USER.13) (KERNEL32.299)  System Time
2399  * Returns the number of milliseconds, modulo 2^32, since the start
2400  * of the current session.
2401  *
2402  * CONFORMANCE
2403  *
2404  * ECMA-234, Win32
2405  */
2406 DWORD WINAPI GetTickCount(void)
2407 {
2408     struct timeval t;
2409     gettimeofday( &t, NULL );
2410     /* make extremely compatible: granularity is 25 msec */
2411     return ((t.tv_sec * 1000) + (t.tv_usec / 25000) * 25) - MSG_WineStartTicks;
2412 }
2413
2414
2415 /***********************************************************************
2416  *           GetCurrentTime16    (USER.15)
2417  *
2418  * (effectively identical to GetTickCount)
2419  */
2420 DWORD WINAPI GetCurrentTime16(void)
2421 {
2422     return GetTickCount();
2423 }
2424
2425
2426 /***********************************************************************
2427  *           InSendMessage16    (USER.192)
2428  */
2429 BOOL16 WINAPI InSendMessage16(void)
2430 {
2431     return InSendMessage();
2432 }
2433
2434
2435 /***********************************************************************
2436  *           InSendMessage32    (USER32.320)
2437  */
2438 BOOL WINAPI InSendMessage(void)
2439 {
2440     MESSAGEQUEUE *queue;
2441     BOOL ret;
2442
2443     if (!(queue = (MESSAGEQUEUE *)QUEUE_Lock( GetFastQueue16() )))
2444         return 0;
2445     ret = (BOOL)queue->smWaiting;
2446
2447     QUEUE_Unlock( queue );
2448     return ret;
2449 }
2450
2451 /***********************************************************************
2452  *           BroadcastSystemMessage    (USER32.12)
2453  */
2454 LONG WINAPI BroadcastSystemMessage(
2455         DWORD dwFlags,LPDWORD recipients,UINT uMessage,WPARAM wParam,
2456         LPARAM lParam
2457 ) {
2458         FIXME_(sendmsg)("(%08lx,%08lx,%08x,%08x,%08lx): stub!\n",
2459               dwFlags,*recipients,uMessage,wParam,lParam
2460         );
2461         return 0;
2462 }
2463
2464 /***********************************************************************
2465  *           SendNotifyMessageA    (USER32.460)
2466  * FIXME
2467  *  The message sended with PostMessage has to be put in the queue
2468  *  with a higher priority as the other "Posted" messages.
2469  *  QUEUE_AddMsg has to be modifyed.
2470  */
2471 BOOL WINAPI SendNotifyMessageA(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
2472 {       BOOL ret = TRUE;
2473         FIXME_(msg)("(%04x,%08x,%08x,%08lx) not complete\n",
2474               hwnd, msg, wParam, lParam);
2475               
2476         if ( GetCurrentThreadId() == GetWindowThreadProcessId ( hwnd, NULL))
2477         {       ret=SendMessageA ( hwnd, msg, wParam, lParam );
2478         }
2479         else
2480         {       PostMessageA ( hwnd, msg, wParam, lParam );
2481         }
2482         return ret;
2483 }
2484
2485 /***********************************************************************
2486  *           SendNotifyMessageW    (USER32.461)
2487  * FIXME
2488  *  The message sended with PostMessage has to be put in the queue
2489  *  with a higher priority as the other "Posted" messages.
2490  *  QUEUE_AddMsg has to be modifyed.
2491  */
2492 BOOL WINAPI SendNotifyMessageW(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
2493 {       BOOL ret = TRUE;
2494         FIXME_(msg)("(%04x,%08x,%08x,%08lx) not complete\n",
2495               hwnd, msg, wParam, lParam);
2496
2497         if ( GetCurrentThreadId() == GetWindowThreadProcessId ( hwnd, NULL))
2498         {       ret=SendMessageW ( hwnd, msg, wParam, lParam );
2499         }
2500         else
2501         {       PostMessageW ( hwnd, msg, wParam, lParam );
2502         }
2503         return ret;
2504 }
2505
2506 /***********************************************************************
2507  *           SendMessageCallback32A
2508  * FIXME: It's like PostMessage. The callback gets called when the message
2509  * is processed. We have to modify the message processing for a exact
2510  * implementation...
2511  */
2512 BOOL WINAPI SendMessageCallbackA(
2513         HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam,
2514         FARPROC lpResultCallBack,DWORD dwData)
2515 {       
2516         FIXME_(msg)("(0x%04x,0x%04x,0x%08x,0x%08lx,%p,0x%08lx),stub!\n",
2517                 hWnd,Msg,wParam,lParam,lpResultCallBack,dwData);
2518         if ( hWnd == HWND_BROADCAST)
2519         {       PostMessageA( hWnd, Msg, wParam, lParam);
2520                 FIXME_(msg)("Broadcast: Callback will not be called!\n");
2521                 return TRUE;
2522         }
2523         (lpResultCallBack)( hWnd, Msg, dwData, SendMessageA ( hWnd, Msg, wParam, lParam ));
2524                 return TRUE;
2525 }
2526 /***********************************************************************
2527  *           SendMessageCallback32W
2528  * FIXME: It's like PostMessage. The callback gets called when the message
2529  * is processed. We have to modify the message processing for a exact
2530  * implementation...
2531  */
2532 BOOL WINAPI SendMessageCallbackW(
2533         HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam,
2534         FARPROC lpResultCallBack,DWORD dwData)
2535 {       
2536         FIXME_(msg)("(0x%04x,0x%04x,0x%08x,0x%08lx,%p,0x%08lx),stub!\n",
2537                 hWnd,Msg,wParam,lParam,lpResultCallBack,dwData);
2538         if ( hWnd == HWND_BROADCAST)
2539         {       PostMessageW( hWnd, Msg, wParam, lParam);
2540                 FIXME_(msg)("Broadcast: Callback will not be called!\n");
2541                 return TRUE;
2542         }
2543         (lpResultCallBack)( hWnd, Msg, dwData, SendMessageA ( hWnd, Msg, wParam, lParam ));
2544                 return TRUE;
2545 }