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