Merged mouse buttons states into the key state array.
[wine/multimedia.git] / windows / message.c
blob8d6644e3fa93a6600c3f9a60257c94c91f5462ba
1 /*
2 * Message queues related functions
4 * Copyright 1993, 1994 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include <sys/time.h>
11 #include <sys/types.h>
13 #include "winbase.h"
14 #include "wingdi.h"
15 #include "winuser.h"
16 #include "message.h"
17 #include "winerror.h"
18 #include "wine/server.h"
19 #include "win.h"
20 #include "heap.h"
21 #include "hook.h"
22 #include "input.h"
23 #include "spy.h"
24 #include "winpos.h"
25 #include "dde.h"
26 #include "queue.h"
27 #include "winproc.h"
28 #include "user.h"
29 #include "thread.h"
30 #include "task.h"
31 #include "controls.h"
32 #include "debugtools.h"
34 DEFAULT_DEBUG_CHANNEL(msg);
35 DECLARE_DEBUG_CHANNEL(key);
37 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
38 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
40 static BYTE QueueKeyStateTable[256];
41 static UINT doubleClickSpeed = 452;
44 /***********************************************************************
45 * is_keyboard_message
47 inline static BOOL is_keyboard_message( UINT message )
49 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
53 /***********************************************************************
54 * is_mouse_message
56 inline static BOOL is_mouse_message( UINT message )
58 return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
59 (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
63 /***********************************************************************
64 * check_message_filter
66 inline static BOOL check_message_filter( const MSG *msg, HWND hwnd, UINT first, UINT last )
68 if (hwnd)
70 if (msg->hwnd != hwnd && !IsChild( hwnd, msg->hwnd )) return FALSE;
72 if (first || last)
74 return (msg->message >= first && msg->message <= last);
76 return TRUE;
80 /***********************************************************************
81 * process_sent_messages
83 * Process all pending sent messages.
85 inline static void process_sent_messages(void)
87 MSG msg;
88 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
92 /***********************************************************************
93 * queue_hardware_message
95 * store a hardware message in the thread queue
97 static void queue_hardware_message( MSG *msg, ULONG_PTR extra_info, enum message_type type )
99 SERVER_START_REQ( send_message )
101 req->type = type;
102 req->id = (void *)GetWindowThreadProcessId( msg->hwnd, NULL );
103 req->win = msg->hwnd;
104 req->msg = msg->message;
105 req->wparam = msg->wParam;
106 req->lparam = msg->lParam;
107 req->x = msg->pt.x;
108 req->y = msg->pt.y;
109 req->time = msg->time;
110 req->info = extra_info;
111 req->timeout = 0;
112 SERVER_CALL();
114 SERVER_END_REQ;
118 /***********************************************************************
119 * update_queue_key_state
121 static void update_queue_key_state( UINT msg, WPARAM wp )
123 BOOL down = FALSE;
125 switch (msg)
127 case WM_LBUTTONDOWN:
128 down = TRUE;
129 /* fall through */
130 case WM_LBUTTONUP:
131 wp = VK_LBUTTON;
132 break;
133 case WM_MBUTTONDOWN:
134 down = TRUE;
135 /* fall through */
136 case WM_MBUTTONUP:
137 wp = VK_MBUTTON;
138 break;
139 case WM_RBUTTONDOWN:
140 down = TRUE;
141 /* fall through */
142 case WM_RBUTTONUP:
143 wp = VK_RBUTTON;
144 break;
145 case WM_KEYDOWN:
146 case WM_SYSKEYDOWN:
147 down = TRUE;
148 /* fall through */
149 case WM_KEYUP:
150 case WM_SYSKEYUP:
151 wp = wp & 0xff;
152 break;
154 if (down)
156 BYTE *p = &QueueKeyStateTable[wp];
157 if (!(*p & 0x80)) *p ^= 0x01;
158 *p |= 0x80;
160 else QueueKeyStateTable[wp] &= ~0x80;
164 /***********************************************************************
165 * MSG_SendParentNotify
167 * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
168 * the window has the WS_EX_NOPARENTNOTIFY style.
170 static void MSG_SendParentNotify( HWND hwnd, WORD event, WORD idChild, POINT pt )
172 /* pt has to be in the client coordinates of the parent window */
173 MapWindowPoints( 0, hwnd, &pt, 1 );
174 for (;;)
176 HWND parent;
178 if (!(GetWindowLongA( hwnd, GWL_STYLE ) & WS_CHILD)) break;
179 if (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
180 if (!(parent = GetParent(hwnd))) break;
181 MapWindowPoints( hwnd, parent, &pt, 1 );
182 hwnd = parent;
183 SendMessageA( hwnd, WM_PARENTNOTIFY,
184 MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
189 /***********************************************************************
190 * MSG_JournalPlayBackMsg
192 * Get an EVENTMSG struct via call JOURNALPLAYBACK hook function
194 void MSG_JournalPlayBackMsg(void)
196 EVENTMSG tmpMsg;
197 MSG msg;
198 LRESULT wtime;
199 int keyDown,i;
201 if (!HOOK_IsHooked( WH_JOURNALPLAYBACK )) return;
203 wtime=HOOK_CallHooksA( WH_JOURNALPLAYBACK, HC_GETNEXT, 0, (LPARAM)&tmpMsg );
204 /* TRACE(msg,"Playback wait time =%ld\n",wtime); */
205 if (wtime<=0)
207 wtime=0;
208 msg.message = tmpMsg.message;
209 msg.hwnd = tmpMsg.hwnd;
210 msg.time = tmpMsg.time;
211 if ((tmpMsg.message >= WM_KEYFIRST) && (tmpMsg.message <= WM_KEYLAST))
213 msg.wParam = tmpMsg.paramL & 0xFF;
214 msg.lParam = MAKELONG(tmpMsg.paramH&0x7ffff,tmpMsg.paramL>>8);
215 if (tmpMsg.message == WM_KEYDOWN || tmpMsg.message == WM_SYSKEYDOWN)
217 for (keyDown=i=0; i<256 && !keyDown; i++)
218 if (InputKeyStateTable[i] & 0x80)
219 keyDown++;
220 if (!keyDown)
221 msg.lParam |= 0x40000000;
222 InputKeyStateTable[msg.wParam] |= 0x80;
223 AsyncKeyStateTable[msg.wParam] |= 0x80;
225 else /* WM_KEYUP, WM_SYSKEYUP */
227 msg.lParam |= 0xC0000000;
228 InputKeyStateTable[msg.wParam] &= ~0x80;
230 if (InputKeyStateTable[VK_MENU] & 0x80)
231 msg.lParam |= 0x20000000;
232 if (tmpMsg.paramH & 0x8000) /*special_key bit*/
233 msg.lParam |= 0x01000000;
235 msg.pt.x = msg.pt.y = 0;
236 queue_hardware_message( &msg, 0, MSG_HARDWARE_RAW );
238 else if ((tmpMsg.message>= WM_MOUSEFIRST) && (tmpMsg.message <= WM_MOUSELAST))
240 switch (tmpMsg.message)
242 case WM_LBUTTONDOWN:
243 InputKeyStateTable[VK_LBUTTON] |= 0x80;
244 AsyncKeyStateTable[VK_LBUTTON] |= 0x80;
245 break;
246 case WM_LBUTTONUP:
247 InputKeyStateTable[VK_LBUTTON] &= ~0x80;
248 break;
249 case WM_MBUTTONDOWN:
250 InputKeyStateTable[VK_MBUTTON] |= 0x80;
251 AsyncKeyStateTable[VK_MBUTTON] |= 0x80;
252 break;
253 case WM_MBUTTONUP:
254 InputKeyStateTable[VK_MBUTTON] &= ~0x80;
255 break;
256 case WM_RBUTTONDOWN:
257 InputKeyStateTable[VK_RBUTTON] |= 0x80;
258 AsyncKeyStateTable[VK_RBUTTON] |= 0x80;
259 break;
260 case WM_RBUTTONUP:
261 InputKeyStateTable[VK_RBUTTON] &= ~0x80;
262 break;
264 SetCursorPos(tmpMsg.paramL,tmpMsg.paramH);
265 msg.lParam=MAKELONG(tmpMsg.paramL,tmpMsg.paramH);
266 msg.wParam=0;
267 if (InputKeyStateTable[VK_LBUTTON] & 0x80) msg.wParam |= MK_LBUTTON;
268 if (InputKeyStateTable[VK_MBUTTON] & 0x80) msg.wParam |= MK_MBUTTON;
269 if (InputKeyStateTable[VK_RBUTTON] & 0x80) msg.wParam |= MK_RBUTTON;
271 msg.pt.x = tmpMsg.paramL;
272 msg.pt.y = tmpMsg.paramH;
273 queue_hardware_message( &msg, 0, MSG_HARDWARE_RAW );
275 HOOK_CallHooksA( WH_JOURNALPLAYBACK, HC_SKIP, 0, (LPARAM)&tmpMsg);
277 else
279 if( tmpMsg.message == WM_QUEUESYNC )
280 if (HOOK_IsHooked( WH_CBT ))
281 HOOK_CallHooksA( WH_CBT, HCBT_QS, 0, 0L);
286 /***********************************************************************
287 * process_raw_keyboard_message
289 * returns TRUE if the contents of 'msg' should be passed to the application
291 static BOOL process_raw_keyboard_message( MSG *msg, ULONG_PTR extra_info )
293 if (!(msg->hwnd = GetFocus()))
295 /* Send the message to the active window instead, */
296 /* translating messages to their WM_SYS equivalent */
297 msg->hwnd = GetActiveWindow();
298 if (msg->message < WM_SYSKEYDOWN) msg->message += WM_SYSKEYDOWN - WM_KEYDOWN;
301 if (HOOK_IsHooked( WH_JOURNALRECORD ))
303 EVENTMSG event;
305 event.message = msg->message;
306 event.hwnd = msg->hwnd;
307 event.time = msg->time;
308 event.paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
309 event.paramH = msg->lParam & 0x7FFF;
310 if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
311 HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event );
314 return (msg->hwnd != 0);
318 /***********************************************************************
319 * process_cooked_keyboard_message
321 * returns TRUE if the contents of 'msg' should be passed to the application
323 static BOOL process_cooked_keyboard_message( MSG *msg, BOOL remove )
325 if (remove)
327 update_queue_key_state( msg->message, msg->wParam );
329 /* Handle F1 key by sending out WM_HELP message */
330 if ((msg->message == WM_KEYUP) &&
331 (msg->wParam == VK_F1) &&
332 (msg->hwnd != GetDesktopWindow()) &&
333 !MENU_IsMenuActive())
335 HELPINFO hi;
336 hi.cbSize = sizeof(HELPINFO);
337 hi.iContextType = HELPINFO_WINDOW;
338 hi.iCtrlId = GetWindowLongA( msg->hwnd, GWL_ID );
339 hi.hItemHandle = msg->hwnd;
340 hi.dwContextId = GetWindowContextHelpId( msg->hwnd );
341 hi.MousePos = msg->pt;
342 SendMessageA(msg->hwnd, WM_HELP, 0, (LPARAM)&hi);
346 if (HOOK_CallHooksA( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
347 LOWORD(msg->wParam), msg->lParam ))
349 /* skip this message */
350 HOOK_CallHooksA( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam );
351 return FALSE;
353 return TRUE;
357 /***********************************************************************
358 * process_raw_mouse_message
360 * returns TRUE if the contents of 'msg' should be passed to the application
362 static BOOL process_raw_mouse_message( MSG *msg, ULONG_PTR extra_info )
364 static MSG clk_msg;
366 POINT pt;
367 INT ht, hittest;
369 /* find the window to dispatch this mouse message to */
371 hittest = HTCLIENT;
372 if (!(msg->hwnd = PERQDATA_GetCaptureWnd( &ht )))
374 /* If no capture HWND, find window which contains the mouse position.
375 * Also find the position of the cursor hot spot (hittest) */
376 HWND hWndScope = (HWND)extra_info;
378 if (!IsWindow(hWndScope)) hWndScope = 0;
379 if (!(msg->hwnd = WINPOS_WindowFromPoint( hWndScope, msg->pt, &hittest )))
380 msg->hwnd = GetDesktopWindow();
381 ht = hittest;
384 if (HOOK_IsHooked( WH_JOURNALRECORD ))
386 EVENTMSG event;
387 event.message = msg->message;
388 event.time = msg->time;
389 event.hwnd = msg->hwnd;
390 event.paramL = msg->pt.x;
391 event.paramH = msg->pt.y;
392 HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event );
395 /* translate double clicks */
397 if ((msg->message == WM_LBUTTONDOWN) ||
398 (msg->message == WM_RBUTTONDOWN) ||
399 (msg->message == WM_MBUTTONDOWN))
401 BOOL update = TRUE;
402 /* translate double clicks -
403 * note that ...MOUSEMOVEs can slip in between
404 * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
406 if (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS || ht != HTCLIENT )
408 if ((msg->message == clk_msg.message) &&
409 (msg->hwnd == clk_msg.hwnd) &&
410 (msg->time - clk_msg.time < doubleClickSpeed) &&
411 (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
412 (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
414 msg->message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
415 clk_msg.message = 0;
416 update = FALSE;
419 /* update static double click conditions */
420 if (update) clk_msg = *msg;
423 pt = msg->pt;
424 /* Note: windows has no concept of a non-client wheel message */
425 if (hittest != HTCLIENT && msg->message != WM_MOUSEWHEEL)
427 msg->message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
428 msg->wParam = hittest;
430 else ScreenToClient( msg->hwnd, &pt );
431 msg->lParam = MAKELONG( pt.x, pt.y );
432 return TRUE;
436 /***********************************************************************
437 * process_cooked_mouse_message
439 * returns TRUE if the contents of 'msg' should be passed to the application
441 static BOOL process_cooked_mouse_message( MSG *msg, BOOL remove )
443 INT hittest = HTCLIENT;
444 UINT raw_message = msg->message;
445 BOOL eatMsg;
447 if (msg->message >= WM_NCMOUSEFIRST && msg->message <= WM_NCMOUSELAST)
449 raw_message += WM_MOUSEFIRST - WM_NCMOUSEFIRST;
450 hittest = msg->wParam;
452 if (raw_message == WM_LBUTTONDBLCLK ||
453 raw_message == WM_RBUTTONDBLCLK ||
454 raw_message == WM_MBUTTONDBLCLK)
456 raw_message += WM_LBUTTONDOWN - WM_LBUTTONDBLCLK;
459 if (remove) update_queue_key_state( raw_message, 0 );
461 if (HOOK_IsHooked( WH_MOUSE ))
463 MOUSEHOOKSTRUCT hook;
464 hook.pt = msg->pt;
465 hook.hwnd = msg->hwnd;
466 hook.wHitTestCode = hittest;
467 hook.dwExtraInfo = 0;
468 if (HOOK_CallHooksA( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
469 msg->message, (LPARAM)&hook ))
471 hook.pt = msg->pt;
472 hook.hwnd = msg->hwnd;
473 hook.wHitTestCode = hittest;
474 hook.dwExtraInfo = 0;
475 HOOK_CallHooksA( WH_CBT, HCBT_CLICKSKIPPED, msg->message, (LPARAM)&hook );
476 return FALSE;
480 if ((hittest == HTERROR) || (hittest == HTNOWHERE))
482 SendMessageA( msg->hwnd, WM_SETCURSOR, msg->hwnd, MAKELONG( hittest, raw_message ));
483 return FALSE;
486 if (!remove || GetCapture()) return TRUE;
488 eatMsg = FALSE;
490 if ((raw_message == WM_LBUTTONDOWN) ||
491 (raw_message == WM_RBUTTONDOWN) ||
492 (raw_message == WM_MBUTTONDOWN))
494 HWND hwndTop = GetAncestor( msg->hwnd, GA_ROOT );
496 /* Send the WM_PARENTNOTIFY,
497 * note that even for double/nonclient clicks
498 * notification message is still WM_L/M/RBUTTONDOWN.
500 MSG_SendParentNotify( msg->hwnd, raw_message, 0, msg->pt );
502 /* Activate the window if needed */
504 if (msg->hwnd != GetActiveWindow() && hwndTop != GetDesktopWindow())
506 LONG ret = SendMessageA( msg->hwnd, WM_MOUSEACTIVATE, hwndTop,
507 MAKELONG( hittest, raw_message ) );
509 switch(ret)
511 case MA_NOACTIVATEANDEAT:
512 eatMsg = TRUE;
513 /* fall through */
514 case MA_NOACTIVATE:
515 break;
516 case MA_ACTIVATEANDEAT:
517 eatMsg = TRUE;
518 /* fall through */
519 case MA_ACTIVATE:
520 if (hwndTop != GetForegroundWindow() )
522 if (!WINPOS_SetActiveWindow( hwndTop, TRUE , TRUE ))
523 eatMsg = TRUE;
525 break;
526 default:
527 WARN( "unknown WM_MOUSEACTIVATE code %ld\n", ret );
528 break;
533 /* send the WM_SETCURSOR message */
535 /* Windows sends the normal mouse message as the message parameter
536 in the WM_SETCURSOR message even if it's non-client mouse message */
537 SendMessageA( msg->hwnd, WM_SETCURSOR, msg->hwnd, MAKELONG( hittest, raw_message ));
539 return !eatMsg;
543 /***********************************************************************
544 * process_hardware_message
546 * returns TRUE if the contents of 'msg' should be passed to the application
548 BOOL MSG_process_raw_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd_filter,
549 UINT first, UINT last, BOOL remove )
551 if (is_keyboard_message( msg->message ))
553 if (!process_raw_keyboard_message( msg, extra_info )) return FALSE;
555 else if (is_mouse_message( msg->message ))
557 if (!process_raw_mouse_message( msg, extra_info )) return FALSE;
559 else
561 ERR( "unknown message type %x\n", msg->message );
562 return FALSE;
565 /* check destination thread and filters */
566 if (!check_message_filter( msg, hwnd_filter, first, last ) ||
567 !WIN_IsCurrentThread( msg->hwnd ))
569 /* queue it for later, or for another thread */
570 queue_hardware_message( msg, extra_info, MSG_HARDWARE_COOKED );
571 return FALSE;
574 /* save the message in the cooked queue if we didn't want to remove it */
575 if (!remove) queue_hardware_message( msg, extra_info, MSG_HARDWARE_COOKED );
576 return TRUE;
580 /***********************************************************************
581 * MSG_process_cooked_hardware_message
583 * returns TRUE if the contents of 'msg' should be passed to the application
585 BOOL MSG_process_cooked_hardware_message( MSG *msg, BOOL remove )
587 if (is_keyboard_message( msg->message ))
588 return process_cooked_keyboard_message( msg, remove );
590 if (is_mouse_message( msg->message ))
591 return process_cooked_mouse_message( msg, remove );
593 ERR( "unknown message type %x\n", msg->message );
594 return FALSE;
598 /**********************************************************************
599 * GetKeyState (USER.106)
601 INT16 WINAPI GetKeyState16(INT16 vkey)
603 return GetKeyState(vkey);
607 /**********************************************************************
608 * GetKeyState (USER32.@)
610 * An application calls the GetKeyState function in response to a
611 * keyboard-input message. This function retrieves the state of the key
612 * at the time the input message was generated. (SDK 3.1 Vol 2. p 390)
614 SHORT WINAPI GetKeyState(INT vkey)
616 INT retval;
618 if (vkey >= 'a' && vkey <= 'z') vkey += 'A' - 'a';
619 retval = ((WORD)(QueueKeyStateTable[vkey] & 0x80) << 8 ) | (QueueKeyStateTable[vkey] & 0x01);
620 /* TRACE(key, "(0x%x) -> %x\n", vkey, retval); */
621 return retval;
625 /**********************************************************************
626 * GetKeyboardState (USER.222)
627 * GetKeyboardState (USER32.@)
629 * An application calls the GetKeyboardState function in response to a
630 * keyboard-input message. This function retrieves the state of the keyboard
631 * at the time the input message was generated. (SDK 3.1 Vol 2. p 387)
633 BOOL WINAPI GetKeyboardState(LPBYTE lpKeyState)
635 TRACE_(key)("(%p)\n", lpKeyState);
636 if (lpKeyState) memcpy(lpKeyState, QueueKeyStateTable, 256);
637 return TRUE;
641 /**********************************************************************
642 * SetKeyboardState (USER.223)
643 * SetKeyboardState (USER32.@)
645 BOOL WINAPI SetKeyboardState(LPBYTE lpKeyState)
647 TRACE_(key)("(%p)\n", lpKeyState);
648 if (lpKeyState) memcpy(QueueKeyStateTable, lpKeyState, 256);
649 return TRUE;
653 /**********************************************************************
654 * SetDoubleClickTime (USER32.@)
656 BOOL WINAPI SetDoubleClickTime( UINT interval )
658 doubleClickSpeed = interval ? interval : 500;
659 return TRUE;
663 /**********************************************************************
664 * GetDoubleClickTime (USER32.@)
666 UINT WINAPI GetDoubleClickTime(void)
668 return doubleClickSpeed;
672 /***********************************************************************
673 * WaitMessage (USER.112) Suspend thread pending messages
674 * WaitMessage (USER32.@) Suspend thread pending messages
676 * WaitMessage() suspends a thread until events appear in the thread's
677 * queue.
679 BOOL WINAPI WaitMessage(void)
681 return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
685 /***********************************************************************
686 * MsgWaitForMultipleObjectsEx (USER32.@)
688 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
689 DWORD timeout, DWORD mask, DWORD flags )
691 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
692 DWORD i, ret;
693 MESSAGEQUEUE *msgQueue;
695 if (count > MAXIMUM_WAIT_OBJECTS-1)
697 SetLastError( ERROR_INVALID_PARAMETER );
698 return WAIT_FAILED;
701 if (!(msgQueue = QUEUE_Current())) return WAIT_FAILED;
703 /* set the queue mask */
704 SERVER_START_REQ( set_queue_mask )
706 req->wake_mask = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
707 req->changed_mask = mask;
708 req->skip_wait = 0;
709 SERVER_CALL();
711 SERVER_END_REQ;
713 /* Add the thread event to the handle list */
714 for (i = 0; i < count; i++) handles[i] = pHandles[i];
715 handles[count] = msgQueue->server_queue;
718 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
720 ret = USER_Driver.pMsgWaitForMultipleObjectsEx( count+1, handles, timeout, mask, flags );
721 if (ret == count+1) ret = count; /* pretend the msg queue is ready */
723 else
724 ret = WaitForMultipleObjectsEx( count+1, handles, flags & MWMO_WAITALL,
725 timeout, flags & MWMO_ALERTABLE );
726 return ret;
730 /***********************************************************************
731 * MsgWaitForMultipleObjects (USER32.@)
733 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
734 BOOL wait_all, DWORD timeout, DWORD mask )
736 return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
737 wait_all ? MWMO_WAITALL : 0 );
741 /***********************************************************************
742 * WaitForInputIdle (USER32.@)
744 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
746 DWORD cur_time, ret;
747 HANDLE idle_event = -1;
749 SERVER_START_REQ( wait_input_idle )
751 req->handle = hProcess;
752 req->timeout = dwTimeOut;
753 if (!(ret = SERVER_CALL_ERR())) idle_event = req->event;
755 SERVER_END_REQ;
756 if (ret) return 0xffffffff; /* error */
757 if (!idle_event) return 0; /* no event to wait on */
759 cur_time = GetTickCount();
761 TRACE("waiting for %x\n", idle_event );
762 while ( dwTimeOut > GetTickCount() - cur_time || dwTimeOut == INFINITE )
764 ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut, QS_SENDMESSAGE );
765 if ( ret == ( WAIT_OBJECT_0 + 1 ))
767 process_sent_messages();
768 continue;
770 if ( ret == WAIT_TIMEOUT || ret == 0xFFFFFFFF )
772 TRACE("timeout or error\n");
773 return ret;
775 else
777 TRACE("finished\n");
778 return 0;
782 return WAIT_TIMEOUT;
786 /***********************************************************************
787 * UserYield (USER.332)
788 * UserYield16 (USER32.@)
790 void WINAPI UserYield16(void)
792 DWORD count;
794 /* Handle sent messages */
795 process_sent_messages();
797 /* Yield */
798 ReleaseThunkLock(&count);
799 if (count)
801 RestoreThunkLock(count);
802 /* Handle sent messages again */
803 process_sent_messages();
808 struct accent_char
810 BYTE ac_accent;
811 BYTE ac_char;
812 BYTE ac_result;
815 static const struct accent_char accent_chars[] =
817 /* A good idea should be to read /usr/X11/lib/X11/locale/iso8859-x/Compose */
818 {'`', 'A', '\300'}, {'`', 'a', '\340'},
819 {'\'', 'A', '\301'}, {'\'', 'a', '\341'},
820 {'^', 'A', '\302'}, {'^', 'a', '\342'},
821 {'~', 'A', '\303'}, {'~', 'a', '\343'},
822 {'"', 'A', '\304'}, {'"', 'a', '\344'},
823 {'O', 'A', '\305'}, {'o', 'a', '\345'},
824 {'0', 'A', '\305'}, {'0', 'a', '\345'},
825 {'A', 'A', '\305'}, {'a', 'a', '\345'},
826 {'A', 'E', '\306'}, {'a', 'e', '\346'},
827 {',', 'C', '\307'}, {',', 'c', '\347'},
828 {'`', 'E', '\310'}, {'`', 'e', '\350'},
829 {'\'', 'E', '\311'}, {'\'', 'e', '\351'},
830 {'^', 'E', '\312'}, {'^', 'e', '\352'},
831 {'"', 'E', '\313'}, {'"', 'e', '\353'},
832 {'`', 'I', '\314'}, {'`', 'i', '\354'},
833 {'\'', 'I', '\315'}, {'\'', 'i', '\355'},
834 {'^', 'I', '\316'}, {'^', 'i', '\356'},
835 {'"', 'I', '\317'}, {'"', 'i', '\357'},
836 {'-', 'D', '\320'}, {'-', 'd', '\360'},
837 {'~', 'N', '\321'}, {'~', 'n', '\361'},
838 {'`', 'O', '\322'}, {'`', 'o', '\362'},
839 {'\'', 'O', '\323'}, {'\'', 'o', '\363'},
840 {'^', 'O', '\324'}, {'^', 'o', '\364'},
841 {'~', 'O', '\325'}, {'~', 'o', '\365'},
842 {'"', 'O', '\326'}, {'"', 'o', '\366'},
843 {'/', 'O', '\330'}, {'/', 'o', '\370'},
844 {'`', 'U', '\331'}, {'`', 'u', '\371'},
845 {'\'', 'U', '\332'}, {'\'', 'u', '\372'},
846 {'^', 'U', '\333'}, {'^', 'u', '\373'},
847 {'"', 'U', '\334'}, {'"', 'u', '\374'},
848 {'\'', 'Y', '\335'}, {'\'', 'y', '\375'},
849 {'T', 'H', '\336'}, {'t', 'h', '\376'},
850 {'s', 's', '\337'}, {'"', 'y', '\377'},
851 {'s', 'z', '\337'}, {'i', 'j', '\377'},
852 /* iso-8859-2 uses this */
853 {'<', 'L', '\245'}, {'<', 'l', '\265'}, /* caron */
854 {'<', 'S', '\251'}, {'<', 's', '\271'},
855 {'<', 'T', '\253'}, {'<', 't', '\273'},
856 {'<', 'Z', '\256'}, {'<', 'z', '\276'},
857 {'<', 'C', '\310'}, {'<', 'c', '\350'},
858 {'<', 'E', '\314'}, {'<', 'e', '\354'},
859 {'<', 'D', '\317'}, {'<', 'd', '\357'},
860 {'<', 'N', '\322'}, {'<', 'n', '\362'},
861 {'<', 'R', '\330'}, {'<', 'r', '\370'},
862 {';', 'A', '\241'}, {';', 'a', '\261'}, /* ogonek */
863 {';', 'E', '\312'}, {';', 'e', '\332'},
864 {'\'', 'Z', '\254'}, {'\'', 'z', '\274'}, /* acute */
865 {'\'', 'R', '\300'}, {'\'', 'r', '\340'},
866 {'\'', 'L', '\305'}, {'\'', 'l', '\345'},
867 {'\'', 'C', '\306'}, {'\'', 'c', '\346'},
868 {'\'', 'N', '\321'}, {'\'', 'n', '\361'},
869 /* collision whith S, from iso-8859-9 !!! */
870 {',', 'S', '\252'}, {',', 's', '\272'}, /* cedilla */
871 {',', 'T', '\336'}, {',', 't', '\376'},
872 {'.', 'Z', '\257'}, {'.', 'z', '\277'}, /* dot above */
873 {'/', 'L', '\243'}, {'/', 'l', '\263'}, /* slash */
874 {'/', 'D', '\320'}, {'/', 'd', '\360'},
875 {'(', 'A', '\303'}, {'(', 'a', '\343'}, /* breve */
876 {'\275', 'O', '\325'}, {'\275', 'o', '\365'}, /* double acute */
877 {'\275', 'U', '\334'}, {'\275', 'u', '\374'},
878 {'0', 'U', '\332'}, {'0', 'u', '\372'}, /* ring above */
879 /* iso-8859-3 uses this */
880 {'/', 'H', '\241'}, {'/', 'h', '\261'}, /* slash */
881 {'>', 'H', '\246'}, {'>', 'h', '\266'}, /* circumflex */
882 {'>', 'J', '\254'}, {'>', 'j', '\274'},
883 {'>', 'C', '\306'}, {'>', 'c', '\346'},
884 {'>', 'G', '\330'}, {'>', 'g', '\370'},
885 {'>', 'S', '\336'}, {'>', 's', '\376'},
886 /* collision whith G( from iso-8859-9 !!! */
887 {'(', 'G', '\253'}, {'(', 'g', '\273'}, /* breve */
888 {'(', 'U', '\335'}, {'(', 'u', '\375'},
889 /* collision whith I. from iso-8859-3 !!! */
890 {'.', 'I', '\251'}, {'.', 'i', '\271'}, /* dot above */
891 {'.', 'C', '\305'}, {'.', 'c', '\345'},
892 {'.', 'G', '\325'}, {'.', 'g', '\365'},
893 /* iso-8859-4 uses this */
894 {',', 'R', '\243'}, {',', 'r', '\263'}, /* cedilla */
895 {',', 'L', '\246'}, {',', 'l', '\266'},
896 {',', 'G', '\253'}, {',', 'g', '\273'},
897 {',', 'N', '\321'}, {',', 'n', '\361'},
898 {',', 'K', '\323'}, {',', 'k', '\363'},
899 {'~', 'I', '\245'}, {'~', 'i', '\265'}, /* tilde */
900 {'-', 'E', '\252'}, {'-', 'e', '\272'}, /* macron */
901 {'-', 'A', '\300'}, {'-', 'a', '\340'},
902 {'-', 'I', '\317'}, {'-', 'i', '\357'},
903 {'-', 'O', '\322'}, {'-', 'o', '\362'},
904 {'-', 'U', '\336'}, {'-', 'u', '\376'},
905 {'/', 'T', '\254'}, {'/', 't', '\274'}, /* slash */
906 {'.', 'E', '\314'}, {'.', 'e', '\344'}, /* dot above */
907 {';', 'I', '\307'}, {';', 'i', '\347'}, /* ogonek */
908 {';', 'U', '\331'}, {';', 'u', '\371'},
909 /* iso-8859-9 uses this */
910 /* iso-8859-9 has really bad choosen G( S, and I. as they collide
911 * whith the same letters on other iso-8859-x (that is they are on
912 * different places :-( ), if you use turkish uncomment these and
913 * comment out the lines in iso-8859-2 and iso-8859-3 sections
914 * FIXME: should be dynamic according to chosen language
915 * if/when Wine has turkish support.
917 /* collision whith G( from iso-8859-3 !!! */
918 /* {'(', 'G', '\320'}, {'(', 'g', '\360'}, */ /* breve */
919 /* collision whith S, from iso-8859-2 !!! */
920 /* {',', 'S', '\336'}, {',', 's', '\376'}, */ /* cedilla */
921 /* collision whith I. from iso-8859-3 !!! */
922 /* {'.', 'I', '\335'}, {'.', 'i', '\375'}, */ /* dot above */
926 /***********************************************************************
927 * TranslateMessage (USER32.@)
929 * Implementation of TranslateMessage.
931 * TranslateMessage translates virtual-key messages into character-messages,
932 * as follows :
933 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
934 * ditto replacing WM_* with WM_SYS*
935 * This produces WM_CHAR messages only for keys mapped to ASCII characters
936 * by the keyboard driver.
938 BOOL WINAPI TranslateMessage( const MSG *msg )
940 static int dead_char;
941 UINT message;
942 WCHAR wp[2];
944 if (msg->message >= WM_KEYFIRST && msg->message <= WM_KEYLAST)
945 TRACE_(key)("(%s, %04X, %08lX)\n",
946 SPY_GetMsgName(msg->message, msg->hwnd), msg->wParam, msg->lParam );
948 if ((msg->message != WM_KEYDOWN) && (msg->message != WM_SYSKEYDOWN)) return FALSE;
950 TRACE_(key)("Translating key %s (%04x), scancode %02x\n",
951 SPY_GetVKeyName(msg->wParam), msg->wParam, LOBYTE(HIWORD(msg->lParam)));
953 /* FIXME : should handle ToUnicode yielding 2 */
954 switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), QueueKeyStateTable, wp, 2, 0))
956 case 1:
957 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
958 /* Should dead chars handling go in ToAscii ? */
959 if (dead_char)
961 int i;
963 if (wp[0] == ' ') wp[0] = dead_char;
964 if (dead_char == 0xa2) dead_char = '(';
965 else if (dead_char == 0xa8) dead_char = '"';
966 else if (dead_char == 0xb2) dead_char = ';';
967 else if (dead_char == 0xb4) dead_char = '\'';
968 else if (dead_char == 0xb7) dead_char = '<';
969 else if (dead_char == 0xb8) dead_char = ',';
970 else if (dead_char == 0xff) dead_char = '.';
971 for (i = 0; i < sizeof(accent_chars)/sizeof(accent_chars[0]); i++)
972 if ((accent_chars[i].ac_accent == dead_char) &&
973 (accent_chars[i].ac_char == wp[0]))
975 wp[0] = accent_chars[i].ac_result;
976 break;
978 dead_char = 0;
980 TRACE_(key)("1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
981 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
982 return TRUE;
984 case -1:
985 message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
986 dead_char = wp[0];
987 TRACE_(key)("-1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
988 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
989 return TRUE;
991 return FALSE;
995 /***********************************************************************
996 * DispatchMessageA (USER32.@)
998 LONG WINAPI DispatchMessageA( const MSG* msg )
1000 WND * wndPtr;
1001 LONG retval;
1002 int painting;
1004 /* Process timer messages */
1005 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1007 if (msg->lParam)
1009 /* HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1011 /* before calling window proc, verify whether timer is still valid;
1012 there's a slim chance that the application kills the timer
1013 between GetMessage and DispatchMessage API calls */
1014 if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (HWINDOWPROC) msg->lParam))
1015 return 0; /* invalid winproc */
1017 return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
1018 msg->message, msg->wParam, GetTickCount() );
1022 if (!msg->hwnd) return 0;
1023 if (!(wndPtr = WIN_FindWndPtr( msg->hwnd ))) return 0;
1024 if (!wndPtr->winproc)
1026 retval = 0;
1027 goto END;
1029 painting = (msg->message == WM_PAINT);
1030 if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1031 /* HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1033 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1034 msg->wParam, msg->lParam );
1035 retval = CallWindowProcA( (WNDPROC)wndPtr->winproc,
1036 msg->hwnd, msg->message,
1037 msg->wParam, msg->lParam );
1038 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1039 msg->wParam, msg->lParam );
1041 WIN_ReleaseWndPtr(wndPtr);
1042 wndPtr = WIN_FindWndPtr(msg->hwnd);
1044 if (painting && wndPtr &&
1045 (wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate)
1047 ERR("BeginPaint not called on WM_PAINT for hwnd %04x!\n",
1048 msg->hwnd);
1049 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1050 /* Validate the update region to avoid infinite WM_PAINT loop */
1051 RedrawWindow( wndPtr->hwndSelf, NULL, 0,
1052 RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1054 END:
1055 WIN_ReleaseWndPtr(wndPtr);
1056 return retval;
1060 /***********************************************************************
1061 * DispatchMessageW (USER32.@) Process Message
1063 * Process the message specified in the structure *_msg_.
1065 * If the lpMsg parameter points to a WM_TIMER message and the
1066 * parameter of the WM_TIMER message is not NULL, the lParam parameter
1067 * points to the function that is called instead of the window
1068 * procedure.
1070 * The message must be valid.
1072 * RETURNS
1074 * DispatchMessage() returns the result of the window procedure invoked.
1076 * CONFORMANCE
1078 * ECMA-234, Win32
1081 LONG WINAPI DispatchMessageW( const MSG* msg )
1083 WND * wndPtr;
1084 LONG retval;
1085 int painting;
1087 /* Process timer messages */
1088 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1090 if (msg->lParam)
1092 /* HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1094 /* before calling window proc, verify whether timer is still valid;
1095 there's a slim chance that the application kills the timer
1096 between GetMessage and DispatchMessage API calls */
1097 if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (HWINDOWPROC) msg->lParam))
1098 return 0; /* invalid winproc */
1100 return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
1101 msg->message, msg->wParam, GetTickCount() );
1105 if (!msg->hwnd) return 0;
1106 if (!(wndPtr = WIN_FindWndPtr( msg->hwnd ))) return 0;
1107 if (!wndPtr->winproc)
1109 retval = 0;
1110 goto END;
1112 painting = (msg->message == WM_PAINT);
1113 if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1114 /* HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1116 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1117 msg->wParam, msg->lParam );
1118 retval = CallWindowProcW( (WNDPROC)wndPtr->winproc,
1119 msg->hwnd, msg->message,
1120 msg->wParam, msg->lParam );
1121 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1122 msg->wParam, msg->lParam );
1124 WIN_ReleaseWndPtr(wndPtr);
1125 wndPtr = WIN_FindWndPtr(msg->hwnd);
1127 if (painting && wndPtr &&
1128 (wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate)
1130 ERR("BeginPaint not called on WM_PAINT for hwnd %04x!\n",
1131 msg->hwnd);
1132 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1133 /* Validate the update region to avoid infinite WM_PAINT loop */
1134 RedrawWindow( wndPtr->hwndSelf, NULL, 0,
1135 RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1137 END:
1138 WIN_ReleaseWndPtr(wndPtr);
1139 return retval;
1143 /***********************************************************************
1144 * RegisterWindowMessage (USER.118)
1145 * RegisterWindowMessageA (USER32.@)
1147 WORD WINAPI RegisterWindowMessageA( LPCSTR str )
1149 TRACE("%s\n", str );
1150 return GlobalAddAtomA( str );
1154 /***********************************************************************
1155 * RegisterWindowMessageW (USER32.@)
1157 WORD WINAPI RegisterWindowMessageW( LPCWSTR str )
1159 TRACE("%p\n", str );
1160 return GlobalAddAtomW( str );
1164 /***********************************************************************
1165 * BroadcastSystemMessage (USER32.@)
1167 LONG WINAPI BroadcastSystemMessage(
1168 DWORD dwFlags,LPDWORD recipients,UINT uMessage,WPARAM wParam,
1169 LPARAM lParam
1171 FIXME("(%08lx,%08lx,%08x,%08x,%08lx): stub!\n",
1172 dwFlags,*recipients,uMessage,wParam,lParam
1174 return 0;