- change the internal functions in windows/cursoricon.c to use 32bit
[wine/multimedia.git] / windows / message.c
blob5951bffa9a654691297b9671aa6e764375a6d00a
1 /*
2 * Message queues related functions
4 * Copyright 1993, 1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #ifdef HAVE_SYS_TIME_H
27 # include <sys/time.h>
28 #endif
29 #include <sys/types.h>
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "message.h"
35 #include "winerror.h"
36 #include "wine/server.h"
37 #include "win.h"
38 #include "heap.h"
39 #include "hook.h"
40 #include "input.h"
41 #include "spy.h"
42 #include "winpos.h"
43 #include "dde.h"
44 #include "queue.h"
45 #include "winproc.h"
46 #include "user.h"
47 #include "thread.h"
48 #include "task.h"
49 #include "controls.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(msg);
53 WINE_DECLARE_DEBUG_CHANNEL(key);
55 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
56 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
58 static BYTE QueueKeyStateTable[256];
61 /***********************************************************************
62 * is_keyboard_message
64 inline static BOOL is_keyboard_message( UINT message )
66 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
70 /***********************************************************************
71 * is_mouse_message
73 inline static BOOL is_mouse_message( UINT message )
75 return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
76 (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
80 /***********************************************************************
81 * check_message_filter
83 inline static BOOL check_message_filter( const MSG *msg, HWND hwnd, UINT first, UINT last )
85 if (hwnd)
87 if (msg->hwnd != hwnd && !IsChild( hwnd, msg->hwnd )) return FALSE;
89 if (first || last)
91 return (msg->message >= first && msg->message <= last);
93 return TRUE;
97 /***********************************************************************
98 * process_sent_messages
100 * Process all pending sent messages.
102 inline static void process_sent_messages(void)
104 MSG msg;
105 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
109 /***********************************************************************
110 * queue_hardware_message
112 * store a hardware message in the thread queue
114 static void queue_hardware_message( MSG *msg, ULONG_PTR extra_info, enum message_type type )
116 SERVER_START_REQ( send_message )
118 req->type = type;
119 req->id = GetWindowThreadProcessId( msg->hwnd, NULL );
120 req->win = msg->hwnd;
121 req->msg = msg->message;
122 req->wparam = msg->wParam;
123 req->lparam = msg->lParam;
124 req->x = msg->pt.x;
125 req->y = msg->pt.y;
126 req->time = msg->time;
127 req->info = extra_info;
128 req->timeout = 0;
129 wine_server_call( req );
131 SERVER_END_REQ;
135 /***********************************************************************
136 * update_queue_key_state
138 static void update_queue_key_state( UINT msg, WPARAM wp )
140 BOOL down = FALSE;
142 switch (msg)
144 case WM_LBUTTONDOWN:
145 down = TRUE;
146 /* fall through */
147 case WM_LBUTTONUP:
148 wp = VK_LBUTTON;
149 break;
150 case WM_MBUTTONDOWN:
151 down = TRUE;
152 /* fall through */
153 case WM_MBUTTONUP:
154 wp = VK_MBUTTON;
155 break;
156 case WM_RBUTTONDOWN:
157 down = TRUE;
158 /* fall through */
159 case WM_RBUTTONUP:
160 wp = VK_RBUTTON;
161 break;
162 case WM_KEYDOWN:
163 case WM_SYSKEYDOWN:
164 down = TRUE;
165 /* fall through */
166 case WM_KEYUP:
167 case WM_SYSKEYUP:
168 wp = wp & 0xff;
169 break;
171 if (down)
173 BYTE *p = &QueueKeyStateTable[wp];
174 if (!(*p & 0x80)) *p ^= 0x01;
175 *p |= 0x80;
177 else QueueKeyStateTable[wp] &= ~0x80;
181 /***********************************************************************
182 * MSG_SendParentNotify
184 * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
185 * the window has the WS_EX_NOPARENTNOTIFY style.
187 static void MSG_SendParentNotify( HWND hwnd, WORD event, WORD idChild, POINT pt )
189 /* pt has to be in the client coordinates of the parent window */
190 MapWindowPoints( 0, hwnd, &pt, 1 );
191 for (;;)
193 HWND parent;
195 if (!(GetWindowLongA( hwnd, GWL_STYLE ) & WS_CHILD)) break;
196 if (GetWindowLongA( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
197 if (!(parent = GetParent(hwnd))) break;
198 MapWindowPoints( hwnd, parent, &pt, 1 );
199 hwnd = parent;
200 SendMessageA( hwnd, WM_PARENTNOTIFY,
201 MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
206 /***********************************************************************
207 * MSG_JournalPlayBackMsg
209 * Get an EVENTMSG struct via call JOURNALPLAYBACK hook function
211 void MSG_JournalPlayBackMsg(void)
213 EVENTMSG tmpMsg;
214 MSG msg;
215 LRESULT wtime;
216 int keyDown,i;
218 if (!HOOK_IsHooked( WH_JOURNALPLAYBACK )) return;
220 wtime=HOOK_CallHooksA( WH_JOURNALPLAYBACK, HC_GETNEXT, 0, (LPARAM)&tmpMsg );
221 /* TRACE(msg,"Playback wait time =%ld\n",wtime); */
222 if (wtime<=0)
224 wtime=0;
225 msg.message = tmpMsg.message;
226 msg.hwnd = tmpMsg.hwnd;
227 msg.time = tmpMsg.time;
228 if ((tmpMsg.message >= WM_KEYFIRST) && (tmpMsg.message <= WM_KEYLAST))
230 msg.wParam = tmpMsg.paramL & 0xFF;
231 msg.lParam = MAKELONG(tmpMsg.paramH&0x7ffff,tmpMsg.paramL>>8);
232 if (tmpMsg.message == WM_KEYDOWN || tmpMsg.message == WM_SYSKEYDOWN)
234 for (keyDown=i=0; i<256 && !keyDown; i++)
235 if (InputKeyStateTable[i] & 0x80)
236 keyDown++;
237 if (!keyDown)
238 msg.lParam |= 0x40000000;
239 InputKeyStateTable[msg.wParam] |= 0x80;
240 AsyncKeyStateTable[msg.wParam] |= 0x80;
242 else /* WM_KEYUP, WM_SYSKEYUP */
244 msg.lParam |= 0xC0000000;
245 InputKeyStateTable[msg.wParam] &= ~0x80;
247 if (InputKeyStateTable[VK_MENU] & 0x80)
248 msg.lParam |= 0x20000000;
249 if (tmpMsg.paramH & 0x8000) /*special_key bit*/
250 msg.lParam |= 0x01000000;
252 msg.pt.x = msg.pt.y = 0;
253 queue_hardware_message( &msg, 0, MSG_HARDWARE_RAW );
255 else if ((tmpMsg.message>= WM_MOUSEFIRST) && (tmpMsg.message <= WM_MOUSELAST))
257 switch (tmpMsg.message)
259 case WM_LBUTTONDOWN:
260 InputKeyStateTable[VK_LBUTTON] |= 0x80;
261 AsyncKeyStateTable[VK_LBUTTON] |= 0x80;
262 break;
263 case WM_LBUTTONUP:
264 InputKeyStateTable[VK_LBUTTON] &= ~0x80;
265 break;
266 case WM_MBUTTONDOWN:
267 InputKeyStateTable[VK_MBUTTON] |= 0x80;
268 AsyncKeyStateTable[VK_MBUTTON] |= 0x80;
269 break;
270 case WM_MBUTTONUP:
271 InputKeyStateTable[VK_MBUTTON] &= ~0x80;
272 break;
273 case WM_RBUTTONDOWN:
274 InputKeyStateTable[VK_RBUTTON] |= 0x80;
275 AsyncKeyStateTable[VK_RBUTTON] |= 0x80;
276 break;
277 case WM_RBUTTONUP:
278 InputKeyStateTable[VK_RBUTTON] &= ~0x80;
279 break;
281 SetCursorPos(tmpMsg.paramL,tmpMsg.paramH);
282 msg.lParam=MAKELONG(tmpMsg.paramL,tmpMsg.paramH);
283 msg.wParam=0;
284 if (InputKeyStateTable[VK_LBUTTON] & 0x80) msg.wParam |= MK_LBUTTON;
285 if (InputKeyStateTable[VK_MBUTTON] & 0x80) msg.wParam |= MK_MBUTTON;
286 if (InputKeyStateTable[VK_RBUTTON] & 0x80) msg.wParam |= MK_RBUTTON;
288 msg.pt.x = tmpMsg.paramL;
289 msg.pt.y = tmpMsg.paramH;
290 queue_hardware_message( &msg, 0, MSG_HARDWARE_RAW );
292 HOOK_CallHooksA( WH_JOURNALPLAYBACK, HC_SKIP, 0, (LPARAM)&tmpMsg);
294 else
296 if( tmpMsg.message == WM_QUEUESYNC )
297 if (HOOK_IsHooked( WH_CBT ))
298 HOOK_CallHooksA( WH_CBT, HCBT_QS, 0, 0L);
303 /***********************************************************************
304 * process_raw_keyboard_message
306 * returns TRUE if the contents of 'msg' should be passed to the application
308 static BOOL process_raw_keyboard_message( MSG *msg, ULONG_PTR extra_info )
310 if (!(msg->hwnd = GetFocus()))
312 /* Send the message to the active window instead, */
313 /* translating messages to their WM_SYS equivalent */
314 msg->hwnd = GetActiveWindow();
315 if (msg->message < WM_SYSKEYDOWN) msg->message += WM_SYSKEYDOWN - WM_KEYDOWN;
318 if (HOOK_IsHooked( WH_JOURNALRECORD ))
320 EVENTMSG event;
322 event.message = msg->message;
323 event.hwnd = msg->hwnd;
324 event.time = msg->time;
325 event.paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
326 event.paramH = msg->lParam & 0x7FFF;
327 if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
328 HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event );
331 /* if we are going to throw away the message, update the queue state now */
332 if (!msg->hwnd) update_queue_key_state( msg->message, msg->wParam );
334 return (msg->hwnd != 0);
338 /***********************************************************************
339 * process_cooked_keyboard_message
341 * returns TRUE if the contents of 'msg' should be passed to the application
343 static BOOL process_cooked_keyboard_message( MSG *msg, BOOL remove )
345 if (remove)
347 update_queue_key_state( msg->message, msg->wParam );
349 /* Handle F1 key by sending out WM_HELP message */
350 if ((msg->message == WM_KEYUP) &&
351 (msg->wParam == VK_F1) &&
352 (msg->hwnd != GetDesktopWindow()) &&
353 !MENU_IsMenuActive())
355 HELPINFO hi;
356 hi.cbSize = sizeof(HELPINFO);
357 hi.iContextType = HELPINFO_WINDOW;
358 hi.iCtrlId = GetWindowLongA( msg->hwnd, GWL_ID );
359 hi.hItemHandle = msg->hwnd;
360 hi.dwContextId = GetWindowContextHelpId( msg->hwnd );
361 hi.MousePos = msg->pt;
362 SendMessageA(msg->hwnd, WM_HELP, 0, (LPARAM)&hi);
366 if (HOOK_CallHooksA( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
367 LOWORD(msg->wParam), msg->lParam ))
369 /* skip this message */
370 HOOK_CallHooksA( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam );
371 return FALSE;
373 return TRUE;
377 /***********************************************************************
378 * process_raw_mouse_message
380 * returns TRUE if the contents of 'msg' should be passed to the application
382 static BOOL process_raw_mouse_message( MSG *msg, ULONG_PTR extra_info )
384 static MSG clk_msg;
386 POINT pt;
387 INT ht, hittest;
389 /* find the window to dispatch this mouse message to */
391 hittest = HTCLIENT;
392 if (!(msg->hwnd = PERQDATA_GetCaptureWnd( &ht )))
394 /* If no capture HWND, find window which contains the mouse position.
395 * Also find the position of the cursor hot spot (hittest) */
396 HWND hWndScope = (HWND)extra_info;
398 if (!IsWindow(hWndScope)) hWndScope = 0;
399 if (!(msg->hwnd = WINPOS_WindowFromPoint( hWndScope, msg->pt, &hittest )))
400 msg->hwnd = GetDesktopWindow();
401 ht = hittest;
404 if (HOOK_IsHooked( WH_JOURNALRECORD ))
406 EVENTMSG event;
407 event.message = msg->message;
408 event.time = msg->time;
409 event.hwnd = msg->hwnd;
410 event.paramL = msg->pt.x;
411 event.paramH = msg->pt.y;
412 HOOK_CallHooksA( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event );
415 /* translate double clicks */
417 if ((msg->message == WM_LBUTTONDOWN) ||
418 (msg->message == WM_RBUTTONDOWN) ||
419 (msg->message == WM_MBUTTONDOWN))
421 BOOL update = TRUE;
422 /* translate double clicks -
423 * note that ...MOUSEMOVEs can slip in between
424 * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
426 if (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS || ht != HTCLIENT )
428 if ((msg->message == clk_msg.message) &&
429 (msg->hwnd == clk_msg.hwnd) &&
430 (msg->time - clk_msg.time < GetDoubleClickTime()) &&
431 (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
432 (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
434 msg->message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
435 clk_msg.message = 0;
436 update = FALSE;
439 /* update static double click conditions */
440 if (update) clk_msg = *msg;
443 pt = msg->pt;
444 /* Note: windows has no concept of a non-client wheel message */
445 if (hittest != HTCLIENT && msg->message != WM_MOUSEWHEEL)
447 msg->message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
448 msg->wParam = hittest;
450 else ScreenToClient( msg->hwnd, &pt );
451 msg->lParam = MAKELONG( pt.x, pt.y );
452 return TRUE;
456 /***********************************************************************
457 * process_cooked_mouse_message
459 * returns TRUE if the contents of 'msg' should be passed to the application
461 static BOOL process_cooked_mouse_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
463 INT hittest = HTCLIENT;
464 UINT raw_message = msg->message;
465 BOOL eatMsg;
467 if (msg->message >= WM_NCMOUSEFIRST && msg->message <= WM_NCMOUSELAST)
469 raw_message += WM_MOUSEFIRST - WM_NCMOUSEFIRST;
470 hittest = msg->wParam;
472 if (raw_message == WM_LBUTTONDBLCLK ||
473 raw_message == WM_RBUTTONDBLCLK ||
474 raw_message == WM_MBUTTONDBLCLK)
476 raw_message += WM_LBUTTONDOWN - WM_LBUTTONDBLCLK;
479 if (remove) update_queue_key_state( raw_message, 0 );
481 if (HOOK_IsHooked( WH_MOUSE ))
483 MOUSEHOOKSTRUCT hook;
484 hook.pt = msg->pt;
485 hook.hwnd = msg->hwnd;
486 hook.wHitTestCode = hittest;
487 hook.dwExtraInfo = extra_info;
488 if (HOOK_CallHooksA( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
489 msg->message, (LPARAM)&hook ))
491 hook.pt = msg->pt;
492 hook.hwnd = msg->hwnd;
493 hook.wHitTestCode = hittest;
494 hook.dwExtraInfo = extra_info;
495 HOOK_CallHooksA( WH_CBT, HCBT_CLICKSKIPPED, msg->message, (LPARAM)&hook );
496 return FALSE;
500 if ((hittest == HTERROR) || (hittest == HTNOWHERE))
502 SendMessageA( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
503 MAKELONG( hittest, raw_message ));
504 return FALSE;
507 if (!remove || GetCapture()) return TRUE;
509 eatMsg = FALSE;
511 if ((raw_message == WM_LBUTTONDOWN) ||
512 (raw_message == WM_RBUTTONDOWN) ||
513 (raw_message == WM_MBUTTONDOWN))
515 HWND hwndTop = GetAncestor( msg->hwnd, GA_ROOT );
517 /* Send the WM_PARENTNOTIFY,
518 * note that even for double/nonclient clicks
519 * notification message is still WM_L/M/RBUTTONDOWN.
521 MSG_SendParentNotify( msg->hwnd, raw_message, 0, msg->pt );
523 /* Activate the window if needed */
525 if (msg->hwnd != GetActiveWindow() && hwndTop != GetDesktopWindow())
527 LONG ret = SendMessageA( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
528 MAKELONG( hittest, raw_message ) );
530 switch(ret)
532 case MA_NOACTIVATEANDEAT:
533 eatMsg = TRUE;
534 /* fall through */
535 case MA_NOACTIVATE:
536 break;
537 case MA_ACTIVATEANDEAT:
538 eatMsg = TRUE;
539 /* fall through */
540 case MA_ACTIVATE:
541 case 0:
542 if (hwndTop != GetForegroundWindow() )
544 if (!WINPOS_SetActiveWindow( hwndTop, TRUE , TRUE ))
545 eatMsg = TRUE;
547 break;
548 default:
549 WARN( "unknown WM_MOUSEACTIVATE code %ld\n", ret );
550 break;
555 /* send the WM_SETCURSOR message */
557 /* Windows sends the normal mouse message as the message parameter
558 in the WM_SETCURSOR message even if it's non-client mouse message */
559 SendMessageA( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
560 MAKELONG( hittest, raw_message ));
562 return !eatMsg;
566 /***********************************************************************
567 * process_hardware_message
569 * returns TRUE if the contents of 'msg' should be passed to the application
571 BOOL MSG_process_raw_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd_filter,
572 UINT first, UINT last, BOOL remove )
574 if (is_keyboard_message( msg->message ))
576 if (!process_raw_keyboard_message( msg, extra_info )) return FALSE;
578 else if (is_mouse_message( msg->message ))
580 if (!process_raw_mouse_message( msg, extra_info )) return FALSE;
582 else
584 ERR( "unknown message type %x\n", msg->message );
585 return FALSE;
588 /* check destination thread and filters */
589 if (!check_message_filter( msg, hwnd_filter, first, last ) ||
590 !WIN_IsCurrentThread( msg->hwnd ))
592 /* queue it for later, or for another thread */
593 queue_hardware_message( msg, extra_info, MSG_HARDWARE_COOKED );
594 return FALSE;
597 /* save the message in the cooked queue if we didn't want to remove it */
598 if (!remove) queue_hardware_message( msg, extra_info, MSG_HARDWARE_COOKED );
599 return TRUE;
603 /***********************************************************************
604 * MSG_process_cooked_hardware_message
606 * returns TRUE if the contents of 'msg' should be passed to the application
608 BOOL MSG_process_cooked_hardware_message( MSG *msg, ULONG_PTR extra_info, BOOL remove )
610 if (is_keyboard_message( msg->message ))
611 return process_cooked_keyboard_message( msg, remove );
613 if (is_mouse_message( msg->message ))
614 return process_cooked_mouse_message( msg, extra_info, remove );
616 ERR( "unknown message type %x\n", msg->message );
617 return FALSE;
621 /**********************************************************************
622 * GetKeyState (USER.106)
624 INT16 WINAPI GetKeyState16(INT16 vkey)
626 return GetKeyState(vkey);
630 /**********************************************************************
631 * GetKeyState (USER32.@)
633 * An application calls the GetKeyState function in response to a
634 * keyboard-input message. This function retrieves the state of the key
635 * at the time the input message was generated. (SDK 3.1 Vol 2. p 390)
637 SHORT WINAPI GetKeyState(INT vkey)
639 INT retval;
641 if (vkey >= 'a' && vkey <= 'z') vkey += 'A' - 'a';
642 retval = ((WORD)(QueueKeyStateTable[vkey] & 0x80) << 8 ) |
643 (QueueKeyStateTable[vkey] & 0x80) |
644 (QueueKeyStateTable[vkey] & 0x01);
645 TRACE("key (0x%x) -> %x\n", vkey, retval);
646 return retval;
650 /**********************************************************************
651 * GetKeyboardState (USER.222)
652 * GetKeyboardState (USER32.@)
654 * An application calls the GetKeyboardState function in response to a
655 * keyboard-input message. This function retrieves the state of the keyboard
656 * at the time the input message was generated. (SDK 3.1 Vol 2. p 387)
658 BOOL WINAPI GetKeyboardState(LPBYTE lpKeyState)
660 TRACE_(key)("(%p)\n", lpKeyState);
661 if (lpKeyState) memcpy(lpKeyState, QueueKeyStateTable, 256);
662 return TRUE;
666 /**********************************************************************
667 * SetKeyboardState (USER.223)
668 * SetKeyboardState (USER32.@)
670 BOOL WINAPI SetKeyboardState(LPBYTE lpKeyState)
672 TRACE_(key)("(%p)\n", lpKeyState);
673 if (lpKeyState) memcpy(QueueKeyStateTable, lpKeyState, 256);
674 return TRUE;
678 /***********************************************************************
679 * WaitMessage (USER.112) Suspend thread pending messages
680 * WaitMessage (USER32.@) Suspend thread pending messages
682 * WaitMessage() suspends a thread until events appear in the thread's
683 * queue.
685 BOOL WINAPI WaitMessage(void)
687 return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
691 /***********************************************************************
692 * MsgWaitForMultipleObjectsEx (USER32.@)
694 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
695 DWORD timeout, DWORD mask, DWORD flags )
697 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
698 DWORD i, ret, lock;
699 MESSAGEQUEUE *msgQueue;
701 if (count > MAXIMUM_WAIT_OBJECTS-1)
703 SetLastError( ERROR_INVALID_PARAMETER );
704 return WAIT_FAILED;
707 if (!(msgQueue = QUEUE_Current())) return WAIT_FAILED;
709 /* set the queue mask */
710 SERVER_START_REQ( set_queue_mask )
712 req->wake_mask = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
713 req->changed_mask = mask;
714 req->skip_wait = 0;
715 wine_server_call( req );
717 SERVER_END_REQ;
719 /* Add the thread event to the handle list */
720 for (i = 0; i < count; i++) handles[i] = pHandles[i];
721 handles[count] = msgQueue->server_queue;
723 ReleaseThunkLock( &lock );
724 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
726 ret = USER_Driver.pMsgWaitForMultipleObjectsEx( count+1, handles, timeout, mask, flags );
727 if (ret == count+1) ret = count; /* pretend the msg queue is ready */
729 else
730 ret = WaitForMultipleObjectsEx( count+1, handles, flags & MWMO_WAITALL,
731 timeout, flags & MWMO_ALERTABLE );
732 if (lock) RestoreThunkLock( lock );
733 return ret;
737 /***********************************************************************
738 * MsgWaitForMultipleObjects (USER32.@)
740 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
741 BOOL wait_all, DWORD timeout, DWORD mask )
743 return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
744 wait_all ? MWMO_WAITALL : 0 );
748 /***********************************************************************
749 * WaitForInputIdle (USER32.@)
751 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
753 DWORD start_time, elapsed, ret;
754 HANDLE idle_event = -1;
756 SERVER_START_REQ( wait_input_idle )
758 req->handle = hProcess;
759 req->timeout = dwTimeOut;
760 if (!(ret = wine_server_call_err( req ))) idle_event = reply->event;
762 SERVER_END_REQ;
763 if (ret) return WAIT_FAILED; /* error */
764 if (!idle_event) return 0; /* no event to wait on */
766 start_time = GetTickCount();
767 elapsed = 0;
769 TRACE("waiting for %x\n", idle_event );
772 ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
773 switch (ret)
775 case WAIT_OBJECT_0+1:
776 process_sent_messages();
777 break;
778 case WAIT_TIMEOUT:
779 case WAIT_FAILED:
780 TRACE("timeout or error\n");
781 return ret;
782 default:
783 TRACE("finished\n");
784 return 0;
786 if (dwTimeOut != INFINITE)
788 elapsed = GetTickCount() - start_time;
789 if (elapsed > dwTimeOut)
790 break;
793 while (1);
795 return WAIT_TIMEOUT;
799 /***********************************************************************
800 * UserYield (USER.332)
802 void WINAPI UserYield16(void)
804 DWORD count;
806 /* Handle sent messages */
807 process_sent_messages();
809 /* Yield */
810 ReleaseThunkLock(&count);
811 if (count)
813 RestoreThunkLock(count);
814 /* Handle sent messages again */
815 process_sent_messages();
820 struct accent_char
822 BYTE ac_accent;
823 BYTE ac_char;
824 BYTE ac_result;
827 static const struct accent_char accent_chars[] =
829 /* A good idea should be to read /usr/X11/lib/X11/locale/iso8859-x/Compose */
830 {'`', 'A', '\300'}, {'`', 'a', '\340'},
831 {'\'', 'A', '\301'}, {'\'', 'a', '\341'},
832 {'^', 'A', '\302'}, {'^', 'a', '\342'},
833 {'~', 'A', '\303'}, {'~', 'a', '\343'},
834 {'"', 'A', '\304'}, {'"', 'a', '\344'},
835 {'O', 'A', '\305'}, {'o', 'a', '\345'},
836 {'0', 'A', '\305'}, {'0', 'a', '\345'},
837 {'A', 'A', '\305'}, {'a', 'a', '\345'},
838 {'A', 'E', '\306'}, {'a', 'e', '\346'},
839 {',', 'C', '\307'}, {',', 'c', '\347'},
840 {'`', 'E', '\310'}, {'`', 'e', '\350'},
841 {'\'', 'E', '\311'}, {'\'', 'e', '\351'},
842 {'^', 'E', '\312'}, {'^', 'e', '\352'},
843 {'"', 'E', '\313'}, {'"', 'e', '\353'},
844 {'`', 'I', '\314'}, {'`', 'i', '\354'},
845 {'\'', 'I', '\315'}, {'\'', 'i', '\355'},
846 {'^', 'I', '\316'}, {'^', 'i', '\356'},
847 {'"', 'I', '\317'}, {'"', 'i', '\357'},
848 {'-', 'D', '\320'}, {'-', 'd', '\360'},
849 {'~', 'N', '\321'}, {'~', 'n', '\361'},
850 {'`', 'O', '\322'}, {'`', 'o', '\362'},
851 {'\'', 'O', '\323'}, {'\'', 'o', '\363'},
852 {'^', 'O', '\324'}, {'^', 'o', '\364'},
853 {'~', 'O', '\325'}, {'~', 'o', '\365'},
854 {'"', 'O', '\326'}, {'"', 'o', '\366'},
855 {'/', 'O', '\330'}, {'/', 'o', '\370'},
856 {'`', 'U', '\331'}, {'`', 'u', '\371'},
857 {'\'', 'U', '\332'}, {'\'', 'u', '\372'},
858 {'^', 'U', '\333'}, {'^', 'u', '\373'},
859 {'"', 'U', '\334'}, {'"', 'u', '\374'},
860 {'\'', 'Y', '\335'}, {'\'', 'y', '\375'},
861 {'T', 'H', '\336'}, {'t', 'h', '\376'},
862 {'s', 's', '\337'}, {'"', 'y', '\377'},
863 {'s', 'z', '\337'}, {'i', 'j', '\377'},
864 /* iso-8859-2 uses this */
865 {'<', 'L', '\245'}, {'<', 'l', '\265'}, /* caron */
866 {'<', 'S', '\251'}, {'<', 's', '\271'},
867 {'<', 'T', '\253'}, {'<', 't', '\273'},
868 {'<', 'Z', '\256'}, {'<', 'z', '\276'},
869 {'<', 'C', '\310'}, {'<', 'c', '\350'},
870 {'<', 'E', '\314'}, {'<', 'e', '\354'},
871 {'<', 'D', '\317'}, {'<', 'd', '\357'},
872 {'<', 'N', '\322'}, {'<', 'n', '\362'},
873 {'<', 'R', '\330'}, {'<', 'r', '\370'},
874 {';', 'A', '\241'}, {';', 'a', '\261'}, /* ogonek */
875 {';', 'E', '\312'}, {';', 'e', '\332'},
876 {'\'', 'Z', '\254'}, {'\'', 'z', '\274'}, /* acute */
877 {'\'', 'R', '\300'}, {'\'', 'r', '\340'},
878 {'\'', 'L', '\305'}, {'\'', 'l', '\345'},
879 {'\'', 'C', '\306'}, {'\'', 'c', '\346'},
880 {'\'', 'N', '\321'}, {'\'', 'n', '\361'},
881 /* collision whith S, from iso-8859-9 !!! */
882 {',', 'S', '\252'}, {',', 's', '\272'}, /* cedilla */
883 {',', 'T', '\336'}, {',', 't', '\376'},
884 {'.', 'Z', '\257'}, {'.', 'z', '\277'}, /* dot above */
885 {'/', 'L', '\243'}, {'/', 'l', '\263'}, /* slash */
886 {'/', 'D', '\320'}, {'/', 'd', '\360'},
887 {'(', 'A', '\303'}, {'(', 'a', '\343'}, /* breve */
888 {'\275', 'O', '\325'}, {'\275', 'o', '\365'}, /* double acute */
889 {'\275', 'U', '\334'}, {'\275', 'u', '\374'},
890 {'0', 'U', '\332'}, {'0', 'u', '\372'}, /* ring above */
891 /* iso-8859-3 uses this */
892 {'/', 'H', '\241'}, {'/', 'h', '\261'}, /* slash */
893 {'>', 'H', '\246'}, {'>', 'h', '\266'}, /* circumflex */
894 {'>', 'J', '\254'}, {'>', 'j', '\274'},
895 {'>', 'C', '\306'}, {'>', 'c', '\346'},
896 {'>', 'G', '\330'}, {'>', 'g', '\370'},
897 {'>', 'S', '\336'}, {'>', 's', '\376'},
898 /* collision whith G( from iso-8859-9 !!! */
899 {'(', 'G', '\253'}, {'(', 'g', '\273'}, /* breve */
900 {'(', 'U', '\335'}, {'(', 'u', '\375'},
901 /* collision whith I. from iso-8859-3 !!! */
902 {'.', 'I', '\251'}, {'.', 'i', '\271'}, /* dot above */
903 {'.', 'C', '\305'}, {'.', 'c', '\345'},
904 {'.', 'G', '\325'}, {'.', 'g', '\365'},
905 /* iso-8859-4 uses this */
906 {',', 'R', '\243'}, {',', 'r', '\263'}, /* cedilla */
907 {',', 'L', '\246'}, {',', 'l', '\266'},
908 {',', 'G', '\253'}, {',', 'g', '\273'},
909 {',', 'N', '\321'}, {',', 'n', '\361'},
910 {',', 'K', '\323'}, {',', 'k', '\363'},
911 {'~', 'I', '\245'}, {'~', 'i', '\265'}, /* tilde */
912 {'-', 'E', '\252'}, {'-', 'e', '\272'}, /* macron */
913 {'-', 'A', '\300'}, {'-', 'a', '\340'},
914 {'-', 'I', '\317'}, {'-', 'i', '\357'},
915 {'-', 'O', '\322'}, {'-', 'o', '\362'},
916 {'-', 'U', '\336'}, {'-', 'u', '\376'},
917 {'/', 'T', '\254'}, {'/', 't', '\274'}, /* slash */
918 {'.', 'E', '\314'}, {'.', 'e', '\344'}, /* dot above */
919 {';', 'I', '\307'}, {';', 'i', '\347'}, /* ogonek */
920 {';', 'U', '\331'}, {';', 'u', '\371'},
921 /* iso-8859-9 uses this */
922 /* iso-8859-9 has really bad choosen G( S, and I. as they collide
923 * whith the same letters on other iso-8859-x (that is they are on
924 * different places :-( ), if you use turkish uncomment these and
925 * comment out the lines in iso-8859-2 and iso-8859-3 sections
926 * FIXME: should be dynamic according to chosen language
927 * if/when Wine has turkish support.
929 /* collision whith G( from iso-8859-3 !!! */
930 /* {'(', 'G', '\320'}, {'(', 'g', '\360'}, */ /* breve */
931 /* collision whith S, from iso-8859-2 !!! */
932 /* {',', 'S', '\336'}, {',', 's', '\376'}, */ /* cedilla */
933 /* collision whith I. from iso-8859-3 !!! */
934 /* {'.', 'I', '\335'}, {'.', 'i', '\375'}, */ /* dot above */
938 /***********************************************************************
939 * TranslateMessage (USER32.@)
941 * Implementation of TranslateMessage.
943 * TranslateMessage translates virtual-key messages into character-messages,
944 * as follows :
945 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
946 * ditto replacing WM_* with WM_SYS*
947 * This produces WM_CHAR messages only for keys mapped to ASCII characters
948 * by the keyboard driver.
950 * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
951 * return value is nonzero, regardless of the translation.
954 BOOL WINAPI TranslateMessage( const MSG *msg )
956 static int dead_char;
957 UINT message;
958 WCHAR wp[2];
959 BOOL rc = FALSE;
961 if (msg->message >= WM_KEYFIRST && msg->message <= WM_KEYLAST)
963 TRACE_(key)("(%s, %04X, %08lX)\n",
964 SPY_GetMsgName(msg->message, msg->hwnd), msg->wParam, msg->lParam );
966 /* Return code must be TRUE no matter what! */
967 rc = TRUE;
970 if ((msg->message != WM_KEYDOWN) && (msg->message != WM_SYSKEYDOWN)) return rc;
972 TRACE_(key)("Translating key %s (%04x), scancode %02x\n",
973 SPY_GetVKeyName(msg->wParam), msg->wParam, LOBYTE(HIWORD(msg->lParam)));
975 /* FIXME : should handle ToUnicode yielding 2 */
976 switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), QueueKeyStateTable, wp, 2, 0))
978 case 1:
979 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
980 /* Should dead chars handling go in ToAscii ? */
981 if (dead_char)
983 int i;
985 if (wp[0] == ' ') wp[0] = dead_char;
986 if (dead_char == 0xa2) dead_char = '(';
987 else if (dead_char == 0xa8) dead_char = '"';
988 else if (dead_char == 0xb2) dead_char = ';';
989 else if (dead_char == 0xb4) dead_char = '\'';
990 else if (dead_char == 0xb7) dead_char = '<';
991 else if (dead_char == 0xb8) dead_char = ',';
992 else if (dead_char == 0xff) dead_char = '.';
993 for (i = 0; i < sizeof(accent_chars)/sizeof(accent_chars[0]); i++)
994 if ((accent_chars[i].ac_accent == dead_char) &&
995 (accent_chars[i].ac_char == wp[0]))
997 wp[0] = accent_chars[i].ac_result;
998 break;
1000 dead_char = 0;
1002 TRACE_(key)("1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
1003 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
1004 break;
1006 case -1:
1007 message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
1008 dead_char = wp[0];
1009 TRACE_(key)("-1 -> PostMessage(%s)\n", SPY_GetMsgName(message, msg->hwnd));
1010 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
1011 return TRUE;
1013 return rc;
1017 /***********************************************************************
1018 * DispatchMessageA (USER32.@)
1020 LONG WINAPI DispatchMessageA( const MSG* msg )
1022 WND * wndPtr;
1023 LONG retval;
1024 int painting;
1025 WNDPROC winproc;
1027 /* Process timer messages */
1028 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1030 if (msg->lParam)
1032 /* HOOK_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1034 /* before calling window proc, verify whether timer is still valid;
1035 there's a slim chance that the application kills the timer
1036 between GetMessage and DispatchMessage API calls */
1037 if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (HWINDOWPROC) msg->lParam))
1038 return 0; /* invalid winproc */
1040 return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
1041 msg->message, msg->wParam, GetTickCount() );
1045 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
1047 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1048 return 0;
1050 if (wndPtr == WND_OTHER_PROCESS)
1052 if (IsWindow( msg->hwnd ))
1053 ERR( "cannot dispatch msg to other process window %x\n", msg->hwnd );
1054 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1055 return 0;
1057 if (!(winproc = wndPtr->winproc))
1059 WIN_ReleasePtr( wndPtr );
1060 return 0;
1062 painting = (msg->message == WM_PAINT);
1063 if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1064 WIN_ReleasePtr( wndPtr );
1065 /* hook_CallHooks32A( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1067 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1068 msg->wParam, msg->lParam );
1069 retval = CallWindowProcA( winproc, msg->hwnd, msg->message,
1070 msg->wParam, msg->lParam );
1071 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1072 msg->wParam, msg->lParam );
1074 if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
1076 BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
1077 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1078 WIN_ReleasePtr( wndPtr );
1079 if (validate)
1081 ERR( "BeginPaint not called on WM_PAINT for hwnd %04x!\n", msg->hwnd );
1082 /* Validate the update region to avoid infinite WM_PAINT loop */
1083 RedrawWindow( msg->hwnd, NULL, 0,
1084 RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1087 return retval;
1091 /***********************************************************************
1092 * DispatchMessageW (USER32.@) Process Message
1094 * Process the message specified in the structure *_msg_.
1096 * If the lpMsg parameter points to a WM_TIMER message and the
1097 * parameter of the WM_TIMER message is not NULL, the lParam parameter
1098 * points to the function that is called instead of the window
1099 * procedure.
1101 * The message must be valid.
1103 * RETURNS
1105 * DispatchMessage() returns the result of the window procedure invoked.
1107 * CONFORMANCE
1109 * ECMA-234, Win32
1112 LONG WINAPI DispatchMessageW( const MSG* msg )
1114 WND * wndPtr;
1115 LONG retval;
1116 int painting;
1117 WNDPROC winproc;
1119 /* Process timer messages */
1120 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
1122 if (msg->lParam)
1124 /* HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1126 /* before calling window proc, verify whether timer is still valid;
1127 there's a slim chance that the application kills the timer
1128 between GetMessage and DispatchMessage API calls */
1129 if (!TIMER_IsTimerValid(msg->hwnd, (UINT) msg->wParam, (HWINDOWPROC) msg->lParam))
1130 return 0; /* invalid winproc */
1132 return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
1133 msg->message, msg->wParam, GetTickCount() );
1137 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
1139 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1140 return 0;
1142 if (wndPtr == WND_OTHER_PROCESS)
1144 if (IsWindow( msg->hwnd ))
1145 ERR( "cannot dispatch msg to other process window %x\n", msg->hwnd );
1146 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1147 return 0;
1149 if (!(winproc = wndPtr->winproc))
1151 WIN_ReleasePtr( wndPtr );
1152 return 0;
1154 painting = (msg->message == WM_PAINT);
1155 if (painting) wndPtr->flags |= WIN_NEEDS_BEGINPAINT;
1156 WIN_ReleasePtr( wndPtr );
1157 /* HOOK_CallHooks32W( WH_CALLWNDPROC, HC_ACTION, 0, FIXME ); */
1159 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
1160 msg->wParam, msg->lParam );
1161 retval = CallWindowProcW( winproc, msg->hwnd, msg->message,
1162 msg->wParam, msg->lParam );
1163 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
1164 msg->wParam, msg->lParam );
1166 if (painting && (wndPtr = WIN_GetPtr( msg->hwnd )) && (wndPtr != WND_OTHER_PROCESS))
1168 BOOL validate = ((wndPtr->flags & WIN_NEEDS_BEGINPAINT) && wndPtr->hrgnUpdate);
1169 wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
1170 WIN_ReleasePtr( wndPtr );
1171 if (validate)
1173 ERR( "BeginPaint not called on WM_PAINT for hwnd %04x!\n", msg->hwnd );
1174 /* Validate the update region to avoid infinite WM_PAINT loop */
1175 RedrawWindow( msg->hwnd, NULL, 0,
1176 RDW_NOFRAME | RDW_VALIDATE | RDW_NOCHILDREN | RDW_NOINTERNALPAINT );
1179 return retval;
1183 /***********************************************************************
1184 * RegisterWindowMessage (USER.118)
1185 * RegisterWindowMessageA (USER32.@)
1187 WORD WINAPI RegisterWindowMessageA( LPCSTR str )
1189 TRACE("%s\n", str );
1190 return GlobalAddAtomA( str );
1194 /***********************************************************************
1195 * RegisterWindowMessageW (USER32.@)
1197 WORD WINAPI RegisterWindowMessageW( LPCWSTR str )
1199 TRACE("%p\n", str );
1200 return GlobalAddAtomW( str );
1204 /***********************************************************************
1205 * BroadcastSystemMessage (USER32.@)
1206 * BroadcastSystemMessageA (USER32.@)
1208 LONG WINAPI BroadcastSystemMessage(
1209 DWORD dwFlags,LPDWORD recipients,UINT uMessage,WPARAM wParam,
1210 LPARAM lParam )
1212 if ((*recipients & BSM_APPLICATIONS)||
1213 (*recipients == BSM_ALLCOMPONENTS))
1215 FIXME("(%08lx,%08lx,%08x,%08x,%08lx): semi-stub!\n",
1216 dwFlags,*recipients,uMessage,wParam,lParam);
1217 PostMessageA(HWND_BROADCAST,uMessage,wParam,lParam);
1218 return 1;
1220 else
1222 FIXME("(%08lx,%08lx,%08x,%08x,%08lx): stub!\n",
1223 dwFlags,*recipients,uMessage,wParam,lParam);
1224 return -1;