2 * Window messaging support
4 * Copyright 2001 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
30 #include "wine/unicode.h"
31 #include "wine/server.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msg);
43 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
44 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
46 #define MAX_PACK_COUNT 4
48 /* description of the data fields that need to be packed along with a sent message */
52 const void *data[MAX_PACK_COUNT];
53 size_t size[MAX_PACK_COUNT];
56 /* info about the message currently being received by the current thread */
57 struct received_message_info
59 enum message_type type;
61 UINT flags; /* InSendMessageEx return flags */
64 /* structure to group all parameters for sent messages of the various kinds */
65 struct send_message_info
67 enum message_type type;
72 UINT flags; /* flags for SendMessageTimeout */
73 UINT timeout; /* timeout for SendMessageTimeout */
74 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
75 ULONG_PTR data; /* callback data */
79 /* flag for messages that contain pointers */
80 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
82 #define SET(msg) (1 << ((msg) & 31))
84 static const unsigned int message_pointer_flags[] =
87 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
88 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
90 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
93 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
94 SET(WM_NOTIFY) | SET(WM_HELP),
96 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
98 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
100 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
102 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
104 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO),
110 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
111 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
112 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
116 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
117 SET(LB_DIR) | SET(LB_FINDSTRING) |
118 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
120 SET(LB_FINDSTRINGEXACT),
126 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
128 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
129 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
143 SET(WM_ASKCBFORMATNAME)
146 /* flags for messages that contain Unicode strings */
147 static const unsigned int message_unicode_flags[] =
150 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
151 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
163 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
167 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
171 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
172 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
176 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
177 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
179 SET(LB_FINDSTRINGEXACT),
201 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
204 /* check whether a given message type includes pointers */
205 inline static int is_pointer_message( UINT message )
207 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
208 return (message_pointer_flags[message / 32] & SET(message)) != 0;
211 /* check whether a given message type contains Unicode (or ASCII) chars */
212 inline static int is_unicode_message( UINT message )
214 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
215 return (message_unicode_flags[message / 32] & SET(message)) != 0;
220 /* add a data field to a packed message */
221 inline static void push_data( struct packed_message *data, const void *ptr, size_t size )
223 data->data[data->count] = ptr;
224 data->size[data->count] = size;
228 /* add a string to a packed message */
229 inline static void push_string( struct packed_message *data, LPCWSTR str )
231 push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
234 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
235 inline static void *get_data( void **buffer, size_t size )
238 *buffer = (char *)*buffer + size;
242 /* make sure that the buffer contains a valid null-terminated Unicode string */
243 inline static BOOL check_string( LPCWSTR str, size_t size )
245 for (size /= sizeof(WCHAR); size; size--, str++)
246 if (!*str) return TRUE;
250 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
251 inline static void *get_buffer_space( void **buffer, size_t size )
254 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
255 HeapFree( GetProcessHeap(), 0, *buffer );
260 /* retrieve a string pointer from packed data */
261 inline static LPWSTR get_string( void **buffer )
263 return get_data( buffer, (strlenW( (LPWSTR)*buffer ) + 1) * sizeof(WCHAR) );
266 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
267 inline static BOOL combobox_has_strings( HWND hwnd )
269 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
270 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
273 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
274 inline static BOOL listbox_has_strings( HWND hwnd )
276 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
277 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
280 /* check if hwnd is a broadcast magic handle */
281 inline static BOOL is_broadcast( HWND hwnd )
283 return (hwnd == HWND_BROADCAST || hwnd == HWND_TOPMOST);
287 /***********************************************************************
288 * broadcast_message_callback
290 * Helper callback for broadcasting messages.
292 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
294 struct send_message_info *info = (struct send_message_info *)lparam;
295 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
299 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
300 info->flags, info->timeout, NULL );
303 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
304 info->flags, info->timeout, NULL );
307 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
310 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
311 info->callback, info->data );
314 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
317 ERR( "bad type %d\n", info->type );
324 /***********************************************************************
327 * Convert the wparam of an ASCII message to Unicode.
329 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
331 if (message == WM_CHARTOITEM ||
332 message == EM_SETPASSWORDCHAR ||
333 message == WM_CHAR ||
334 message == WM_DEADCHAR ||
335 message == WM_SYSCHAR ||
336 message == WM_SYSDEADCHAR ||
337 message == WM_MENUCHAR)
339 char ch = LOWORD(wparam);
341 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
342 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
348 /***********************************************************************
351 * Convert the wparam of a Unicode message to ASCII.
353 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
355 if (message == WM_CHARTOITEM ||
356 message == EM_SETPASSWORDCHAR ||
357 message == WM_CHAR ||
358 message == WM_DEADCHAR ||
359 message == WM_SYSCHAR ||
360 message == WM_SYSDEADCHAR ||
361 message == WM_MENUCHAR)
363 WCHAR wch = LOWORD(wparam);
365 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
366 wparam = MAKEWPARAM( (unsigned char)ch, HIWORD(wparam) );
372 /***********************************************************************
375 * Pack a message for sending to another process.
376 * Return the size of the data we expect in the message reply.
377 * Set data->count to -1 if there is an error.
379 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
380 struct packed_message *data )
388 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
389 push_data( data, cs, sizeof(*cs) );
390 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
391 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
395 case WM_ASKCBFORMATNAME:
396 return wparam * sizeof(WCHAR);
398 case WM_WININICHANGE:
399 case WM_DEVMODECHANGE:
404 push_string( data, (LPWSTR)lparam );
406 case WM_GETMINMAXINFO:
407 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
408 return sizeof(MINMAXINFO);
410 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
413 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
414 return sizeof(MEASUREITEMSTRUCT);
416 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
419 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
421 case WM_WINDOWPOSCHANGING:
422 case WM_WINDOWPOSCHANGED:
423 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
424 return sizeof(WINDOWPOS);
427 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
428 push_data( data, cp, sizeof(*cp) );
429 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
433 /* WM_NOTIFY cannot be sent across processes (MSDN) */
437 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
439 case WM_STYLECHANGING:
440 case WM_STYLECHANGED:
441 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
446 push_data( data, (RECT *)lparam, sizeof(RECT) );
451 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
452 push_data( data, nc, sizeof(*nc) );
453 push_data( data, nc->lppos, sizeof(*nc->lppos) );
454 return sizeof(*nc) + sizeof(*nc->lppos);
457 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
459 case SBM_SETSCROLLINFO:
460 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
462 case SBM_GETSCROLLINFO:
463 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
464 return sizeof(SCROLLINFO);
470 if (wparam) size += sizeof(DWORD);
471 if (lparam) size += sizeof(DWORD);
476 case CB_GETDROPPEDCONTROLRECT:
480 push_data( data, (RECT *)lparam, sizeof(RECT) );
484 WORD *pw = (WORD *)lparam;
485 push_data( data, pw, sizeof(*pw) );
486 return *pw * sizeof(WCHAR);
490 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
493 case CB_INSERTSTRING:
495 case CB_FINDSTRINGEXACT:
496 case CB_SELECTSTRING:
497 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
500 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
501 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
503 case LB_INSERTSTRING:
505 case LB_FINDSTRINGEXACT:
506 case LB_SELECTSTRING:
507 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
510 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
511 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
513 return wparam * sizeof(UINT);
515 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
516 return sizeof(MDINEXTMENU);
519 push_data( data, (RECT *)lparam, sizeof(RECT) );
523 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
524 push_data( data, cs, sizeof(*cs) );
525 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
526 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
529 case WM_MDIGETACTIVE:
530 if (lparam) return sizeof(BOOL);
532 case WM_WINE_SETWINDOWPOS:
533 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
536 /* these contain an HFONT */
539 /* these contain an HDC */
542 case WM_ICONERASEBKGND:
544 case WM_CTLCOLORMSGBOX:
545 case WM_CTLCOLOREDIT:
546 case WM_CTLCOLORLISTBOX:
549 case WM_CTLCOLORSCROLLBAR:
550 case WM_CTLCOLORSTATIC:
553 /* these contain an HGLOBAL */
554 case WM_PAINTCLIPBOARD:
555 case WM_SIZECLIPBOARD:
556 /* these contain pointers */
558 case WM_QUERYDROPOBJECT:
562 case WM_DEVICECHANGE:
563 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
571 /***********************************************************************
574 * Unpack a message received from another process.
576 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
577 void **buffer, size_t size )
586 CREATESTRUCTW *cs = *buffer;
587 WCHAR *str = (WCHAR *)(cs + 1);
588 if (size < sizeof(*cs)) return FALSE;
590 if (HIWORD(cs->lpszName))
592 if (!check_string( str, size )) return FALSE;
594 size -= (strlenW(str) + 1) * sizeof(WCHAR);
595 str += strlenW(str) + 1;
597 if (HIWORD(cs->lpszClass))
599 if (!check_string( str, size )) return FALSE;
605 case WM_ASKCBFORMATNAME:
606 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
609 case WM_WININICHANGE:
610 case WM_DEVMODECHANGE:
615 if (!check_string( *buffer, size )) return FALSE;
617 case WM_GETMINMAXINFO:
618 minsize = sizeof(MINMAXINFO);
621 minsize = sizeof(DRAWITEMSTRUCT);
624 minsize = sizeof(MEASUREITEMSTRUCT);
627 minsize = sizeof(DELETEITEMSTRUCT);
630 minsize = sizeof(COMPAREITEMSTRUCT);
632 case WM_WINDOWPOSCHANGING:
633 case WM_WINDOWPOSCHANGED:
634 case WM_WINE_SETWINDOWPOS:
635 minsize = sizeof(WINDOWPOS);
639 COPYDATASTRUCT *cp = *buffer;
640 if (size < sizeof(*cp)) return FALSE;
643 minsize = sizeof(*cp) + cp->cbData;
649 /* WM_NOTIFY cannot be sent across processes (MSDN) */
652 minsize = sizeof(HELPINFO);
654 case WM_STYLECHANGING:
655 case WM_STYLECHANGED:
656 minsize = sizeof(STYLESTRUCT);
659 if (!*wparam) minsize = sizeof(RECT);
662 NCCALCSIZE_PARAMS *nc = *buffer;
663 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
664 nc->lppos = (WINDOWPOS *)(nc + 1);
668 if (!*lparam) return TRUE;
669 minsize = sizeof(MSG);
671 case SBM_SETSCROLLINFO:
672 minsize = sizeof(SCROLLINFO);
674 case SBM_GETSCROLLINFO:
675 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
680 if (*wparam || *lparam)
682 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
683 if (*wparam) *wparam = (WPARAM)*buffer;
684 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
689 case CB_GETDROPPEDCONTROLRECT:
690 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
694 minsize = sizeof(RECT);
699 if (size < sizeof(WORD)) return FALSE;
700 len = *(WORD *)*buffer;
701 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
702 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
707 if (!*wparam) return TRUE;
708 minsize = *wparam * sizeof(UINT);
711 case CB_INSERTSTRING:
713 case CB_FINDSTRINGEXACT:
714 case CB_SELECTSTRING:
716 case LB_INSERTSTRING:
718 case LB_FINDSTRINGEXACT:
719 case LB_SELECTSTRING:
720 if (!*buffer) return TRUE;
721 if (!check_string( *buffer, size )) return FALSE;
725 size = sizeof(ULONG_PTR);
726 if (combobox_has_strings( hwnd ))
727 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
728 if (!get_buffer_space( buffer, size )) return FALSE;
733 size = sizeof(ULONG_PTR);
734 if (listbox_has_strings( hwnd ))
735 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
736 if (!get_buffer_space( buffer, size )) return FALSE;
740 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
743 minsize = sizeof(MDINEXTMENU);
744 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
748 minsize = sizeof(RECT);
749 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
753 MDICREATESTRUCTW *cs = *buffer;
754 WCHAR *str = (WCHAR *)(cs + 1);
755 if (size < sizeof(*cs)) return FALSE;
757 if (HIWORD(cs->szTitle))
759 if (!check_string( str, size )) return FALSE;
761 size -= (strlenW(str) + 1) * sizeof(WCHAR);
762 str += strlenW(str) + 1;
764 if (HIWORD(cs->szClass))
766 if (!check_string( str, size )) return FALSE;
771 case WM_MDIGETACTIVE:
772 if (!*lparam) return TRUE;
773 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
775 /* these contain an HFONT */
778 /* these contain an HDC */
781 case WM_ICONERASEBKGND:
783 case WM_CTLCOLORMSGBOX:
784 case WM_CTLCOLOREDIT:
785 case WM_CTLCOLORLISTBOX:
788 case WM_CTLCOLORSCROLLBAR:
789 case WM_CTLCOLORSTATIC:
792 /* these contain an HGLOBAL */
793 case WM_PAINTCLIPBOARD:
794 case WM_SIZECLIPBOARD:
795 /* these contain pointers */
797 case WM_QUERYDROPOBJECT:
801 case WM_DEVICECHANGE:
802 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
806 return TRUE; /* message doesn't need any unpacking */
809 /* default exit for most messages: check minsize and store buffer in lparam */
810 if (size < minsize) return FALSE;
811 *lparam = (LPARAM)*buffer;
816 /***********************************************************************
819 * Pack a reply to a message for sending to another process.
821 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
822 LRESULT res, struct packed_message *data )
829 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
834 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
836 case WM_GETMINMAXINFO:
837 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
840 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
842 case WM_WINDOWPOSCHANGING:
843 case WM_WINDOWPOSCHANGED:
844 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
847 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
849 case SBM_GETSCROLLINFO:
850 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
854 case CB_GETDROPPEDCONTROLRECT:
857 push_data( data, (RECT *)lparam, sizeof(RECT) );
861 WORD *ptr = (WORD *)lparam;
862 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
866 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
868 case WM_MDIGETACTIVE:
869 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
873 push_data( data, (RECT *)lparam, sizeof(RECT) );
876 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
877 push_data( data, nc, sizeof(*nc) );
878 push_data( data, nc->lppos, sizeof(*nc->lppos) );
884 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
885 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
888 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
891 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
893 case WM_ASKCBFORMATNAME:
894 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
900 /***********************************************************************
903 * Unpack a message reply received from another process.
905 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
906 void *buffer, size_t size )
913 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
914 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
915 memcpy( cs, buffer, min( sizeof(*cs), size ));
916 cs->lpszName = name; /* restore the original pointers */
917 cs->lpszClass = class;
921 case WM_ASKCBFORMATNAME:
922 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
924 case WM_GETMINMAXINFO:
925 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
928 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
930 case WM_WINDOWPOSCHANGING:
931 case WM_WINDOWPOSCHANGED:
932 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
935 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
937 case SBM_GETSCROLLINFO:
938 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
941 case CB_GETDROPPEDCONTROLRECT:
945 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
948 size = min( size, (size_t)*(WORD *)lparam );
949 memcpy( (WCHAR *)lparam, buffer, size );
952 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
956 memcpy( (WCHAR *)lparam, buffer, size );
959 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
961 case WM_MDIGETACTIVE:
962 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
966 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
969 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
970 WINDOWPOS *wp = nc->lppos;
971 memcpy( nc, buffer, min( sizeof(*nc), size ));
972 if (size > sizeof(*nc))
975 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
977 nc->lppos = wp; /* restore the original pointer */
985 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
986 if (size <= sizeof(DWORD)) break;
987 size -= sizeof(DWORD);
988 buffer = (DWORD *)buffer + 1;
990 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
994 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
995 LPCWSTR title = cs->szTitle, class = cs->szClass;
996 memcpy( cs, buffer, min( sizeof(*cs), size ));
997 cs->szTitle = title; /* restore the original pointers */
1002 ERR( "should not happen: unexpected message %x\n", message );
1008 /***********************************************************************
1011 * Send a reply to a sent message.
1013 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1015 struct packed_message data;
1016 int i, replied = info->flags & ISMEX_REPLIED;
1018 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1019 if (!remove && replied) return; /* replied already */
1022 info->flags |= ISMEX_REPLIED;
1024 if (info->type == MSG_OTHER_PROCESS && !replied)
1026 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1027 info->msg.lParam, result, &data );
1030 SERVER_START_REQ( reply_message )
1032 req->result = result;
1033 req->remove = remove;
1034 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1035 wine_server_call( req );
1041 /***********************************************************************
1042 * handle_internal_message
1044 * Handle an internal Wine message instead of calling the window proc.
1046 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1048 if (hwnd == GetDesktopWindow()) return 0;
1051 case WM_WINE_DESTROYWINDOW:
1052 return WIN_DestroyWindow( hwnd );
1053 case WM_WINE_SETWINDOWPOS:
1054 return USER_Driver.pSetWindowPos( (WINDOWPOS *)lparam );
1055 case WM_WINE_SHOWWINDOW:
1056 return ShowWindow( hwnd, wparam );
1057 case WM_WINE_SETPARENT:
1058 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1059 case WM_WINE_SETWINDOWLONG:
1060 return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1061 case WM_WINE_ENABLEWINDOW:
1062 return EnableWindow( hwnd, wparam );
1064 FIXME( "unknown internal message %x\n", msg );
1069 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1070 * to the memory handle, we keep track (in the server side) of all pairs of handle
1071 * used (the client passes its value and the content of the memory handle), and
1072 * the server stored both values (the client, and the local one, created after the
1073 * content). When a ACK message is generated, the list of pair is searched for a
1074 * matching pair, so that the client memory handle can be returned.
1077 HGLOBAL client_hMem;
1078 HGLOBAL server_hMem;
1081 static struct DDE_pair* dde_pairs;
1082 static int dde_num_alloc;
1083 static int dde_num_used;
1084 static CRITICAL_SECTION dde_crst = CRITICAL_SECTION_INIT("Raw_DDE_CritSect");
1086 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1091 EnterCriticalSection(&dde_crst);
1093 /* now remember the pair of hMem on both sides */
1094 if (dde_num_used == dde_num_alloc)
1096 struct DDE_pair* tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1097 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1100 LeaveCriticalSection(&dde_crst);
1104 /* zero out newly allocated part */
1105 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1106 dde_num_alloc += GROWBY;
1109 for (i = 0; i < dde_num_alloc; i++)
1111 if (dde_pairs[i].server_hMem == 0)
1113 dde_pairs[i].client_hMem = chm;
1114 dde_pairs[i].server_hMem = shm;
1119 LeaveCriticalSection(&dde_crst);
1123 static HGLOBAL dde_get_pair(HGLOBAL shm)
1128 EnterCriticalSection(&dde_crst);
1129 for (i = 0; i < dde_num_alloc; i++)
1131 if (dde_pairs[i].server_hMem == shm)
1133 /* free this pair */
1134 dde_pairs[i].server_hMem = 0;
1136 ret = dde_pairs[i].client_hMem;
1140 LeaveCriticalSection(&dde_crst);
1144 /***********************************************************************
1149 static BOOL post_dde_message( DWORD dest_tid, struct packed_message *data, const struct send_message_info *info )
1155 HGLOBAL hunlock = 0;
1159 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1165 /* DDE messages which don't require packing are:
1174 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1175 HGLOBAL h = dde_get_pair( uiHi );
1178 /* send back the value of h on the other side */
1179 push_data( data, &h, sizeof(HGLOBAL) );
1181 TRACE( "send dde-ack %x %08x => %08lx\n", uiLo, uiHi, (DWORD)h );
1186 /* uiHi should contain either an atom or 0 */
1187 TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
1188 lp = MAKELONG( uiLo, uiHi );
1197 size = GlobalSize( (HGLOBAL)uiLo ) ;
1198 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1199 (info->msg == WM_DDE_DATA && size < sizeof(DDEDATA)) ||
1200 (info->msg == WM_DDE_POKE && size < sizeof(DDEPOKE))
1204 else if (info->msg != WM_DDE_DATA) return FALSE;
1209 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1211 push_data( data, ptr, size );
1212 hunlock = (HGLOBAL)uiLo;
1215 TRACE( "send ddepack %u %x\n", size, uiHi );
1217 case WM_DDE_EXECUTE:
1220 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1222 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1223 /* so that the other side can send it back on ACK */
1225 hunlock = (HGLOBAL)info->lparam;
1230 SERVER_START_REQ( send_message )
1232 req->id = (void *)dest_tid;
1233 req->type = info->type;
1234 req->win = info->hwnd;
1235 req->msg = info->msg;
1236 req->wparam = info->wparam;
1238 req->time = GetCurrentTime();
1240 for (i = 0; i < data->count; i++)
1241 wine_server_add_data( req, data->data[i], data->size[i] );
1242 if ((res = wine_server_call( req )))
1244 if (res == STATUS_INVALID_PARAMETER)
1245 /* FIXME: find a STATUS_ value for this one */
1246 SetLastError( ERROR_INVALID_THREAD_ID );
1248 SetLastError( RtlNtStatusToDosError(res) );
1251 FreeDDElParam(info->msg, info->lparam);
1254 if (hunlock) GlobalUnlock(hunlock);
1259 /***********************************************************************
1260 * unpack_dde_message
1262 * Unpack a posted DDE message received from another process.
1264 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1265 void **buffer, size_t size )
1276 /* hMem is being passed */
1277 if (size != sizeof(HGLOBAL)) return FALSE;
1278 if (!buffer || !*buffer) return FALSE;
1280 memcpy( &hMem, *buffer, size );
1282 TRACE("recv dde-ack %u mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( uiHi ));
1286 uiLo = LOWORD( *lparam );
1287 uiHi = HIWORD( *lparam );
1288 TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
1290 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1295 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1297 TRACE( "recv ddepack %u %x\n", size, uiHi );
1300 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1301 if (hMem && (ptr = GlobalLock( hMem )))
1303 memcpy( ptr, *buffer, size );
1304 GlobalUnlock( hMem );
1310 *lparam = PackDDElParam( message, uiLo, uiHi );
1312 case WM_DDE_EXECUTE:
1315 if (!buffer || !*buffer) return FALSE;
1316 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1317 if (hMem && (ptr = GlobalLock( hMem )))
1319 memcpy( ptr, *buffer, size );
1320 GlobalUnlock( hMem );
1321 TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
1322 if (!dde_add_pair( *lparam, hMem ))
1328 } else return FALSE;
1335 /***********************************************************************
1338 * Call a window procedure and the corresponding hooks.
1340 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam, BOOL unicode )
1345 if (msg & 0x80000000) return handle_internal_message( hwnd, msg, wparam, lparam );
1347 /* first the WH_CALLWNDPROC hook */
1348 if (HOOK_IsHooked( WH_CALLWNDPROC ))
1351 cwp.lParam = lparam;
1352 cwp.wParam = wparam;
1354 cwp.hwnd = WIN_GetFullHandle( hwnd );
1355 if (unicode) HOOK_CallHooksW( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1356 else HOOK_CallHooksA( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1357 lparam = cwp.lParam;
1358 wparam = cwp.wParam;
1363 /* now call the window procedure */
1366 if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) return 0;
1367 result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1371 if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) return 0;
1372 result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1375 /* and finally the WH_CALLWNDPROCRET hook */
1376 if (HOOK_IsHooked( WH_CALLWNDPROCRET ))
1379 cwp.lResult = result;
1380 cwp.lParam = lparam;
1381 cwp.wParam = wparam;
1383 cwp.hwnd = WIN_GetFullHandle( hwnd );
1384 if (unicode) HOOK_CallHooksW( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
1385 else HOOK_CallHooksA( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
1391 /***********************************************************************
1394 * Peek for a message matching the given parameters. Return FALSE if none available.
1395 * All pending sent messages are processed before returning.
1397 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1400 ULONG_PTR extra_info = 0;
1401 MESSAGEQUEUE *queue = QUEUE_Current();
1402 struct received_message_info info, *old_info;
1404 if (!first && !last) last = ~0;
1409 void *buffer = NULL;
1410 size_t size = 0, buffer_size = 0;
1412 do /* loop while buffer is too small */
1414 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1416 SERVER_START_REQ( get_message )
1419 req->get_win = hwnd;
1420 req->get_first = first;
1421 req->get_last = last;
1422 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1423 if (!(res = wine_server_call( req )))
1425 size = wine_server_reply_size( reply );
1426 info.type = reply->type;
1427 info.msg.hwnd = reply->win;
1428 info.msg.message = reply->msg;
1429 info.msg.wParam = reply->wparam;
1430 info.msg.lParam = reply->lparam;
1431 info.msg.time = reply->time;
1432 info.msg.pt.x = reply->x;
1433 info.msg.pt.y = reply->y;
1434 extra_info = reply->info;
1438 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1439 buffer_size = reply->total;
1443 } while (res == STATUS_BUFFER_OVERFLOW);
1445 if (res) return FALSE;
1447 TRACE( "got type %d msg %x hwnd %x wp %x lp %lx\n",
1448 info.type, info.msg.message, info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1454 info.flags = ISMEX_SEND;
1457 info.flags = ISMEX_NOTIFY;
1460 info.flags = ISMEX_CALLBACK;
1462 case MSG_OTHER_PROCESS:
1463 info.flags = ISMEX_SEND;
1464 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1465 &info.msg.lParam, &buffer, size ))
1467 ERR( "invalid packed message %x (%s) hwnd %x wp %x lp %lx size %d\n",
1468 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
1469 info.msg.wParam, info.msg.lParam, size );
1471 reply_message( &info, 0, TRUE );
1475 case MSG_HARDWARE_RAW:
1476 if (!MSG_process_raw_hardware_message( &info.msg, extra_info,
1477 hwnd, first, last, flags & GET_MSG_REMOVE ))
1480 case MSG_HARDWARE_COOKED:
1481 if (!MSG_process_cooked_hardware_message( &info.msg, extra_info,
1482 flags & GET_MSG_REMOVE ))
1484 flags |= GET_MSG_REMOVE_LAST;
1487 queue->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
1490 queue->GetMessageExtraInfoVal = extra_info;
1491 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
1493 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1494 &info.msg.lParam, &buffer, size ))
1496 ERR( "invalid packed dde-message %x (%s) hwnd %x wp %x lp %lx size %d\n",
1497 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1498 info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
1504 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1508 /* if we get here, we have a sent message; call the window procedure */
1509 old_info = queue->receive_info;
1510 queue->receive_info = &info;
1511 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1512 info.msg.lParam, (info.type != MSG_ASCII) );
1513 reply_message( &info, result, TRUE );
1514 queue->receive_info = old_info;
1516 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1521 /***********************************************************************
1522 * wait_message_reply
1524 * Wait until a sent message gets replied to.
1526 static void wait_message_reply( UINT flags )
1528 MESSAGEQUEUE *queue;
1530 if (!(queue = QUEUE_Current())) return;
1534 unsigned int wake_bits = 0, changed_bits = 0;
1537 SERVER_START_REQ( set_queue_mask )
1539 req->wake_mask = (flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE;
1540 req->changed_mask = QS_SMRESULT | req->wake_mask;
1542 if (!wine_server_call( req ))
1544 wake_bits = reply->wake_bits;
1545 changed_bits = reply->changed_bits;
1550 if (changed_bits & QS_SMRESULT) return; /* got a result */
1551 if (wake_bits & QS_SENDMESSAGE)
1553 /* Process the sent message immediately */
1555 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1559 /* now wait for it */
1561 ReleaseThunkLock( &dwlc );
1563 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1564 res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1567 res = WaitForSingleObject( queue->server_queue, INFINITE );
1569 if (dwlc) RestoreThunkLock( dwlc );
1573 /***********************************************************************
1574 * put_message_in_queue
1576 * Put a sent message into the destination queue.
1577 * For inter-process message, reply_size is set to expected size of reply data.
1579 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1580 size_t *reply_size )
1582 struct packed_message data;
1584 int i, timeout = -1;
1586 if (info->type != MSG_NOTIFY &&
1587 info->type != MSG_CALLBACK &&
1588 info->type != MSG_POSTED &&
1589 info->timeout != INFINITE)
1590 timeout = info->timeout;
1593 if (info->type == MSG_OTHER_PROCESS)
1595 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1596 if (data.count == -1)
1598 WARN( "cannot pack message %x\n", info->msg );
1602 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
1604 return post_dde_message( dest_tid, &data, info );
1607 SERVER_START_REQ( send_message )
1609 req->id = (void *)dest_tid;
1610 req->type = info->type;
1611 req->win = info->hwnd;
1612 req->msg = info->msg;
1613 req->wparam = info->wparam;
1614 req->lparam = info->lparam;
1615 req->time = GetCurrentTime();
1616 req->timeout = timeout;
1617 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1618 if ((res = wine_server_call( req )))
1620 if (res == STATUS_INVALID_PARAMETER)
1621 /* FIXME: find a STATUS_ value for this one */
1622 SetLastError( ERROR_INVALID_THREAD_ID );
1624 SetLastError( RtlNtStatusToDosError(res) );
1632 /***********************************************************************
1635 * Retrieve a message reply from the server.
1637 static LRESULT retrieve_reply( const struct send_message_info *info,
1638 size_t reply_size, LRESULT *result )
1641 void *reply_data = NULL;
1645 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
1647 WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
1651 SERVER_START_REQ( get_message_reply )
1654 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
1655 if (!(status = wine_server_call( req ))) *result = reply->result;
1656 reply_size = wine_server_reply_size( reply );
1659 if (!status && reply_size)
1660 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
1662 if (reply_data) HeapFree( GetProcessHeap(), 0, reply_data );
1664 TRACE( "hwnd %x msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1665 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
1666 info->lparam, *result, status );
1668 if (!status) return 1;
1669 if (status == STATUS_TIMEOUT) SetLastError(0); /* timeout */
1670 else SetLastError( RtlNtStatusToDosError(status) );
1675 /***********************************************************************
1676 * send_inter_thread_message
1678 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1683 size_t reply_size = 0;
1685 TRACE( "hwnd %x msg %x (%s) wp %x lp %lx\n",
1686 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
1688 if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1690 /* there's no reply to wait for on notify/callback messages */
1691 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1693 locks = WIN_SuspendWndsLock();
1695 wait_message_reply( info->flags );
1696 ret = retrieve_reply( info, reply_size, res_ptr );
1698 WIN_RestoreWndsLock( locks );
1703 /***********************************************************************
1704 * SendMessageTimeoutW (USER32.@)
1706 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1707 UINT flags, UINT timeout, LPDWORD res_ptr )
1709 struct send_message_info info;
1710 DWORD dest_tid, dest_pid;
1711 LRESULT ret, result;
1713 info.type = MSG_UNICODE;
1716 info.wparam = wparam;
1717 info.lparam = lparam;
1719 info.timeout = timeout;
1721 if (is_broadcast(hwnd))
1723 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1724 if (res_ptr) *res_ptr = 1;
1728 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1730 if (USER_IsExitingThread( dest_tid )) return 0;
1732 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1734 if (dest_tid == GetCurrentThreadId())
1736 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1741 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1742 ret = send_inter_thread_message( dest_tid, &info, &result );
1745 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1746 if (ret && res_ptr) *res_ptr = result;
1751 /***********************************************************************
1752 * SendMessageTimeoutA (USER32.@)
1754 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1755 UINT flags, UINT timeout, LPDWORD res_ptr )
1757 struct send_message_info info;
1758 DWORD dest_tid, dest_pid;
1759 LRESULT ret, result;
1761 info.type = MSG_ASCII;
1764 info.wparam = wparam;
1765 info.lparam = lparam;
1767 info.timeout = timeout;
1769 if (is_broadcast(hwnd))
1771 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1772 if (res_ptr) *res_ptr = 1;
1776 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1778 if (USER_IsExitingThread( dest_tid )) return 0;
1780 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1782 if (dest_tid == GetCurrentThreadId())
1784 result = call_window_proc( hwnd, msg, wparam, lparam, FALSE );
1787 else if (dest_pid == GetCurrentProcessId())
1789 ret = send_inter_thread_message( dest_tid, &info, &result );
1793 /* inter-process message: need to map to Unicode */
1794 info.type = MSG_OTHER_PROCESS;
1795 if (is_unicode_message( info.msg ))
1797 if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1799 ret = send_inter_thread_message( dest_tid, &info, &result );
1800 result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1801 info.lparam, result );
1803 else ret = send_inter_thread_message( dest_tid, &info, &result );
1805 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1806 if (ret && res_ptr) *res_ptr = result;
1811 /***********************************************************************
1812 * SendMessageW (USER32.@)
1814 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1817 SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1822 /***********************************************************************
1823 * SendMessageA (USER32.@)
1825 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1828 SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1833 /***********************************************************************
1834 * SendNotifyMessageA (USER32.@)
1836 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1838 return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1842 /***********************************************************************
1843 * SendNotifyMessageW (USER32.@)
1845 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1847 struct send_message_info info;
1851 if (is_pointer_message(msg))
1853 SetLastError(ERROR_INVALID_PARAMETER);
1857 info.type = MSG_NOTIFY;
1860 info.wparam = wparam;
1861 info.lparam = lparam;
1863 if (is_broadcast(hwnd))
1865 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1869 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1871 if (USER_IsExitingThread( dest_tid )) return TRUE;
1873 if (dest_tid == GetCurrentThreadId())
1875 call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1878 return send_inter_thread_message( dest_tid, &info, &result );
1882 /***********************************************************************
1883 * SendMessageCallbackA (USER32.@)
1885 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1886 SENDASYNCPROC callback, ULONG_PTR data )
1888 return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
1889 lparam, callback, data );
1893 /***********************************************************************
1894 * SendMessageCallbackW (USER32.@)
1896 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1897 SENDASYNCPROC callback, ULONG_PTR data )
1899 struct send_message_info info;
1903 if (is_pointer_message(msg))
1905 SetLastError(ERROR_INVALID_PARAMETER);
1909 info.type = MSG_CALLBACK;
1912 info.wparam = wparam;
1913 info.lparam = lparam;
1914 info.callback = callback;
1917 if (is_broadcast(hwnd))
1919 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1923 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1925 if (USER_IsExitingThread( dest_tid )) return TRUE;
1927 if (dest_tid == GetCurrentThreadId())
1929 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1930 callback( hwnd, msg, data, result );
1933 FIXME( "callback will not be called\n" );
1934 return send_inter_thread_message( dest_tid, &info, &result );
1938 /***********************************************************************
1939 * ReplyMessage (USER32.@)
1941 BOOL WINAPI ReplyMessage( LRESULT result )
1943 MESSAGEQUEUE *queue = QUEUE_Current();
1944 struct received_message_info *info = queue->receive_info;
1946 if (!info) return FALSE;
1947 reply_message( info, result, FALSE );
1952 /***********************************************************************
1953 * InSendMessage (USER32.@)
1955 BOOL WINAPI InSendMessage(void)
1957 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
1961 /***********************************************************************
1962 * InSendMessageEx (USER32.@)
1964 DWORD WINAPI InSendMessageEx( LPVOID reserved )
1966 MESSAGEQUEUE *queue = QUEUE_Current();
1967 struct received_message_info *info = queue->receive_info;
1969 if (info) return info->flags;
1970 return ISMEX_NOSEND;
1974 /***********************************************************************
1975 * PostMessageA (USER32.@)
1977 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1979 return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1983 /***********************************************************************
1984 * PostMessageW (USER32.@)
1986 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1988 struct send_message_info info;
1991 if (is_pointer_message( msg ))
1993 SetLastError( ERROR_INVALID_PARAMETER );
1997 info.type = MSG_POSTED;
2000 info.wparam = wparam;
2001 info.lparam = lparam;
2003 if (is_broadcast(hwnd))
2005 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2008 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2010 if (USER_IsExitingThread( dest_tid )) return TRUE;
2012 return put_message_in_queue( dest_tid, &info, NULL );
2016 /**********************************************************************
2017 * PostThreadMessageA (USER32.@)
2019 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2021 return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2025 /**********************************************************************
2026 * PostThreadMessageW (USER32.@)
2028 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2030 struct send_message_info info;
2032 if (is_pointer_message( msg ))
2034 SetLastError( ERROR_INVALID_PARAMETER );
2037 if (USER_IsExitingThread( thread )) return TRUE;
2039 info.type = MSG_POSTED;
2042 info.wparam = wparam;
2043 info.lparam = lparam;
2044 return put_message_in_queue( thread, &info, NULL );
2048 /***********************************************************************
2049 * PostQuitMessage (USER32.@)
2051 void WINAPI PostQuitMessage( INT exitCode )
2053 PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
2057 /***********************************************************************
2058 * PeekMessageW (USER32.@)
2060 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2062 MESSAGEQUEUE *queue;
2066 /* check for graphics events */
2067 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2068 USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
2070 hwnd = WIN_GetFullHandle( hwnd );
2071 locks = WIN_SuspendWndsLock();
2073 if (!MSG_peek_message( &msg, hwnd, first, last,
2074 (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2076 /* FIXME: should be done before checking for hw events */
2077 MSG_JournalPlayBackMsg();
2079 if (!(flags & PM_NOYIELD))
2082 ReleaseThunkLock(&count);
2083 if (count) RestoreThunkLock(count);
2085 WIN_RestoreWndsLock( locks );
2089 WIN_RestoreWndsLock( locks );
2091 /* need to fill the window handle for WM_PAINT message */
2092 if (msg.message == WM_PAINT)
2094 if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
2096 msg.message = WM_PAINTICON;
2099 /* clear internal paint flag */
2100 RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
2103 if ((queue = QUEUE_Current()))
2105 queue->GetMessageTimeVal = msg.time;
2106 msg.pt.x = LOWORD( queue->GetMessagePosVal );
2107 msg.pt.y = HIWORD( queue->GetMessagePosVal );
2110 HOOK_CallHooksW( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg );
2112 /* copy back our internal safe copy of message data to msg_out.
2113 * msg_out is a variable from the *program*, so it can't be used
2114 * internally as it can get "corrupted" by our use of SendMessage()
2115 * (back to the program) inside the message handling itself. */
2121 /***********************************************************************
2122 * PeekMessageA (USER32.@)
2124 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2126 BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2127 if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2132 /***********************************************************************
2133 * GetMessageW (USER32.@)
2135 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2137 MESSAGEQUEUE *queue = QUEUE_Current();
2140 mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2143 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2144 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2145 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2146 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2147 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2148 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2150 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2152 locks = WIN_SuspendWndsLock();
2154 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2156 /* wait until one of the bits is set */
2157 unsigned int wake_bits = 0, changed_bits = 0;
2160 SERVER_START_REQ( set_queue_mask )
2162 req->wake_mask = QS_SENDMESSAGE;
2163 req->changed_mask = mask;
2165 if (!wine_server_call( req ))
2167 wake_bits = reply->wake_bits;
2168 changed_bits = reply->changed_bits;
2173 if (changed_bits & mask) continue;
2174 if (wake_bits & QS_SENDMESSAGE) continue;
2176 TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2177 queue->self, mask, wake_bits, changed_bits );
2179 ReleaseThunkLock( &dwlc );
2180 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2181 USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
2183 WaitForSingleObject( queue->server_queue, INFINITE );
2184 if (dwlc) RestoreThunkLock( dwlc );
2187 WIN_RestoreWndsLock( locks );
2189 return (msg->message != WM_QUIT);
2193 /***********************************************************************
2194 * GetMessageA (USER32.@)
2196 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2198 GetMessageW( msg, hwnd, first, last );
2199 msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2200 return (msg->message != WM_QUIT);
2204 /***********************************************************************
2205 * SetMessageQueue (USER32.@)
2207 BOOL WINAPI SetMessageQueue( INT size )
2209 /* now obsolete the message queue will be expanded dynamically as necessary */