Set default video memory to 64Mb (was 16Mb) as many d3d9 demos use
[wine] / windows / message.c
1 /*
2  * Message queues related functions
3  *
4  * Copyright 1993, 1994 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #ifdef HAVE_SYS_TIME_H
29 # include <sys/time.h>
30 #endif
31 #include <sys/types.h>
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wingdi.h"
36 #include "winuser.h"
37 #include "message.h"
38 #include "winerror.h"
39 #include "ntstatus.h"
40 #include "wine/server.h"
41 #include "controls.h"
42 #include "dde.h"
43 #include "message.h"
44 #include "user_private.h"
45 #include "win.h"
46 #include "winpos.h"
47 #include "winproc.h"
48 #include "wine/debug.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(msg);
51 WINE_DECLARE_DEBUG_CHANNEL(key);
52
53 #define WM_NCMOUSEFIRST         WM_NCMOUSEMOVE
54 #define WM_NCMOUSELAST          WM_NCMBUTTONDBLCLK
55
56
57 /***********************************************************************
58  *           is_keyboard_message
59  */
60 inline static BOOL is_keyboard_message( UINT message )
61 {
62     return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
63 }
64
65
66 /***********************************************************************
67  *           is_mouse_message
68  */
69 inline static BOOL is_mouse_message( UINT message )
70 {
71     return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
72             (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
73 }
74
75
76 /***********************************************************************
77  *           check_message_filter
78  */
79 inline static BOOL check_message_filter( const MSG *msg, HWND hwnd, UINT first, UINT last )
80 {
81     if (hwnd)
82     {
83         if (msg->hwnd != hwnd && !IsChild( hwnd, msg->hwnd )) return FALSE;
84     }
85     if (first || last)
86     {
87        return (msg->message >= first && msg->message <= last);
88     }
89     return TRUE;
90 }
91
92
93 /***********************************************************************
94  *           process_sent_messages
95  *
96  * Process all pending sent messages.
97  */
98 inline static void process_sent_messages(void)
99 {
100     MSG msg;
101     MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
102 }
103
104
105 /***********************************************************************
106  *           queue_hardware_message
107  *
108  * store a hardware message in the thread queue
109  */
110 #if 0
111 static void queue_hardware_message( MSG *msg, ULONG_PTR extra_info )
112 {
113     SERVER_START_REQ( send_message )
114     {
115         req->type   = MSG_HARDWARE;
116         req->id     = GetWindowThreadProcessId( msg->hwnd, NULL );
117         req->win    = msg->hwnd;
118         req->msg    = msg->message;
119         req->wparam = msg->wParam;
120         req->lparam = msg->lParam;
121         req->x      = msg->pt.x;
122         req->y      = msg->pt.y;
123         req->time   = msg->time;
124         req->info   = extra_info;
125         req->timeout = 0;
126         wine_server_call( req );
127     }
128     SERVER_END_REQ;
129 }
130 #endif
131
132
133 /***********************************************************************
134  *           MSG_SendParentNotify
135  *
136  * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
137  * the window has the WS_EX_NOPARENTNOTIFY style.
138  */
139 static void MSG_SendParentNotify( HWND hwnd, WORD event, WORD idChild, POINT pt )
140 {
141     /* pt has to be in the client coordinates of the parent window */
142     MapWindowPoints( 0, hwnd, &pt, 1 );
143     for (;;)
144     {
145         HWND parent;
146
147         if (!(GetWindowLongA( hwnd, GWL_STYLE ) & WS_CHILD)) break;
148         if (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
149         if (!(parent = GetParent(hwnd))) break;
150         MapWindowPoints( hwnd, parent, &pt, 1 );
151         hwnd = parent;
152         SendMessageA( hwnd, WM_PARENTNOTIFY,
153                       MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
154     }
155 }
156
157
158 #if 0  /* this is broken for now, will require proper support in the server */
159
160 /***********************************************************************
161  *          MSG_JournalPlayBackMsg
162  *
163  * Get an EVENTMSG struct via call JOURNALPLAYBACK hook function
164  */
165 void MSG_JournalPlayBackMsg(void)
166 {
167     EVENTMSG tmpMsg;
168     MSG msg;
169     LRESULT wtime;
170     int keyDown,i;
171
172     wtime=HOOK_CallHooks( WH_JOURNALPLAYBACK, HC_GETNEXT, 0, (LPARAM)&tmpMsg, TRUE );
173     /*  TRACE(msg,"Playback wait time =%ld\n",wtime); */
174     if (wtime<=0)
175     {
176         wtime=0;
177         msg.message = tmpMsg.message;
178         msg.hwnd    = tmpMsg.hwnd;
179         msg.time    = tmpMsg.time;
180         if ((tmpMsg.message >= WM_KEYFIRST) && (tmpMsg.message <= WM_KEYLAST))
181         {
182             msg.wParam  = tmpMsg.paramL & 0xFF;
183             msg.lParam  = MAKELONG(tmpMsg.paramH&0x7ffff,tmpMsg.paramL>>8);
184             if (tmpMsg.message == WM_KEYDOWN || tmpMsg.message == WM_SYSKEYDOWN)
185             {
186                 for (keyDown=i=0; i<256 && !keyDown; i++)
187                     if (InputKeyStateTable[i] & 0x80)
188                         keyDown++;
189                 if (!keyDown)
190                     msg.lParam |= 0x40000000;
191                 InputKeyStateTable[msg.wParam] |= 0x80;
192                 AsyncKeyStateTable[msg.wParam] |= 0x80;
193             }
194             else                                       /* WM_KEYUP, WM_SYSKEYUP */
195             {
196                 msg.lParam |= 0xC0000000;
197                 InputKeyStateTable[msg.wParam] &= ~0x80;
198             }
199             if (InputKeyStateTable[VK_MENU] & 0x80)
200                 msg.lParam |= 0x20000000;
201             if (tmpMsg.paramH & 0x8000)              /*special_key bit*/
202                 msg.lParam |= 0x01000000;
203
204             msg.pt.x = msg.pt.y = 0;
205             queue_hardware_message( &msg, 0 );
206         }
207         else if ((tmpMsg.message>= WM_MOUSEFIRST) && (tmpMsg.message <= WM_MOUSELAST))
208         {
209             switch (tmpMsg.message)
210             {
211             case WM_LBUTTONDOWN:
212                 InputKeyStateTable[VK_LBUTTON] |= 0x80;
213                 AsyncKeyStateTable[VK_LBUTTON] |= 0x80;
214                 break;
215             case WM_LBUTTONUP:
216                 InputKeyStateTable[VK_LBUTTON] &= ~0x80;
217                 break;
218             case WM_MBUTTONDOWN:
219                 InputKeyStateTable[VK_MBUTTON] |= 0x80;
220                 AsyncKeyStateTable[VK_MBUTTON] |= 0x80;
221                 break;
222             case WM_MBUTTONUP:
223                 InputKeyStateTable[VK_MBUTTON] &= ~0x80;
224                 break;
225             case WM_RBUTTONDOWN:
226                 InputKeyStateTable[VK_RBUTTON] |= 0x80;
227                 AsyncKeyStateTable[VK_RBUTTON] |= 0x80;
228                 break;
229             case WM_RBUTTONUP:
230                 InputKeyStateTable[VK_RBUTTON] &= ~0x80;
231                 break;
232             }
233             SetCursorPos(tmpMsg.paramL,tmpMsg.paramH);
234             msg.lParam=MAKELONG(tmpMsg.paramL,tmpMsg.paramH);
235             msg.wParam=0;
236             if (InputKeyStateTable[VK_LBUTTON] & 0x80) msg.wParam |= MK_LBUTTON;
237             if (InputKeyStateTable[VK_MBUTTON] & 0x80) msg.wParam |= MK_MBUTTON;
238             if (InputKeyStateTable[VK_RBUTTON] & 0x80) msg.wParam |= MK_RBUTTON;
239
240             msg.pt.x = tmpMsg.paramL;
241             msg.pt.y = tmpMsg.paramH;
242             queue_hardware_message( &msg, 0 );
243         }
244         HOOK_CallHooks( WH_JOURNALPLAYBACK, HC_SKIP, 0, (LPARAM)&tmpMsg, TRUE );
245     }
246     else
247     {
248         if( tmpMsg.message == WM_QUEUESYNC ) HOOK_CallHooks( WH_CBT, HCBT_QS, 0, 0, TRUE );
249     }
250 }
251 #endif
252
253
254 /***********************************************************************
255  *          process_raw_keyboard_message
256  *
257  * returns TRUE if the contents of 'msg' should be passed to the application
258  */
259 static void process_raw_keyboard_message( MSG *msg )
260 {
261     EVENTMSG event;
262
263     event.message = msg->message;
264     event.hwnd    = msg->hwnd;
265     event.time    = msg->time;
266     event.paramL  = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
267     event.paramH  = msg->lParam & 0x7FFF;
268     if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
269     HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
270 }
271
272
273 /***********************************************************************
274  *          process_cooked_keyboard_message
275  *
276  * returns TRUE if the contents of 'msg' should be passed to the application
277  */
278 static BOOL process_cooked_keyboard_message( MSG *msg, BOOL remove )
279 {
280     if (remove)
281     {
282         /* Handle F1 key by sending out WM_HELP message */
283         if ((msg->message == WM_KEYUP) &&
284             (msg->wParam == VK_F1) &&
285             (msg->hwnd != GetDesktopWindow()) &&
286             !MENU_IsMenuActive())
287         {
288             HELPINFO hi;
289             hi.cbSize = sizeof(HELPINFO);
290             hi.iContextType = HELPINFO_WINDOW;
291             hi.iCtrlId = GetWindowLongPtrA( msg->hwnd, GWLP_ID );
292             hi.hItemHandle = msg->hwnd;
293             hi.dwContextId = GetWindowContextHelpId( msg->hwnd );
294             hi.MousePos = msg->pt;
295             SendMessageA(msg->hwnd, WM_HELP, 0, (LPARAM)&hi);
296         }
297     }
298
299     if (HOOK_CallHooks( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
300                         LOWORD(msg->wParam), msg->lParam, TRUE ))
301     {
302         /* skip this message */
303         HOOK_CallHooks( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam, TRUE );
304         return FALSE;
305     }
306     return TRUE;
307 }
308
309
310 /***********************************************************************
311  *          process_raw_mouse_message
312  */
313 static void process_raw_mouse_message( MSG *msg, BOOL remove )
314 {
315     static MSG clk_msg;
316
317     POINT pt;
318     INT hittest;
319     EVENTMSG event;
320     GUITHREADINFO info;
321     HWND hWndScope = msg->hwnd;
322
323     /* find the window to dispatch this mouse message to */
324
325     hittest = HTCLIENT;
326     GetGUIThreadInfo( GetCurrentThreadId(), &info );
327     if (!(msg->hwnd = info.hwndCapture))
328     {
329         /* If no capture HWND, find window which contains the mouse position.
330          * Also find the position of the cursor hot spot (hittest) */
331         if (!IsWindow(hWndScope)) hWndScope = 0;
332         if (!(msg->hwnd = WINPOS_WindowFromPoint( hWndScope, msg->pt, &hittest )))
333             msg->hwnd = GetDesktopWindow();
334     }
335
336     event.message = msg->message;
337     event.time    = msg->time;
338     event.hwnd    = msg->hwnd;
339     event.paramL  = msg->pt.x;
340     event.paramH  = msg->pt.y;
341     HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
342
343     /* translate double clicks */
344
345     if ((msg->message == WM_LBUTTONDOWN) ||
346         (msg->message == WM_RBUTTONDOWN) ||
347         (msg->message == WM_MBUTTONDOWN))
348     {
349         BOOL update = remove;
350         /* translate double clicks -
351          * note that ...MOUSEMOVEs can slip in between
352          * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
353
354         if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
355             hittest != HTCLIENT ||
356             (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
357         {
358            if ((msg->message == clk_msg.message) &&
359                (msg->hwnd == clk_msg.hwnd) &&
360                (msg->time - clk_msg.time < GetDoubleClickTime()) &&
361                (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
362                (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
363            {
364                msg->message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
365                if (remove)
366                {
367                    clk_msg.message = 0;
368                    update = FALSE;
369                }
370            }
371         }
372         /* update static double click conditions */
373         if (update) clk_msg = *msg;
374     }
375
376     pt = msg->pt;
377     /* Note: windows has no concept of a non-client wheel message */
378     if (msg->message != WM_MOUSEWHEEL)
379     {
380         if (hittest != HTCLIENT)
381         {
382             msg->message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
383             msg->wParam = hittest;
384         }
385         else
386         {
387             /* coordinates don't get translated while tracking a menu */
388             /* FIXME: should differentiate popups and top-level menus */
389             if (!(info.flags & GUI_INMENUMODE))
390                 ScreenToClient( msg->hwnd, &pt );
391         }
392     }
393     msg->lParam = MAKELONG( pt.x, pt.y );
394 }
395
396
397 /***********************************************************************
398  *          process_cooked_mouse_message
399  *
400  * returns TRUE if the contents of 'msg' should be passed to the application
401  */
402 static BOOL process_cooked_mouse_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
403 {
404     MOUSEHOOKSTRUCT hook;
405     INT hittest = HTCLIENT;
406     UINT raw_message = msg->message;
407     BOOL eatMsg;
408
409     if (msg->message >= WM_NCMOUSEFIRST && msg->message <= WM_NCMOUSELAST)
410     {
411         raw_message += WM_MOUSEFIRST - WM_NCMOUSEFIRST;
412         hittest = msg->wParam;
413     }
414     if (raw_message == WM_LBUTTONDBLCLK ||
415         raw_message == WM_RBUTTONDBLCLK ||
416         raw_message == WM_MBUTTONDBLCLK)
417     {
418         raw_message += WM_LBUTTONDOWN - WM_LBUTTONDBLCLK;
419     }
420
421     hook.pt           = msg->pt;
422     hook.hwnd         = msg->hwnd;
423     hook.wHitTestCode = hittest;
424     hook.dwExtraInfo  = extra_info;
425     if (HOOK_CallHooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
426                         msg->message, (LPARAM)&hook, TRUE ))
427     {
428         hook.pt           = msg->pt;
429         hook.hwnd         = msg->hwnd;
430         hook.wHitTestCode = hittest;
431         hook.dwExtraInfo  = extra_info;
432         HOOK_CallHooks( WH_CBT, HCBT_CLICKSKIPPED, msg->message, (LPARAM)&hook, TRUE );
433         return FALSE;
434     }
435
436     if ((hittest == HTERROR) || (hittest == HTNOWHERE))
437     {
438         SendMessageA( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
439                       MAKELONG( hittest, raw_message ));
440         return FALSE;
441     }
442
443     if (!remove || GetCapture()) return TRUE;
444
445     eatMsg = FALSE;
446
447     if ((raw_message == WM_LBUTTONDOWN) ||
448         (raw_message == WM_RBUTTONDOWN) ||
449         (raw_message == WM_MBUTTONDOWN))
450     {
451         /* Send the WM_PARENTNOTIFY,
452          * note that even for double/nonclient clicks
453          * notification message is still WM_L/M/RBUTTONDOWN.
454          */
455         MSG_SendParentNotify( msg->hwnd, raw_message, 0, msg->pt );
456
457         /* Activate the window if needed */
458
459         if (msg->hwnd != GetActiveWindow())
460         {
461             HWND hwndTop = msg->hwnd;
462             while (hwndTop)
463             {
464                 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
465                 hwndTop = GetParent( hwndTop );
466             }
467
468             if (hwndTop && hwndTop != GetDesktopWindow())
469             {
470                 LONG ret = SendMessageA( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
471                                          MAKELONG( hittest, raw_message ) );
472                 switch(ret)
473                 {
474                 case MA_NOACTIVATEANDEAT:
475                     eatMsg = TRUE;
476                     /* fall through */
477                 case MA_NOACTIVATE:
478                     break;
479                 case MA_ACTIVATEANDEAT:
480                     eatMsg = TRUE;
481                     /* fall through */
482                 case MA_ACTIVATE:
483                 case 0:
484                     if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
485                     break;
486                 default:
487                     WARN( "unknown WM_MOUSEACTIVATE code %ld\n", ret );
488                     break;
489                 }
490             }
491         }
492     }
493
494     /* send the WM_SETCURSOR message */
495
496     /* Windows sends the normal mouse message as the message parameter
497        in the WM_SETCURSOR message even if it's non-client mouse message */
498     SendMessageA( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
499                   MAKELONG( hittest, raw_message ));
500
501     return !eatMsg;
502 }
503
504
505 /***********************************************************************
506  *          MSG_process_raw_hardware_message
507  *
508  * returns TRUE if the contents of 'msg' should be passed to the application
509  */
510 BOOL MSG_process_raw_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd_filter,
511                                        UINT first, UINT last, BOOL remove )
512 {
513     if (is_keyboard_message( msg->message ))
514     {
515         process_raw_keyboard_message( msg );
516     }
517     else if (is_mouse_message( msg->message ))
518     {
519         process_raw_mouse_message( msg, remove );
520     }
521     else
522     {
523         ERR( "unknown message type %x\n", msg->message );
524         return FALSE;
525     }
526     return check_message_filter( msg, hwnd_filter, first, last );
527 }
528
529
530 /***********************************************************************
531  *          MSG_process_cooked_hardware_message
532  *
533  * returns TRUE if the contents of 'msg' should be passed to the application
534  */
535 BOOL MSG_process_cooked_hardware_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
536 {
537     if (is_keyboard_message( msg->message ))
538         return process_cooked_keyboard_message( msg, remove );
539
540     if (is_mouse_message( msg->message ))
541         return process_cooked_mouse_message( msg, extra_info, remove );
542
543     ERR( "unknown message type %x\n", msg->message );
544     return FALSE;
545 }
546
547
548 /***********************************************************************
549  *              WaitMessage (USER.112) Suspend thread pending messages
550  *              WaitMessage (USER32.@) Suspend thread pending messages
551  *
552  * WaitMessage() suspends a thread until events appear in the thread's
553  * queue.
554  */
555 BOOL WINAPI WaitMessage(void)
556 {
557     return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
558 }
559
560
561 /***********************************************************************
562  *              MsgWaitForMultipleObjectsEx   (USER32.@)
563  */
564 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
565                                           DWORD timeout, DWORD mask, DWORD flags )
566 {
567     HANDLE handles[MAXIMUM_WAIT_OBJECTS];
568     DWORD i, ret, lock;
569     MESSAGEQUEUE *msgQueue;
570
571     if (count > MAXIMUM_WAIT_OBJECTS-1)
572     {
573         SetLastError( ERROR_INVALID_PARAMETER );
574         return WAIT_FAILED;
575     }
576
577     if (!(msgQueue = QUEUE_Current())) return WAIT_FAILED;
578
579     /* set the queue mask */
580     SERVER_START_REQ( set_queue_mask )
581     {
582         req->wake_mask    = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
583         req->changed_mask = mask;
584         req->skip_wait    = 0;
585         wine_server_call( req );
586     }
587     SERVER_END_REQ;
588
589     /* Add the thread event to the handle list */
590     for (i = 0; i < count; i++) handles[i] = pHandles[i];
591     handles[count] = msgQueue->server_queue;
592
593     ReleaseThunkLock( &lock );
594     if (USER_Driver.pMsgWaitForMultipleObjectsEx)
595     {
596         ret = USER_Driver.pMsgWaitForMultipleObjectsEx( count+1, handles, timeout, mask, flags );
597         if (ret == count+1) ret = count; /* pretend the msg queue is ready */
598     }
599     else
600         ret = WaitForMultipleObjectsEx( count+1, handles, flags & MWMO_WAITALL,
601                                         timeout, flags & MWMO_ALERTABLE );
602     if (lock) RestoreThunkLock( lock );
603     return ret;
604 }
605
606
607 /***********************************************************************
608  *              MsgWaitForMultipleObjects (USER32.@)
609  */
610 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
611                                         BOOL wait_all, DWORD timeout, DWORD mask )
612 {
613     return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
614                                         wait_all ? MWMO_WAITALL : 0 );
615 }
616
617
618 /***********************************************************************
619  *              WaitForInputIdle (USER32.@)
620  */
621 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
622 {
623     DWORD start_time, elapsed, ret;
624     HANDLE idle_event = (HANDLE)-1;
625
626     SERVER_START_REQ( wait_input_idle )
627     {
628         req->handle = hProcess;
629         req->timeout = dwTimeOut;
630         if (!(ret = wine_server_call_err( req ))) idle_event = reply->event;
631     }
632     SERVER_END_REQ;
633     if (ret) return WAIT_FAILED;  /* error */
634     if (!idle_event) return 0;  /* no event to wait on */
635
636     start_time = GetTickCount();
637     elapsed = 0;
638
639     TRACE("waiting for %p\n", idle_event );
640     do
641     {
642         ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
643         switch (ret)
644         {
645         case WAIT_OBJECT_0+1:
646             process_sent_messages();
647             break;
648         case WAIT_TIMEOUT:
649         case WAIT_FAILED:
650             TRACE("timeout or error\n");
651             return ret;
652         default:
653             TRACE("finished\n");
654             return 0;
655         }
656         if (dwTimeOut != INFINITE)
657         {
658             elapsed = GetTickCount() - start_time;
659             if (elapsed > dwTimeOut)
660                 break;
661         }
662     }
663     while (1);
664
665     return WAIT_TIMEOUT;
666 }
667
668
669 /***********************************************************************
670  *              UserYield (USER.332)
671  */
672 void WINAPI UserYield16(void)
673 {
674    DWORD count;
675
676     /* Handle sent messages */
677     process_sent_messages();
678
679     /* Yield */
680     ReleaseThunkLock(&count);
681     if (count)
682     {
683         RestoreThunkLock(count);
684         /* Handle sent messages again */
685         process_sent_messages();
686     }
687 }
688
689
690 /***********************************************************************
691  *              TranslateMessage (USER32.@)
692  *
693  * Implementation of TranslateMessage.
694  *
695  * TranslateMessage translates virtual-key messages into character-messages,
696  * as follows :
697  * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
698  * ditto replacing WM_* with WM_SYS*
699  * This produces WM_CHAR messages only for keys mapped to ASCII characters
700  * by the keyboard driver.
701  *
702  * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
703  * return value is nonzero, regardless of the translation.
704  *
705  */
706 BOOL WINAPI TranslateMessage( const MSG *msg )
707 {
708     UINT message;
709     WCHAR wp[2];
710     BOOL rc = FALSE;
711     BYTE state[256];
712
713     if (msg->message >= WM_KEYFIRST && msg->message <= WM_KEYLAST)
714     {
715         TRACE_(key)("(%s, %04X, %08lX)\n",
716                     SPY_GetMsgName(msg->message, msg->hwnd), msg->wParam, msg->lParam );
717
718         /* Return code must be TRUE no matter what! */
719         rc = TRUE;
720     }
721
722     if ((msg->message != WM_KEYDOWN) && (msg->message != WM_SYSKEYDOWN)) return rc;
723
724     TRACE_(key)("Translating key %s (%04x), scancode %02x\n",
725                  SPY_GetVKeyName(msg->wParam), msg->wParam, LOBYTE(HIWORD(msg->lParam)));
726
727     GetKeyboardState( state );
728     /* FIXME : should handle ToUnicode yielding 2 */
729     switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), state, wp, 2, 0))
730     {
731     case 1:
732         message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
733         TRACE_(key)("1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
734             msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
735         PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
736         break;
737
738     case -1:
739         message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
740         TRACE_(key)("-1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
741             msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
742         PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
743         return TRUE;
744     }
745     return rc;
746 }
747
748
749 /***********************************************************************
750  *              DispatchMessageA (USER32.@)
751  */
752 LONG WINAPI DispatchMessageA( const MSG* msg )
753 {
754     WND * wndPtr;
755     LONG retval;
756     WNDPROC winproc;
757
758       /* Process timer messages */
759     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
760     {
761         if (msg->lParam)
762         {
763 /*            HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
764             return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
765                                    msg->message, msg->wParam, GetTickCount() );
766         }
767     }
768
769     if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
770     {
771         if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
772         return 0;
773     }
774     if (wndPtr == WND_OTHER_PROCESS)
775     {
776         if (IsWindow( msg->hwnd )) SetLastError( ERROR_MESSAGE_SYNC_ONLY );
777         else SetLastError( ERROR_INVALID_WINDOW_HANDLE );
778         return 0;
779     }
780     if (wndPtr->tid != GetCurrentThreadId())
781     {
782         SetLastError( ERROR_MESSAGE_SYNC_ONLY );
783         WIN_ReleasePtr( wndPtr );
784         return 0;
785     }
786     winproc = wndPtr->winproc;
787     WIN_ReleasePtr( wndPtr );
788
789 /*    hook_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
790
791     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
792                       msg->wParam, msg->lParam );
793     retval = CallWindowProcA( winproc, msg->hwnd, msg->message,
794                               msg->wParam, msg->lParam );
795     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
796                      msg->wParam, msg->lParam );
797
798     return retval;
799 }
800
801
802 /***********************************************************************
803  *              DispatchMessageW (USER32.@) Process Message
804  *
805  * Process the message specified in the structure *_msg_.
806  *
807  * If the lpMsg parameter points to a WM_TIMER message and the
808  * parameter of the WM_TIMER message is not NULL, the lParam parameter
809  * points to the function that is called instead of the window
810  * procedure.
811  *
812  * The message must be valid.
813  *
814  * RETURNS
815  *
816  *   DispatchMessage() returns the result of the window procedure invoked.
817  *
818  * CONFORMANCE
819  *
820  *   ECMA-234, Win32
821  *
822  */
823 LONG WINAPI DispatchMessageW( const MSG* msg )
824 {
825     WND * wndPtr;
826     LONG retval;
827     WNDPROC winproc;
828
829       /* Process timer messages */
830     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
831     {
832         if (msg->lParam)
833         {
834 /*            HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
835             return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
836                                    msg->message, msg->wParam, GetTickCount() );
837         }
838     }
839
840     if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
841     {
842         if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
843         return 0;
844     }
845     if (wndPtr == WND_OTHER_PROCESS)
846     {
847         if (IsWindow( msg->hwnd )) SetLastError( ERROR_MESSAGE_SYNC_ONLY );
848         else SetLastError( ERROR_INVALID_WINDOW_HANDLE );
849         return 0;
850     }
851     if (wndPtr->tid != GetCurrentThreadId())
852     {
853         SetLastError( ERROR_MESSAGE_SYNC_ONLY );
854         WIN_ReleasePtr( wndPtr );
855         return 0;
856     }
857     winproc = wndPtr->winproc;
858     WIN_ReleasePtr( wndPtr );
859
860 /*    HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
861
862     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
863                       msg->wParam, msg->lParam );
864     retval = CallWindowProcW( winproc, msg->hwnd, msg->message,
865                               msg->wParam, msg->lParam );
866     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
867                      msg->wParam, msg->lParam );
868
869     return retval;
870 }
871
872
873 /***********************************************************************
874  *              RegisterWindowMessage (USER.118)
875  *              RegisterWindowMessageA (USER32.@)
876  */
877 WORD WINAPI RegisterWindowMessageA( LPCSTR str )
878 {
879     TRACE("%s\n", str );
880     return GlobalAddAtomA( str );
881 }
882
883
884 /***********************************************************************
885  *              RegisterWindowMessageW (USER32.@)
886  */
887 WORD WINAPI RegisterWindowMessageW( LPCWSTR str )
888 {
889     TRACE("%p\n", str );
890     return GlobalAddAtomW( str );
891 }
892
893
894 /***********************************************************************
895  *              BroadcastSystemMessage  (USER32.@)
896  *              BroadcastSystemMessageA (USER32.@)
897  */
898 LONG WINAPI BroadcastSystemMessageA(
899         DWORD dwFlags,LPDWORD recipients,UINT uMessage,WPARAM wParam,
900         LPARAM lParam )
901 {
902     if ((*recipients & BSM_APPLICATIONS)||
903         (*recipients == BSM_ALLCOMPONENTS))
904     {
905         FIXME("(%08lx,%08lx,%08x,%08x,%08lx): semi-stub!\n",
906               dwFlags,*recipients,uMessage,wParam,lParam);
907         PostMessageA(HWND_BROADCAST,uMessage,wParam,lParam);
908         return 1;
909     }
910     else
911     {
912         FIXME("(%08lx,%08lx,%08x,%08x,%08lx): stub!\n",
913               dwFlags,*recipients,uMessage,wParam,lParam);
914         return -1;
915     }
916 }
917
918 /***********************************************************************
919  *              BroadcastSystemMessageW (USER32.@)
920  */
921 LONG WINAPI BroadcastSystemMessageW(
922         DWORD dwFlags,LPDWORD recipients,UINT uMessage,WPARAM wParam,
923         LPARAM lParam )
924 {
925     if ((*recipients & BSM_APPLICATIONS)||
926         (*recipients == BSM_ALLCOMPONENTS))
927     {
928         FIXME("(%08lx,%08lx,%08x,%08x,%08lx): semi-stub!\n",
929               dwFlags,*recipients,uMessage,wParam,lParam);
930         PostMessageW(HWND_BROADCAST,uMessage,wParam,lParam);
931         return 1;
932     }
933     else
934     {
935         FIXME("(%08lx,%08lx,%08x,%08x,%08lx): stub!\n",
936               dwFlags,*recipients,uMessage,wParam,lParam);
937         return -1;
938     }
939 }