Grab the thread object in case the thread terminates with pending
[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 "controls.h"
38 #include "dde.h"
39 #include "heap.h"
40 #include "message.h"
41 #include "thread.h"
42 #include "user.h"
43 #include "win.h"
44 #include "winpos.h"
45 #include "winproc.h"
46 #include "wine/debug.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(msg);
49 WINE_DECLARE_DEBUG_CHANNEL(key);
50
51 #define WM_NCMOUSEFIRST         WM_NCMOUSEMOVE
52 #define WM_NCMOUSELAST          WM_NCMBUTTONDBLCLK
53
54 static BYTE QueueKeyStateTable[256];
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  *           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 );
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 );
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, BOOL remove )
405 {
406     static MSG clk_msg;
407
408     POINT pt;
409     INT hittest;
410     EVENTMSG event;
411     GUITHREADINFO info;
412     HWND hWndScope = msg->hwnd;
413
414     /* find the window to dispatch this mouse message to */
415
416     hittest = HTCLIENT;
417     GetGUIThreadInfo( GetCurrentThreadId(), &info );
418     if (!(msg->hwnd = info.hwndCapture))
419     {
420         /* If no capture HWND, find window which contains the mouse position.
421          * Also find the position of the cursor hot spot (hittest) */
422         if (!IsWindow(hWndScope)) hWndScope = 0;
423         if (!(msg->hwnd = WINPOS_WindowFromPoint( hWndScope, msg->pt, &hittest )))
424             msg->hwnd = GetDesktopWindow();
425     }
426
427     event.message = msg->message;
428     event.time    = msg->time;
429     event.hwnd    = msg->hwnd;
430     event.paramL  = msg->pt.x;
431     event.paramH  = msg->pt.y;
432     HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
433
434     /* translate double clicks */
435
436     if ((msg->message == WM_LBUTTONDOWN) ||
437         (msg->message == WM_RBUTTONDOWN) ||
438         (msg->message == WM_MBUTTONDOWN))
439     {
440         BOOL update = remove;
441         /* translate double clicks -
442          * note that ...MOUSEMOVEs can slip in between
443          * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
444
445         if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
446             hittest != HTCLIENT ||
447             (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
448         {
449            if ((msg->message == clk_msg.message) &&
450                (msg->hwnd == clk_msg.hwnd) &&
451                (msg->time - clk_msg.time < GetDoubleClickTime()) &&
452                (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
453                (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
454            {
455                msg->message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
456                if (remove)
457                {
458                    clk_msg.message = 0;
459                    update = FALSE;
460                }
461            }
462         }
463         /* update static double click conditions */
464         if (update) clk_msg = *msg;
465     }
466
467     pt = msg->pt;
468     /* Note: windows has no concept of a non-client wheel message */
469     if (hittest != HTCLIENT && msg->message != WM_MOUSEWHEEL)
470     {
471         msg->message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
472         msg->wParam = hittest;
473     }
474     else
475     {
476         /* coordinates don't get translated while tracking a menu */
477         /* FIXME: should differentiate popups and top-level menus */
478         if (!(info.flags & GUI_INMENUMODE)) ScreenToClient( msg->hwnd, &pt );
479     }
480     msg->lParam = MAKELONG( pt.x, pt.y );
481     return TRUE;
482 }
483
484
485 /***********************************************************************
486  *          process_cooked_mouse_message
487  *
488  * returns TRUE if the contents of 'msg' should be passed to the application
489  */
490 static BOOL process_cooked_mouse_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
491 {
492     MOUSEHOOKSTRUCT hook;
493     INT hittest = HTCLIENT;
494     UINT raw_message = msg->message;
495     BOOL eatMsg;
496
497     if (msg->message >= WM_NCMOUSEFIRST && msg->message <= WM_NCMOUSELAST)
498     {
499         raw_message += WM_MOUSEFIRST - WM_NCMOUSEFIRST;
500         hittest = msg->wParam;
501     }
502     if (raw_message == WM_LBUTTONDBLCLK ||
503         raw_message == WM_RBUTTONDBLCLK ||
504         raw_message == WM_MBUTTONDBLCLK)
505     {
506         raw_message += WM_LBUTTONDOWN - WM_LBUTTONDBLCLK;
507     }
508
509     if (remove) update_queue_key_state( raw_message, 0, 0 );
510
511     hook.pt           = msg->pt;
512     hook.hwnd         = msg->hwnd;
513     hook.wHitTestCode = hittest;
514     hook.dwExtraInfo  = extra_info;
515     if (HOOK_CallHooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
516                         msg->message, (LPARAM)&hook, TRUE ))
517     {
518         hook.pt           = msg->pt;
519         hook.hwnd         = msg->hwnd;
520         hook.wHitTestCode = hittest;
521         hook.dwExtraInfo  = extra_info;
522         HOOK_CallHooks( WH_CBT, HCBT_CLICKSKIPPED, msg->message, (LPARAM)&hook, TRUE );
523         return FALSE;
524     }
525
526     if ((hittest == HTERROR) || (hittest == HTNOWHERE))
527     {
528         SendMessageA( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
529                       MAKELONG( hittest, raw_message ));
530         return FALSE;
531     }
532
533     if (!remove || GetCapture()) return TRUE;
534
535     eatMsg = FALSE;
536
537     if ((raw_message == WM_LBUTTONDOWN) ||
538         (raw_message == WM_RBUTTONDOWN) ||
539         (raw_message == WM_MBUTTONDOWN))
540     {
541         /* Send the WM_PARENTNOTIFY,
542          * note that even for double/nonclient clicks
543          * notification message is still WM_L/M/RBUTTONDOWN.
544          */
545         MSG_SendParentNotify( msg->hwnd, raw_message, 0, msg->pt );
546
547         /* Activate the window if needed */
548
549         if (msg->hwnd != GetActiveWindow())
550         {
551             HWND hwndTop = msg->hwnd;
552             while (hwndTop)
553             {
554                 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
555                 hwndTop = GetParent( hwndTop );
556             }
557
558             if (hwndTop && hwndTop != GetDesktopWindow())
559             {
560                 LONG ret = SendMessageA( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
561                                          MAKELONG( hittest, raw_message ) );
562                 switch(ret)
563                 {
564                 case MA_NOACTIVATEANDEAT:
565                     eatMsg = TRUE;
566                     /* fall through */
567                 case MA_NOACTIVATE:
568                     break;
569                 case MA_ACTIVATEANDEAT:
570                     eatMsg = TRUE;
571                     /* fall through */
572                 case MA_ACTIVATE:
573                 case 0:
574                     if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
575                     break;
576                 default:
577                     WARN( "unknown WM_MOUSEACTIVATE code %ld\n", ret );
578                     break;
579                 }
580             }
581         }
582     }
583
584     /* send the WM_SETCURSOR message */
585
586     /* Windows sends the normal mouse message as the message parameter
587        in the WM_SETCURSOR message even if it's non-client mouse message */
588     SendMessageA( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
589                   MAKELONG( hittest, raw_message ));
590
591     return !eatMsg;
592 }
593
594
595 /***********************************************************************
596  *          process_hardware_message
597  *
598  * returns TRUE if the contents of 'msg' should be passed to the application
599  */
600 BOOL MSG_process_raw_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd_filter,
601                                        UINT first, UINT last, BOOL remove )
602 {
603     if (is_keyboard_message( msg->message ))
604     {
605         if (!process_raw_keyboard_message( msg, extra_info )) return FALSE;
606     }
607     else if (is_mouse_message( msg->message ))
608     {
609         if (!process_raw_mouse_message( msg, extra_info, remove )) return FALSE;
610     }
611     else
612     {
613         ERR( "unknown message type %x\n", msg->message );
614         return FALSE;
615     }
616     return check_message_filter( msg, hwnd_filter, first, last );
617 }
618
619
620 /***********************************************************************
621  *          MSG_process_cooked_hardware_message
622  *
623  * returns TRUE if the contents of 'msg' should be passed to the application
624  */
625 BOOL MSG_process_cooked_hardware_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
626 {
627     if (is_keyboard_message( msg->message ))
628         return process_cooked_keyboard_message( msg, remove );
629
630     if (is_mouse_message( msg->message ))
631         return process_cooked_mouse_message( msg, extra_info, remove );
632
633     ERR( "unknown message type %x\n", msg->message );
634     return FALSE;
635 }
636
637
638 /**********************************************************************
639  *              GetKeyState (USER.106)
640  */
641 INT16 WINAPI GetKeyState16(INT16 vkey)
642 {
643     return GetKeyState(vkey);
644 }
645
646
647 /**********************************************************************
648  *              GetKeyState (USER32.@)
649  *
650  * An application calls the GetKeyState function in response to a
651  * keyboard-input message.  This function retrieves the state of the key
652  * at the time the input message was generated.  (SDK 3.1 Vol 2. p 390)
653  */
654 SHORT WINAPI GetKeyState(INT vkey)
655 {
656     INT retval;
657
658     if (vkey >= 'a' && vkey <= 'z') vkey += 'A' - 'a';
659     retval = ((WORD)(QueueKeyStateTable[vkey] & 0x80) << 8 ) |
660               (QueueKeyStateTable[vkey] & 0x80) |
661               (QueueKeyStateTable[vkey] & 0x01);
662     TRACE("key (0x%x) -> %x\n", vkey, retval);
663     return retval;
664 }
665
666
667 /**********************************************************************
668  *              GetKeyboardState (USER.222)
669  *              GetKeyboardState (USER32.@)
670  *
671  * An application calls the GetKeyboardState function in response to a
672  * keyboard input message.  This function retrieves the state of the keyboard
673  * at the time the input message was generated.  (SDK 3.1 Vol 2. p 387)
674  */
675 BOOL WINAPI GetKeyboardState(LPBYTE lpKeyState)
676 {
677     TRACE_(key)("(%p)\n", lpKeyState);
678     if (lpKeyState) memcpy(lpKeyState, QueueKeyStateTable, 256);
679     return TRUE;
680 }
681
682
683 /**********************************************************************
684  *              SetKeyboardState (USER.223)
685  *              SetKeyboardState (USER32.@)
686  */
687 BOOL WINAPI SetKeyboardState(LPBYTE lpKeyState)
688 {
689     TRACE_(key)("(%p)\n", lpKeyState);
690     if (lpKeyState) memcpy(QueueKeyStateTable, lpKeyState, 256);
691     return TRUE;
692 }
693
694
695 /***********************************************************************
696  *              WaitMessage (USER.112) Suspend thread pending messages
697  *              WaitMessage (USER32.@) Suspend thread pending messages
698  *
699  * WaitMessage() suspends a thread until events appear in the thread's
700  * queue.
701  */
702 BOOL WINAPI WaitMessage(void)
703 {
704     return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
705 }
706
707
708 /***********************************************************************
709  *              MsgWaitForMultipleObjectsEx   (USER32.@)
710  */
711 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
712                                           DWORD timeout, DWORD mask, DWORD flags )
713 {
714     HANDLE handles[MAXIMUM_WAIT_OBJECTS];
715     DWORD i, ret, lock;
716     MESSAGEQUEUE *msgQueue;
717
718     if (count > MAXIMUM_WAIT_OBJECTS-1)
719     {
720         SetLastError( ERROR_INVALID_PARAMETER );
721         return WAIT_FAILED;
722     }
723
724     if (!(msgQueue = QUEUE_Current())) return WAIT_FAILED;
725
726     /* set the queue mask */
727     SERVER_START_REQ( set_queue_mask )
728     {
729         req->wake_mask    = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
730         req->changed_mask = mask;
731         req->skip_wait    = 0;
732         wine_server_call( req );
733     }
734     SERVER_END_REQ;
735
736     /* Add the thread event to the handle list */
737     for (i = 0; i < count; i++) handles[i] = pHandles[i];
738     handles[count] = msgQueue->server_queue;
739
740     ReleaseThunkLock( &lock );
741     if (USER_Driver.pMsgWaitForMultipleObjectsEx)
742     {
743         ret = USER_Driver.pMsgWaitForMultipleObjectsEx( count+1, handles, timeout, mask, flags );
744         if (ret == count+1) ret = count; /* pretend the msg queue is ready */
745     }
746     else
747         ret = WaitForMultipleObjectsEx( count+1, handles, flags & MWMO_WAITALL,
748                                         timeout, flags & MWMO_ALERTABLE );
749     if (lock) RestoreThunkLock( lock );
750     return ret;
751 }
752
753
754 /***********************************************************************
755  *              MsgWaitForMultipleObjects (USER32.@)
756  */
757 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
758                                         BOOL wait_all, DWORD timeout, DWORD mask )
759 {
760     return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
761                                         wait_all ? MWMO_WAITALL : 0 );
762 }
763
764
765 /***********************************************************************
766  *              WaitForInputIdle (USER32.@)
767  */
768 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
769 {
770     DWORD start_time, elapsed, ret;
771     HANDLE idle_event = (HANDLE)-1;
772
773     SERVER_START_REQ( wait_input_idle )
774     {
775         req->handle = hProcess;
776         req->timeout = dwTimeOut;
777         if (!(ret = wine_server_call_err( req ))) idle_event = reply->event;
778     }
779     SERVER_END_REQ;
780     if (ret) return WAIT_FAILED;  /* error */
781     if (!idle_event) return 0;  /* no event to wait on */
782
783     start_time = GetTickCount();
784     elapsed = 0;
785
786     TRACE("waiting for %p\n", idle_event );
787     do
788     {
789         ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
790         switch (ret)
791         {
792         case WAIT_OBJECT_0+1:
793             process_sent_messages();
794             break;
795         case WAIT_TIMEOUT:
796         case WAIT_FAILED:
797             TRACE("timeout or error\n");
798             return ret;
799         default:
800             TRACE("finished\n");
801             return 0;
802         }
803         if (dwTimeOut != INFINITE)
804         {
805             elapsed = GetTickCount() - start_time;
806             if (elapsed > dwTimeOut)
807                 break;
808         }
809     }
810     while (1);
811
812     return WAIT_TIMEOUT;
813 }
814
815
816 /***********************************************************************
817  *              UserYield (USER.332)
818  */
819 void WINAPI UserYield16(void)
820 {
821    DWORD count;
822
823     /* Handle sent messages */
824     process_sent_messages();
825
826     /* Yield */
827     ReleaseThunkLock(&count);
828     if (count)
829     {
830         RestoreThunkLock(count);
831         /* Handle sent messages again */
832         process_sent_messages();
833     }
834 }
835
836
837 struct accent_char
838 {
839     BYTE ac_accent;
840     BYTE ac_char;
841     BYTE ac_result;
842 };
843
844 static const struct accent_char accent_chars[] =
845 {
846 /* A good idea should be to read /usr/X11/lib/X11/locale/iso8859-x/Compose */
847     {'`', 'A', '\300'},  {'`', 'a', '\340'},
848     {'\'', 'A', '\301'}, {'\'', 'a', '\341'},
849     {'^', 'A', '\302'},  {'^', 'a', '\342'},
850     {'~', 'A', '\303'},  {'~', 'a', '\343'},
851     {'"', 'A', '\304'},  {'"', 'a', '\344'},
852     {'O', 'A', '\305'},  {'o', 'a', '\345'},
853     {'0', 'A', '\305'},  {'0', 'a', '\345'},
854     {'A', 'A', '\305'},  {'a', 'a', '\345'},
855     {'A', 'E', '\306'},  {'a', 'e', '\346'},
856     {',', 'C', '\307'},  {',', 'c', '\347'},
857     {'`', 'E', '\310'},  {'`', 'e', '\350'},
858     {'\'', 'E', '\311'}, {'\'', 'e', '\351'},
859     {'^', 'E', '\312'},  {'^', 'e', '\352'},
860     {'"', 'E', '\313'},  {'"', 'e', '\353'},
861     {'`', 'I', '\314'},  {'`', 'i', '\354'},
862     {'\'', 'I', '\315'}, {'\'', 'i', '\355'},
863     {'^', 'I', '\316'},  {'^', 'i', '\356'},
864     {'"', 'I', '\317'},  {'"', 'i', '\357'},
865     {'-', 'D', '\320'},  {'-', 'd', '\360'},
866     {'~', 'N', '\321'},  {'~', 'n', '\361'},
867     {'`', 'O', '\322'},  {'`', 'o', '\362'},
868     {'\'', 'O', '\323'}, {'\'', 'o', '\363'},
869     {'^', 'O', '\324'},  {'^', 'o', '\364'},
870     {'~', 'O', '\325'},  {'~', 'o', '\365'},
871     {'"', 'O', '\326'},  {'"', 'o', '\366'},
872     {'/', 'O', '\330'},  {'/', 'o', '\370'},
873     {'`', 'U', '\331'},  {'`', 'u', '\371'},
874     {'\'', 'U', '\332'}, {'\'', 'u', '\372'},
875     {'^', 'U', '\333'},  {'^', 'u', '\373'},
876     {'"', 'U', '\334'},  {'"', 'u', '\374'},
877     {'\'', 'Y', '\335'}, {'\'', 'y', '\375'},
878     {'T', 'H', '\336'},  {'t', 'h', '\376'},
879     {'s', 's', '\337'},  {'"', 'y', '\377'},
880     {'s', 'z', '\337'},  {'i', 'j', '\377'},
881         /* iso-8859-2 uses this */
882     {'<', 'L', '\245'},  {'<', 'l', '\265'},    /* caron */
883     {'<', 'S', '\251'},  {'<', 's', '\271'},
884     {'<', 'T', '\253'},  {'<', 't', '\273'},
885     {'<', 'Z', '\256'},  {'<', 'z', '\276'},
886     {'<', 'C', '\310'},  {'<', 'c', '\350'},
887     {'<', 'E', '\314'},  {'<', 'e', '\354'},
888     {'<', 'D', '\317'},  {'<', 'd', '\357'},
889     {'<', 'N', '\322'},  {'<', 'n', '\362'},
890     {'<', 'R', '\330'},  {'<', 'r', '\370'},
891     {';', 'A', '\241'},  {';', 'a', '\261'},    /* ogonek */
892     {';', 'E', '\312'},  {';', 'e', '\332'},
893     {'\'', 'Z', '\254'}, {'\'', 'z', '\274'},   /* acute */
894     {'\'', 'R', '\300'}, {'\'', 'r', '\340'},
895     {'\'', 'L', '\305'}, {'\'', 'l', '\345'},
896     {'\'', 'C', '\306'}, {'\'', 'c', '\346'},
897     {'\'', 'N', '\321'}, {'\'', 'n', '\361'},
898 /*  collision whith S, from iso-8859-9 !!! */
899     {',', 'S', '\252'},  {',', 's', '\272'},    /* cedilla */
900     {',', 'T', '\336'},  {',', 't', '\376'},
901     {'.', 'Z', '\257'},  {'.', 'z', '\277'},    /* dot above */
902     {'/', 'L', '\243'},  {'/', 'l', '\263'},    /* slash */
903     {'/', 'D', '\320'},  {'/', 'd', '\360'},
904     {'(', 'A', '\303'},  {'(', 'a', '\343'},    /* breve */
905     {'\275', 'O', '\325'}, {'\275', 'o', '\365'},       /* double acute */
906     {'\275', 'U', '\334'}, {'\275', 'u', '\374'},
907     {'0', 'U', '\332'},  {'0', 'u', '\372'},    /* ring above */
908         /* iso-8859-3 uses this */
909     {'/', 'H', '\241'},  {'/', 'h', '\261'},    /* slash */
910     {'>', 'H', '\246'},  {'>', 'h', '\266'},    /* circumflex */
911     {'>', 'J', '\254'},  {'>', 'j', '\274'},
912     {'>', 'C', '\306'},  {'>', 'c', '\346'},
913     {'>', 'G', '\330'},  {'>', 'g', '\370'},
914     {'>', 'S', '\336'},  {'>', 's', '\376'},
915 /*  collision whith G( from iso-8859-9 !!!   */
916     {'(', 'G', '\253'},  {'(', 'g', '\273'},    /* breve */
917     {'(', 'U', '\335'},  {'(', 'u', '\375'},
918 /*  collision whith I. from iso-8859-3 !!!   */
919     {'.', 'I', '\251'},  {'.', 'i', '\271'},    /* dot above */
920     {'.', 'C', '\305'},  {'.', 'c', '\345'},
921     {'.', 'G', '\325'},  {'.', 'g', '\365'},
922         /* iso-8859-4 uses this */
923     {',', 'R', '\243'},  {',', 'r', '\263'},    /* cedilla */
924     {',', 'L', '\246'},  {',', 'l', '\266'},
925     {',', 'G', '\253'},  {',', 'g', '\273'},
926     {',', 'N', '\321'},  {',', 'n', '\361'},
927     {',', 'K', '\323'},  {',', 'k', '\363'},
928     {'~', 'I', '\245'},  {'~', 'i', '\265'},    /* tilde */
929     {'-', 'E', '\252'},  {'-', 'e', '\272'},    /* macron */
930     {'-', 'A', '\300'},  {'-', 'a', '\340'},
931     {'-', 'I', '\317'},  {'-', 'i', '\357'},
932     {'-', 'O', '\322'},  {'-', 'o', '\362'},
933     {'-', 'U', '\336'},  {'-', 'u', '\376'},
934     {'/', 'T', '\254'},  {'/', 't', '\274'},    /* slash */
935     {'.', 'E', '\314'},  {'.', 'e', '\344'},    /* dot above */
936     {';', 'I', '\307'},  {';', 'i', '\347'},    /* ogonek */
937     {';', 'U', '\331'},  {';', 'u', '\371'},
938         /* iso-8859-9 uses this */
939         /* iso-8859-9 has really bad choosen G( S, and I. as they collide
940          * whith the same letters on other iso-8859-x (that is they are on
941          * different places :-( ), if you use turkish uncomment these and
942          * comment out the lines in iso-8859-2 and iso-8859-3 sections
943          * FIXME: should be dynamic according to chosen language
944          *        if/when Wine has turkish support.
945          */
946 /*  collision whith G( from iso-8859-3 !!!   */
947 /*  {'(', 'G', '\320'},  {'(', 'g', '\360'}, */ /* breve */
948 /*  collision whith S, from iso-8859-2 !!! */
949 /*  {',', 'S', '\336'},  {',', 's', '\376'}, */ /* cedilla */
950 /*  collision whith I. from iso-8859-3 !!!   */
951 /*  {'.', 'I', '\335'},  {'.', 'i', '\375'}, */ /* dot above */
952 };
953
954
955 /***********************************************************************
956  *              TranslateMessage (USER32.@)
957  *
958  * Implementation of TranslateMessage.
959  *
960  * TranslateMessage translates virtual-key messages into character-messages,
961  * as follows :
962  * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
963  * ditto replacing WM_* with WM_SYS*
964  * This produces WM_CHAR messages only for keys mapped to ASCII characters
965  * by the keyboard driver.
966  *
967  * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
968  * return value is nonzero, regardless of the translation.
969  *
970  */
971 BOOL WINAPI TranslateMessage( const MSG *msg )
972 {
973     static int dead_char;
974     UINT message;
975     WCHAR wp[2];
976     BOOL rc = FALSE;
977
978     if (msg->message >= WM_KEYFIRST && msg->message <= WM_KEYLAST)
979     {
980         TRACE_(key)("(%s, %04X, %08lX)\n",
981                     SPY_GetMsgName(msg->message, msg->hwnd), msg->wParam, msg->lParam );
982
983         /* Return code must be TRUE no matter what! */
984         rc = TRUE;
985     }
986
987     if ((msg->message != WM_KEYDOWN) && (msg->message != WM_SYSKEYDOWN)) return rc;
988
989     TRACE_(key)("Translating key %s (%04x), scancode %02x\n",
990                  SPY_GetVKeyName(msg->wParam), msg->wParam, LOBYTE(HIWORD(msg->lParam)));
991
992     /* FIXME : should handle ToUnicode yielding 2 */
993     switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), QueueKeyStateTable, wp, 2, 0))
994     {
995     case 1:
996         message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
997         /* Should dead chars handling go in ToAscii ? */
998         if (dead_char)
999         {
1000             int i;
1001
1002             if (wp[0] == ' ') wp[0] =  dead_char;
1003             if (dead_char == 0xa2) dead_char = '(';
1004             else if (dead_char == 0xa8) dead_char = '"';
1005             else if (dead_char == 0xb2) dead_char = ';';
1006             else if (dead_char == 0xb4) dead_char = '\'';
1007             else if (dead_char == 0xb7) dead_char = '<';
1008             else if (dead_char == 0xb8) dead_char = ',';
1009             else if (dead_char == 0xff) dead_char = '.';
1010             for (i = 0; i < sizeof(accent_chars)/sizeof(accent_chars[0]); i++)
1011                 if ((accent_chars[i].ac_accent == dead_char) &&
1012                     (accent_chars[i].ac_char == wp[0]))
1013                 {
1014                     wp[0] = accent_chars[i].ac_result;
1015                     break;
1016                 }
1017             dead_char = 0;
1018         }
1019         TRACE_(key)("1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
1020         PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
1021         break;
1022
1023     case -1:
1024         message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
1025         dead_char = wp[0];
1026         TRACE_(key)("-1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
1027         PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
1028         return TRUE;
1029     }
1030     return rc;
1031 }
1032
1033
1034 /***********************************************************************
1035  *              DispatchMessageA (USER32.@)
1036  */
1037 LONG WINAPI DispatchMessageA( const MSG* msg )
1038 {
1039     WND * wndPtr;
1040     LONG retval;
1041     int painting;
1042     WNDPROC winproc;
1043
1044       /* Process timer messages */
1045     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1046     {
1047         if (msg->lParam)
1048         {
1049 /*            HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1050
1051             /* before calling window proc, verify whether timer is still valid;
1052                there's a slim chance that the application kills the timer
1053                between GetMessage and DispatchMessage API calls */
1054             if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (WNDPROC)msg->lParam))
1055                 return 0; /* invalid winproc */
1056
1057             return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
1058                                    msg->message, msg->wParam, GetTickCount() );
1059         }
1060     }
1061
1062     if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
1063     {
1064         if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1065         return 0;
1066     }
1067     if (wndPtr == WND_OTHER_PROCESS)
1068     {
1069         if (IsWindow( msg->hwnd ))
1070             ERR( "cannot dispatch msg to other process window %p\n", msg->hwnd );
1071         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1072         return 0;
1073     }
1074     if (!(winproc = wndPtr->winproc))
1075     {
1076         WIN_ReleasePtr( wndPtr );
1077         return 0;
1078     }
1079     painting = (msg->message == WM_PAINT);
1080     if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1081     WIN_ReleasePtr( wndPtr );
1082 /*    hook_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1083
1084     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1085                       msg->wParam, msg->lParam );
1086     retval = CallWindowProcA( winproc, msg->hwnd, msg->message,
1087                               msg->wParam, msg->lParam );
1088     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1089                      msg->wParam, msg->lParam );
1090
1091     if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
1092     {
1093         BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
1094         wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1095         WIN_ReleasePtr( wndPtr );
1096         if (validate)
1097         {
1098             ERR( "BeginPaint not called on WM_PAINT for hwnd %p!\n", msg->hwnd );
1099             /* Validate the update region to avoid infinite WM_PAINT loop */
1100             RedrawWindow( msg->hwnd, NULL, 0,
1101                           RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1102         }
1103     }
1104     return retval;
1105 }
1106
1107
1108 /***********************************************************************
1109  *              DispatchMessageW (USER32.@) Process Message
1110  *
1111  * Process the message specified in the structure *_msg_.
1112  *
1113  * If the lpMsg parameter points to a WM_TIMER message and the
1114  * parameter of the WM_TIMER message is not NULL, the lParam parameter
1115  * points to the function that is called instead of the window
1116  * procedure.
1117  *
1118  * The message must be valid.
1119  *
1120  * RETURNS
1121  *
1122  *   DispatchMessage() returns the result of the window procedure invoked.
1123  *
1124  * CONFORMANCE
1125  *
1126  *   ECMA-234, Win32
1127  *
1128  */
1129 LONG WINAPI DispatchMessageW( const MSG* msg )
1130 {
1131     WND * wndPtr;
1132     LONG retval;
1133     int painting;
1134     WNDPROC winproc;
1135
1136       /* Process timer messages */
1137     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1138     {
1139         if (msg->lParam)
1140         {
1141 /*            HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1142
1143             /* before calling window proc, verify whether timer is still valid;
1144                there's a slim chance that the application kills the timer
1145                between GetMessage and DispatchMessage API calls */
1146             if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (WNDPROC)msg->lParam))
1147                 return 0; /* invalid winproc */
1148
1149             return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
1150                                    msg->message, msg->wParam, GetTickCount() );
1151         }
1152     }
1153
1154     if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
1155     {
1156         if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1157         return 0;
1158     }
1159     if (wndPtr == WND_OTHER_PROCESS)
1160     {
1161         if (IsWindow( msg->hwnd ))
1162             ERR( "cannot dispatch msg to other process window %p\n", msg->hwnd );
1163         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1164         return 0;
1165     }
1166     if (!(winproc = wndPtr->winproc))
1167     {
1168         WIN_ReleasePtr( wndPtr );
1169         return 0;
1170     }
1171     painting = (msg->message == WM_PAINT);
1172     if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1173     WIN_ReleasePtr( wndPtr );
1174 /*    HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1175
1176     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1177                       msg->wParam, msg->lParam );
1178     retval = CallWindowProcW( winproc, msg->hwnd, msg->message,
1179                               msg->wParam, msg->lParam );
1180     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1181                      msg->wParam, msg->lParam );
1182
1183     if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
1184     {
1185         BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
1186         wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1187         WIN_ReleasePtr( wndPtr );
1188         if (validate)
1189         {
1190             ERR( "BeginPaint not called on WM_PAINT for hwnd %p!\n", msg->hwnd );
1191             /* Validate the update region to avoid infinite WM_PAINT loop */
1192             RedrawWindow( msg->hwnd, NULL, 0,
1193                           RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1194         }
1195     }
1196     return retval;
1197 }
1198
1199
1200 /***********************************************************************
1201  *              RegisterWindowMessage (USER.118)
1202  *              RegisterWindowMessageA (USER32.@)
1203  */
1204 WORD WINAPI RegisterWindowMessageA( LPCSTR str )
1205 {
1206     TRACE("%s\n", str );
1207     return GlobalAddAtomA( str );
1208 }
1209
1210
1211 /***********************************************************************
1212  *              RegisterWindowMessageW (USER32.@)
1213  */
1214 WORD WINAPI RegisterWindowMessageW( LPCWSTR str )
1215 {
1216     TRACE("%p\n", str );
1217     return GlobalAddAtomW( str );
1218 }
1219
1220
1221 /***********************************************************************
1222  *              BroadcastSystemMessage  (USER32.@)
1223  *              BroadcastSystemMessageA (USER32.@)
1224  */
1225 LONG WINAPI BroadcastSystemMessage(
1226         DWORD dwFlags,LPDWORD recipients,UINT uMessage,WPARAM wParam,
1227         LPARAM lParam )
1228 {
1229     if ((*recipients & BSM_APPLICATIONS)||
1230         (*recipients == BSM_ALLCOMPONENTS))
1231     {
1232         FIXME("(%08lx,%08lx,%08x,%08x,%08lx): semi-stub!\n",
1233               dwFlags,*recipients,uMessage,wParam,lParam);
1234         PostMessageA(HWND_BROADCAST,uMessage,wParam,lParam);
1235         return 1;
1236     }
1237     else
1238     {
1239         FIXME("(%08lx,%08lx,%08x,%08x,%08lx): stub!\n",
1240               dwFlags,*recipients,uMessage,wParam,lParam);
1241         return -1;
1242     }
1243 }