Add casts to compile with -DSTRICT.
[wine/multimedia.git] / windows / message.c
blob47ca0b29c2ceba9e5ef02d420934eb6ad16f7eb2
1 /*
2 * Message queues related functions
4 * Copyright 1993, 1994 Alexandre Julliard
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.
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.
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
21 #include "config.h"
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>
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 "hook.h"
40 #include "input.h"
41 #include "spy.h"
42 #include "winpos.h"
43 #include "dde.h"
44 #include "queue.h"
45 #include "winproc.h"
46 #include "user.h"
47 #include "thread.h"
48 #include "controls.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(msg);
52 WINE_DECLARE_DEBUG_CHANNEL(key);
54 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
55 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
57 static BYTE QueueKeyStateTable[256];
60 /***********************************************************************
61 * is_keyboard_message
63 inline static BOOL is_keyboard_message( UINT message )
65 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
69 /***********************************************************************
70 * is_mouse_message
72 inline static BOOL is_mouse_message( UINT message )
74 return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
75 (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
79 /***********************************************************************
80 * check_message_filter
82 inline static BOOL check_message_filter( const MSG *msg, HWND hwnd, UINT first, UINT last )
84 if (hwnd)
86 if (msg->hwnd != hwnd && !IsChild( hwnd, msg->hwnd )) return FALSE;
88 if (first || last)
90 return (msg->message >= first && msg->message <= last);
92 return TRUE;
96 /***********************************************************************
97 * process_sent_messages
99 * Process all pending sent messages.
101 inline static void process_sent_messages(void)
103 MSG msg;
104 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
108 /***********************************************************************
109 * queue_hardware_message
111 * store a hardware message in the thread queue
113 static void queue_hardware_message( MSG *msg, ULONG_PTR extra_info, enum message_type type )
115 SERVER_START_REQ( send_message )
117 req->type = type;
118 req->id = GetWindowThreadProcessId( msg->hwnd, NULL );
119 req->win = msg->hwnd;
120 req->msg = msg->message;
121 req->wparam = msg->wParam;
122 req->lparam = msg->lParam;
123 req->x = msg->pt.x;
124 req->y = msg->pt.y;
125 req->time = msg->time;
126 req->info = extra_info;
127 req->timeout = 0;
128 wine_server_call( req );
130 SERVER_END_REQ;
134 /***********************************************************************
135 * update_queue_key_state
137 static void update_queue_key_state( UINT msg, WPARAM wp )
139 BOOL down = FALSE;
141 switch (msg)
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 break;
170 if (down)
172 BYTE *p = &QueueKeyStateTable[wp];
173 if (!(*p & 0x80)) *p ^= 0x01;
174 *p |= 0x80;
176 else QueueKeyStateTable[wp] &= ~0x80;
180 /***********************************************************************
181 * MSG_SendParentNotify
183 * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
184 * the window has the WS_EX_NOPARENTNOTIFY style.
186 static void MSG_SendParentNotify( HWND hwnd, WORD event, WORD idChild, POINT pt )
188 /* pt has to be in the client coordinates of the parent window */
189 MapWindowPoints( 0, hwnd, &pt, 1 );
190 for (;;)
192 HWND parent;
194 if (!(GetWindowLongA( hwnd, GWL_STYLE ) & WS_CHILD)) break;
195 if (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
196 if (!(parent = GetParent(hwnd))) break;
197 MapWindowPoints( hwnd, parent, &pt, 1 );
198 hwnd = parent;
199 SendMessageA( hwnd, WM_PARENTNOTIFY,
200 MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
205 /***********************************************************************
206 * MSG_JournalPlayBackMsg
208 * Get an EVENTMSG struct via call JOURNALPLAYBACK hook function
210 void MSG_JournalPlayBackMsg(void)
212 EVENTMSG tmpMsg;
213 MSG msg;
214 LRESULT wtime;
215 int keyDown,i;
217 if (!HOOK_IsHooked( WH_JOURNALPLAYBACK )) return;
219 wtime=HOOK_CallHooksA( WH_JOURNALPLAYBACK, HC_GETNEXT, 0, (LPARAM)&tmpMsg );
220 /* TRACE(msg,"Playback wait time =%ld\n",wtime); */
221 if (wtime<=0)
223 wtime=0;
224 msg.message = tmpMsg.message;
225 msg.hwnd = tmpMsg.hwnd;
226 msg.time = tmpMsg.time;
227 if ((tmpMsg.message >= WM_KEYFIRST) && (tmpMsg.message <= WM_KEYLAST))
229 msg.wParam = tmpMsg.paramL & 0xFF;
230 msg.lParam = MAKELONG(tmpMsg.paramH&0x7ffff,tmpMsg.paramL>>8);
231 if (tmpMsg.message == WM_KEYDOWN || tmpMsg.message == WM_SYSKEYDOWN)
233 for (keyDown=i=0; i<256 && !keyDown; i++)
234 if (InputKeyStateTable[i] & 0x80)
235 keyDown++;
236 if (!keyDown)
237 msg.lParam |= 0x40000000;
238 InputKeyStateTable[msg.wParam] |= 0x80;
239 AsyncKeyStateTable[msg.wParam] |= 0x80;
241 else /* WM_KEYUP, WM_SYSKEYUP */
243 msg.lParam |= 0xC0000000;
244 InputKeyStateTable[msg.wParam] &= ~0x80;
246 if (InputKeyStateTable[VK_MENU] & 0x80)
247 msg.lParam |= 0x20000000;
248 if (tmpMsg.paramH & 0x8000) /*special_key bit*/
249 msg.lParam |= 0x01000000;
251 msg.pt.x = msg.pt.y = 0;
252 queue_hardware_message( &msg, 0, MSG_HARDWARE_RAW );
254 else if ((tmpMsg.message>= WM_MOUSEFIRST) && (tmpMsg.message <= WM_MOUSELAST))
256 switch (tmpMsg.message)
258 case WM_LBUTTONDOWN:
259 InputKeyStateTable[VK_LBUTTON] |= 0x80;
260 AsyncKeyStateTable[VK_LBUTTON] |= 0x80;
261 break;
262 case WM_LBUTTONUP:
263 InputKeyStateTable[VK_LBUTTON] &= ~0x80;
264 break;
265 case WM_MBUTTONDOWN:
266 InputKeyStateTable[VK_MBUTTON] |= 0x80;
267 AsyncKeyStateTable[VK_MBUTTON] |= 0x80;
268 break;
269 case WM_MBUTTONUP:
270 InputKeyStateTable[VK_MBUTTON] &= ~0x80;
271 break;
272 case WM_RBUTTONDOWN:
273 InputKeyStateTable[VK_RBUTTON] |= 0x80;
274 AsyncKeyStateTable[VK_RBUTTON] |= 0x80;
275 break;
276 case WM_RBUTTONUP:
277 InputKeyStateTable[VK_RBUTTON] &= ~0x80;
278 break;
280 SetCursorPos(tmpMsg.paramL,tmpMsg.paramH);
281 msg.lParam=MAKELONG(tmpMsg.paramL,tmpMsg.paramH);
282 msg.wParam=0;
283 if (InputKeyStateTable[VK_LBUTTON] & 0x80) msg.wParam |= MK_LBUTTON;
284 if (InputKeyStateTable[VK_MBUTTON] & 0x80) msg.wParam |= MK_MBUTTON;
285 if (InputKeyStateTable[VK_RBUTTON] & 0x80) msg.wParam |= MK_RBUTTON;
287 msg.pt.x = tmpMsg.paramL;
288 msg.pt.y = tmpMsg.paramH;
289 queue_hardware_message( &msg, 0, MSG_HARDWARE_RAW );
291 HOOK_CallHooksA( WH_JOURNALPLAYBACK, HC_SKIP, 0, (LPARAM)&tmpMsg);
293 else
295 if( tmpMsg.message == WM_QUEUESYNC )
296 if (HOOK_IsHooked( WH_CBT ))
297 HOOK_CallHooksA( WH_CBT, HCBT_QS, 0, 0L);
302 /***********************************************************************
303 * process_raw_keyboard_message
305 * returns TRUE if the contents of 'msg' should be passed to the application
307 static BOOL process_raw_keyboard_message( MSG *msg, ULONG_PTR extra_info )
309 if (!(msg->hwnd = GetFocus()))
311 /* Send the message to the active window instead, */
312 /* translating messages to their WM_SYS equivalent */
313 msg->hwnd = GetActiveWindow();
314 if (msg->message < WM_SYSKEYDOWN) msg->message += WM_SYSKEYDOWN - WM_KEYDOWN;
317 if (HOOK_IsHooked( WH_JOURNALRECORD ))
319 EVENTMSG event;
321 event.message = msg->message;
322 event.hwnd = msg->hwnd;
323 event.time = msg->time;
324 event.paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
325 event.paramH = msg->lParam & 0x7FFF;
326 if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
327 HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event );
330 /* if we are going to throw away the message, update the queue state now */
331 if (!msg->hwnd) update_queue_key_state( msg->message, msg->wParam );
333 return (msg->hwnd != 0);
337 /***********************************************************************
338 * process_cooked_keyboard_message
340 * returns TRUE if the contents of 'msg' should be passed to the application
342 static BOOL process_cooked_keyboard_message( MSG *msg, BOOL remove )
344 if (remove)
346 update_queue_key_state( msg->message, msg->wParam );
348 /* Handle F1 key by sending out WM_HELP message */
349 if ((msg->message == WM_KEYUP) &&
350 (msg->wParam == VK_F1) &&
351 (msg->hwnd != GetDesktopWindow()) &&
352 !MENU_IsMenuActive())
354 HELPINFO hi;
355 hi.cbSize = sizeof(HELPINFO);
356 hi.iContextType = HELPINFO_WINDOW;
357 hi.iCtrlId = GetWindowLongA( msg->hwnd, GWL_ID );
358 hi.hItemHandle = msg->hwnd;
359 hi.dwContextId = GetWindowContextHelpId( msg->hwnd );
360 hi.MousePos = msg->pt;
361 SendMessageA(msg->hwnd, WM_HELP, 0, (LPARAM)&hi);
365 if (HOOK_CallHooksA( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
366 LOWORD(msg->wParam), msg->lParam ))
368 /* skip this message */
369 HOOK_CallHooksA( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam );
370 return FALSE;
372 return TRUE;
376 /***********************************************************************
377 * process_raw_mouse_message
379 * returns TRUE if the contents of 'msg' should be passed to the application
381 static BOOL process_raw_mouse_message( MSG *msg, ULONG_PTR extra_info )
383 static MSG clk_msg;
385 POINT pt;
386 INT hittest;
387 GUITHREADINFO info;
389 /* find the window to dispatch this mouse message to */
391 hittest = HTCLIENT;
392 GetGUIThreadInfo( GetCurrentThreadId(), &info );
393 if (!(msg->hwnd = info.hwndCapture))
395 /* If no capture HWND, find window which contains the mouse position.
396 * Also find the position of the cursor hot spot (hittest) */
397 HWND hWndScope = (HWND)extra_info;
399 if (!IsWindow(hWndScope)) hWndScope = 0;
400 if (!(msg->hwnd = WINPOS_WindowFromPoint( hWndScope, msg->pt, &hittest )))
401 msg->hwnd = GetDesktopWindow();
404 if (HOOK_IsHooked( WH_JOURNALRECORD ))
406 EVENTMSG event;
407 event.message = msg->message;
408 event.time = msg->time;
409 event.hwnd = msg->hwnd;
410 event.paramL = msg->pt.x;
411 event.paramH = msg->pt.y;
412 HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event );
415 /* translate double clicks */
417 if ((msg->message == WM_LBUTTONDOWN) ||
418 (msg->message == WM_RBUTTONDOWN) ||
419 (msg->message == WM_MBUTTONDOWN))
421 BOOL update = TRUE;
422 /* translate double clicks -
423 * note that ...MOUSEMOVEs can slip in between
424 * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
426 if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
427 hittest != HTCLIENT ||
428 (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
430 if ((msg->message == clk_msg.message) &&
431 (msg->hwnd == clk_msg.hwnd) &&
432 (msg->time - clk_msg.time < GetDoubleClickTime()) &&
433 (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
434 (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
436 msg->message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
437 clk_msg.message = 0;
438 update = FALSE;
441 /* update static double click conditions */
442 if (update) clk_msg = *msg;
445 pt = msg->pt;
446 /* Note: windows has no concept of a non-client wheel message */
447 if (hittest != HTCLIENT && msg->message != WM_MOUSEWHEEL)
449 msg->message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
450 msg->wParam = hittest;
452 else
454 /* coordinates don't get translated while tracking a menu */
455 /* FIXME: should differentiate popups and top-level menus */
456 if (!(info.flags & GUI_INMENUMODE)) ScreenToClient( msg->hwnd, &pt );
458 msg->lParam = MAKELONG( pt.x, pt.y );
459 return TRUE;
463 /***********************************************************************
464 * process_cooked_mouse_message
466 * returns TRUE if the contents of 'msg' should be passed to the application
468 static BOOL process_cooked_mouse_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
470 INT hittest = HTCLIENT;
471 UINT raw_message = msg->message;
472 BOOL eatMsg;
474 if (msg->message >= WM_NCMOUSEFIRST && msg->message <= WM_NCMOUSELAST)
476 raw_message += WM_MOUSEFIRST - WM_NCMOUSEFIRST;
477 hittest = msg->wParam;
479 if (raw_message == WM_LBUTTONDBLCLK ||
480 raw_message == WM_RBUTTONDBLCLK ||
481 raw_message == WM_MBUTTONDBLCLK)
483 raw_message += WM_LBUTTONDOWN - WM_LBUTTONDBLCLK;
486 if (remove) update_queue_key_state( raw_message, 0 );
488 if (HOOK_IsHooked( WH_MOUSE ))
490 MOUSEHOOKSTRUCT hook;
491 hook.pt = msg->pt;
492 hook.hwnd = msg->hwnd;
493 hook.wHitTestCode = hittest;
494 hook.dwExtraInfo = extra_info;
495 if (HOOK_CallHooksA( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
496 msg->message, (LPARAM)&hook ))
498 hook.pt = msg->pt;
499 hook.hwnd = msg->hwnd;
500 hook.wHitTestCode = hittest;
501 hook.dwExtraInfo = extra_info;
502 HOOK_CallHooksA( WH_CBT, HCBT_CLICKSKIPPED, msg->message, (LPARAM)&hook );
503 return FALSE;
507 if ((hittest == HTERROR) || (hittest == HTNOWHERE))
509 SendMessageA( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
510 MAKELONG( hittest, raw_message ));
511 return FALSE;
514 if (!remove || GetCapture()) return TRUE;
516 eatMsg = FALSE;
518 if ((raw_message == WM_LBUTTONDOWN) ||
519 (raw_message == WM_RBUTTONDOWN) ||
520 (raw_message == WM_MBUTTONDOWN))
522 /* Send the WM_PARENTNOTIFY,
523 * note that even for double/nonclient clicks
524 * notification message is still WM_L/M/RBUTTONDOWN.
526 MSG_SendParentNotify( msg->hwnd, raw_message, 0, msg->pt );
528 /* Activate the window if needed */
530 if (msg->hwnd != GetActiveWindow())
532 HWND hwndTop = msg->hwnd;
533 while (hwndTop)
535 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
536 hwndTop = GetParent( hwndTop );
539 if (hwndTop && hwndTop != GetDesktopWindow())
541 LONG ret = SendMessageA( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
542 MAKELONG( hittest, raw_message ) );
543 switch(ret)
545 case MA_NOACTIVATEANDEAT:
546 eatMsg = TRUE;
547 /* fall through */
548 case MA_NOACTIVATE:
549 break;
550 case MA_ACTIVATEANDEAT:
551 eatMsg = TRUE;
552 /* fall through */
553 case MA_ACTIVATE:
554 case 0:
555 if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
556 break;
557 default:
558 WARN( "unknown WM_MOUSEACTIVATE code %ld\n", ret );
559 break;
565 /* send the WM_SETCURSOR message */
567 /* Windows sends the normal mouse message as the message parameter
568 in the WM_SETCURSOR message even if it's non-client mouse message */
569 SendMessageA( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
570 MAKELONG( hittest, raw_message ));
572 return !eatMsg;
576 /***********************************************************************
577 * process_hardware_message
579 * returns TRUE if the contents of 'msg' should be passed to the application
581 BOOL MSG_process_raw_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd_filter,
582 UINT first, UINT last, BOOL remove )
584 if (is_keyboard_message( msg->message ))
586 if (!process_raw_keyboard_message( msg, extra_info )) return FALSE;
588 else if (is_mouse_message( msg->message ))
590 if (!process_raw_mouse_message( msg, extra_info )) return FALSE;
592 else
594 ERR( "unknown message type %x\n", msg->message );
595 return FALSE;
598 /* check destination thread and filters */
599 if (!check_message_filter( msg, hwnd_filter, first, last ) ||
600 !WIN_IsCurrentThread( msg->hwnd ))
602 /* queue it for later, or for another thread */
603 queue_hardware_message( msg, extra_info, MSG_HARDWARE_COOKED );
604 return FALSE;
607 /* save the message in the cooked queue if we didn't want to remove it */
608 if (!remove) queue_hardware_message( msg, extra_info, MSG_HARDWARE_COOKED );
609 return TRUE;
613 /***********************************************************************
614 * MSG_process_cooked_hardware_message
616 * returns TRUE if the contents of 'msg' should be passed to the application
618 BOOL MSG_process_cooked_hardware_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
620 if (is_keyboard_message( msg->message ))
621 return process_cooked_keyboard_message( msg, remove );
623 if (is_mouse_message( msg->message ))
624 return process_cooked_mouse_message( msg, extra_info, remove );
626 ERR( "unknown message type %x\n", msg->message );
627 return FALSE;
631 /**********************************************************************
632 * GetKeyState (USER.106)
634 INT16 WINAPI GetKeyState16(INT16 vkey)
636 return GetKeyState(vkey);
640 /**********************************************************************
641 * GetKeyState (USER32.@)
643 * An application calls the GetKeyState function in response to a
644 * keyboard-input message. This function retrieves the state of the key
645 * at the time the input message was generated. (SDK 3.1 Vol 2. p 390)
647 SHORT WINAPI GetKeyState(INT vkey)
649 INT retval;
651 if (vkey >= 'a' && vkey <= 'z') vkey += 'A' - 'a';
652 retval = ((WORD)(QueueKeyStateTable[vkey] & 0x80) << 8 ) |
653 (QueueKeyStateTable[vkey] & 0x80) |
654 (QueueKeyStateTable[vkey] & 0x01);
655 TRACE("key (0x%x) -> %x\n", vkey, retval);
656 return retval;
660 /**********************************************************************
661 * GetKeyboardState (USER.222)
662 * GetKeyboardState (USER32.@)
664 * An application calls the GetKeyboardState function in response to a
665 * keyboard-input message. This function retrieves the state of the keyboard
666 * at the time the input message was generated. (SDK 3.1 Vol 2. p 387)
668 BOOL WINAPI GetKeyboardState(LPBYTE lpKeyState)
670 TRACE_(key)("(%p)\n", lpKeyState);
671 if (lpKeyState) memcpy(lpKeyState, QueueKeyStateTable, 256);
672 return TRUE;
676 /**********************************************************************
677 * SetKeyboardState (USER.223)
678 * SetKeyboardState (USER32.@)
680 BOOL WINAPI SetKeyboardState(LPBYTE lpKeyState)
682 TRACE_(key)("(%p)\n", lpKeyState);
683 if (lpKeyState) memcpy(QueueKeyStateTable, lpKeyState, 256);
684 return TRUE;
688 /***********************************************************************
689 * WaitMessage (USER.112) Suspend thread pending messages
690 * WaitMessage (USER32.@) Suspend thread pending messages
692 * WaitMessage() suspends a thread until events appear in the thread's
693 * queue.
695 BOOL WINAPI WaitMessage(void)
697 return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
701 /***********************************************************************
702 * MsgWaitForMultipleObjectsEx (USER32.@)
704 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
705 DWORD timeout, DWORD mask, DWORD flags )
707 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
708 DWORD i, ret, lock;
709 MESSAGEQUEUE *msgQueue;
711 if (count > MAXIMUM_WAIT_OBJECTS-1)
713 SetLastError( ERROR_INVALID_PARAMETER );
714 return WAIT_FAILED;
717 if (!(msgQueue = QUEUE_Current())) return WAIT_FAILED;
719 /* set the queue mask */
720 SERVER_START_REQ( set_queue_mask )
722 req->wake_mask = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
723 req->changed_mask = mask;
724 req->skip_wait = 0;
725 wine_server_call( req );
727 SERVER_END_REQ;
729 /* Add the thread event to the handle list */
730 for (i = 0; i < count; i++) handles[i] = pHandles[i];
731 handles[count] = msgQueue->server_queue;
733 ReleaseThunkLock( &lock );
734 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
736 ret = USER_Driver.pMsgWaitForMultipleObjectsEx( count+1, handles, timeout, mask, flags );
737 if (ret == count+1) ret = count; /* pretend the msg queue is ready */
739 else
740 ret = WaitForMultipleObjectsEx( count+1, handles, flags & MWMO_WAITALL,
741 timeout, flags & MWMO_ALERTABLE );
742 if (lock) RestoreThunkLock( lock );
743 return ret;
747 /***********************************************************************
748 * MsgWaitForMultipleObjects (USER32.@)
750 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
751 BOOL wait_all, DWORD timeout, DWORD mask )
753 return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
754 wait_all ? MWMO_WAITALL : 0 );
758 /***********************************************************************
759 * WaitForInputIdle (USER32.@)
761 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
763 DWORD start_time, elapsed, ret;
764 HANDLE idle_event = -1;
766 SERVER_START_REQ( wait_input_idle )
768 req->handle = hProcess;
769 req->timeout = dwTimeOut;
770 if (!(ret = wine_server_call_err( req ))) idle_event = reply->event;
772 SERVER_END_REQ;
773 if (ret) return WAIT_FAILED; /* error */
774 if (!idle_event) return 0; /* no event to wait on */
776 start_time = GetTickCount();
777 elapsed = 0;
779 TRACE("waiting for %x\n", idle_event );
782 ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
783 switch (ret)
785 case WAIT_OBJECT_0+1:
786 process_sent_messages();
787 break;
788 case WAIT_TIMEOUT:
789 case WAIT_FAILED:
790 TRACE("timeout or error\n");
791 return ret;
792 default:
793 TRACE("finished\n");
794 return 0;
796 if (dwTimeOut != INFINITE)
798 elapsed = GetTickCount() - start_time;
799 if (elapsed > dwTimeOut)
800 break;
803 while (1);
805 return WAIT_TIMEOUT;
809 /***********************************************************************
810 * UserYield (USER.332)
812 void WINAPI UserYield16(void)
814 DWORD count;
816 /* Handle sent messages */
817 process_sent_messages();
819 /* Yield */
820 ReleaseThunkLock(&count);
821 if (count)
823 RestoreThunkLock(count);
824 /* Handle sent messages again */
825 process_sent_messages();
830 struct accent_char
832 BYTE ac_accent;
833 BYTE ac_char;
834 BYTE ac_result;
837 static const struct accent_char accent_chars[] =
839 /* A good idea should be to read /usr/X11/lib/X11/locale/iso8859-x/Compose */
840 {'`', 'A', '\300'}, {'`', 'a', '\340'},
841 {'\'', 'A', '\301'}, {'\'', 'a', '\341'},
842 {'^', 'A', '\302'}, {'^', 'a', '\342'},
843 {'~', 'A', '\303'}, {'~', 'a', '\343'},
844 {'"', 'A', '\304'}, {'"', 'a', '\344'},
845 {'O', 'A', '\305'}, {'o', 'a', '\345'},
846 {'0', 'A', '\305'}, {'0', 'a', '\345'},
847 {'A', 'A', '\305'}, {'a', 'a', '\345'},
848 {'A', 'E', '\306'}, {'a', 'e', '\346'},
849 {',', 'C', '\307'}, {',', 'c', '\347'},
850 {'`', 'E', '\310'}, {'`', 'e', '\350'},
851 {'\'', 'E', '\311'}, {'\'', 'e', '\351'},
852 {'^', 'E', '\312'}, {'^', 'e', '\352'},
853 {'"', 'E', '\313'}, {'"', 'e', '\353'},
854 {'`', 'I', '\314'}, {'`', 'i', '\354'},
855 {'\'', 'I', '\315'}, {'\'', 'i', '\355'},
856 {'^', 'I', '\316'}, {'^', 'i', '\356'},
857 {'"', 'I', '\317'}, {'"', 'i', '\357'},
858 {'-', 'D', '\320'}, {'-', 'd', '\360'},
859 {'~', 'N', '\321'}, {'~', 'n', '\361'},
860 {'`', 'O', '\322'}, {'`', 'o', '\362'},
861 {'\'', 'O', '\323'}, {'\'', 'o', '\363'},
862 {'^', 'O', '\324'}, {'^', 'o', '\364'},
863 {'~', 'O', '\325'}, {'~', 'o', '\365'},
864 {'"', 'O', '\326'}, {'"', 'o', '\366'},
865 {'/', 'O', '\330'}, {'/', 'o', '\370'},
866 {'`', 'U', '\331'}, {'`', 'u', '\371'},
867 {'\'', 'U', '\332'}, {'\'', 'u', '\372'},
868 {'^', 'U', '\333'}, {'^', 'u', '\373'},
869 {'"', 'U', '\334'}, {'"', 'u', '\374'},
870 {'\'', 'Y', '\335'}, {'\'', 'y', '\375'},
871 {'T', 'H', '\336'}, {'t', 'h', '\376'},
872 {'s', 's', '\337'}, {'"', 'y', '\377'},
873 {'s', 'z', '\337'}, {'i', 'j', '\377'},
874 /* iso-8859-2 uses this */
875 {'<', 'L', '\245'}, {'<', 'l', '\265'}, /* caron */
876 {'<', 'S', '\251'}, {'<', 's', '\271'},
877 {'<', 'T', '\253'}, {'<', 't', '\273'},
878 {'<', 'Z', '\256'}, {'<', 'z', '\276'},
879 {'<', 'C', '\310'}, {'<', 'c', '\350'},
880 {'<', 'E', '\314'}, {'<', 'e', '\354'},
881 {'<', 'D', '\317'}, {'<', 'd', '\357'},
882 {'<', 'N', '\322'}, {'<', 'n', '\362'},
883 {'<', 'R', '\330'}, {'<', 'r', '\370'},
884 {';', 'A', '\241'}, {';', 'a', '\261'}, /* ogonek */
885 {';', 'E', '\312'}, {';', 'e', '\332'},
886 {'\'', 'Z', '\254'}, {'\'', 'z', '\274'}, /* acute */
887 {'\'', 'R', '\300'}, {'\'', 'r', '\340'},
888 {'\'', 'L', '\305'}, {'\'', 'l', '\345'},
889 {'\'', 'C', '\306'}, {'\'', 'c', '\346'},
890 {'\'', 'N', '\321'}, {'\'', 'n', '\361'},
891 /* collision whith S, from iso-8859-9 !!! */
892 {',', 'S', '\252'}, {',', 's', '\272'}, /* cedilla */
893 {',', 'T', '\336'}, {',', 't', '\376'},
894 {'.', 'Z', '\257'}, {'.', 'z', '\277'}, /* dot above */
895 {'/', 'L', '\243'}, {'/', 'l', '\263'}, /* slash */
896 {'/', 'D', '\320'}, {'/', 'd', '\360'},
897 {'(', 'A', '\303'}, {'(', 'a', '\343'}, /* breve */
898 {'\275', 'O', '\325'}, {'\275', 'o', '\365'}, /* double acute */
899 {'\275', 'U', '\334'}, {'\275', 'u', '\374'},
900 {'0', 'U', '\332'}, {'0', 'u', '\372'}, /* ring above */
901 /* iso-8859-3 uses this */
902 {'/', 'H', '\241'}, {'/', 'h', '\261'}, /* slash */
903 {'>', 'H', '\246'}, {'>', 'h', '\266'}, /* circumflex */
904 {'>', 'J', '\254'}, {'>', 'j', '\274'},
905 {'>', 'C', '\306'}, {'>', 'c', '\346'},
906 {'>', 'G', '\330'}, {'>', 'g', '\370'},
907 {'>', 'S', '\336'}, {'>', 's', '\376'},
908 /* collision whith G( from iso-8859-9 !!! */
909 {'(', 'G', '\253'}, {'(', 'g', '\273'}, /* breve */
910 {'(', 'U', '\335'}, {'(', 'u', '\375'},
911 /* collision whith I. from iso-8859-3 !!! */
912 {'.', 'I', '\251'}, {'.', 'i', '\271'}, /* dot above */
913 {'.', 'C', '\305'}, {'.', 'c', '\345'},
914 {'.', 'G', '\325'}, {'.', 'g', '\365'},
915 /* iso-8859-4 uses this */
916 {',', 'R', '\243'}, {',', 'r', '\263'}, /* cedilla */
917 {',', 'L', '\246'}, {',', 'l', '\266'},
918 {',', 'G', '\253'}, {',', 'g', '\273'},
919 {',', 'N', '\321'}, {',', 'n', '\361'},
920 {',', 'K', '\323'}, {',', 'k', '\363'},
921 {'~', 'I', '\245'}, {'~', 'i', '\265'}, /* tilde */
922 {'-', 'E', '\252'}, {'-', 'e', '\272'}, /* macron */
923 {'-', 'A', '\300'}, {'-', 'a', '\340'},
924 {'-', 'I', '\317'}, {'-', 'i', '\357'},
925 {'-', 'O', '\322'}, {'-', 'o', '\362'},
926 {'-', 'U', '\336'}, {'-', 'u', '\376'},
927 {'/', 'T', '\254'}, {'/', 't', '\274'}, /* slash */
928 {'.', 'E', '\314'}, {'.', 'e', '\344'}, /* dot above */
929 {';', 'I', '\307'}, {';', 'i', '\347'}, /* ogonek */
930 {';', 'U', '\331'}, {';', 'u', '\371'},
931 /* iso-8859-9 uses this */
932 /* iso-8859-9 has really bad choosen G( S, and I. as they collide
933 * whith the same letters on other iso-8859-x (that is they are on
934 * different places :-( ), if you use turkish uncomment these and
935 * comment out the lines in iso-8859-2 and iso-8859-3 sections
936 * FIXME: should be dynamic according to chosen language
937 * if/when Wine has turkish support.
939 /* collision whith G( from iso-8859-3 !!! */
940 /* {'(', 'G', '\320'}, {'(', 'g', '\360'}, */ /* breve */
941 /* collision whith S, from iso-8859-2 !!! */
942 /* {',', 'S', '\336'}, {',', 's', '\376'}, */ /* cedilla */
943 /* collision whith I. from iso-8859-3 !!! */
944 /* {'.', 'I', '\335'}, {'.', 'i', '\375'}, */ /* dot above */
948 /***********************************************************************
949 * TranslateMessage (USER32.@)
951 * Implementation of TranslateMessage.
953 * TranslateMessage translates virtual-key messages into character-messages,
954 * as follows :
955 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
956 * ditto replacing WM_* with WM_SYS*
957 * This produces WM_CHAR messages only for keys mapped to ASCII characters
958 * by the keyboard driver.
960 * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
961 * return value is nonzero, regardless of the translation.
964 BOOL WINAPI TranslateMessage( const MSG *msg )
966 static int dead_char;
967 UINT message;
968 WCHAR wp[2];
969 BOOL rc = FALSE;
971 if (msg->message >= WM_KEYFIRST && msg->message <= WM_KEYLAST)
973 TRACE_(key)("(%s, %04X, %08lX)\n",
974 SPY_GetMsgName(msg->message, msg->hwnd), msg->wParam, msg->lParam );
976 /* Return code must be TRUE no matter what! */
977 rc = TRUE;
980 if ((msg->message != WM_KEYDOWN) && (msg->message != WM_SYSKEYDOWN)) return rc;
982 TRACE_(key)("Translating key %s (%04x), scancode %02x\n",
983 SPY_GetVKeyName(msg->wParam), msg->wParam, LOBYTE(HIWORD(msg->lParam)));
985 /* FIXME : should handle ToUnicode yielding 2 */
986 switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), QueueKeyStateTable, wp, 2, 0))
988 case 1:
989 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
990 /* Should dead chars handling go in ToAscii ? */
991 if (dead_char)
993 int i;
995 if (wp[0] == ' ') wp[0] = dead_char;
996 if (dead_char == 0xa2) dead_char = '(';
997 else if (dead_char == 0xa8) dead_char = '"';
998 else if (dead_char == 0xb2) dead_char = ';';
999 else if (dead_char == 0xb4) dead_char = '\'';
1000 else if (dead_char == 0xb7) dead_char = '<';
1001 else if (dead_char == 0xb8) dead_char = ',';
1002 else if (dead_char == 0xff) dead_char = '.';
1003 for (i = 0; i < sizeof(accent_chars)/sizeof(accent_chars[0]); i++)
1004 if ((accent_chars[i].ac_accent == dead_char) &&
1005 (accent_chars[i].ac_char == wp[0]))
1007 wp[0] = accent_chars[i].ac_result;
1008 break;
1010 dead_char = 0;
1012 TRACE_(key)("1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
1013 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
1014 break;
1016 case -1:
1017 message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
1018 dead_char = wp[0];
1019 TRACE_(key)("-1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
1020 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
1021 return TRUE;
1023 return rc;
1027 /***********************************************************************
1028 * DispatchMessageA (USER32.@)
1030 LONG WINAPI DispatchMessageA( const MSG* msg )
1032 WND * wndPtr;
1033 LONG retval;
1034 int painting;
1035 WNDPROC winproc;
1037 /* Process timer messages */
1038 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1040 if (msg->lParam)
1042 /* HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1044 /* before calling window proc, verify whether timer is still valid;
1045 there's a slim chance that the application kills the timer
1046 between GetMessage and DispatchMessage API calls */
1047 if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (HWINDOWPROC) msg->lParam))
1048 return 0; /* invalid winproc */
1050 return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
1051 msg->message, msg->wParam, GetTickCount() );
1055 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
1057 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1058 return 0;
1060 if (wndPtr == WND_OTHER_PROCESS)
1062 if (IsWindow( msg->hwnd ))
1063 ERR( "cannot dispatch msg to other process window %x\n", msg->hwnd );
1064 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1065 return 0;
1067 if (!(winproc = wndPtr->winproc))
1069 WIN_ReleasePtr( wndPtr );
1070 return 0;
1072 painting = (msg->message == WM_PAINT);
1073 if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1074 WIN_ReleasePtr( wndPtr );
1075 /* hook_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1077 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1078 msg->wParam, msg->lParam );
1079 retval = CallWindowProcA( winproc, msg->hwnd, msg->message,
1080 msg->wParam, msg->lParam );
1081 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1082 msg->wParam, msg->lParam );
1084 if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
1086 BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
1087 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1088 WIN_ReleasePtr( wndPtr );
1089 if (validate)
1091 ERR( "BeginPaint not called on WM_PAINT for hwnd %04x!\n", msg->hwnd );
1092 /* Validate the update region to avoid infinite WM_PAINT loop */
1093 RedrawWindow( msg->hwnd, NULL, 0,
1094 RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1097 return retval;
1101 /***********************************************************************
1102 * DispatchMessageW (USER32.@) Process Message
1104 * Process the message specified in the structure *_msg_.
1106 * If the lpMsg parameter points to a WM_TIMER message and the
1107 * parameter of the WM_TIMER message is not NULL, the lParam parameter
1108 * points to the function that is called instead of the window
1109 * procedure.
1111 * The message must be valid.
1113 * RETURNS
1115 * DispatchMessage() returns the result of the window procedure invoked.
1117 * CONFORMANCE
1119 * ECMA-234, Win32
1122 LONG WINAPI DispatchMessageW( const MSG* msg )
1124 WND * wndPtr;
1125 LONG retval;
1126 int painting;
1127 WNDPROC winproc;
1129 /* Process timer messages */
1130 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1132 if (msg->lParam)
1134 /* HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1136 /* before calling window proc, verify whether timer is still valid;
1137 there's a slim chance that the application kills the timer
1138 between GetMessage and DispatchMessage API calls */
1139 if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (HWINDOWPROC) msg->lParam))
1140 return 0; /* invalid winproc */
1142 return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
1143 msg->message, msg->wParam, GetTickCount() );
1147 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
1149 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1150 return 0;
1152 if (wndPtr == WND_OTHER_PROCESS)
1154 if (IsWindow( msg->hwnd ))
1155 ERR( "cannot dispatch msg to other process window %x\n", msg->hwnd );
1156 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1157 return 0;
1159 if (!(winproc = wndPtr->winproc))
1161 WIN_ReleasePtr( wndPtr );
1162 return 0;
1164 painting = (msg->message == WM_PAINT);
1165 if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1166 WIN_ReleasePtr( wndPtr );
1167 /* HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1169 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1170 msg->wParam, msg->lParam );
1171 retval = CallWindowProcW( winproc, msg->hwnd, msg->message,
1172 msg->wParam, msg->lParam );
1173 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1174 msg->wParam, msg->lParam );
1176 if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
1178 BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
1179 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1180 WIN_ReleasePtr( wndPtr );
1181 if (validate)
1183 ERR( "BeginPaint not called on WM_PAINT for hwnd %04x!\n", msg->hwnd );
1184 /* Validate the update region to avoid infinite WM_PAINT loop */
1185 RedrawWindow( msg->hwnd, NULL, 0,
1186 RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1189 return retval;
1193 /***********************************************************************
1194 * RegisterWindowMessage (USER.118)
1195 * RegisterWindowMessageA (USER32.@)
1197 WORD WINAPI RegisterWindowMessageA( LPCSTR str )
1199 TRACE("%s\n", str );
1200 return GlobalAddAtomA( str );
1204 /***********************************************************************
1205 * RegisterWindowMessageW (USER32.@)
1207 WORD WINAPI RegisterWindowMessageW( LPCWSTR str )
1209 TRACE("%p\n", str );
1210 return GlobalAddAtomW( str );
1214 /***********************************************************************
1215 * BroadcastSystemMessage (USER32.@)
1216 * BroadcastSystemMessageA (USER32.@)
1218 LONG WINAPI BroadcastSystemMessage(
1219 DWORD dwFlags,LPDWORD recipients,UINT uMessage,WPARAM wParam,
1220 LPARAM lParam )
1222 if ((*recipients & BSM_APPLICATIONS)||
1223 (*recipients == BSM_ALLCOMPONENTS))
1225 FIXME("(%08lx,%08lx,%08x,%08x,%08lx): semi-stub!\n",
1226 dwFlags,*recipients,uMessage,wParam,lParam);
1227 PostMessageA(HWND_BROADCAST,uMessage,wParam,lParam);
1228 return 1;
1230 else
1232 FIXME("(%08lx,%08lx,%08x,%08x,%08lx): stub!\n",
1233 dwFlags,*recipients,uMessage,wParam,lParam);
1234 return -1;