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
27 #include "wine/unicode.h"
28 #include "wine/server.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msg);
40 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
41 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
43 #define MAX_PACK_COUNT 4
45 /* description of the data fields that need to be packed along with a sent message */
49 const void *data[MAX_PACK_COUNT];
50 size_t size[MAX_PACK_COUNT];
53 /* info about the message currently being received by the current thread */
54 struct received_message_info
56 enum message_type type;
58 UINT flags; /* InSendMessageEx return flags */
61 /* structure to group all parameters for sent messages of the various kinds */
62 struct send_message_info
64 enum message_type type;
69 UINT flags; /* flags for SendMessageTimeout */
70 UINT timeout; /* timeout for SendMessageTimeout */
71 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
72 ULONG_PTR data; /* callback data */
76 /* flag for messages that contain pointers */
77 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
79 #define SET(msg) (1 << ((msg) & 31))
81 static const unsigned int message_pointer_flags[] =
84 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
85 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
87 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
90 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
91 SET(WM_NOTIFY) | SET(WM_HELP),
93 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
95 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
97 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
99 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
101 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO),
107 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
108 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
109 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
113 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
114 SET(LB_DIR) | SET(LB_FINDSTRING) |
115 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
117 SET(LB_FINDSTRINGEXACT),
123 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
125 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
126 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
140 SET(WM_ASKCBFORMATNAME)
143 /* flags for messages that contain Unicode strings */
144 static const unsigned int message_unicode_flags[] =
147 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
148 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
160 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
164 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
168 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
169 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
173 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
174 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
176 SET(LB_FINDSTRINGEXACT),
198 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
201 /* check whether a given message type includes pointers */
202 inline static int is_pointer_message( UINT message )
204 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
205 return (message_pointer_flags[message / 32] & SET(message)) != 0;
208 /* check whether a given message type contains Unicode (or ASCII) chars */
209 inline static int is_unicode_message( UINT message )
211 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
212 return (message_unicode_flags[message / 32] & SET(message)) != 0;
217 /* add a data field to a packed message */
218 inline static void push_data( struct packed_message *data, const void *ptr, size_t size )
220 data->data[data->count] = ptr;
221 data->size[data->count] = size;
225 /* add a string to a packed message */
226 inline static void push_string( struct packed_message *data, LPCWSTR str )
228 push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
231 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
232 inline static void *get_data( void **buffer, size_t size )
235 *buffer = (char *)*buffer + size;
239 /* make sure that the buffer contains a valid null-terminated Unicode string */
240 inline static BOOL check_string( LPCWSTR str, size_t size )
242 for (size /= sizeof(WCHAR); size; size--, str++)
243 if (!*str) return TRUE;
247 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
248 inline static void *get_buffer_space( void **buffer, size_t size )
251 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
252 HeapFree( GetProcessHeap(), 0, *buffer );
257 /* retrieve a string pointer from packed data */
258 inline static LPWSTR get_string( void **buffer )
260 return get_data( buffer, (strlenW( (LPWSTR)*buffer ) + 1) * sizeof(WCHAR) );
263 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
264 inline static BOOL combobox_has_strings( HWND hwnd )
266 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
267 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
270 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
271 inline static BOOL listbox_has_strings( HWND hwnd )
273 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
274 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
277 /* check if hwnd is a broadcast magic handle */
278 inline static BOOL is_broadcast( HWND hwnd )
280 return (hwnd == HWND_BROADCAST || hwnd == HWND_TOPMOST);
284 /***********************************************************************
285 * broadcast_message_callback
287 * Helper callback for broadcasting messages.
289 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
291 struct send_message_info *info = (struct send_message_info *)lparam;
292 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
296 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
297 info->flags, info->timeout, NULL );
300 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
301 info->flags, info->timeout, NULL );
304 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
307 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
308 info->callback, info->data );
311 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
314 ERR( "bad type %d\n", info->type );
321 /***********************************************************************
324 * Convert the wparam of an ASCII message to Unicode.
326 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
328 if (message == WM_CHARTOITEM ||
329 message == EM_SETPASSWORDCHAR ||
330 message == WM_CHAR ||
331 message == WM_DEADCHAR ||
332 message == WM_SYSCHAR ||
333 message == WM_SYSDEADCHAR ||
334 message == WM_MENUCHAR)
336 char ch = LOWORD(wparam);
338 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
339 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
345 /***********************************************************************
348 * Convert the wparam of a Unicode message to ASCII.
350 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
352 if (message == WM_CHARTOITEM ||
353 message == EM_SETPASSWORDCHAR ||
354 message == WM_CHAR ||
355 message == WM_DEADCHAR ||
356 message == WM_SYSCHAR ||
357 message == WM_SYSDEADCHAR ||
358 message == WM_MENUCHAR)
360 WCHAR wch = LOWORD(wparam);
362 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
363 wparam = MAKEWPARAM( (unsigned char)ch, HIWORD(wparam) );
369 /***********************************************************************
372 * Pack a message for sending to another process.
373 * Return the size of the data we expect in the message reply.
374 * Set data->count to -1 if there is an error.
376 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
377 struct packed_message *data )
385 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
386 push_data( data, cs, sizeof(*cs) );
387 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
388 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
392 case WM_ASKCBFORMATNAME:
393 return wparam * sizeof(WCHAR);
395 case WM_WININICHANGE:
396 case WM_DEVMODECHANGE:
401 push_string( data, (LPWSTR)lparam );
403 case WM_GETMINMAXINFO:
404 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
405 return sizeof(MINMAXINFO);
407 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
410 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
411 return sizeof(MEASUREITEMSTRUCT);
413 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
416 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
418 case WM_WINDOWPOSCHANGING:
419 case WM_WINDOWPOSCHANGED:
420 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
421 return sizeof(WINDOWPOS);
424 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
425 push_data( data, cp, sizeof(*cp) );
426 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
430 /* WM_NOTIFY cannot be sent across processes (MSDN) */
434 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
436 case WM_STYLECHANGING:
437 case WM_STYLECHANGED:
438 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
443 push_data( data, (RECT *)lparam, sizeof(RECT) );
448 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
449 push_data( data, nc, sizeof(*nc) );
450 push_data( data, nc->lppos, sizeof(*nc->lppos) );
451 return sizeof(*nc) + sizeof(*nc->lppos);
454 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
456 case SBM_SETSCROLLINFO:
457 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
459 case SBM_GETSCROLLINFO:
460 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
461 return sizeof(SCROLLINFO);
467 if (wparam) size += sizeof(DWORD);
468 if (lparam) size += sizeof(DWORD);
473 case CB_GETDROPPEDCONTROLRECT:
477 push_data( data, (RECT *)lparam, sizeof(RECT) );
481 WORD *pw = (WORD *)lparam;
482 push_data( data, pw, sizeof(*pw) );
483 return *pw * sizeof(WCHAR);
487 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
490 case CB_INSERTSTRING:
492 case CB_FINDSTRINGEXACT:
493 case CB_SELECTSTRING:
494 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
497 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
498 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
500 case LB_INSERTSTRING:
502 case LB_FINDSTRINGEXACT:
503 case LB_SELECTSTRING:
504 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
507 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
508 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
510 return wparam * sizeof(UINT);
512 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
513 return sizeof(MDINEXTMENU);
516 push_data( data, (RECT *)lparam, sizeof(RECT) );
520 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
521 push_data( data, cs, sizeof(*cs) );
522 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
523 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
526 case WM_MDIGETACTIVE:
527 if (lparam) return sizeof(BOOL);
529 case WM_WINE_SETWINDOWPOS:
530 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
533 /* these contain an HFONT */
536 /* these contain an HDC */
539 case WM_ICONERASEBKGND:
541 case WM_CTLCOLORMSGBOX:
542 case WM_CTLCOLOREDIT:
543 case WM_CTLCOLORLISTBOX:
546 case WM_CTLCOLORSCROLLBAR:
547 case WM_CTLCOLORSTATIC:
550 /* these contain an HGLOBAL */
551 case WM_PAINTCLIPBOARD:
552 case WM_SIZECLIPBOARD:
553 /* these contain pointers */
555 case WM_QUERYDROPOBJECT:
559 case WM_DEVICECHANGE:
560 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
568 /***********************************************************************
571 * Unpack a message received from another process.
573 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
574 void **buffer, size_t size )
583 CREATESTRUCTW *cs = *buffer;
584 WCHAR *str = (WCHAR *)(cs + 1);
585 if (size < sizeof(*cs)) return FALSE;
587 if (HIWORD(cs->lpszName))
589 if (!check_string( str, size )) return FALSE;
591 size -= (strlenW(str) + 1) * sizeof(WCHAR);
592 str += strlenW(str) + 1;
594 if (HIWORD(cs->lpszClass))
596 if (!check_string( str, size )) return FALSE;
602 case WM_ASKCBFORMATNAME:
603 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
606 case WM_WININICHANGE:
607 case WM_DEVMODECHANGE:
612 if (!check_string( *buffer, size )) return FALSE;
614 case WM_GETMINMAXINFO:
615 minsize = sizeof(MINMAXINFO);
618 minsize = sizeof(DRAWITEMSTRUCT);
621 minsize = sizeof(MEASUREITEMSTRUCT);
624 minsize = sizeof(DELETEITEMSTRUCT);
627 minsize = sizeof(COMPAREITEMSTRUCT);
629 case WM_WINDOWPOSCHANGING:
630 case WM_WINDOWPOSCHANGED:
631 case WM_WINE_SETWINDOWPOS:
632 minsize = sizeof(WINDOWPOS);
636 COPYDATASTRUCT *cp = *buffer;
637 if (size < sizeof(*cp)) return FALSE;
640 minsize = sizeof(*cp) + cp->cbData;
646 /* WM_NOTIFY cannot be sent across processes (MSDN) */
649 minsize = sizeof(HELPINFO);
651 case WM_STYLECHANGING:
652 case WM_STYLECHANGED:
653 minsize = sizeof(STYLESTRUCT);
656 if (!*wparam) minsize = sizeof(RECT);
659 NCCALCSIZE_PARAMS *nc = *buffer;
660 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
661 nc->lppos = (WINDOWPOS *)(nc + 1);
665 if (!*lparam) return TRUE;
666 minsize = sizeof(MSG);
668 case SBM_SETSCROLLINFO:
669 minsize = sizeof(SCROLLINFO);
671 case SBM_GETSCROLLINFO:
672 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
677 if (*wparam || *lparam)
679 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
680 if (*wparam) *wparam = (WPARAM)*buffer;
681 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
686 case CB_GETDROPPEDCONTROLRECT:
687 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
691 minsize = sizeof(RECT);
696 if (size < sizeof(WORD)) return FALSE;
697 len = *(WORD *)*buffer;
698 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
699 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
704 if (!*wparam) return TRUE;
705 minsize = *wparam * sizeof(UINT);
708 case CB_INSERTSTRING:
710 case CB_FINDSTRINGEXACT:
711 case CB_SELECTSTRING:
713 case LB_INSERTSTRING:
715 case LB_FINDSTRINGEXACT:
716 case LB_SELECTSTRING:
717 if (!*buffer) return TRUE;
718 if (!check_string( *buffer, size )) return FALSE;
722 size = sizeof(ULONG_PTR);
723 if (combobox_has_strings( hwnd ))
724 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
725 if (!get_buffer_space( buffer, size )) return FALSE;
730 size = sizeof(ULONG_PTR);
731 if (listbox_has_strings( hwnd ))
732 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
733 if (!get_buffer_space( buffer, size )) return FALSE;
737 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
740 minsize = sizeof(MDINEXTMENU);
741 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
745 minsize = sizeof(RECT);
746 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
750 MDICREATESTRUCTW *cs = *buffer;
751 WCHAR *str = (WCHAR *)(cs + 1);
752 if (size < sizeof(*cs)) return FALSE;
754 if (HIWORD(cs->szTitle))
756 if (!check_string( str, size )) return FALSE;
758 size -= (strlenW(str) + 1) * sizeof(WCHAR);
759 str += strlenW(str) + 1;
761 if (HIWORD(cs->szClass))
763 if (!check_string( str, size )) return FALSE;
768 case WM_MDIGETACTIVE:
769 if (!*lparam) return TRUE;
770 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
772 /* these contain an HFONT */
775 /* these contain an HDC */
778 case WM_ICONERASEBKGND:
780 case WM_CTLCOLORMSGBOX:
781 case WM_CTLCOLOREDIT:
782 case WM_CTLCOLORLISTBOX:
785 case WM_CTLCOLORSCROLLBAR:
786 case WM_CTLCOLORSTATIC:
789 /* these contain an HGLOBAL */
790 case WM_PAINTCLIPBOARD:
791 case WM_SIZECLIPBOARD:
792 /* these contain pointers */
794 case WM_QUERYDROPOBJECT:
798 case WM_DEVICECHANGE:
799 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
803 return TRUE; /* message doesn't need any unpacking */
806 /* default exit for most messages: check minsize and store buffer in lparam */
807 if (size < minsize) return FALSE;
808 *lparam = (LPARAM)*buffer;
813 /***********************************************************************
816 * Pack a reply to a message for sending to another process.
818 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
819 LRESULT res, struct packed_message *data )
826 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
831 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
833 case WM_GETMINMAXINFO:
834 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
837 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
839 case WM_WINDOWPOSCHANGING:
840 case WM_WINDOWPOSCHANGED:
841 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
844 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
846 case SBM_GETSCROLLINFO:
847 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
851 case CB_GETDROPPEDCONTROLRECT:
854 push_data( data, (RECT *)lparam, sizeof(RECT) );
858 WORD *ptr = (WORD *)lparam;
859 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
863 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
865 case WM_MDIGETACTIVE:
866 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
870 push_data( data, (RECT *)lparam, sizeof(RECT) );
873 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
874 push_data( data, nc, sizeof(*nc) );
875 push_data( data, nc->lppos, sizeof(*nc->lppos) );
881 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
882 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
885 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
888 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
890 case WM_ASKCBFORMATNAME:
891 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
897 /***********************************************************************
900 * Unpack a message reply received from another process.
902 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
903 void *buffer, size_t size )
910 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
911 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
912 memcpy( cs, buffer, min( sizeof(*cs), size ));
913 cs->lpszName = name; /* restore the original pointers */
914 cs->lpszClass = class;
918 case WM_ASKCBFORMATNAME:
919 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
921 case WM_GETMINMAXINFO:
922 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
925 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
927 case WM_WINDOWPOSCHANGING:
928 case WM_WINDOWPOSCHANGED:
929 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
932 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
934 case SBM_GETSCROLLINFO:
935 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
938 case CB_GETDROPPEDCONTROLRECT:
942 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
945 size = min( size, (size_t)*(WORD *)lparam );
946 memcpy( (WCHAR *)lparam, buffer, size );
949 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
953 memcpy( (WCHAR *)lparam, buffer, size );
956 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
958 case WM_MDIGETACTIVE:
959 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
963 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
966 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
967 WINDOWPOS *wp = nc->lppos;
968 memcpy( nc, buffer, min( sizeof(*nc), size ));
969 if (size > sizeof(*nc))
972 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
974 nc->lppos = wp; /* restore the original pointer */
982 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
983 if (size <= sizeof(DWORD)) break;
984 size -= sizeof(DWORD);
985 buffer = (DWORD *)buffer + 1;
987 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
991 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
992 LPCWSTR title = cs->szTitle, class = cs->szClass;
993 memcpy( cs, buffer, min( sizeof(*cs), size ));
994 cs->szTitle = title; /* restore the original pointers */
999 ERR( "should not happen: unexpected message %x\n", message );
1005 /***********************************************************************
1008 * Send a reply to a sent message.
1010 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1012 struct packed_message data;
1013 int i, replied = info->flags & ISMEX_REPLIED;
1015 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1016 if (!remove && replied) return; /* replied already */
1019 info->flags |= ISMEX_REPLIED;
1021 if (info->type == MSG_OTHER_PROCESS && !replied)
1023 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1024 info->msg.lParam, result, &data );
1027 SERVER_START_REQ( reply_message )
1029 req->result = result;
1030 req->remove = remove;
1031 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1032 wine_server_call( req );
1038 /***********************************************************************
1039 * handle_internal_message
1041 * Handle an internal Wine message instead of calling the window proc.
1043 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1045 if (hwnd == GetDesktopWindow()) return 0;
1048 case WM_WINE_DESTROYWINDOW:
1049 return WIN_DestroyWindow( hwnd );
1050 case WM_WINE_SETWINDOWPOS:
1051 return USER_Driver.pSetWindowPos( (WINDOWPOS *)lparam );
1052 case WM_WINE_SHOWWINDOW:
1053 return ShowWindow( hwnd, wparam );
1054 case WM_WINE_SETPARENT:
1055 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1056 case WM_WINE_SETWINDOWLONG:
1057 return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1058 case WM_WINE_ENABLEWINDOW:
1059 return EnableWindow( hwnd, wparam );
1061 FIXME( "unknown internal message %x\n", msg );
1066 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1067 * to the memory handle, we keep track (in the server side) of all pairs of handle
1068 * used (the client passes its value and the content of the memory handle), and
1069 * the server stored both values (the client, and the local one, created after the
1070 * content). When a ACK message is generated, the list of pair is searched for a
1071 * matching pair, so that the client memory handle can be returned.
1074 HGLOBAL client_hMem;
1075 HGLOBAL server_hMem;
1078 static struct DDE_pair* dde_pairs;
1079 static int dde_num_alloc;
1080 static int dde_num_used;
1081 static CRITICAL_SECTION dde_crst = CRITICAL_SECTION_INIT("Raw_DDE_CritSect");
1083 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1088 EnterCriticalSection(&dde_crst);
1090 /* now remember the pair of hMem on both sides */
1091 if (dde_num_used == dde_num_alloc)
1093 struct DDE_pair* tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1094 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1097 LeaveCriticalSection(&dde_crst);
1101 /* zero out newly allocated part */
1102 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1103 dde_num_alloc += GROWBY;
1106 for (i = 0; i < dde_num_alloc; i++)
1108 if (dde_pairs[i].server_hMem == 0)
1110 dde_pairs[i].client_hMem = chm;
1111 dde_pairs[i].server_hMem = shm;
1116 LeaveCriticalSection(&dde_crst);
1120 static HGLOBAL dde_get_pair(HGLOBAL shm)
1125 EnterCriticalSection(&dde_crst);
1126 for (i = 0; i < dde_num_alloc; i++)
1128 if (dde_pairs[i].server_hMem == shm)
1130 /* free this pair */
1131 dde_pairs[i].server_hMem = 0;
1133 ret = dde_pairs[i].client_hMem;
1137 LeaveCriticalSection(&dde_crst);
1141 /***********************************************************************
1146 static BOOL post_dde_message( DWORD dest_tid, struct packed_message *data, const struct send_message_info *info )
1152 HGLOBAL hunlock = 0;
1156 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1162 /* DDE messages which don't require packing are:
1171 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1172 HGLOBAL h = dde_get_pair( uiHi );
1175 /* send back the value of h on the other side */
1176 push_data( data, &h, sizeof(HGLOBAL) );
1178 TRACE( "send dde-ack %x %08x => %08lx\n", uiLo, uiHi, (DWORD)h );
1183 /* uiHi should contain either an atom or 0 */
1184 TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
1185 lp = MAKELONG( uiLo, uiHi );
1194 size = GlobalSize( (HGLOBAL)uiLo ) ;
1195 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1196 (info->msg == WM_DDE_DATA && size < sizeof(DDEDATA)) ||
1197 (info->msg == WM_DDE_POKE && size < sizeof(DDEPOKE))
1201 else if (info->msg != WM_DDE_DATA) return FALSE;
1206 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1208 push_data( data, ptr, size );
1209 hunlock = (HGLOBAL)uiLo;
1212 TRACE( "send ddepack %u %x\n", size, uiHi );
1214 case WM_DDE_EXECUTE:
1217 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1219 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1220 /* so that the other side can send it back on ACK */
1222 hunlock = (HGLOBAL)info->lparam;
1227 SERVER_START_REQ( send_message )
1229 req->id = (void *)dest_tid;
1230 req->type = info->type;
1231 req->win = info->hwnd;
1232 req->msg = info->msg;
1233 req->wparam = info->wparam;
1235 req->time = GetCurrentTime();
1237 for (i = 0; i < data->count; i++)
1238 wine_server_add_data( req, data->data[i], data->size[i] );
1239 if ((res = wine_server_call( req )))
1241 if (res == STATUS_INVALID_PARAMETER)
1242 /* FIXME: find a STATUS_ value for this one */
1243 SetLastError( ERROR_INVALID_THREAD_ID );
1245 SetLastError( RtlNtStatusToDosError(res) );
1248 FreeDDElParam(info->msg, info->lparam);
1251 if (hunlock) GlobalUnlock(hunlock);
1256 /***********************************************************************
1257 * unpack_dde_message
1259 * Unpack a posted DDE message received from another process.
1261 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1262 void **buffer, size_t size )
1273 /* hMem is being passed */
1274 if (size != sizeof(HGLOBAL)) return FALSE;
1275 if (!buffer || !*buffer) return FALSE;
1277 memcpy( &hMem, *buffer, size );
1279 TRACE("recv dde-ack %u mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( uiHi ));
1283 uiLo = LOWORD( *lparam );
1284 uiHi = HIWORD( *lparam );
1285 TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
1287 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1292 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1294 TRACE( "recv ddepack %u %x\n", size, uiHi );
1297 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1298 if (hMem && (ptr = GlobalLock( hMem )))
1300 memcpy( ptr, *buffer, size );
1301 GlobalUnlock( hMem );
1307 *lparam = PackDDElParam( message, uiLo, uiHi );
1309 case WM_DDE_EXECUTE:
1312 if (!buffer || !*buffer) return FALSE;
1313 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1314 if (hMem && (ptr = GlobalLock( hMem )))
1316 memcpy( ptr, *buffer, size );
1317 GlobalUnlock( hMem );
1318 TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
1319 if (!dde_add_pair( *lparam, hMem ))
1325 } else return FALSE;
1332 /***********************************************************************
1335 * Call a window procedure and the corresponding hooks.
1337 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam, BOOL unicode )
1342 if (msg & 0x80000000) return handle_internal_message( hwnd, msg, wparam, lparam );
1344 /* first the WH_CALLWNDPROC hook */
1345 if (HOOK_IsHooked( WH_CALLWNDPROC ))
1348 cwp.lParam = lparam;
1349 cwp.wParam = wparam;
1351 cwp.hwnd = WIN_GetFullHandle( hwnd );
1352 if (unicode) HOOK_CallHooksW( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1353 else HOOK_CallHooksA( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1354 lparam = cwp.lParam;
1355 wparam = cwp.wParam;
1360 /* now call the window procedure */
1363 if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) return 0;
1364 result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1368 if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) return 0;
1369 result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1372 /* and finally the WH_CALLWNDPROCRET hook */
1373 if (HOOK_IsHooked( WH_CALLWNDPROCRET ))
1376 cwp.lResult = result;
1377 cwp.lParam = lparam;
1378 cwp.wParam = wparam;
1380 cwp.hwnd = WIN_GetFullHandle( hwnd );
1381 if (unicode) HOOK_CallHooksW( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
1382 else HOOK_CallHooksA( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
1388 /***********************************************************************
1391 * Peek for a message matching the given parameters. Return FALSE if none available.
1392 * All pending sent messages are processed before returning.
1394 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1397 ULONG_PTR extra_info = 0;
1398 MESSAGEQUEUE *queue = QUEUE_Current();
1399 struct received_message_info info, *old_info;
1401 if (!first && !last) last = ~0;
1406 void *buffer = NULL;
1407 size_t size = 0, buffer_size = 0;
1409 do /* loop while buffer is too small */
1411 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1413 SERVER_START_REQ( get_message )
1416 req->get_win = hwnd;
1417 req->get_first = first;
1418 req->get_last = last;
1419 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1420 if (!(res = wine_server_call( req )))
1422 size = wine_server_reply_size( reply );
1423 info.type = reply->type;
1424 info.msg.hwnd = reply->win;
1425 info.msg.message = reply->msg;
1426 info.msg.wParam = reply->wparam;
1427 info.msg.lParam = reply->lparam;
1428 info.msg.time = reply->time;
1429 info.msg.pt.x = reply->x;
1430 info.msg.pt.y = reply->y;
1431 extra_info = reply->info;
1435 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1436 buffer_size = reply->total;
1440 } while (res == STATUS_BUFFER_OVERFLOW);
1442 if (res) return FALSE;
1444 TRACE( "got type %d msg %x hwnd %x wp %x lp %lx\n",
1445 info.type, info.msg.message, info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1451 info.flags = ISMEX_SEND;
1454 info.flags = ISMEX_NOTIFY;
1457 info.flags = ISMEX_CALLBACK;
1459 case MSG_OTHER_PROCESS:
1460 info.flags = ISMEX_SEND;
1461 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1462 &info.msg.lParam, &buffer, size ))
1464 ERR( "invalid packed message %x (%s) hwnd %x wp %x lp %lx size %d\n",
1465 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
1466 info.msg.wParam, info.msg.lParam, size );
1468 reply_message( &info, 0, TRUE );
1472 case MSG_HARDWARE_RAW:
1473 if (!MSG_process_raw_hardware_message( &info.msg, extra_info,
1474 hwnd, first, last, flags & GET_MSG_REMOVE ))
1477 case MSG_HARDWARE_COOKED:
1478 if (!MSG_process_cooked_hardware_message( &info.msg, extra_info,
1479 flags & GET_MSG_REMOVE ))
1481 flags |= GET_MSG_REMOVE_LAST;
1484 queue->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
1487 queue->GetMessageExtraInfoVal = extra_info;
1488 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
1490 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1491 &info.msg.lParam, &buffer, size ))
1493 ERR( "invalid packed dde-message %x (%s) hwnd %x wp %x lp %lx size %d\n",
1494 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1495 info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
1501 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1505 /* if we get here, we have a sent message; call the window procedure */
1506 old_info = queue->receive_info;
1507 queue->receive_info = &info;
1508 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1509 info.msg.lParam, (info.type != MSG_ASCII) );
1510 reply_message( &info, result, TRUE );
1511 queue->receive_info = old_info;
1513 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1518 /***********************************************************************
1519 * wait_message_reply
1521 * Wait until a sent message gets replied to.
1523 static void wait_message_reply( UINT flags )
1525 MESSAGEQUEUE *queue;
1527 if (!(queue = QUEUE_Current())) return;
1531 unsigned int wake_bits = 0, changed_bits = 0;
1534 SERVER_START_REQ( set_queue_mask )
1536 req->wake_mask = (flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE;
1537 req->changed_mask = QS_SMRESULT | req->wake_mask;
1539 if (!wine_server_call( req ))
1541 wake_bits = reply->wake_bits;
1542 changed_bits = reply->changed_bits;
1547 if (changed_bits & QS_SMRESULT) return; /* got a result */
1548 if (wake_bits & QS_SENDMESSAGE)
1550 /* Process the sent message immediately */
1552 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1556 /* now wait for it */
1558 ReleaseThunkLock( &dwlc );
1560 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1561 res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1564 res = WaitForSingleObject( queue->server_queue, INFINITE );
1566 if (dwlc) RestoreThunkLock( dwlc );
1570 /***********************************************************************
1571 * put_message_in_queue
1573 * Put a sent message into the destination queue.
1574 * For inter-process message, reply_size is set to expected size of reply data.
1576 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1577 size_t *reply_size )
1579 struct packed_message data;
1581 int i, timeout = -1;
1583 if (info->type != MSG_NOTIFY &&
1584 info->type != MSG_CALLBACK &&
1585 info->type != MSG_POSTED &&
1586 info->timeout != INFINITE)
1587 timeout = info->timeout;
1590 if (info->type == MSG_OTHER_PROCESS)
1592 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1593 if (data.count == -1)
1595 WARN( "cannot pack message %x\n", info->msg );
1599 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
1601 return post_dde_message( dest_tid, &data, info );
1604 SERVER_START_REQ( send_message )
1606 req->id = (void *)dest_tid;
1607 req->type = info->type;
1608 req->win = info->hwnd;
1609 req->msg = info->msg;
1610 req->wparam = info->wparam;
1611 req->lparam = info->lparam;
1612 req->time = GetCurrentTime();
1613 req->timeout = timeout;
1614 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1615 if ((res = wine_server_call( req )))
1617 if (res == STATUS_INVALID_PARAMETER)
1618 /* FIXME: find a STATUS_ value for this one */
1619 SetLastError( ERROR_INVALID_THREAD_ID );
1621 SetLastError( RtlNtStatusToDosError(res) );
1629 /***********************************************************************
1632 * Retrieve a message reply from the server.
1634 static LRESULT retrieve_reply( const struct send_message_info *info,
1635 size_t reply_size, LRESULT *result )
1638 void *reply_data = NULL;
1642 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
1644 WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
1648 SERVER_START_REQ( get_message_reply )
1651 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
1652 if (!(status = wine_server_call( req ))) *result = reply->result;
1653 reply_size = wine_server_reply_size( reply );
1656 if (!status && reply_size)
1657 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
1659 if (reply_data) HeapFree( GetProcessHeap(), 0, reply_data );
1661 TRACE( "hwnd %x msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1662 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
1663 info->lparam, *result, status );
1665 if (!status) return 1;
1666 if (status == STATUS_TIMEOUT) SetLastError(0); /* timeout */
1667 else SetLastError( RtlNtStatusToDosError(status) );
1672 /***********************************************************************
1673 * send_inter_thread_message
1675 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1680 size_t reply_size = 0;
1682 TRACE( "hwnd %x msg %x (%s) wp %x lp %lx\n",
1683 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
1685 if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1687 /* there's no reply to wait for on notify/callback messages */
1688 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1690 locks = WIN_SuspendWndsLock();
1692 wait_message_reply( info->flags );
1693 ret = retrieve_reply( info, reply_size, res_ptr );
1695 WIN_RestoreWndsLock( locks );
1700 /***********************************************************************
1701 * SendMessageTimeoutW (USER32.@)
1703 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1704 UINT flags, UINT timeout, LPDWORD res_ptr )
1706 struct send_message_info info;
1707 DWORD dest_tid, dest_pid;
1708 LRESULT ret, result;
1710 info.type = MSG_UNICODE;
1713 info.wparam = wparam;
1714 info.lparam = lparam;
1716 info.timeout = timeout;
1718 if (is_broadcast(hwnd))
1720 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1721 if (res_ptr) *res_ptr = 1;
1725 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1727 if (USER_IsExitingThread( dest_tid )) return 0;
1729 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1731 if (dest_tid == GetCurrentThreadId())
1733 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1738 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1739 ret = send_inter_thread_message( dest_tid, &info, &result );
1742 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1743 if (ret && res_ptr) *res_ptr = result;
1748 /***********************************************************************
1749 * SendMessageTimeoutA (USER32.@)
1751 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1752 UINT flags, UINT timeout, LPDWORD res_ptr )
1754 struct send_message_info info;
1755 DWORD dest_tid, dest_pid;
1756 LRESULT ret, result;
1758 info.type = MSG_ASCII;
1761 info.wparam = wparam;
1762 info.lparam = lparam;
1764 info.timeout = timeout;
1766 if (is_broadcast(hwnd))
1768 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1769 if (res_ptr) *res_ptr = 1;
1773 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1775 if (USER_IsExitingThread( dest_tid )) return 0;
1777 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1779 if (dest_tid == GetCurrentThreadId())
1781 result = call_window_proc( hwnd, msg, wparam, lparam, FALSE );
1784 else if (dest_pid == GetCurrentProcessId())
1786 ret = send_inter_thread_message( dest_tid, &info, &result );
1790 /* inter-process message: need to map to Unicode */
1791 info.type = MSG_OTHER_PROCESS;
1792 if (is_unicode_message( info.msg ))
1794 if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1796 ret = send_inter_thread_message( dest_tid, &info, &result );
1797 result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1798 info.lparam, result );
1800 else ret = send_inter_thread_message( dest_tid, &info, &result );
1802 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1803 if (ret && res_ptr) *res_ptr = result;
1808 /***********************************************************************
1809 * SendMessageW (USER32.@)
1811 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1814 SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1819 /***********************************************************************
1820 * SendMessageA (USER32.@)
1822 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1825 SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1830 /***********************************************************************
1831 * SendNotifyMessageA (USER32.@)
1833 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1835 return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1839 /***********************************************************************
1840 * SendNotifyMessageW (USER32.@)
1842 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1844 struct send_message_info info;
1848 if (is_pointer_message(msg))
1850 SetLastError(ERROR_INVALID_PARAMETER);
1854 info.type = MSG_NOTIFY;
1857 info.wparam = wparam;
1858 info.lparam = lparam;
1860 if (is_broadcast(hwnd))
1862 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1866 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1868 if (USER_IsExitingThread( dest_tid )) return TRUE;
1870 if (dest_tid == GetCurrentThreadId())
1872 call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1875 return send_inter_thread_message( dest_tid, &info, &result );
1879 /***********************************************************************
1880 * SendMessageCallbackA (USER32.@)
1882 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1883 SENDASYNCPROC callback, ULONG_PTR data )
1885 return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
1886 lparam, callback, data );
1890 /***********************************************************************
1891 * SendMessageCallbackW (USER32.@)
1893 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1894 SENDASYNCPROC callback, ULONG_PTR data )
1896 struct send_message_info info;
1900 if (is_pointer_message(msg))
1902 SetLastError(ERROR_INVALID_PARAMETER);
1906 info.type = MSG_CALLBACK;
1909 info.wparam = wparam;
1910 info.lparam = lparam;
1911 info.callback = callback;
1914 if (is_broadcast(hwnd))
1916 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1920 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1922 if (USER_IsExitingThread( dest_tid )) return TRUE;
1924 if (dest_tid == GetCurrentThreadId())
1926 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1927 callback( hwnd, msg, data, result );
1930 FIXME( "callback will not be called\n" );
1931 return send_inter_thread_message( dest_tid, &info, &result );
1935 /***********************************************************************
1936 * ReplyMessage (USER32.@)
1938 BOOL WINAPI ReplyMessage( LRESULT result )
1940 MESSAGEQUEUE *queue = QUEUE_Current();
1941 struct received_message_info *info = queue->receive_info;
1943 if (!info) return FALSE;
1944 reply_message( info, result, FALSE );
1949 /***********************************************************************
1950 * InSendMessage (USER32.@)
1952 BOOL WINAPI InSendMessage(void)
1954 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
1958 /***********************************************************************
1959 * InSendMessageEx (USER32.@)
1961 DWORD WINAPI InSendMessageEx( LPVOID reserved )
1963 MESSAGEQUEUE *queue = QUEUE_Current();
1964 struct received_message_info *info = queue->receive_info;
1966 if (info) return info->flags;
1967 return ISMEX_NOSEND;
1971 /***********************************************************************
1972 * PostMessageA (USER32.@)
1974 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1976 return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1980 /***********************************************************************
1981 * PostMessageW (USER32.@)
1983 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1985 struct send_message_info info;
1988 if (is_pointer_message( msg ))
1990 SetLastError( ERROR_INVALID_PARAMETER );
1994 info.type = MSG_POSTED;
1997 info.wparam = wparam;
1998 info.lparam = lparam;
2000 if (is_broadcast(hwnd))
2002 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2005 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2007 if (USER_IsExitingThread( dest_tid )) return TRUE;
2009 return put_message_in_queue( dest_tid, &info, NULL );
2013 /**********************************************************************
2014 * PostThreadMessageA (USER32.@)
2016 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2018 return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2022 /**********************************************************************
2023 * PostThreadMessageW (USER32.@)
2025 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2027 struct send_message_info info;
2029 if (is_pointer_message( msg ))
2031 SetLastError( ERROR_INVALID_PARAMETER );
2034 if (USER_IsExitingThread( thread )) return TRUE;
2036 info.type = MSG_POSTED;
2039 info.wparam = wparam;
2040 info.lparam = lparam;
2041 return put_message_in_queue( thread, &info, NULL );
2045 /***********************************************************************
2046 * PostQuitMessage (USER32.@)
2048 void WINAPI PostQuitMessage( INT exitCode )
2050 PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
2054 /***********************************************************************
2055 * PeekMessageW (USER32.@)
2057 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2059 MESSAGEQUEUE *queue;
2063 /* check for graphics events */
2064 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2065 USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
2067 hwnd = WIN_GetFullHandle( hwnd );
2068 locks = WIN_SuspendWndsLock();
2070 if (!MSG_peek_message( &msg, hwnd, first, last,
2071 (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2073 /* FIXME: should be done before checking for hw events */
2074 MSG_JournalPlayBackMsg();
2076 if (!(flags & PM_NOYIELD))
2079 ReleaseThunkLock(&count);
2080 if (count) RestoreThunkLock(count);
2082 WIN_RestoreWndsLock( locks );
2086 WIN_RestoreWndsLock( locks );
2088 /* need to fill the window handle for WM_PAINT message */
2089 if (msg.message == WM_PAINT)
2091 if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
2093 msg.message = WM_PAINTICON;
2096 /* clear internal paint flag */
2097 RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
2100 if ((queue = QUEUE_Current()))
2102 queue->GetMessageTimeVal = msg.time;
2103 msg.pt.x = LOWORD( queue->GetMessagePosVal );
2104 msg.pt.y = HIWORD( queue->GetMessagePosVal );
2107 HOOK_CallHooksW( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg );
2109 /* copy back our internal safe copy of message data to msg_out.
2110 * msg_out is a variable from the *program*, so it can't be used
2111 * internally as it can get "corrupted" by our use of SendMessage()
2112 * (back to the program) inside the message handling itself. */
2118 /***********************************************************************
2119 * PeekMessageA (USER32.@)
2121 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2123 BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2124 if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2129 /***********************************************************************
2130 * GetMessageW (USER32.@)
2132 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2134 MESSAGEQUEUE *queue = QUEUE_Current();
2137 mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2140 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2141 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2142 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2143 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2144 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2145 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2147 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2149 locks = WIN_SuspendWndsLock();
2151 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2153 /* wait until one of the bits is set */
2154 unsigned int wake_bits = 0, changed_bits = 0;
2157 SERVER_START_REQ( set_queue_mask )
2159 req->wake_mask = QS_SENDMESSAGE;
2160 req->changed_mask = mask;
2162 if (!wine_server_call( req ))
2164 wake_bits = reply->wake_bits;
2165 changed_bits = reply->changed_bits;
2170 if (changed_bits & mask) continue;
2171 if (wake_bits & QS_SENDMESSAGE) continue;
2173 TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2174 queue->self, mask, wake_bits, changed_bits );
2176 ReleaseThunkLock( &dwlc );
2177 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2178 USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
2180 WaitForSingleObject( queue->server_queue, INFINITE );
2181 if (dwlc) RestoreThunkLock( dwlc );
2184 WIN_RestoreWndsLock( locks );
2186 return (msg->message != WM_QUIT);
2190 /***********************************************************************
2191 * GetMessageA (USER32.@)
2193 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2195 GetMessageW( msg, hwnd, first, last );
2196 msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2197 return (msg->message != WM_QUIT);
2201 /***********************************************************************
2202 * SetMessageQueue (USER32.@)
2204 BOOL WINAPI SetMessageQueue( INT size )
2206 /* now obsolete the message queue will be expanded dynamically as necessary */