Fix some types and function prototypes according to Platform SDK
[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 <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #ifdef HAVE_SYS_TIME_H
27 # include <sys/time.h>
28 #endif
29 #include <sys/types.h>
30
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "message.h"
35 #include "winerror.h"
36 #include "wine/server.h"
37 #include "win.h"
38 #include "heap.h"
39 #include "input.h"
40 #include "spy.h"
41 #include "winpos.h"
42 #include "dde.h"
43 #include "queue.h"
44 #include "winproc.h"
45 #include "user.h"
46 #include "thread.h"
47 #include "controls.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 static BYTE QueueKeyStateTable[256];
57
58
59 /***********************************************************************
60  *           is_keyboard_message
61  */
62 inline static BOOL is_keyboard_message( UINT message )
63 {
64     return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
65 }
66
67
68 /***********************************************************************
69  *           is_mouse_message
70  */
71 inline static BOOL is_mouse_message( UINT message )
72 {
73     return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
74             (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
75 }
76
77
78 /***********************************************************************
79  *           check_message_filter
80  */
81 inline static BOOL check_message_filter( const MSG *msg, HWND hwnd, UINT first, UINT last )
82 {
83     if (hwnd)
84     {
85         if (msg->hwnd != hwnd && !IsChild( hwnd, msg->hwnd )) return FALSE;
86     }
87     if (first || last)
88     {
89        return (msg->message >= first && msg->message <= last);
90     }
91     return TRUE;
92 }
93
94
95 /***********************************************************************
96  *           process_sent_messages
97  *
98  * Process all pending sent messages.
99  */
100 inline static void process_sent_messages(void)
101 {
102     MSG msg;
103     MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
104 }
105
106
107 /***********************************************************************
108  *           queue_hardware_message
109  *
110  * store a hardware message in the thread queue
111  */
112 static void queue_hardware_message( MSG *msg, ULONG_PTR extra_info, enum message_type type )
113 {
114     SERVER_START_REQ( send_message )
115     {
116         req->type   = type;
117         req->id     = GetWindowThreadProcessId( msg->hwnd, NULL );
118         req->win    = msg->hwnd;
119         req->msg    = msg->message;
120         req->wparam = msg->wParam;
121         req->lparam = msg->lParam;
122         req->x      = msg->pt.x;
123         req->y      = msg->pt.y;
124         req->time   = msg->time;
125         req->info   = extra_info;
126         req->timeout = 0;
127         wine_server_call( req );
128     }
129     SERVER_END_REQ;
130 }
131
132
133 /***********************************************************************
134  *           update_queue_key_state
135  */
136 static void update_queue_key_state( UINT msg, WPARAM wp, LPARAM lp )
137 {
138     BOOL down = FALSE, iskey = FALSE;
139     WPARAM dualkey = 0;
140
141     switch (msg)
142     {
143     case WM_LBUTTONDOWN:
144         down = TRUE;
145         /* fall through */
146     case WM_LBUTTONUP:
147         wp = VK_LBUTTON;
148         break;
149     case WM_MBUTTONDOWN:
150         down = TRUE;
151         /* fall through */
152     case WM_MBUTTONUP:
153         wp = VK_MBUTTON;
154         break;
155     case WM_RBUTTONDOWN:
156         down = TRUE;
157         /* fall through */
158     case WM_RBUTTONUP:
159         wp = VK_RBUTTON;
160         break;
161     case WM_KEYDOWN:
162     case WM_SYSKEYDOWN:
163         down = TRUE;
164         /* fall through */
165     case WM_KEYUP:
166     case WM_SYSKEYUP:
167         wp = wp & 0xff;
168         iskey = TRUE;
169         break;
170     }
171     if (iskey)
172     {
173         switch(wp)
174         {
175             case VK_SHIFT:
176                 dualkey = (HIWORD(lp) & KF_EXTENDED) ? VK_RSHIFT : VK_LSHIFT;
177                 break;
178             case VK_CONTROL:
179                 dualkey = (HIWORD(lp) & KF_EXTENDED) ? VK_RCONTROL : VK_LCONTROL;
180                 break;
181             case VK_MENU:
182                 dualkey = (HIWORD(lp) & KF_EXTENDED) ? VK_RMENU : VK_LMENU;
183                 break;
184             
185         }
186     }
187     if (down)
188     {
189         BYTE *p = &QueueKeyStateTable[wp];
190         if (!(*p & 0x80)) *p ^= 0x01;
191         *p |= 0x80;
192     }
193     else QueueKeyStateTable[wp] &= ~0x80;
194     if (dualkey)
195     { /* also update the "dual" keys properly */
196         if (down)
197         {
198             BYTE *p = &QueueKeyStateTable[dualkey];
199             if (!(*p & 0x80)) *p ^= 0x01;
200             *p |= 0x80;
201         }
202         else QueueKeyStateTable[dualkey] &= ~0x80;
203     }
204 }
205
206
207 /***********************************************************************
208  *           MSG_SendParentNotify
209  *
210  * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
211  * the window has the WS_EX_NOPARENTNOTIFY style.
212  */
213 static void MSG_SendParentNotify( HWND hwnd, WORD event, WORD idChild, POINT pt )
214 {
215     /* pt has to be in the client coordinates of the parent window */
216     MapWindowPoints( 0, hwnd, &pt, 1 );
217     for (;;)
218     {
219         HWND parent;
220
221         if (!(GetWindowLongA( hwnd, GWL_STYLE ) & WS_CHILD)) break;
222         if (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
223         if (!(parent = GetParent(hwnd))) break;
224         MapWindowPoints( hwnd, parent, &pt, 1 );
225         hwnd = parent;
226         SendMessageA( hwnd, WM_PARENTNOTIFY,
227                       MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
228     }
229 }
230
231
232 #if 0  /* this is broken for now, will require proper support in the server */
233
234 /***********************************************************************
235  *          MSG_JournalPlayBackMsg
236  *
237  * Get an EVENTMSG struct via call JOURNALPLAYBACK hook function
238  */
239 void MSG_JournalPlayBackMsg(void)
240 {
241     EVENTMSG tmpMsg;
242     MSG msg;
243     LRESULT wtime;
244     int keyDown,i;
245
246     wtime=HOOK_CallHooks( WH_JOURNALPLAYBACK, HC_GETNEXT, 0, (LPARAM)&tmpMsg, TRUE );
247     /*  TRACE(msg,"Playback wait time =%ld\n",wtime); */
248     if (wtime<=0)
249     {
250         wtime=0;
251         msg.message = tmpMsg.message;
252         msg.hwnd    = tmpMsg.hwnd;
253         msg.time    = tmpMsg.time;
254         if ((tmpMsg.message >= WM_KEYFIRST) && (tmpMsg.message <= WM_KEYLAST))
255         {
256             msg.wParam  = tmpMsg.paramL & 0xFF;
257             msg.lParam  = MAKELONG(tmpMsg.paramH&0x7ffff,tmpMsg.paramL>>8);
258             if (tmpMsg.message == WM_KEYDOWN || tmpMsg.message == WM_SYSKEYDOWN)
259             {
260                 for (keyDown=i=0; i<256 && !keyDown; i++)
261                     if (InputKeyStateTable[i] & 0x80)
262                         keyDown++;
263                 if (!keyDown)
264                     msg.lParam |= 0x40000000;
265                 InputKeyStateTable[msg.wParam] |= 0x80;
266                 AsyncKeyStateTable[msg.wParam] |= 0x80;
267             }
268             else                                       /* WM_KEYUP, WM_SYSKEYUP */
269             {
270                 msg.lParam |= 0xC0000000;
271                 InputKeyStateTable[msg.wParam] &= ~0x80;
272             }
273             if (InputKeyStateTable[VK_MENU] & 0x80)
274                 msg.lParam |= 0x20000000;
275             if (tmpMsg.paramH & 0x8000)              /*special_key bit*/
276                 msg.lParam |= 0x01000000;
277
278             msg.pt.x = msg.pt.y = 0;
279             queue_hardware_message( &msg, 0, MSG_HARDWARE_RAW );
280         }
281         else if ((tmpMsg.message>= WM_MOUSEFIRST) && (tmpMsg.message <= WM_MOUSELAST))
282         {
283             switch (tmpMsg.message)
284             {
285             case WM_LBUTTONDOWN:
286                 InputKeyStateTable[VK_LBUTTON] |= 0x80;
287                 AsyncKeyStateTable[VK_LBUTTON] |= 0x80;
288                 break;
289             case WM_LBUTTONUP:
290                 InputKeyStateTable[VK_LBUTTON] &= ~0x80;
291                 break;
292             case WM_MBUTTONDOWN:
293                 InputKeyStateTable[VK_MBUTTON] |= 0x80;
294                 AsyncKeyStateTable[VK_MBUTTON] |= 0x80;
295                 break;
296             case WM_MBUTTONUP:
297                 InputKeyStateTable[VK_MBUTTON] &= ~0x80;
298                 break;
299             case WM_RBUTTONDOWN:
300                 InputKeyStateTable[VK_RBUTTON] |= 0x80;
301                 AsyncKeyStateTable[VK_RBUTTON] |= 0x80;
302                 break;
303             case WM_RBUTTONUP:
304                 InputKeyStateTable[VK_RBUTTON] &= ~0x80;
305                 break;
306             }
307             SetCursorPos(tmpMsg.paramL,tmpMsg.paramH);
308             msg.lParam=MAKELONG(tmpMsg.paramL,tmpMsg.paramH);
309             msg.wParam=0;
310             if (InputKeyStateTable[VK_LBUTTON] & 0x80) msg.wParam |= MK_LBUTTON;
311             if (InputKeyStateTable[VK_MBUTTON] & 0x80) msg.wParam |= MK_MBUTTON;
312             if (InputKeyStateTable[VK_RBUTTON] & 0x80) msg.wParam |= MK_RBUTTON;
313
314             msg.pt.x = tmpMsg.paramL;
315             msg.pt.y = tmpMsg.paramH;
316             queue_hardware_message( &msg, 0, MSG_HARDWARE_RAW );
317         }
318         HOOK_CallHooks( WH_JOURNALPLAYBACK, HC_SKIP, 0, (LPARAM)&tmpMsg, TRUE );
319     }
320     else
321     {
322         if( tmpMsg.message == WM_QUEUESYNC ) HOOK_CallHooks( WH_CBT, HCBT_QS, 0, 0, TRUE );
323     }
324 }
325 #endif
326
327
328 /***********************************************************************
329  *          process_raw_keyboard_message
330  *
331  * returns TRUE if the contents of 'msg' should be passed to the application
332  */
333 static BOOL process_raw_keyboard_message( MSG *msg, ULONG_PTR extra_info )
334 {
335     EVENTMSG event;
336
337     if (!(msg->hwnd = GetFocus()))
338     {
339         /* Send the message to the active window instead,  */
340         /* translating messages to their WM_SYS equivalent */
341         msg->hwnd = GetActiveWindow();
342         if (msg->message < WM_SYSKEYDOWN) msg->message += WM_SYSKEYDOWN - WM_KEYDOWN;
343     }
344
345     event.message = msg->message;
346     event.hwnd    = msg->hwnd;
347     event.time    = msg->time;
348     event.paramL  = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
349     event.paramH  = msg->lParam & 0x7FFF;
350     if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
351     HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
352
353     /* if we are going to throw away the message, update the queue state now */
354     if (!msg->hwnd) update_queue_key_state( msg->message, msg->wParam, msg->lParam );
355
356     return (msg->hwnd != 0);
357 }
358
359
360 /***********************************************************************
361  *          process_cooked_keyboard_message
362  *
363  * returns TRUE if the contents of 'msg' should be passed to the application
364  */
365 static BOOL process_cooked_keyboard_message( MSG *msg, BOOL remove )
366 {
367     if (remove)
368     {
369         update_queue_key_state( msg->message, msg->wParam, msg->lParam );
370
371         /* Handle F1 key by sending out WM_HELP message */
372         if ((msg->message == WM_KEYUP) &&
373             (msg->wParam == VK_F1) &&
374             (msg->hwnd != GetDesktopWindow()) &&
375             !MENU_IsMenuActive())
376         {
377             HELPINFO hi;
378             hi.cbSize = sizeof(HELPINFO);
379             hi.iContextType = HELPINFO_WINDOW;
380             hi.iCtrlId = GetWindowLongA( msg->hwnd, GWL_ID );
381             hi.hItemHandle = msg->hwnd;
382             hi.dwContextId = GetWindowContextHelpId( msg->hwnd );
383             hi.MousePos = msg->pt;
384             SendMessageA(msg->hwnd, WM_HELP, 0, (LPARAM)&hi);
385         }
386     }
387
388     if (HOOK_CallHooks( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
389                         LOWORD(msg->wParam), msg->lParam, TRUE ))
390     {
391         /* skip this message */
392         HOOK_CallHooks( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam, TRUE );
393         return FALSE;
394     }
395     return TRUE;
396 }
397
398
399 /***********************************************************************
400  *          process_raw_mouse_message
401  *
402  * returns TRUE if the contents of 'msg' should be passed to the application
403  */
404 static BOOL process_raw_mouse_message( MSG *msg, ULONG_PTR extra_info )
405 {
406     static MSG clk_msg;
407
408     POINT pt;
409     INT hittest;
410     EVENTMSG event;
411     GUITHREADINFO info;
412
413     /* find the window to dispatch this mouse message to */
414
415     hittest = HTCLIENT;
416     GetGUIThreadInfo( GetCurrentThreadId(), &info );
417     if (!(msg->hwnd = info.hwndCapture))
418     {
419         /* If no capture HWND, find window which contains the mouse position.
420          * Also find the position of the cursor hot spot (hittest) */
421         HWND hWndScope = (HWND)extra_info;
422
423         if (!IsWindow(hWndScope)) hWndScope = 0;
424         if (!(msg->hwnd = WINPOS_WindowFromPoint( hWndScope, msg->pt, &hittest )))
425             msg->hwnd = GetDesktopWindow();
426     }
427
428     event.message = msg->message;
429     event.time    = msg->time;
430     event.hwnd    = msg->hwnd;
431     event.paramL  = msg->pt.x;
432     event.paramH  = msg->pt.y;
433     HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
434
435     /* translate double clicks */
436
437     if ((msg->message == WM_LBUTTONDOWN) ||
438         (msg->message == WM_RBUTTONDOWN) ||
439         (msg->message == WM_MBUTTONDOWN))
440     {
441         BOOL update = TRUE;
442         /* translate double clicks -
443          * note that ...MOUSEMOVEs can slip in between
444          * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
445
446         if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
447             hittest != HTCLIENT ||
448             (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
449         {
450            if ((msg->message == clk_msg.message) &&
451                (msg->hwnd == clk_msg.hwnd) &&
452                (msg->time - clk_msg.time < GetDoubleClickTime()) &&
453                (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
454                (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
455            {
456                msg->message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
457                clk_msg.message = 0;
458                update = FALSE;
459            }
460         }
461         /* update static double click conditions */
462         if (update) clk_msg = *msg;
463     }
464
465     pt = msg->pt;
466     /* Note: windows has no concept of a non-client wheel message */
467     if (hittest != HTCLIENT && msg->message != WM_MOUSEWHEEL)
468     {
469         msg->message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
470         msg->wParam = hittest;
471     }
472     else
473     {
474         /* coordinates don't get translated while tracking a menu */
475         /* FIXME: should differentiate popups and top-level menus */
476         if (!(info.flags & GUI_INMENUMODE)) ScreenToClient( msg->hwnd, &pt );
477     }
478     msg->lParam = MAKELONG( pt.x, pt.y );
479     return TRUE;
480 }
481
482
483 /***********************************************************************
484  *          process_cooked_mouse_message
485  *
486  * returns TRUE if the contents of 'msg' should be passed to the application
487  */
488 static BOOL process_cooked_mouse_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
489 {
490     MOUSEHOOKSTRUCT hook;
491     INT hittest = HTCLIENT;
492     UINT raw_message = msg->message;
493     BOOL eatMsg;
494
495     if (msg->message >= WM_NCMOUSEFIRST && msg->message <= WM_NCMOUSELAST)
496     {
497         raw_message += WM_MOUSEFIRST - WM_NCMOUSEFIRST;
498         hittest = msg->wParam;
499     }
500     if (raw_message == WM_LBUTTONDBLCLK ||
501         raw_message == WM_RBUTTONDBLCLK ||
502         raw_message == WM_MBUTTONDBLCLK)
503     {
504         raw_message += WM_LBUTTONDOWN - WM_LBUTTONDBLCLK;
505     }
506
507     if (remove) update_queue_key_state( raw_message, 0, 0 );
508
509     hook.pt           = msg->pt;
510     hook.hwnd         = msg->hwnd;
511     hook.wHitTestCode = hittest;
512     hook.dwExtraInfo  = extra_info;
513     if (HOOK_CallHooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
514                         msg->message, (LPARAM)&hook, TRUE ))
515     {
516         hook.pt           = msg->pt;
517         hook.hwnd         = msg->hwnd;
518         hook.wHitTestCode = hittest;
519         hook.dwExtraInfo  = extra_info;
520         HOOK_CallHooks( WH_CBT, HCBT_CLICKSKIPPED, msg->message, (LPARAM)&hook, TRUE );
521         return FALSE;
522     }
523
524     if ((hittest == HTERROR) || (hittest == HTNOWHERE))
525     {
526         SendMessageA( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
527                       MAKELONG( hittest, raw_message ));
528         return FALSE;
529     }
530
531     if (!remove || GetCapture()) return TRUE;
532
533     eatMsg = FALSE;
534
535     if ((raw_message == WM_LBUTTONDOWN) ||
536         (raw_message == WM_RBUTTONDOWN) ||
537         (raw_message == WM_MBUTTONDOWN))
538     {
539         /* Send the WM_PARENTNOTIFY,
540          * note that even for double/nonclient clicks
541          * notification message is still WM_L/M/RBUTTONDOWN.
542          */
543         MSG_SendParentNotify( msg->hwnd, raw_message, 0, msg->pt );
544
545         /* Activate the window if needed */
546
547         if (msg->hwnd != GetActiveWindow())
548         {
549             HWND hwndTop = msg->hwnd;
550             while (hwndTop)
551             {
552                 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
553                 hwndTop = GetParent( hwndTop );
554             }
555
556             if (hwndTop && hwndTop != GetDesktopWindow())
557             {
558                 LONG ret = SendMessageA( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
559                                          MAKELONG( hittest, raw_message ) );
560                 switch(ret)
561                 {
562                 case MA_NOACTIVATEANDEAT:
563                     eatMsg = TRUE;
564                     /* fall through */
565                 case MA_NOACTIVATE:
566                     break;
567                 case MA_ACTIVATEANDEAT:
568                     eatMsg = TRUE;
569                     /* fall through */
570                 case MA_ACTIVATE:
571                 case 0:
572                     if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
573                     break;
574                 default:
575                     WARN( "unknown WM_MOUSEACTIVATE code %ld\n", ret );
576                     break;
577                 }
578             }
579         }
580     }
581
582     /* send the WM_SETCURSOR message */
583
584     /* Windows sends the normal mouse message as the message parameter
585        in the WM_SETCURSOR message even if it's non-client mouse message */
586     SendMessageA( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
587                   MAKELONG( hittest, raw_message ));
588
589     return !eatMsg;
590 }
591
592
593 /***********************************************************************
594  *          process_hardware_message
595  *
596  * returns TRUE if the contents of 'msg' should be passed to the application
597  */
598 BOOL MSG_process_raw_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd_filter,
599                                        UINT first, UINT last, BOOL remove )
600 {
601     if (is_keyboard_message( msg->message ))
602     {
603         if (!process_raw_keyboard_message( msg, extra_info )) return FALSE;
604     }
605     else if (is_mouse_message( msg->message ))
606     {
607         if (!process_raw_mouse_message( msg, extra_info )) return FALSE;
608     }
609     else
610     {
611         ERR( "unknown message type %x\n", msg->message );
612         return FALSE;
613     }
614
615     /* check destination thread and filters */
616     if (!check_message_filter( msg, hwnd_filter, first, last ) ||
617         !WIN_IsCurrentThread( msg->hwnd ))
618     {
619         /* queue it for later, or for another thread */
620         queue_hardware_message( msg, extra_info, MSG_HARDWARE_COOKED );
621         return FALSE;
622     }
623
624     /* save the message in the cooked queue if we didn't want to remove it */
625     if (!remove) queue_hardware_message( msg, extra_info, MSG_HARDWARE_COOKED );
626     return TRUE;
627 }
628
629
630 /***********************************************************************
631  *          MSG_process_cooked_hardware_message
632  *
633  * returns TRUE if the contents of 'msg' should be passed to the application
634  */
635 BOOL MSG_process_cooked_hardware_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
636 {
637     if (is_keyboard_message( msg->message ))
638         return process_cooked_keyboard_message( msg, remove );
639
640     if (is_mouse_message( msg->message ))
641         return process_cooked_mouse_message( msg, extra_info, remove );
642
643     ERR( "unknown message type %x\n", msg->message );
644     return FALSE;
645 }
646
647
648 /**********************************************************************
649  *              GetKeyState (USER.106)
650  */
651 INT16 WINAPI GetKeyState16(INT16 vkey)
652 {
653     return GetKeyState(vkey);
654 }
655
656
657 /**********************************************************************
658  *              GetKeyState (USER32.@)
659  *
660  * An application calls the GetKeyState function in response to a
661  * keyboard-input message.  This function retrieves the state of the key
662  * at the time the input message was generated.  (SDK 3.1 Vol 2. p 390)
663  */
664 SHORT WINAPI GetKeyState(INT vkey)
665 {
666     INT retval;
667
668     if (vkey >= 'a' && vkey <= 'z') vkey += 'A' - 'a';
669     retval = ((WORD)(QueueKeyStateTable[vkey] & 0x80) << 8 ) |
670               (QueueKeyStateTable[vkey] & 0x80) |
671               (QueueKeyStateTable[vkey] & 0x01);
672     TRACE("key (0x%x) -> %x\n", vkey, retval);
673     return retval;
674 }
675
676
677 /**********************************************************************
678  *              GetKeyboardState (USER.222)
679  *              GetKeyboardState (USER32.@)
680  *
681  * An application calls the GetKeyboardState function in response to a
682  * keyboard input message.  This function retrieves the state of the keyboard
683  * at the time the input message was generated.  (SDK 3.1 Vol 2. p 387)
684  */
685 BOOL WINAPI GetKeyboardState(LPBYTE lpKeyState)
686 {
687     TRACE_(key)("(%p)\n", lpKeyState);
688     if (lpKeyState) memcpy(lpKeyState, QueueKeyStateTable, 256);
689     return TRUE;
690 }
691
692
693 /**********************************************************************
694  *              SetKeyboardState (USER.223)
695  *              SetKeyboardState (USER32.@)
696  */
697 BOOL WINAPI SetKeyboardState(LPBYTE lpKeyState)
698 {
699     TRACE_(key)("(%p)\n", lpKeyState);
700     if (lpKeyState) memcpy(QueueKeyStateTable, lpKeyState, 256);
701     return TRUE;
702 }
703
704
705 /***********************************************************************
706  *              WaitMessage (USER.112) Suspend thread pending messages
707  *              WaitMessage (USER32.@) Suspend thread pending messages
708  *
709  * WaitMessage() suspends a thread until events appear in the thread's
710  * queue.
711  */
712 BOOL WINAPI WaitMessage(void)
713 {
714     return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
715 }
716
717
718 /***********************************************************************
719  *              MsgWaitForMultipleObjectsEx   (USER32.@)
720  */
721 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
722                                           DWORD timeout, DWORD mask, DWORD flags )
723 {
724     HANDLE handles[MAXIMUM_WAIT_OBJECTS];
725     DWORD i, ret, lock;
726     MESSAGEQUEUE *msgQueue;
727
728     if (count > MAXIMUM_WAIT_OBJECTS-1)
729     {
730         SetLastError( ERROR_INVALID_PARAMETER );
731         return WAIT_FAILED;
732     }
733
734     if (!(msgQueue = QUEUE_Current())) return WAIT_FAILED;
735
736     /* set the queue mask */
737     SERVER_START_REQ( set_queue_mask )
738     {
739         req->wake_mask    = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
740         req->changed_mask = mask;
741         req->skip_wait    = 0;
742         wine_server_call( req );
743     }
744     SERVER_END_REQ;
745
746     /* Add the thread event to the handle list */
747     for (i = 0; i < count; i++) handles[i] = pHandles[i];
748     handles[count] = msgQueue->server_queue;
749
750     ReleaseThunkLock( &lock );
751     if (USER_Driver.pMsgWaitForMultipleObjectsEx)
752     {
753         ret = USER_Driver.pMsgWaitForMultipleObjectsEx( count+1, handles, timeout, mask, flags );
754         if (ret == count+1) ret = count; /* pretend the msg queue is ready */
755     }
756     else
757         ret = WaitForMultipleObjectsEx( count+1, handles, flags & MWMO_WAITALL,
758                                         timeout, flags & MWMO_ALERTABLE );
759     if (lock) RestoreThunkLock( lock );
760     return ret;
761 }
762
763
764 /***********************************************************************
765  *              MsgWaitForMultipleObjects (USER32.@)
766  */
767 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
768                                         BOOL wait_all, DWORD timeout, DWORD mask )
769 {
770     return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
771                                         wait_all ? MWMO_WAITALL : 0 );
772 }
773
774
775 /***********************************************************************
776  *              WaitForInputIdle (USER32.@)
777  */
778 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
779 {
780     DWORD start_time, elapsed, ret;
781     HANDLE idle_event = (HANDLE)-1;
782
783     SERVER_START_REQ( wait_input_idle )
784     {
785         req->handle = hProcess;
786         req->timeout = dwTimeOut;
787         if (!(ret = wine_server_call_err( req ))) idle_event = reply->event;
788     }
789     SERVER_END_REQ;
790     if (ret) return WAIT_FAILED;  /* error */
791     if (!idle_event) return 0;  /* no event to wait on */
792
793     start_time = GetTickCount();
794     elapsed = 0;
795
796     TRACE("waiting for %x\n", idle_event );
797     do
798     {
799         ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
800         switch (ret)
801         {
802         case WAIT_OBJECT_0+1:
803             process_sent_messages();
804             break;
805         case WAIT_TIMEOUT:
806         case WAIT_FAILED:
807             TRACE("timeout or error\n");
808             return ret;
809         default:
810             TRACE("finished\n");
811             return 0;
812         }
813         if (dwTimeOut != INFINITE)
814         {
815             elapsed = GetTickCount() - start_time;
816             if (elapsed > dwTimeOut)
817                 break;
818         }
819     }
820     while (1);
821
822     return WAIT_TIMEOUT;
823 }
824
825
826 /***********************************************************************
827  *              UserYield (USER.332)
828  */
829 void WINAPI UserYield16(void)
830 {
831    DWORD count;
832
833     /* Handle sent messages */
834     process_sent_messages();
835
836     /* Yield */
837     ReleaseThunkLock(&count);
838     if (count)
839     {
840         RestoreThunkLock(count);
841         /* Handle sent messages again */
842         process_sent_messages();
843     }
844 }
845
846
847 struct accent_char
848 {
849     BYTE ac_accent;
850     BYTE ac_char;
851     BYTE ac_result;
852 };
853
854 static const struct accent_char accent_chars[] =
855 {
856 /* A good idea should be to read /usr/X11/lib/X11/locale/iso8859-x/Compose */
857     {'`', 'A', '\300'},  {'`', 'a', '\340'},
858     {'\'', 'A', '\301'}, {'\'', 'a', '\341'},
859     {'^', 'A', '\302'},  {'^', 'a', '\342'},
860     {'~', 'A', '\303'},  {'~', 'a', '\343'},
861     {'"', 'A', '\304'},  {'"', 'a', '\344'},
862     {'O', 'A', '\305'},  {'o', 'a', '\345'},
863     {'0', 'A', '\305'},  {'0', 'a', '\345'},
864     {'A', 'A', '\305'},  {'a', 'a', '\345'},
865     {'A', 'E', '\306'},  {'a', 'e', '\346'},
866     {',', 'C', '\307'},  {',', 'c', '\347'},
867     {'`', 'E', '\310'},  {'`', 'e', '\350'},
868     {'\'', 'E', '\311'}, {'\'', 'e', '\351'},
869     {'^', 'E', '\312'},  {'^', 'e', '\352'},
870     {'"', 'E', '\313'},  {'"', 'e', '\353'},
871     {'`', 'I', '\314'},  {'`', 'i', '\354'},
872     {'\'', 'I', '\315'}, {'\'', 'i', '\355'},
873     {'^', 'I', '\316'},  {'^', 'i', '\356'},
874     {'"', 'I', '\317'},  {'"', 'i', '\357'},
875     {'-', 'D', '\320'},  {'-', 'd', '\360'},
876     {'~', 'N', '\321'},  {'~', 'n', '\361'},
877     {'`', 'O', '\322'},  {'`', 'o', '\362'},
878     {'\'', 'O', '\323'}, {'\'', 'o', '\363'},
879     {'^', 'O', '\324'},  {'^', 'o', '\364'},
880     {'~', 'O', '\325'},  {'~', 'o', '\365'},
881     {'"', 'O', '\326'},  {'"', 'o', '\366'},
882     {'/', 'O', '\330'},  {'/', 'o', '\370'},
883     {'`', 'U', '\331'},  {'`', 'u', '\371'},
884     {'\'', 'U', '\332'}, {'\'', 'u', '\372'},
885     {'^', 'U', '\333'},  {'^', 'u', '\373'},
886     {'"', 'U', '\334'},  {'"', 'u', '\374'},
887     {'\'', 'Y', '\335'}, {'\'', 'y', '\375'},
888     {'T', 'H', '\336'},  {'t', 'h', '\376'},
889     {'s', 's', '\337'},  {'"', 'y', '\377'},
890     {'s', 'z', '\337'},  {'i', 'j', '\377'},
891         /* iso-8859-2 uses this */
892     {'<', 'L', '\245'},  {'<', 'l', '\265'},    /* caron */
893     {'<', 'S', '\251'},  {'<', 's', '\271'},
894     {'<', 'T', '\253'},  {'<', 't', '\273'},
895     {'<', 'Z', '\256'},  {'<', 'z', '\276'},
896     {'<', 'C', '\310'},  {'<', 'c', '\350'},
897     {'<', 'E', '\314'},  {'<', 'e', '\354'},
898     {'<', 'D', '\317'},  {'<', 'd', '\357'},
899     {'<', 'N', '\322'},  {'<', 'n', '\362'},
900     {'<', 'R', '\330'},  {'<', 'r', '\370'},
901     {';', 'A', '\241'},  {';', 'a', '\261'},    /* ogonek */
902     {';', 'E', '\312'},  {';', 'e', '\332'},
903     {'\'', 'Z', '\254'}, {'\'', 'z', '\274'},   /* acute */
904     {'\'', 'R', '\300'}, {'\'', 'r', '\340'},
905     {'\'', 'L', '\305'}, {'\'', 'l', '\345'},
906     {'\'', 'C', '\306'}, {'\'', 'c', '\346'},
907     {'\'', 'N', '\321'}, {'\'', 'n', '\361'},
908 /*  collision whith S, from iso-8859-9 !!! */
909     {',', 'S', '\252'},  {',', 's', '\272'},    /* cedilla */
910     {',', 'T', '\336'},  {',', 't', '\376'},
911     {'.', 'Z', '\257'},  {'.', 'z', '\277'},    /* dot above */
912     {'/', 'L', '\243'},  {'/', 'l', '\263'},    /* slash */
913     {'/', 'D', '\320'},  {'/', 'd', '\360'},
914     {'(', 'A', '\303'},  {'(', 'a', '\343'},    /* breve */
915     {'\275', 'O', '\325'}, {'\275', 'o', '\365'},       /* double acute */
916     {'\275', 'U', '\334'}, {'\275', 'u', '\374'},
917     {'0', 'U', '\332'},  {'0', 'u', '\372'},    /* ring above */
918         /* iso-8859-3 uses this */
919     {'/', 'H', '\241'},  {'/', 'h', '\261'},    /* slash */
920     {'>', 'H', '\246'},  {'>', 'h', '\266'},    /* circumflex */
921     {'>', 'J', '\254'},  {'>', 'j', '\274'},
922     {'>', 'C', '\306'},  {'>', 'c', '\346'},
923     {'>', 'G', '\330'},  {'>', 'g', '\370'},
924     {'>', 'S', '\336'},  {'>', 's', '\376'},
925 /*  collision whith G( from iso-8859-9 !!!   */
926     {'(', 'G', '\253'},  {'(', 'g', '\273'},    /* breve */
927     {'(', 'U', '\335'},  {'(', 'u', '\375'},
928 /*  collision whith I. from iso-8859-3 !!!   */
929     {'.', 'I', '\251'},  {'.', 'i', '\271'},    /* dot above */
930     {'.', 'C', '\305'},  {'.', 'c', '\345'},
931     {'.', 'G', '\325'},  {'.', 'g', '\365'},
932         /* iso-8859-4 uses this */
933     {',', 'R', '\243'},  {',', 'r', '\263'},    /* cedilla */
934     {',', 'L', '\246'},  {',', 'l', '\266'},
935     {',', 'G', '\253'},  {',', 'g', '\273'},
936     {',', 'N', '\321'},  {',', 'n', '\361'},
937     {',', 'K', '\323'},  {',', 'k', '\363'},
938     {'~', 'I', '\245'},  {'~', 'i', '\265'},    /* tilde */
939     {'-', 'E', '\252'},  {'-', 'e', '\272'},    /* macron */
940     {'-', 'A', '\300'},  {'-', 'a', '\340'},
941     {'-', 'I', '\317'},  {'-', 'i', '\357'},
942     {'-', 'O', '\322'},  {'-', 'o', '\362'},
943     {'-', 'U', '\336'},  {'-', 'u', '\376'},
944     {'/', 'T', '\254'},  {'/', 't', '\274'},    /* slash */
945     {'.', 'E', '\314'},  {'.', 'e', '\344'},    /* dot above */
946     {';', 'I', '\307'},  {';', 'i', '\347'},    /* ogonek */
947     {';', 'U', '\331'},  {';', 'u', '\371'},
948         /* iso-8859-9 uses this */
949         /* iso-8859-9 has really bad choosen G( S, and I. as they collide
950          * whith the same letters on other iso-8859-x (that is they are on
951          * different places :-( ), if you use turkish uncomment these and
952          * comment out the lines in iso-8859-2 and iso-8859-3 sections
953          * FIXME: should be dynamic according to chosen language
954          *        if/when Wine has turkish support.
955          */
956 /*  collision whith G( from iso-8859-3 !!!   */
957 /*  {'(', 'G', '\320'},  {'(', 'g', '\360'}, */ /* breve */
958 /*  collision whith S, from iso-8859-2 !!! */
959 /*  {',', 'S', '\336'},  {',', 's', '\376'}, */ /* cedilla */
960 /*  collision whith I. from iso-8859-3 !!!   */
961 /*  {'.', 'I', '\335'},  {'.', 'i', '\375'}, */ /* dot above */
962 };
963
964
965 /***********************************************************************
966  *              TranslateMessage (USER32.@)
967  *
968  * Implementation of TranslateMessage.
969  *
970  * TranslateMessage translates virtual-key messages into character-messages,
971  * as follows :
972  * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
973  * ditto replacing WM_* with WM_SYS*
974  * This produces WM_CHAR messages only for keys mapped to ASCII characters
975  * by the keyboard driver.
976  *
977  * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
978  * return value is nonzero, regardless of the translation.
979  *
980  */
981 BOOL WINAPI TranslateMessage( const MSG *msg )
982 {
983     static int dead_char;
984     UINT message;
985     WCHAR wp[2];
986     BOOL rc = FALSE;
987
988     if (msg->message >= WM_KEYFIRST && msg->message <= WM_KEYLAST)
989     {
990         TRACE_(key)("(%s, %04X, %08lX)\n",
991                     SPY_GetMsgName(msg->message, msg->hwnd), msg->wParam, msg->lParam );
992
993         /* Return code must be TRUE no matter what! */
994         rc = TRUE;
995     }
996
997     if ((msg->message != WM_KEYDOWN) && (msg->message != WM_SYSKEYDOWN)) return rc;
998
999     TRACE_(key)("Translating key %s (%04x), scancode %02x\n",
1000                  SPY_GetVKeyName(msg->wParam), msg->wParam, LOBYTE(HIWORD(msg->lParam)));
1001
1002     /* FIXME : should handle ToUnicode yielding 2 */
1003     switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), QueueKeyStateTable, wp, 2, 0))
1004     {
1005     case 1:
1006         message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
1007         /* Should dead chars handling go in ToAscii ? */
1008         if (dead_char)
1009         {
1010             int i;
1011
1012             if (wp[0] == ' ') wp[0] =  dead_char;
1013             if (dead_char == 0xa2) dead_char = '(';
1014             else if (dead_char == 0xa8) dead_char = '"';
1015             else if (dead_char == 0xb2) dead_char = ';';
1016             else if (dead_char == 0xb4) dead_char = '\'';
1017             else if (dead_char == 0xb7) dead_char = '<';
1018             else if (dead_char == 0xb8) dead_char = ',';
1019             else if (dead_char == 0xff) dead_char = '.';
1020             for (i = 0; i < sizeof(accent_chars)/sizeof(accent_chars[0]); i++)
1021                 if ((accent_chars[i].ac_accent == dead_char) &&
1022                     (accent_chars[i].ac_char == wp[0]))
1023                 {
1024                     wp[0] = accent_chars[i].ac_result;
1025                     break;
1026                 }
1027             dead_char = 0;
1028         }
1029         TRACE_(key)("1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
1030         PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
1031         break;
1032
1033     case -1:
1034         message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
1035         dead_char = wp[0];
1036         TRACE_(key)("-1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
1037         PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
1038         return TRUE;
1039     }
1040     return rc;
1041 }
1042
1043
1044 /***********************************************************************
1045  *              DispatchMessageA (USER32.@)
1046  */
1047 LONG WINAPI DispatchMessageA( const MSG* msg )
1048 {
1049     WND * wndPtr;
1050     LONG retval;
1051     int painting;
1052     WNDPROC winproc;
1053
1054       /* Process timer messages */
1055     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1056     {
1057         if (msg->lParam)
1058         {
1059 /*            HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1060
1061             /* before calling window proc, verify whether timer is still valid;
1062                there's a slim chance that the application kills the timer
1063                between GetMessage and DispatchMessage API calls */
1064             if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (HWINDOWPROC) msg->lParam))
1065                 return 0; /* invalid winproc */
1066
1067             return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
1068                                    msg->message, msg->wParam, GetTickCount() );
1069         }
1070     }
1071
1072     if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
1073     {
1074         if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1075         return 0;
1076     }
1077     if (wndPtr == WND_OTHER_PROCESS)
1078     {
1079         if (IsWindow( msg->hwnd ))
1080             ERR( "cannot dispatch msg to other process window %x\n", msg->hwnd );
1081         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1082         return 0;
1083     }
1084     if (!(winproc = wndPtr->winproc))
1085     {
1086         WIN_ReleasePtr( wndPtr );
1087         return 0;
1088     }
1089     painting = (msg->message == WM_PAINT);
1090     if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1091     WIN_ReleasePtr( wndPtr );
1092 /*    hook_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1093
1094     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1095                       msg->wParam, msg->lParam );
1096     retval = CallWindowProcA( winproc, msg->hwnd, msg->message,
1097                               msg->wParam, msg->lParam );
1098     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1099                      msg->wParam, msg->lParam );
1100
1101     if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
1102     {
1103         BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
1104         wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1105         WIN_ReleasePtr( wndPtr );
1106         if (validate)
1107         {
1108             ERR( "BeginPaint not called on WM_PAINT for hwnd %04x!\n", msg->hwnd );
1109             /* Validate the update region to avoid infinite WM_PAINT loop */
1110             RedrawWindow( msg->hwnd, NULL, 0,
1111                           RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1112         }
1113     }
1114     return retval;
1115 }
1116
1117
1118 /***********************************************************************
1119  *              DispatchMessageW (USER32.@) Process Message
1120  *
1121  * Process the message specified in the structure *_msg_.
1122  *
1123  * If the lpMsg parameter points to a WM_TIMER message and the
1124  * parameter of the WM_TIMER message is not NULL, the lParam parameter
1125  * points to the function that is called instead of the window
1126  * procedure.
1127  *
1128  * The message must be valid.
1129  *
1130  * RETURNS
1131  *
1132  *   DispatchMessage() returns the result of the window procedure invoked.
1133  *
1134  * CONFORMANCE
1135  *
1136  *   ECMA-234, Win32
1137  *
1138  */
1139 LONG WINAPI DispatchMessageW( const MSG* msg )
1140 {
1141     WND * wndPtr;
1142     LONG retval;
1143     int painting;
1144     WNDPROC winproc;
1145
1146       /* Process timer messages */
1147     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1148     {
1149         if (msg->lParam)
1150         {
1151 /*            HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1152
1153             /* before calling window proc, verify whether timer is still valid;
1154                there's a slim chance that the application kills the timer
1155                between GetMessage and DispatchMessage API calls */
1156             if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (HWINDOWPROC) msg->lParam))
1157                 return 0; /* invalid winproc */
1158
1159             return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
1160                                    msg->message, msg->wParam, GetTickCount() );
1161         }
1162     }
1163
1164     if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
1165     {
1166         if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1167         return 0;
1168     }
1169     if (wndPtr == WND_OTHER_PROCESS)
1170     {
1171         if (IsWindow( msg->hwnd ))
1172             ERR( "cannot dispatch msg to other process window %x\n", msg->hwnd );
1173         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1174         return 0;
1175     }
1176     if (!(winproc = wndPtr->winproc))
1177     {
1178         WIN_ReleasePtr( wndPtr );
1179         return 0;
1180     }
1181     painting = (msg->message == WM_PAINT);
1182     if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1183     WIN_ReleasePtr( wndPtr );
1184 /*    HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1185
1186     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1187                       msg->wParam, msg->lParam );
1188     retval = CallWindowProcW( winproc, msg->hwnd, msg->message,
1189                               msg->wParam, msg->lParam );
1190     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1191                      msg->wParam, msg->lParam );
1192
1193     if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
1194     {
1195         BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
1196         wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1197         WIN_ReleasePtr( wndPtr );
1198         if (validate)
1199         {
1200             ERR( "BeginPaint not called on WM_PAINT for hwnd %04x!\n", msg->hwnd );
1201             /* Validate the update region to avoid infinite WM_PAINT loop */
1202             RedrawWindow( msg->hwnd, NULL, 0,
1203                           RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1204         }
1205     }
1206     return retval;
1207 }
1208
1209
1210 /***********************************************************************
1211  *              RegisterWindowMessage (USER.118)
1212  *              RegisterWindowMessageA (USER32.@)
1213  */
1214 WORD WINAPI RegisterWindowMessageA( LPCSTR str )
1215 {
1216     TRACE("%s\n", str );
1217     return GlobalAddAtomA( str );
1218 }
1219
1220
1221 /***********************************************************************
1222  *              RegisterWindowMessageW (USER32.@)
1223  */
1224 WORD WINAPI RegisterWindowMessageW( LPCWSTR str )
1225 {
1226     TRACE("%p\n", str );
1227     return GlobalAddAtomW( str );
1228 }
1229
1230
1231 /***********************************************************************
1232  *              BroadcastSystemMessage  (USER32.@)
1233  *              BroadcastSystemMessageA (USER32.@)
1234  */
1235 LONG WINAPI BroadcastSystemMessage(
1236         DWORD dwFlags,LPDWORD recipients,UINT uMessage,WPARAM wParam,
1237         LPARAM lParam )
1238 {
1239     if ((*recipients & BSM_APPLICATIONS)||
1240         (*recipients == BSM_ALLCOMPONENTS))
1241     {
1242         FIXME("(%08lx,%08lx,%08x,%08x,%08lx): semi-stub!\n",
1243               dwFlags,*recipients,uMessage,wParam,lParam);
1244         PostMessageA(HWND_BROADCAST,uMessage,wParam,lParam);
1245         return 1;
1246     }
1247     else
1248     {
1249         FIXME("(%08lx,%08lx,%08x,%08x,%08lx): stub!\n",
1250               dwFlags,*recipients,uMessage,wParam,lParam);
1251         return -1;
1252     }
1253 }