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 /* description of the data fields that need to be packed along with a sent message */
63 const void *data[MAX_PACK_COUNT];
64 size_t size[MAX_PACK_COUNT];
67 /* info about the message currently being received by the current thread */
68 struct received_message_info
70 enum message_type type;
72 UINT flags; /* InSendMessageEx return flags */
75 /* structure to group all parameters for sent messages of the various kinds */
76 struct send_message_info
78 enum message_type type;
84 UINT flags; /* flags for SendMessageTimeout */
85 UINT timeout; /* timeout for SendMessageTimeout */
86 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
87 ULONG_PTR data; /* callback data */
88 enum wm_char_mapping wm_char;
92 /* Message class descriptor */
93 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
94 static LRESULT WINAPI message_winproc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
95 const struct builtin_class_descr MESSAGE_builtin_class =
99 NULL, /* procA (winproc is Unicode only) */
100 message_winproc, /* procW */
102 IDC_ARROW, /* cursor */
108 /* flag for messages that contain pointers */
109 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
111 #define SET(msg) (1 << ((msg) & 31))
113 static const unsigned int message_pointer_flags[] =
116 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
117 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
119 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
122 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
123 SET(WM_NOTIFY) | SET(WM_HELP),
125 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
127 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
129 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
131 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
133 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO) | SET(SBM_GETSCROLLBARINFO),
139 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
140 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
141 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
145 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
146 SET(LB_DIR) | SET(LB_FINDSTRING) |
147 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
149 SET(LB_FINDSTRINGEXACT),
155 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
157 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
158 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
172 SET(WM_ASKCBFORMATNAME)
175 /* flags for messages that contain Unicode strings */
176 static const unsigned int message_unicode_flags[] =
179 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
180 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
192 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
196 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
200 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
201 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
205 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
206 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
208 SET(LB_FINDSTRINGEXACT),
230 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
233 /* check whether a given message type includes pointers */
234 static inline int is_pointer_message( UINT message )
236 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
237 return (message_pointer_flags[message / 32] & SET(message)) != 0;
240 /* check whether a given message type contains Unicode (or ASCII) chars */
241 static inline int is_unicode_message( UINT message )
243 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
244 return (message_unicode_flags[message / 32] & SET(message)) != 0;
249 /* add a data field to a packed message */
250 static inline void push_data( struct packed_message *data, const void *ptr, size_t size )
252 data->data[data->count] = ptr;
253 data->size[data->count] = size;
257 /* add a string to a packed message */
258 static inline void push_string( struct packed_message *data, LPCWSTR str )
260 push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
263 /* make sure that the buffer contains a valid null-terminated Unicode string */
264 static inline BOOL check_string( LPCWSTR str, size_t size )
266 for (size /= sizeof(WCHAR); size; size--, str++)
267 if (!*str) return TRUE;
271 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
272 static inline void *get_buffer_space( void **buffer, size_t size )
278 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
279 HeapFree( GetProcessHeap(), 0, *buffer );
281 else ret = HeapAlloc( GetProcessHeap(), 0, size );
287 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
288 static inline BOOL combobox_has_strings( HWND hwnd )
290 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
291 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
294 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
295 static inline BOOL listbox_has_strings( HWND hwnd )
297 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
298 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
301 /* check whether message is in the range of keyboard messages */
302 static inline BOOL is_keyboard_message( UINT message )
304 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
307 /* check whether message is in the range of mouse messages */
308 static inline BOOL is_mouse_message( UINT message )
310 return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
311 (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
314 /* check whether message matches the specified hwnd filter */
315 static inline BOOL check_hwnd_filter( const MSG *msg, HWND hwnd_filter )
317 if (!hwnd_filter || hwnd_filter == GetDesktopWindow()) return TRUE;
318 return (msg->hwnd == hwnd_filter || IsChild( hwnd_filter, msg->hwnd ));
321 /* check for pending WM_CHAR message with DBCS trailing byte */
322 static inline BOOL get_pending_wmchar( MSG *msg, UINT first, UINT last, BOOL remove )
324 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
326 if (!data || !data->get_msg.message) return FALSE;
327 if ((first || last) && (first > WM_CHAR || last < WM_CHAR)) return FALSE;
328 if (!msg) return FALSE;
329 *msg = data->get_msg;
330 if (remove) data->get_msg.message = 0;
335 /***********************************************************************
338 * Window procedure for "Message" windows (HWND_MESSAGE parent).
340 static LRESULT WINAPI message_winproc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
342 if (message == WM_NCCREATE) return TRUE;
343 return 0; /* all other messages are ignored */
347 /***********************************************************************
348 * broadcast_message_callback
350 * Helper callback for broadcasting messages.
352 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
354 struct send_message_info *info = (struct send_message_info *)lparam;
355 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
359 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
360 info->flags, info->timeout, NULL );
363 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
364 info->flags, info->timeout, NULL );
367 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
370 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
371 info->callback, info->data );
374 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
377 ERR( "bad type %d\n", info->type );
384 /***********************************************************************
387 * Convert the wparam of an ASCII message to Unicode.
389 BOOL map_wparam_AtoW( UINT message, WPARAM *wparam, enum wm_char_mapping mapping )
398 /* WM_CHAR is magic: a DBCS char can be sent/posted as two consecutive WM_CHAR
399 * messages, in which case the first char is stored, and the conversion
400 * to Unicode only takes place once the second char is sent/posted.
402 if (mapping != WMCHAR_MAP_NOMAPPING)
404 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
405 BYTE low = LOBYTE(*wparam);
410 ch[1] = HIBYTE(*wparam);
411 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
412 TRACE( "map %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
413 if (data) data->lead_byte[mapping] = 0;
415 else if (data && data->lead_byte[mapping])
417 ch[0] = data->lead_byte[mapping];
419 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
420 TRACE( "map stored %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
421 data->lead_byte[mapping] = 0;
423 else if (!IsDBCSLeadByte( low ))
426 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 1 );
427 TRACE( "map %02x -> %04x\n", (BYTE)ch[0], wch[0] );
428 if (data) data->lead_byte[mapping] = 0;
430 else /* store it and wait for trail byte */
434 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) )))
436 get_user_thread_info()->wmchar_data = data;
438 TRACE( "storing lead byte %02x mapping %u\n", low, mapping );
439 data->lead_byte[mapping] = low;
442 *wparam = MAKEWPARAM(wch[0], wch[1]);
445 /* else fall through */
447 case EM_SETPASSWORDCHAR:
452 ch[0] = LOBYTE(*wparam);
453 ch[1] = HIBYTE(*wparam);
454 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
455 *wparam = MAKEWPARAM(wch[0], wch[1]);
458 ch[0] = HIBYTE(*wparam);
459 ch[1] = LOBYTE(*wparam);
460 if (ch[0]) RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch, 2 );
461 else RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch + 1, 1 );
462 *wparam = MAKEWPARAM(wch[0], HIWORD(*wparam));
469 /***********************************************************************
472 * Convert the wparam of a Unicode message to ASCII.
474 static void map_wparam_WtoA( MSG *msg, BOOL remove )
483 if (!HIWORD(msg->wParam))
485 wch[0] = LOWORD(msg->wParam);
487 RtlUnicodeToMultiByteN( (LPSTR)ch, 2, &len, wch, sizeof(wch[0]) );
488 if (len == 2) /* DBCS char */
490 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
493 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) ))) return;
494 get_user_thread_info()->wmchar_data = data;
498 data->get_msg = *msg;
499 data->get_msg.wParam = ch[1];
505 /* else fall through */
507 case EM_SETPASSWORDCHAR:
512 wch[0] = LOWORD(msg->wParam);
513 wch[1] = HIWORD(msg->wParam);
515 RtlUnicodeToMultiByteN( (LPSTR)ch, 2, NULL, wch, sizeof(wch) );
516 msg->wParam = MAKEWPARAM( ch[0] | (ch[1] << 8), 0 );
519 wch[0] = LOWORD(msg->wParam);
521 RtlUnicodeToMultiByteN( (LPSTR)ch, 2, &len, wch, sizeof(wch[0]) );
523 msg->wParam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(msg->wParam) );
525 msg->wParam = MAKEWPARAM( ch[0], HIWORD(msg->wParam) );
531 /***********************************************************************
534 * Pack a message for sending to another process.
535 * Return the size of the data we expect in the message reply.
536 * Set data->count to -1 if there is an error.
538 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
539 struct packed_message *data )
547 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
548 push_data( data, cs, sizeof(*cs) );
549 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
550 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
554 case WM_ASKCBFORMATNAME:
555 return wparam * sizeof(WCHAR);
556 case WM_WININICHANGE:
557 if (lparam) push_string(data, (LPWSTR)lparam );
560 case WM_DEVMODECHANGE:
565 push_string( data, (LPWSTR)lparam );
567 case WM_GETMINMAXINFO:
568 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
569 return sizeof(MINMAXINFO);
571 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
574 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
575 return sizeof(MEASUREITEMSTRUCT);
577 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
580 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
582 case WM_WINDOWPOSCHANGING:
583 case WM_WINDOWPOSCHANGED:
584 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
585 return sizeof(WINDOWPOS);
588 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
589 push_data( data, cp, sizeof(*cp) );
590 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
594 /* WM_NOTIFY cannot be sent across processes (MSDN) */
598 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
600 case WM_STYLECHANGING:
601 case WM_STYLECHANGED:
602 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
607 push_data( data, (RECT *)lparam, sizeof(RECT) );
612 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
613 push_data( data, nc, sizeof(*nc) );
614 push_data( data, nc->lppos, sizeof(*nc->lppos) );
615 return sizeof(*nc) + sizeof(*nc->lppos);
618 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
620 case SBM_SETSCROLLINFO:
621 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
623 case SBM_GETSCROLLINFO:
624 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
625 return sizeof(SCROLLINFO);
626 case SBM_GETSCROLLBARINFO:
628 const SCROLLBARINFO *info = (const SCROLLBARINFO *)lparam;
629 size_t size = min( info->cbSize, sizeof(SCROLLBARINFO) );
630 push_data( data, info, size );
638 if (wparam) size += sizeof(DWORD);
639 if (lparam) size += sizeof(DWORD);
644 case CB_GETDROPPEDCONTROLRECT:
648 push_data( data, (RECT *)lparam, sizeof(RECT) );
652 WORD *pw = (WORD *)lparam;
653 push_data( data, pw, sizeof(*pw) );
654 return *pw * sizeof(WCHAR);
658 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
661 case CB_INSERTSTRING:
663 case CB_FINDSTRINGEXACT:
664 case CB_SELECTSTRING:
665 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
668 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
669 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
671 case LB_INSERTSTRING:
673 case LB_FINDSTRINGEXACT:
674 case LB_SELECTSTRING:
675 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
678 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
679 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
681 return wparam * sizeof(UINT);
683 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
684 return sizeof(MDINEXTMENU);
687 push_data( data, (RECT *)lparam, sizeof(RECT) );
691 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
692 push_data( data, cs, sizeof(*cs) );
693 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
694 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
697 case WM_MDIGETACTIVE:
698 if (lparam) return sizeof(BOOL);
700 case WM_DEVICECHANGE:
702 DEV_BROADCAST_HDR *header = (DEV_BROADCAST_HDR *)lparam;
703 push_data( data, header, header->dbch_size );
706 case WM_WINE_SETWINDOWPOS:
707 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
709 case WM_WINE_KEYBOARD_LL_HOOK:
711 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
712 push_data( data, h_extra, sizeof(*h_extra) );
713 push_data( data, (LPVOID)h_extra->lparam, sizeof(KBDLLHOOKSTRUCT) );
716 case WM_WINE_MOUSE_LL_HOOK:
718 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
719 push_data( data, h_extra, sizeof(*h_extra) );
720 push_data( data, (LPVOID)h_extra->lparam, sizeof(MSLLHOOKSTRUCT) );
724 if (wparam <= 1) return 0;
725 FIXME( "WM_NCPAINT hdc packing not supported yet\n" );
729 if (!wparam) return 0;
732 /* these contain an HFONT */
735 /* these contain an HDC */
737 case WM_ICONERASEBKGND:
738 case WM_CTLCOLORMSGBOX:
739 case WM_CTLCOLOREDIT:
740 case WM_CTLCOLORLISTBOX:
743 case WM_CTLCOLORSCROLLBAR:
744 case WM_CTLCOLORSTATIC:
747 /* these contain an HGLOBAL */
748 case WM_PAINTCLIPBOARD:
749 case WM_SIZECLIPBOARD:
750 /* these contain HICON */
753 case WM_QUERYDRAGICON:
754 case WM_QUERYPARKICON:
755 /* these contain pointers */
757 case WM_QUERYDROPOBJECT:
761 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
769 /***********************************************************************
772 * Unpack a message received from another process.
774 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
775 void **buffer, size_t size )
784 CREATESTRUCTW *cs = *buffer;
785 WCHAR *str = (WCHAR *)(cs + 1);
786 if (size < sizeof(*cs)) return FALSE;
788 if (HIWORD(cs->lpszName))
790 if (!check_string( str, size )) return FALSE;
792 size -= (strlenW(str) + 1) * sizeof(WCHAR);
793 str += strlenW(str) + 1;
795 if (HIWORD(cs->lpszClass))
797 if (!check_string( str, size )) return FALSE;
803 case WM_ASKCBFORMATNAME:
804 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
806 case WM_WININICHANGE:
807 if (!*lparam) return TRUE;
810 case WM_DEVMODECHANGE:
815 if (!check_string( *buffer, size )) return FALSE;
817 case WM_GETMINMAXINFO:
818 minsize = sizeof(MINMAXINFO);
821 minsize = sizeof(DRAWITEMSTRUCT);
824 minsize = sizeof(MEASUREITEMSTRUCT);
827 minsize = sizeof(DELETEITEMSTRUCT);
830 minsize = sizeof(COMPAREITEMSTRUCT);
832 case WM_WINDOWPOSCHANGING:
833 case WM_WINDOWPOSCHANGED:
834 case WM_WINE_SETWINDOWPOS:
835 minsize = sizeof(WINDOWPOS);
839 COPYDATASTRUCT *cp = *buffer;
840 if (size < sizeof(*cp)) return FALSE;
843 minsize = sizeof(*cp) + cp->cbData;
849 /* WM_NOTIFY cannot be sent across processes (MSDN) */
852 minsize = sizeof(HELPINFO);
854 case WM_STYLECHANGING:
855 case WM_STYLECHANGED:
856 minsize = sizeof(STYLESTRUCT);
859 if (!*wparam) minsize = sizeof(RECT);
862 NCCALCSIZE_PARAMS *nc = *buffer;
863 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
864 nc->lppos = (WINDOWPOS *)(nc + 1);
868 if (!*lparam) return TRUE;
869 minsize = sizeof(MSG);
871 case SBM_SETSCROLLINFO:
872 minsize = sizeof(SCROLLINFO);
874 case SBM_GETSCROLLINFO:
875 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
877 case SBM_GETSCROLLBARINFO:
878 if (!get_buffer_space( buffer, sizeof(SCROLLBARINFO ))) return FALSE;
883 if (*wparam || *lparam)
885 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
886 if (*wparam) *wparam = (WPARAM)*buffer;
887 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
892 case CB_GETDROPPEDCONTROLRECT:
893 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
897 minsize = sizeof(RECT);
902 if (size < sizeof(WORD)) return FALSE;
903 len = *(WORD *)*buffer;
904 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
905 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
910 if (!*wparam) return TRUE;
911 minsize = *wparam * sizeof(UINT);
914 case CB_INSERTSTRING:
916 case CB_FINDSTRINGEXACT:
917 case CB_SELECTSTRING:
919 case LB_INSERTSTRING:
921 case LB_FINDSTRINGEXACT:
922 case LB_SELECTSTRING:
923 if (!*buffer) return TRUE;
924 if (!check_string( *buffer, size )) return FALSE;
928 size = sizeof(ULONG_PTR);
929 if (combobox_has_strings( hwnd ))
930 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
931 if (!get_buffer_space( buffer, size )) return FALSE;
936 size = sizeof(ULONG_PTR);
937 if (listbox_has_strings( hwnd ))
938 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
939 if (!get_buffer_space( buffer, size )) return FALSE;
943 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
946 minsize = sizeof(MDINEXTMENU);
947 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
951 minsize = sizeof(RECT);
952 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
956 MDICREATESTRUCTW *cs = *buffer;
957 WCHAR *str = (WCHAR *)(cs + 1);
958 if (size < sizeof(*cs)) return FALSE;
960 if (HIWORD(cs->szTitle))
962 if (!check_string( str, size )) return FALSE;
964 size -= (strlenW(str) + 1) * sizeof(WCHAR);
965 str += strlenW(str) + 1;
967 if (HIWORD(cs->szClass))
969 if (!check_string( str, size )) return FALSE;
974 case WM_MDIGETACTIVE:
975 if (!*lparam) return TRUE;
976 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
978 case WM_DEVICECHANGE:
979 minsize = sizeof(DEV_BROADCAST_HDR);
981 case WM_WINE_KEYBOARD_LL_HOOK:
982 case WM_WINE_MOUSE_LL_HOOK:
984 struct hook_extra_info *h_extra = *buffer;
986 minsize = sizeof(struct hook_extra_info) +
987 (message == WM_WINE_KEYBOARD_LL_HOOK ? sizeof(KBDLLHOOKSTRUCT)
988 : sizeof(MSLLHOOKSTRUCT));
989 if (size < minsize) return FALSE;
990 h_extra->lparam = (LPARAM)(h_extra + 1);
994 if (*wparam <= 1) return TRUE;
995 FIXME( "WM_NCPAINT hdc unpacking not supported\n" );
998 if (!*wparam) return TRUE;
1001 /* these contain an HFONT */
1004 /* these contain an HDC */
1006 case WM_ICONERASEBKGND:
1007 case WM_CTLCOLORMSGBOX:
1008 case WM_CTLCOLOREDIT:
1009 case WM_CTLCOLORLISTBOX:
1010 case WM_CTLCOLORBTN:
1011 case WM_CTLCOLORDLG:
1012 case WM_CTLCOLORSCROLLBAR:
1013 case WM_CTLCOLORSTATIC:
1015 case WM_PRINTCLIENT:
1016 /* these contain an HGLOBAL */
1017 case WM_PAINTCLIPBOARD:
1018 case WM_SIZECLIPBOARD:
1019 /* these contain HICON */
1022 case WM_QUERYDRAGICON:
1023 case WM_QUERYPARKICON:
1024 /* these contain pointers */
1026 case WM_QUERYDROPOBJECT:
1030 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
1034 return TRUE; /* message doesn't need any unpacking */
1037 /* default exit for most messages: check minsize and store buffer in lparam */
1038 if (size < minsize) return FALSE;
1039 *lparam = (LPARAM)*buffer;
1044 /***********************************************************************
1047 * Pack a reply to a message for sending to another process.
1049 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1050 LRESULT res, struct packed_message *data )
1057 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
1062 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
1064 case WM_GETMINMAXINFO:
1065 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
1067 case WM_MEASUREITEM:
1068 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
1070 case WM_WINDOWPOSCHANGING:
1071 case WM_WINDOWPOSCHANGED:
1072 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
1075 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
1077 case SBM_GETSCROLLINFO:
1078 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
1081 case LB_GETITEMRECT:
1082 case CB_GETDROPPEDCONTROLRECT:
1085 push_data( data, (RECT *)lparam, sizeof(RECT) );
1089 WORD *ptr = (WORD *)lparam;
1090 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
1093 case LB_GETSELITEMS:
1094 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
1096 case WM_MDIGETACTIVE:
1097 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
1101 push_data( data, (RECT *)lparam, sizeof(RECT) );
1104 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1105 push_data( data, nc, sizeof(*nc) );
1106 push_data( data, nc->lppos, sizeof(*nc->lppos) );
1112 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
1113 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
1116 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
1119 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
1121 case WM_ASKCBFORMATNAME:
1122 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
1128 /***********************************************************************
1131 * Unpack a message reply received from another process.
1133 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1134 void *buffer, size_t size )
1141 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
1142 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
1143 memcpy( cs, buffer, min( sizeof(*cs), size ));
1144 cs->lpszName = name; /* restore the original pointers */
1145 cs->lpszClass = class;
1149 case WM_ASKCBFORMATNAME:
1150 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
1152 case WM_GETMINMAXINFO:
1153 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
1155 case WM_MEASUREITEM:
1156 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
1158 case WM_WINDOWPOSCHANGING:
1159 case WM_WINDOWPOSCHANGED:
1160 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
1163 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
1165 case SBM_GETSCROLLINFO:
1166 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
1168 case SBM_GETSCROLLBARINFO:
1169 memcpy( (SCROLLBARINFO *)lparam, buffer, min( sizeof(SCROLLBARINFO), size ));
1172 case CB_GETDROPPEDCONTROLRECT:
1173 case LB_GETITEMRECT:
1176 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1179 size = min( size, (size_t)*(WORD *)lparam );
1180 memcpy( (WCHAR *)lparam, buffer, size );
1182 case LB_GETSELITEMS:
1183 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
1187 memcpy( (WCHAR *)lparam, buffer, size );
1190 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
1192 case WM_MDIGETACTIVE:
1193 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
1197 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1200 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1201 WINDOWPOS *wp = nc->lppos;
1202 memcpy( nc, buffer, min( sizeof(*nc), size ));
1203 if (size > sizeof(*nc))
1205 size -= sizeof(*nc);
1206 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
1208 nc->lppos = wp; /* restore the original pointer */
1216 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1217 if (size <= sizeof(DWORD)) break;
1218 size -= sizeof(DWORD);
1219 buffer = (DWORD *)buffer + 1;
1221 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1225 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
1226 LPCWSTR title = cs->szTitle, class = cs->szClass;
1227 memcpy( cs, buffer, min( sizeof(*cs), size ));
1228 cs->szTitle = title; /* restore the original pointers */
1229 cs->szClass = class;
1233 ERR( "should not happen: unexpected message %x\n", message );
1239 /***********************************************************************
1242 * Send a reply to a sent message.
1244 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1246 struct packed_message data;
1247 int i, replied = info->flags & ISMEX_REPLIED;
1249 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1250 if (!remove && replied) return; /* replied already */
1253 info->flags |= ISMEX_REPLIED;
1255 if (info->type == MSG_OTHER_PROCESS && !replied)
1257 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1258 info->msg.lParam, result, &data );
1261 SERVER_START_REQ( reply_message )
1263 req->result = result;
1264 req->remove = remove;
1265 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1266 wine_server_call( req );
1272 /***********************************************************************
1273 * handle_internal_message
1275 * Handle an internal Wine message instead of calling the window proc.
1277 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1281 case WM_WINE_DESTROYWINDOW:
1282 return WIN_DestroyWindow( hwnd );
1283 case WM_WINE_SETWINDOWPOS:
1284 if (is_desktop_window( hwnd )) return 0;
1285 return USER_SetWindowPos( (WINDOWPOS *)lparam );
1286 case WM_WINE_SHOWWINDOW:
1287 if (is_desktop_window( hwnd )) return 0;
1288 return ShowWindow( hwnd, wparam );
1289 case WM_WINE_SETPARENT:
1290 if (is_desktop_window( hwnd )) return 0;
1291 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1292 case WM_WINE_SETWINDOWLONG:
1293 return WIN_SetWindowLong( hwnd, (short)LOWORD(wparam), HIWORD(wparam), lparam, TRUE );
1294 case WM_WINE_ENABLEWINDOW:
1295 if (is_desktop_window( hwnd )) return 0;
1296 return EnableWindow( hwnd, wparam );
1297 case WM_WINE_SETACTIVEWINDOW:
1298 if (is_desktop_window( hwnd )) return 0;
1299 return (LRESULT)SetActiveWindow( (HWND)wparam );
1300 case WM_WINE_KEYBOARD_LL_HOOK:
1301 case WM_WINE_MOUSE_LL_HOOK:
1303 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
1305 return call_current_hook( h_extra->handle, HC_ACTION, wparam, h_extra->lparam );
1308 if (msg >= WM_WINE_FIRST_DRIVER_MSG && msg <= WM_WINE_LAST_DRIVER_MSG)
1309 return USER_Driver->pWindowMessage( hwnd, msg, wparam, lparam );
1310 FIXME( "unknown internal message %x\n", msg );
1315 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1316 * to the memory handle, we keep track (in the server side) of all pairs of handle
1317 * used (the client passes its value and the content of the memory handle), and
1318 * the server stored both values (the client, and the local one, created after the
1319 * content). When a ACK message is generated, the list of pair is searched for a
1320 * matching pair, so that the client memory handle can be returned.
1323 HGLOBAL client_hMem;
1324 HGLOBAL server_hMem;
1327 static struct DDE_pair* dde_pairs;
1328 static int dde_num_alloc;
1329 static int dde_num_used;
1331 static CRITICAL_SECTION dde_crst;
1332 static CRITICAL_SECTION_DEBUG critsect_debug =
1335 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
1336 0, 0, { (DWORD_PTR)(__FILE__ ": dde_crst") }
1338 static CRITICAL_SECTION dde_crst = { &critsect_debug, -1, 0, 0, 0, 0 };
1340 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1345 EnterCriticalSection(&dde_crst);
1347 /* now remember the pair of hMem on both sides */
1348 if (dde_num_used == dde_num_alloc)
1350 struct DDE_pair* tmp;
1352 tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1353 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1355 tmp = HeapAlloc( GetProcessHeap(), 0,
1356 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1360 LeaveCriticalSection(&dde_crst);
1364 /* zero out newly allocated part */
1365 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1366 dde_num_alloc += GROWBY;
1369 for (i = 0; i < dde_num_alloc; i++)
1371 if (dde_pairs[i].server_hMem == 0)
1373 dde_pairs[i].client_hMem = chm;
1374 dde_pairs[i].server_hMem = shm;
1379 LeaveCriticalSection(&dde_crst);
1383 static HGLOBAL dde_get_pair(HGLOBAL shm)
1388 EnterCriticalSection(&dde_crst);
1389 for (i = 0; i < dde_num_alloc; i++)
1391 if (dde_pairs[i].server_hMem == shm)
1393 /* free this pair */
1394 dde_pairs[i].server_hMem = 0;
1396 ret = dde_pairs[i].client_hMem;
1400 LeaveCriticalSection(&dde_crst);
1404 /***********************************************************************
1407 * Post a DDE message
1409 static BOOL post_dde_message( struct packed_message *data, const struct send_message_info *info )
1413 UINT_PTR uiLo, uiHi;
1415 HGLOBAL hunlock = 0;
1419 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1425 /* DDE messages which don't require packing are:
1434 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1435 HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1438 /* send back the value of h on the other side */
1439 push_data( data, &h, sizeof(HGLOBAL) );
1441 TRACE( "send dde-ack %lx %08lx => %p\n", uiLo, uiHi, h );
1446 /* uiHi should contain either an atom or 0 */
1447 TRACE( "send dde-ack %lx atom=%lx\n", uiLo, uiHi );
1448 lp = MAKELONG( uiLo, uiHi );
1457 size = GlobalSize( (HGLOBAL)uiLo ) ;
1458 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1459 (info->msg == WM_DDE_DATA && size < FIELD_OFFSET(DDEDATA, Value)) ||
1460 (info->msg == WM_DDE_POKE && size < FIELD_OFFSET(DDEPOKE, Value))
1464 else if (info->msg != WM_DDE_DATA) return FALSE;
1469 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1471 DDEDATA *dde_data = ptr;
1472 TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
1473 dde_data->unused, dde_data->fResponse, dde_data->fRelease,
1474 dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
1475 push_data( data, ptr, size );
1476 hunlock = (HGLOBAL)uiLo;
1479 TRACE( "send ddepack %u %lx\n", size, uiHi );
1481 case WM_DDE_EXECUTE:
1484 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1486 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1487 /* so that the other side can send it back on ACK */
1489 hunlock = (HGLOBAL)info->lparam;
1494 SERVER_START_REQ( send_message )
1496 req->id = info->dest_tid;
1497 req->type = info->type;
1499 req->win = wine_server_user_handle( info->hwnd );
1500 req->msg = info->msg;
1501 req->wparam = info->wparam;
1503 req->timeout = TIMEOUT_INFINITE;
1504 for (i = 0; i < data->count; i++)
1505 wine_server_add_data( req, data->data[i], data->size[i] );
1506 if ((res = wine_server_call( req )))
1508 if (res == STATUS_INVALID_PARAMETER)
1509 /* FIXME: find a STATUS_ value for this one */
1510 SetLastError( ERROR_INVALID_THREAD_ID );
1512 SetLastError( RtlNtStatusToDosError(res) );
1515 FreeDDElParam(info->msg, info->lparam);
1518 if (hunlock) GlobalUnlock(hunlock);
1523 /***********************************************************************
1524 * unpack_dde_message
1526 * Unpack a posted DDE message received from another process.
1528 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1529 void **buffer, size_t size )
1531 UINT_PTR uiLo, uiHi;
1540 /* hMem is being passed */
1541 if (size != sizeof(HGLOBAL)) return FALSE;
1542 if (!buffer || !*buffer) return FALSE;
1544 memcpy( &hMem, *buffer, size );
1545 uiHi = (UINT_PTR)hMem;
1546 TRACE("recv dde-ack %lx mem=%lx[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1550 uiLo = LOWORD( *lparam );
1551 uiHi = HIWORD( *lparam );
1552 TRACE("recv dde-ack %lx atom=%lx\n", uiLo, uiHi);
1554 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1559 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1563 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size )))
1565 if ((ptr = GlobalLock( hMem )))
1567 memcpy( ptr, *buffer, size );
1568 GlobalUnlock( hMem );
1576 uiLo = (UINT_PTR)hMem;
1578 *lparam = PackDDElParam( message, uiLo, uiHi );
1580 case WM_DDE_EXECUTE:
1583 if (!buffer || !*buffer) return FALSE;
1584 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size ))) return FALSE;
1585 if ((ptr = GlobalLock( hMem )))
1587 memcpy( ptr, *buffer, size );
1588 GlobalUnlock( hMem );
1589 TRACE( "exec: pairing c=%08lx s=%p\n", *lparam, hMem );
1590 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1601 } else return FALSE;
1602 *lparam = (LPARAM)hMem;
1608 /***********************************************************************
1611 * Call a window procedure and the corresponding hooks.
1613 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1614 BOOL unicode, BOOL same_thread, enum wm_char_mapping mapping )
1618 CWPRETSTRUCT cwpret;
1620 if (msg & 0x80000000)
1622 result = handle_internal_message( hwnd, msg, wparam, lparam );
1626 /* first the WH_CALLWNDPROC hook */
1627 hwnd = WIN_GetFullHandle( hwnd );
1628 cwp.lParam = lparam;
1629 cwp.wParam = wparam;
1632 HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1634 /* now call the window procedure */
1635 if (!WINPROC_call_window( hwnd, msg, wparam, lparam, &result, unicode, mapping )) goto done;
1637 /* and finally the WH_CALLWNDPROCRET hook */
1638 cwpret.lResult = result;
1639 cwpret.lParam = lparam;
1640 cwpret.wParam = wparam;
1641 cwpret.message = msg;
1643 HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1649 /***********************************************************************
1650 * send_parent_notify
1652 * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
1653 * the window has the WS_EX_NOPARENTNOTIFY style.
1655 static void send_parent_notify( HWND hwnd, WORD event, WORD idChild, POINT pt )
1657 /* pt has to be in the client coordinates of the parent window */
1658 MapWindowPoints( 0, hwnd, &pt, 1 );
1663 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)) break;
1664 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
1665 if (!(parent = GetParent(hwnd))) break;
1666 if (parent == GetDesktopWindow()) break;
1667 MapWindowPoints( hwnd, parent, &pt, 1 );
1669 SendMessageW( hwnd, WM_PARENTNOTIFY,
1670 MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
1675 /***********************************************************************
1676 * accept_hardware_message
1678 * Tell the server we have passed the message to the app
1679 * (even though we may end up dropping it later on)
1681 static void accept_hardware_message( UINT hw_id, BOOL remove, HWND new_hwnd )
1683 SERVER_START_REQ( accept_hardware_message )
1686 req->remove = remove;
1687 req->new_win = wine_server_user_handle( new_hwnd );
1688 if (wine_server_call( req ))
1689 FIXME("Failed to reply to MSG_HARDWARE message. Message may not be removed from queue.\n");
1695 /***********************************************************************
1696 * process_keyboard_message
1698 * returns TRUE if the contents of 'msg' should be passed to the application
1700 static BOOL process_keyboard_message( MSG *msg, UINT hw_id, HWND hwnd_filter,
1701 UINT first, UINT last, BOOL remove )
1705 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN ||
1706 msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP)
1707 switch (msg->wParam)
1709 case VK_LSHIFT: case VK_RSHIFT:
1710 msg->wParam = VK_SHIFT;
1712 case VK_LCONTROL: case VK_RCONTROL:
1713 msg->wParam = VK_CONTROL;
1715 case VK_LMENU: case VK_RMENU:
1716 msg->wParam = VK_MENU;
1720 /* FIXME: is this really the right place for this hook? */
1721 event.message = msg->message;
1722 event.hwnd = msg->hwnd;
1723 event.time = msg->time;
1724 event.paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
1725 event.paramH = msg->lParam & 0x7FFF;
1726 if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
1727 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1729 /* check message filters */
1730 if (msg->message < first || msg->message > last) return FALSE;
1731 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1735 if((msg->message == WM_KEYDOWN) &&
1736 (msg->hwnd != GetDesktopWindow()))
1738 /* Handle F1 key by sending out WM_HELP message */
1739 if (msg->wParam == VK_F1)
1741 PostMessageW( msg->hwnd, WM_KEYF1, 0, 0 );
1743 else if(msg->wParam >= VK_BROWSER_BACK &&
1744 msg->wParam <= VK_LAUNCH_APP2)
1746 /* FIXME: Process keystate */
1747 SendMessageW(msg->hwnd, WM_APPCOMMAND, (WPARAM)msg->hwnd, MAKELPARAM(0, (FAPPCOMMAND_KEY | (msg->wParam - VK_BROWSER_BACK + 1))));
1750 else if (msg->message == WM_KEYUP)
1752 /* Handle VK_APPS key by posting a WM_CONTEXTMENU message */
1753 if (msg->wParam == VK_APPS && !MENU_IsMenuActive())
1754 PostMessageW(msg->hwnd, WM_CONTEXTMENU, (WPARAM)msg->hwnd, (LPARAM)-1);
1758 if (HOOK_CallHooks( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
1759 LOWORD(msg->wParam), msg->lParam, TRUE ))
1761 /* skip this message */
1762 HOOK_CallHooks( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam, TRUE );
1763 accept_hardware_message( hw_id, TRUE, 0 );
1766 accept_hardware_message( hw_id, remove, 0 );
1768 if ( msg->message == WM_KEYDOWN || msg->message == WM_KEYUP )
1769 if ( ImmProcessKey(msg->hwnd, GetKeyboardLayout(0), msg->wParam, msg->lParam, 0) )
1770 msg->wParam = VK_PROCESSKEY;
1776 /***********************************************************************
1777 * process_mouse_message
1779 * returns TRUE if the contents of 'msg' should be passed to the application
1781 static BOOL process_mouse_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
1782 UINT first, UINT last, BOOL remove )
1791 MOUSEHOOKSTRUCT hook;
1794 /* find the window to dispatch this mouse message to */
1796 GetGUIThreadInfo( GetCurrentThreadId(), &info );
1797 if (info.hwndCapture)
1800 msg->hwnd = info.hwndCapture;
1804 msg->hwnd = WINPOS_WindowFromPoint( msg->hwnd, msg->pt, &hittest );
1807 if (!msg->hwnd || !WIN_IsCurrentThread( msg->hwnd ))
1809 accept_hardware_message( hw_id, TRUE, msg->hwnd );
1813 /* FIXME: is this really the right place for this hook? */
1814 event.message = msg->message;
1815 event.time = msg->time;
1816 event.hwnd = msg->hwnd;
1817 event.paramL = msg->pt.x;
1818 event.paramH = msg->pt.y;
1819 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1821 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1824 message = msg->message;
1825 /* Note: windows has no concept of a non-client wheel message */
1826 if (message != WM_MOUSEWHEEL)
1828 if (hittest != HTCLIENT)
1830 message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
1831 msg->wParam = hittest;
1835 /* coordinates don't get translated while tracking a menu */
1836 /* FIXME: should differentiate popups and top-level menus */
1837 if (!(info.flags & GUI_INMENUMODE))
1838 ScreenToClient( msg->hwnd, &pt );
1841 msg->lParam = MAKELONG( pt.x, pt.y );
1843 /* translate double clicks */
1845 if ((msg->message == WM_LBUTTONDOWN) ||
1846 (msg->message == WM_RBUTTONDOWN) ||
1847 (msg->message == WM_MBUTTONDOWN) ||
1848 (msg->message == WM_XBUTTONDOWN))
1850 BOOL update = remove;
1852 /* translate double clicks -
1853 * note that ...MOUSEMOVEs can slip in between
1854 * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
1856 if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
1857 hittest != HTCLIENT ||
1858 (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
1860 if ((msg->message == clk_msg.message) &&
1861 (msg->hwnd == clk_msg.hwnd) &&
1862 (msg->wParam == clk_msg.wParam) &&
1863 (msg->time - clk_msg.time < GetDoubleClickTime()) &&
1864 (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
1865 (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
1867 message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
1870 clk_msg.message = 0; /* clear the double click conditions */
1875 if (message < first || message > last) return FALSE;
1876 /* update static double click conditions */
1877 if (update) clk_msg = *msg;
1881 if (message < first || message > last) return FALSE;
1884 /* message is accepted now (but may still get dropped) */
1887 hook.hwnd = msg->hwnd;
1888 hook.wHitTestCode = hittest;
1889 hook.dwExtraInfo = extra_info;
1890 if (HOOK_CallHooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
1891 message, (LPARAM)&hook, TRUE ))
1894 hook.hwnd = msg->hwnd;
1895 hook.wHitTestCode = hittest;
1896 hook.dwExtraInfo = extra_info;
1897 HOOK_CallHooks( WH_CBT, HCBT_CLICKSKIPPED, message, (LPARAM)&hook, TRUE );
1898 accept_hardware_message( hw_id, TRUE, 0 );
1902 if ((hittest == HTERROR) || (hittest == HTNOWHERE))
1904 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
1905 MAKELONG( hittest, msg->message ));
1906 accept_hardware_message( hw_id, TRUE, 0 );
1910 accept_hardware_message( hw_id, remove, 0 );
1912 if (!remove || info.hwndCapture)
1914 msg->message = message;
1920 if ((msg->message == WM_LBUTTONDOWN) ||
1921 (msg->message == WM_RBUTTONDOWN) ||
1922 (msg->message == WM_MBUTTONDOWN) ||
1923 (msg->message == WM_XBUTTONDOWN))
1925 /* Send the WM_PARENTNOTIFY,
1926 * note that even for double/nonclient clicks
1927 * notification message is still WM_L/M/RBUTTONDOWN.
1929 send_parent_notify( msg->hwnd, msg->message, 0, msg->pt );
1931 /* Activate the window if needed */
1933 if (msg->hwnd != info.hwndActive)
1935 HWND hwndTop = msg->hwnd;
1938 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
1939 hwndTop = GetParent( hwndTop );
1942 if (hwndTop && hwndTop != GetDesktopWindow())
1944 LONG ret = SendMessageW( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
1945 MAKELONG( hittest, msg->message ) );
1948 case MA_NOACTIVATEANDEAT:
1953 case MA_ACTIVATEANDEAT:
1958 if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
1961 WARN( "unknown WM_MOUSEACTIVATE code %d\n", ret );
1968 /* send the WM_SETCURSOR message */
1970 /* Windows sends the normal mouse message as the message parameter
1971 in the WM_SETCURSOR message even if it's non-client mouse message */
1972 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd, MAKELONG( hittest, msg->message ));
1974 msg->message = message;
1979 /***********************************************************************
1980 * process_hardware_message
1982 * Process a hardware message; return TRUE if message should be passed on to the app
1984 static BOOL process_hardware_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
1985 UINT first, UINT last, BOOL remove )
1987 if (is_keyboard_message( msg->message ))
1988 return process_keyboard_message( msg, hw_id, hwnd_filter, first, last, remove );
1990 if (is_mouse_message( msg->message ))
1991 return process_mouse_message( msg, hw_id, extra_info, hwnd_filter, first, last, remove );
1993 ERR( "unknown message type %x\n", msg->message );
1998 /***********************************************************************
1999 * call_sendmsg_callback
2001 * Call the callback function of SendMessageCallback.
2003 static inline void call_sendmsg_callback( SENDASYNCPROC callback, HWND hwnd, UINT msg,
2004 ULONG_PTR data, LRESULT result )
2006 if (!callback) return;
2008 if (TRACE_ON(relay))
2009 DPRINTF( "%04x:Call message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
2010 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
2012 callback( hwnd, msg, data, result );
2013 if (TRACE_ON(relay))
2014 DPRINTF( "%04x:Ret message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
2015 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
2020 /***********************************************************************
2023 * Peek for a message matching the given parameters. Return FALSE if none available.
2024 * All pending sent messages are processed before returning.
2026 static BOOL peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags, UINT changed_mask )
2029 struct user_thread_info *thread_info = get_user_thread_info();
2030 struct received_message_info info, *old_info;
2031 unsigned int hw_id = 0; /* id of previous hardware message */
2033 size_t buffer_size = 256;
2035 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size ))) return FALSE;
2037 if (!first && !last) last = ~0;
2038 if (hwnd == HWND_BROADCAST || hwnd == HWND_TOPMOST || hwnd == HWND_BOTTOM)
2045 const message_data_t *msg_data = buffer;
2047 SERVER_START_REQ( get_message )
2050 req->get_win = wine_server_user_handle( hwnd );
2051 req->get_first = first;
2052 req->get_last = last;
2054 req->wake_mask = changed_mask & (QS_SENDMESSAGE | QS_SMRESULT);
2055 req->changed_mask = changed_mask;
2056 wine_server_set_reply( req, buffer, buffer_size );
2057 if (!(res = wine_server_call( req )))
2059 size = wine_server_reply_size( reply );
2060 info.type = reply->type;
2061 info.msg.hwnd = wine_server_ptr_handle( reply->win );
2062 info.msg.message = reply->msg;
2063 info.msg.wParam = reply->wparam;
2064 info.msg.lParam = reply->lparam;
2065 info.msg.time = reply->time;
2069 thread_info->active_hooks = reply->active_hooks;
2071 else buffer_size = reply->total;
2077 HeapFree( GetProcessHeap(), 0, buffer );
2078 if (res != STATUS_BUFFER_OVERFLOW) return FALSE;
2079 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size ))) return FALSE;
2083 TRACE( "got type %d msg %x (%s) hwnd %p wp %lx lp %lx\n",
2084 info.type, info.msg.message,
2085 (info.type == MSG_WINEVENT) ? "MSG_WINEVENT" : SPY_GetMsgName(info.msg.message, info.msg.hwnd),
2086 info.msg.hwnd, info.msg.wParam, info.msg.lParam );
2092 info.flags = ISMEX_SEND;
2095 info.flags = ISMEX_NOTIFY;
2098 info.flags = ISMEX_CALLBACK;
2100 case MSG_CALLBACK_RESULT:
2101 if (size >= sizeof(msg_data->callback))
2102 call_sendmsg_callback( wine_server_get_ptr(msg_data->callback.callback),
2103 info.msg.hwnd, info.msg.message,
2104 msg_data->callback.data, msg_data->callback.result );
2107 if (size >= sizeof(msg_data->winevent))
2109 WINEVENTPROC hook_proc;
2111 hook_proc = wine_server_get_ptr( msg_data->winevent.hook_proc );
2112 size -= sizeof(msg_data->winevent);
2115 WCHAR module[MAX_PATH];
2117 size = min( size, (MAX_PATH - 1) * sizeof(WCHAR) );
2118 memcpy( module, &msg_data->winevent + 1, size );
2119 module[size / sizeof(WCHAR)] = 0;
2120 if (!(hook_proc = get_hook_proc( hook_proc, module )))
2122 ERR( "invalid winevent hook module name %s\n", debugstr_w(module) );
2127 if (TRACE_ON(relay))
2128 DPRINTF( "%04x:Call winevent proc %p (hook=%04x,event=%x,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04x,time=%x)\n",
2129 GetCurrentThreadId(), hook_proc,
2130 msg_data->winevent.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2131 info.msg.lParam, msg_data->winevent.tid, info.msg.time);
2133 hook_proc( wine_server_ptr_handle( msg_data->winevent.hook ), info.msg.message,
2134 info.msg.hwnd, info.msg.wParam, info.msg.lParam,
2135 msg_data->winevent.tid, info.msg.time );
2137 if (TRACE_ON(relay))
2138 DPRINTF( "%04x:Ret winevent proc %p (hook=%04x,event=%x,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04x,time=%x)\n",
2139 GetCurrentThreadId(), hook_proc,
2140 msg_data->winevent.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2141 info.msg.lParam, msg_data->winevent.tid, info.msg.time);
2144 case MSG_OTHER_PROCESS:
2145 info.flags = ISMEX_SEND;
2146 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2147 &info.msg.lParam, &buffer, size ))
2150 reply_message( &info, 0, TRUE );
2155 if (size >= sizeof(msg_data->hardware))
2157 info.msg.pt.x = msg_data->hardware.x;
2158 info.msg.pt.y = msg_data->hardware.y;
2159 hw_id = msg_data->hardware.hw_id;
2160 if (!process_hardware_message( &info.msg, hw_id, msg_data->hardware.info,
2161 hwnd, first, last, flags & PM_REMOVE ))
2163 TRACE("dropping msg %x\n", info.msg.message );
2164 continue; /* ignore it */
2167 thread_info->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
2168 thread_info->GetMessageTimeVal = info.msg.time;
2169 thread_info->GetMessageExtraInfoVal = msg_data->hardware.info;
2170 HeapFree( GetProcessHeap(), 0, buffer );
2171 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)msg, TRUE );
2176 if (info.msg.message & 0x80000000) /* internal message */
2178 if (flags & PM_REMOVE)
2180 handle_internal_message( info.msg.hwnd, info.msg.message,
2181 info.msg.wParam, info.msg.lParam );
2182 /* if this is a nested call return right away */
2183 if (first == info.msg.message && last == info.msg.message) return FALSE;
2186 peek_message( msg, info.msg.hwnd, info.msg.message,
2187 info.msg.message, flags | PM_REMOVE, changed_mask );
2190 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
2192 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2193 &info.msg.lParam, &buffer, size ))
2194 continue; /* ignore it */
2197 msg->pt.x = (short)LOWORD( thread_info->GetMessagePosVal );
2198 msg->pt.y = (short)HIWORD( thread_info->GetMessagePosVal );
2199 thread_info->GetMessageTimeVal = info.msg.time;
2200 thread_info->GetMessageExtraInfoVal = 0;
2201 HeapFree( GetProcessHeap(), 0, buffer );
2202 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)msg, TRUE );
2206 /* if we get here, we have a sent message; call the window procedure */
2207 old_info = thread_info->receive_info;
2208 thread_info->receive_info = &info;
2209 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
2210 info.msg.lParam, (info.type != MSG_ASCII), FALSE,
2211 WMCHAR_MAP_RECVMESSAGE );
2212 reply_message( &info, result, TRUE );
2213 thread_info->receive_info = old_info;
2215 /* if some PM_QS* flags were specified, only handle sent messages from now on */
2216 if (HIWORD(flags) && !changed_mask) flags = PM_QS_SENDMESSAGE | LOWORD(flags);
2221 /***********************************************************************
2222 * process_sent_messages
2224 * Process all pending sent messages.
2226 static inline void process_sent_messages(void)
2229 peek_message( &msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE, 0 );
2233 /***********************************************************************
2234 * get_server_queue_handle
2236 * Get a handle to the server message queue for the current thread.
2238 static HANDLE get_server_queue_handle(void)
2240 struct user_thread_info *thread_info = get_user_thread_info();
2243 if (!(ret = thread_info->server_queue))
2245 SERVER_START_REQ( get_msg_queue )
2247 wine_server_call( req );
2248 ret = wine_server_ptr_handle( reply->handle );
2251 thread_info->server_queue = ret;
2252 if (!ret) ERR( "Cannot get server thread queue\n" );
2258 /***********************************************************************
2259 * wait_message_reply
2261 * Wait until a sent message gets replied to.
2263 static void wait_message_reply( UINT flags )
2265 HANDLE server_queue = get_server_queue_handle();
2269 unsigned int wake_bits = 0;
2272 SERVER_START_REQ( set_queue_mask )
2274 req->wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
2275 req->changed_mask = req->wake_mask;
2277 if (!wine_server_call( req ))
2278 wake_bits = reply->wake_bits;
2282 if (wake_bits & QS_SMRESULT) return; /* got a result */
2283 if (wake_bits & QS_SENDMESSAGE)
2285 /* Process the sent message immediately */
2286 process_sent_messages();
2290 /* now wait for it */
2292 ReleaseThunkLock( &dwlc );
2293 res = USER_Driver->pMsgWaitForMultipleObjectsEx( 1, &server_queue,
2294 INFINITE, QS_SENDMESSAGE, 0 );
2295 if (dwlc) RestoreThunkLock( dwlc );
2299 /***********************************************************************
2300 * put_message_in_queue
2302 * Put a sent message into the destination queue.
2303 * For inter-process message, reply_size is set to expected size of reply data.
2305 static BOOL put_message_in_queue( const struct send_message_info *info, size_t *reply_size )
2307 struct packed_message data;
2308 message_data_t msg_data;
2311 timeout_t timeout = TIMEOUT_INFINITE;
2313 /* Check for INFINITE timeout for compatibility with Win9x,
2314 * although Windows >= NT does not do so
2316 if (info->type != MSG_NOTIFY &&
2317 info->type != MSG_CALLBACK &&
2318 info->type != MSG_POSTED &&
2320 info->timeout != INFINITE)
2322 /* timeout is signed despite the prototype */
2323 timeout = (timeout_t)max( 0, (int)info->timeout ) * -10000;
2327 if (info->type == MSG_OTHER_PROCESS)
2329 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
2330 if (data.count == -1)
2332 WARN( "cannot pack message %x\n", info->msg );
2336 else if (info->type == MSG_CALLBACK)
2338 msg_data.callback.callback = wine_server_client_ptr( info->callback );
2339 msg_data.callback.data = info->data;
2340 msg_data.callback.result = 0;
2341 data.data[0] = &msg_data;
2342 data.size[0] = sizeof(msg_data.callback);
2345 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
2347 return post_dde_message( &data, info );
2350 SERVER_START_REQ( send_message )
2352 req->id = info->dest_tid;
2353 req->type = info->type;
2355 req->win = wine_server_user_handle( info->hwnd );
2356 req->msg = info->msg;
2357 req->wparam = info->wparam;
2358 req->lparam = info->lparam;
2359 req->timeout = timeout;
2361 if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
2362 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
2363 if ((res = wine_server_call( req )))
2365 if (res == STATUS_INVALID_PARAMETER)
2366 /* FIXME: find a STATUS_ value for this one */
2367 SetLastError( ERROR_INVALID_THREAD_ID );
2369 SetLastError( RtlNtStatusToDosError(res) );
2377 /***********************************************************************
2380 * Retrieve a message reply from the server.
2382 static LRESULT retrieve_reply( const struct send_message_info *info,
2383 size_t reply_size, LRESULT *result )
2386 void *reply_data = NULL;
2390 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
2392 WARN( "no memory for reply, will be truncated\n" );
2396 SERVER_START_REQ( get_message_reply )
2399 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
2400 if (!(status = wine_server_call( req ))) *result = reply->result;
2401 reply_size = wine_server_reply_size( reply );
2404 if (!status && reply_size)
2405 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
2407 HeapFree( GetProcessHeap(), 0, reply_data );
2409 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx got reply %lx (err=%d)\n",
2410 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
2411 info->lparam, *result, status );
2413 /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
2414 if (status) SetLastError( RtlNtStatusToDosError(status) );
2419 /***********************************************************************
2420 * send_inter_thread_message
2422 static LRESULT send_inter_thread_message( const struct send_message_info *info, LRESULT *res_ptr )
2424 size_t reply_size = 0;
2426 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
2427 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
2429 USER_CheckNotLock();
2431 if (!put_message_in_queue( info, &reply_size )) return 0;
2433 /* there's no reply to wait for on notify/callback messages */
2434 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
2436 wait_message_reply( info->flags );
2437 return retrieve_reply( info, reply_size, res_ptr );
2441 /***********************************************************************
2442 * send_inter_thread_callback
2444 static LRESULT send_inter_thread_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
2445 LRESULT *result, void *arg )
2447 struct send_message_info *info = arg;
2452 return send_inter_thread_message( info, result );
2456 /***********************************************************************
2459 * Backend implementation of the various SendMessage functions.
2461 static BOOL send_message( struct send_message_info *info, DWORD_PTR *res_ptr, BOOL unicode )
2467 if (is_broadcast(info->hwnd))
2469 EnumWindows( broadcast_message_callback, (LPARAM)info );
2470 if (res_ptr) *res_ptr = 1;
2474 if (!(info->dest_tid = GetWindowThreadProcessId( info->hwnd, &dest_pid ))) return FALSE;
2476 if (USER_IsExitingThread( info->dest_tid )) return FALSE;
2478 SPY_EnterMessage( SPY_SENDMESSAGE, info->hwnd, info->msg, info->wparam, info->lparam );
2480 if (info->dest_tid == GetCurrentThreadId())
2482 result = call_window_proc( info->hwnd, info->msg, info->wparam, info->lparam,
2483 unicode, TRUE, info->wm_char );
2484 if (info->type == MSG_CALLBACK)
2485 call_sendmsg_callback( info->callback, info->hwnd, info->msg, info->data, result );
2490 if (dest_pid != GetCurrentProcessId() && (info->type == MSG_ASCII || info->type == MSG_UNICODE))
2491 info->type = MSG_OTHER_PROCESS;
2493 /* MSG_ASCII can be sent unconverted except for WM_CHAR; everything else needs to be Unicode */
2494 if (!unicode && is_unicode_message( info->msg ) &&
2495 (info->type != MSG_ASCII || info->msg == WM_CHAR))
2496 ret = WINPROC_CallProcAtoW( send_inter_thread_callback, info->hwnd, info->msg,
2497 info->wparam, info->lparam, &result, info, info->wm_char );
2499 ret = send_inter_thread_message( info, &result );
2502 SPY_ExitMessage( SPY_RESULT_OK, info->hwnd, info->msg, result, info->wparam, info->lparam );
2503 if (ret && res_ptr) *res_ptr = result;
2508 /***********************************************************************
2509 * MSG_SendInternalMessageTimeout
2511 * Same as SendMessageTimeoutW but sends the message to a specific thread
2512 * without requiring a window handle. Only works for internal Wine messages.
2514 LRESULT MSG_SendInternalMessageTimeout( DWORD dest_pid, DWORD dest_tid,
2515 UINT msg, WPARAM wparam, LPARAM lparam,
2516 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2518 struct send_message_info info;
2519 LRESULT ret, result;
2521 assert( msg & 0x80000000 ); /* must be an internal Wine message */
2523 info.type = MSG_UNICODE;
2524 info.dest_tid = dest_tid;
2527 info.wparam = wparam;
2528 info.lparam = lparam;
2530 info.timeout = timeout;
2532 if (USER_IsExitingThread( dest_tid )) return 0;
2534 if (dest_tid == GetCurrentThreadId())
2536 result = handle_internal_message( 0, msg, wparam, lparam );
2541 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
2542 ret = send_inter_thread_message( &info, &result );
2544 if (ret && res_ptr) *res_ptr = result;
2549 /***********************************************************************
2550 * SendMessageTimeoutW (USER32.@)
2552 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2553 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2555 struct send_message_info info;
2557 info.type = MSG_UNICODE;
2560 info.wparam = wparam;
2561 info.lparam = lparam;
2563 info.timeout = timeout;
2565 return send_message( &info, res_ptr, TRUE );
2568 /***********************************************************************
2569 * SendMessageTimeoutA (USER32.@)
2571 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2572 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2574 struct send_message_info info;
2576 info.type = MSG_ASCII;
2579 info.wparam = wparam;
2580 info.lparam = lparam;
2582 info.timeout = timeout;
2583 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
2585 return send_message( &info, res_ptr, FALSE );
2589 /***********************************************************************
2590 * SendMessageW (USER32.@)
2592 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2595 struct send_message_info info;
2597 info.type = MSG_UNICODE;
2600 info.wparam = wparam;
2601 info.lparam = lparam;
2602 info.flags = SMTO_NORMAL;
2605 send_message( &info, &res, TRUE );
2610 /***********************************************************************
2611 * SendMessageA (USER32.@)
2613 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2616 struct send_message_info info;
2618 info.type = MSG_ASCII;
2621 info.wparam = wparam;
2622 info.lparam = lparam;
2623 info.flags = SMTO_NORMAL;
2625 info.wm_char = WMCHAR_MAP_SENDMESSAGE;
2627 send_message( &info, &res, FALSE );
2632 /***********************************************************************
2633 * SendNotifyMessageA (USER32.@)
2635 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2637 struct send_message_info info;
2639 if (is_pointer_message(msg))
2641 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2645 info.type = MSG_NOTIFY;
2648 info.wparam = wparam;
2649 info.lparam = lparam;
2651 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
2653 return send_message( &info, NULL, FALSE );
2657 /***********************************************************************
2658 * SendNotifyMessageW (USER32.@)
2660 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2662 struct send_message_info info;
2664 if (is_pointer_message(msg))
2666 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2670 info.type = MSG_NOTIFY;
2673 info.wparam = wparam;
2674 info.lparam = lparam;
2677 return send_message( &info, NULL, TRUE );
2681 /***********************************************************************
2682 * SendMessageCallbackA (USER32.@)
2684 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2685 SENDASYNCPROC callback, ULONG_PTR data )
2687 struct send_message_info info;
2689 if (is_pointer_message(msg))
2691 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2695 info.type = MSG_CALLBACK;
2698 info.wparam = wparam;
2699 info.lparam = lparam;
2700 info.callback = callback;
2703 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
2705 return send_message( &info, NULL, FALSE );
2709 /***********************************************************************
2710 * SendMessageCallbackW (USER32.@)
2712 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2713 SENDASYNCPROC callback, ULONG_PTR data )
2715 struct send_message_info info;
2717 if (is_pointer_message(msg))
2719 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2723 info.type = MSG_CALLBACK;
2726 info.wparam = wparam;
2727 info.lparam = lparam;
2728 info.callback = callback;
2732 return send_message( &info, NULL, TRUE );
2736 /***********************************************************************
2737 * ReplyMessage (USER32.@)
2739 BOOL WINAPI ReplyMessage( LRESULT result )
2741 struct received_message_info *info = get_user_thread_info()->receive_info;
2743 if (!info) return FALSE;
2744 reply_message( info, result, FALSE );
2749 /***********************************************************************
2750 * InSendMessage (USER32.@)
2752 BOOL WINAPI InSendMessage(void)
2754 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
2758 /***********************************************************************
2759 * InSendMessageEx (USER32.@)
2761 DWORD WINAPI InSendMessageEx( LPVOID reserved )
2763 struct received_message_info *info = get_user_thread_info()->receive_info;
2765 if (info) return info->flags;
2766 return ISMEX_NOSEND;
2770 /***********************************************************************
2771 * PostMessageA (USER32.@)
2773 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2775 if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
2776 return PostMessageW( hwnd, msg, wparam, lparam );
2780 /***********************************************************************
2781 * PostMessageW (USER32.@)
2783 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2785 struct send_message_info info;
2787 if (is_pointer_message( msg ))
2789 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2793 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
2794 hwnd, msg, SPY_GetMsgName(msg, hwnd), wparam, lparam );
2796 info.type = MSG_POSTED;
2799 info.wparam = wparam;
2800 info.lparam = lparam;
2803 if (is_broadcast(hwnd))
2805 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2809 if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2811 if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2813 if (USER_IsExitingThread( info.dest_tid )) return TRUE;
2815 return put_message_in_queue( &info, NULL );
2819 /**********************************************************************
2820 * PostThreadMessageA (USER32.@)
2822 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2824 if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
2825 return PostThreadMessageW( thread, msg, wparam, lparam );
2829 /**********************************************************************
2830 * PostThreadMessageW (USER32.@)
2832 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2834 struct send_message_info info;
2836 if (is_pointer_message( msg ))
2838 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2841 if (USER_IsExitingThread( thread )) return TRUE;
2843 info.type = MSG_POSTED;
2844 info.dest_tid = thread;
2847 info.wparam = wparam;
2848 info.lparam = lparam;
2850 return put_message_in_queue( &info, NULL );
2854 /***********************************************************************
2855 * PostQuitMessage (USER32.@)
2857 * Posts a quit message to the current thread's message queue.
2860 * exit_code [I] Exit code to return from message loop.
2866 * This function is not the same as calling:
2867 *|PostThreadMessage(GetCurrentThreadId(), WM_QUIT, exit_code, 0);
2868 * It instead sets a flag in the message queue that signals it to generate
2869 * a WM_QUIT message when there are no other pending sent or posted messages
2872 void WINAPI PostQuitMessage( INT exit_code )
2874 SERVER_START_REQ( post_quit_message )
2876 req->exit_code = exit_code;
2877 wine_server_call( req );
2883 /***********************************************************************
2884 * PeekMessageW (USER32.@)
2886 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2890 USER_CheckNotLock();
2892 /* check for graphics events */
2893 USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
2895 if (!peek_message( &msg, hwnd, first, last, flags, 0 ))
2897 if (!(flags & PM_NOYIELD))
2900 ReleaseThunkLock(&count);
2902 if (count) RestoreThunkLock(count);
2907 /* copy back our internal safe copy of message data to msg_out.
2908 * msg_out is a variable from the *program*, so it can't be used
2909 * internally as it can get "corrupted" by our use of SendMessage()
2910 * (back to the program) inside the message handling itself. */
2913 SetLastError( ERROR_NOACCESS );
2921 /***********************************************************************
2922 * PeekMessageA (USER32.@)
2924 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2926 if (get_pending_wmchar( msg, first, last, (flags & PM_REMOVE) )) return TRUE;
2927 if (!PeekMessageW( msg, hwnd, first, last, flags )) return FALSE;
2928 map_wparam_WtoA( msg, (flags & PM_REMOVE) );
2933 /***********************************************************************
2934 * GetMessageW (USER32.@)
2936 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2938 HANDLE server_queue = get_server_queue_handle();
2939 unsigned int mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2941 USER_CheckNotLock();
2943 /* check for graphics events */
2944 USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
2948 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2949 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2950 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2951 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2952 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2953 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2955 else mask = QS_ALLINPUT;
2957 while (!peek_message( msg, hwnd, first, last, PM_REMOVE | (mask << 16), mask ))
2961 ReleaseThunkLock( &dwlc );
2962 USER_Driver->pMsgWaitForMultipleObjectsEx( 1, &server_queue, INFINITE, mask, 0 );
2963 if (dwlc) RestoreThunkLock( dwlc );
2966 return (msg->message != WM_QUIT);
2970 /***********************************************************************
2971 * GetMessageA (USER32.@)
2973 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2975 if (get_pending_wmchar( msg, first, last, TRUE )) return TRUE;
2976 GetMessageW( msg, hwnd, first, last );
2977 map_wparam_WtoA( msg, TRUE );
2978 return (msg->message != WM_QUIT);
2982 /***********************************************************************
2983 * IsDialogMessageA (USER32.@)
2984 * IsDialogMessage (USER32.@)
2986 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2989 map_wparam_AtoW( msg.message, &msg.wParam, WMCHAR_MAP_NOMAPPING );
2990 return IsDialogMessageW( hwndDlg, &msg );
2994 /***********************************************************************
2995 * TranslateMessage (USER32.@)
2997 * Implementation of TranslateMessage.
2999 * TranslateMessage translates virtual-key messages into character-messages,
3001 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
3002 * ditto replacing WM_* with WM_SYS*
3003 * This produces WM_CHAR messages only for keys mapped to ASCII characters
3004 * by the keyboard driver.
3006 * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
3007 * return value is nonzero, regardless of the translation.
3010 BOOL WINAPI TranslateMessage( const MSG *msg )
3016 if (msg->message < WM_KEYFIRST || msg->message > WM_KEYLAST) return FALSE;
3017 if (msg->message != WM_KEYDOWN && msg->message != WM_SYSKEYDOWN) return TRUE;
3019 TRACE_(key)("Translating key %s (%04lx), scancode %04x\n",
3020 SPY_GetVKeyName(msg->wParam), msg->wParam, HIWORD(msg->lParam));
3022 switch (msg->wParam)
3025 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
3026 TRACE_(key)("PostMessageW(%p,%s,%04x,%08x)\n",
3027 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), HIWORD(msg->lParam), LOWORD(msg->lParam));
3028 PostMessageW( msg->hwnd, message, HIWORD(msg->lParam), LOWORD(msg->lParam));
3032 return ImmTranslateMessage(msg->hwnd, msg->message, msg->wParam, msg->lParam);
3035 GetKeyboardState( state );
3036 /* FIXME : should handle ToUnicode yielding 2 */
3037 switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), state, wp, 2, 0))
3040 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
3041 TRACE_(key)("1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
3042 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
3043 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
3047 message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
3048 TRACE_(key)("-1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
3049 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
3050 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
3057 /***********************************************************************
3058 * DispatchMessageA (USER32.@)
3060 * See DispatchMessageW.
3062 LRESULT WINAPI DECLSPEC_HOTPATCH DispatchMessageA( const MSG* msg )
3066 /* Process timer messages */
3067 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3073 retval = CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
3074 msg->message, msg->wParam, GetTickCount() );
3084 if (!msg->hwnd) return 0;
3086 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3087 msg->wParam, msg->lParam );
3089 if (!WINPROC_call_window( msg->hwnd, msg->message, msg->wParam, msg->lParam,
3090 &retval, FALSE, WMCHAR_MAP_DISPATCHMESSAGE ))
3092 if (!IsWindow( msg->hwnd )) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3093 else SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3097 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3098 msg->wParam, msg->lParam );
3100 if (msg->message == WM_PAINT)
3102 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3103 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3104 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3105 DeleteObject( hrgn );
3111 /***********************************************************************
3112 * DispatchMessageW (USER32.@) Process a message
3114 * Process the message specified in the structure *_msg_.
3116 * If the lpMsg parameter points to a WM_TIMER message and the
3117 * parameter of the WM_TIMER message is not NULL, the lParam parameter
3118 * points to the function that is called instead of the window
3119 * procedure. The function stored in lParam (timer callback) is protected
3120 * from causing page-faults.
3122 * The message must be valid.
3126 * DispatchMessage() returns the result of the window procedure invoked.
3133 LRESULT WINAPI DECLSPEC_HOTPATCH DispatchMessageW( const MSG* msg )
3137 /* Process timer messages */
3138 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3144 retval = CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
3145 msg->message, msg->wParam, GetTickCount() );
3155 if (!msg->hwnd) return 0;
3157 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3158 msg->wParam, msg->lParam );
3160 if (!WINPROC_call_window( msg->hwnd, msg->message, msg->wParam, msg->lParam,
3161 &retval, TRUE, WMCHAR_MAP_DISPATCHMESSAGE ))
3163 if (!IsWindow( msg->hwnd )) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3164 else SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3168 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3169 msg->wParam, msg->lParam );
3171 if (msg->message == WM_PAINT)
3173 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3174 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3175 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3176 DeleteObject( hrgn );
3182 /***********************************************************************
3183 * GetMessagePos (USER.119)
3184 * GetMessagePos (USER32.@)
3186 * The GetMessagePos() function returns a long value representing a
3187 * cursor position, in screen coordinates, when the last message
3188 * retrieved by the GetMessage() function occurs. The x-coordinate is
3189 * in the low-order word of the return value, the y-coordinate is in
3190 * the high-order word. The application can use the MAKEPOINT()
3191 * macro to obtain a POINT structure from the return value.
3193 * For the current cursor position, use GetCursorPos().
3197 * Cursor position of last message on success, zero on failure.
3204 DWORD WINAPI GetMessagePos(void)
3206 return get_user_thread_info()->GetMessagePosVal;
3210 /***********************************************************************
3211 * GetMessageTime (USER.120)
3212 * GetMessageTime (USER32.@)
3214 * GetMessageTime() returns the message time for the last message
3215 * retrieved by the function. The time is measured in milliseconds with
3216 * the same offset as GetTickCount().
3218 * Since the tick count wraps, this is only useful for moderately short
3219 * relative time comparisons.
3223 * Time of last message on success, zero on failure.
3225 LONG WINAPI GetMessageTime(void)
3227 return get_user_thread_info()->GetMessageTimeVal;
3231 /***********************************************************************
3232 * GetMessageExtraInfo (USER.288)
3233 * GetMessageExtraInfo (USER32.@)
3235 LPARAM WINAPI GetMessageExtraInfo(void)
3237 return get_user_thread_info()->GetMessageExtraInfoVal;
3241 /***********************************************************************
3242 * SetMessageExtraInfo (USER32.@)
3244 LPARAM WINAPI SetMessageExtraInfo(LPARAM lParam)
3246 struct user_thread_info *thread_info = get_user_thread_info();
3247 LONG old_value = thread_info->GetMessageExtraInfoVal;
3248 thread_info->GetMessageExtraInfoVal = lParam;
3253 /***********************************************************************
3254 * WaitMessage (USER.112) Suspend thread pending messages
3255 * WaitMessage (USER32.@) Suspend thread pending messages
3257 * WaitMessage() suspends a thread until events appear in the thread's
3260 BOOL WINAPI WaitMessage(void)
3262 return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
3266 /***********************************************************************
3267 * MsgWaitForMultipleObjectsEx (USER32.@)
3269 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
3270 DWORD timeout, DWORD mask, DWORD flags )
3272 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
3275 if (count > MAXIMUM_WAIT_OBJECTS-1)
3277 SetLastError( ERROR_INVALID_PARAMETER );
3281 /* set the queue mask */
3282 SERVER_START_REQ( set_queue_mask )
3284 req->wake_mask = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
3285 req->changed_mask = mask;
3287 wine_server_call( req );
3291 /* add the queue to the handle list */
3292 for (i = 0; i < count; i++) handles[i] = pHandles[i];
3293 handles[count] = get_server_queue_handle();
3295 ReleaseThunkLock( &lock );
3296 ret = USER_Driver->pMsgWaitForMultipleObjectsEx( count+1, handles, timeout, mask, flags );
3297 if (lock) RestoreThunkLock( lock );
3302 /***********************************************************************
3303 * MsgWaitForMultipleObjects (USER32.@)
3305 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
3306 BOOL wait_all, DWORD timeout, DWORD mask )
3308 return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
3309 wait_all ? MWMO_WAITALL : 0 );
3313 /***********************************************************************
3314 * WaitForInputIdle (USER32.@)
3316 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
3318 DWORD start_time, elapsed, ret;
3321 handles[0] = hProcess;
3322 SERVER_START_REQ( get_process_idle_event )
3324 req->handle = wine_server_obj_handle( hProcess );
3325 if (!(ret = wine_server_call_err( req )))
3326 handles[1] = wine_server_ptr_handle( reply->event );
3329 if (ret) return WAIT_FAILED; /* error */
3330 if (!handles[1]) return 0; /* no event to wait on */
3332 start_time = GetTickCount();
3335 TRACE("waiting for %p\n", handles[1] );
3338 ret = MsgWaitForMultipleObjects ( 2, handles, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
3343 case WAIT_OBJECT_0+2:
3344 process_sent_messages();
3348 TRACE("timeout or error\n");
3351 TRACE("finished\n");
3354 if (dwTimeOut != INFINITE)
3356 elapsed = GetTickCount() - start_time;
3357 if (elapsed > dwTimeOut)
3363 return WAIT_TIMEOUT;
3367 /***********************************************************************
3368 * RegisterWindowMessageA (USER32.@)
3369 * RegisterWindowMessage (USER.118)
3371 UINT WINAPI RegisterWindowMessageA( LPCSTR str )
3373 UINT ret = GlobalAddAtomA(str);
3374 TRACE("%s, ret=%x\n", str, ret);
3379 /***********************************************************************
3380 * RegisterWindowMessageW (USER32.@)
3382 UINT WINAPI RegisterWindowMessageW( LPCWSTR str )
3384 UINT ret = GlobalAddAtomW(str);
3385 TRACE("%s ret=%x\n", debugstr_w(str), ret);
3389 typedef struct BroadcastParm
3400 static BOOL CALLBACK bcast_childwindow( HWND hw, LPARAM lp )
3402 BroadcastParm *parm = (BroadcastParm*)lp;
3403 DWORD_PTR retval = 0;
3406 if (parm->flags & BSF_IGNORECURRENTTASK && WIN_IsCurrentProcess(hw))
3408 TRACE("Not telling myself %p\n", hw);
3412 /* I don't know 100% for sure if this is what Windows does, but it fits the tests */
3413 if (parm->flags & BSF_QUERY)
3415 TRACE("Telling window %p using SendMessageTimeout\n", hw);
3417 /* Not tested for conflicting flags */
3418 if (parm->flags & BSF_FORCEIFHUNG || parm->flags & BSF_NOHANG)
3419 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_ABORTIFHUNG, 2000, &retval );
3420 else if (parm->flags & BSF_NOTIMEOUTIFNOTHUNG)
3421 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NOTIMEOUTIFNOTHUNG, 2000, &retval );
3423 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NORMAL, 2000, &retval );
3425 if (!lresult && GetLastError() == ERROR_TIMEOUT)
3427 WARN("Timed out!\n");
3428 if (!(parm->flags & BSF_FORCEIFHUNG))
3431 if (retval == BROADCAST_QUERY_DENY)
3440 else if (parm->flags & BSF_POSTMESSAGE)
3442 TRACE("Telling window %p using PostMessage\n", hw);
3443 PostMessageW( hw, parm->msg, parm->wp, parm->lp );
3447 TRACE("Telling window %p using SendNotifyMessage\n", hw);
3448 SendNotifyMessageW( hw, parm->msg, parm->wp, parm->lp );
3454 static BOOL CALLBACK bcast_desktop( LPWSTR desktop, LPARAM lp )
3458 BroadcastParm *parm = (BroadcastParm*)lp;
3460 TRACE("desktop: %s\n", debugstr_w( desktop ));
3462 hdesktop = open_winstation_desktop( parm->winsta, desktop, 0, FALSE, DESKTOP_ENUMERATE|DESKTOP_WRITEOBJECTS|STANDARD_RIGHTS_WRITE );
3465 FIXME("Could not open desktop %s\n", debugstr_w(desktop));
3469 ret = EnumDesktopWindows( hdesktop, bcast_childwindow, lp );
3470 CloseDesktop(hdesktop);
3471 TRACE("-->%d\n", ret);
3472 return parm->success;
3475 static BOOL CALLBACK bcast_winsta( LPWSTR winsta, LPARAM lp )
3478 HWINSTA hwinsta = OpenWindowStationW( winsta, FALSE, WINSTA_ENUMDESKTOPS );
3479 TRACE("hwinsta: %p/%s/%08x\n", hwinsta, debugstr_w( winsta ), GetLastError());
3482 ((BroadcastParm *)lp)->winsta = hwinsta;
3483 ret = EnumDesktopsW( hwinsta, bcast_desktop, lp );
3484 CloseWindowStation( hwinsta );
3485 TRACE("-->%d\n", ret);
3489 /***********************************************************************
3490 * BroadcastSystemMessageA (USER32.@)
3491 * BroadcastSystemMessage (USER32.@)
3493 LONG WINAPI BroadcastSystemMessageA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
3495 return BroadcastSystemMessageExA( flags, recipients, msg, wp, lp, NULL );
3499 /***********************************************************************
3500 * BroadcastSystemMessageW (USER32.@)
3502 LONG WINAPI BroadcastSystemMessageW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
3504 return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, NULL );
3507 /***********************************************************************
3508 * BroadcastSystemMessageExA (USER32.@)
3510 LONG WINAPI BroadcastSystemMessageExA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
3512 map_wparam_AtoW( msg, &wp, WMCHAR_MAP_NOMAPPING );
3513 return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, NULL );
3517 /***********************************************************************
3518 * BroadcastSystemMessageExW (USER32.@)
3520 LONG WINAPI BroadcastSystemMessageExW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
3523 DWORD recips = BSM_ALLCOMPONENTS;
3525 static const DWORD all_flags = ( BSF_QUERY | BSF_IGNORECURRENTTASK | BSF_FLUSHDISK | BSF_NOHANG
3526 | BSF_POSTMESSAGE | BSF_FORCEIFHUNG | BSF_NOTIMEOUTIFNOTHUNG
3527 | BSF_ALLOWSFW | BSF_SENDNOTIFYMESSAGE | BSF_RETURNHDESK | BSF_LUID );
3529 TRACE("Flags: %08x, recipients: %p(0x%x), msg: %04x, wparam: %08lx, lparam: %08lx\n", flags, recipients,
3530 (recipients ? *recipients : recips), msg, wp, lp);
3532 if (flags & ~all_flags)
3534 SetLastError(ERROR_INVALID_PARAMETER);
3539 recipients = &recips;
3541 if ( pinfo && flags & BSF_QUERY )
3542 FIXME("Not returning PBSMINFO information yet\n");
3545 parm.recipients = recipients;
3549 parm.success = TRUE;
3551 if (*recipients & BSM_ALLDESKTOPS || *recipients == BSM_ALLCOMPONENTS)
3552 ret = EnumWindowStationsW(bcast_winsta, (LONG_PTR)&parm);
3553 else if (*recipients & BSM_APPLICATIONS)
3555 EnumWindows(bcast_childwindow, (LONG_PTR)&parm);
3559 FIXME("Recipients %08x not supported!\n", *recipients);
3564 /***********************************************************************
3565 * SetMessageQueue (USER32.@)
3567 BOOL WINAPI SetMessageQueue( INT size )
3569 /* now obsolete the message queue will be expanded dynamically as necessary */
3574 /***********************************************************************
3575 * MessageBeep (USER32.@)
3577 BOOL WINAPI MessageBeep( UINT i )
3580 SystemParametersInfoA( SPI_GETBEEP, 0, &active, FALSE );
3581 if (active) USER_Driver->pBeep();
3586 /***********************************************************************
3587 * SetTimer (USER32.@)
3589 UINT_PTR WINAPI SetTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3592 WNDPROC winproc = 0;
3594 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, NULL );
3596 SERVER_START_REQ( set_win_timer )
3598 req->win = wine_server_user_handle( hwnd );
3599 req->msg = WM_TIMER;
3601 req->rate = max( timeout, SYS_TIMER_RATE );
3602 req->lparam = (ULONG_PTR)winproc;
3603 if (!wine_server_call_err( req ))
3606 if (!ret) ret = TRUE;
3612 TRACE("Added %p %lx %p timeout %d\n", hwnd, id, winproc, timeout );
3617 /***********************************************************************
3618 * SetSystemTimer (USER32.@)
3620 UINT_PTR WINAPI SetSystemTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3623 WNDPROC winproc = 0;
3625 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, NULL );
3627 SERVER_START_REQ( set_win_timer )
3629 req->win = wine_server_user_handle( hwnd );
3630 req->msg = WM_SYSTIMER;
3632 req->rate = max( timeout, SYS_TIMER_RATE );
3633 req->lparam = (ULONG_PTR)winproc;
3634 if (!wine_server_call_err( req ))
3637 if (!ret) ret = TRUE;
3643 TRACE("Added %p %lx %p timeout %d\n", hwnd, id, winproc, timeout );
3648 /***********************************************************************
3649 * KillTimer (USER32.@)
3651 BOOL WINAPI KillTimer( HWND hwnd, UINT_PTR id )
3655 SERVER_START_REQ( kill_win_timer )
3657 req->win = wine_server_user_handle( hwnd );
3658 req->msg = WM_TIMER;
3660 ret = !wine_server_call_err( req );
3667 /***********************************************************************
3668 * KillSystemTimer (USER32.@)
3670 BOOL WINAPI KillSystemTimer( HWND hwnd, UINT_PTR id )
3674 SERVER_START_REQ( kill_win_timer )
3676 req->win = wine_server_user_handle( hwnd );
3677 req->msg = WM_SYSTIMER;
3679 ret = !wine_server_call_err( req );
3686 /**********************************************************************
3687 * GetGUIThreadInfo (USER32.@)
3689 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
3693 SERVER_START_REQ( get_thread_input )
3696 if ((ret = !wine_server_call_err( req )))
3699 info->hwndActive = wine_server_ptr_handle( reply->active );
3700 info->hwndFocus = wine_server_ptr_handle( reply->focus );
3701 info->hwndCapture = wine_server_ptr_handle( reply->capture );
3702 info->hwndMenuOwner = wine_server_ptr_handle( reply->menu_owner );
3703 info->hwndMoveSize = wine_server_ptr_handle( reply->move_size );
3704 info->hwndCaret = wine_server_ptr_handle( reply->caret );
3705 info->rcCaret.left = reply->rect.left;
3706 info->rcCaret.top = reply->rect.top;
3707 info->rcCaret.right = reply->rect.right;
3708 info->rcCaret.bottom = reply->rect.bottom;
3709 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
3710 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
3711 if (reply->caret) info->flags |= GUI_CARETBLINKING;
3719 /******************************************************************
3720 * IsHungAppWindow (USER32.@)
3723 BOOL WINAPI IsHungAppWindow( HWND hWnd )
3727 SERVER_START_REQ( is_window_hung )
3729 req->win = wine_server_user_handle( hWnd );
3730 ret = !wine_server_call_err( req ) && reply->is_hung;
3736 /******************************************************************
3737 * ChangeWindowMessageFilter (USER32.@)
3739 BOOL WINAPI ChangeWindowMessageFilter( UINT message, DWORD flag )
3741 FIXME( "%x %08x\n", message, flag );