2 * Window messaging support
4 * Copyright 2001 Alexandre Julliard
5 * Copyright 2008 Maarten Lankhorst
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
29 #define WIN32_NO_STATUS
39 #include "wine/unicode.h"
40 #include "wine/server.h"
41 #include "user_private.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(msg);
47 WINE_DECLARE_DEBUG_CHANNEL(relay);
48 WINE_DECLARE_DEBUG_CHANNEL(key);
50 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
51 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
53 #define MAX_PACK_COUNT 4
54 #define MAX_SENDMSG_RECURSION 64
56 #define SYS_TIMER_RATE 55 /* min. timer rate in ms (actually 54.925)*/
58 /* description of the data fields that need to be packed along with a sent message */
62 const void *data[MAX_PACK_COUNT];
63 size_t size[MAX_PACK_COUNT];
66 /* info about the message currently being received by the current thread */
67 struct received_message_info
69 enum message_type type;
71 UINT flags; /* InSendMessageEx return flags */
74 /* structure to group all parameters for sent messages of the various kinds */
75 struct send_message_info
77 enum message_type type;
83 UINT flags; /* flags for SendMessageTimeout */
84 UINT timeout; /* timeout for SendMessageTimeout */
85 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
86 ULONG_PTR data; /* callback data */
87 enum wm_char_mapping wm_char;
91 /* flag for messages that contain pointers */
92 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
94 #define SET(msg) (1 << ((msg) & 31))
96 static const unsigned int message_pointer_flags[] =
99 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
100 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
102 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
105 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
106 SET(WM_NOTIFY) | SET(WM_HELP),
108 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
110 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
112 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
114 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
116 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO) | SET(SBM_GETSCROLLBARINFO),
122 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
123 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
124 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
128 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
129 SET(LB_DIR) | SET(LB_FINDSTRING) |
130 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
132 SET(LB_FINDSTRINGEXACT),
138 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
140 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
141 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
155 SET(WM_ASKCBFORMATNAME)
158 /* flags for messages that contain Unicode strings */
159 static const unsigned int message_unicode_flags[] =
162 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
163 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
175 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
179 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
183 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
184 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
188 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
189 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
191 SET(LB_FINDSTRINGEXACT),
213 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
216 /* check whether a given message type includes pointers */
217 static inline int is_pointer_message( UINT message )
219 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
220 return (message_pointer_flags[message / 32] & SET(message)) != 0;
223 /* check whether a given message type contains Unicode (or ASCII) chars */
224 static inline int is_unicode_message( UINT message )
226 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
227 return (message_unicode_flags[message / 32] & SET(message)) != 0;
232 /* add a data field to a packed message */
233 static inline void push_data( struct packed_message *data, const void *ptr, size_t size )
235 data->data[data->count] = ptr;
236 data->size[data->count] = size;
240 /* add a string to a packed message */
241 static inline void push_string( struct packed_message *data, LPCWSTR str )
243 push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
246 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
247 static inline void *get_data( void **buffer, size_t size )
250 *buffer = (char *)*buffer + size;
254 /* make sure that the buffer contains a valid null-terminated Unicode string */
255 static inline BOOL check_string( LPCWSTR str, size_t size )
257 for (size /= sizeof(WCHAR); size; size--, str++)
258 if (!*str) return TRUE;
262 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
263 static inline void *get_buffer_space( void **buffer, size_t size )
269 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
270 HeapFree( GetProcessHeap(), 0, *buffer );
272 else ret = HeapAlloc( GetProcessHeap(), 0, size );
278 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
279 static inline BOOL combobox_has_strings( HWND hwnd )
281 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
282 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
285 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
286 static inline BOOL listbox_has_strings( HWND hwnd )
288 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
289 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
292 /* check whether message is in the range of keyboard messages */
293 static inline BOOL is_keyboard_message( UINT message )
295 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
298 /* check whether message is in the range of mouse messages */
299 static inline BOOL is_mouse_message( UINT message )
301 return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
302 (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
305 /* check whether message matches the specified hwnd filter */
306 static inline BOOL check_hwnd_filter( const MSG *msg, HWND hwnd_filter )
308 if (!hwnd_filter || hwnd_filter == GetDesktopWindow()) return TRUE;
309 return (msg->hwnd == hwnd_filter || IsChild( hwnd_filter, msg->hwnd ));
312 /* check for pending WM_CHAR message with DBCS trailing byte */
313 static inline BOOL get_pending_wmchar( MSG *msg, UINT first, UINT last, BOOL remove )
315 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
317 if (!data || !data->get_msg.message) return FALSE;
318 if ((first || last) && (first > WM_CHAR || last < WM_CHAR)) return FALSE;
319 if (!msg) return FALSE;
320 *msg = data->get_msg;
321 if (remove) data->get_msg.message = 0;
325 /***********************************************************************
326 * broadcast_message_callback
328 * Helper callback for broadcasting messages.
330 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
332 struct send_message_info *info = (struct send_message_info *)lparam;
333 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
337 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
338 info->flags, info->timeout, NULL );
341 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
342 info->flags, info->timeout, NULL );
345 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
348 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
349 info->callback, info->data );
352 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
355 ERR( "bad type %d\n", info->type );
362 /***********************************************************************
365 * Convert the wparam of an ASCII message to Unicode.
367 BOOL map_wparam_AtoW( UINT message, WPARAM *wparam, enum wm_char_mapping mapping )
376 /* WM_CHAR is magic: a DBCS char can be sent/posted as two consecutive WM_CHAR
377 * messages, in which case the first char is stored, and the conversion
378 * to Unicode only takes place once the second char is sent/posted.
380 if (mapping != WMCHAR_MAP_NOMAPPING)
382 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
383 BYTE low = LOBYTE(*wparam);
388 ch[1] = HIBYTE(*wparam);
389 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
390 TRACE( "map %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
391 if (data) data->lead_byte[mapping] = 0;
393 else if (data && data->lead_byte[mapping])
395 ch[0] = data->lead_byte[mapping];
397 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
398 TRACE( "map stored %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
399 data->lead_byte[mapping] = 0;
401 else if (!IsDBCSLeadByte( low ))
404 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 1 );
405 TRACE( "map %02x -> %04x\n", (BYTE)ch[0], wch[0] );
406 if (data) data->lead_byte[mapping] = 0;
408 else /* store it and wait for trail byte */
412 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) )))
414 get_user_thread_info()->wmchar_data = data;
416 TRACE( "storing lead byte %02x mapping %u\n", low, mapping );
417 data->lead_byte[mapping] = low;
420 *wparam = MAKEWPARAM(wch[0], wch[1]);
423 /* else fall through */
425 case EM_SETPASSWORDCHAR:
430 ch[0] = LOBYTE(*wparam);
431 ch[1] = HIBYTE(*wparam);
432 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
433 *wparam = MAKEWPARAM(wch[0], wch[1]);
436 ch[0] = HIBYTE(*wparam);
437 ch[1] = LOBYTE(*wparam);
438 if (ch[0]) RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch, 2 );
439 else RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch + 1, 1 );
440 *wparam = MAKEWPARAM(wch[0], HIWORD(*wparam));
447 /***********************************************************************
450 * Convert the wparam of a Unicode message to ASCII.
452 static void map_wparam_WtoA( MSG *msg, BOOL remove )
461 if (!HIWORD(msg->wParam))
463 wch[0] = LOWORD(msg->wParam);
465 RtlUnicodeToMultiByteN( (LPSTR)ch, 2, &len, wch, sizeof(wch[0]) );
466 if (len == 2) /* DBCS char */
468 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
471 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) ))) return;
472 get_user_thread_info()->wmchar_data = data;
476 data->get_msg = *msg;
477 data->get_msg.wParam = ch[1];
483 /* else fall through */
485 case EM_SETPASSWORDCHAR:
490 wch[0] = LOWORD(msg->wParam);
491 wch[1] = HIWORD(msg->wParam);
493 RtlUnicodeToMultiByteN( (LPSTR)ch, 2, NULL, wch, sizeof(wch) );
494 msg->wParam = MAKEWPARAM( ch[0] | (ch[1] << 8), 0 );
497 wch[0] = LOWORD(msg->wParam);
499 RtlUnicodeToMultiByteN( (LPSTR)ch, 2, &len, wch, sizeof(wch[0]) );
501 msg->wParam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(msg->wParam) );
503 msg->wParam = MAKEWPARAM( ch[0], HIWORD(msg->wParam) );
509 /***********************************************************************
512 * Pack a message for sending to another process.
513 * Return the size of the data we expect in the message reply.
514 * Set data->count to -1 if there is an error.
516 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
517 struct packed_message *data )
525 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
526 push_data( data, cs, sizeof(*cs) );
527 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
528 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
532 case WM_ASKCBFORMATNAME:
533 return wparam * sizeof(WCHAR);
534 case WM_WININICHANGE:
535 if (lparam) push_string(data, (LPWSTR)lparam );
538 case WM_DEVMODECHANGE:
543 push_string( data, (LPWSTR)lparam );
545 case WM_GETMINMAXINFO:
546 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
547 return sizeof(MINMAXINFO);
549 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
552 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
553 return sizeof(MEASUREITEMSTRUCT);
555 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
558 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
560 case WM_WINDOWPOSCHANGING:
561 case WM_WINDOWPOSCHANGED:
562 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
563 return sizeof(WINDOWPOS);
566 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
567 push_data( data, cp, sizeof(*cp) );
568 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
572 /* WM_NOTIFY cannot be sent across processes (MSDN) */
576 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
578 case WM_STYLECHANGING:
579 case WM_STYLECHANGED:
580 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
585 push_data( data, (RECT *)lparam, sizeof(RECT) );
590 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
591 push_data( data, nc, sizeof(*nc) );
592 push_data( data, nc->lppos, sizeof(*nc->lppos) );
593 return sizeof(*nc) + sizeof(*nc->lppos);
596 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
598 case SBM_SETSCROLLINFO:
599 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
601 case SBM_GETSCROLLINFO:
602 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
603 return sizeof(SCROLLINFO);
604 case SBM_GETSCROLLBARINFO:
606 const SCROLLBARINFO *info = (const SCROLLBARINFO *)lparam;
607 size_t size = min( info->cbSize, sizeof(SCROLLBARINFO) );
608 push_data( data, info, size );
616 if (wparam) size += sizeof(DWORD);
617 if (lparam) size += sizeof(DWORD);
622 case CB_GETDROPPEDCONTROLRECT:
626 push_data( data, (RECT *)lparam, sizeof(RECT) );
630 WORD *pw = (WORD *)lparam;
631 push_data( data, pw, sizeof(*pw) );
632 return *pw * sizeof(WCHAR);
636 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
639 case CB_INSERTSTRING:
641 case CB_FINDSTRINGEXACT:
642 case CB_SELECTSTRING:
643 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
646 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
647 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
649 case LB_INSERTSTRING:
651 case LB_FINDSTRINGEXACT:
652 case LB_SELECTSTRING:
653 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
656 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
657 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
659 return wparam * sizeof(UINT);
661 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
662 return sizeof(MDINEXTMENU);
665 push_data( data, (RECT *)lparam, sizeof(RECT) );
669 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
670 push_data( data, cs, sizeof(*cs) );
671 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
672 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
675 case WM_MDIGETACTIVE:
676 if (lparam) return sizeof(BOOL);
678 case WM_DEVICECHANGE:
680 DEV_BROADCAST_HDR *header = (DEV_BROADCAST_HDR *)lparam;
681 push_data( data, header, header->dbch_size );
684 case WM_WINE_SETWINDOWPOS:
685 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
687 case WM_WINE_KEYBOARD_LL_HOOK:
689 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
690 push_data( data, h_extra, sizeof(*h_extra) );
691 push_data( data, (LPVOID)h_extra->lparam, sizeof(KBDLLHOOKSTRUCT) );
694 case WM_WINE_MOUSE_LL_HOOK:
696 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
697 push_data( data, h_extra, sizeof(*h_extra) );
698 push_data( data, (LPVOID)h_extra->lparam, sizeof(MSLLHOOKSTRUCT) );
702 if (wparam <= 1) return 0;
703 FIXME( "WM_NCPAINT hdc packing not supported yet\n" );
707 if (!wparam) return 0;
710 /* these contain an HFONT */
713 /* these contain an HDC */
715 case WM_ICONERASEBKGND:
716 case WM_CTLCOLORMSGBOX:
717 case WM_CTLCOLOREDIT:
718 case WM_CTLCOLORLISTBOX:
721 case WM_CTLCOLORSCROLLBAR:
722 case WM_CTLCOLORSTATIC:
725 /* these contain an HGLOBAL */
726 case WM_PAINTCLIPBOARD:
727 case WM_SIZECLIPBOARD:
728 /* these contain HICON */
731 case WM_QUERYDRAGICON:
732 case WM_QUERYPARKICON:
733 /* these contain pointers */
735 case WM_QUERYDROPOBJECT:
739 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
747 /***********************************************************************
750 * Unpack a message received from another process.
752 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
753 void **buffer, size_t size )
762 CREATESTRUCTW *cs = *buffer;
763 WCHAR *str = (WCHAR *)(cs + 1);
764 if (size < sizeof(*cs)) return FALSE;
766 if (HIWORD(cs->lpszName))
768 if (!check_string( str, size )) return FALSE;
770 size -= (strlenW(str) + 1) * sizeof(WCHAR);
771 str += strlenW(str) + 1;
773 if (HIWORD(cs->lpszClass))
775 if (!check_string( str, size )) return FALSE;
781 case WM_ASKCBFORMATNAME:
782 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
784 case WM_WININICHANGE:
785 if (!*lparam) return TRUE;
788 case WM_DEVMODECHANGE:
793 if (!check_string( *buffer, size )) return FALSE;
795 case WM_GETMINMAXINFO:
796 minsize = sizeof(MINMAXINFO);
799 minsize = sizeof(DRAWITEMSTRUCT);
802 minsize = sizeof(MEASUREITEMSTRUCT);
805 minsize = sizeof(DELETEITEMSTRUCT);
808 minsize = sizeof(COMPAREITEMSTRUCT);
810 case WM_WINDOWPOSCHANGING:
811 case WM_WINDOWPOSCHANGED:
812 case WM_WINE_SETWINDOWPOS:
813 minsize = sizeof(WINDOWPOS);
817 COPYDATASTRUCT *cp = *buffer;
818 if (size < sizeof(*cp)) return FALSE;
821 minsize = sizeof(*cp) + cp->cbData;
827 /* WM_NOTIFY cannot be sent across processes (MSDN) */
830 minsize = sizeof(HELPINFO);
832 case WM_STYLECHANGING:
833 case WM_STYLECHANGED:
834 minsize = sizeof(STYLESTRUCT);
837 if (!*wparam) minsize = sizeof(RECT);
840 NCCALCSIZE_PARAMS *nc = *buffer;
841 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
842 nc->lppos = (WINDOWPOS *)(nc + 1);
846 if (!*lparam) return TRUE;
847 minsize = sizeof(MSG);
849 case SBM_SETSCROLLINFO:
850 minsize = sizeof(SCROLLINFO);
852 case SBM_GETSCROLLINFO:
853 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
855 case SBM_GETSCROLLBARINFO:
856 if (!get_buffer_space( buffer, sizeof(SCROLLBARINFO ))) return FALSE;
861 if (*wparam || *lparam)
863 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
864 if (*wparam) *wparam = (WPARAM)*buffer;
865 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
870 case CB_GETDROPPEDCONTROLRECT:
871 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
875 minsize = sizeof(RECT);
880 if (size < sizeof(WORD)) return FALSE;
881 len = *(WORD *)*buffer;
882 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
883 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
888 if (!*wparam) return TRUE;
889 minsize = *wparam * sizeof(UINT);
892 case CB_INSERTSTRING:
894 case CB_FINDSTRINGEXACT:
895 case CB_SELECTSTRING:
897 case LB_INSERTSTRING:
899 case LB_FINDSTRINGEXACT:
900 case LB_SELECTSTRING:
901 if (!*buffer) return TRUE;
902 if (!check_string( *buffer, size )) return FALSE;
906 size = sizeof(ULONG_PTR);
907 if (combobox_has_strings( hwnd ))
908 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
909 if (!get_buffer_space( buffer, size )) return FALSE;
914 size = sizeof(ULONG_PTR);
915 if (listbox_has_strings( hwnd ))
916 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
917 if (!get_buffer_space( buffer, size )) return FALSE;
921 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
924 minsize = sizeof(MDINEXTMENU);
925 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
929 minsize = sizeof(RECT);
930 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
934 MDICREATESTRUCTW *cs = *buffer;
935 WCHAR *str = (WCHAR *)(cs + 1);
936 if (size < sizeof(*cs)) return FALSE;
938 if (HIWORD(cs->szTitle))
940 if (!check_string( str, size )) return FALSE;
942 size -= (strlenW(str) + 1) * sizeof(WCHAR);
943 str += strlenW(str) + 1;
945 if (HIWORD(cs->szClass))
947 if (!check_string( str, size )) return FALSE;
952 case WM_MDIGETACTIVE:
953 if (!*lparam) return TRUE;
954 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
956 case WM_DEVICECHANGE:
957 minsize = sizeof(DEV_BROADCAST_HDR);
959 case WM_WINE_KEYBOARD_LL_HOOK:
960 case WM_WINE_MOUSE_LL_HOOK:
962 struct hook_extra_info *h_extra = (struct hook_extra_info *)*buffer;
964 minsize = sizeof(struct hook_extra_info) +
965 (message == WM_WINE_KEYBOARD_LL_HOOK ? sizeof(KBDLLHOOKSTRUCT)
966 : sizeof(MSLLHOOKSTRUCT));
967 if (size < minsize) return FALSE;
968 h_extra->lparam = (LPARAM)(h_extra + 1);
972 if (*wparam <= 1) return TRUE;
973 FIXME( "WM_NCPAINT hdc unpacking not supported\n" );
976 if (!*wparam) return TRUE;
979 /* these contain an HFONT */
982 /* these contain an HDC */
984 case WM_ICONERASEBKGND:
985 case WM_CTLCOLORMSGBOX:
986 case WM_CTLCOLOREDIT:
987 case WM_CTLCOLORLISTBOX:
990 case WM_CTLCOLORSCROLLBAR:
991 case WM_CTLCOLORSTATIC:
994 /* these contain an HGLOBAL */
995 case WM_PAINTCLIPBOARD:
996 case WM_SIZECLIPBOARD:
997 /* these contain HICON */
1000 case WM_QUERYDRAGICON:
1001 case WM_QUERYPARKICON:
1002 /* these contain pointers */
1004 case WM_QUERYDROPOBJECT:
1008 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
1012 return TRUE; /* message doesn't need any unpacking */
1015 /* default exit for most messages: check minsize and store buffer in lparam */
1016 if (size < minsize) return FALSE;
1017 *lparam = (LPARAM)*buffer;
1022 /***********************************************************************
1025 * Pack a reply to a message for sending to another process.
1027 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1028 LRESULT res, struct packed_message *data )
1035 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
1040 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
1042 case WM_GETMINMAXINFO:
1043 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
1045 case WM_MEASUREITEM:
1046 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
1048 case WM_WINDOWPOSCHANGING:
1049 case WM_WINDOWPOSCHANGED:
1050 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
1053 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
1055 case SBM_GETSCROLLINFO:
1056 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
1059 case LB_GETITEMRECT:
1060 case CB_GETDROPPEDCONTROLRECT:
1063 push_data( data, (RECT *)lparam, sizeof(RECT) );
1067 WORD *ptr = (WORD *)lparam;
1068 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
1071 case LB_GETSELITEMS:
1072 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
1074 case WM_MDIGETACTIVE:
1075 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
1079 push_data( data, (RECT *)lparam, sizeof(RECT) );
1082 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1083 push_data( data, nc, sizeof(*nc) );
1084 push_data( data, nc->lppos, sizeof(*nc->lppos) );
1090 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
1091 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
1094 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
1097 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
1099 case WM_ASKCBFORMATNAME:
1100 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
1106 /***********************************************************************
1109 * Unpack a message reply received from another process.
1111 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1112 void *buffer, size_t size )
1119 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
1120 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
1121 memcpy( cs, buffer, min( sizeof(*cs), size ));
1122 cs->lpszName = name; /* restore the original pointers */
1123 cs->lpszClass = class;
1127 case WM_ASKCBFORMATNAME:
1128 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
1130 case WM_GETMINMAXINFO:
1131 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
1133 case WM_MEASUREITEM:
1134 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
1136 case WM_WINDOWPOSCHANGING:
1137 case WM_WINDOWPOSCHANGED:
1138 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
1141 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
1143 case SBM_GETSCROLLINFO:
1144 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
1146 case SBM_GETSCROLLBARINFO:
1147 memcpy( (SCROLLBARINFO *)lparam, buffer, min( sizeof(SCROLLBARINFO), size ));
1150 case CB_GETDROPPEDCONTROLRECT:
1151 case LB_GETITEMRECT:
1154 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1157 size = min( size, (size_t)*(WORD *)lparam );
1158 memcpy( (WCHAR *)lparam, buffer, size );
1160 case LB_GETSELITEMS:
1161 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
1165 memcpy( (WCHAR *)lparam, buffer, size );
1168 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
1170 case WM_MDIGETACTIVE:
1171 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
1175 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1178 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1179 WINDOWPOS *wp = nc->lppos;
1180 memcpy( nc, buffer, min( sizeof(*nc), size ));
1181 if (size > sizeof(*nc))
1183 size -= sizeof(*nc);
1184 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
1186 nc->lppos = wp; /* restore the original pointer */
1194 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1195 if (size <= sizeof(DWORD)) break;
1196 size -= sizeof(DWORD);
1197 buffer = (DWORD *)buffer + 1;
1199 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1203 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
1204 LPCWSTR title = cs->szTitle, class = cs->szClass;
1205 memcpy( cs, buffer, min( sizeof(*cs), size ));
1206 cs->szTitle = title; /* restore the original pointers */
1207 cs->szClass = class;
1211 ERR( "should not happen: unexpected message %x\n", message );
1217 /***********************************************************************
1220 * Send a reply to a sent message.
1222 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1224 struct packed_message data;
1225 int i, replied = info->flags & ISMEX_REPLIED;
1227 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1228 if (!remove && replied) return; /* replied already */
1231 info->flags |= ISMEX_REPLIED;
1233 if (info->type == MSG_OTHER_PROCESS && !replied)
1235 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1236 info->msg.lParam, result, &data );
1239 SERVER_START_REQ( reply_message )
1241 req->result = result;
1242 req->remove = remove;
1243 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1244 wine_server_call( req );
1250 /***********************************************************************
1251 * handle_internal_message
1253 * Handle an internal Wine message instead of calling the window proc.
1255 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1259 case WM_WINE_DESTROYWINDOW:
1260 return WIN_DestroyWindow( hwnd );
1261 case WM_WINE_SETWINDOWPOS:
1262 if (hwnd == GetDesktopWindow()) return 0;
1263 return USER_SetWindowPos( (WINDOWPOS *)lparam );
1264 case WM_WINE_SHOWWINDOW:
1265 if (hwnd == GetDesktopWindow()) return 0;
1266 return ShowWindow( hwnd, wparam );
1267 case WM_WINE_SETPARENT:
1268 if (hwnd == GetDesktopWindow()) return 0;
1269 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1270 case WM_WINE_SETWINDOWLONG:
1271 return WIN_SetWindowLong( hwnd, (short)LOWORD(wparam), HIWORD(wparam), lparam, TRUE );
1272 case WM_WINE_ENABLEWINDOW:
1273 if (hwnd == GetDesktopWindow()) return 0;
1274 return EnableWindow( hwnd, wparam );
1275 case WM_WINE_SETACTIVEWINDOW:
1276 if (hwnd == GetDesktopWindow()) return 0;
1277 return (LRESULT)SetActiveWindow( (HWND)wparam );
1278 case WM_WINE_KEYBOARD_LL_HOOK:
1279 case WM_WINE_MOUSE_LL_HOOK:
1281 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
1283 return call_current_hook( h_extra->handle, HC_ACTION, wparam, h_extra->lparam );
1286 if (msg >= WM_WINE_FIRST_DRIVER_MSG && msg <= WM_WINE_LAST_DRIVER_MSG)
1287 return USER_Driver->pWindowMessage( hwnd, msg, wparam, lparam );
1288 FIXME( "unknown internal message %x\n", msg );
1293 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1294 * to the memory handle, we keep track (in the server side) of all pairs of handle
1295 * used (the client passes its value and the content of the memory handle), and
1296 * the server stored both values (the client, and the local one, created after the
1297 * content). When a ACK message is generated, the list of pair is searched for a
1298 * matching pair, so that the client memory handle can be returned.
1301 HGLOBAL client_hMem;
1302 HGLOBAL server_hMem;
1305 static struct DDE_pair* dde_pairs;
1306 static int dde_num_alloc;
1307 static int dde_num_used;
1309 static CRITICAL_SECTION dde_crst;
1310 static CRITICAL_SECTION_DEBUG critsect_debug =
1313 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
1314 0, 0, { (DWORD_PTR)(__FILE__ ": dde_crst") }
1316 static CRITICAL_SECTION dde_crst = { &critsect_debug, -1, 0, 0, 0, 0 };
1318 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1323 EnterCriticalSection(&dde_crst);
1325 /* now remember the pair of hMem on both sides */
1326 if (dde_num_used == dde_num_alloc)
1328 struct DDE_pair* tmp;
1330 tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1331 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1333 tmp = HeapAlloc( GetProcessHeap(), 0,
1334 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1338 LeaveCriticalSection(&dde_crst);
1342 /* zero out newly allocated part */
1343 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1344 dde_num_alloc += GROWBY;
1347 for (i = 0; i < dde_num_alloc; i++)
1349 if (dde_pairs[i].server_hMem == 0)
1351 dde_pairs[i].client_hMem = chm;
1352 dde_pairs[i].server_hMem = shm;
1357 LeaveCriticalSection(&dde_crst);
1361 static HGLOBAL dde_get_pair(HGLOBAL shm)
1366 EnterCriticalSection(&dde_crst);
1367 for (i = 0; i < dde_num_alloc; i++)
1369 if (dde_pairs[i].server_hMem == shm)
1371 /* free this pair */
1372 dde_pairs[i].server_hMem = 0;
1374 ret = dde_pairs[i].client_hMem;
1378 LeaveCriticalSection(&dde_crst);
1382 /***********************************************************************
1385 * Post a DDE message
1387 static BOOL post_dde_message( struct packed_message *data, const struct send_message_info *info )
1391 UINT_PTR uiLo, uiHi;
1393 HGLOBAL hunlock = 0;
1397 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1403 /* DDE messages which don't require packing are:
1412 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1413 HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1416 /* send back the value of h on the other side */
1417 push_data( data, &h, sizeof(HGLOBAL) );
1419 TRACE( "send dde-ack %lx %08lx => %p\n", uiLo, uiHi, h );
1424 /* uiHi should contain either an atom or 0 */
1425 TRACE( "send dde-ack %lx atom=%lx\n", uiLo, uiHi );
1426 lp = MAKELONG( uiLo, uiHi );
1435 size = GlobalSize( (HGLOBAL)uiLo ) ;
1436 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1437 (info->msg == WM_DDE_DATA && size < sizeof(DDEDATA)) ||
1438 (info->msg == WM_DDE_POKE && size < sizeof(DDEPOKE))
1442 else if (info->msg != WM_DDE_DATA) return FALSE;
1447 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1449 DDEDATA *dde_data = (DDEDATA *)ptr;
1450 TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
1451 dde_data->unused, dde_data->fResponse, dde_data->fRelease,
1452 dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
1453 push_data( data, ptr, size );
1454 hunlock = (HGLOBAL)uiLo;
1457 TRACE( "send ddepack %u %lx\n", size, uiHi );
1459 case WM_DDE_EXECUTE:
1462 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1464 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1465 /* so that the other side can send it back on ACK */
1467 hunlock = (HGLOBAL)info->lparam;
1472 SERVER_START_REQ( send_message )
1474 req->id = info->dest_tid;
1475 req->type = info->type;
1477 req->win = info->hwnd;
1478 req->msg = info->msg;
1479 req->wparam = info->wparam;
1481 req->timeout = TIMEOUT_INFINITE;
1482 for (i = 0; i < data->count; i++)
1483 wine_server_add_data( req, data->data[i], data->size[i] );
1484 if ((res = wine_server_call( req )))
1486 if (res == STATUS_INVALID_PARAMETER)
1487 /* FIXME: find a STATUS_ value for this one */
1488 SetLastError( ERROR_INVALID_THREAD_ID );
1490 SetLastError( RtlNtStatusToDosError(res) );
1493 FreeDDElParam(info->msg, info->lparam);
1496 if (hunlock) GlobalUnlock(hunlock);
1501 /***********************************************************************
1502 * unpack_dde_message
1504 * Unpack a posted DDE message received from another process.
1506 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1507 void **buffer, size_t size )
1509 UINT_PTR uiLo, uiHi;
1518 /* hMem is being passed */
1519 if (size != sizeof(HGLOBAL)) return FALSE;
1520 if (!buffer || !*buffer) return FALSE;
1522 memcpy( &hMem, *buffer, size );
1523 uiHi = (UINT_PTR)hMem;
1524 TRACE("recv dde-ack %lx mem=%lx[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1528 uiLo = LOWORD( *lparam );
1529 uiHi = HIWORD( *lparam );
1530 TRACE("recv dde-ack %lx atom=%lx\n", uiLo, uiHi);
1532 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1537 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1541 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size )))
1543 if ((ptr = GlobalLock( hMem )))
1545 memcpy( ptr, *buffer, size );
1546 GlobalUnlock( hMem );
1554 uiLo = (UINT_PTR)hMem;
1556 *lparam = PackDDElParam( message, uiLo, uiHi );
1558 case WM_DDE_EXECUTE:
1561 if (!buffer || !*buffer) return FALSE;
1562 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size ))) return FALSE;
1563 if ((ptr = GlobalLock( hMem )))
1565 memcpy( ptr, *buffer, size );
1566 GlobalUnlock( hMem );
1567 TRACE( "exec: pairing c=%08lx s=%p\n", *lparam, hMem );
1568 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1579 } else return FALSE;
1580 *lparam = (LPARAM)hMem;
1586 /***********************************************************************
1589 * Call a window procedure and the corresponding hooks.
1591 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1592 BOOL unicode, BOOL same_thread, enum wm_char_mapping mapping )
1594 struct user_thread_info *thread_info = get_user_thread_info();
1597 CWPRETSTRUCT cwpret;
1599 if (thread_info->recursion_count > MAX_SENDMSG_RECURSION) return 0;
1600 thread_info->recursion_count++;
1602 if (msg & 0x80000000)
1604 result = handle_internal_message( hwnd, msg, wparam, lparam );
1608 /* first the WH_CALLWNDPROC hook */
1609 hwnd = WIN_GetFullHandle( hwnd );
1610 cwp.lParam = lparam;
1611 cwp.wParam = wparam;
1614 HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1616 /* now call the window procedure */
1617 if (!WINPROC_call_window( hwnd, msg, wparam, lparam, &result, unicode, mapping )) goto done;
1619 /* and finally the WH_CALLWNDPROCRET hook */
1620 cwpret.lResult = result;
1621 cwpret.lParam = lparam;
1622 cwpret.wParam = wparam;
1623 cwpret.message = msg;
1625 HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1627 thread_info->recursion_count--;
1632 /***********************************************************************
1633 * send_parent_notify
1635 * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
1636 * the window has the WS_EX_NOPARENTNOTIFY style.
1638 static void send_parent_notify( HWND hwnd, WORD event, WORD idChild, POINT pt )
1640 /* pt has to be in the client coordinates of the parent window */
1641 MapWindowPoints( 0, hwnd, &pt, 1 );
1646 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)) break;
1647 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
1648 if (!(parent = GetParent(hwnd))) break;
1649 if (parent == GetDesktopWindow()) break;
1650 MapWindowPoints( hwnd, parent, &pt, 1 );
1652 SendMessageW( hwnd, WM_PARENTNOTIFY,
1653 MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
1658 /***********************************************************************
1659 * accept_hardware_message
1661 * Tell the server we have passed the message to the app
1662 * (even though we may end up dropping it later on)
1664 static void accept_hardware_message( UINT hw_id, BOOL remove, HWND new_hwnd )
1666 SERVER_START_REQ( accept_hardware_message )
1669 req->remove = remove;
1670 req->new_win = new_hwnd;
1671 if (wine_server_call( req ))
1672 FIXME("Failed to reply to MSG_HARDWARE message. Message may not be removed from queue.\n");
1678 /***********************************************************************
1679 * process_keyboard_message
1681 * returns TRUE if the contents of 'msg' should be passed to the application
1683 static BOOL process_keyboard_message( MSG *msg, UINT hw_id, HWND hwnd_filter,
1684 UINT first, UINT last, BOOL remove )
1688 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN ||
1689 msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP)
1690 switch (msg->wParam)
1692 case VK_LSHIFT: case VK_RSHIFT:
1693 msg->wParam = VK_SHIFT;
1695 case VK_LCONTROL: case VK_RCONTROL:
1696 msg->wParam = VK_CONTROL;
1698 case VK_LMENU: case VK_RMENU:
1699 msg->wParam = VK_MENU;
1703 /* FIXME: is this really the right place for this hook? */
1704 event.message = msg->message;
1705 event.hwnd = msg->hwnd;
1706 event.time = msg->time;
1707 event.paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
1708 event.paramH = msg->lParam & 0x7FFF;
1709 if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
1710 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1712 /* check message filters */
1713 if (msg->message < first || msg->message > last) return FALSE;
1714 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1718 if((msg->message == WM_KEYDOWN) &&
1719 (msg->hwnd != GetDesktopWindow()))
1721 /* Handle F1 key by sending out WM_HELP message */
1722 if (msg->wParam == VK_F1)
1724 PostMessageW( msg->hwnd, WM_KEYF1, 0, 0 );
1726 else if(msg->wParam >= VK_BROWSER_BACK &&
1727 msg->wParam <= VK_LAUNCH_APP2)
1729 /* FIXME: Process keystate */
1730 SendMessageW(msg->hwnd, WM_APPCOMMAND, (WPARAM)msg->hwnd, MAKELPARAM(0, (FAPPCOMMAND_KEY | (msg->wParam - VK_BROWSER_BACK + 1))));
1733 else if (msg->message == WM_KEYUP)
1735 /* Handle VK_APPS key by posting a WM_CONTEXTMENU message */
1736 if (msg->wParam == VK_APPS && !MENU_IsMenuActive())
1737 PostMessageW(msg->hwnd, WM_CONTEXTMENU, (WPARAM)msg->hwnd, (LPARAM)-1);
1741 if (HOOK_CallHooks( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
1742 LOWORD(msg->wParam), msg->lParam, TRUE ))
1744 /* skip this message */
1745 HOOK_CallHooks( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam, TRUE );
1746 accept_hardware_message( hw_id, TRUE, 0 );
1749 accept_hardware_message( hw_id, remove, 0 );
1754 /***********************************************************************
1755 * process_mouse_message
1757 * returns TRUE if the contents of 'msg' should be passed to the application
1759 static BOOL process_mouse_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
1760 UINT first, UINT last, BOOL remove )
1769 MOUSEHOOKSTRUCT hook;
1772 /* find the window to dispatch this mouse message to */
1774 GetGUIThreadInfo( GetCurrentThreadId(), &info );
1775 if (info.hwndCapture)
1778 msg->hwnd = info.hwndCapture;
1782 msg->hwnd = WINPOS_WindowFromPoint( msg->hwnd, msg->pt, &hittest );
1785 if (!msg->hwnd || !WIN_IsCurrentThread( msg->hwnd ))
1787 accept_hardware_message( hw_id, TRUE, msg->hwnd );
1791 /* FIXME: is this really the right place for this hook? */
1792 event.message = msg->message;
1793 event.time = msg->time;
1794 event.hwnd = msg->hwnd;
1795 event.paramL = msg->pt.x;
1796 event.paramH = msg->pt.y;
1797 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1799 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1802 message = msg->message;
1803 /* Note: windows has no concept of a non-client wheel message */
1804 if (message != WM_MOUSEWHEEL)
1806 if (hittest != HTCLIENT)
1808 message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
1809 msg->wParam = hittest;
1813 /* coordinates don't get translated while tracking a menu */
1814 /* FIXME: should differentiate popups and top-level menus */
1815 if (!(info.flags & GUI_INMENUMODE))
1816 ScreenToClient( msg->hwnd, &pt );
1819 msg->lParam = MAKELONG( pt.x, pt.y );
1821 /* translate double clicks */
1823 if ((msg->message == WM_LBUTTONDOWN) ||
1824 (msg->message == WM_RBUTTONDOWN) ||
1825 (msg->message == WM_MBUTTONDOWN) ||
1826 (msg->message == WM_XBUTTONDOWN))
1828 BOOL update = remove;
1830 /* translate double clicks -
1831 * note that ...MOUSEMOVEs can slip in between
1832 * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
1834 if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
1835 hittest != HTCLIENT ||
1836 (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
1838 if ((msg->message == clk_msg.message) &&
1839 (msg->hwnd == clk_msg.hwnd) &&
1840 (msg->wParam == clk_msg.wParam) &&
1841 (msg->time - clk_msg.time < GetDoubleClickTime()) &&
1842 (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
1843 (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
1845 message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
1848 clk_msg.message = 0; /* clear the double click conditions */
1853 if (message < first || message > last) return FALSE;
1854 /* update static double click conditions */
1855 if (update) clk_msg = *msg;
1859 if (message < first || message > last) return FALSE;
1862 /* message is accepted now (but may still get dropped) */
1865 hook.hwnd = msg->hwnd;
1866 hook.wHitTestCode = hittest;
1867 hook.dwExtraInfo = extra_info;
1868 if (HOOK_CallHooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
1869 message, (LPARAM)&hook, TRUE ))
1872 hook.hwnd = msg->hwnd;
1873 hook.wHitTestCode = hittest;
1874 hook.dwExtraInfo = extra_info;
1875 HOOK_CallHooks( WH_CBT, HCBT_CLICKSKIPPED, message, (LPARAM)&hook, TRUE );
1876 accept_hardware_message( hw_id, TRUE, 0 );
1880 if ((hittest == HTERROR) || (hittest == HTNOWHERE))
1882 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
1883 MAKELONG( hittest, msg->message ));
1884 accept_hardware_message( hw_id, TRUE, 0 );
1888 accept_hardware_message( hw_id, remove, 0 );
1890 if (!remove || info.hwndCapture)
1892 msg->message = message;
1898 if ((msg->message == WM_LBUTTONDOWN) ||
1899 (msg->message == WM_RBUTTONDOWN) ||
1900 (msg->message == WM_MBUTTONDOWN) ||
1901 (msg->message == WM_XBUTTONDOWN))
1903 /* Send the WM_PARENTNOTIFY,
1904 * note that even for double/nonclient clicks
1905 * notification message is still WM_L/M/RBUTTONDOWN.
1907 send_parent_notify( msg->hwnd, msg->message, 0, msg->pt );
1909 /* Activate the window if needed */
1911 if (msg->hwnd != info.hwndActive)
1913 HWND hwndTop = msg->hwnd;
1916 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
1917 hwndTop = GetParent( hwndTop );
1920 if (hwndTop && hwndTop != GetDesktopWindow())
1922 LONG ret = SendMessageW( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
1923 MAKELONG( hittest, msg->message ) );
1926 case MA_NOACTIVATEANDEAT:
1931 case MA_ACTIVATEANDEAT:
1936 if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
1939 WARN( "unknown WM_MOUSEACTIVATE code %d\n", ret );
1946 /* send the WM_SETCURSOR message */
1948 /* Windows sends the normal mouse message as the message parameter
1949 in the WM_SETCURSOR message even if it's non-client mouse message */
1950 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd, MAKELONG( hittest, msg->message ));
1952 msg->message = message;
1957 /***********************************************************************
1958 * process_hardware_message
1960 * Process a hardware message; return TRUE if message should be passed on to the app
1962 static BOOL process_hardware_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
1963 UINT first, UINT last, BOOL remove )
1965 if (is_keyboard_message( msg->message ))
1966 return process_keyboard_message( msg, hw_id, hwnd_filter, first, last, remove );
1968 if (is_mouse_message( msg->message ))
1969 return process_mouse_message( msg, hw_id, extra_info, hwnd_filter, first, last, remove );
1971 ERR( "unknown message type %x\n", msg->message );
1976 /***********************************************************************
1977 * call_sendmsg_callback
1979 * Call the callback function of SendMessageCallback.
1981 static inline void call_sendmsg_callback( SENDASYNCPROC callback, HWND hwnd, UINT msg,
1982 ULONG_PTR data, LRESULT result )
1984 if (!callback) return;
1986 if (TRACE_ON(relay))
1987 DPRINTF( "%04x:Call message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1988 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
1990 callback( hwnd, msg, data, result );
1991 if (TRACE_ON(relay))
1992 DPRINTF( "%04x:Ret message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1993 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
1998 /***********************************************************************
2001 * Peek for a message matching the given parameters. Return FALSE if none available.
2002 * All pending sent messages are processed before returning.
2004 static BOOL peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2007 ULONG_PTR extra_info = 0;
2008 struct user_thread_info *thread_info = get_user_thread_info();
2009 struct received_message_info info, *old_info;
2010 unsigned int wake_mask, changed_mask = HIWORD(flags);
2011 unsigned int hw_id = 0; /* id of previous hardware message */
2013 if (!first && !last) last = ~0;
2014 if (!changed_mask) changed_mask = QS_ALLINPUT;
2015 wake_mask = changed_mask & (QS_SENDMESSAGE | QS_SMRESULT);
2020 void *buffer = NULL;
2021 size_t size = 0, buffer_size = 0;
2023 do /* loop while buffer is too small */
2025 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
2027 SERVER_START_REQ( get_message )
2030 req->get_win = hwnd;
2031 req->get_first = first;
2032 req->get_last = last;
2034 req->wake_mask = wake_mask;
2035 req->changed_mask = changed_mask;
2036 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
2037 if (!(res = wine_server_call( req )))
2039 size = wine_server_reply_size( reply );
2040 info.type = reply->type;
2041 info.msg.hwnd = reply->win;
2042 info.msg.message = reply->msg;
2043 info.msg.wParam = reply->wparam;
2044 info.msg.lParam = reply->lparam;
2045 info.msg.time = reply->time;
2046 info.msg.pt.x = reply->x;
2047 info.msg.pt.y = reply->y;
2048 hw_id = reply->hw_id;
2049 extra_info = reply->info;
2050 thread_info->active_hooks = reply->active_hooks;
2054 HeapFree( GetProcessHeap(), 0, buffer );
2055 buffer_size = reply->total;
2059 } while (res == STATUS_BUFFER_OVERFLOW);
2061 if (res) return FALSE;
2063 TRACE( "got type %d msg %x (%s) hwnd %p wp %lx lp %lx\n",
2064 info.type, info.msg.message,
2065 (info.type == MSG_WINEVENT) ? "MSG_WINEVENT" : SPY_GetMsgName(info.msg.message, info.msg.hwnd),
2066 info.msg.hwnd, info.msg.wParam, info.msg.lParam );
2072 info.flags = ISMEX_SEND;
2075 info.flags = ISMEX_NOTIFY;
2078 info.flags = ISMEX_CALLBACK;
2080 case MSG_CALLBACK_RESULT:
2081 if (size >= sizeof(struct callback_msg_data))
2083 const struct callback_msg_data *data = (const struct callback_msg_data *)buffer;
2084 call_sendmsg_callback( data->callback, info.msg.hwnd,
2085 info.msg.message, data->data, data->result );
2089 if (size >= sizeof(struct winevent_msg_data))
2091 WINEVENTPROC hook_proc;
2092 const struct winevent_msg_data *data = (const struct winevent_msg_data *)buffer;
2094 hook_proc = data->hook_proc;
2095 size -= sizeof(*data);
2098 WCHAR module[MAX_PATH];
2100 size = min( size, (MAX_PATH - 1) * sizeof(WCHAR) );
2101 memcpy( module, buffer, size );
2102 module[size / sizeof(WCHAR)] = 0;
2103 if (!(hook_proc = get_hook_proc( hook_proc, module )))
2105 ERR( "invalid winevent hook module name %s\n", debugstr_w(module) );
2110 if (TRACE_ON(relay))
2111 DPRINTF( "%04x:Call winevent proc %p (hook=%p,event=%x,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04x,time=%x)\n",
2112 GetCurrentThreadId(), hook_proc,
2113 data->hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2114 info.msg.lParam, data->tid, info.msg.time);
2116 hook_proc( data->hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2117 info.msg.lParam, data->tid, info.msg.time );
2119 if (TRACE_ON(relay))
2120 DPRINTF( "%04x:Ret winevent proc %p (hook=%p,event=%x,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04x,time=%x)\n",
2121 GetCurrentThreadId(), hook_proc,
2122 data->hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2123 info.msg.lParam, data->tid, info.msg.time);
2126 case MSG_OTHER_PROCESS:
2127 info.flags = ISMEX_SEND;
2128 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2129 &info.msg.lParam, &buffer, size ))
2132 reply_message( &info, 0, TRUE );
2137 if (!process_hardware_message( &info.msg, hw_id, extra_info,
2138 hwnd, first, last, flags & PM_REMOVE ))
2140 TRACE("dropping msg %x\n", info.msg.message );
2141 goto next; /* ignore it */
2143 thread_info->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
2146 thread_info->GetMessageExtraInfoVal = extra_info;
2147 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
2149 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2150 &info.msg.lParam, &buffer, size ))
2151 goto next; /* ignore it */
2154 HeapFree( GetProcessHeap(), 0, buffer );
2158 /* if we get here, we have a sent message; call the window procedure */
2159 old_info = thread_info->receive_info;
2160 thread_info->receive_info = &info;
2161 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
2162 info.msg.lParam, (info.type != MSG_ASCII), FALSE,
2163 WMCHAR_MAP_RECVMESSAGE );
2164 reply_message( &info, result, TRUE );
2165 thread_info->receive_info = old_info;
2167 /* if some PM_QS* flags were specified, only handle sent messages from now on */
2168 if (HIWORD(flags)) flags = PM_QS_SENDMESSAGE | LOWORD(flags);
2170 HeapFree( GetProcessHeap(), 0, buffer );
2175 /***********************************************************************
2176 * process_sent_messages
2178 * Process all pending sent messages.
2180 static inline void process_sent_messages(void)
2183 peek_message( &msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE );
2187 /***********************************************************************
2188 * get_server_queue_handle
2190 * Get a handle to the server message queue for the current thread.
2192 static HANDLE get_server_queue_handle(void)
2194 struct user_thread_info *thread_info = get_user_thread_info();
2197 if (!(ret = thread_info->server_queue))
2199 SERVER_START_REQ( get_msg_queue )
2201 wine_server_call( req );
2202 ret = reply->handle;
2205 thread_info->server_queue = ret;
2206 if (!ret) ERR( "Cannot get server thread queue\n" );
2212 /***********************************************************************
2213 * wait_message_reply
2215 * Wait until a sent message gets replied to.
2217 static void wait_message_reply( UINT flags )
2219 HANDLE server_queue = get_server_queue_handle();
2223 unsigned int wake_bits = 0, changed_bits = 0;
2226 SERVER_START_REQ( set_queue_mask )
2228 req->wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
2229 req->changed_mask = req->wake_mask;
2231 if (!wine_server_call( req ))
2233 wake_bits = reply->wake_bits;
2234 changed_bits = reply->changed_bits;
2239 if (wake_bits & QS_SMRESULT) return; /* got a result */
2240 if (wake_bits & QS_SENDMESSAGE)
2242 /* Process the sent message immediately */
2243 process_sent_messages();
2247 /* now wait for it */
2249 ReleaseThunkLock( &dwlc );
2250 res = USER_Driver->pMsgWaitForMultipleObjectsEx( 1, &server_queue,
2251 INFINITE, QS_SENDMESSAGE, 0 );
2252 if (dwlc) RestoreThunkLock( dwlc );
2256 /***********************************************************************
2257 * put_message_in_queue
2259 * Put a sent message into the destination queue.
2260 * For inter-process message, reply_size is set to expected size of reply data.
2262 static BOOL put_message_in_queue( const struct send_message_info *info, size_t *reply_size )
2264 struct packed_message data;
2265 message_data_t msg_data;
2268 timeout_t timeout = TIMEOUT_INFINITE;
2270 /* Check for INFINITE timeout for compatibility with Win9x,
2271 * although Windows >= NT does not do so
2273 if (info->type != MSG_NOTIFY &&
2274 info->type != MSG_CALLBACK &&
2275 info->type != MSG_POSTED &&
2277 info->timeout != INFINITE)
2279 /* timeout is signed despite the prototype */
2280 timeout = (timeout_t)max( 0, (int)info->timeout ) * -10000;
2284 if (info->type == MSG_OTHER_PROCESS)
2286 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
2287 if (data.count == -1)
2289 WARN( "cannot pack message %x\n", info->msg );
2293 else if (info->type == MSG_CALLBACK)
2295 msg_data.callback.callback = info->callback;
2296 msg_data.callback.data = info->data;
2297 msg_data.callback.result = 0;
2298 data.data[0] = &msg_data;
2299 data.size[0] = sizeof(msg_data.callback);
2302 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
2304 return post_dde_message( &data, info );
2307 SERVER_START_REQ( send_message )
2309 req->id = info->dest_tid;
2310 req->type = info->type;
2312 req->win = info->hwnd;
2313 req->msg = info->msg;
2314 req->wparam = info->wparam;
2315 req->lparam = info->lparam;
2316 req->timeout = timeout;
2318 if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
2319 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
2320 if ((res = wine_server_call( req )))
2322 if (res == STATUS_INVALID_PARAMETER)
2323 /* FIXME: find a STATUS_ value for this one */
2324 SetLastError( ERROR_INVALID_THREAD_ID );
2326 SetLastError( RtlNtStatusToDosError(res) );
2334 /***********************************************************************
2337 * Retrieve a message reply from the server.
2339 static LRESULT retrieve_reply( const struct send_message_info *info,
2340 size_t reply_size, LRESULT *result )
2343 void *reply_data = NULL;
2347 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
2349 WARN( "no memory for reply, will be truncated\n" );
2353 SERVER_START_REQ( get_message_reply )
2356 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
2357 if (!(status = wine_server_call( req ))) *result = reply->result;
2358 reply_size = wine_server_reply_size( reply );
2361 if (!status && reply_size)
2362 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
2364 HeapFree( GetProcessHeap(), 0, reply_data );
2366 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx got reply %lx (err=%d)\n",
2367 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
2368 info->lparam, *result, status );
2370 /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
2371 if (status) SetLastError( RtlNtStatusToDosError(status) );
2376 /***********************************************************************
2377 * send_inter_thread_message
2379 static LRESULT send_inter_thread_message( const struct send_message_info *info, LRESULT *res_ptr )
2381 size_t reply_size = 0;
2383 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
2384 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
2386 USER_CheckNotLock();
2388 if (!put_message_in_queue( info, &reply_size )) return 0;
2390 /* there's no reply to wait for on notify/callback messages */
2391 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
2393 wait_message_reply( info->flags );
2394 return retrieve_reply( info, reply_size, res_ptr );
2398 /***********************************************************************
2399 * send_inter_thread_callback
2401 static LRESULT send_inter_thread_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
2402 LRESULT *result, void *arg )
2404 struct send_message_info *info = arg;
2409 return send_inter_thread_message( info, result );
2413 /***********************************************************************
2416 * Backend implementation of the various SendMessage functions.
2418 static BOOL send_message( struct send_message_info *info, DWORD_PTR *res_ptr, BOOL unicode )
2424 if (is_broadcast(info->hwnd))
2426 EnumWindows( broadcast_message_callback, (LPARAM)info );
2427 if (res_ptr) *res_ptr = 1;
2431 if (!(info->dest_tid = GetWindowThreadProcessId( info->hwnd, &dest_pid ))) return FALSE;
2433 if (USER_IsExitingThread( info->dest_tid )) return FALSE;
2435 SPY_EnterMessage( SPY_SENDMESSAGE, info->hwnd, info->msg, info->wparam, info->lparam );
2437 if (info->dest_tid == GetCurrentThreadId())
2439 result = call_window_proc( info->hwnd, info->msg, info->wparam, info->lparam,
2440 unicode, TRUE, info->wm_char );
2441 if (info->type == MSG_CALLBACK)
2442 call_sendmsg_callback( info->callback, info->hwnd, info->msg, info->data, result );
2447 if (dest_pid != GetCurrentProcessId() && (info->type == MSG_ASCII || info->type == MSG_UNICODE))
2448 info->type = MSG_OTHER_PROCESS;
2450 /* MSG_ASCII can be sent unconverted except for WM_CHAR; everything else needs to be Unicode */
2451 if (!unicode && is_unicode_message( info->msg ) &&
2452 (info->type != MSG_ASCII || info->msg == WM_CHAR))
2453 ret = WINPROC_CallProcAtoW( send_inter_thread_callback, info->hwnd, info->msg,
2454 info->wparam, info->lparam, &result, info, info->wm_char );
2456 ret = send_inter_thread_message( info, &result );
2459 SPY_ExitMessage( SPY_RESULT_OK, info->hwnd, info->msg, result, info->wparam, info->lparam );
2460 if (ret && res_ptr) *res_ptr = result;
2465 /***********************************************************************
2466 * MSG_SendInternalMessageTimeout
2468 * Same as SendMessageTimeoutW but sends the message to a specific thread
2469 * without requiring a window handle. Only works for internal Wine messages.
2471 LRESULT MSG_SendInternalMessageTimeout( DWORD dest_pid, DWORD dest_tid,
2472 UINT msg, WPARAM wparam, LPARAM lparam,
2473 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2475 struct send_message_info info;
2476 LRESULT ret, result;
2478 assert( msg & 0x80000000 ); /* must be an internal Wine message */
2480 info.type = MSG_UNICODE;
2481 info.dest_tid = dest_tid;
2484 info.wparam = wparam;
2485 info.lparam = lparam;
2487 info.timeout = timeout;
2489 if (USER_IsExitingThread( dest_tid )) return 0;
2491 if (dest_tid == GetCurrentThreadId())
2493 result = handle_internal_message( 0, msg, wparam, lparam );
2498 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
2499 ret = send_inter_thread_message( &info, &result );
2501 if (ret && res_ptr) *res_ptr = result;
2506 /***********************************************************************
2507 * SendMessageTimeoutW (USER32.@)
2509 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2510 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2512 struct send_message_info info;
2514 info.type = MSG_UNICODE;
2517 info.wparam = wparam;
2518 info.lparam = lparam;
2520 info.timeout = timeout;
2522 return send_message( &info, res_ptr, TRUE );
2525 /***********************************************************************
2526 * SendMessageTimeoutA (USER32.@)
2528 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2529 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2531 struct send_message_info info;
2533 info.type = MSG_ASCII;
2536 info.wparam = wparam;
2537 info.lparam = lparam;
2539 info.timeout = timeout;
2540 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
2542 return send_message( &info, res_ptr, FALSE );
2546 /***********************************************************************
2547 * SendMessageW (USER32.@)
2549 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2552 struct send_message_info info;
2554 info.type = MSG_UNICODE;
2557 info.wparam = wparam;
2558 info.lparam = lparam;
2559 info.flags = SMTO_NORMAL;
2562 send_message( &info, &res, TRUE );
2567 /***********************************************************************
2568 * SendMessageA (USER32.@)
2570 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2573 struct send_message_info info;
2575 info.type = MSG_ASCII;
2578 info.wparam = wparam;
2579 info.lparam = lparam;
2580 info.flags = SMTO_NORMAL;
2582 info.wm_char = WMCHAR_MAP_SENDMESSAGE;
2584 send_message( &info, &res, FALSE );
2589 /***********************************************************************
2590 * SendNotifyMessageA (USER32.@)
2592 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2594 struct send_message_info info;
2596 if (is_pointer_message(msg))
2598 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2602 info.type = MSG_NOTIFY;
2605 info.wparam = wparam;
2606 info.lparam = lparam;
2608 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
2610 return send_message( &info, NULL, FALSE );
2614 /***********************************************************************
2615 * SendNotifyMessageW (USER32.@)
2617 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2619 struct send_message_info info;
2621 if (is_pointer_message(msg))
2623 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2627 info.type = MSG_NOTIFY;
2630 info.wparam = wparam;
2631 info.lparam = lparam;
2634 return send_message( &info, NULL, TRUE );
2638 /***********************************************************************
2639 * SendMessageCallbackA (USER32.@)
2641 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2642 SENDASYNCPROC callback, ULONG_PTR data )
2644 struct send_message_info info;
2646 if (is_pointer_message(msg))
2648 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2652 info.type = MSG_CALLBACK;
2655 info.wparam = wparam;
2656 info.lparam = lparam;
2657 info.callback = callback;
2660 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
2662 return send_message( &info, NULL, FALSE );
2666 /***********************************************************************
2667 * SendMessageCallbackW (USER32.@)
2669 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2670 SENDASYNCPROC callback, ULONG_PTR data )
2672 struct send_message_info info;
2674 if (is_pointer_message(msg))
2676 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2680 info.type = MSG_CALLBACK;
2683 info.wparam = wparam;
2684 info.lparam = lparam;
2685 info.callback = callback;
2689 return send_message( &info, NULL, TRUE );
2693 /***********************************************************************
2694 * ReplyMessage (USER32.@)
2696 BOOL WINAPI ReplyMessage( LRESULT result )
2698 struct received_message_info *info = get_user_thread_info()->receive_info;
2700 if (!info) return FALSE;
2701 reply_message( info, result, FALSE );
2706 /***********************************************************************
2707 * InSendMessage (USER32.@)
2709 BOOL WINAPI InSendMessage(void)
2711 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
2715 /***********************************************************************
2716 * InSendMessageEx (USER32.@)
2718 DWORD WINAPI InSendMessageEx( LPVOID reserved )
2720 struct received_message_info *info = get_user_thread_info()->receive_info;
2722 if (info) return info->flags;
2723 return ISMEX_NOSEND;
2727 /***********************************************************************
2728 * PostMessageA (USER32.@)
2730 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2732 if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
2733 return PostMessageW( hwnd, msg, wparam, lparam );
2737 /***********************************************************************
2738 * PostMessageW (USER32.@)
2740 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2742 struct send_message_info info;
2744 if (is_pointer_message( msg ))
2746 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2750 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
2751 hwnd, msg, SPY_GetMsgName(msg, hwnd), wparam, lparam );
2753 info.type = MSG_POSTED;
2756 info.wparam = wparam;
2757 info.lparam = lparam;
2760 if (is_broadcast(hwnd))
2762 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2766 if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2768 if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2770 if (USER_IsExitingThread( info.dest_tid )) return TRUE;
2772 return put_message_in_queue( &info, NULL );
2776 /**********************************************************************
2777 * PostThreadMessageA (USER32.@)
2779 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2781 if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
2782 return PostThreadMessageW( thread, msg, wparam, lparam );
2786 /**********************************************************************
2787 * PostThreadMessageW (USER32.@)
2789 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2791 struct send_message_info info;
2793 if (is_pointer_message( msg ))
2795 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2798 if (USER_IsExitingThread( thread )) return TRUE;
2800 info.type = MSG_POSTED;
2801 info.dest_tid = thread;
2804 info.wparam = wparam;
2805 info.lparam = lparam;
2807 return put_message_in_queue( &info, NULL );
2811 /***********************************************************************
2812 * PostQuitMessage (USER32.@)
2814 * Posts a quit message to the current thread's message queue.
2817 * exit_code [I] Exit code to return from message loop.
2823 * This function is not the same as calling:
2824 *|PostThreadMessage(GetCurrentThreadId(), WM_QUIT, exit_code, 0);
2825 * It instead sets a flag in the message queue that signals it to generate
2826 * a WM_QUIT message when there are no other pending sent or posted messages
2829 void WINAPI PostQuitMessage( INT exit_code )
2831 SERVER_START_REQ( post_quit_message )
2833 req->exit_code = exit_code;
2834 wine_server_call( req );
2840 /***********************************************************************
2841 * PeekMessageW (USER32.@)
2843 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2845 struct user_thread_info *thread_info = get_user_thread_info();
2848 USER_CheckNotLock();
2850 /* check for graphics events */
2851 USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
2853 hwnd = WIN_GetFullHandle( hwnd );
2857 if (!peek_message( &msg, hwnd, first, last, flags ))
2859 if (!(flags & PM_NOYIELD))
2862 ReleaseThunkLock(&count);
2864 if (count) RestoreThunkLock(count);
2868 if (msg.message & 0x80000000)
2870 if (!(flags & PM_REMOVE))
2872 /* Have to remove the message explicitly.
2873 Do this before handling it, because the message handler may
2874 call PeekMessage again */
2875 peek_message( &msg, msg.hwnd, msg.message, msg.message, flags | PM_REMOVE );
2877 handle_internal_message( msg.hwnd, msg.message, msg.wParam, msg.lParam );
2882 thread_info->GetMessageTimeVal = msg.time;
2883 msg.pt.x = (short)LOWORD( thread_info->GetMessagePosVal );
2884 msg.pt.y = (short)HIWORD( thread_info->GetMessagePosVal );
2886 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
2888 /* copy back our internal safe copy of message data to msg_out.
2889 * msg_out is a variable from the *program*, so it can't be used
2890 * internally as it can get "corrupted" by our use of SendMessage()
2891 * (back to the program) inside the message handling itself. */
2894 SetLastError( ERROR_NOACCESS );
2902 /***********************************************************************
2903 * PeekMessageA (USER32.@)
2905 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2907 if (get_pending_wmchar( msg, first, last, (flags & PM_REMOVE) )) return TRUE;
2908 if (!PeekMessageW( msg, hwnd, first, last, flags )) return FALSE;
2909 map_wparam_WtoA( msg, (flags & PM_REMOVE) );
2914 /***********************************************************************
2915 * GetMessageW (USER32.@)
2917 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2919 HANDLE server_queue = get_server_queue_handle();
2920 int mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2924 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2925 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2926 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2927 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2928 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2929 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2931 else mask = QS_ALLINPUT;
2933 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE | PM_NOYIELD | (mask << 16) ))
2937 ReleaseThunkLock( &dwlc );
2938 USER_Driver->pMsgWaitForMultipleObjectsEx( 1, &server_queue, INFINITE, mask, 0 );
2939 if (dwlc) RestoreThunkLock( dwlc );
2942 return (msg->message != WM_QUIT);
2946 /***********************************************************************
2947 * GetMessageA (USER32.@)
2949 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2951 if (get_pending_wmchar( msg, first, last, TRUE )) return TRUE;
2952 GetMessageW( msg, hwnd, first, last );
2953 map_wparam_WtoA( msg, TRUE );
2954 return (msg->message != WM_QUIT);
2958 /***********************************************************************
2959 * IsDialogMessageA (USER32.@)
2960 * IsDialogMessage (USER32.@)
2962 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2965 map_wparam_AtoW( msg.message, &msg.wParam, WMCHAR_MAP_NOMAPPING );
2966 return IsDialogMessageW( hwndDlg, &msg );
2970 /***********************************************************************
2971 * TranslateMessage (USER32.@)
2973 * Implementation of TranslateMessage.
2975 * TranslateMessage translates virtual-key messages into character-messages,
2977 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
2978 * ditto replacing WM_* with WM_SYS*
2979 * This produces WM_CHAR messages only for keys mapped to ASCII characters
2980 * by the keyboard driver.
2982 * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
2983 * return value is nonzero, regardless of the translation.
2986 BOOL WINAPI TranslateMessage( const MSG *msg )
2992 if (msg->message < WM_KEYFIRST || msg->message > WM_KEYLAST) return FALSE;
2993 if (msg->message != WM_KEYDOWN && msg->message != WM_SYSKEYDOWN) return TRUE;
2995 TRACE_(key)("Translating key %s (%04lx), scancode %02x\n",
2996 SPY_GetVKeyName(msg->wParam), msg->wParam, LOBYTE(HIWORD(msg->lParam)));
2998 if (ImmProcessKey(msg->hwnd, GetKeyboardLayout(0), msg->wParam, msg->lParam,0))
3001 GetKeyboardState( state );
3002 /* FIXME : should handle ToUnicode yielding 2 */
3003 switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), state, wp, 2, 0))
3006 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
3007 TRACE_(key)("1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
3008 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
3009 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
3013 message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
3014 TRACE_(key)("-1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
3015 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
3016 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
3023 /***********************************************************************
3024 * DispatchMessageA (USER32.@)
3026 * See DispatchMessageW.
3028 LRESULT WINAPI DispatchMessageA( const MSG* msg )
3032 /* Process timer messages */
3033 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3035 if (msg->lParam) return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
3036 msg->message, msg->wParam, GetTickCount() );
3038 if (!msg->hwnd) return 0;
3040 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3041 msg->wParam, msg->lParam );
3043 if (!WINPROC_call_window( msg->hwnd, msg->message, msg->wParam, msg->lParam,
3044 &retval, FALSE, WMCHAR_MAP_DISPATCHMESSAGE ))
3046 if (!IsWindow( msg->hwnd )) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3047 else SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3051 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3052 msg->wParam, msg->lParam );
3054 if (msg->message == WM_PAINT)
3056 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3057 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3058 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3059 DeleteObject( hrgn );
3065 /***********************************************************************
3066 * DispatchMessageW (USER32.@) Process a message
3068 * Process the message specified in the structure *_msg_.
3070 * If the lpMsg parameter points to a WM_TIMER message and the
3071 * parameter of the WM_TIMER message is not NULL, the lParam parameter
3072 * points to the function that is called instead of the window
3075 * The message must be valid.
3079 * DispatchMessage() returns the result of the window procedure invoked.
3086 LRESULT WINAPI DispatchMessageW( const MSG* msg )
3090 /* Process timer messages */
3091 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3093 if (msg->lParam) return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
3094 msg->message, msg->wParam, GetTickCount() );
3096 if (!msg->hwnd) return 0;
3098 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3099 msg->wParam, msg->lParam );
3101 if (!WINPROC_call_window( msg->hwnd, msg->message, msg->wParam, msg->lParam,
3102 &retval, TRUE, WMCHAR_MAP_DISPATCHMESSAGE ))
3104 if (!IsWindow( msg->hwnd )) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3105 else SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3109 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3110 msg->wParam, msg->lParam );
3112 if (msg->message == WM_PAINT)
3114 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3115 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3116 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3117 DeleteObject( hrgn );
3123 /***********************************************************************
3124 * GetMessagePos (USER.119)
3125 * GetMessagePos (USER32.@)
3127 * The GetMessagePos() function returns a long value representing a
3128 * cursor position, in screen coordinates, when the last message
3129 * retrieved by the GetMessage() function occurs. The x-coordinate is
3130 * in the low-order word of the return value, the y-coordinate is in
3131 * the high-order word. The application can use the MAKEPOINT()
3132 * macro to obtain a POINT structure from the return value.
3134 * For the current cursor position, use GetCursorPos().
3138 * Cursor position of last message on success, zero on failure.
3145 DWORD WINAPI GetMessagePos(void)
3147 return get_user_thread_info()->GetMessagePosVal;
3151 /***********************************************************************
3152 * GetMessageTime (USER.120)
3153 * GetMessageTime (USER32.@)
3155 * GetMessageTime() returns the message time for the last message
3156 * retrieved by the function. The time is measured in milliseconds with
3157 * the same offset as GetTickCount().
3159 * Since the tick count wraps, this is only useful for moderately short
3160 * relative time comparisons.
3164 * Time of last message on success, zero on failure.
3166 LONG WINAPI GetMessageTime(void)
3168 return get_user_thread_info()->GetMessageTimeVal;
3172 /***********************************************************************
3173 * GetMessageExtraInfo (USER.288)
3174 * GetMessageExtraInfo (USER32.@)
3176 LPARAM WINAPI GetMessageExtraInfo(void)
3178 return get_user_thread_info()->GetMessageExtraInfoVal;
3182 /***********************************************************************
3183 * SetMessageExtraInfo (USER32.@)
3185 LPARAM WINAPI SetMessageExtraInfo(LPARAM lParam)
3187 struct user_thread_info *thread_info = get_user_thread_info();
3188 LONG old_value = thread_info->GetMessageExtraInfoVal;
3189 thread_info->GetMessageExtraInfoVal = lParam;
3194 /***********************************************************************
3195 * WaitMessage (USER.112) Suspend thread pending messages
3196 * WaitMessage (USER32.@) Suspend thread pending messages
3198 * WaitMessage() suspends a thread until events appear in the thread's
3201 BOOL WINAPI WaitMessage(void)
3203 return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
3207 /***********************************************************************
3208 * MsgWaitForMultipleObjectsEx (USER32.@)
3210 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
3211 DWORD timeout, DWORD mask, DWORD flags )
3213 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
3216 if (count > MAXIMUM_WAIT_OBJECTS-1)
3218 SetLastError( ERROR_INVALID_PARAMETER );
3222 /* set the queue mask */
3223 SERVER_START_REQ( set_queue_mask )
3225 req->wake_mask = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
3226 req->changed_mask = mask;
3228 wine_server_call( req );
3232 /* add the queue to the handle list */
3233 for (i = 0; i < count; i++) handles[i] = pHandles[i];
3234 handles[count] = get_server_queue_handle();
3236 ReleaseThunkLock( &lock );
3237 ret = USER_Driver->pMsgWaitForMultipleObjectsEx( count+1, handles, timeout, mask, flags );
3238 if (lock) RestoreThunkLock( lock );
3243 /***********************************************************************
3244 * MsgWaitForMultipleObjects (USER32.@)
3246 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
3247 BOOL wait_all, DWORD timeout, DWORD mask )
3249 return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
3250 wait_all ? MWMO_WAITALL : 0 );
3254 /***********************************************************************
3255 * WaitForInputIdle (USER32.@)
3257 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
3259 DWORD start_time, elapsed, ret;
3262 handles[0] = hProcess;
3263 SERVER_START_REQ( get_process_idle_event )
3265 req->handle = hProcess;
3266 if (!(ret = wine_server_call_err( req ))) handles[1] = reply->event;
3269 if (ret) return WAIT_FAILED; /* error */
3270 if (!handles[1]) return 0; /* no event to wait on */
3272 start_time = GetTickCount();
3275 TRACE("waiting for %p\n", handles[1] );
3278 ret = MsgWaitForMultipleObjects ( 2, handles, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
3283 case WAIT_OBJECT_0+2:
3284 process_sent_messages();
3288 TRACE("timeout or error\n");
3291 TRACE("finished\n");
3294 if (dwTimeOut != INFINITE)
3296 elapsed = GetTickCount() - start_time;
3297 if (elapsed > dwTimeOut)
3303 return WAIT_TIMEOUT;
3307 /***********************************************************************
3308 * UserYield (USER.332)
3310 void WINAPI UserYield16(void)
3314 /* Handle sent messages */
3315 process_sent_messages();
3318 ReleaseThunkLock(&count);
3322 RestoreThunkLock(count);
3323 /* Handle sent messages again */
3324 process_sent_messages();
3329 /***********************************************************************
3330 * RegisterWindowMessageA (USER32.@)
3331 * RegisterWindowMessage (USER.118)
3333 UINT WINAPI RegisterWindowMessageA( LPCSTR str )
3335 UINT ret = GlobalAddAtomA(str);
3336 TRACE("%s, ret=%x\n", str, ret);
3341 /***********************************************************************
3342 * RegisterWindowMessageW (USER32.@)
3344 UINT WINAPI RegisterWindowMessageW( LPCWSTR str )
3346 UINT ret = GlobalAddAtomW(str);
3347 TRACE("%s ret=%x\n", debugstr_w(str), ret);
3351 typedef struct BroadcastParm
3362 static BOOL CALLBACK bcast_childwindow( HWND hw, LPARAM lp )
3364 BroadcastParm *parm = (BroadcastParm*)lp;
3365 DWORD_PTR retval = 0;
3368 if (parm->flags & BSF_IGNORECURRENTTASK && WIN_IsCurrentProcess(hw))
3370 TRACE("Not telling myself %p\n", hw);
3374 /* I don't know 100% for sure if this is what Windows does, but it fits the tests */
3375 if (parm->flags & BSF_QUERY)
3377 TRACE("Telling window %p using SendMessageTimeout\n", hw);
3379 /* Not tested for conflicting flags */
3380 if (parm->flags & BSF_FORCEIFHUNG || parm->flags & BSF_NOHANG)
3381 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_ABORTIFHUNG, 2000, &retval );
3382 else if (parm->flags & BSF_NOTIMEOUTIFNOTHUNG)
3383 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NOTIMEOUTIFNOTHUNG, 2000, &retval );
3385 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NORMAL, 2000, &retval );
3387 if (!lresult && GetLastError() == ERROR_TIMEOUT)
3389 WARN("Timed out!\n");
3390 if (!(parm->flags & BSF_FORCEIFHUNG))
3393 if (retval == BROADCAST_QUERY_DENY)
3402 else if (parm->flags & BSF_POSTMESSAGE)
3404 TRACE("Telling window %p using PostMessage\n", hw);
3405 PostMessageW( hw, parm->msg, parm->wp, parm->lp );
3409 TRACE("Telling window %p using SendNotifyMessage\n", hw);
3410 SendNotifyMessageW( hw, parm->msg, parm->wp, parm->lp );
3416 static BOOL CALLBACK bcast_desktop( LPWSTR desktop, LPARAM lp )
3420 BroadcastParm *parm = (BroadcastParm*)lp;
3422 TRACE("desktop: %s\n", debugstr_w( desktop ));
3424 hdesktop = open_winstation_desktop( parm->winsta, desktop, 0, FALSE, DESKTOP_ENUMERATE|DESKTOP_WRITEOBJECTS|STANDARD_RIGHTS_WRITE );
3427 FIXME("Could not open desktop %s\n", debugstr_w(desktop));
3431 ret = EnumDesktopWindows( hdesktop, bcast_childwindow, lp );
3432 CloseDesktop(hdesktop);
3433 TRACE("-->%d\n", ret);
3434 return parm->success;
3437 static BOOL CALLBACK bcast_winsta( LPWSTR winsta, LPARAM lp )
3440 HWINSTA hwinsta = OpenWindowStationW( winsta, FALSE, WINSTA_ENUMDESKTOPS );
3441 TRACE("hwinsta: %p/%s/%08x\n", hwinsta, debugstr_w( winsta ), GetLastError());
3444 ((BroadcastParm *)lp)->winsta = hwinsta;
3445 ret = EnumDesktopsW( hwinsta, bcast_desktop, lp );
3446 CloseWindowStation( hwinsta );
3447 TRACE("-->%d\n", ret);
3451 /***********************************************************************
3452 * BroadcastSystemMessageA (USER32.@)
3453 * BroadcastSystemMessage (USER32.@)
3455 LONG WINAPI BroadcastSystemMessageA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
3457 return BroadcastSystemMessageExA( flags, recipients, msg, wp, lp, NULL );
3461 /***********************************************************************
3462 * BroadcastSystemMessageW (USER32.@)
3464 LONG WINAPI BroadcastSystemMessageW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
3466 return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, NULL );
3469 /***********************************************************************
3470 * BroadcastSystemMessageExA (USER32.@)
3472 LONG WINAPI BroadcastSystemMessageExA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
3474 map_wparam_AtoW( msg, &wp, WMCHAR_MAP_NOMAPPING );
3475 return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, NULL );
3479 /***********************************************************************
3480 * BroadcastSystemMessageExW (USER32.@)
3482 LONG WINAPI BroadcastSystemMessageExW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
3485 DWORD recips = BSM_ALLCOMPONENTS;
3487 static const DWORD all_flags = ( BSF_QUERY | BSF_IGNORECURRENTTASK | BSF_FLUSHDISK | BSF_NOHANG
3488 | BSF_POSTMESSAGE | BSF_FORCEIFHUNG | BSF_NOTIMEOUTIFNOTHUNG
3489 | BSF_ALLOWSFW | BSF_SENDNOTIFYMESSAGE | BSF_RETURNHDESK | BSF_LUID );
3491 TRACE("Flags: %08x, recipients: %p(0x%x), msg: %04x, wparam: %08lx, lparam: %08lx\n", flags, recipients,
3492 (recipients ? *recipients : recips), msg, wp, lp);
3494 if (flags & ~all_flags)
3496 SetLastError(ERROR_INVALID_PARAMETER);
3501 recipients = &recips;
3503 if ( pinfo && flags & BSF_QUERY )
3504 FIXME("Not returning PBSMINFO information yet\n");
3507 parm.recipients = recipients;
3511 parm.success = TRUE;
3513 if (*recipients & BSM_ALLDESKTOPS || *recipients == BSM_ALLCOMPONENTS)
3514 ret = EnumWindowStationsW(bcast_winsta, (LONG_PTR)&parm);
3515 else if (*recipients & BSM_APPLICATIONS)
3517 EnumWindows(bcast_childwindow, (LONG_PTR)&parm);
3521 FIXME("Recipients %08x not supported!\n", *recipients);
3526 /***********************************************************************
3527 * SetMessageQueue (USER32.@)
3529 BOOL WINAPI SetMessageQueue( INT size )
3531 /* now obsolete the message queue will be expanded dynamically as necessary */
3536 /***********************************************************************
3537 * MessageBeep (USER32.@)
3539 BOOL WINAPI MessageBeep( UINT i )
3542 SystemParametersInfoA( SPI_GETBEEP, 0, &active, FALSE );
3543 if (active) USER_Driver->pBeep();
3548 /***********************************************************************
3549 * SetTimer (USER32.@)
3551 UINT_PTR WINAPI SetTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3554 WNDPROC winproc = 0;
3556 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, NULL );
3558 SERVER_START_REQ( set_win_timer )
3561 req->msg = WM_TIMER;
3563 req->rate = max( timeout, SYS_TIMER_RATE );
3564 req->lparam = (unsigned long)winproc;
3565 if (!wine_server_call_err( req ))
3568 if (!ret) ret = TRUE;
3574 TRACE("Added %p %lx %p timeout %d\n", hwnd, id, winproc, timeout );
3579 /***********************************************************************
3580 * SetSystemTimer (USER32.@)
3582 UINT_PTR WINAPI SetSystemTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3585 WNDPROC winproc = 0;
3587 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, NULL );
3589 SERVER_START_REQ( set_win_timer )
3592 req->msg = WM_SYSTIMER;
3594 req->rate = max( timeout, SYS_TIMER_RATE );
3595 req->lparam = (unsigned long)winproc;
3596 if (!wine_server_call_err( req ))
3599 if (!ret) ret = TRUE;
3605 TRACE("Added %p %lx %p timeout %d\n", hwnd, id, winproc, timeout );
3610 /***********************************************************************
3611 * KillTimer (USER32.@)
3613 BOOL WINAPI KillTimer( HWND hwnd, UINT_PTR id )
3617 SERVER_START_REQ( kill_win_timer )
3620 req->msg = WM_TIMER;
3622 ret = !wine_server_call_err( req );
3629 /***********************************************************************
3630 * KillSystemTimer (USER32.@)
3632 BOOL WINAPI KillSystemTimer( HWND hwnd, UINT_PTR id )
3636 SERVER_START_REQ( kill_win_timer )
3639 req->msg = WM_SYSTIMER;
3641 ret = !wine_server_call_err( req );
3648 /**********************************************************************
3649 * GetGUIThreadInfo (USER32.@)
3651 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
3655 SERVER_START_REQ( get_thread_input )
3658 if ((ret = !wine_server_call_err( req )))
3661 info->hwndActive = reply->active;
3662 info->hwndFocus = reply->focus;
3663 info->hwndCapture = reply->capture;
3664 info->hwndMenuOwner = reply->menu_owner;
3665 info->hwndMoveSize = reply->move_size;
3666 info->hwndCaret = reply->caret;
3667 info->rcCaret.left = reply->rect.left;
3668 info->rcCaret.top = reply->rect.top;
3669 info->rcCaret.right = reply->rect.right;
3670 info->rcCaret.bottom = reply->rect.bottom;
3671 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
3672 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
3673 if (reply->caret) info->flags |= GUI_CARETBLINKING;
3681 /******************************************************************
3682 * IsHungAppWindow (USER32.@)
3685 BOOL WINAPI IsHungAppWindow( HWND hWnd )
3689 SERVER_START_REQ( is_window_hung )
3692 ret = !wine_server_call_err( req ) && reply->is_hung;