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