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