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