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
40 #include "wine/unicode.h"
41 #include "wine/server.h"
42 #include "user_private.h"
45 #include "wine/debug.h"
46 #include "wine/exception.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msg);
49 WINE_DECLARE_DEBUG_CHANNEL(relay);
50 WINE_DECLARE_DEBUG_CHANNEL(key);
52 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
53 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
55 #define MAX_PACK_COUNT 4
57 #define SYS_TIMER_RATE 55 /* min. timer rate in ms (actually 54.925)*/
59 /* the various structures that can be sent in messages, in platform-independent layout */
60 struct packed_CREATESTRUCTW
62 ULONGLONG lpCreateParams;
66 user_handle_t hwndParent;
79 /* the structures are unpacked on top of the packed ones, so make sure they fit */
80 C_ASSERT( sizeof(struct packed_CREATESTRUCTW) >= sizeof(CREATESTRUCTW) );
84 struct packed_CREATESTRUCTW cs;
87 /* description of the data fields that need to be packed along with a sent message */
90 union packed_structs ps;
92 const void *data[MAX_PACK_COUNT];
93 size_t size[MAX_PACK_COUNT];
96 /* info about the message currently being received by the current thread */
97 struct received_message_info
99 enum message_type type;
101 UINT flags; /* InSendMessageEx return flags */
104 /* structure to group all parameters for sent messages of the various kinds */
105 struct send_message_info
107 enum message_type type;
113 UINT flags; /* flags for SendMessageTimeout */
114 UINT timeout; /* timeout for SendMessageTimeout */
115 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
116 ULONG_PTR data; /* callback data */
117 enum wm_char_mapping wm_char;
121 /* Message class descriptor */
122 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
124 const struct builtin_class_descr MESSAGE_builtin_class =
128 WINPROC_MESSAGE, /* proc */
130 IDC_ARROW, /* cursor */
136 /* flag for messages that contain pointers */
137 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
139 #define SET(msg) (1 << ((msg) & 31))
141 static const unsigned int message_pointer_flags[] =
144 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
145 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
147 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
150 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
151 SET(WM_NOTIFY) | SET(WM_HELP),
153 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
155 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
157 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
159 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
161 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO) | SET(SBM_GETSCROLLBARINFO),
167 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
168 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
169 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
173 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
174 SET(LB_DIR) | SET(LB_FINDSTRING) |
175 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
177 SET(LB_FINDSTRINGEXACT),
183 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
185 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
186 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
200 SET(WM_ASKCBFORMATNAME)
203 /* flags for messages that contain Unicode strings */
204 static const unsigned int message_unicode_flags[] =
207 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
208 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
220 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
224 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
228 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
229 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
233 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
234 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
236 SET(LB_FINDSTRINGEXACT),
258 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
261 /* check whether a given message type includes pointers */
262 static inline int is_pointer_message( UINT message )
264 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
265 return (message_pointer_flags[message / 32] & SET(message)) != 0;
268 /* check whether a given message type contains Unicode (or ASCII) chars */
269 static inline int is_unicode_message( UINT message )
271 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
272 return (message_unicode_flags[message / 32] & SET(message)) != 0;
277 /* add a data field to a packed message */
278 static inline void push_data( struct packed_message *data, const void *ptr, size_t size )
280 data->data[data->count] = ptr;
281 data->size[data->count] = size;
285 /* add a string to a packed message */
286 static inline void push_string( struct packed_message *data, LPCWSTR str )
288 push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
291 /* make sure that the buffer contains a valid null-terminated Unicode string */
292 static inline BOOL check_string( LPCWSTR str, size_t size )
294 for (size /= sizeof(WCHAR); size; size--, str++)
295 if (!*str) return TRUE;
299 /* pack a pointer into a 32/64 portable format */
300 static inline ULONGLONG pack_ptr( const void *ptr )
302 return (ULONG_PTR)ptr;
305 /* unpack a potentially 64-bit pointer, returning 0 when truncated */
306 static inline void *unpack_ptr( ULONGLONG ptr64 )
308 if ((ULONG_PTR)ptr64 != ptr64) return 0;
309 return (void *)(ULONG_PTR)ptr64;
312 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
313 static inline void *get_buffer_space( void **buffer, size_t size )
319 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
320 HeapFree( GetProcessHeap(), 0, *buffer );
322 else ret = HeapAlloc( GetProcessHeap(), 0, size );
328 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
329 static inline BOOL combobox_has_strings( HWND hwnd )
331 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
332 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
335 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
336 static inline BOOL listbox_has_strings( HWND hwnd )
338 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
339 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
342 /* check whether message is in the range of keyboard messages */
343 static inline BOOL is_keyboard_message( UINT message )
345 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
348 /* check whether message is in the range of mouse messages */
349 static inline BOOL is_mouse_message( UINT message )
351 return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
352 (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
355 /* check whether message matches the specified hwnd filter */
356 static inline BOOL check_hwnd_filter( const MSG *msg, HWND hwnd_filter )
358 if (!hwnd_filter || hwnd_filter == GetDesktopWindow()) return TRUE;
359 return (msg->hwnd == hwnd_filter || IsChild( hwnd_filter, msg->hwnd ));
362 /* check for pending WM_CHAR message with DBCS trailing byte */
363 static inline BOOL get_pending_wmchar( MSG *msg, UINT first, UINT last, BOOL remove )
365 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
367 if (!data || !data->get_msg.message) return FALSE;
368 if ((first || last) && (first > WM_CHAR || last < WM_CHAR)) return FALSE;
369 if (!msg) return FALSE;
370 *msg = data->get_msg;
371 if (remove) data->get_msg.message = 0;
376 /***********************************************************************
379 * Window procedure for "Message" windows (HWND_MESSAGE parent).
381 LRESULT WINAPI MessageWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
383 if (message == WM_NCCREATE) return TRUE;
384 return 0; /* all other messages are ignored */
388 /***********************************************************************
389 * broadcast_message_callback
391 * Helper callback for broadcasting messages.
393 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
395 struct send_message_info *info = (struct send_message_info *)lparam;
396 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
400 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
401 info->flags, info->timeout, NULL );
404 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
405 info->flags, info->timeout, NULL );
408 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
411 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
412 info->callback, info->data );
415 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
418 ERR( "bad type %d\n", info->type );
425 /***********************************************************************
428 * Convert the wparam of an ASCII message to Unicode.
430 BOOL map_wparam_AtoW( UINT message, WPARAM *wparam, enum wm_char_mapping mapping )
439 /* WM_CHAR is magic: a DBCS char can be sent/posted as two consecutive WM_CHAR
440 * messages, in which case the first char is stored, and the conversion
441 * to Unicode only takes place once the second char is sent/posted.
443 if (mapping != WMCHAR_MAP_NOMAPPING)
445 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
446 BYTE low = LOBYTE(*wparam);
451 ch[1] = HIBYTE(*wparam);
452 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
453 TRACE( "map %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
454 if (data) data->lead_byte[mapping] = 0;
456 else if (data && data->lead_byte[mapping])
458 ch[0] = data->lead_byte[mapping];
460 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
461 TRACE( "map stored %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
462 data->lead_byte[mapping] = 0;
464 else if (!IsDBCSLeadByte( low ))
467 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 1 );
468 TRACE( "map %02x -> %04x\n", (BYTE)ch[0], wch[0] );
469 if (data) data->lead_byte[mapping] = 0;
471 else /* store it and wait for trail byte */
475 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) )))
477 get_user_thread_info()->wmchar_data = data;
479 TRACE( "storing lead byte %02x mapping %u\n", low, mapping );
480 data->lead_byte[mapping] = low;
483 *wparam = MAKEWPARAM(wch[0], wch[1]);
486 /* else fall through */
488 case EM_SETPASSWORDCHAR:
493 ch[0] = LOBYTE(*wparam);
494 ch[1] = HIBYTE(*wparam);
495 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
496 *wparam = MAKEWPARAM(wch[0], wch[1]);
499 ch[0] = HIBYTE(*wparam);
500 ch[1] = LOBYTE(*wparam);
501 if (ch[0]) RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch, 2 );
502 else RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch + 1, 1 );
503 *wparam = MAKEWPARAM(wch[0], HIWORD(*wparam));
510 /***********************************************************************
513 * Convert the wparam of a Unicode message to ASCII.
515 static void map_wparam_WtoA( MSG *msg, BOOL remove )
524 if (!HIWORD(msg->wParam))
526 wch[0] = LOWORD(msg->wParam);
528 RtlUnicodeToMultiByteN( (LPSTR)ch, 2, &len, wch, sizeof(wch[0]) );
529 if (len == 2) /* DBCS char */
531 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
534 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) ))) return;
535 get_user_thread_info()->wmchar_data = data;
539 data->get_msg = *msg;
540 data->get_msg.wParam = ch[1];
546 /* else fall through */
548 case EM_SETPASSWORDCHAR:
553 wch[0] = LOWORD(msg->wParam);
554 wch[1] = HIWORD(msg->wParam);
556 RtlUnicodeToMultiByteN( (LPSTR)ch, 2, NULL, wch, sizeof(wch) );
557 msg->wParam = MAKEWPARAM( ch[0] | (ch[1] << 8), 0 );
560 wch[0] = LOWORD(msg->wParam);
562 RtlUnicodeToMultiByteN( (LPSTR)ch, 2, &len, wch, sizeof(wch[0]) );
564 msg->wParam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(msg->wParam) );
566 msg->wParam = MAKEWPARAM( ch[0], HIWORD(msg->wParam) );
572 /***********************************************************************
575 * Pack a message for sending to another process.
576 * Return the size of the data we expect in the message reply.
577 * Set data->count to -1 if there is an error.
579 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
580 struct packed_message *data )
588 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
589 data->ps.cs.lpCreateParams = pack_ptr( cs->lpCreateParams );
590 data->ps.cs.hInstance = pack_ptr( cs->hInstance );
591 data->ps.cs.hMenu = wine_server_user_handle( cs->hMenu );
592 data->ps.cs.hwndParent = wine_server_user_handle( cs->hwndParent );
593 data->ps.cs.cy = cs->cy;
594 data->ps.cs.cx = cs->cx;
595 data->ps.cs.y = cs->y;
596 data->ps.cs.x = cs->x;
597 data->ps.cs.style = cs->style;
598 data->ps.cs.dwExStyle = cs->dwExStyle;
599 data->ps.cs.lpszName = pack_ptr( cs->lpszName );
600 data->ps.cs.lpszClass = pack_ptr( cs->lpszClass );
601 push_data( data, &data->ps.cs, sizeof(data->ps.cs) );
602 if (!IS_INTRESOURCE(cs->lpszName)) push_string( data, cs->lpszName );
603 if (!IS_INTRESOURCE(cs->lpszClass)) push_string( data, cs->lpszClass );
604 return sizeof(data->ps.cs);
607 case WM_ASKCBFORMATNAME:
608 return wparam * sizeof(WCHAR);
609 case WM_WININICHANGE:
610 if (lparam) push_string(data, (LPWSTR)lparam );
613 case WM_DEVMODECHANGE:
618 push_string( data, (LPWSTR)lparam );
620 case WM_GETMINMAXINFO:
621 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
622 return sizeof(MINMAXINFO);
624 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
627 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
628 return sizeof(MEASUREITEMSTRUCT);
630 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
633 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
635 case WM_WINDOWPOSCHANGING:
636 case WM_WINDOWPOSCHANGED:
637 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
638 return sizeof(WINDOWPOS);
641 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
642 push_data( data, cp, sizeof(*cp) );
643 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
647 /* WM_NOTIFY cannot be sent across processes (MSDN) */
651 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
653 case WM_STYLECHANGING:
654 case WM_STYLECHANGED:
655 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
660 push_data( data, (RECT *)lparam, sizeof(RECT) );
665 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
666 push_data( data, nc, sizeof(*nc) );
667 push_data( data, nc->lppos, sizeof(*nc->lppos) );
668 return sizeof(*nc) + sizeof(*nc->lppos);
671 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
673 case SBM_SETSCROLLINFO:
674 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
676 case SBM_GETSCROLLINFO:
677 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
678 return sizeof(SCROLLINFO);
679 case SBM_GETSCROLLBARINFO:
681 const SCROLLBARINFO *info = (const SCROLLBARINFO *)lparam;
682 size_t size = min( info->cbSize, sizeof(SCROLLBARINFO) );
683 push_data( data, info, size );
691 if (wparam) size += sizeof(DWORD);
692 if (lparam) size += sizeof(DWORD);
697 case CB_GETDROPPEDCONTROLRECT:
701 push_data( data, (RECT *)lparam, sizeof(RECT) );
705 WORD *pw = (WORD *)lparam;
706 push_data( data, pw, sizeof(*pw) );
707 return *pw * sizeof(WCHAR);
711 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
714 case CB_INSERTSTRING:
716 case CB_FINDSTRINGEXACT:
717 case CB_SELECTSTRING:
718 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
721 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
722 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
724 case LB_INSERTSTRING:
726 case LB_FINDSTRINGEXACT:
727 case LB_SELECTSTRING:
728 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
731 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
732 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
734 return wparam * sizeof(UINT);
736 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
737 return sizeof(MDINEXTMENU);
740 push_data( data, (RECT *)lparam, sizeof(RECT) );
744 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
745 push_data( data, cs, sizeof(*cs) );
746 if (!IS_INTRESOURCE(cs->szTitle)) push_string( data, cs->szTitle );
747 if (!IS_INTRESOURCE(cs->szClass)) push_string( data, cs->szClass );
750 case WM_MDIGETACTIVE:
751 if (lparam) return sizeof(BOOL);
753 case WM_DEVICECHANGE:
755 DEV_BROADCAST_HDR *header = (DEV_BROADCAST_HDR *)lparam;
756 push_data( data, header, header->dbch_size );
759 case WM_WINE_SETWINDOWPOS:
760 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
762 case WM_WINE_KEYBOARD_LL_HOOK:
764 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
765 push_data( data, h_extra, sizeof(*h_extra) );
766 push_data( data, (LPVOID)h_extra->lparam, sizeof(KBDLLHOOKSTRUCT) );
769 case WM_WINE_MOUSE_LL_HOOK:
771 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
772 push_data( data, h_extra, sizeof(*h_extra) );
773 push_data( data, (LPVOID)h_extra->lparam, sizeof(MSLLHOOKSTRUCT) );
777 if (wparam <= 1) return 0;
778 FIXME( "WM_NCPAINT hdc packing not supported yet\n" );
782 if (!wparam) return 0;
785 /* these contain an HFONT */
788 /* these contain an HDC */
790 case WM_ICONERASEBKGND:
791 case WM_CTLCOLORMSGBOX:
792 case WM_CTLCOLOREDIT:
793 case WM_CTLCOLORLISTBOX:
796 case WM_CTLCOLORSCROLLBAR:
797 case WM_CTLCOLORSTATIC:
800 /* these contain an HGLOBAL */
801 case WM_PAINTCLIPBOARD:
802 case WM_SIZECLIPBOARD:
803 /* these contain HICON */
806 case WM_QUERYDRAGICON:
807 case WM_QUERYPARKICON:
808 /* these contain pointers */
810 case WM_QUERYDROPOBJECT:
814 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
822 /***********************************************************************
825 * Unpack a message received from another process.
827 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
828 void **buffer, size_t size )
831 union packed_structs *ps = *buffer;
839 WCHAR *str = (WCHAR *)(&ps->cs + 1);
840 if (size < sizeof(ps->cs)) return FALSE;
841 size -= sizeof(ps->cs);
842 cs.lpCreateParams = unpack_ptr( ps->cs.lpCreateParams );
843 cs.hInstance = unpack_ptr( ps->cs.hInstance );
844 cs.hMenu = wine_server_ptr_handle( ps->cs.hMenu );
845 cs.hwndParent = wine_server_ptr_handle( ps->cs.hwndParent );
850 cs.style = ps->cs.style;
851 cs.dwExStyle = ps->cs.dwExStyle;
852 cs.lpszName = unpack_ptr( ps->cs.lpszName );
853 cs.lpszClass = unpack_ptr( ps->cs.lpszClass );
854 if (ps->cs.lpszName >> 16)
856 if (!check_string( str, size )) return FALSE;
858 size -= (strlenW(str) + 1) * sizeof(WCHAR);
859 str += strlenW(str) + 1;
861 if (ps->cs.lpszClass >> 16)
863 if (!check_string( str, size )) return FALSE;
866 memcpy( &ps->cs, &cs, sizeof(cs) );
870 case WM_ASKCBFORMATNAME:
871 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
873 case WM_WININICHANGE:
874 if (!*lparam) return TRUE;
877 case WM_DEVMODECHANGE:
882 if (!check_string( *buffer, size )) return FALSE;
884 case WM_GETMINMAXINFO:
885 minsize = sizeof(MINMAXINFO);
888 minsize = sizeof(DRAWITEMSTRUCT);
891 minsize = sizeof(MEASUREITEMSTRUCT);
894 minsize = sizeof(DELETEITEMSTRUCT);
897 minsize = sizeof(COMPAREITEMSTRUCT);
899 case WM_WINDOWPOSCHANGING:
900 case WM_WINDOWPOSCHANGED:
901 case WM_WINE_SETWINDOWPOS:
902 minsize = sizeof(WINDOWPOS);
906 COPYDATASTRUCT *cp = *buffer;
907 if (size < sizeof(*cp)) return FALSE;
910 minsize = sizeof(*cp) + cp->cbData;
916 /* WM_NOTIFY cannot be sent across processes (MSDN) */
919 minsize = sizeof(HELPINFO);
921 case WM_STYLECHANGING:
922 case WM_STYLECHANGED:
923 minsize = sizeof(STYLESTRUCT);
926 if (!*wparam) minsize = sizeof(RECT);
929 NCCALCSIZE_PARAMS *nc = *buffer;
930 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
931 nc->lppos = (WINDOWPOS *)(nc + 1);
935 if (!*lparam) return TRUE;
936 minsize = sizeof(MSG);
938 case SBM_SETSCROLLINFO:
939 minsize = sizeof(SCROLLINFO);
941 case SBM_GETSCROLLINFO:
942 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
944 case SBM_GETSCROLLBARINFO:
945 if (!get_buffer_space( buffer, sizeof(SCROLLBARINFO ))) return FALSE;
950 if (*wparam || *lparam)
952 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
953 if (*wparam) *wparam = (WPARAM)*buffer;
954 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
959 case CB_GETDROPPEDCONTROLRECT:
960 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
964 minsize = sizeof(RECT);
969 if (size < sizeof(WORD)) return FALSE;
970 len = *(WORD *)*buffer;
971 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
972 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
977 if (!*wparam) return TRUE;
978 minsize = *wparam * sizeof(UINT);
981 case CB_INSERTSTRING:
983 case CB_FINDSTRINGEXACT:
984 case CB_SELECTSTRING:
986 case LB_INSERTSTRING:
988 case LB_FINDSTRINGEXACT:
989 case LB_SELECTSTRING:
990 if (!*buffer) return TRUE;
991 if (!check_string( *buffer, size )) return FALSE;
995 size = sizeof(ULONG_PTR);
996 if (combobox_has_strings( hwnd ))
997 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
998 if (!get_buffer_space( buffer, size )) return FALSE;
1003 size = sizeof(ULONG_PTR);
1004 if (listbox_has_strings( hwnd ))
1005 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
1006 if (!get_buffer_space( buffer, size )) return FALSE;
1009 case LB_GETSELITEMS:
1010 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
1013 minsize = sizeof(MDINEXTMENU);
1014 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
1018 minsize = sizeof(RECT);
1019 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
1023 MDICREATESTRUCTW *cs = *buffer;
1024 WCHAR *str = (WCHAR *)(cs + 1);
1025 if (size < sizeof(*cs)) return FALSE;
1026 size -= sizeof(*cs);
1027 if (!IS_INTRESOURCE(cs->szTitle))
1029 if (!check_string( str, size )) return FALSE;
1031 size -= (strlenW(str) + 1) * sizeof(WCHAR);
1032 str += strlenW(str) + 1;
1034 if (!IS_INTRESOURCE(cs->szClass))
1036 if (!check_string( str, size )) return FALSE;
1041 case WM_MDIGETACTIVE:
1042 if (!*lparam) return TRUE;
1043 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
1045 case WM_DEVICECHANGE:
1046 minsize = sizeof(DEV_BROADCAST_HDR);
1048 case WM_WINE_KEYBOARD_LL_HOOK:
1049 case WM_WINE_MOUSE_LL_HOOK:
1051 struct hook_extra_info *h_extra = *buffer;
1053 minsize = sizeof(struct hook_extra_info) +
1054 (message == WM_WINE_KEYBOARD_LL_HOOK ? sizeof(KBDLLHOOKSTRUCT)
1055 : sizeof(MSLLHOOKSTRUCT));
1056 if (size < minsize) return FALSE;
1057 h_extra->lparam = (LPARAM)(h_extra + 1);
1061 if (*wparam <= 1) return TRUE;
1062 FIXME( "WM_NCPAINT hdc unpacking not supported\n" );
1065 if (!*wparam) return TRUE;
1068 /* these contain an HFONT */
1071 /* these contain an HDC */
1073 case WM_ICONERASEBKGND:
1074 case WM_CTLCOLORMSGBOX:
1075 case WM_CTLCOLOREDIT:
1076 case WM_CTLCOLORLISTBOX:
1077 case WM_CTLCOLORBTN:
1078 case WM_CTLCOLORDLG:
1079 case WM_CTLCOLORSCROLLBAR:
1080 case WM_CTLCOLORSTATIC:
1082 case WM_PRINTCLIENT:
1083 /* these contain an HGLOBAL */
1084 case WM_PAINTCLIPBOARD:
1085 case WM_SIZECLIPBOARD:
1086 /* these contain HICON */
1089 case WM_QUERYDRAGICON:
1090 case WM_QUERYPARKICON:
1091 /* these contain pointers */
1093 case WM_QUERYDROPOBJECT:
1097 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
1101 return TRUE; /* message doesn't need any unpacking */
1104 /* default exit for most messages: check minsize and store buffer in lparam */
1105 if (size < minsize) return FALSE;
1106 *lparam = (LPARAM)*buffer;
1111 /***********************************************************************
1114 * Pack a reply to a message for sending to another process.
1116 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1117 LRESULT res, struct packed_message *data )
1125 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
1126 data->ps.cs.lpCreateParams = (ULONG_PTR)cs->lpCreateParams;
1127 data->ps.cs.hInstance = (ULONG_PTR)cs->hInstance;
1128 data->ps.cs.hMenu = wine_server_user_handle( cs->hMenu );
1129 data->ps.cs.hwndParent = wine_server_user_handle( cs->hwndParent );
1130 data->ps.cs.cy = cs->cy;
1131 data->ps.cs.cx = cs->cx;
1132 data->ps.cs.y = cs->y;
1133 data->ps.cs.x = cs->x;
1134 data->ps.cs.style = cs->style;
1135 data->ps.cs.dwExStyle = cs->dwExStyle;
1136 data->ps.cs.lpszName = (ULONG_PTR)cs->lpszName;
1137 data->ps.cs.lpszClass = (ULONG_PTR)cs->lpszClass;
1138 push_data( data, &data->ps.cs, sizeof(data->ps.cs) );
1144 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
1146 case WM_GETMINMAXINFO:
1147 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
1149 case WM_MEASUREITEM:
1150 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
1152 case WM_WINDOWPOSCHANGING:
1153 case WM_WINDOWPOSCHANGED:
1154 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
1157 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
1159 case SBM_GETSCROLLINFO:
1160 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
1163 case LB_GETITEMRECT:
1164 case CB_GETDROPPEDCONTROLRECT:
1167 push_data( data, (RECT *)lparam, sizeof(RECT) );
1171 WORD *ptr = (WORD *)lparam;
1172 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
1175 case LB_GETSELITEMS:
1176 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
1178 case WM_MDIGETACTIVE:
1179 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
1183 push_data( data, (RECT *)lparam, sizeof(RECT) );
1186 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1187 push_data( data, nc, sizeof(*nc) );
1188 push_data( data, nc->lppos, sizeof(*nc->lppos) );
1194 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
1195 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
1198 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
1201 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
1203 case WM_ASKCBFORMATNAME:
1204 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
1210 /***********************************************************************
1213 * Unpack a message reply received from another process.
1215 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1216 void *buffer, size_t size )
1218 union packed_structs *ps = buffer;
1224 if (size >= sizeof(ps->cs))
1226 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
1227 cs->lpCreateParams = unpack_ptr( ps->cs.lpCreateParams );
1228 cs->hInstance = unpack_ptr( ps->cs.hInstance );
1229 cs->hMenu = wine_server_ptr_handle( ps->cs.hMenu );
1230 cs->hwndParent = wine_server_ptr_handle( ps->cs.hwndParent );
1235 cs->style = ps->cs.style;
1236 cs->dwExStyle = ps->cs.dwExStyle;
1237 /* don't allow changing name and class pointers */
1241 case WM_ASKCBFORMATNAME:
1242 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
1244 case WM_GETMINMAXINFO:
1245 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
1247 case WM_MEASUREITEM:
1248 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
1250 case WM_WINDOWPOSCHANGING:
1251 case WM_WINDOWPOSCHANGED:
1252 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
1255 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
1257 case SBM_GETSCROLLINFO:
1258 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
1260 case SBM_GETSCROLLBARINFO:
1261 memcpy( (SCROLLBARINFO *)lparam, buffer, min( sizeof(SCROLLBARINFO), size ));
1264 case CB_GETDROPPEDCONTROLRECT:
1265 case LB_GETITEMRECT:
1268 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1271 size = min( size, (size_t)*(WORD *)lparam );
1272 memcpy( (WCHAR *)lparam, buffer, size );
1274 case LB_GETSELITEMS:
1275 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
1279 memcpy( (WCHAR *)lparam, buffer, size );
1282 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
1284 case WM_MDIGETACTIVE:
1285 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
1289 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1292 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1293 WINDOWPOS *wp = nc->lppos;
1294 memcpy( nc, buffer, min( sizeof(*nc), size ));
1295 if (size > sizeof(*nc))
1297 size -= sizeof(*nc);
1298 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
1300 nc->lppos = wp; /* restore the original pointer */
1308 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1309 if (size <= sizeof(DWORD)) break;
1310 size -= sizeof(DWORD);
1311 buffer = (DWORD *)buffer + 1;
1313 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1317 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
1318 LPCWSTR title = cs->szTitle, class = cs->szClass;
1319 memcpy( cs, buffer, min( sizeof(*cs), size ));
1320 cs->szTitle = title; /* restore the original pointers */
1321 cs->szClass = class;
1325 ERR( "should not happen: unexpected message %x\n", message );
1331 /***********************************************************************
1334 * Send a reply to a sent message.
1336 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1338 struct packed_message data;
1339 int i, replied = info->flags & ISMEX_REPLIED;
1341 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1342 if (!remove && replied) return; /* replied already */
1344 memset( &data, 0, sizeof(data) );
1345 info->flags |= ISMEX_REPLIED;
1347 if (info->type == MSG_OTHER_PROCESS && !replied)
1349 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1350 info->msg.lParam, result, &data );
1353 SERVER_START_REQ( reply_message )
1355 req->result = result;
1356 req->remove = remove;
1357 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1358 wine_server_call( req );
1364 /***********************************************************************
1365 * handle_internal_message
1367 * Handle an internal Wine message instead of calling the window proc.
1369 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1373 case WM_WINE_DESTROYWINDOW:
1374 return WIN_DestroyWindow( hwnd );
1375 case WM_WINE_SETWINDOWPOS:
1376 if (is_desktop_window( hwnd )) return 0;
1377 return USER_SetWindowPos( (WINDOWPOS *)lparam );
1378 case WM_WINE_SHOWWINDOW:
1379 if (is_desktop_window( hwnd )) return 0;
1380 return ShowWindow( hwnd, wparam );
1381 case WM_WINE_SETPARENT:
1382 if (is_desktop_window( hwnd )) return 0;
1383 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1384 case WM_WINE_SETWINDOWLONG:
1385 return WIN_SetWindowLong( hwnd, (short)LOWORD(wparam), HIWORD(wparam), lparam, TRUE );
1386 case WM_WINE_ENABLEWINDOW:
1387 if (is_desktop_window( hwnd )) return 0;
1388 return EnableWindow( hwnd, wparam );
1389 case WM_WINE_SETACTIVEWINDOW:
1390 if (is_desktop_window( hwnd )) return 0;
1391 return (LRESULT)SetActiveWindow( (HWND)wparam );
1392 case WM_WINE_KEYBOARD_LL_HOOK:
1393 case WM_WINE_MOUSE_LL_HOOK:
1395 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
1397 return call_current_hook( h_extra->handle, HC_ACTION, wparam, h_extra->lparam );
1400 if (msg >= WM_WINE_FIRST_DRIVER_MSG && msg <= WM_WINE_LAST_DRIVER_MSG)
1401 return USER_Driver->pWindowMessage( hwnd, msg, wparam, lparam );
1402 FIXME( "unknown internal message %x\n", msg );
1407 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1408 * to the memory handle, we keep track (in the server side) of all pairs of handle
1409 * used (the client passes its value and the content of the memory handle), and
1410 * the server stored both values (the client, and the local one, created after the
1411 * content). When a ACK message is generated, the list of pair is searched for a
1412 * matching pair, so that the client memory handle can be returned.
1415 HGLOBAL client_hMem;
1416 HGLOBAL server_hMem;
1419 static struct DDE_pair* dde_pairs;
1420 static int dde_num_alloc;
1421 static int dde_num_used;
1423 static CRITICAL_SECTION dde_crst;
1424 static CRITICAL_SECTION_DEBUG critsect_debug =
1427 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
1428 0, 0, { (DWORD_PTR)(__FILE__ ": dde_crst") }
1430 static CRITICAL_SECTION dde_crst = { &critsect_debug, -1, 0, 0, 0, 0 };
1432 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1437 EnterCriticalSection(&dde_crst);
1439 /* now remember the pair of hMem on both sides */
1440 if (dde_num_used == dde_num_alloc)
1442 struct DDE_pair* tmp;
1444 tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1445 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1447 tmp = HeapAlloc( GetProcessHeap(), 0,
1448 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1452 LeaveCriticalSection(&dde_crst);
1456 /* zero out newly allocated part */
1457 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1458 dde_num_alloc += GROWBY;
1461 for (i = 0; i < dde_num_alloc; i++)
1463 if (dde_pairs[i].server_hMem == 0)
1465 dde_pairs[i].client_hMem = chm;
1466 dde_pairs[i].server_hMem = shm;
1471 LeaveCriticalSection(&dde_crst);
1475 static HGLOBAL dde_get_pair(HGLOBAL shm)
1480 EnterCriticalSection(&dde_crst);
1481 for (i = 0; i < dde_num_alloc; i++)
1483 if (dde_pairs[i].server_hMem == shm)
1485 /* free this pair */
1486 dde_pairs[i].server_hMem = 0;
1488 ret = dde_pairs[i].client_hMem;
1492 LeaveCriticalSection(&dde_crst);
1496 /***********************************************************************
1499 * Post a DDE message
1501 static BOOL post_dde_message( struct packed_message *data, const struct send_message_info *info )
1505 UINT_PTR uiLo, uiHi;
1507 HGLOBAL hunlock = 0;
1511 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1517 /* DDE messages which don't require packing are:
1526 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1527 HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1530 /* send back the value of h on the other side */
1531 push_data( data, &h, sizeof(HGLOBAL) );
1533 TRACE( "send dde-ack %lx %08lx => %p\n", uiLo, uiHi, h );
1538 /* uiHi should contain either an atom or 0 */
1539 TRACE( "send dde-ack %lx atom=%lx\n", uiLo, uiHi );
1540 lp = MAKELONG( uiLo, uiHi );
1549 size = GlobalSize( (HGLOBAL)uiLo ) ;
1550 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1551 (info->msg == WM_DDE_DATA && size < FIELD_OFFSET(DDEDATA, Value)) ||
1552 (info->msg == WM_DDE_POKE && size < FIELD_OFFSET(DDEPOKE, Value))
1556 else if (info->msg != WM_DDE_DATA) return FALSE;
1561 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1563 DDEDATA *dde_data = ptr;
1564 TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
1565 dde_data->unused, dde_data->fResponse, dde_data->fRelease,
1566 dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
1567 push_data( data, ptr, size );
1568 hunlock = (HGLOBAL)uiLo;
1571 TRACE( "send ddepack %u %lx\n", size, uiHi );
1573 case WM_DDE_EXECUTE:
1576 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1578 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1579 /* so that the other side can send it back on ACK */
1581 hunlock = (HGLOBAL)info->lparam;
1586 SERVER_START_REQ( send_message )
1588 req->id = info->dest_tid;
1589 req->type = info->type;
1591 req->win = wine_server_user_handle( info->hwnd );
1592 req->msg = info->msg;
1593 req->wparam = info->wparam;
1595 req->timeout = TIMEOUT_INFINITE;
1596 for (i = 0; i < data->count; i++)
1597 wine_server_add_data( req, data->data[i], data->size[i] );
1598 if ((res = wine_server_call( req )))
1600 if (res == STATUS_INVALID_PARAMETER)
1601 /* FIXME: find a STATUS_ value for this one */
1602 SetLastError( ERROR_INVALID_THREAD_ID );
1604 SetLastError( RtlNtStatusToDosError(res) );
1607 FreeDDElParam(info->msg, info->lparam);
1610 if (hunlock) GlobalUnlock(hunlock);
1615 /***********************************************************************
1616 * unpack_dde_message
1618 * Unpack a posted DDE message received from another process.
1620 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1621 void **buffer, size_t size )
1623 UINT_PTR uiLo, uiHi;
1632 /* hMem is being passed */
1633 if (size != sizeof(HGLOBAL)) return FALSE;
1634 if (!buffer || !*buffer) return FALSE;
1636 memcpy( &hMem, *buffer, size );
1637 uiHi = (UINT_PTR)hMem;
1638 TRACE("recv dde-ack %lx mem=%lx[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1642 uiLo = LOWORD( *lparam );
1643 uiHi = HIWORD( *lparam );
1644 TRACE("recv dde-ack %lx atom=%lx\n", uiLo, uiHi);
1646 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1651 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1655 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size )))
1657 if ((ptr = GlobalLock( hMem )))
1659 memcpy( ptr, *buffer, size );
1660 GlobalUnlock( hMem );
1668 uiLo = (UINT_PTR)hMem;
1670 *lparam = PackDDElParam( message, uiLo, uiHi );
1672 case WM_DDE_EXECUTE:
1675 if (!buffer || !*buffer) return FALSE;
1676 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size ))) return FALSE;
1677 if ((ptr = GlobalLock( hMem )))
1679 memcpy( ptr, *buffer, size );
1680 GlobalUnlock( hMem );
1681 TRACE( "exec: pairing c=%08lx s=%p\n", *lparam, hMem );
1682 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1693 } else return FALSE;
1694 *lparam = (LPARAM)hMem;
1700 /***********************************************************************
1703 * Call a window procedure and the corresponding hooks.
1705 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1706 BOOL unicode, BOOL same_thread, enum wm_char_mapping mapping )
1710 CWPRETSTRUCT cwpret;
1712 if (msg & 0x80000000)
1714 result = handle_internal_message( hwnd, msg, wparam, lparam );
1718 /* first the WH_CALLWNDPROC hook */
1719 hwnd = WIN_GetFullHandle( hwnd );
1720 cwp.lParam = lparam;
1721 cwp.wParam = wparam;
1724 HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1726 /* now call the window procedure */
1727 if (!WINPROC_call_window( hwnd, msg, wparam, lparam, &result, unicode, mapping )) goto done;
1729 /* and finally the WH_CALLWNDPROCRET hook */
1730 cwpret.lResult = result;
1731 cwpret.lParam = lparam;
1732 cwpret.wParam = wparam;
1733 cwpret.message = msg;
1735 HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1741 /***********************************************************************
1742 * send_parent_notify
1744 * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
1745 * the window has the WS_EX_NOPARENTNOTIFY style.
1747 static void send_parent_notify( HWND hwnd, WORD event, WORD idChild, POINT pt )
1749 /* pt has to be in the client coordinates of the parent window */
1750 MapWindowPoints( 0, hwnd, &pt, 1 );
1755 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)) break;
1756 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
1757 if (!(parent = GetParent(hwnd))) break;
1758 if (parent == GetDesktopWindow()) break;
1759 MapWindowPoints( hwnd, parent, &pt, 1 );
1761 SendMessageW( hwnd, WM_PARENTNOTIFY,
1762 MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
1767 /***********************************************************************
1768 * accept_hardware_message
1770 * Tell the server we have passed the message to the app
1771 * (even though we may end up dropping it later on)
1773 static void accept_hardware_message( UINT hw_id, BOOL remove, HWND new_hwnd )
1775 SERVER_START_REQ( accept_hardware_message )
1778 req->remove = remove;
1779 req->new_win = wine_server_user_handle( new_hwnd );
1780 if (wine_server_call( req ))
1781 FIXME("Failed to reply to MSG_HARDWARE message. Message may not be removed from queue.\n");
1787 /***********************************************************************
1788 * process_keyboard_message
1790 * returns TRUE if the contents of 'msg' should be passed to the application
1792 static BOOL process_keyboard_message( MSG *msg, UINT hw_id, HWND hwnd_filter,
1793 UINT first, UINT last, BOOL remove )
1797 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN ||
1798 msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP)
1799 switch (msg->wParam)
1801 case VK_LSHIFT: case VK_RSHIFT:
1802 msg->wParam = VK_SHIFT;
1804 case VK_LCONTROL: case VK_RCONTROL:
1805 msg->wParam = VK_CONTROL;
1807 case VK_LMENU: case VK_RMENU:
1808 msg->wParam = VK_MENU;
1812 /* FIXME: is this really the right place for this hook? */
1813 event.message = msg->message;
1814 event.hwnd = msg->hwnd;
1815 event.time = msg->time;
1816 event.paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
1817 event.paramH = msg->lParam & 0x7FFF;
1818 if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
1819 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1821 /* check message filters */
1822 if (msg->message < first || msg->message > last) return FALSE;
1823 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1827 if((msg->message == WM_KEYDOWN) &&
1828 (msg->hwnd != GetDesktopWindow()))
1830 /* Handle F1 key by sending out WM_HELP message */
1831 if (msg->wParam == VK_F1)
1833 PostMessageW( msg->hwnd, WM_KEYF1, 0, 0 );
1835 else if(msg->wParam >= VK_BROWSER_BACK &&
1836 msg->wParam <= VK_LAUNCH_APP2)
1838 /* FIXME: Process keystate */
1839 SendMessageW(msg->hwnd, WM_APPCOMMAND, (WPARAM)msg->hwnd, MAKELPARAM(0, (FAPPCOMMAND_KEY | (msg->wParam - VK_BROWSER_BACK + 1))));
1842 else if (msg->message == WM_KEYUP)
1844 /* Handle VK_APPS key by posting a WM_CONTEXTMENU message */
1845 if (msg->wParam == VK_APPS && !MENU_IsMenuActive())
1846 PostMessageW(msg->hwnd, WM_CONTEXTMENU, (WPARAM)msg->hwnd, -1);
1850 if (HOOK_CallHooks( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
1851 LOWORD(msg->wParam), msg->lParam, TRUE ))
1853 /* skip this message */
1854 HOOK_CallHooks( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam, TRUE );
1855 accept_hardware_message( hw_id, TRUE, 0 );
1858 accept_hardware_message( hw_id, remove, 0 );
1860 if ( msg->message == WM_KEYDOWN || msg->message == WM_KEYUP )
1861 if ( ImmProcessKey(msg->hwnd, GetKeyboardLayout(0), msg->wParam, msg->lParam, 0) )
1862 msg->wParam = VK_PROCESSKEY;
1868 /***********************************************************************
1869 * process_mouse_message
1871 * returns TRUE if the contents of 'msg' should be passed to the application
1873 static BOOL process_mouse_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
1874 UINT first, UINT last, BOOL remove )
1883 MOUSEHOOKSTRUCT hook;
1886 /* find the window to dispatch this mouse message to */
1888 GetGUIThreadInfo( GetCurrentThreadId(), &info );
1889 if (info.hwndCapture)
1892 msg->hwnd = info.hwndCapture;
1896 msg->hwnd = WINPOS_WindowFromPoint( msg->hwnd, msg->pt, &hittest );
1899 if (!msg->hwnd || !WIN_IsCurrentThread( msg->hwnd ))
1901 accept_hardware_message( hw_id, TRUE, msg->hwnd );
1905 /* FIXME: is this really the right place for this hook? */
1906 event.message = msg->message;
1907 event.time = msg->time;
1908 event.hwnd = msg->hwnd;
1909 event.paramL = msg->pt.x;
1910 event.paramH = msg->pt.y;
1911 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1913 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1916 message = msg->message;
1917 /* Note: windows has no concept of a non-client wheel message */
1918 if (message != WM_MOUSEWHEEL)
1920 if (hittest != HTCLIENT)
1922 message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
1923 msg->wParam = hittest;
1927 /* coordinates don't get translated while tracking a menu */
1928 /* FIXME: should differentiate popups and top-level menus */
1929 if (!(info.flags & GUI_INMENUMODE))
1930 ScreenToClient( msg->hwnd, &pt );
1933 msg->lParam = MAKELONG( pt.x, pt.y );
1935 /* translate double clicks */
1937 if ((msg->message == WM_LBUTTONDOWN) ||
1938 (msg->message == WM_RBUTTONDOWN) ||
1939 (msg->message == WM_MBUTTONDOWN) ||
1940 (msg->message == WM_XBUTTONDOWN))
1942 BOOL update = remove;
1944 /* translate double clicks -
1945 * note that ...MOUSEMOVEs can slip in between
1946 * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
1948 if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
1949 hittest != HTCLIENT ||
1950 (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
1952 if ((msg->message == clk_msg.message) &&
1953 (msg->hwnd == clk_msg.hwnd) &&
1954 (msg->wParam == clk_msg.wParam) &&
1955 (msg->time - clk_msg.time < GetDoubleClickTime()) &&
1956 (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
1957 (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
1959 message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
1962 clk_msg.message = 0; /* clear the double click conditions */
1967 if (message < first || message > last) return FALSE;
1968 /* update static double click conditions */
1969 if (update) clk_msg = *msg;
1973 if (message < first || message > last) return FALSE;
1976 /* message is accepted now (but may still get dropped) */
1979 hook.hwnd = msg->hwnd;
1980 hook.wHitTestCode = hittest;
1981 hook.dwExtraInfo = extra_info;
1982 if (HOOK_CallHooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
1983 message, (LPARAM)&hook, TRUE ))
1986 hook.hwnd = msg->hwnd;
1987 hook.wHitTestCode = hittest;
1988 hook.dwExtraInfo = extra_info;
1989 HOOK_CallHooks( WH_CBT, HCBT_CLICKSKIPPED, message, (LPARAM)&hook, TRUE );
1990 accept_hardware_message( hw_id, TRUE, 0 );
1994 if ((hittest == HTERROR) || (hittest == HTNOWHERE))
1996 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
1997 MAKELONG( hittest, msg->message ));
1998 accept_hardware_message( hw_id, TRUE, 0 );
2002 accept_hardware_message( hw_id, remove, 0 );
2004 if (!remove || info.hwndCapture)
2006 msg->message = message;
2012 if ((msg->message == WM_LBUTTONDOWN) ||
2013 (msg->message == WM_RBUTTONDOWN) ||
2014 (msg->message == WM_MBUTTONDOWN) ||
2015 (msg->message == WM_XBUTTONDOWN))
2017 /* Send the WM_PARENTNOTIFY,
2018 * note that even for double/nonclient clicks
2019 * notification message is still WM_L/M/RBUTTONDOWN.
2021 send_parent_notify( msg->hwnd, msg->message, 0, msg->pt );
2023 /* Activate the window if needed */
2025 if (msg->hwnd != info.hwndActive)
2027 HWND hwndTop = msg->hwnd;
2030 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
2031 hwndTop = GetParent( hwndTop );
2034 if (hwndTop && hwndTop != GetDesktopWindow())
2036 LONG ret = SendMessageW( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
2037 MAKELONG( hittest, msg->message ) );
2040 case MA_NOACTIVATEANDEAT:
2045 case MA_ACTIVATEANDEAT:
2050 if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
2053 WARN( "unknown WM_MOUSEACTIVATE code %d\n", ret );
2060 /* send the WM_SETCURSOR message */
2062 /* Windows sends the normal mouse message as the message parameter
2063 in the WM_SETCURSOR message even if it's non-client mouse message */
2064 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd, MAKELONG( hittest, msg->message ));
2066 msg->message = message;
2071 /***********************************************************************
2072 * process_hardware_message
2074 * Process a hardware message; return TRUE if message should be passed on to the app
2076 static BOOL process_hardware_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
2077 UINT first, UINT last, BOOL remove )
2079 if (is_keyboard_message( msg->message ))
2080 return process_keyboard_message( msg, hw_id, hwnd_filter, first, last, remove );
2082 if (is_mouse_message( msg->message ))
2083 return process_mouse_message( msg, hw_id, extra_info, hwnd_filter, first, last, remove );
2085 ERR( "unknown message type %x\n", msg->message );
2090 /***********************************************************************
2091 * call_sendmsg_callback
2093 * Call the callback function of SendMessageCallback.
2095 static inline void call_sendmsg_callback( SENDASYNCPROC callback, HWND hwnd, UINT msg,
2096 ULONG_PTR data, LRESULT result )
2098 if (!callback) return;
2100 if (TRACE_ON(relay))
2101 DPRINTF( "%04x:Call message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
2102 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
2104 callback( hwnd, msg, data, result );
2105 if (TRACE_ON(relay))
2106 DPRINTF( "%04x:Ret message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
2107 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
2112 /***********************************************************************
2115 * Peek for a message matching the given parameters. Return FALSE if none available.
2116 * All pending sent messages are processed before returning.
2118 static BOOL peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags, UINT changed_mask )
2121 struct user_thread_info *thread_info = get_user_thread_info();
2122 struct received_message_info info, *old_info;
2123 unsigned int hw_id = 0; /* id of previous hardware message */
2125 size_t buffer_size = 256;
2127 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size ))) return FALSE;
2129 if (!first && !last) last = ~0;
2130 if (hwnd == HWND_BROADCAST) hwnd = HWND_TOPMOST;
2136 const message_data_t *msg_data = buffer;
2138 SERVER_START_REQ( get_message )
2141 req->get_win = wine_server_user_handle( hwnd );
2142 req->get_first = first;
2143 req->get_last = last;
2145 req->wake_mask = changed_mask & (QS_SENDMESSAGE | QS_SMRESULT);
2146 req->changed_mask = changed_mask;
2147 wine_server_set_reply( req, buffer, buffer_size );
2148 if (!(res = wine_server_call( req )))
2150 size = wine_server_reply_size( reply );
2151 info.type = reply->type;
2152 info.msg.hwnd = wine_server_ptr_handle( reply->win );
2153 info.msg.message = reply->msg;
2154 info.msg.wParam = reply->wparam;
2155 info.msg.lParam = reply->lparam;
2156 info.msg.time = reply->time;
2160 thread_info->active_hooks = reply->active_hooks;
2162 else buffer_size = reply->total;
2168 HeapFree( GetProcessHeap(), 0, buffer );
2169 if (res != STATUS_BUFFER_OVERFLOW) return FALSE;
2170 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size ))) return FALSE;
2174 TRACE( "got type %d msg %x (%s) hwnd %p wp %lx lp %lx\n",
2175 info.type, info.msg.message,
2176 (info.type == MSG_WINEVENT) ? "MSG_WINEVENT" : SPY_GetMsgName(info.msg.message, info.msg.hwnd),
2177 info.msg.hwnd, info.msg.wParam, info.msg.lParam );
2183 info.flags = ISMEX_SEND;
2186 info.flags = ISMEX_NOTIFY;
2189 info.flags = ISMEX_CALLBACK;
2191 case MSG_CALLBACK_RESULT:
2192 if (size >= sizeof(msg_data->callback))
2193 call_sendmsg_callback( wine_server_get_ptr(msg_data->callback.callback),
2194 info.msg.hwnd, info.msg.message,
2195 msg_data->callback.data, msg_data->callback.result );
2198 if (size >= sizeof(msg_data->winevent))
2200 WINEVENTPROC hook_proc;
2202 hook_proc = wine_server_get_ptr( msg_data->winevent.hook_proc );
2203 size -= sizeof(msg_data->winevent);
2206 WCHAR module[MAX_PATH];
2208 size = min( size, (MAX_PATH - 1) * sizeof(WCHAR) );
2209 memcpy( module, &msg_data->winevent + 1, size );
2210 module[size / sizeof(WCHAR)] = 0;
2211 if (!(hook_proc = get_hook_proc( hook_proc, module )))
2213 ERR( "invalid winevent hook module name %s\n", debugstr_w(module) );
2218 if (TRACE_ON(relay))
2219 DPRINTF( "%04x:Call winevent proc %p (hook=%04x,event=%x,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04x,time=%x)\n",
2220 GetCurrentThreadId(), hook_proc,
2221 msg_data->winevent.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2222 info.msg.lParam, msg_data->winevent.tid, info.msg.time);
2224 hook_proc( wine_server_ptr_handle( msg_data->winevent.hook ), info.msg.message,
2225 info.msg.hwnd, info.msg.wParam, info.msg.lParam,
2226 msg_data->winevent.tid, info.msg.time );
2228 if (TRACE_ON(relay))
2229 DPRINTF( "%04x:Ret winevent proc %p (hook=%04x,event=%x,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04x,time=%x)\n",
2230 GetCurrentThreadId(), hook_proc,
2231 msg_data->winevent.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2232 info.msg.lParam, msg_data->winevent.tid, info.msg.time);
2235 case MSG_OTHER_PROCESS:
2236 info.flags = ISMEX_SEND;
2237 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2238 &info.msg.lParam, &buffer, size ))
2241 reply_message( &info, 0, TRUE );
2246 if (size >= sizeof(msg_data->hardware))
2248 info.msg.pt.x = msg_data->hardware.x;
2249 info.msg.pt.y = msg_data->hardware.y;
2250 hw_id = msg_data->hardware.hw_id;
2251 if (!process_hardware_message( &info.msg, hw_id, msg_data->hardware.info,
2252 hwnd, first, last, flags & PM_REMOVE ))
2254 TRACE("dropping msg %x\n", info.msg.message );
2255 continue; /* ignore it */
2258 thread_info->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
2259 thread_info->GetMessageTimeVal = info.msg.time;
2260 thread_info->GetMessageExtraInfoVal = msg_data->hardware.info;
2261 HeapFree( GetProcessHeap(), 0, buffer );
2262 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)msg, TRUE );
2267 if (info.msg.message & 0x80000000) /* internal message */
2269 if (flags & PM_REMOVE)
2271 handle_internal_message( info.msg.hwnd, info.msg.message,
2272 info.msg.wParam, info.msg.lParam );
2273 /* if this is a nested call return right away */
2274 if (first == info.msg.message && last == info.msg.message) return FALSE;
2277 peek_message( msg, info.msg.hwnd, info.msg.message,
2278 info.msg.message, flags | PM_REMOVE, changed_mask );
2281 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
2283 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2284 &info.msg.lParam, &buffer, size ))
2285 continue; /* ignore it */
2288 msg->pt.x = (short)LOWORD( thread_info->GetMessagePosVal );
2289 msg->pt.y = (short)HIWORD( thread_info->GetMessagePosVal );
2290 thread_info->GetMessageTimeVal = info.msg.time;
2291 thread_info->GetMessageExtraInfoVal = 0;
2292 HeapFree( GetProcessHeap(), 0, buffer );
2293 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)msg, TRUE );
2297 /* if we get here, we have a sent message; call the window procedure */
2298 old_info = thread_info->receive_info;
2299 thread_info->receive_info = &info;
2300 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
2301 info.msg.lParam, (info.type != MSG_ASCII), FALSE,
2302 WMCHAR_MAP_RECVMESSAGE );
2303 reply_message( &info, result, TRUE );
2304 thread_info->receive_info = old_info;
2306 /* if some PM_QS* flags were specified, only handle sent messages from now on */
2307 if (HIWORD(flags) && !changed_mask) flags = PM_QS_SENDMESSAGE | LOWORD(flags);
2312 /***********************************************************************
2313 * process_sent_messages
2315 * Process all pending sent messages.
2317 static inline void process_sent_messages(void)
2320 peek_message( &msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE, 0 );
2324 /***********************************************************************
2325 * get_server_queue_handle
2327 * Get a handle to the server message queue for the current thread.
2329 static HANDLE get_server_queue_handle(void)
2331 struct user_thread_info *thread_info = get_user_thread_info();
2334 if (!(ret = thread_info->server_queue))
2336 SERVER_START_REQ( get_msg_queue )
2338 wine_server_call( req );
2339 ret = wine_server_ptr_handle( reply->handle );
2342 thread_info->server_queue = ret;
2343 if (!ret) ERR( "Cannot get server thread queue\n" );
2349 /***********************************************************************
2350 * wait_message_reply
2352 * Wait until a sent message gets replied to.
2354 static void wait_message_reply( UINT flags )
2356 HANDLE server_queue = get_server_queue_handle();
2360 unsigned int wake_bits = 0;
2362 SERVER_START_REQ( set_queue_mask )
2364 req->wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
2365 req->changed_mask = req->wake_mask;
2367 if (!wine_server_call( req ))
2368 wake_bits = reply->wake_bits;
2372 if (wake_bits & QS_SMRESULT) return; /* got a result */
2373 if (wake_bits & QS_SENDMESSAGE)
2375 /* Process the sent message immediately */
2376 process_sent_messages();
2380 wow_handlers.wait_message( 1, &server_queue, INFINITE, QS_SENDMESSAGE, 0 );
2384 /***********************************************************************
2385 * put_message_in_queue
2387 * Put a sent message into the destination queue.
2388 * For inter-process message, reply_size is set to expected size of reply data.
2390 static BOOL put_message_in_queue( const struct send_message_info *info, size_t *reply_size )
2392 struct packed_message data;
2393 message_data_t msg_data;
2396 timeout_t timeout = TIMEOUT_INFINITE;
2398 /* Check for INFINITE timeout for compatibility with Win9x,
2399 * although Windows >= NT does not do so
2401 if (info->type != MSG_NOTIFY &&
2402 info->type != MSG_CALLBACK &&
2403 info->type != MSG_POSTED &&
2405 info->timeout != INFINITE)
2407 /* timeout is signed despite the prototype */
2408 timeout = (timeout_t)max( 0, (int)info->timeout ) * -10000;
2411 memset( &data, 0, sizeof(data) );
2412 if (info->type == MSG_OTHER_PROCESS)
2414 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
2415 if (data.count == -1)
2417 WARN( "cannot pack message %x\n", info->msg );
2421 else if (info->type == MSG_CALLBACK)
2423 msg_data.callback.callback = wine_server_client_ptr( info->callback );
2424 msg_data.callback.data = info->data;
2425 msg_data.callback.result = 0;
2426 data.data[0] = &msg_data;
2427 data.size[0] = sizeof(msg_data.callback);
2430 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
2432 return post_dde_message( &data, info );
2435 SERVER_START_REQ( send_message )
2437 req->id = info->dest_tid;
2438 req->type = info->type;
2440 req->win = wine_server_user_handle( info->hwnd );
2441 req->msg = info->msg;
2442 req->wparam = info->wparam;
2443 req->lparam = info->lparam;
2444 req->timeout = timeout;
2446 if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
2447 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
2448 if ((res = wine_server_call( req )))
2450 if (res == STATUS_INVALID_PARAMETER)
2451 /* FIXME: find a STATUS_ value for this one */
2452 SetLastError( ERROR_INVALID_THREAD_ID );
2454 SetLastError( RtlNtStatusToDosError(res) );
2462 /***********************************************************************
2465 * Retrieve a message reply from the server.
2467 static LRESULT retrieve_reply( const struct send_message_info *info,
2468 size_t reply_size, LRESULT *result )
2471 void *reply_data = NULL;
2475 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
2477 WARN( "no memory for reply, will be truncated\n" );
2481 SERVER_START_REQ( get_message_reply )
2484 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
2485 if (!(status = wine_server_call( req ))) *result = reply->result;
2486 reply_size = wine_server_reply_size( reply );
2489 if (!status && reply_size)
2490 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
2492 HeapFree( GetProcessHeap(), 0, reply_data );
2494 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx got reply %lx (err=%d)\n",
2495 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
2496 info->lparam, *result, status );
2498 /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
2499 if (status) SetLastError( RtlNtStatusToDosError(status) );
2504 /***********************************************************************
2505 * send_inter_thread_message
2507 static LRESULT send_inter_thread_message( const struct send_message_info *info, LRESULT *res_ptr )
2509 size_t reply_size = 0;
2511 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
2512 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
2514 USER_CheckNotLock();
2516 if (!put_message_in_queue( info, &reply_size )) return 0;
2518 /* there's no reply to wait for on notify/callback messages */
2519 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
2521 wait_message_reply( info->flags );
2522 return retrieve_reply( info, reply_size, res_ptr );
2526 /***********************************************************************
2527 * send_inter_thread_callback
2529 static LRESULT send_inter_thread_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
2530 LRESULT *result, void *arg )
2532 struct send_message_info *info = arg;
2537 return send_inter_thread_message( info, result );
2541 /***********************************************************************
2544 * Backend implementation of the various SendMessage functions.
2546 static BOOL send_message( struct send_message_info *info, DWORD_PTR *res_ptr, BOOL unicode )
2552 if (is_broadcast(info->hwnd))
2554 EnumWindows( broadcast_message_callback, (LPARAM)info );
2555 if (res_ptr) *res_ptr = 1;
2559 if (!(info->dest_tid = GetWindowThreadProcessId( info->hwnd, &dest_pid ))) return FALSE;
2561 if (USER_IsExitingThread( info->dest_tid )) return FALSE;
2563 SPY_EnterMessage( SPY_SENDMESSAGE, info->hwnd, info->msg, info->wparam, info->lparam );
2565 if (info->dest_tid == GetCurrentThreadId())
2567 result = call_window_proc( info->hwnd, info->msg, info->wparam, info->lparam,
2568 unicode, TRUE, info->wm_char );
2569 if (info->type == MSG_CALLBACK)
2570 call_sendmsg_callback( info->callback, info->hwnd, info->msg, info->data, result );
2575 if (dest_pid != GetCurrentProcessId() && (info->type == MSG_ASCII || info->type == MSG_UNICODE))
2576 info->type = MSG_OTHER_PROCESS;
2578 /* MSG_ASCII can be sent unconverted except for WM_CHAR; everything else needs to be Unicode */
2579 if (!unicode && is_unicode_message( info->msg ) &&
2580 (info->type != MSG_ASCII || info->msg == WM_CHAR))
2581 ret = WINPROC_CallProcAtoW( send_inter_thread_callback, info->hwnd, info->msg,
2582 info->wparam, info->lparam, &result, info, info->wm_char );
2584 ret = send_inter_thread_message( info, &result );
2587 SPY_ExitMessage( SPY_RESULT_OK, info->hwnd, info->msg, result, info->wparam, info->lparam );
2588 if (ret && res_ptr) *res_ptr = result;
2593 /***********************************************************************
2594 * MSG_SendInternalMessageTimeout
2596 * Same as SendMessageTimeoutW but sends the message to a specific thread
2597 * without requiring a window handle. Only works for internal Wine messages.
2599 LRESULT MSG_SendInternalMessageTimeout( DWORD dest_pid, DWORD dest_tid,
2600 UINT msg, WPARAM wparam, LPARAM lparam,
2601 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2603 struct send_message_info info;
2604 LRESULT ret, result;
2606 assert( msg & 0x80000000 ); /* must be an internal Wine message */
2608 info.type = MSG_UNICODE;
2609 info.dest_tid = dest_tid;
2612 info.wparam = wparam;
2613 info.lparam = lparam;
2615 info.timeout = timeout;
2617 if (USER_IsExitingThread( dest_tid )) return 0;
2619 if (dest_tid == GetCurrentThreadId())
2621 result = handle_internal_message( 0, msg, wparam, lparam );
2626 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
2627 ret = send_inter_thread_message( &info, &result );
2629 if (ret && res_ptr) *res_ptr = result;
2634 /***********************************************************************
2635 * SendMessageTimeoutW (USER32.@)
2637 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2638 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2640 struct send_message_info info;
2642 info.type = MSG_UNICODE;
2645 info.wparam = wparam;
2646 info.lparam = lparam;
2648 info.timeout = timeout;
2650 return send_message( &info, res_ptr, TRUE );
2653 /***********************************************************************
2654 * SendMessageTimeoutA (USER32.@)
2656 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2657 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2659 struct send_message_info info;
2661 info.type = MSG_ASCII;
2664 info.wparam = wparam;
2665 info.lparam = lparam;
2667 info.timeout = timeout;
2668 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
2670 return send_message( &info, res_ptr, FALSE );
2674 /***********************************************************************
2675 * SendMessageW (USER32.@)
2677 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2680 struct send_message_info info;
2682 info.type = MSG_UNICODE;
2685 info.wparam = wparam;
2686 info.lparam = lparam;
2687 info.flags = SMTO_NORMAL;
2690 send_message( &info, &res, TRUE );
2695 /***********************************************************************
2696 * SendMessageA (USER32.@)
2698 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2701 struct send_message_info info;
2703 info.type = MSG_ASCII;
2706 info.wparam = wparam;
2707 info.lparam = lparam;
2708 info.flags = SMTO_NORMAL;
2710 info.wm_char = WMCHAR_MAP_SENDMESSAGE;
2712 send_message( &info, &res, FALSE );
2717 /***********************************************************************
2718 * SendNotifyMessageA (USER32.@)
2720 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2722 struct send_message_info info;
2724 if (is_pointer_message(msg))
2726 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2730 info.type = MSG_NOTIFY;
2733 info.wparam = wparam;
2734 info.lparam = lparam;
2736 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
2738 return send_message( &info, NULL, FALSE );
2742 /***********************************************************************
2743 * SendNotifyMessageW (USER32.@)
2745 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2747 struct send_message_info info;
2749 if (is_pointer_message(msg))
2751 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2755 info.type = MSG_NOTIFY;
2758 info.wparam = wparam;
2759 info.lparam = lparam;
2762 return send_message( &info, NULL, TRUE );
2766 /***********************************************************************
2767 * SendMessageCallbackA (USER32.@)
2769 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2770 SENDASYNCPROC callback, ULONG_PTR data )
2772 struct send_message_info info;
2774 if (is_pointer_message(msg))
2776 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2780 info.type = MSG_CALLBACK;
2783 info.wparam = wparam;
2784 info.lparam = lparam;
2785 info.callback = callback;
2788 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
2790 return send_message( &info, NULL, FALSE );
2794 /***********************************************************************
2795 * SendMessageCallbackW (USER32.@)
2797 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2798 SENDASYNCPROC callback, ULONG_PTR data )
2800 struct send_message_info info;
2802 if (is_pointer_message(msg))
2804 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2808 info.type = MSG_CALLBACK;
2811 info.wparam = wparam;
2812 info.lparam = lparam;
2813 info.callback = callback;
2817 return send_message( &info, NULL, TRUE );
2821 /***********************************************************************
2822 * ReplyMessage (USER32.@)
2824 BOOL WINAPI ReplyMessage( LRESULT result )
2826 struct received_message_info *info = get_user_thread_info()->receive_info;
2828 if (!info) return FALSE;
2829 reply_message( info, result, FALSE );
2834 /***********************************************************************
2835 * InSendMessage (USER32.@)
2837 BOOL WINAPI InSendMessage(void)
2839 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
2843 /***********************************************************************
2844 * InSendMessageEx (USER32.@)
2846 DWORD WINAPI InSendMessageEx( LPVOID reserved )
2848 struct received_message_info *info = get_user_thread_info()->receive_info;
2850 if (info) return info->flags;
2851 return ISMEX_NOSEND;
2855 /***********************************************************************
2856 * PostMessageA (USER32.@)
2858 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2860 if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
2861 return PostMessageW( hwnd, msg, wparam, lparam );
2865 /***********************************************************************
2866 * PostMessageW (USER32.@)
2868 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2870 struct send_message_info info;
2872 if (is_pointer_message( msg ))
2874 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2878 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
2879 hwnd, msg, SPY_GetMsgName(msg, hwnd), wparam, lparam );
2881 info.type = MSG_POSTED;
2884 info.wparam = wparam;
2885 info.lparam = lparam;
2888 if (is_broadcast(hwnd))
2890 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2894 if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2896 if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2898 if (USER_IsExitingThread( info.dest_tid )) return TRUE;
2900 return put_message_in_queue( &info, NULL );
2904 /**********************************************************************
2905 * PostThreadMessageA (USER32.@)
2907 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2909 if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
2910 return PostThreadMessageW( thread, msg, wparam, lparam );
2914 /**********************************************************************
2915 * PostThreadMessageW (USER32.@)
2917 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2919 struct send_message_info info;
2921 if (is_pointer_message( msg ))
2923 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2926 if (USER_IsExitingThread( thread )) return TRUE;
2928 info.type = MSG_POSTED;
2929 info.dest_tid = thread;
2932 info.wparam = wparam;
2933 info.lparam = lparam;
2935 return put_message_in_queue( &info, NULL );
2939 /***********************************************************************
2940 * PostQuitMessage (USER32.@)
2942 * Posts a quit message to the current thread's message queue.
2945 * exit_code [I] Exit code to return from message loop.
2951 * This function is not the same as calling:
2952 *|PostThreadMessage(GetCurrentThreadId(), WM_QUIT, exit_code, 0);
2953 * It instead sets a flag in the message queue that signals it to generate
2954 * a WM_QUIT message when there are no other pending sent or posted messages
2957 void WINAPI PostQuitMessage( INT exit_code )
2959 SERVER_START_REQ( post_quit_message )
2961 req->exit_code = exit_code;
2962 wine_server_call( req );
2968 /***********************************************************************
2969 * PeekMessageW (USER32.@)
2971 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2975 USER_CheckNotLock();
2977 /* check for graphics events */
2978 USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
2980 if (!peek_message( &msg, hwnd, first, last, flags, 0 ))
2982 if (!(flags & PM_NOYIELD)) wow_handlers.wait_message( 0, NULL, 0, 0, 0 );
2986 /* copy back our internal safe copy of message data to msg_out.
2987 * msg_out is a variable from the *program*, so it can't be used
2988 * internally as it can get "corrupted" by our use of SendMessage()
2989 * (back to the program) inside the message handling itself. */
2992 SetLastError( ERROR_NOACCESS );
3000 /***********************************************************************
3001 * PeekMessageA (USER32.@)
3003 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
3005 if (get_pending_wmchar( msg, first, last, (flags & PM_REMOVE) )) return TRUE;
3006 if (!PeekMessageW( msg, hwnd, first, last, flags )) return FALSE;
3007 map_wparam_WtoA( msg, (flags & PM_REMOVE) );
3012 /***********************************************************************
3013 * GetMessageW (USER32.@)
3015 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
3017 HANDLE server_queue = get_server_queue_handle();
3018 unsigned int mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
3020 USER_CheckNotLock();
3022 /* check for graphics events */
3023 USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
3027 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
3028 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
3029 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
3030 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
3031 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
3032 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
3034 else mask = QS_ALLINPUT;
3036 while (!peek_message( msg, hwnd, first, last, PM_REMOVE | (mask << 16), mask ))
3038 wow_handlers.wait_message( 1, &server_queue, INFINITE, mask, 0 );
3041 return (msg->message != WM_QUIT);
3045 /***********************************************************************
3046 * GetMessageA (USER32.@)
3048 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
3050 if (get_pending_wmchar( msg, first, last, TRUE )) return TRUE;
3051 GetMessageW( msg, hwnd, first, last );
3052 map_wparam_WtoA( msg, TRUE );
3053 return (msg->message != WM_QUIT);
3057 /***********************************************************************
3058 * IsDialogMessageA (USER32.@)
3059 * IsDialogMessage (USER32.@)
3061 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
3064 map_wparam_AtoW( msg.message, &msg.wParam, WMCHAR_MAP_NOMAPPING );
3065 return IsDialogMessageW( hwndDlg, &msg );
3069 /***********************************************************************
3070 * TranslateMessage (USER32.@)
3072 * Implementation of TranslateMessage.
3074 * TranslateMessage translates virtual-key messages into character-messages,
3076 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
3077 * ditto replacing WM_* with WM_SYS*
3078 * This produces WM_CHAR messages only for keys mapped to ASCII characters
3079 * by the keyboard driver.
3081 * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
3082 * return value is nonzero, regardless of the translation.
3085 BOOL WINAPI TranslateMessage( const MSG *msg )
3091 if (msg->message < WM_KEYFIRST || msg->message > WM_KEYLAST) return FALSE;
3092 if (msg->message != WM_KEYDOWN && msg->message != WM_SYSKEYDOWN) return TRUE;
3094 TRACE_(key)("Translating key %s (%04lX), scancode %04x\n",
3095 SPY_GetVKeyName(msg->wParam), msg->wParam, HIWORD(msg->lParam));
3097 switch (msg->wParam)
3100 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
3101 TRACE_(key)("PostMessageW(%p,%s,%04x,%08x)\n",
3102 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), HIWORD(msg->lParam), LOWORD(msg->lParam));
3103 PostMessageW( msg->hwnd, message, HIWORD(msg->lParam), LOWORD(msg->lParam));
3107 return ImmTranslateMessage(msg->hwnd, msg->message, msg->wParam, msg->lParam);
3110 GetKeyboardState( state );
3111 /* FIXME : should handle ToUnicode yielding 2 */
3112 switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), state, wp, 2, 0))
3115 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
3116 TRACE_(key)("1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
3117 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
3118 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
3122 message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
3123 TRACE_(key)("-1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
3124 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
3125 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
3132 /***********************************************************************
3133 * DispatchMessageA (USER32.@)
3135 * See DispatchMessageW.
3137 LRESULT WINAPI DECLSPEC_HOTPATCH DispatchMessageA( const MSG* msg )
3141 /* Process timer messages */
3142 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3148 retval = CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
3149 msg->message, msg->wParam, GetTickCount() );
3159 if (!msg->hwnd) return 0;
3161 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3162 msg->wParam, msg->lParam );
3164 if (!WINPROC_call_window( msg->hwnd, msg->message, msg->wParam, msg->lParam,
3165 &retval, FALSE, WMCHAR_MAP_DISPATCHMESSAGE ))
3167 if (!IsWindow( msg->hwnd )) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3168 else SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3172 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3173 msg->wParam, msg->lParam );
3175 if (msg->message == WM_PAINT)
3177 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3178 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3179 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3180 DeleteObject( hrgn );
3186 /***********************************************************************
3187 * DispatchMessageW (USER32.@) Process a message
3189 * Process the message specified in the structure *_msg_.
3191 * If the lpMsg parameter points to a WM_TIMER message and the
3192 * parameter of the WM_TIMER message is not NULL, the lParam parameter
3193 * points to the function that is called instead of the window
3194 * procedure. The function stored in lParam (timer callback) is protected
3195 * from causing page-faults.
3197 * The message must be valid.
3201 * DispatchMessage() returns the result of the window procedure invoked.
3208 LRESULT WINAPI DECLSPEC_HOTPATCH DispatchMessageW( const MSG* msg )
3212 /* Process timer messages */
3213 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3219 retval = CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
3220 msg->message, msg->wParam, GetTickCount() );
3230 if (!msg->hwnd) return 0;
3232 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3233 msg->wParam, msg->lParam );
3235 if (!WINPROC_call_window( msg->hwnd, msg->message, msg->wParam, msg->lParam,
3236 &retval, TRUE, WMCHAR_MAP_DISPATCHMESSAGE ))
3238 if (!IsWindow( msg->hwnd )) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3239 else SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3243 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3244 msg->wParam, msg->lParam );
3246 if (msg->message == WM_PAINT)
3248 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3249 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3250 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3251 DeleteObject( hrgn );
3257 /***********************************************************************
3258 * GetMessagePos (USER.119)
3259 * GetMessagePos (USER32.@)
3261 * The GetMessagePos() function returns a long value representing a
3262 * cursor position, in screen coordinates, when the last message
3263 * retrieved by the GetMessage() function occurs. The x-coordinate is
3264 * in the low-order word of the return value, the y-coordinate is in
3265 * the high-order word. The application can use the MAKEPOINT()
3266 * macro to obtain a POINT structure from the return value.
3268 * For the current cursor position, use GetCursorPos().
3272 * Cursor position of last message on success, zero on failure.
3279 DWORD WINAPI GetMessagePos(void)
3281 return get_user_thread_info()->GetMessagePosVal;
3285 /***********************************************************************
3286 * GetMessageTime (USER.120)
3287 * GetMessageTime (USER32.@)
3289 * GetMessageTime() returns the message time for the last message
3290 * retrieved by the function. The time is measured in milliseconds with
3291 * the same offset as GetTickCount().
3293 * Since the tick count wraps, this is only useful for moderately short
3294 * relative time comparisons.
3298 * Time of last message on success, zero on failure.
3300 LONG WINAPI GetMessageTime(void)
3302 return get_user_thread_info()->GetMessageTimeVal;
3306 /***********************************************************************
3307 * GetMessageExtraInfo (USER.288)
3308 * GetMessageExtraInfo (USER32.@)
3310 LPARAM WINAPI GetMessageExtraInfo(void)
3312 return get_user_thread_info()->GetMessageExtraInfoVal;
3316 /***********************************************************************
3317 * SetMessageExtraInfo (USER32.@)
3319 LPARAM WINAPI SetMessageExtraInfo(LPARAM lParam)
3321 struct user_thread_info *thread_info = get_user_thread_info();
3322 LONG old_value = thread_info->GetMessageExtraInfoVal;
3323 thread_info->GetMessageExtraInfoVal = lParam;
3328 /***********************************************************************
3329 * WaitMessage (USER.112) Suspend thread pending messages
3330 * WaitMessage (USER32.@) Suspend thread pending messages
3332 * WaitMessage() suspends a thread until events appear in the thread's
3335 BOOL WINAPI WaitMessage(void)
3337 return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
3341 /***********************************************************************
3342 * MsgWaitForMultipleObjectsEx (USER32.@)
3344 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
3345 DWORD timeout, DWORD mask, DWORD flags )
3347 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
3350 if (count > MAXIMUM_WAIT_OBJECTS-1)
3352 SetLastError( ERROR_INVALID_PARAMETER );
3356 /* set the queue mask */
3357 SERVER_START_REQ( set_queue_mask )
3359 req->wake_mask = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
3360 req->changed_mask = mask;
3362 wine_server_call( req );
3366 /* add the queue to the handle list */
3367 for (i = 0; i < count; i++) handles[i] = pHandles[i];
3368 handles[count] = get_server_queue_handle();
3370 return wow_handlers.wait_message( count+1, handles, timeout, mask, flags );
3374 /***********************************************************************
3375 * MsgWaitForMultipleObjects (USER32.@)
3377 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
3378 BOOL wait_all, DWORD timeout, DWORD mask )
3380 return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
3381 wait_all ? MWMO_WAITALL : 0 );
3385 /***********************************************************************
3386 * WaitForInputIdle (USER32.@)
3388 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
3390 DWORD start_time, elapsed, ret;
3393 handles[0] = hProcess;
3394 SERVER_START_REQ( get_process_idle_event )
3396 req->handle = wine_server_obj_handle( hProcess );
3397 wine_server_call_err( req );
3398 handles[1] = wine_server_ptr_handle( reply->event );
3401 if (!handles[1]) return WAIT_FAILED; /* no event to wait on */
3403 start_time = GetTickCount();
3406 TRACE("waiting for %p\n", handles[1] );
3409 ret = MsgWaitForMultipleObjects ( 2, handles, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
3414 case WAIT_OBJECT_0+2:
3415 process_sent_messages();
3419 TRACE("timeout or error\n");
3422 TRACE("finished\n");
3425 if (dwTimeOut != INFINITE)
3427 elapsed = GetTickCount() - start_time;
3428 if (elapsed > dwTimeOut)
3434 return WAIT_TIMEOUT;
3438 /***********************************************************************
3439 * RegisterWindowMessageA (USER32.@)
3440 * RegisterWindowMessage (USER.118)
3442 UINT WINAPI RegisterWindowMessageA( LPCSTR str )
3444 UINT ret = GlobalAddAtomA(str);
3445 TRACE("%s, ret=%x\n", str, ret);
3450 /***********************************************************************
3451 * RegisterWindowMessageW (USER32.@)
3453 UINT WINAPI RegisterWindowMessageW( LPCWSTR str )
3455 UINT ret = GlobalAddAtomW(str);
3456 TRACE("%s ret=%x\n", debugstr_w(str), ret);
3460 typedef struct BroadcastParm
3471 static BOOL CALLBACK bcast_childwindow( HWND hw, LPARAM lp )
3473 BroadcastParm *parm = (BroadcastParm*)lp;
3474 DWORD_PTR retval = 0;
3477 if (parm->flags & BSF_IGNORECURRENTTASK && WIN_IsCurrentProcess(hw))
3479 TRACE("Not telling myself %p\n", hw);
3483 /* I don't know 100% for sure if this is what Windows does, but it fits the tests */
3484 if (parm->flags & BSF_QUERY)
3486 TRACE("Telling window %p using SendMessageTimeout\n", hw);
3488 /* Not tested for conflicting flags */
3489 if (parm->flags & BSF_FORCEIFHUNG || parm->flags & BSF_NOHANG)
3490 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_ABORTIFHUNG, 2000, &retval );
3491 else if (parm->flags & BSF_NOTIMEOUTIFNOTHUNG)
3492 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NOTIMEOUTIFNOTHUNG, 2000, &retval );
3494 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NORMAL, 2000, &retval );
3496 if (!lresult && GetLastError() == ERROR_TIMEOUT)
3498 WARN("Timed out!\n");
3499 if (!(parm->flags & BSF_FORCEIFHUNG))
3502 if (retval == BROADCAST_QUERY_DENY)
3511 else if (parm->flags & BSF_POSTMESSAGE)
3513 TRACE("Telling window %p using PostMessage\n", hw);
3514 PostMessageW( hw, parm->msg, parm->wp, parm->lp );
3518 TRACE("Telling window %p using SendNotifyMessage\n", hw);
3519 SendNotifyMessageW( hw, parm->msg, parm->wp, parm->lp );
3525 static BOOL CALLBACK bcast_desktop( LPWSTR desktop, LPARAM lp )
3529 BroadcastParm *parm = (BroadcastParm*)lp;
3531 TRACE("desktop: %s\n", debugstr_w( desktop ));
3533 hdesktop = open_winstation_desktop( parm->winsta, desktop, 0, FALSE, DESKTOP_ENUMERATE|DESKTOP_WRITEOBJECTS|STANDARD_RIGHTS_WRITE );
3536 FIXME("Could not open desktop %s\n", debugstr_w(desktop));
3540 ret = EnumDesktopWindows( hdesktop, bcast_childwindow, lp );
3541 CloseDesktop(hdesktop);
3542 TRACE("-->%d\n", ret);
3543 return parm->success;
3546 static BOOL CALLBACK bcast_winsta( LPWSTR winsta, LPARAM lp )
3549 HWINSTA hwinsta = OpenWindowStationW( winsta, FALSE, WINSTA_ENUMDESKTOPS );
3550 TRACE("hwinsta: %p/%s/%08x\n", hwinsta, debugstr_w( winsta ), GetLastError());
3553 ((BroadcastParm *)lp)->winsta = hwinsta;
3554 ret = EnumDesktopsW( hwinsta, bcast_desktop, lp );
3555 CloseWindowStation( hwinsta );
3556 TRACE("-->%d\n", ret);
3560 /***********************************************************************
3561 * BroadcastSystemMessageA (USER32.@)
3562 * BroadcastSystemMessage (USER32.@)
3564 LONG WINAPI BroadcastSystemMessageA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
3566 return BroadcastSystemMessageExA( flags, recipients, msg, wp, lp, NULL );
3570 /***********************************************************************
3571 * BroadcastSystemMessageW (USER32.@)
3573 LONG WINAPI BroadcastSystemMessageW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
3575 return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, NULL );
3578 /***********************************************************************
3579 * BroadcastSystemMessageExA (USER32.@)
3581 LONG WINAPI BroadcastSystemMessageExA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
3583 map_wparam_AtoW( msg, &wp, WMCHAR_MAP_NOMAPPING );
3584 return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, NULL );
3588 /***********************************************************************
3589 * BroadcastSystemMessageExW (USER32.@)
3591 LONG WINAPI BroadcastSystemMessageExW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
3594 DWORD recips = BSM_ALLCOMPONENTS;
3596 static const DWORD all_flags = ( BSF_QUERY | BSF_IGNORECURRENTTASK | BSF_FLUSHDISK | BSF_NOHANG
3597 | BSF_POSTMESSAGE | BSF_FORCEIFHUNG | BSF_NOTIMEOUTIFNOTHUNG
3598 | BSF_ALLOWSFW | BSF_SENDNOTIFYMESSAGE | BSF_RETURNHDESK | BSF_LUID );
3600 TRACE("Flags: %08x, recipients: %p(0x%x), msg: %04x, wparam: %08lx, lparam: %08lx\n", flags, recipients,
3601 (recipients ? *recipients : recips), msg, wp, lp);
3603 if (flags & ~all_flags)
3605 SetLastError(ERROR_INVALID_PARAMETER);
3610 recipients = &recips;
3612 if ( pinfo && flags & BSF_QUERY )
3613 FIXME("Not returning PBSMINFO information yet\n");
3616 parm.recipients = recipients;
3620 parm.success = TRUE;
3622 if (*recipients & BSM_ALLDESKTOPS || *recipients == BSM_ALLCOMPONENTS)
3623 ret = EnumWindowStationsW(bcast_winsta, (LONG_PTR)&parm);
3624 else if (*recipients & BSM_APPLICATIONS)
3626 EnumWindows(bcast_childwindow, (LONG_PTR)&parm);
3630 FIXME("Recipients %08x not supported!\n", *recipients);
3635 /***********************************************************************
3636 * SetMessageQueue (USER32.@)
3638 BOOL WINAPI SetMessageQueue( INT size )
3640 /* now obsolete the message queue will be expanded dynamically as necessary */
3645 /***********************************************************************
3646 * MessageBeep (USER32.@)
3648 BOOL WINAPI MessageBeep( UINT i )
3651 SystemParametersInfoA( SPI_GETBEEP, 0, &active, FALSE );
3652 if (active) USER_Driver->pBeep();
3657 /***********************************************************************
3658 * SetTimer (USER32.@)
3660 UINT_PTR WINAPI SetTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3663 WNDPROC winproc = 0;
3665 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, FALSE );
3667 SERVER_START_REQ( set_win_timer )
3669 req->win = wine_server_user_handle( hwnd );
3670 req->msg = WM_TIMER;
3672 req->rate = max( timeout, SYS_TIMER_RATE );
3673 req->lparam = (ULONG_PTR)winproc;
3674 if (!wine_server_call_err( req ))
3677 if (!ret) ret = TRUE;
3683 TRACE("Added %p %lx %p timeout %d\n", hwnd, id, winproc, timeout );
3688 /***********************************************************************
3689 * SetSystemTimer (USER32.@)
3691 UINT_PTR WINAPI SetSystemTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3694 WNDPROC winproc = 0;
3696 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, FALSE );
3698 SERVER_START_REQ( set_win_timer )
3700 req->win = wine_server_user_handle( hwnd );
3701 req->msg = WM_SYSTIMER;
3703 req->rate = max( timeout, SYS_TIMER_RATE );
3704 req->lparam = (ULONG_PTR)winproc;
3705 if (!wine_server_call_err( req ))
3708 if (!ret) ret = TRUE;
3714 TRACE("Added %p %lx %p timeout %d\n", hwnd, id, winproc, timeout );
3719 /***********************************************************************
3720 * KillTimer (USER32.@)
3722 BOOL WINAPI KillTimer( HWND hwnd, UINT_PTR id )
3726 SERVER_START_REQ( kill_win_timer )
3728 req->win = wine_server_user_handle( hwnd );
3729 req->msg = WM_TIMER;
3731 ret = !wine_server_call_err( req );
3738 /***********************************************************************
3739 * KillSystemTimer (USER32.@)
3741 BOOL WINAPI KillSystemTimer( HWND hwnd, UINT_PTR id )
3745 SERVER_START_REQ( kill_win_timer )
3747 req->win = wine_server_user_handle( hwnd );
3748 req->msg = WM_SYSTIMER;
3750 ret = !wine_server_call_err( req );
3757 /**********************************************************************
3758 * GetGUIThreadInfo (USER32.@)
3760 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
3764 SERVER_START_REQ( get_thread_input )
3767 if ((ret = !wine_server_call_err( req )))
3770 info->hwndActive = wine_server_ptr_handle( reply->active );
3771 info->hwndFocus = wine_server_ptr_handle( reply->focus );
3772 info->hwndCapture = wine_server_ptr_handle( reply->capture );
3773 info->hwndMenuOwner = wine_server_ptr_handle( reply->menu_owner );
3774 info->hwndMoveSize = wine_server_ptr_handle( reply->move_size );
3775 info->hwndCaret = wine_server_ptr_handle( reply->caret );
3776 info->rcCaret.left = reply->rect.left;
3777 info->rcCaret.top = reply->rect.top;
3778 info->rcCaret.right = reply->rect.right;
3779 info->rcCaret.bottom = reply->rect.bottom;
3780 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
3781 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
3782 if (reply->caret) info->flags |= GUI_CARETBLINKING;
3790 /******************************************************************
3791 * IsHungAppWindow (USER32.@)
3794 BOOL WINAPI IsHungAppWindow( HWND hWnd )
3798 SERVER_START_REQ( is_window_hung )
3800 req->win = wine_server_user_handle( hWnd );
3801 ret = !wine_server_call_err( req ) && reply->is_hung;
3807 /******************************************************************
3808 * ChangeWindowMessageFilter (USER32.@)
3810 BOOL WINAPI ChangeWindowMessageFilter( UINT message, DWORD flag )
3812 FIXME( "%x %08x\n", message, flag );