And in addition to Mingw32 we will want the _ versions.
[wine/wine-kai.git] / windows / message.c
blob517751c45a1978bce39abe13fbe5b0cb4e05b20c
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];
43 /***********************************************************************
44 * is_keyboard_message
46 inline static BOOL is_keyboard_message( UINT message )
48 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
52 /***********************************************************************
53 * is_mouse_message
55 inline static BOOL is_mouse_message( UINT message )
57 return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
58 (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
62 /***********************************************************************
63 * check_message_filter
65 inline static BOOL check_message_filter( const MSG *msg, HWND hwnd, UINT first, UINT last )
67 if (hwnd)
69 if (msg->hwnd != hwnd && !IsChild( hwnd, msg->hwnd )) return FALSE;
71 if (first || last)
73 return (msg->message >= first && msg->message <= last);
75 return TRUE;
79 /***********************************************************************
80 * process_sent_messages
82 * Process all pending sent messages.
84 inline static void process_sent_messages(void)
86 MSG msg;
87 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
91 /***********************************************************************
92 * queue_hardware_message
94 * store a hardware message in the thread queue
96 static void queue_hardware_message( MSG *msg, ULONG_PTR extra_info, enum message_type type )
98 SERVER_START_REQ( send_message )
100 req->type = type;
101 req->id = (void *)GetWindowThreadProcessId( msg->hwnd, NULL );
102 req->win = msg->hwnd;
103 req->msg = msg->message;
104 req->wparam = msg->wParam;
105 req->lparam = msg->lParam;
106 req->x = msg->pt.x;
107 req->y = msg->pt.y;
108 req->time = msg->time;
109 req->info = extra_info;
110 req->timeout = 0;
111 wine_server_call( req );
113 SERVER_END_REQ;
117 /***********************************************************************
118 * update_queue_key_state
120 static void update_queue_key_state( UINT msg, WPARAM wp )
122 BOOL down = FALSE;
124 switch (msg)
126 case WM_LBUTTONDOWN:
127 down = TRUE;
128 /* fall through */
129 case WM_LBUTTONUP:
130 wp = VK_LBUTTON;
131 break;
132 case WM_MBUTTONDOWN:
133 down = TRUE;
134 /* fall through */
135 case WM_MBUTTONUP:
136 wp = VK_MBUTTON;
137 break;
138 case WM_RBUTTONDOWN:
139 down = TRUE;
140 /* fall through */
141 case WM_RBUTTONUP:
142 wp = VK_RBUTTON;
143 break;
144 case WM_KEYDOWN:
145 case WM_SYSKEYDOWN:
146 down = TRUE;
147 /* fall through */
148 case WM_KEYUP:
149 case WM_SYSKEYUP:
150 wp = wp & 0xff;
151 break;
153 if (down)
155 BYTE *p = &QueueKeyStateTable[wp];
156 if (!(*p & 0x80)) *p ^= 0x01;
157 *p |= 0x80;
159 else QueueKeyStateTable[wp] &= ~0x80;
163 /***********************************************************************
164 * MSG_SendParentNotify
166 * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
167 * the window has the WS_EX_NOPARENTNOTIFY style.
169 static void MSG_SendParentNotify( HWND hwnd, WORD event, WORD idChild, POINT pt )
171 /* pt has to be in the client coordinates of the parent window */
172 MapWindowPoints( 0, hwnd, &pt, 1 );
173 for (;;)
175 HWND parent;
177 if (!(GetWindowLongA( hwnd, GWL_STYLE ) & WS_CHILD)) break;
178 if (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
179 if (!(parent = GetParent(hwnd))) break;
180 MapWindowPoints( hwnd, parent, &pt, 1 );
181 hwnd = parent;
182 SendMessageA( hwnd, WM_PARENTNOTIFY,
183 MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
188 /***********************************************************************
189 * MSG_JournalPlayBackMsg
191 * Get an EVENTMSG struct via call JOURNALPLAYBACK hook function
193 void MSG_JournalPlayBackMsg(void)
195 EVENTMSG tmpMsg;
196 MSG msg;
197 LRESULT wtime;
198 int keyDown,i;
200 if (!HOOK_IsHooked( WH_JOURNALPLAYBACK )) return;
202 wtime=HOOK_CallHooksA( WH_JOURNALPLAYBACK, HC_GETNEXT, 0, (LPARAM)&tmpMsg );
203 /* TRACE(msg,"Playback wait time =%ld\n",wtime); */
204 if (wtime<=0)
206 wtime=0;
207 msg.message = tmpMsg.message;
208 msg.hwnd = tmpMsg.hwnd;
209 msg.time = tmpMsg.time;
210 if ((tmpMsg.message >= WM_KEYFIRST) && (tmpMsg.message <= WM_KEYLAST))
212 msg.wParam = tmpMsg.paramL & 0xFF;
213 msg.lParam = MAKELONG(tmpMsg.paramH&0x7ffff,tmpMsg.paramL>>8);
214 if (tmpMsg.message == WM_KEYDOWN || tmpMsg.message == WM_SYSKEYDOWN)
216 for (keyDown=i=0; i<256 && !keyDown; i++)
217 if (InputKeyStateTable[i] & 0x80)
218 keyDown++;
219 if (!keyDown)
220 msg.lParam |= 0x40000000;
221 InputKeyStateTable[msg.wParam] |= 0x80;
222 AsyncKeyStateTable[msg.wParam] |= 0x80;
224 else /* WM_KEYUP, WM_SYSKEYUP */
226 msg.lParam |= 0xC0000000;
227 InputKeyStateTable[msg.wParam] &= ~0x80;
229 if (InputKeyStateTable[VK_MENU] & 0x80)
230 msg.lParam |= 0x20000000;
231 if (tmpMsg.paramH & 0x8000) /*special_key bit*/
232 msg.lParam |= 0x01000000;
234 msg.pt.x = msg.pt.y = 0;
235 queue_hardware_message( &msg, 0, MSG_HARDWARE_RAW );
237 else if ((tmpMsg.message>= WM_MOUSEFIRST) && (tmpMsg.message <= WM_MOUSELAST))
239 switch (tmpMsg.message)
241 case WM_LBUTTONDOWN:
242 InputKeyStateTable[VK_LBUTTON] |= 0x80;
243 AsyncKeyStateTable[VK_LBUTTON] |= 0x80;
244 break;
245 case WM_LBUTTONUP:
246 InputKeyStateTable[VK_LBUTTON] &= ~0x80;
247 break;
248 case WM_MBUTTONDOWN:
249 InputKeyStateTable[VK_MBUTTON] |= 0x80;
250 AsyncKeyStateTable[VK_MBUTTON] |= 0x80;
251 break;
252 case WM_MBUTTONUP:
253 InputKeyStateTable[VK_MBUTTON] &= ~0x80;
254 break;
255 case WM_RBUTTONDOWN:
256 InputKeyStateTable[VK_RBUTTON] |= 0x80;
257 AsyncKeyStateTable[VK_RBUTTON] |= 0x80;
258 break;
259 case WM_RBUTTONUP:
260 InputKeyStateTable[VK_RBUTTON] &= ~0x80;
261 break;
263 SetCursorPos(tmpMsg.paramL,tmpMsg.paramH);
264 msg.lParam=MAKELONG(tmpMsg.paramL,tmpMsg.paramH);
265 msg.wParam=0;
266 if (InputKeyStateTable[VK_LBUTTON] & 0x80) msg.wParam |= MK_LBUTTON;
267 if (InputKeyStateTable[VK_MBUTTON] & 0x80) msg.wParam |= MK_MBUTTON;
268 if (InputKeyStateTable[VK_RBUTTON] & 0x80) msg.wParam |= MK_RBUTTON;
270 msg.pt.x = tmpMsg.paramL;
271 msg.pt.y = tmpMsg.paramH;
272 queue_hardware_message( &msg, 0, MSG_HARDWARE_RAW );
274 HOOK_CallHooksA( WH_JOURNALPLAYBACK, HC_SKIP, 0, (LPARAM)&tmpMsg);
276 else
278 if( tmpMsg.message == WM_QUEUESYNC )
279 if (HOOK_IsHooked( WH_CBT ))
280 HOOK_CallHooksA( WH_CBT, HCBT_QS, 0, 0L);
285 /***********************************************************************
286 * process_raw_keyboard_message
288 * returns TRUE if the contents of 'msg' should be passed to the application
290 static BOOL process_raw_keyboard_message( MSG *msg, ULONG_PTR extra_info )
292 if (!(msg->hwnd = GetFocus()))
294 /* Send the message to the active window instead, */
295 /* translating messages to their WM_SYS equivalent */
296 msg->hwnd = GetActiveWindow();
297 if (msg->message < WM_SYSKEYDOWN) msg->message += WM_SYSKEYDOWN - WM_KEYDOWN;
300 if (HOOK_IsHooked( WH_JOURNALRECORD ))
302 EVENTMSG event;
304 event.message = msg->message;
305 event.hwnd = msg->hwnd;
306 event.time = msg->time;
307 event.paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
308 event.paramH = msg->lParam & 0x7FFF;
309 if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
310 HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event );
313 return (msg->hwnd != 0);
317 /***********************************************************************
318 * process_cooked_keyboard_message
320 * returns TRUE if the contents of 'msg' should be passed to the application
322 static BOOL process_cooked_keyboard_message( MSG *msg, BOOL remove )
324 if (remove)
326 update_queue_key_state( msg->message, msg->wParam );
328 /* Handle F1 key by sending out WM_HELP message */
329 if ((msg->message == WM_KEYUP) &&
330 (msg->wParam == VK_F1) &&
331 (msg->hwnd != GetDesktopWindow()) &&
332 !MENU_IsMenuActive())
334 HELPINFO hi;
335 hi.cbSize = sizeof(HELPINFO);
336 hi.iContextType = HELPINFO_WINDOW;
337 hi.iCtrlId = GetWindowLongA( msg->hwnd, GWL_ID );
338 hi.hItemHandle = msg->hwnd;
339 hi.dwContextId = GetWindowContextHelpId( msg->hwnd );
340 hi.MousePos = msg->pt;
341 SendMessageA(msg->hwnd, WM_HELP, 0, (LPARAM)&hi);
345 if (HOOK_CallHooksA( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
346 LOWORD(msg->wParam), msg->lParam ))
348 /* skip this message */
349 HOOK_CallHooksA( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam );
350 return FALSE;
352 return TRUE;
356 /***********************************************************************
357 * process_raw_mouse_message
359 * returns TRUE if the contents of 'msg' should be passed to the application
361 static BOOL process_raw_mouse_message( MSG *msg, ULONG_PTR extra_info )
363 static MSG clk_msg;
365 POINT pt;
366 INT ht, hittest;
368 /* find the window to dispatch this mouse message to */
370 hittest = HTCLIENT;
371 if (!(msg->hwnd = PERQDATA_GetCaptureWnd( &ht )))
373 /* If no capture HWND, find window which contains the mouse position.
374 * Also find the position of the cursor hot spot (hittest) */
375 HWND hWndScope = (HWND)extra_info;
377 if (!IsWindow(hWndScope)) hWndScope = 0;
378 if (!(msg->hwnd = WINPOS_WindowFromPoint( hWndScope, msg->pt, &hittest )))
379 msg->hwnd = GetDesktopWindow();
380 ht = hittest;
383 if (HOOK_IsHooked( WH_JOURNALRECORD ))
385 EVENTMSG event;
386 event.message = msg->message;
387 event.time = msg->time;
388 event.hwnd = msg->hwnd;
389 event.paramL = msg->pt.x;
390 event.paramH = msg->pt.y;
391 HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event );
394 /* translate double clicks */
396 if ((msg->message == WM_LBUTTONDOWN) ||
397 (msg->message == WM_RBUTTONDOWN) ||
398 (msg->message == WM_MBUTTONDOWN))
400 BOOL update = TRUE;
401 /* translate double clicks -
402 * note that ...MOUSEMOVEs can slip in between
403 * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
405 if (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS || ht != HTCLIENT )
407 if ((msg->message == clk_msg.message) &&
408 (msg->hwnd == clk_msg.hwnd) &&
409 (msg->time - clk_msg.time < GetDoubleClickTime()) &&
410 (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
411 (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
413 msg->message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
414 clk_msg.message = 0;
415 update = FALSE;
418 /* update static double click conditions */
419 if (update) clk_msg = *msg;
422 pt = msg->pt;
423 /* Note: windows has no concept of a non-client wheel message */
424 if (hittest != HTCLIENT && msg->message != WM_MOUSEWHEEL)
426 msg->message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
427 msg->wParam = hittest;
429 else ScreenToClient( msg->hwnd, &pt );
430 msg->lParam = MAKELONG( pt.x, pt.y );
431 return TRUE;
435 /***********************************************************************
436 * process_cooked_mouse_message
438 * returns TRUE if the contents of 'msg' should be passed to the application
440 static BOOL process_cooked_mouse_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
442 INT hittest = HTCLIENT;
443 UINT raw_message = msg->message;
444 BOOL eatMsg;
446 if (msg->message >= WM_NCMOUSEFIRST && msg->message <= WM_NCMOUSELAST)
448 raw_message += WM_MOUSEFIRST - WM_NCMOUSEFIRST;
449 hittest = msg->wParam;
451 if (raw_message == WM_LBUTTONDBLCLK ||
452 raw_message == WM_RBUTTONDBLCLK ||
453 raw_message == WM_MBUTTONDBLCLK)
455 raw_message += WM_LBUTTONDOWN - WM_LBUTTONDBLCLK;
458 if (remove) update_queue_key_state( raw_message, 0 );
460 if (HOOK_IsHooked( WH_MOUSE ))
462 MOUSEHOOKSTRUCT hook;
463 hook.pt = msg->pt;
464 hook.hwnd = msg->hwnd;
465 hook.wHitTestCode = hittest;
466 hook.dwExtraInfo = extra_info;
467 if (HOOK_CallHooksA( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
468 msg->message, (LPARAM)&hook ))
470 hook.pt = msg->pt;
471 hook.hwnd = msg->hwnd;
472 hook.wHitTestCode = hittest;
473 hook.dwExtraInfo = extra_info;
474 HOOK_CallHooksA( WH_CBT, HCBT_CLICKSKIPPED, msg->message, (LPARAM)&hook );
475 return FALSE;
479 if ((hittest == HTERROR) || (hittest == HTNOWHERE))
481 SendMessageA( msg->hwnd, WM_SETCURSOR, msg->hwnd, MAKELONG( hittest, raw_message ));
482 return FALSE;
485 if (!remove || GetCapture()) return TRUE;
487 eatMsg = FALSE;
489 if ((raw_message == WM_LBUTTONDOWN) ||
490 (raw_message == WM_RBUTTONDOWN) ||
491 (raw_message == WM_MBUTTONDOWN))
493 HWND hwndTop = GetAncestor( msg->hwnd, GA_ROOT );
495 /* Send the WM_PARENTNOTIFY,
496 * note that even for double/nonclient clicks
497 * notification message is still WM_L/M/RBUTTONDOWN.
499 MSG_SendParentNotify( msg->hwnd, raw_message, 0, msg->pt );
501 /* Activate the window if needed */
503 if (msg->hwnd != GetActiveWindow() && hwndTop != GetDesktopWindow())
505 LONG ret = SendMessageA( msg->hwnd, WM_MOUSEACTIVATE, hwndTop,
506 MAKELONG( hittest, raw_message ) );
508 switch(ret)
510 case MA_NOACTIVATEANDEAT:
511 eatMsg = TRUE;
512 /* fall through */
513 case MA_NOACTIVATE:
514 break;
515 case MA_ACTIVATEANDEAT:
516 eatMsg = TRUE;
517 /* fall through */
518 case MA_ACTIVATE:
519 case 0:
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, ULONG_PTR extra_info, 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, extra_info, 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 * WaitMessage (USER.112) Suspend thread pending messages
655 * WaitMessage (USER32.@) Suspend thread pending messages
657 * WaitMessage() suspends a thread until events appear in the thread's
658 * queue.
660 BOOL WINAPI WaitMessage(void)
662 return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
666 /***********************************************************************
667 * MsgWaitForMultipleObjectsEx (USER32.@)
669 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
670 DWORD timeout, DWORD mask, DWORD flags )
672 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
673 DWORD i, ret;
674 MESSAGEQUEUE *msgQueue;
676 if (count > MAXIMUM_WAIT_OBJECTS-1)
678 SetLastError( ERROR_INVALID_PARAMETER );
679 return WAIT_FAILED;
682 if (!(msgQueue = QUEUE_Current())) return WAIT_FAILED;
684 /* set the queue mask */
685 SERVER_START_REQ( set_queue_mask )
687 req->wake_mask = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
688 req->changed_mask = mask;
689 req->skip_wait = 0;
690 wine_server_call( req );
692 SERVER_END_REQ;
694 /* Add the thread event to the handle list */
695 for (i = 0; i < count; i++) handles[i] = pHandles[i];
696 handles[count] = msgQueue->server_queue;
699 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
701 ret = USER_Driver.pMsgWaitForMultipleObjectsEx( count+1, handles, timeout, mask, flags );
702 if (ret == count+1) ret = count; /* pretend the msg queue is ready */
704 else
705 ret = WaitForMultipleObjectsEx( count+1, handles, flags & MWMO_WAITALL,
706 timeout, flags & MWMO_ALERTABLE );
707 return ret;
711 /***********************************************************************
712 * MsgWaitForMultipleObjects (USER32.@)
714 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
715 BOOL wait_all, DWORD timeout, DWORD mask )
717 return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
718 wait_all ? MWMO_WAITALL : 0 );
722 /***********************************************************************
723 * WaitForInputIdle (USER32.@)
725 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
727 DWORD start_time, elapsed, ret;
728 HANDLE idle_event = -1;
730 SERVER_START_REQ( wait_input_idle )
732 req->handle = hProcess;
733 req->timeout = dwTimeOut;
734 if (!(ret = wine_server_call_err( req ))) idle_event = reply->event;
736 SERVER_END_REQ;
737 if (ret) return WAIT_FAILED; /* error */
738 if (!idle_event) return 0; /* no event to wait on */
740 start_time = GetTickCount();
741 elapsed = 0;
743 TRACE("waiting for %x\n", idle_event );
746 ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
747 switch (ret)
749 case WAIT_OBJECT_0+1:
750 process_sent_messages();
751 break;
752 case WAIT_TIMEOUT:
753 case WAIT_FAILED:
754 TRACE("timeout or error\n");
755 return ret;
756 default:
757 TRACE("finished\n");
758 return 0;
760 if (dwTimeOut != INFINITE)
762 elapsed = GetTickCount() - start_time;
763 if (elapsed > dwTimeOut)
764 break;
767 while (1);
769 return WAIT_TIMEOUT;
773 /***********************************************************************
774 * UserYield (USER.332)
775 * UserYield16 (USER32.@)
777 void WINAPI UserYield16(void)
779 DWORD count;
781 /* Handle sent messages */
782 process_sent_messages();
784 /* Yield */
785 ReleaseThunkLock(&count);
786 if (count)
788 RestoreThunkLock(count);
789 /* Handle sent messages again */
790 process_sent_messages();
795 struct accent_char
797 BYTE ac_accent;
798 BYTE ac_char;
799 BYTE ac_result;
802 static const struct accent_char accent_chars[] =
804 /* A good idea should be to read /usr/X11/lib/X11/locale/iso8859-x/Compose */
805 {'`', 'A', '\300'}, {'`', 'a', '\340'},
806 {'\'', 'A', '\301'}, {'\'', 'a', '\341'},
807 {'^', 'A', '\302'}, {'^', 'a', '\342'},
808 {'~', 'A', '\303'}, {'~', 'a', '\343'},
809 {'"', 'A', '\304'}, {'"', 'a', '\344'},
810 {'O', 'A', '\305'}, {'o', 'a', '\345'},
811 {'0', 'A', '\305'}, {'0', 'a', '\345'},
812 {'A', 'A', '\305'}, {'a', 'a', '\345'},
813 {'A', 'E', '\306'}, {'a', 'e', '\346'},
814 {',', 'C', '\307'}, {',', 'c', '\347'},
815 {'`', 'E', '\310'}, {'`', 'e', '\350'},
816 {'\'', 'E', '\311'}, {'\'', 'e', '\351'},
817 {'^', 'E', '\312'}, {'^', 'e', '\352'},
818 {'"', 'E', '\313'}, {'"', 'e', '\353'},
819 {'`', 'I', '\314'}, {'`', 'i', '\354'},
820 {'\'', 'I', '\315'}, {'\'', 'i', '\355'},
821 {'^', 'I', '\316'}, {'^', 'i', '\356'},
822 {'"', 'I', '\317'}, {'"', 'i', '\357'},
823 {'-', 'D', '\320'}, {'-', 'd', '\360'},
824 {'~', 'N', '\321'}, {'~', 'n', '\361'},
825 {'`', 'O', '\322'}, {'`', 'o', '\362'},
826 {'\'', 'O', '\323'}, {'\'', 'o', '\363'},
827 {'^', 'O', '\324'}, {'^', 'o', '\364'},
828 {'~', 'O', '\325'}, {'~', 'o', '\365'},
829 {'"', 'O', '\326'}, {'"', 'o', '\366'},
830 {'/', 'O', '\330'}, {'/', 'o', '\370'},
831 {'`', 'U', '\331'}, {'`', 'u', '\371'},
832 {'\'', 'U', '\332'}, {'\'', 'u', '\372'},
833 {'^', 'U', '\333'}, {'^', 'u', '\373'},
834 {'"', 'U', '\334'}, {'"', 'u', '\374'},
835 {'\'', 'Y', '\335'}, {'\'', 'y', '\375'},
836 {'T', 'H', '\336'}, {'t', 'h', '\376'},
837 {'s', 's', '\337'}, {'"', 'y', '\377'},
838 {'s', 'z', '\337'}, {'i', 'j', '\377'},
839 /* iso-8859-2 uses this */
840 {'<', 'L', '\245'}, {'<', 'l', '\265'}, /* caron */
841 {'<', 'S', '\251'}, {'<', 's', '\271'},
842 {'<', 'T', '\253'}, {'<', 't', '\273'},
843 {'<', 'Z', '\256'}, {'<', 'z', '\276'},
844 {'<', 'C', '\310'}, {'<', 'c', '\350'},
845 {'<', 'E', '\314'}, {'<', 'e', '\354'},
846 {'<', 'D', '\317'}, {'<', 'd', '\357'},
847 {'<', 'N', '\322'}, {'<', 'n', '\362'},
848 {'<', 'R', '\330'}, {'<', 'r', '\370'},
849 {';', 'A', '\241'}, {';', 'a', '\261'}, /* ogonek */
850 {';', 'E', '\312'}, {';', 'e', '\332'},
851 {'\'', 'Z', '\254'}, {'\'', 'z', '\274'}, /* acute */
852 {'\'', 'R', '\300'}, {'\'', 'r', '\340'},
853 {'\'', 'L', '\305'}, {'\'', 'l', '\345'},
854 {'\'', 'C', '\306'}, {'\'', 'c', '\346'},
855 {'\'', 'N', '\321'}, {'\'', 'n', '\361'},
856 /* collision whith S, from iso-8859-9 !!! */
857 {',', 'S', '\252'}, {',', 's', '\272'}, /* cedilla */
858 {',', 'T', '\336'}, {',', 't', '\376'},
859 {'.', 'Z', '\257'}, {'.', 'z', '\277'}, /* dot above */
860 {'/', 'L', '\243'}, {'/', 'l', '\263'}, /* slash */
861 {'/', 'D', '\320'}, {'/', 'd', '\360'},
862 {'(', 'A', '\303'}, {'(', 'a', '\343'}, /* breve */
863 {'\275', 'O', '\325'}, {'\275', 'o', '\365'}, /* double acute */
864 {'\275', 'U', '\334'}, {'\275', 'u', '\374'},
865 {'0', 'U', '\332'}, {'0', 'u', '\372'}, /* ring above */
866 /* iso-8859-3 uses this */
867 {'/', 'H', '\241'}, {'/', 'h', '\261'}, /* slash */
868 {'>', 'H', '\246'}, {'>', 'h', '\266'}, /* circumflex */
869 {'>', 'J', '\254'}, {'>', 'j', '\274'},
870 {'>', 'C', '\306'}, {'>', 'c', '\346'},
871 {'>', 'G', '\330'}, {'>', 'g', '\370'},
872 {'>', 'S', '\336'}, {'>', 's', '\376'},
873 /* collision whith G( from iso-8859-9 !!! */
874 {'(', 'G', '\253'}, {'(', 'g', '\273'}, /* breve */
875 {'(', 'U', '\335'}, {'(', 'u', '\375'},
876 /* collision whith I. from iso-8859-3 !!! */
877 {'.', 'I', '\251'}, {'.', 'i', '\271'}, /* dot above */
878 {'.', 'C', '\305'}, {'.', 'c', '\345'},
879 {'.', 'G', '\325'}, {'.', 'g', '\365'},
880 /* iso-8859-4 uses this */
881 {',', 'R', '\243'}, {',', 'r', '\263'}, /* cedilla */
882 {',', 'L', '\246'}, {',', 'l', '\266'},
883 {',', 'G', '\253'}, {',', 'g', '\273'},
884 {',', 'N', '\321'}, {',', 'n', '\361'},
885 {',', 'K', '\323'}, {',', 'k', '\363'},
886 {'~', 'I', '\245'}, {'~', 'i', '\265'}, /* tilde */
887 {'-', 'E', '\252'}, {'-', 'e', '\272'}, /* macron */
888 {'-', 'A', '\300'}, {'-', 'a', '\340'},
889 {'-', 'I', '\317'}, {'-', 'i', '\357'},
890 {'-', 'O', '\322'}, {'-', 'o', '\362'},
891 {'-', 'U', '\336'}, {'-', 'u', '\376'},
892 {'/', 'T', '\254'}, {'/', 't', '\274'}, /* slash */
893 {'.', 'E', '\314'}, {'.', 'e', '\344'}, /* dot above */
894 {';', 'I', '\307'}, {';', 'i', '\347'}, /* ogonek */
895 {';', 'U', '\331'}, {';', 'u', '\371'},
896 /* iso-8859-9 uses this */
897 /* iso-8859-9 has really bad choosen G( S, and I. as they collide
898 * whith the same letters on other iso-8859-x (that is they are on
899 * different places :-( ), if you use turkish uncomment these and
900 * comment out the lines in iso-8859-2 and iso-8859-3 sections
901 * FIXME: should be dynamic according to chosen language
902 * if/when Wine has turkish support.
904 /* collision whith G( from iso-8859-3 !!! */
905 /* {'(', 'G', '\320'}, {'(', 'g', '\360'}, */ /* breve */
906 /* collision whith S, from iso-8859-2 !!! */
907 /* {',', 'S', '\336'}, {',', 's', '\376'}, */ /* cedilla */
908 /* collision whith I. from iso-8859-3 !!! */
909 /* {'.', 'I', '\335'}, {'.', 'i', '\375'}, */ /* dot above */
913 /***********************************************************************
914 * TranslateMessage (USER32.@)
916 * Implementation of TranslateMessage.
918 * TranslateMessage translates virtual-key messages into character-messages,
919 * as follows :
920 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
921 * ditto replacing WM_* with WM_SYS*
922 * This produces WM_CHAR messages only for keys mapped to ASCII characters
923 * by the keyboard driver.
925 BOOL WINAPI TranslateMessage( const MSG *msg )
927 static int dead_char;
928 UINT message;
929 WCHAR wp[2];
931 if (msg->message >= WM_KEYFIRST && msg->message <= WM_KEYLAST)
932 TRACE_(key)("(%s, %04X, %08lX)\n",
933 SPY_GetMsgName(msg->message, msg->hwnd), msg->wParam, msg->lParam );
935 if ((msg->message != WM_KEYDOWN) && (msg->message != WM_SYSKEYDOWN)) return FALSE;
937 TRACE_(key)("Translating key %s (%04x), scancode %02x\n",
938 SPY_GetVKeyName(msg->wParam), msg->wParam, LOBYTE(HIWORD(msg->lParam)));
940 /* FIXME : should handle ToUnicode yielding 2 */
941 switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), QueueKeyStateTable, wp, 2, 0))
943 case 1:
944 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
945 /* Should dead chars handling go in ToAscii ? */
946 if (dead_char)
948 int i;
950 if (wp[0] == ' ') wp[0] = dead_char;
951 if (dead_char == 0xa2) dead_char = '(';
952 else if (dead_char == 0xa8) dead_char = '"';
953 else if (dead_char == 0xb2) dead_char = ';';
954 else if (dead_char == 0xb4) dead_char = '\'';
955 else if (dead_char == 0xb7) dead_char = '<';
956 else if (dead_char == 0xb8) dead_char = ',';
957 else if (dead_char == 0xff) dead_char = '.';
958 for (i = 0; i < sizeof(accent_chars)/sizeof(accent_chars[0]); i++)
959 if ((accent_chars[i].ac_accent == dead_char) &&
960 (accent_chars[i].ac_char == wp[0]))
962 wp[0] = accent_chars[i].ac_result;
963 break;
965 dead_char = 0;
967 TRACE_(key)("1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
968 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
969 return TRUE;
971 case -1:
972 message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
973 dead_char = wp[0];
974 TRACE_(key)("-1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
975 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
976 return TRUE;
978 return FALSE;
982 /***********************************************************************
983 * DispatchMessageA (USER32.@)
985 LONG WINAPI DispatchMessageA( const MSG* msg )
987 WND * wndPtr;
988 LONG retval;
989 int painting;
990 WNDPROC winproc;
992 /* Process timer messages */
993 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
995 if (msg->lParam)
997 /* HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
999 /* before calling window proc, verify whether timer is still valid;
1000 there's a slim chance that the application kills the timer
1001 between GetMessage and DispatchMessage API calls */
1002 if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (HWINDOWPROC) msg->lParam))
1003 return 0; /* invalid winproc */
1005 return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
1006 msg->message, msg->wParam, GetTickCount() );
1010 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
1012 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1013 return 0;
1015 if (wndPtr == WND_OTHER_PROCESS)
1017 if (IsWindow( msg->hwnd ))
1018 ERR( "cannot dispatch msg to other process window %x\n", msg->hwnd );
1019 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1020 return 0;
1022 if (!(winproc = wndPtr->winproc))
1024 WIN_ReleasePtr( wndPtr );
1025 return 0;
1027 painting = (msg->message == WM_PAINT);
1028 if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1029 WIN_ReleasePtr( wndPtr );
1030 /* hook_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1032 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1033 msg->wParam, msg->lParam );
1034 retval = CallWindowProcA( winproc, msg->hwnd, msg->message,
1035 msg->wParam, msg->lParam );
1036 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1037 msg->wParam, msg->lParam );
1039 if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
1041 BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
1042 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1043 WIN_ReleasePtr( wndPtr );
1044 if (validate)
1046 ERR( "BeginPaint not called on WM_PAINT for hwnd %04x!\n", msg->hwnd );
1047 /* Validate the update region to avoid infinite WM_PAINT loop */
1048 RedrawWindow( msg->hwnd, NULL, 0,
1049 RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1052 return retval;
1056 /***********************************************************************
1057 * DispatchMessageW (USER32.@) Process Message
1059 * Process the message specified in the structure *_msg_.
1061 * If the lpMsg parameter points to a WM_TIMER message and the
1062 * parameter of the WM_TIMER message is not NULL, the lParam parameter
1063 * points to the function that is called instead of the window
1064 * procedure.
1066 * The message must be valid.
1068 * RETURNS
1070 * DispatchMessage() returns the result of the window procedure invoked.
1072 * CONFORMANCE
1074 * ECMA-234, Win32
1077 LONG WINAPI DispatchMessageW( const MSG* msg )
1079 WND * wndPtr;
1080 LONG retval;
1081 int painting;
1082 WNDPROC winproc;
1084 /* Process timer messages */
1085 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1087 if (msg->lParam)
1089 /* HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1091 /* before calling window proc, verify whether timer is still valid;
1092 there's a slim chance that the application kills the timer
1093 between GetMessage and DispatchMessage API calls */
1094 if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (HWINDOWPROC) msg->lParam))
1095 return 0; /* invalid winproc */
1097 return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
1098 msg->message, msg->wParam, GetTickCount() );
1102 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
1104 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1105 return 0;
1107 if (wndPtr == WND_OTHER_PROCESS)
1109 if (IsWindow( msg->hwnd ))
1110 ERR( "cannot dispatch msg to other process window %x\n", msg->hwnd );
1111 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1112 return 0;
1114 if (!(winproc = wndPtr->winproc))
1116 WIN_ReleasePtr( wndPtr );
1117 return 0;
1119 painting = (msg->message == WM_PAINT);
1120 if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1121 WIN_ReleasePtr( wndPtr );
1122 /* HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1124 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1125 msg->wParam, msg->lParam );
1126 retval = CallWindowProcW( winproc, msg->hwnd, msg->message,
1127 msg->wParam, msg->lParam );
1128 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1129 msg->wParam, msg->lParam );
1131 if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
1133 BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
1134 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1135 WIN_ReleasePtr( wndPtr );
1136 if (validate)
1138 ERR( "BeginPaint not called on WM_PAINT for hwnd %04x!\n", msg->hwnd );
1139 /* Validate the update region to avoid infinite WM_PAINT loop */
1140 RedrawWindow( msg->hwnd, NULL, 0,
1141 RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1144 return retval;
1148 /***********************************************************************
1149 * RegisterWindowMessage (USER.118)
1150 * RegisterWindowMessageA (USER32.@)
1152 WORD WINAPI RegisterWindowMessageA( LPCSTR str )
1154 TRACE("%s\n", str );
1155 return GlobalAddAtomA( str );
1159 /***********************************************************************
1160 * RegisterWindowMessageW (USER32.@)
1162 WORD WINAPI RegisterWindowMessageW( LPCWSTR str )
1164 TRACE("%p\n", str );
1165 return GlobalAddAtomW( str );
1169 /***********************************************************************
1170 * BroadcastSystemMessage (USER32.@)
1172 LONG WINAPI BroadcastSystemMessage(
1173 DWORD dwFlags,LPDWORD recipients,UINT uMessage,WPARAM wParam,
1174 LPARAM lParam
1176 FIXME("(%08lx,%08lx,%08x,%08x,%08lx): stub!\n",
1177 dwFlags,*recipients,uMessage,wParam,lParam
1179 return 0;