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"
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 )
331 case EM_SETPASSWORDCHAR:
338 char ch = LOWORD(wparam);
340 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
341 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
348 ch[0] = (wparam >> 8);
349 ch[1] = (wparam & 0xff);
350 MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
351 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
359 /***********************************************************************
362 * Convert the wparam of a Unicode message to ASCII.
364 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
369 case EM_SETPASSWORDCHAR:
376 WCHAR wch = LOWORD(wparam);
378 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
379 wparam = MAKEWPARAM( ch, HIWORD(wparam) );
384 WCHAR wch = LOWORD(wparam);
388 WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL );
389 wparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wparam) );
397 /***********************************************************************
400 * Pack a message for sending to another process.
401 * Return the size of the data we expect in the message reply.
402 * Set data->count to -1 if there is an error.
404 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
405 struct packed_message *data )
413 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
414 push_data( data, cs, sizeof(*cs) );
415 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
416 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
420 case WM_ASKCBFORMATNAME:
421 return wparam * sizeof(WCHAR);
422 case WM_WININICHANGE:
423 if (lparam) push_string(data, (LPWSTR)lparam );
426 case WM_DEVMODECHANGE:
431 push_string( data, (LPWSTR)lparam );
433 case WM_GETMINMAXINFO:
434 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
435 return sizeof(MINMAXINFO);
437 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
440 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
441 return sizeof(MEASUREITEMSTRUCT);
443 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
446 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
448 case WM_WINDOWPOSCHANGING:
449 case WM_WINDOWPOSCHANGED:
450 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
451 return sizeof(WINDOWPOS);
454 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
455 push_data( data, cp, sizeof(*cp) );
456 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
460 /* WM_NOTIFY cannot be sent across processes (MSDN) */
464 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
466 case WM_STYLECHANGING:
467 case WM_STYLECHANGED:
468 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
473 push_data( data, (RECT *)lparam, sizeof(RECT) );
478 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
479 push_data( data, nc, sizeof(*nc) );
480 push_data( data, nc->lppos, sizeof(*nc->lppos) );
481 return sizeof(*nc) + sizeof(*nc->lppos);
484 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
486 case SBM_SETSCROLLINFO:
487 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
489 case SBM_GETSCROLLINFO:
490 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
491 return sizeof(SCROLLINFO);
497 if (wparam) size += sizeof(DWORD);
498 if (lparam) size += sizeof(DWORD);
503 case CB_GETDROPPEDCONTROLRECT:
507 push_data( data, (RECT *)lparam, sizeof(RECT) );
511 WORD *pw = (WORD *)lparam;
512 push_data( data, pw, sizeof(*pw) );
513 return *pw * sizeof(WCHAR);
517 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
520 case CB_INSERTSTRING:
522 case CB_FINDSTRINGEXACT:
523 case CB_SELECTSTRING:
524 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
527 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
528 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
530 case LB_INSERTSTRING:
532 case LB_FINDSTRINGEXACT:
533 case LB_SELECTSTRING:
534 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
537 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
538 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
540 return wparam * sizeof(UINT);
542 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
543 return sizeof(MDINEXTMENU);
546 push_data( data, (RECT *)lparam, sizeof(RECT) );
550 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
551 push_data( data, cs, sizeof(*cs) );
552 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
553 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
556 case WM_MDIGETACTIVE:
557 if (lparam) return sizeof(BOOL);
559 case WM_WINE_SETWINDOWPOS:
560 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
563 /* these contain an HFONT */
566 /* these contain an HDC */
569 case WM_ICONERASEBKGND:
571 case WM_CTLCOLORMSGBOX:
572 case WM_CTLCOLOREDIT:
573 case WM_CTLCOLORLISTBOX:
576 case WM_CTLCOLORSCROLLBAR:
577 case WM_CTLCOLORSTATIC:
580 /* these contain an HGLOBAL */
581 case WM_PAINTCLIPBOARD:
582 case WM_SIZECLIPBOARD:
583 /* these contain pointers */
585 case WM_QUERYDROPOBJECT:
589 case WM_DEVICECHANGE:
590 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
598 /***********************************************************************
601 * Unpack a message received from another process.
603 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
604 void **buffer, size_t size )
613 CREATESTRUCTW *cs = *buffer;
614 WCHAR *str = (WCHAR *)(cs + 1);
615 if (size < sizeof(*cs)) return FALSE;
617 if (HIWORD(cs->lpszName))
619 if (!check_string( str, size )) return FALSE;
621 size -= (strlenW(str) + 1) * sizeof(WCHAR);
622 str += strlenW(str) + 1;
624 if (HIWORD(cs->lpszClass))
626 if (!check_string( str, size )) return FALSE;
632 case WM_ASKCBFORMATNAME:
633 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
635 case WM_WININICHANGE:
636 if (!*lparam) return TRUE;
639 case WM_DEVMODECHANGE:
644 if (!check_string( *buffer, size )) return FALSE;
646 case WM_GETMINMAXINFO:
647 minsize = sizeof(MINMAXINFO);
650 minsize = sizeof(DRAWITEMSTRUCT);
653 minsize = sizeof(MEASUREITEMSTRUCT);
656 minsize = sizeof(DELETEITEMSTRUCT);
659 minsize = sizeof(COMPAREITEMSTRUCT);
661 case WM_WINDOWPOSCHANGING:
662 case WM_WINDOWPOSCHANGED:
663 case WM_WINE_SETWINDOWPOS:
664 minsize = sizeof(WINDOWPOS);
668 COPYDATASTRUCT *cp = *buffer;
669 if (size < sizeof(*cp)) return FALSE;
672 minsize = sizeof(*cp) + cp->cbData;
678 /* WM_NOTIFY cannot be sent across processes (MSDN) */
681 minsize = sizeof(HELPINFO);
683 case WM_STYLECHANGING:
684 case WM_STYLECHANGED:
685 minsize = sizeof(STYLESTRUCT);
688 if (!*wparam) minsize = sizeof(RECT);
691 NCCALCSIZE_PARAMS *nc = *buffer;
692 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
693 nc->lppos = (WINDOWPOS *)(nc + 1);
697 if (!*lparam) return TRUE;
698 minsize = sizeof(MSG);
700 case SBM_SETSCROLLINFO:
701 minsize = sizeof(SCROLLINFO);
703 case SBM_GETSCROLLINFO:
704 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
709 if (*wparam || *lparam)
711 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
712 if (*wparam) *wparam = (WPARAM)*buffer;
713 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
718 case CB_GETDROPPEDCONTROLRECT:
719 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
723 minsize = sizeof(RECT);
728 if (size < sizeof(WORD)) return FALSE;
729 len = *(WORD *)*buffer;
730 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
731 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
736 if (!*wparam) return TRUE;
737 minsize = *wparam * sizeof(UINT);
740 case CB_INSERTSTRING:
742 case CB_FINDSTRINGEXACT:
743 case CB_SELECTSTRING:
745 case LB_INSERTSTRING:
747 case LB_FINDSTRINGEXACT:
748 case LB_SELECTSTRING:
749 if (!*buffer) return TRUE;
750 if (!check_string( *buffer, size )) return FALSE;
754 size = sizeof(ULONG_PTR);
755 if (combobox_has_strings( hwnd ))
756 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
757 if (!get_buffer_space( buffer, size )) return FALSE;
762 size = sizeof(ULONG_PTR);
763 if (listbox_has_strings( hwnd ))
764 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
765 if (!get_buffer_space( buffer, size )) return FALSE;
769 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
772 minsize = sizeof(MDINEXTMENU);
773 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
777 minsize = sizeof(RECT);
778 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
782 MDICREATESTRUCTW *cs = *buffer;
783 WCHAR *str = (WCHAR *)(cs + 1);
784 if (size < sizeof(*cs)) return FALSE;
786 if (HIWORD(cs->szTitle))
788 if (!check_string( str, size )) return FALSE;
790 size -= (strlenW(str) + 1) * sizeof(WCHAR);
791 str += strlenW(str) + 1;
793 if (HIWORD(cs->szClass))
795 if (!check_string( str, size )) return FALSE;
800 case WM_MDIGETACTIVE:
801 if (!*lparam) return TRUE;
802 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
804 /* these contain an HFONT */
807 /* these contain an HDC */
810 case WM_ICONERASEBKGND:
812 case WM_CTLCOLORMSGBOX:
813 case WM_CTLCOLOREDIT:
814 case WM_CTLCOLORLISTBOX:
817 case WM_CTLCOLORSCROLLBAR:
818 case WM_CTLCOLORSTATIC:
821 /* these contain an HGLOBAL */
822 case WM_PAINTCLIPBOARD:
823 case WM_SIZECLIPBOARD:
824 /* these contain pointers */
826 case WM_QUERYDROPOBJECT:
830 case WM_DEVICECHANGE:
831 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
835 return TRUE; /* message doesn't need any unpacking */
838 /* default exit for most messages: check minsize and store buffer in lparam */
839 if (size < minsize) return FALSE;
840 *lparam = (LPARAM)*buffer;
845 /***********************************************************************
848 * Pack a reply to a message for sending to another process.
850 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
851 LRESULT res, struct packed_message *data )
858 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
863 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
865 case WM_GETMINMAXINFO:
866 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
869 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
871 case WM_WINDOWPOSCHANGING:
872 case WM_WINDOWPOSCHANGED:
873 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
876 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
878 case SBM_GETSCROLLINFO:
879 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
883 case CB_GETDROPPEDCONTROLRECT:
886 push_data( data, (RECT *)lparam, sizeof(RECT) );
890 WORD *ptr = (WORD *)lparam;
891 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
895 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
897 case WM_MDIGETACTIVE:
898 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
902 push_data( data, (RECT *)lparam, sizeof(RECT) );
905 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
906 push_data( data, nc, sizeof(*nc) );
907 push_data( data, nc->lppos, sizeof(*nc->lppos) );
913 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
914 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
917 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
920 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
922 case WM_ASKCBFORMATNAME:
923 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
929 /***********************************************************************
932 * Unpack a message reply received from another process.
934 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
935 void *buffer, size_t size )
942 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
943 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
944 memcpy( cs, buffer, min( sizeof(*cs), size ));
945 cs->lpszName = name; /* restore the original pointers */
946 cs->lpszClass = class;
950 case WM_ASKCBFORMATNAME:
951 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
953 case WM_GETMINMAXINFO:
954 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
957 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
959 case WM_WINDOWPOSCHANGING:
960 case WM_WINDOWPOSCHANGED:
961 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
964 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
966 case SBM_GETSCROLLINFO:
967 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
970 case CB_GETDROPPEDCONTROLRECT:
974 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
977 size = min( size, (size_t)*(WORD *)lparam );
978 memcpy( (WCHAR *)lparam, buffer, size );
981 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
985 memcpy( (WCHAR *)lparam, buffer, size );
988 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
990 case WM_MDIGETACTIVE:
991 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
995 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
998 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
999 WINDOWPOS *wp = nc->lppos;
1000 memcpy( nc, buffer, min( sizeof(*nc), size ));
1001 if (size > sizeof(*nc))
1003 size -= sizeof(*nc);
1004 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
1006 nc->lppos = wp; /* restore the original pointer */
1014 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1015 if (size <= sizeof(DWORD)) break;
1016 size -= sizeof(DWORD);
1017 buffer = (DWORD *)buffer + 1;
1019 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1023 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
1024 LPCWSTR title = cs->szTitle, class = cs->szClass;
1025 memcpy( cs, buffer, min( sizeof(*cs), size ));
1026 cs->szTitle = title; /* restore the original pointers */
1027 cs->szClass = class;
1031 ERR( "should not happen: unexpected message %x\n", message );
1037 /***********************************************************************
1040 * Send a reply to a sent message.
1042 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1044 struct packed_message data;
1045 int i, replied = info->flags & ISMEX_REPLIED;
1047 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1048 if (!remove && replied) return; /* replied already */
1051 info->flags |= ISMEX_REPLIED;
1053 if (info->type == MSG_OTHER_PROCESS && !replied)
1055 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1056 info->msg.lParam, result, &data );
1059 SERVER_START_REQ( reply_message )
1061 req->type = info->type;
1062 req->result = result;
1063 req->remove = remove;
1064 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1065 wine_server_call( req );
1071 /***********************************************************************
1072 * handle_internal_message
1074 * Handle an internal Wine message instead of calling the window proc.
1076 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1078 if (hwnd == GetDesktopWindow()) return 0;
1081 case WM_WINE_DESTROYWINDOW:
1082 return WIN_DestroyWindow( hwnd );
1083 case WM_WINE_SETWINDOWPOS:
1084 return USER_Driver.pSetWindowPos( (WINDOWPOS *)lparam );
1085 case WM_WINE_SHOWWINDOW:
1086 return ShowWindow( hwnd, wparam );
1087 case WM_WINE_SETPARENT:
1088 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1089 case WM_WINE_SETWINDOWLONG:
1090 return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1091 case WM_WINE_ENABLEWINDOW:
1092 return EnableWindow( hwnd, wparam );
1093 case WM_WINE_SETACTIVEWINDOW:
1094 return (LRESULT)SetActiveWindow( (HWND)wparam );
1096 FIXME( "unknown internal message %x\n", msg );
1101 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1102 * to the memory handle, we keep track (in the server side) of all pairs of handle
1103 * used (the client passes its value and the content of the memory handle), and
1104 * the server stored both values (the client, and the local one, created after the
1105 * content). When a ACK message is generated, the list of pair is searched for a
1106 * matching pair, so that the client memory handle can be returned.
1109 HGLOBAL client_hMem;
1110 HGLOBAL server_hMem;
1113 static struct DDE_pair* dde_pairs;
1114 static int dde_num_alloc;
1115 static int dde_num_used;
1116 static CRITICAL_SECTION dde_crst = CRITICAL_SECTION_INIT("Raw_DDE_CritSect");
1118 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1123 EnterCriticalSection(&dde_crst);
1125 /* now remember the pair of hMem on both sides */
1126 if (dde_num_used == dde_num_alloc)
1128 struct DDE_pair* tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1129 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1132 LeaveCriticalSection(&dde_crst);
1136 /* zero out newly allocated part */
1137 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1138 dde_num_alloc += GROWBY;
1141 for (i = 0; i < dde_num_alloc; i++)
1143 if (dde_pairs[i].server_hMem == 0)
1145 dde_pairs[i].client_hMem = chm;
1146 dde_pairs[i].server_hMem = shm;
1151 LeaveCriticalSection(&dde_crst);
1155 static HGLOBAL dde_get_pair(HGLOBAL shm)
1160 EnterCriticalSection(&dde_crst);
1161 for (i = 0; i < dde_num_alloc; i++)
1163 if (dde_pairs[i].server_hMem == shm)
1165 /* free this pair */
1166 dde_pairs[i].server_hMem = 0;
1168 ret = dde_pairs[i].client_hMem;
1172 LeaveCriticalSection(&dde_crst);
1176 /***********************************************************************
1181 static BOOL post_dde_message( DWORD dest_tid, struct packed_message *data, const struct send_message_info *info )
1187 HGLOBAL hunlock = 0;
1191 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1197 /* DDE messages which don't require packing are:
1206 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1207 HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1210 /* send back the value of h on the other side */
1211 push_data( data, &h, sizeof(HGLOBAL) );
1213 TRACE( "send dde-ack %x %08x => %08lx\n", uiLo, uiHi, (DWORD)h );
1218 /* uiHi should contain either an atom or 0 */
1219 TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
1220 lp = MAKELONG( uiLo, uiHi );
1229 size = GlobalSize( (HGLOBAL)uiLo ) ;
1230 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1231 (info->msg == WM_DDE_DATA && size < sizeof(DDEDATA)) ||
1232 (info->msg == WM_DDE_POKE && size < sizeof(DDEPOKE))
1236 else if (info->msg != WM_DDE_DATA) return FALSE;
1241 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1243 push_data( data, ptr, size );
1244 hunlock = (HGLOBAL)uiLo;
1247 TRACE( "send ddepack %u %x\n", size, uiHi );
1249 case WM_DDE_EXECUTE:
1252 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1254 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1255 /* so that the other side can send it back on ACK */
1257 hunlock = (HGLOBAL)info->lparam;
1262 SERVER_START_REQ( send_message )
1265 req->type = info->type;
1266 req->win = info->hwnd;
1267 req->msg = info->msg;
1268 req->wparam = info->wparam;
1270 req->time = GetCurrentTime();
1272 for (i = 0; i < data->count; i++)
1273 wine_server_add_data( req, data->data[i], data->size[i] );
1274 if ((res = wine_server_call( req )))
1276 if (res == STATUS_INVALID_PARAMETER)
1277 /* FIXME: find a STATUS_ value for this one */
1278 SetLastError( ERROR_INVALID_THREAD_ID );
1280 SetLastError( RtlNtStatusToDosError(res) );
1283 FreeDDElParam(info->msg, info->lparam);
1286 if (hunlock) GlobalUnlock(hunlock);
1291 /***********************************************************************
1292 * unpack_dde_message
1294 * Unpack a posted DDE message received from another process.
1296 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1297 void **buffer, size_t size )
1308 /* hMem is being passed */
1309 if (size != sizeof(HGLOBAL)) return FALSE;
1310 if (!buffer || !*buffer) return FALSE;
1312 memcpy( &hMem, *buffer, size );
1314 TRACE("recv dde-ack %u mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1318 uiLo = LOWORD( *lparam );
1319 uiHi = HIWORD( *lparam );
1320 TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
1322 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1327 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1329 TRACE( "recv ddepack %u %x\n", size, uiHi );
1332 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1333 if (hMem && (ptr = GlobalLock( hMem )))
1335 memcpy( ptr, *buffer, size );
1336 GlobalUnlock( hMem );
1342 *lparam = PackDDElParam( message, uiLo, uiHi );
1344 case WM_DDE_EXECUTE:
1347 if (!buffer || !*buffer) return FALSE;
1348 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1349 if (hMem && (ptr = GlobalLock( hMem )))
1351 memcpy( ptr, *buffer, size );
1352 GlobalUnlock( hMem );
1353 TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
1354 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1360 } else return FALSE;
1361 *lparam = (LPARAM)hMem;
1367 /***********************************************************************
1370 * Call a window procedure and the corresponding hooks.
1372 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1373 BOOL unicode, BOOL same_thread )
1378 CWPRETSTRUCT cwpret;
1379 MESSAGEQUEUE *queue = QUEUE_Current();
1381 if (queue->recursion_count > MAX_SENDMSG_RECURSION) return 0;
1382 queue->recursion_count++;
1384 if (msg & 0x80000000)
1386 result = handle_internal_message( hwnd, msg, wparam, lparam );
1390 /* first the WH_CALLWNDPROC hook */
1391 hwnd = WIN_GetFullHandle( hwnd );
1392 cwp.lParam = lparam;
1393 cwp.wParam = wparam;
1396 HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1398 /* now call the window procedure */
1401 if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) goto done;
1402 result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1406 if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) goto done;
1407 result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1410 /* and finally the WH_CALLWNDPROCRET hook */
1411 cwpret.lResult = result;
1412 cwpret.lParam = lparam;
1413 cwpret.wParam = wparam;
1414 cwpret.message = msg;
1416 HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1418 queue->recursion_count--;
1423 /***********************************************************************
1424 * process_hardware_message
1426 * Process a hardware message; return TRUE if message should be passed on to the app
1428 static BOOL process_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd,
1429 UINT first, UINT last, BOOL remove )
1433 if (!MSG_process_raw_hardware_message( msg, extra_info, hwnd, first, last, remove ))
1436 ret = MSG_process_cooked_hardware_message( msg, extra_info, remove );
1438 /* tell the server we have passed it to the app
1439 * (even though we may end up dropping it later on)
1441 SERVER_START_REQ( reply_message )
1443 req->type = MSG_HARDWARE;
1445 req->remove = remove || !ret;
1446 wine_server_call( req );
1453 /***********************************************************************
1456 * Peek for a message matching the given parameters. Return FALSE if none available.
1457 * All pending sent messages are processed before returning.
1459 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1462 ULONG_PTR extra_info = 0;
1463 MESSAGEQUEUE *queue = QUEUE_Current();
1464 struct received_message_info info, *old_info;
1466 if (!first && !last) last = ~0;
1471 void *buffer = NULL;
1472 size_t size = 0, buffer_size = 0;
1474 do /* loop while buffer is too small */
1476 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1478 SERVER_START_REQ( get_message )
1481 req->get_win = hwnd;
1482 req->get_first = first;
1483 req->get_last = last;
1484 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1485 if (!(res = wine_server_call( req )))
1487 size = wine_server_reply_size( reply );
1488 info.type = reply->type;
1489 info.msg.hwnd = reply->win;
1490 info.msg.message = reply->msg;
1491 info.msg.wParam = reply->wparam;
1492 info.msg.lParam = reply->lparam;
1493 info.msg.time = reply->time;
1494 info.msg.pt.x = reply->x;
1495 info.msg.pt.y = reply->y;
1496 extra_info = reply->info;
1500 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1501 buffer_size = reply->total;
1505 } while (res == STATUS_BUFFER_OVERFLOW);
1507 if (res) return FALSE;
1509 TRACE( "got type %d msg %x hwnd %p wp %x lp %lx\n",
1510 info.type, info.msg.message, info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1516 info.flags = ISMEX_SEND;
1519 info.flags = ISMEX_NOTIFY;
1522 info.flags = ISMEX_CALLBACK;
1524 case MSG_OTHER_PROCESS:
1525 info.flags = ISMEX_SEND;
1526 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1527 &info.msg.lParam, &buffer, size ))
1529 ERR( "invalid packed message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1530 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
1531 info.msg.wParam, info.msg.lParam, size );
1533 reply_message( &info, 0, TRUE );
1538 if (!process_hardware_message( &info.msg, extra_info,
1539 hwnd, first, last, flags & GET_MSG_REMOVE ))
1541 TRACE("dropping msg %x\n", info.msg.message );
1542 goto next; /* ignore it */
1544 queue->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
1547 queue->GetMessageExtraInfoVal = extra_info;
1548 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
1550 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1551 &info.msg.lParam, &buffer, size ))
1553 ERR( "invalid packed dde-message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1554 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1555 info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
1556 goto next; /* ignore it */
1560 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1564 /* if we get here, we have a sent message; call the window procedure */
1565 old_info = queue->receive_info;
1566 queue->receive_info = &info;
1567 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1568 info.msg.lParam, (info.type != MSG_ASCII), FALSE );
1569 reply_message( &info, result, TRUE );
1570 queue->receive_info = old_info;
1572 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1577 /***********************************************************************
1578 * wait_message_reply
1580 * Wait until a sent message gets replied to.
1582 static void wait_message_reply( UINT flags )
1584 MESSAGEQUEUE *queue;
1586 if (!(queue = QUEUE_Current())) return;
1590 unsigned int wake_bits = 0, changed_bits = 0;
1593 SERVER_START_REQ( set_queue_mask )
1595 req->wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
1596 req->changed_mask = req->wake_mask;
1598 if (!wine_server_call( req ))
1600 wake_bits = reply->wake_bits;
1601 changed_bits = reply->changed_bits;
1606 if (wake_bits & QS_SMRESULT) return; /* got a result */
1607 if (wake_bits & QS_SENDMESSAGE)
1609 /* Process the sent message immediately */
1611 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1615 /* now wait for it */
1617 ReleaseThunkLock( &dwlc );
1619 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1620 res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1623 res = WaitForSingleObject( queue->server_queue, INFINITE );
1625 if (dwlc) RestoreThunkLock( dwlc );
1629 /***********************************************************************
1630 * put_message_in_queue
1632 * Put a sent message into the destination queue.
1633 * For inter-process message, reply_size is set to expected size of reply data.
1635 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1636 size_t *reply_size )
1638 struct packed_message data;
1640 int i, timeout = -1;
1642 if (info->type != MSG_NOTIFY &&
1643 info->type != MSG_CALLBACK &&
1644 info->type != MSG_POSTED &&
1645 info->timeout != INFINITE)
1646 timeout = info->timeout;
1649 if (info->type == MSG_OTHER_PROCESS)
1651 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1652 if (data.count == -1)
1654 WARN( "cannot pack message %x\n", info->msg );
1658 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
1660 return post_dde_message( dest_tid, &data, info );
1663 SERVER_START_REQ( send_message )
1666 req->type = info->type;
1667 req->win = info->hwnd;
1668 req->msg = info->msg;
1669 req->wparam = info->wparam;
1670 req->lparam = info->lparam;
1671 req->time = GetCurrentTime();
1672 req->timeout = timeout;
1673 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1674 if ((res = wine_server_call( req )))
1676 if (res == STATUS_INVALID_PARAMETER)
1677 /* FIXME: find a STATUS_ value for this one */
1678 SetLastError( ERROR_INVALID_THREAD_ID );
1680 SetLastError( RtlNtStatusToDosError(res) );
1688 /***********************************************************************
1691 * Retrieve a message reply from the server.
1693 static LRESULT retrieve_reply( const struct send_message_info *info,
1694 size_t reply_size, LRESULT *result )
1697 void *reply_data = NULL;
1701 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
1703 WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
1707 SERVER_START_REQ( get_message_reply )
1710 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
1711 if (!(status = wine_server_call( req ))) *result = reply->result;
1712 reply_size = wine_server_reply_size( reply );
1715 if (!status && reply_size)
1716 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
1718 if (reply_data) HeapFree( GetProcessHeap(), 0, reply_data );
1720 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1721 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
1722 info->lparam, *result, status );
1724 if (!status) return 1;
1725 if (status == STATUS_TIMEOUT) SetLastError(0); /* timeout */
1726 else SetLastError( RtlNtStatusToDosError(status) );
1731 /***********************************************************************
1732 * send_inter_thread_message
1734 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1739 size_t reply_size = 0;
1741 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
1742 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
1744 if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1746 /* there's no reply to wait for on notify/callback messages */
1747 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1749 locks = WIN_SuspendWndsLock();
1751 wait_message_reply( info->flags );
1752 ret = retrieve_reply( info, reply_size, res_ptr );
1754 WIN_RestoreWndsLock( locks );
1759 /***********************************************************************
1760 * SendMessageTimeoutW (USER32.@)
1762 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1763 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1765 struct send_message_info info;
1766 DWORD dest_tid, dest_pid;
1767 LRESULT ret, result;
1769 info.type = MSG_UNICODE;
1772 info.wparam = wparam;
1773 info.lparam = lparam;
1775 info.timeout = timeout;
1777 if (is_broadcast(hwnd))
1779 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1780 if (res_ptr) *res_ptr = 1;
1784 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1786 if (USER_IsExitingThread( dest_tid )) return 0;
1788 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1790 if (dest_tid == GetCurrentThreadId())
1792 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1797 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1798 ret = send_inter_thread_message( dest_tid, &info, &result );
1801 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1802 if (ret && res_ptr) *res_ptr = result;
1807 /***********************************************************************
1808 * SendMessageTimeoutA (USER32.@)
1810 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1811 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1813 struct send_message_info info;
1814 DWORD dest_tid, dest_pid;
1815 LRESULT ret, result;
1817 info.type = MSG_ASCII;
1820 info.wparam = wparam;
1821 info.lparam = lparam;
1823 info.timeout = timeout;
1825 if (is_broadcast(hwnd))
1827 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1828 if (res_ptr) *res_ptr = 1;
1832 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1834 if (USER_IsExitingThread( dest_tid )) return 0;
1836 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1838 if (dest_tid == GetCurrentThreadId())
1840 result = call_window_proc( hwnd, msg, wparam, lparam, FALSE, TRUE );
1843 else if (dest_pid == GetCurrentProcessId())
1845 ret = send_inter_thread_message( dest_tid, &info, &result );
1849 /* inter-process message: need to map to Unicode */
1850 info.type = MSG_OTHER_PROCESS;
1851 if (is_unicode_message( info.msg ))
1853 if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1855 ret = send_inter_thread_message( dest_tid, &info, &result );
1856 result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1857 info.lparam, result );
1859 else ret = send_inter_thread_message( dest_tid, &info, &result );
1861 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1862 if (ret && res_ptr) *res_ptr = result;
1867 /***********************************************************************
1868 * SendMessageW (USER32.@)
1870 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1873 SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1878 /***********************************************************************
1879 * SendMessageA (USER32.@)
1881 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1884 SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1889 /***********************************************************************
1890 * SendNotifyMessageA (USER32.@)
1892 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1894 return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1898 /***********************************************************************
1899 * SendNotifyMessageW (USER32.@)
1901 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1903 struct send_message_info info;
1907 if (is_pointer_message(msg))
1909 SetLastError(ERROR_INVALID_PARAMETER);
1913 info.type = MSG_NOTIFY;
1916 info.wparam = wparam;
1917 info.lparam = lparam;
1919 if (is_broadcast(hwnd))
1921 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1925 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1927 if (USER_IsExitingThread( dest_tid )) return TRUE;
1929 if (dest_tid == GetCurrentThreadId())
1931 call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1934 return send_inter_thread_message( dest_tid, &info, &result );
1938 /***********************************************************************
1939 * SendMessageCallbackA (USER32.@)
1941 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1942 SENDASYNCPROC callback, ULONG_PTR data )
1944 return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
1945 lparam, callback, data );
1949 /***********************************************************************
1950 * SendMessageCallbackW (USER32.@)
1952 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1953 SENDASYNCPROC callback, ULONG_PTR data )
1955 struct send_message_info info;
1959 if (is_pointer_message(msg))
1961 SetLastError(ERROR_INVALID_PARAMETER);
1965 info.type = MSG_CALLBACK;
1968 info.wparam = wparam;
1969 info.lparam = lparam;
1970 info.callback = callback;
1973 if (is_broadcast(hwnd))
1975 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1979 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1981 if (USER_IsExitingThread( dest_tid )) return TRUE;
1983 if (dest_tid == GetCurrentThreadId())
1985 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1986 callback( hwnd, msg, data, result );
1989 FIXME( "callback will not be called\n" );
1990 return send_inter_thread_message( dest_tid, &info, &result );
1994 /***********************************************************************
1995 * ReplyMessage (USER32.@)
1997 BOOL WINAPI ReplyMessage( LRESULT result )
1999 MESSAGEQUEUE *queue = QUEUE_Current();
2000 struct received_message_info *info = queue->receive_info;
2002 if (!info) return FALSE;
2003 reply_message( info, result, FALSE );
2008 /***********************************************************************
2009 * InSendMessage (USER32.@)
2011 BOOL WINAPI InSendMessage(void)
2013 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
2017 /***********************************************************************
2018 * InSendMessageEx (USER32.@)
2020 DWORD WINAPI InSendMessageEx( LPVOID reserved )
2022 MESSAGEQUEUE *queue = QUEUE_Current();
2023 struct received_message_info *info = queue->receive_info;
2025 if (info) return info->flags;
2026 return ISMEX_NOSEND;
2030 /***********************************************************************
2031 * PostMessageA (USER32.@)
2033 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2035 return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
2039 /***********************************************************************
2040 * PostMessageW (USER32.@)
2042 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2044 struct send_message_info info;
2047 if (is_pointer_message( msg ))
2049 SetLastError( ERROR_INVALID_PARAMETER );
2053 info.type = MSG_POSTED;
2056 info.wparam = wparam;
2057 info.lparam = lparam;
2059 if (is_broadcast(hwnd))
2061 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2065 if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2067 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2069 if (USER_IsExitingThread( dest_tid )) return TRUE;
2071 return put_message_in_queue( dest_tid, &info, NULL );
2075 /**********************************************************************
2076 * PostThreadMessageA (USER32.@)
2078 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2080 return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2084 /**********************************************************************
2085 * PostThreadMessageW (USER32.@)
2087 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2089 struct send_message_info info;
2091 if (is_pointer_message( msg ))
2093 SetLastError( ERROR_INVALID_PARAMETER );
2096 if (USER_IsExitingThread( thread )) return TRUE;
2098 info.type = MSG_POSTED;
2101 info.wparam = wparam;
2102 info.lparam = lparam;
2103 return put_message_in_queue( thread, &info, NULL );
2107 /***********************************************************************
2108 * PostQuitMessage (USER32.@)
2110 void WINAPI PostQuitMessage( INT exitCode )
2112 PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
2116 /***********************************************************************
2117 * PeekMessageW (USER32.@)
2119 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2121 MESSAGEQUEUE *queue;
2125 /* check for graphics events */
2126 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2127 USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
2129 hwnd = WIN_GetFullHandle( hwnd );
2130 locks = WIN_SuspendWndsLock();
2132 if (!MSG_peek_message( &msg, hwnd, first, last,
2133 (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2135 if (!(flags & PM_NOYIELD))
2138 ReleaseThunkLock(&count);
2139 if (count) RestoreThunkLock(count);
2141 WIN_RestoreWndsLock( locks );
2145 WIN_RestoreWndsLock( locks );
2147 /* need to fill the window handle for WM_PAINT message */
2148 if (msg.message == WM_PAINT)
2150 if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
2152 msg.message = WM_PAINTICON;
2155 /* clear internal paint flag */
2156 RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
2159 if ((queue = QUEUE_Current()))
2161 queue->GetMessageTimeVal = msg.time;
2162 msg.pt.x = LOWORD( queue->GetMessagePosVal );
2163 msg.pt.y = HIWORD( queue->GetMessagePosVal );
2166 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
2168 /* copy back our internal safe copy of message data to msg_out.
2169 * msg_out is a variable from the *program*, so it can't be used
2170 * internally as it can get "corrupted" by our use of SendMessage()
2171 * (back to the program) inside the message handling itself. */
2177 /***********************************************************************
2178 * PeekMessageA (USER32.@)
2180 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2182 BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2183 if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2188 /***********************************************************************
2189 * GetMessageW (USER32.@)
2191 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2193 MESSAGEQUEUE *queue = QUEUE_Current();
2196 mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2199 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2200 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2201 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2202 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2203 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2204 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2206 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2208 locks = WIN_SuspendWndsLock();
2210 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2212 /* wait until one of the bits is set */
2213 unsigned int wake_bits = 0, changed_bits = 0;
2216 SERVER_START_REQ( set_queue_mask )
2218 req->wake_mask = QS_SENDMESSAGE;
2219 req->changed_mask = mask;
2221 if (!wine_server_call( req ))
2223 wake_bits = reply->wake_bits;
2224 changed_bits = reply->changed_bits;
2229 if (changed_bits & mask) continue;
2230 if (wake_bits & QS_SENDMESSAGE) continue;
2232 TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2233 queue->self, mask, wake_bits, changed_bits );
2235 ReleaseThunkLock( &dwlc );
2236 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2237 USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
2239 WaitForSingleObject( queue->server_queue, INFINITE );
2240 if (dwlc) RestoreThunkLock( dwlc );
2243 WIN_RestoreWndsLock( locks );
2245 return (msg->message != WM_QUIT);
2249 /***********************************************************************
2250 * GetMessageA (USER32.@)
2252 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2254 GetMessageW( msg, hwnd, first, last );
2255 msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2256 return (msg->message != WM_QUIT);
2260 /***********************************************************************
2261 * IsDialogMessage (USER32.@)
2262 * IsDialogMessageA (USER32.@)
2264 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2267 msg.wParam = map_wparam_AtoW( msg.message, msg.wParam );
2268 return IsDialogMessageW( hwndDlg, &msg );
2272 /***********************************************************************
2273 * SetMessageQueue (USER32.@)
2275 BOOL WINAPI SetMessageQueue( INT size )
2277 /* now obsolete the message queue will be expanded dynamically as necessary */
2282 /**********************************************************************
2283 * AttachThreadInput (USER32.@)
2285 * Attaches the input processing mechanism of one thread to that of
2288 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
2292 SERVER_START_REQ( attach_thread_input )
2294 req->tid_from = from;
2296 req->attach = attach;
2297 ret = !wine_server_call_err( req );
2304 /**********************************************************************
2305 * GetGUIThreadInfo (USER32.@)
2307 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
2311 SERVER_START_REQ( get_thread_input )
2314 if ((ret = !wine_server_call_err( req )))
2317 info->hwndActive = reply->active;
2318 info->hwndFocus = reply->focus;
2319 info->hwndCapture = reply->capture;
2320 info->hwndMenuOwner = reply->menu_owner;
2321 info->hwndMoveSize = reply->move_size;
2322 info->hwndCaret = reply->caret;
2323 info->rcCaret.left = reply->rect.left;
2324 info->rcCaret.top = reply->rect.top;
2325 info->rcCaret.right = reply->rect.right;
2326 info->rcCaret.bottom = reply->rect.bottom;
2327 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
2328 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
2329 if (reply->caret) info->flags |= GUI_CARETBLINKING;
2337 /**********************************************************************
2338 * GetKeyState (USER32.@)
2340 * An application calls the GetKeyState function in response to a
2341 * keyboard-input message. This function retrieves the state of the key
2342 * at the time the input message was generated.
2344 SHORT WINAPI GetKeyState(INT vkey)
2348 SERVER_START_REQ( get_key_state )
2350 req->tid = GetCurrentThreadId();
2352 if (!wine_server_call( req )) retval = (signed char)reply->state;
2355 TRACE("key (0x%x) -> %x\n", vkey, retval);
2360 /**********************************************************************
2361 * GetKeyboardState (USER32.@)
2363 BOOL WINAPI GetKeyboardState( LPBYTE state )
2367 TRACE("(%p)\n", state);
2369 memset( state, 0, 256 );
2370 SERVER_START_REQ( get_key_state )
2372 req->tid = GetCurrentThreadId();
2374 wine_server_set_reply( req, state, 256 );
2375 ret = !wine_server_call_err( req );
2382 /**********************************************************************
2383 * SetKeyboardState (USER32.@)
2385 BOOL WINAPI SetKeyboardState( LPBYTE state )
2389 TRACE("(%p)\n", state);
2391 SERVER_START_REQ( set_key_state )
2393 req->tid = GetCurrentThreadId();
2394 wine_server_add_data( req, state, 256 );
2395 ret = !wine_server_call_err( req );