Authors: David Hammerton <david@transgaming.com>, Peter Hunnisett <peter@transgaming...
[wine.git] / windows / message.c
blobdc8b530e91d95b35c3ee004cb2468326b8abbb96
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 <stdlib.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <sys/time.h>
25 #include <sys/types.h>
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "message.h"
31 #include "winerror.h"
32 #include "wine/server.h"
33 #include "win.h"
34 #include "heap.h"
35 #include "hook.h"
36 #include "input.h"
37 #include "spy.h"
38 #include "winpos.h"
39 #include "dde.h"
40 #include "queue.h"
41 #include "winproc.h"
42 #include "user.h"
43 #include "thread.h"
44 #include "task.h"
45 #include "controls.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msg);
49 WINE_DECLARE_DEBUG_CHANNEL(key);
51 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
52 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
54 static BYTE QueueKeyStateTable[256];
57 /***********************************************************************
58 * is_keyboard_message
60 inline static BOOL is_keyboard_message( UINT message )
62 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
66 /***********************************************************************
67 * is_mouse_message
69 inline static BOOL is_mouse_message( UINT message )
71 return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
72 (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
76 /***********************************************************************
77 * check_message_filter
79 inline static BOOL check_message_filter( const MSG *msg, HWND hwnd, UINT first, UINT last )
81 if (hwnd)
83 if (msg->hwnd != hwnd && !IsChild( hwnd, msg->hwnd )) return FALSE;
85 if (first || last)
87 return (msg->message >= first && msg->message <= last);
89 return TRUE;
93 /***********************************************************************
94 * process_sent_messages
96 * Process all pending sent messages.
98 inline static void process_sent_messages(void)
100 MSG msg;
101 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
105 /***********************************************************************
106 * queue_hardware_message
108 * store a hardware message in the thread queue
110 static void queue_hardware_message( MSG *msg, ULONG_PTR extra_info, enum message_type type )
112 SERVER_START_REQ( send_message )
114 req->type = type;
115 req->id = (void *)GetWindowThreadProcessId( msg->hwnd, NULL );
116 req->win = msg->hwnd;
117 req->msg = msg->message;
118 req->wparam = msg->wParam;
119 req->lparam = msg->lParam;
120 req->x = msg->pt.x;
121 req->y = msg->pt.y;
122 req->time = msg->time;
123 req->info = extra_info;
124 req->timeout = 0;
125 wine_server_call( req );
127 SERVER_END_REQ;
131 /***********************************************************************
132 * update_queue_key_state
134 static void update_queue_key_state( UINT msg, WPARAM wp )
136 BOOL down = FALSE;
138 switch (msg)
140 case WM_LBUTTONDOWN:
141 down = TRUE;
142 /* fall through */
143 case WM_LBUTTONUP:
144 wp = VK_LBUTTON;
145 break;
146 case WM_MBUTTONDOWN:
147 down = TRUE;
148 /* fall through */
149 case WM_MBUTTONUP:
150 wp = VK_MBUTTON;
151 break;
152 case WM_RBUTTONDOWN:
153 down = TRUE;
154 /* fall through */
155 case WM_RBUTTONUP:
156 wp = VK_RBUTTON;
157 break;
158 case WM_KEYDOWN:
159 case WM_SYSKEYDOWN:
160 down = TRUE;
161 /* fall through */
162 case WM_KEYUP:
163 case WM_SYSKEYUP:
164 wp = wp & 0xff;
165 break;
167 if (down)
169 BYTE *p = &QueueKeyStateTable[wp];
170 if (!(*p & 0x80)) *p ^= 0x01;
171 *p |= 0x80;
173 else QueueKeyStateTable[wp] &= ~0x80;
177 /***********************************************************************
178 * MSG_SendParentNotify
180 * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
181 * the window has the WS_EX_NOPARENTNOTIFY style.
183 static void MSG_SendParentNotify( HWND hwnd, WORD event, WORD idChild, POINT pt )
185 /* pt has to be in the client coordinates of the parent window */
186 MapWindowPoints( 0, hwnd, &pt, 1 );
187 for (;;)
189 HWND parent;
191 if (!(GetWindowLongA( hwnd, GWL_STYLE ) & WS_CHILD)) break;
192 if (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
193 if (!(parent = GetParent(hwnd))) break;
194 MapWindowPoints( hwnd, parent, &pt, 1 );
195 hwnd = parent;
196 SendMessageA( hwnd, WM_PARENTNOTIFY,
197 MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
202 /***********************************************************************
203 * MSG_JournalPlayBackMsg
205 * Get an EVENTMSG struct via call JOURNALPLAYBACK hook function
207 void MSG_JournalPlayBackMsg(void)
209 EVENTMSG tmpMsg;
210 MSG msg;
211 LRESULT wtime;
212 int keyDown,i;
214 if (!HOOK_IsHooked( WH_JOURNALPLAYBACK )) return;
216 wtime=HOOK_CallHooksA( WH_JOURNALPLAYBACK, HC_GETNEXT, 0, (LPARAM)&tmpMsg );
217 /* TRACE(msg,"Playback wait time =%ld\n",wtime); */
218 if (wtime<=0)
220 wtime=0;
221 msg.message = tmpMsg.message;
222 msg.hwnd = tmpMsg.hwnd;
223 msg.time = tmpMsg.time;
224 if ((tmpMsg.message >= WM_KEYFIRST) && (tmpMsg.message <= WM_KEYLAST))
226 msg.wParam = tmpMsg.paramL & 0xFF;
227 msg.lParam = MAKELONG(tmpMsg.paramH&0x7ffff,tmpMsg.paramL>>8);
228 if (tmpMsg.message == WM_KEYDOWN || tmpMsg.message == WM_SYSKEYDOWN)
230 for (keyDown=i=0; i<256 && !keyDown; i++)
231 if (InputKeyStateTable[i] & 0x80)
232 keyDown++;
233 if (!keyDown)
234 msg.lParam |= 0x40000000;
235 InputKeyStateTable[msg.wParam] |= 0x80;
236 AsyncKeyStateTable[msg.wParam] |= 0x80;
238 else /* WM_KEYUP, WM_SYSKEYUP */
240 msg.lParam |= 0xC0000000;
241 InputKeyStateTable[msg.wParam] &= ~0x80;
243 if (InputKeyStateTable[VK_MENU] & 0x80)
244 msg.lParam |= 0x20000000;
245 if (tmpMsg.paramH & 0x8000) /*special_key bit*/
246 msg.lParam |= 0x01000000;
248 msg.pt.x = msg.pt.y = 0;
249 queue_hardware_message( &msg, 0, MSG_HARDWARE_RAW );
251 else if ((tmpMsg.message>= WM_MOUSEFIRST) && (tmpMsg.message <= WM_MOUSELAST))
253 switch (tmpMsg.message)
255 case WM_LBUTTONDOWN:
256 InputKeyStateTable[VK_LBUTTON] |= 0x80;
257 AsyncKeyStateTable[VK_LBUTTON] |= 0x80;
258 break;
259 case WM_LBUTTONUP:
260 InputKeyStateTable[VK_LBUTTON] &= ~0x80;
261 break;
262 case WM_MBUTTONDOWN:
263 InputKeyStateTable[VK_MBUTTON] |= 0x80;
264 AsyncKeyStateTable[VK_MBUTTON] |= 0x80;
265 break;
266 case WM_MBUTTONUP:
267 InputKeyStateTable[VK_MBUTTON] &= ~0x80;
268 break;
269 case WM_RBUTTONDOWN:
270 InputKeyStateTable[VK_RBUTTON] |= 0x80;
271 AsyncKeyStateTable[VK_RBUTTON] |= 0x80;
272 break;
273 case WM_RBUTTONUP:
274 InputKeyStateTable[VK_RBUTTON] &= ~0x80;
275 break;
277 SetCursorPos(tmpMsg.paramL,tmpMsg.paramH);
278 msg.lParam=MAKELONG(tmpMsg.paramL,tmpMsg.paramH);
279 msg.wParam=0;
280 if (InputKeyStateTable[VK_LBUTTON] & 0x80) msg.wParam |= MK_LBUTTON;
281 if (InputKeyStateTable[VK_MBUTTON] & 0x80) msg.wParam |= MK_MBUTTON;
282 if (InputKeyStateTable[VK_RBUTTON] & 0x80) msg.wParam |= MK_RBUTTON;
284 msg.pt.x = tmpMsg.paramL;
285 msg.pt.y = tmpMsg.paramH;
286 queue_hardware_message( &msg, 0, MSG_HARDWARE_RAW );
288 HOOK_CallHooksA( WH_JOURNALPLAYBACK, HC_SKIP, 0, (LPARAM)&tmpMsg);
290 else
292 if( tmpMsg.message == WM_QUEUESYNC )
293 if (HOOK_IsHooked( WH_CBT ))
294 HOOK_CallHooksA( WH_CBT, HCBT_QS, 0, 0L);
299 /***********************************************************************
300 * process_raw_keyboard_message
302 * returns TRUE if the contents of 'msg' should be passed to the application
304 static BOOL process_raw_keyboard_message( MSG *msg, ULONG_PTR extra_info )
306 if (!(msg->hwnd = GetFocus()))
308 /* Send the message to the active window instead, */
309 /* translating messages to their WM_SYS equivalent */
310 msg->hwnd = GetActiveWindow();
311 if (msg->message < WM_SYSKEYDOWN) msg->message += WM_SYSKEYDOWN - WM_KEYDOWN;
314 if (HOOK_IsHooked( WH_JOURNALRECORD ))
316 EVENTMSG event;
318 event.message = msg->message;
319 event.hwnd = msg->hwnd;
320 event.time = msg->time;
321 event.paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
322 event.paramH = msg->lParam & 0x7FFF;
323 if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
324 HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event );
327 /* if we are going to throw away the message, update the queue state now */
328 if (!msg->hwnd) update_queue_key_state( msg->message, msg->wParam );
330 return (msg->hwnd != 0);
334 /***********************************************************************
335 * process_cooked_keyboard_message
337 * returns TRUE if the contents of 'msg' should be passed to the application
339 static BOOL process_cooked_keyboard_message( MSG *msg, BOOL remove )
341 if (remove)
343 update_queue_key_state( msg->message, msg->wParam );
345 /* Handle F1 key by sending out WM_HELP message */
346 if ((msg->message == WM_KEYUP) &&
347 (msg->wParam == VK_F1) &&
348 (msg->hwnd != GetDesktopWindow()) &&
349 !MENU_IsMenuActive())
351 HELPINFO hi;
352 hi.cbSize = sizeof(HELPINFO);
353 hi.iContextType = HELPINFO_WINDOW;
354 hi.iCtrlId = GetWindowLongA( msg->hwnd, GWL_ID );
355 hi.hItemHandle = msg->hwnd;
356 hi.dwContextId = GetWindowContextHelpId( msg->hwnd );
357 hi.MousePos = msg->pt;
358 SendMessageA(msg->hwnd, WM_HELP, 0, (LPARAM)&hi);
362 if (HOOK_CallHooksA( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
363 LOWORD(msg->wParam), msg->lParam ))
365 /* skip this message */
366 HOOK_CallHooksA( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam );
367 return FALSE;
369 return TRUE;
373 /***********************************************************************
374 * process_raw_mouse_message
376 * returns TRUE if the contents of 'msg' should be passed to the application
378 static BOOL process_raw_mouse_message( MSG *msg, ULONG_PTR extra_info )
380 static MSG clk_msg;
382 POINT pt;
383 INT ht, hittest;
385 /* find the window to dispatch this mouse message to */
387 hittest = HTCLIENT;
388 if (!(msg->hwnd = PERQDATA_GetCaptureWnd( &ht )))
390 /* If no capture HWND, find window which contains the mouse position.
391 * Also find the position of the cursor hot spot (hittest) */
392 HWND hWndScope = (HWND)extra_info;
394 if (!IsWindow(hWndScope)) hWndScope = 0;
395 if (!(msg->hwnd = WINPOS_WindowFromPoint( hWndScope, msg->pt, &hittest )))
396 msg->hwnd = GetDesktopWindow();
397 ht = hittest;
400 if (HOOK_IsHooked( WH_JOURNALRECORD ))
402 EVENTMSG event;
403 event.message = msg->message;
404 event.time = msg->time;
405 event.hwnd = msg->hwnd;
406 event.paramL = msg->pt.x;
407 event.paramH = msg->pt.y;
408 HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event );
411 /* translate double clicks */
413 if ((msg->message == WM_LBUTTONDOWN) ||
414 (msg->message == WM_RBUTTONDOWN) ||
415 (msg->message == WM_MBUTTONDOWN))
417 BOOL update = TRUE;
418 /* translate double clicks -
419 * note that ...MOUSEMOVEs can slip in between
420 * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
422 if (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS || ht != HTCLIENT )
424 if ((msg->message == clk_msg.message) &&
425 (msg->hwnd == clk_msg.hwnd) &&
426 (msg->time - clk_msg.time < GetDoubleClickTime()) &&
427 (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
428 (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
430 msg->message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
431 clk_msg.message = 0;
432 update = FALSE;
435 /* update static double click conditions */
436 if (update) clk_msg = *msg;
439 pt = msg->pt;
440 /* Note: windows has no concept of a non-client wheel message */
441 if (hittest != HTCLIENT && msg->message != WM_MOUSEWHEEL)
443 msg->message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
444 msg->wParam = hittest;
446 else ScreenToClient( msg->hwnd, &pt );
447 msg->lParam = MAKELONG( pt.x, pt.y );
448 return TRUE;
452 /***********************************************************************
453 * process_cooked_mouse_message
455 * returns TRUE if the contents of 'msg' should be passed to the application
457 static BOOL process_cooked_mouse_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
459 INT hittest = HTCLIENT;
460 UINT raw_message = msg->message;
461 BOOL eatMsg;
463 if (msg->message >= WM_NCMOUSEFIRST && msg->message <= WM_NCMOUSELAST)
465 raw_message += WM_MOUSEFIRST - WM_NCMOUSEFIRST;
466 hittest = msg->wParam;
468 if (raw_message == WM_LBUTTONDBLCLK ||
469 raw_message == WM_RBUTTONDBLCLK ||
470 raw_message == WM_MBUTTONDBLCLK)
472 raw_message += WM_LBUTTONDOWN - WM_LBUTTONDBLCLK;
475 if (remove) update_queue_key_state( raw_message, 0 );
477 if (HOOK_IsHooked( WH_MOUSE ))
479 MOUSEHOOKSTRUCT hook;
480 hook.pt = msg->pt;
481 hook.hwnd = msg->hwnd;
482 hook.wHitTestCode = hittest;
483 hook.dwExtraInfo = extra_info;
484 if (HOOK_CallHooksA( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
485 msg->message, (LPARAM)&hook ))
487 hook.pt = msg->pt;
488 hook.hwnd = msg->hwnd;
489 hook.wHitTestCode = hittest;
490 hook.dwExtraInfo = extra_info;
491 HOOK_CallHooksA( WH_CBT, HCBT_CLICKSKIPPED, msg->message, (LPARAM)&hook );
492 return FALSE;
496 if ((hittest == HTERROR) || (hittest == HTNOWHERE))
498 SendMessageA( msg->hwnd, WM_SETCURSOR, msg->hwnd, MAKELONG( hittest, raw_message ));
499 return FALSE;
502 if (!remove || GetCapture()) return TRUE;
504 eatMsg = FALSE;
506 if ((raw_message == WM_LBUTTONDOWN) ||
507 (raw_message == WM_RBUTTONDOWN) ||
508 (raw_message == WM_MBUTTONDOWN))
510 HWND hwndTop = GetAncestor( msg->hwnd, GA_ROOT );
512 /* Send the WM_PARENTNOTIFY,
513 * note that even for double/nonclient clicks
514 * notification message is still WM_L/M/RBUTTONDOWN.
516 MSG_SendParentNotify( msg->hwnd, raw_message, 0, msg->pt );
518 /* Activate the window if needed */
520 if (msg->hwnd != GetActiveWindow() && hwndTop != GetDesktopWindow())
522 LONG ret = SendMessageA( msg->hwnd, WM_MOUSEACTIVATE, hwndTop,
523 MAKELONG( hittest, raw_message ) );
525 switch(ret)
527 case MA_NOACTIVATEANDEAT:
528 eatMsg = TRUE;
529 /* fall through */
530 case MA_NOACTIVATE:
531 break;
532 case MA_ACTIVATEANDEAT:
533 eatMsg = TRUE;
534 /* fall through */
535 case MA_ACTIVATE:
536 case 0:
537 if (hwndTop != GetForegroundWindow() )
539 if (!WINPOS_SetActiveWindow( hwndTop, TRUE , TRUE ))
540 eatMsg = TRUE;
542 break;
543 default:
544 WARN( "unknown WM_MOUSEACTIVATE code %ld\n", ret );
545 break;
550 /* send the WM_SETCURSOR message */
552 /* Windows sends the normal mouse message as the message parameter
553 in the WM_SETCURSOR message even if it's non-client mouse message */
554 SendMessageA( msg->hwnd, WM_SETCURSOR, msg->hwnd, MAKELONG( hittest, raw_message ));
556 return !eatMsg;
560 /***********************************************************************
561 * process_hardware_message
563 * returns TRUE if the contents of 'msg' should be passed to the application
565 BOOL MSG_process_raw_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd_filter,
566 UINT first, UINT last, BOOL remove )
568 if (is_keyboard_message( msg->message ))
570 if (!process_raw_keyboard_message( msg, extra_info )) return FALSE;
572 else if (is_mouse_message( msg->message ))
574 if (!process_raw_mouse_message( msg, extra_info )) return FALSE;
576 else
578 ERR( "unknown message type %x\n", msg->message );
579 return FALSE;
582 /* check destination thread and filters */
583 if (!check_message_filter( msg, hwnd_filter, first, last ) ||
584 !WIN_IsCurrentThread( msg->hwnd ))
586 /* queue it for later, or for another thread */
587 queue_hardware_message( msg, extra_info, MSG_HARDWARE_COOKED );
588 return FALSE;
591 /* save the message in the cooked queue if we didn't want to remove it */
592 if (!remove) queue_hardware_message( msg, extra_info, MSG_HARDWARE_COOKED );
593 return TRUE;
597 /***********************************************************************
598 * MSG_process_cooked_hardware_message
600 * returns TRUE if the contents of 'msg' should be passed to the application
602 BOOL MSG_process_cooked_hardware_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
604 if (is_keyboard_message( msg->message ))
605 return process_cooked_keyboard_message( msg, remove );
607 if (is_mouse_message( msg->message ))
608 return process_cooked_mouse_message( msg, extra_info, remove );
610 ERR( "unknown message type %x\n", msg->message );
611 return FALSE;
615 /**********************************************************************
616 * GetKeyState (USER.106)
618 INT16 WINAPI GetKeyState16(INT16 vkey)
620 return GetKeyState(vkey);
624 /**********************************************************************
625 * GetKeyState (USER32.@)
627 * An application calls the GetKeyState function in response to a
628 * keyboard-input message. This function retrieves the state of the key
629 * at the time the input message was generated. (SDK 3.1 Vol 2. p 390)
631 SHORT WINAPI GetKeyState(INT vkey)
633 INT retval;
635 if (vkey >= 'a' && vkey <= 'z') vkey += 'A' - 'a';
636 retval = ((WORD)(QueueKeyStateTable[vkey] & 0x80) << 8 ) |
637 (QueueKeyStateTable[vkey] & 0x80) |
638 (QueueKeyStateTable[vkey] & 0x01);
639 TRACE("key (0x%x) -> %x\n", vkey, retval);
640 return retval;
644 /**********************************************************************
645 * GetKeyboardState (USER.222)
646 * GetKeyboardState (USER32.@)
648 * An application calls the GetKeyboardState function in response to a
649 * keyboard-input message. This function retrieves the state of the keyboard
650 * at the time the input message was generated. (SDK 3.1 Vol 2. p 387)
652 BOOL WINAPI GetKeyboardState(LPBYTE lpKeyState)
654 TRACE_(key)("(%p)\n", lpKeyState);
655 if (lpKeyState) memcpy(lpKeyState, QueueKeyStateTable, 256);
656 return TRUE;
660 /**********************************************************************
661 * SetKeyboardState (USER.223)
662 * SetKeyboardState (USER32.@)
664 BOOL WINAPI SetKeyboardState(LPBYTE lpKeyState)
666 TRACE_(key)("(%p)\n", lpKeyState);
667 if (lpKeyState) memcpy(QueueKeyStateTable, lpKeyState, 256);
668 return TRUE;
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 wine_server_call( req );
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 start_time, elapsed, ret;
747 HANDLE idle_event = -1;
749 SERVER_START_REQ( wait_input_idle )
751 req->handle = hProcess;
752 req->timeout = dwTimeOut;
753 if (!(ret = wine_server_call_err( req ))) idle_event = reply->event;
755 SERVER_END_REQ;
756 if (ret) return WAIT_FAILED; /* error */
757 if (!idle_event) return 0; /* no event to wait on */
759 start_time = GetTickCount();
760 elapsed = 0;
762 TRACE("waiting for %x\n", idle_event );
765 ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
766 switch (ret)
768 case WAIT_OBJECT_0+1:
769 process_sent_messages();
770 break;
771 case WAIT_TIMEOUT:
772 case WAIT_FAILED:
773 TRACE("timeout or error\n");
774 return ret;
775 default:
776 TRACE("finished\n");
777 return 0;
779 if (dwTimeOut != INFINITE)
781 elapsed = GetTickCount() - start_time;
782 if (elapsed > dwTimeOut)
783 break;
786 while (1);
788 return WAIT_TIMEOUT;
792 /***********************************************************************
793 * UserYield (USER.332)
794 * UserYield16 (USER32.@)
796 void WINAPI UserYield16(void)
798 DWORD count;
800 /* Handle sent messages */
801 process_sent_messages();
803 /* Yield */
804 ReleaseThunkLock(&count);
805 if (count)
807 RestoreThunkLock(count);
808 /* Handle sent messages again */
809 process_sent_messages();
814 struct accent_char
816 BYTE ac_accent;
817 BYTE ac_char;
818 BYTE ac_result;
821 static const struct accent_char accent_chars[] =
823 /* A good idea should be to read /usr/X11/lib/X11/locale/iso8859-x/Compose */
824 {'`', 'A', '\300'}, {'`', 'a', '\340'},
825 {'\'', 'A', '\301'}, {'\'', 'a', '\341'},
826 {'^', 'A', '\302'}, {'^', 'a', '\342'},
827 {'~', 'A', '\303'}, {'~', 'a', '\343'},
828 {'"', 'A', '\304'}, {'"', 'a', '\344'},
829 {'O', 'A', '\305'}, {'o', 'a', '\345'},
830 {'0', 'A', '\305'}, {'0', 'a', '\345'},
831 {'A', 'A', '\305'}, {'a', 'a', '\345'},
832 {'A', 'E', '\306'}, {'a', 'e', '\346'},
833 {',', 'C', '\307'}, {',', 'c', '\347'},
834 {'`', 'E', '\310'}, {'`', 'e', '\350'},
835 {'\'', 'E', '\311'}, {'\'', 'e', '\351'},
836 {'^', 'E', '\312'}, {'^', 'e', '\352'},
837 {'"', 'E', '\313'}, {'"', 'e', '\353'},
838 {'`', 'I', '\314'}, {'`', 'i', '\354'},
839 {'\'', 'I', '\315'}, {'\'', 'i', '\355'},
840 {'^', 'I', '\316'}, {'^', 'i', '\356'},
841 {'"', 'I', '\317'}, {'"', 'i', '\357'},
842 {'-', 'D', '\320'}, {'-', 'd', '\360'},
843 {'~', 'N', '\321'}, {'~', 'n', '\361'},
844 {'`', 'O', '\322'}, {'`', 'o', '\362'},
845 {'\'', 'O', '\323'}, {'\'', 'o', '\363'},
846 {'^', 'O', '\324'}, {'^', 'o', '\364'},
847 {'~', 'O', '\325'}, {'~', 'o', '\365'},
848 {'"', 'O', '\326'}, {'"', 'o', '\366'},
849 {'/', 'O', '\330'}, {'/', 'o', '\370'},
850 {'`', 'U', '\331'}, {'`', 'u', '\371'},
851 {'\'', 'U', '\332'}, {'\'', 'u', '\372'},
852 {'^', 'U', '\333'}, {'^', 'u', '\373'},
853 {'"', 'U', '\334'}, {'"', 'u', '\374'},
854 {'\'', 'Y', '\335'}, {'\'', 'y', '\375'},
855 {'T', 'H', '\336'}, {'t', 'h', '\376'},
856 {'s', 's', '\337'}, {'"', 'y', '\377'},
857 {'s', 'z', '\337'}, {'i', 'j', '\377'},
858 /* iso-8859-2 uses this */
859 {'<', 'L', '\245'}, {'<', 'l', '\265'}, /* caron */
860 {'<', 'S', '\251'}, {'<', 's', '\271'},
861 {'<', 'T', '\253'}, {'<', 't', '\273'},
862 {'<', 'Z', '\256'}, {'<', 'z', '\276'},
863 {'<', 'C', '\310'}, {'<', 'c', '\350'},
864 {'<', 'E', '\314'}, {'<', 'e', '\354'},
865 {'<', 'D', '\317'}, {'<', 'd', '\357'},
866 {'<', 'N', '\322'}, {'<', 'n', '\362'},
867 {'<', 'R', '\330'}, {'<', 'r', '\370'},
868 {';', 'A', '\241'}, {';', 'a', '\261'}, /* ogonek */
869 {';', 'E', '\312'}, {';', 'e', '\332'},
870 {'\'', 'Z', '\254'}, {'\'', 'z', '\274'}, /* acute */
871 {'\'', 'R', '\300'}, {'\'', 'r', '\340'},
872 {'\'', 'L', '\305'}, {'\'', 'l', '\345'},
873 {'\'', 'C', '\306'}, {'\'', 'c', '\346'},
874 {'\'', 'N', '\321'}, {'\'', 'n', '\361'},
875 /* collision whith S, from iso-8859-9 !!! */
876 {',', 'S', '\252'}, {',', 's', '\272'}, /* cedilla */
877 {',', 'T', '\336'}, {',', 't', '\376'},
878 {'.', 'Z', '\257'}, {'.', 'z', '\277'}, /* dot above */
879 {'/', 'L', '\243'}, {'/', 'l', '\263'}, /* slash */
880 {'/', 'D', '\320'}, {'/', 'd', '\360'},
881 {'(', 'A', '\303'}, {'(', 'a', '\343'}, /* breve */
882 {'\275', 'O', '\325'}, {'\275', 'o', '\365'}, /* double acute */
883 {'\275', 'U', '\334'}, {'\275', 'u', '\374'},
884 {'0', 'U', '\332'}, {'0', 'u', '\372'}, /* ring above */
885 /* iso-8859-3 uses this */
886 {'/', 'H', '\241'}, {'/', 'h', '\261'}, /* slash */
887 {'>', 'H', '\246'}, {'>', 'h', '\266'}, /* circumflex */
888 {'>', 'J', '\254'}, {'>', 'j', '\274'},
889 {'>', 'C', '\306'}, {'>', 'c', '\346'},
890 {'>', 'G', '\330'}, {'>', 'g', '\370'},
891 {'>', 'S', '\336'}, {'>', 's', '\376'},
892 /* collision whith G( from iso-8859-9 !!! */
893 {'(', 'G', '\253'}, {'(', 'g', '\273'}, /* breve */
894 {'(', 'U', '\335'}, {'(', 'u', '\375'},
895 /* collision whith I. from iso-8859-3 !!! */
896 {'.', 'I', '\251'}, {'.', 'i', '\271'}, /* dot above */
897 {'.', 'C', '\305'}, {'.', 'c', '\345'},
898 {'.', 'G', '\325'}, {'.', 'g', '\365'},
899 /* iso-8859-4 uses this */
900 {',', 'R', '\243'}, {',', 'r', '\263'}, /* cedilla */
901 {',', 'L', '\246'}, {',', 'l', '\266'},
902 {',', 'G', '\253'}, {',', 'g', '\273'},
903 {',', 'N', '\321'}, {',', 'n', '\361'},
904 {',', 'K', '\323'}, {',', 'k', '\363'},
905 {'~', 'I', '\245'}, {'~', 'i', '\265'}, /* tilde */
906 {'-', 'E', '\252'}, {'-', 'e', '\272'}, /* macron */
907 {'-', 'A', '\300'}, {'-', 'a', '\340'},
908 {'-', 'I', '\317'}, {'-', 'i', '\357'},
909 {'-', 'O', '\322'}, {'-', 'o', '\362'},
910 {'-', 'U', '\336'}, {'-', 'u', '\376'},
911 {'/', 'T', '\254'}, {'/', 't', '\274'}, /* slash */
912 {'.', 'E', '\314'}, {'.', 'e', '\344'}, /* dot above */
913 {';', 'I', '\307'}, {';', 'i', '\347'}, /* ogonek */
914 {';', 'U', '\331'}, {';', 'u', '\371'},
915 /* iso-8859-9 uses this */
916 /* iso-8859-9 has really bad choosen G( S, and I. as they collide
917 * whith the same letters on other iso-8859-x (that is they are on
918 * different places :-( ), if you use turkish uncomment these and
919 * comment out the lines in iso-8859-2 and iso-8859-3 sections
920 * FIXME: should be dynamic according to chosen language
921 * if/when Wine has turkish support.
923 /* collision whith G( from iso-8859-3 !!! */
924 /* {'(', 'G', '\320'}, {'(', 'g', '\360'}, */ /* breve */
925 /* collision whith S, from iso-8859-2 !!! */
926 /* {',', 'S', '\336'}, {',', 's', '\376'}, */ /* cedilla */
927 /* collision whith I. from iso-8859-3 !!! */
928 /* {'.', 'I', '\335'}, {'.', 'i', '\375'}, */ /* dot above */
932 /***********************************************************************
933 * TranslateMessage (USER32.@)
935 * Implementation of TranslateMessage.
937 * TranslateMessage translates virtual-key messages into character-messages,
938 * as follows :
939 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
940 * ditto replacing WM_* with WM_SYS*
941 * This produces WM_CHAR messages only for keys mapped to ASCII characters
942 * by the keyboard driver.
944 * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
945 * return value is nonzero, regardless of the translation.
948 BOOL WINAPI TranslateMessage( const MSG *msg )
950 static int dead_char;
951 UINT message;
952 WCHAR wp[2];
953 BOOL rc = FALSE;
955 if (msg->message >= WM_KEYFIRST && msg->message <= WM_KEYLAST)
957 TRACE_(key)("(%s, %04X, %08lX)\n",
958 SPY_GetMsgName(msg->message, msg->hwnd), msg->wParam, msg->lParam );
960 /* Return code must be TRUE no matter what! */
961 rc = TRUE;
964 if ((msg->message != WM_KEYDOWN) && (msg->message != WM_SYSKEYDOWN)) return rc;
966 TRACE_(key)("Translating key %s (%04x), scancode %02x\n",
967 SPY_GetVKeyName(msg->wParam), msg->wParam, LOBYTE(HIWORD(msg->lParam)));
969 /* FIXME : should handle ToUnicode yielding 2 */
970 switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), QueueKeyStateTable, wp, 2, 0))
972 case 1:
973 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
974 /* Should dead chars handling go in ToAscii ? */
975 if (dead_char)
977 int i;
979 if (wp[0] == ' ') wp[0] = dead_char;
980 if (dead_char == 0xa2) dead_char = '(';
981 else if (dead_char == 0xa8) dead_char = '"';
982 else if (dead_char == 0xb2) dead_char = ';';
983 else if (dead_char == 0xb4) dead_char = '\'';
984 else if (dead_char == 0xb7) dead_char = '<';
985 else if (dead_char == 0xb8) dead_char = ',';
986 else if (dead_char == 0xff) dead_char = '.';
987 for (i = 0; i < sizeof(accent_chars)/sizeof(accent_chars[0]); i++)
988 if ((accent_chars[i].ac_accent == dead_char) &&
989 (accent_chars[i].ac_char == wp[0]))
991 wp[0] = accent_chars[i].ac_result;
992 break;
994 dead_char = 0;
996 TRACE_(key)("1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
997 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
998 break;
1000 case -1:
1001 message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
1002 dead_char = wp[0];
1003 TRACE_(key)("-1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
1004 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
1005 return TRUE;
1007 return rc;
1011 /***********************************************************************
1012 * DispatchMessageA (USER32.@)
1014 LONG WINAPI DispatchMessageA( const MSG* msg )
1016 WND * wndPtr;
1017 LONG retval;
1018 int painting;
1019 WNDPROC winproc;
1021 /* Process timer messages */
1022 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1024 if (msg->lParam)
1026 /* HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1028 /* before calling window proc, verify whether timer is still valid;
1029 there's a slim chance that the application kills the timer
1030 between GetMessage and DispatchMessage API calls */
1031 if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (HWINDOWPROC) msg->lParam))
1032 return 0; /* invalid winproc */
1034 return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
1035 msg->message, msg->wParam, GetTickCount() );
1039 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
1041 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1042 return 0;
1044 if (wndPtr == WND_OTHER_PROCESS)
1046 if (IsWindow( msg->hwnd ))
1047 ERR( "cannot dispatch msg to other process window %x\n", msg->hwnd );
1048 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1049 return 0;
1051 if (!(winproc = wndPtr->winproc))
1053 WIN_ReleasePtr( wndPtr );
1054 return 0;
1056 painting = (msg->message == WM_PAINT);
1057 if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1058 WIN_ReleasePtr( wndPtr );
1059 /* hook_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1061 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1062 msg->wParam, msg->lParam );
1063 retval = CallWindowProcA( winproc, msg->hwnd, msg->message,
1064 msg->wParam, msg->lParam );
1065 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1066 msg->wParam, msg->lParam );
1068 if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
1070 BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
1071 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1072 WIN_ReleasePtr( wndPtr );
1073 if (validate)
1075 ERR( "BeginPaint not called on WM_PAINT for hwnd %04x!\n", msg->hwnd );
1076 /* Validate the update region to avoid infinite WM_PAINT loop */
1077 RedrawWindow( msg->hwnd, NULL, 0,
1078 RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1081 return retval;
1085 /***********************************************************************
1086 * DispatchMessageW (USER32.@) Process Message
1088 * Process the message specified in the structure *_msg_.
1090 * If the lpMsg parameter points to a WM_TIMER message and the
1091 * parameter of the WM_TIMER message is not NULL, the lParam parameter
1092 * points to the function that is called instead of the window
1093 * procedure.
1095 * The message must be valid.
1097 * RETURNS
1099 * DispatchMessage() returns the result of the window procedure invoked.
1101 * CONFORMANCE
1103 * ECMA-234, Win32
1106 LONG WINAPI DispatchMessageW( const MSG* msg )
1108 WND * wndPtr;
1109 LONG retval;
1110 int painting;
1111 WNDPROC winproc;
1113 /* Process timer messages */
1114 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1116 if (msg->lParam)
1118 /* HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1120 /* before calling window proc, verify whether timer is still valid;
1121 there's a slim chance that the application kills the timer
1122 between GetMessage and DispatchMessage API calls */
1123 if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (HWINDOWPROC) msg->lParam))
1124 return 0; /* invalid winproc */
1126 return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
1127 msg->message, msg->wParam, GetTickCount() );
1131 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
1133 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1134 return 0;
1136 if (wndPtr == WND_OTHER_PROCESS)
1138 if (IsWindow( msg->hwnd ))
1139 ERR( "cannot dispatch msg to other process window %x\n", msg->hwnd );
1140 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1141 return 0;
1143 if (!(winproc = wndPtr->winproc))
1145 WIN_ReleasePtr( wndPtr );
1146 return 0;
1148 painting = (msg->message == WM_PAINT);
1149 if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1150 WIN_ReleasePtr( wndPtr );
1151 /* HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1153 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1154 msg->wParam, msg->lParam );
1155 retval = CallWindowProcW( winproc, msg->hwnd, msg->message,
1156 msg->wParam, msg->lParam );
1157 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1158 msg->wParam, msg->lParam );
1160 if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
1162 BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
1163 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1164 WIN_ReleasePtr( wndPtr );
1165 if (validate)
1167 ERR( "BeginPaint not called on WM_PAINT for hwnd %04x!\n", msg->hwnd );
1168 /* Validate the update region to avoid infinite WM_PAINT loop */
1169 RedrawWindow( msg->hwnd, NULL, 0,
1170 RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1173 return retval;
1177 /***********************************************************************
1178 * RegisterWindowMessage (USER.118)
1179 * RegisterWindowMessageA (USER32.@)
1181 WORD WINAPI RegisterWindowMessageA( LPCSTR str )
1183 TRACE("%s\n", str );
1184 return GlobalAddAtomA( str );
1188 /***********************************************************************
1189 * RegisterWindowMessageW (USER32.@)
1191 WORD WINAPI RegisterWindowMessageW( LPCWSTR str )
1193 TRACE("%p\n", str );
1194 return GlobalAddAtomW( str );
1198 /***********************************************************************
1199 * BroadcastSystemMessage (USER32.@)
1201 LONG WINAPI BroadcastSystemMessage(
1202 DWORD dwFlags,LPDWORD recipients,UINT uMessage,WPARAM wParam,
1203 LPARAM lParam
1205 FIXME("(%08lx,%08lx,%08x,%08x,%08lx): stub!\n",
1206 dwFlags,*recipients,uMessage,wParam,lParam
1208 return 0;