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"
32 #include "wine/unicode.h"
33 #include "wine/server.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msg);
41 WINE_DECLARE_DEBUG_CHANNEL(relay);
43 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
44 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
46 #define MAX_PACK_COUNT 4
48 /* description of the data fields that need to be packed along with a sent message */
52 const void *data[MAX_PACK_COUNT];
53 size_t size[MAX_PACK_COUNT];
56 /* info about the message currently being received by the current thread */
57 struct received_message_info
59 enum message_type type;
61 UINT flags; /* InSendMessageEx return flags */
64 /* structure to group all parameters for sent messages of the various kinds */
65 struct send_message_info
67 enum message_type type;
72 UINT flags; /* flags for SendMessageTimeout */
73 UINT timeout; /* timeout for SendMessageTimeout */
74 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
75 ULONG_PTR data; /* callback data */
79 /* flag for messages that contain pointers */
80 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
82 #define SET(msg) (1 << ((msg) & 31))
84 static const unsigned int message_pointer_flags[] =
87 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
88 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
90 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
93 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
94 SET(WM_NOTIFY) | SET(WM_HELP),
96 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
98 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
100 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
102 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
104 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO),
110 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
111 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
112 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
116 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
117 SET(LB_DIR) | SET(LB_FINDSTRING) |
118 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
120 SET(LB_FINDSTRINGEXACT),
126 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
128 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
129 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
143 SET(WM_ASKCBFORMATNAME)
146 /* flags for messages that contain Unicode strings */
147 static const unsigned int message_unicode_flags[] =
150 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
151 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
163 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
167 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
171 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
172 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
176 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
177 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
179 SET(LB_FINDSTRINGEXACT),
201 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
204 /* check whether a given message type includes pointers */
205 inline static int is_pointer_message( UINT message )
207 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
208 return (message_pointer_flags[message / 32] & SET(message)) != 0;
211 /* check whether a given message type contains Unicode (or ASCII) chars */
212 inline static int is_unicode_message( UINT message )
214 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
215 return (message_unicode_flags[message / 32] & SET(message)) != 0;
220 /* add a data field to a packed message */
221 inline static void push_data( struct packed_message *data, const void *ptr, size_t size )
223 data->data[data->count] = ptr;
224 data->size[data->count] = size;
228 /* add a string to a packed message */
229 inline static void push_string( struct packed_message *data, LPCWSTR str )
231 push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
234 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
235 inline static void *get_data( void **buffer, size_t size )
238 *buffer = (char *)*buffer + size;
242 /* make sure that the buffer contains a valid null-terminated Unicode string */
243 inline static BOOL check_string( LPCWSTR str, size_t size )
245 for (size /= sizeof(WCHAR); size; size--, str++)
246 if (!*str) return TRUE;
250 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
251 inline static void *get_buffer_space( void **buffer, size_t size )
254 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
255 HeapFree( GetProcessHeap(), 0, *buffer );
260 /* retrieve a string pointer from packed data */
261 inline static LPWSTR get_string( void **buffer )
263 return get_data( buffer, (strlenW( (LPWSTR)*buffer ) + 1) * sizeof(WCHAR) );
266 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
267 inline static BOOL combobox_has_strings( HWND hwnd )
269 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
270 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
273 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
274 inline static BOOL listbox_has_strings( HWND hwnd )
276 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
277 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
281 /***********************************************************************
282 * broadcast_message_callback
284 * Helper callback for broadcasting messages.
286 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
288 struct send_message_info *info = (struct send_message_info *)lparam;
289 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
293 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
294 info->flags, info->timeout, NULL );
297 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
298 info->flags, info->timeout, NULL );
301 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
304 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
305 info->callback, info->data );
308 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
311 ERR( "bad type %d\n", info->type );
318 /***********************************************************************
321 * Convert the wparam of an ASCII message to Unicode.
323 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
328 case EM_SETPASSWORDCHAR:
335 char ch = LOWORD(wparam);
337 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
338 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
345 ch[0] = (wparam >> 8);
346 ch[1] = (wparam & 0xff);
347 MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
348 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
356 /***********************************************************************
359 * Convert the wparam of a Unicode message to ASCII.
361 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
366 case EM_SETPASSWORDCHAR:
373 WCHAR wch = LOWORD(wparam);
375 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
376 wparam = MAKEWPARAM( ch, HIWORD(wparam) );
381 WCHAR wch = LOWORD(wparam);
385 WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL );
386 wparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wparam) );
394 /***********************************************************************
397 * Pack a message for sending to another process.
398 * Return the size of the data we expect in the message reply.
399 * Set data->count to -1 if there is an error.
401 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
402 struct packed_message *data )
410 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
411 push_data( data, cs, sizeof(*cs) );
412 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
413 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
417 case WM_ASKCBFORMATNAME:
418 return wparam * sizeof(WCHAR);
419 case WM_WININICHANGE:
420 if (lparam) push_string(data, (LPWSTR)lparam );
423 case WM_DEVMODECHANGE:
428 push_string( data, (LPWSTR)lparam );
430 case WM_GETMINMAXINFO:
431 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
432 return sizeof(MINMAXINFO);
434 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
437 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
438 return sizeof(MEASUREITEMSTRUCT);
440 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
443 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
445 case WM_WINDOWPOSCHANGING:
446 case WM_WINDOWPOSCHANGED:
447 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
448 return sizeof(WINDOWPOS);
451 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
452 push_data( data, cp, sizeof(*cp) );
453 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
457 /* WM_NOTIFY cannot be sent across processes (MSDN) */
461 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
463 case WM_STYLECHANGING:
464 case WM_STYLECHANGED:
465 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
470 push_data( data, (RECT *)lparam, sizeof(RECT) );
475 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
476 push_data( data, nc, sizeof(*nc) );
477 push_data( data, nc->lppos, sizeof(*nc->lppos) );
478 return sizeof(*nc) + sizeof(*nc->lppos);
481 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
483 case SBM_SETSCROLLINFO:
484 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
486 case SBM_GETSCROLLINFO:
487 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
488 return sizeof(SCROLLINFO);
494 if (wparam) size += sizeof(DWORD);
495 if (lparam) size += sizeof(DWORD);
500 case CB_GETDROPPEDCONTROLRECT:
504 push_data( data, (RECT *)lparam, sizeof(RECT) );
508 WORD *pw = (WORD *)lparam;
509 push_data( data, pw, sizeof(*pw) );
510 return *pw * sizeof(WCHAR);
514 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
517 case CB_INSERTSTRING:
519 case CB_FINDSTRINGEXACT:
520 case CB_SELECTSTRING:
521 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
524 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
525 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
527 case LB_INSERTSTRING:
529 case LB_FINDSTRINGEXACT:
530 case LB_SELECTSTRING:
531 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
534 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
535 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
537 return wparam * sizeof(UINT);
539 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
540 return sizeof(MDINEXTMENU);
543 push_data( data, (RECT *)lparam, sizeof(RECT) );
547 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
548 push_data( data, cs, sizeof(*cs) );
549 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
550 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
553 case WM_MDIGETACTIVE:
554 if (lparam) return sizeof(BOOL);
556 case WM_WINE_SETWINDOWPOS:
557 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
559 case WM_WINE_KEYBOARD_LL_HOOK:
560 push_data( data, (KBDLLHOOKSTRUCT *)lparam, sizeof(KBDLLHOOKSTRUCT) );
562 case WM_WINE_MOUSE_LL_HOOK:
563 push_data( data, (MSLLHOOKSTRUCT *)lparam, sizeof(MSLLHOOKSTRUCT) );
566 /* these contain an HFONT */
569 /* these contain an HDC */
572 case WM_ICONERASEBKGND:
574 case WM_CTLCOLORMSGBOX:
575 case WM_CTLCOLOREDIT:
576 case WM_CTLCOLORLISTBOX:
579 case WM_CTLCOLORSCROLLBAR:
580 case WM_CTLCOLORSTATIC:
583 /* these contain an HGLOBAL */
584 case WM_PAINTCLIPBOARD:
585 case WM_SIZECLIPBOARD:
586 /* these contain pointers */
588 case WM_QUERYDROPOBJECT:
592 case WM_DEVICECHANGE:
593 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
601 /***********************************************************************
604 * Unpack a message received from another process.
606 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
607 void **buffer, size_t size )
616 CREATESTRUCTW *cs = *buffer;
617 WCHAR *str = (WCHAR *)(cs + 1);
618 if (size < sizeof(*cs)) return FALSE;
620 if (HIWORD(cs->lpszName))
622 if (!check_string( str, size )) return FALSE;
624 size -= (strlenW(str) + 1) * sizeof(WCHAR);
625 str += strlenW(str) + 1;
627 if (HIWORD(cs->lpszClass))
629 if (!check_string( str, size )) return FALSE;
635 case WM_ASKCBFORMATNAME:
636 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
638 case WM_WININICHANGE:
639 if (!*lparam) return TRUE;
642 case WM_DEVMODECHANGE:
647 if (!check_string( *buffer, size )) return FALSE;
649 case WM_GETMINMAXINFO:
650 minsize = sizeof(MINMAXINFO);
653 minsize = sizeof(DRAWITEMSTRUCT);
656 minsize = sizeof(MEASUREITEMSTRUCT);
659 minsize = sizeof(DELETEITEMSTRUCT);
662 minsize = sizeof(COMPAREITEMSTRUCT);
664 case WM_WINDOWPOSCHANGING:
665 case WM_WINDOWPOSCHANGED:
666 case WM_WINE_SETWINDOWPOS:
667 minsize = sizeof(WINDOWPOS);
671 COPYDATASTRUCT *cp = *buffer;
672 if (size < sizeof(*cp)) return FALSE;
675 minsize = sizeof(*cp) + cp->cbData;
681 /* WM_NOTIFY cannot be sent across processes (MSDN) */
684 minsize = sizeof(HELPINFO);
686 case WM_STYLECHANGING:
687 case WM_STYLECHANGED:
688 minsize = sizeof(STYLESTRUCT);
691 if (!*wparam) minsize = sizeof(RECT);
694 NCCALCSIZE_PARAMS *nc = *buffer;
695 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
696 nc->lppos = (WINDOWPOS *)(nc + 1);
700 if (!*lparam) return TRUE;
701 minsize = sizeof(MSG);
703 case SBM_SETSCROLLINFO:
704 minsize = sizeof(SCROLLINFO);
706 case SBM_GETSCROLLINFO:
707 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
712 if (*wparam || *lparam)
714 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
715 if (*wparam) *wparam = (WPARAM)*buffer;
716 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
721 case CB_GETDROPPEDCONTROLRECT:
722 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
726 minsize = sizeof(RECT);
731 if (size < sizeof(WORD)) return FALSE;
732 len = *(WORD *)*buffer;
733 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
734 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
739 if (!*wparam) return TRUE;
740 minsize = *wparam * sizeof(UINT);
743 case CB_INSERTSTRING:
745 case CB_FINDSTRINGEXACT:
746 case CB_SELECTSTRING:
748 case LB_INSERTSTRING:
750 case LB_FINDSTRINGEXACT:
751 case LB_SELECTSTRING:
752 if (!*buffer) return TRUE;
753 if (!check_string( *buffer, size )) return FALSE;
757 size = sizeof(ULONG_PTR);
758 if (combobox_has_strings( hwnd ))
759 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
760 if (!get_buffer_space( buffer, size )) return FALSE;
765 size = sizeof(ULONG_PTR);
766 if (listbox_has_strings( hwnd ))
767 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
768 if (!get_buffer_space( buffer, size )) return FALSE;
772 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
775 minsize = sizeof(MDINEXTMENU);
776 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
780 minsize = sizeof(RECT);
781 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
785 MDICREATESTRUCTW *cs = *buffer;
786 WCHAR *str = (WCHAR *)(cs + 1);
787 if (size < sizeof(*cs)) return FALSE;
789 if (HIWORD(cs->szTitle))
791 if (!check_string( str, size )) return FALSE;
793 size -= (strlenW(str) + 1) * sizeof(WCHAR);
794 str += strlenW(str) + 1;
796 if (HIWORD(cs->szClass))
798 if (!check_string( str, size )) return FALSE;
803 case WM_MDIGETACTIVE:
804 if (!*lparam) return TRUE;
805 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
807 case WM_WINE_KEYBOARD_LL_HOOK:
808 minsize = sizeof(KBDLLHOOKSTRUCT);
810 case WM_WINE_MOUSE_LL_HOOK:
811 minsize = sizeof(MSLLHOOKSTRUCT);
814 /* these contain an HFONT */
817 /* these contain an HDC */
820 case WM_ICONERASEBKGND:
822 case WM_CTLCOLORMSGBOX:
823 case WM_CTLCOLOREDIT:
824 case WM_CTLCOLORLISTBOX:
827 case WM_CTLCOLORSCROLLBAR:
828 case WM_CTLCOLORSTATIC:
831 /* these contain an HGLOBAL */
832 case WM_PAINTCLIPBOARD:
833 case WM_SIZECLIPBOARD:
834 /* these contain pointers */
836 case WM_QUERYDROPOBJECT:
840 case WM_DEVICECHANGE:
841 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
845 return TRUE; /* message doesn't need any unpacking */
848 /* default exit for most messages: check minsize and store buffer in lparam */
849 if (size < minsize) return FALSE;
850 *lparam = (LPARAM)*buffer;
855 /***********************************************************************
858 * Pack a reply to a message for sending to another process.
860 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
861 LRESULT res, struct packed_message *data )
868 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
873 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
875 case WM_GETMINMAXINFO:
876 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
879 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
881 case WM_WINDOWPOSCHANGING:
882 case WM_WINDOWPOSCHANGED:
883 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
886 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
888 case SBM_GETSCROLLINFO:
889 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
893 case CB_GETDROPPEDCONTROLRECT:
896 push_data( data, (RECT *)lparam, sizeof(RECT) );
900 WORD *ptr = (WORD *)lparam;
901 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
905 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
907 case WM_MDIGETACTIVE:
908 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
912 push_data( data, (RECT *)lparam, sizeof(RECT) );
915 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
916 push_data( data, nc, sizeof(*nc) );
917 push_data( data, nc->lppos, sizeof(*nc->lppos) );
923 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
924 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
927 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
930 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
932 case WM_ASKCBFORMATNAME:
933 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
939 /***********************************************************************
942 * Unpack a message reply received from another process.
944 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
945 void *buffer, size_t size )
952 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
953 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
954 memcpy( cs, buffer, min( sizeof(*cs), size ));
955 cs->lpszName = name; /* restore the original pointers */
956 cs->lpszClass = class;
960 case WM_ASKCBFORMATNAME:
961 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
963 case WM_GETMINMAXINFO:
964 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
967 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
969 case WM_WINDOWPOSCHANGING:
970 case WM_WINDOWPOSCHANGED:
971 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
974 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
976 case SBM_GETSCROLLINFO:
977 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
980 case CB_GETDROPPEDCONTROLRECT:
984 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
987 size = min( size, (size_t)*(WORD *)lparam );
988 memcpy( (WCHAR *)lparam, buffer, size );
991 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
995 memcpy( (WCHAR *)lparam, buffer, size );
998 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
1000 case WM_MDIGETACTIVE:
1001 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
1005 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1008 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1009 WINDOWPOS *wp = nc->lppos;
1010 memcpy( nc, buffer, min( sizeof(*nc), size ));
1011 if (size > sizeof(*nc))
1013 size -= sizeof(*nc);
1014 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
1016 nc->lppos = wp; /* restore the original pointer */
1024 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1025 if (size <= sizeof(DWORD)) break;
1026 size -= sizeof(DWORD);
1027 buffer = (DWORD *)buffer + 1;
1029 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1033 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
1034 LPCWSTR title = cs->szTitle, class = cs->szClass;
1035 memcpy( cs, buffer, min( sizeof(*cs), size ));
1036 cs->szTitle = title; /* restore the original pointers */
1037 cs->szClass = class;
1041 ERR( "should not happen: unexpected message %x\n", message );
1047 /***********************************************************************
1050 * Send a reply to a sent message.
1052 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1054 struct packed_message data;
1055 int i, replied = info->flags & ISMEX_REPLIED;
1057 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1058 if (!remove && replied) return; /* replied already */
1061 info->flags |= ISMEX_REPLIED;
1063 if (info->type == MSG_OTHER_PROCESS && !replied)
1065 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1066 info->msg.lParam, result, &data );
1069 SERVER_START_REQ( reply_message )
1071 req->type = info->type;
1072 req->result = result;
1073 req->remove = remove;
1074 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1075 wine_server_call( req );
1081 /***********************************************************************
1082 * handle_internal_message
1084 * Handle an internal Wine message instead of calling the window proc.
1086 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1088 if (hwnd == GetDesktopWindow()) return 0;
1091 case WM_WINE_DESTROYWINDOW:
1092 return WIN_DestroyWindow( hwnd );
1093 case WM_WINE_SETWINDOWPOS:
1094 return USER_Driver.pSetWindowPos( (WINDOWPOS *)lparam );
1095 case WM_WINE_SHOWWINDOW:
1096 return ShowWindow( hwnd, wparam );
1097 case WM_WINE_SETPARENT:
1098 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1099 case WM_WINE_SETWINDOWLONG:
1100 return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1101 case WM_WINE_ENABLEWINDOW:
1102 return EnableWindow( hwnd, wparam );
1103 case WM_WINE_SETACTIVEWINDOW:
1104 return (LRESULT)SetActiveWindow( (HWND)wparam );
1105 case WM_WINE_KEYBOARD_LL_HOOK:
1106 return HOOK_CallHooks( WH_KEYBOARD_LL, HC_ACTION, wparam, lparam, TRUE );
1107 case WM_WINE_MOUSE_LL_HOOK:
1108 return HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, wparam, lparam, TRUE );
1110 FIXME( "unknown internal message %x\n", msg );
1115 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1116 * to the memory handle, we keep track (in the server side) of all pairs of handle
1117 * used (the client passes its value and the content of the memory handle), and
1118 * the server stored both values (the client, and the local one, created after the
1119 * content). When a ACK message is generated, the list of pair is searched for a
1120 * matching pair, so that the client memory handle can be returned.
1123 HGLOBAL client_hMem;
1124 HGLOBAL server_hMem;
1127 static struct DDE_pair* dde_pairs;
1128 static int dde_num_alloc;
1129 static int dde_num_used;
1130 static CRITICAL_SECTION dde_crst = CRITICAL_SECTION_INIT("Raw_DDE_CritSect");
1132 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1137 EnterCriticalSection(&dde_crst);
1139 /* now remember the pair of hMem on both sides */
1140 if (dde_num_used == dde_num_alloc)
1142 struct DDE_pair* tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1143 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1146 LeaveCriticalSection(&dde_crst);
1150 /* zero out newly allocated part */
1151 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1152 dde_num_alloc += GROWBY;
1155 for (i = 0; i < dde_num_alloc; i++)
1157 if (dde_pairs[i].server_hMem == 0)
1159 dde_pairs[i].client_hMem = chm;
1160 dde_pairs[i].server_hMem = shm;
1165 LeaveCriticalSection(&dde_crst);
1169 static HGLOBAL dde_get_pair(HGLOBAL shm)
1174 EnterCriticalSection(&dde_crst);
1175 for (i = 0; i < dde_num_alloc; i++)
1177 if (dde_pairs[i].server_hMem == shm)
1179 /* free this pair */
1180 dde_pairs[i].server_hMem = 0;
1182 ret = dde_pairs[i].client_hMem;
1186 LeaveCriticalSection(&dde_crst);
1190 /***********************************************************************
1193 * Post a DDE message
1195 static BOOL post_dde_message( DWORD dest_tid, struct packed_message *data, const struct send_message_info *info )
1201 HGLOBAL hunlock = 0;
1205 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1211 /* DDE messages which don't require packing are:
1220 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1221 HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1224 /* send back the value of h on the other side */
1225 push_data( data, &h, sizeof(HGLOBAL) );
1227 TRACE( "send dde-ack %x %08x => %08lx\n", uiLo, uiHi, (DWORD)h );
1232 /* uiHi should contain either an atom or 0 */
1233 TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
1234 lp = MAKELONG( uiLo, uiHi );
1243 size = GlobalSize( (HGLOBAL)uiLo ) ;
1244 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1245 (info->msg == WM_DDE_DATA && size < sizeof(DDEDATA)) ||
1246 (info->msg == WM_DDE_POKE && size < sizeof(DDEPOKE))
1250 else if (info->msg != WM_DDE_DATA) return FALSE;
1255 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1257 DDEDATA *dde_data = (DDEDATA *)ptr;
1258 TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
1259 dde_data->unused, dde_data->fResponse, dde_data->fRelease,
1260 dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
1261 push_data( data, ptr, size );
1262 hunlock = (HGLOBAL)uiLo;
1265 TRACE( "send ddepack %u %x\n", size, uiHi );
1267 case WM_DDE_EXECUTE:
1270 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1272 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1273 /* so that the other side can send it back on ACK */
1275 hunlock = (HGLOBAL)info->lparam;
1280 SERVER_START_REQ( send_message )
1283 req->type = info->type;
1285 req->win = info->hwnd;
1286 req->msg = info->msg;
1287 req->wparam = info->wparam;
1289 req->time = GetCurrentTime();
1291 for (i = 0; i < data->count; i++)
1292 wine_server_add_data( req, data->data[i], data->size[i] );
1293 if ((res = wine_server_call( req )))
1295 if (res == STATUS_INVALID_PARAMETER)
1296 /* FIXME: find a STATUS_ value for this one */
1297 SetLastError( ERROR_INVALID_THREAD_ID );
1299 SetLastError( RtlNtStatusToDosError(res) );
1302 FreeDDElParam(info->msg, info->lparam);
1305 if (hunlock) GlobalUnlock(hunlock);
1310 /***********************************************************************
1311 * unpack_dde_message
1313 * Unpack a posted DDE message received from another process.
1315 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1316 void **buffer, size_t size )
1327 /* hMem is being passed */
1328 if (size != sizeof(HGLOBAL)) return FALSE;
1329 if (!buffer || !*buffer) return FALSE;
1331 memcpy( &hMem, *buffer, size );
1333 TRACE("recv dde-ack %x mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1337 uiLo = LOWORD( *lparam );
1338 uiHi = HIWORD( *lparam );
1339 TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
1341 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1346 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1348 TRACE( "recv ddepack %u %x\n", size, uiHi );
1351 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1352 if (hMem && (ptr = GlobalLock( hMem )))
1354 memcpy( ptr, *buffer, size );
1355 GlobalUnlock( hMem );
1361 *lparam = PackDDElParam( message, uiLo, uiHi );
1363 case WM_DDE_EXECUTE:
1366 if (!buffer || !*buffer) return FALSE;
1367 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1368 if (hMem && (ptr = GlobalLock( hMem )))
1370 memcpy( ptr, *buffer, size );
1371 GlobalUnlock( hMem );
1372 TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
1373 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1379 } else return FALSE;
1380 *lparam = (LPARAM)hMem;
1386 /***********************************************************************
1389 * Call a window procedure and the corresponding hooks.
1391 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1392 BOOL unicode, BOOL same_thread )
1397 CWPRETSTRUCT cwpret;
1398 MESSAGEQUEUE *queue = QUEUE_Current();
1400 if (queue->recursion_count > MAX_SENDMSG_RECURSION) return 0;
1401 queue->recursion_count++;
1403 if (msg & 0x80000000)
1405 result = handle_internal_message( hwnd, msg, wparam, lparam );
1409 /* first the WH_CALLWNDPROC hook */
1410 hwnd = WIN_GetFullHandle( hwnd );
1411 cwp.lParam = lparam;
1412 cwp.wParam = wparam;
1415 HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1417 /* now call the window procedure */
1420 if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) goto done;
1421 result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1425 if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) goto done;
1426 result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1429 /* and finally the WH_CALLWNDPROCRET hook */
1430 cwpret.lResult = result;
1431 cwpret.lParam = lparam;
1432 cwpret.wParam = wparam;
1433 cwpret.message = msg;
1435 HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1437 queue->recursion_count--;
1442 /***********************************************************************
1443 * process_hardware_message
1445 * Process a hardware message; return TRUE if message should be passed on to the app
1447 static BOOL process_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd,
1448 UINT first, UINT last, BOOL remove )
1452 if (!MSG_process_raw_hardware_message( msg, extra_info, hwnd, first, last, remove ))
1455 ret = MSG_process_cooked_hardware_message( msg, extra_info, remove );
1457 /* tell the server we have passed it to the app
1458 * (even though we may end up dropping it later on)
1460 SERVER_START_REQ( reply_message )
1462 req->type = MSG_HARDWARE;
1464 req->remove = remove || !ret;
1465 wine_server_call( req );
1472 /***********************************************************************
1473 * call_sendmsg_callback
1475 * Call the callback function of SendMessageCallback.
1477 static inline void call_sendmsg_callback( SENDASYNCPROC callback, HWND hwnd, UINT msg,
1478 ULONG_PTR data, LRESULT result )
1480 if (TRACE_ON(relay))
1481 DPRINTF( "%04lx:Call message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1482 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
1484 callback( hwnd, msg, data, result );
1485 if (TRACE_ON(relay))
1486 DPRINTF( "%04lx:Ret message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1487 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
1492 /***********************************************************************
1495 * Peek for a message matching the given parameters. Return FALSE if none available.
1496 * All pending sent messages are processed before returning.
1498 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1501 ULONG_PTR extra_info = 0;
1502 MESSAGEQUEUE *queue = QUEUE_Current();
1503 struct received_message_info info, *old_info;
1505 if (!first && !last) last = ~0;
1510 void *buffer = NULL;
1511 size_t size = 0, buffer_size = 0;
1513 do /* loop while buffer is too small */
1515 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1517 SERVER_START_REQ( get_message )
1520 req->get_win = hwnd;
1521 req->get_first = first;
1522 req->get_last = last;
1523 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1524 if (!(res = wine_server_call( req )))
1526 size = wine_server_reply_size( reply );
1527 info.type = reply->type;
1528 info.msg.hwnd = reply->win;
1529 info.msg.message = reply->msg;
1530 info.msg.wParam = reply->wparam;
1531 info.msg.lParam = reply->lparam;
1532 info.msg.time = reply->time;
1533 info.msg.pt.x = reply->x;
1534 info.msg.pt.y = reply->y;
1535 extra_info = reply->info;
1539 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1540 buffer_size = reply->total;
1544 } while (res == STATUS_BUFFER_OVERFLOW);
1546 if (res) return FALSE;
1548 TRACE( "got type %d msg %x (%s) hwnd %p wp %x lp %lx\n",
1549 info.type, info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1550 info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1556 info.flags = ISMEX_SEND;
1559 info.flags = ISMEX_NOTIFY;
1562 info.flags = ISMEX_CALLBACK;
1564 case MSG_CALLBACK_RESULT:
1565 call_sendmsg_callback( (SENDASYNCPROC)info.msg.wParam, info.msg.hwnd,
1566 info.msg.message, extra_info, info.msg.lParam );
1568 case MSG_OTHER_PROCESS:
1569 info.flags = ISMEX_SEND;
1570 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1571 &info.msg.lParam, &buffer, size ))
1573 ERR( "invalid packed message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1574 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
1575 info.msg.wParam, info.msg.lParam, size );
1577 reply_message( &info, 0, TRUE );
1582 if (!process_hardware_message( &info.msg, extra_info,
1583 hwnd, first, last, flags & GET_MSG_REMOVE ))
1585 TRACE("dropping msg %x\n", info.msg.message );
1586 goto next; /* ignore it */
1588 queue->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
1591 queue->GetMessageExtraInfoVal = extra_info;
1592 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
1594 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1595 &info.msg.lParam, &buffer, size ))
1597 ERR( "invalid packed dde-message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1598 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1599 info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
1600 goto next; /* ignore it */
1604 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1608 /* if we get here, we have a sent message; call the window procedure */
1609 old_info = queue->receive_info;
1610 queue->receive_info = &info;
1611 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1612 info.msg.lParam, (info.type != MSG_ASCII), FALSE );
1613 reply_message( &info, result, TRUE );
1614 queue->receive_info = old_info;
1616 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1621 /***********************************************************************
1622 * wait_message_reply
1624 * Wait until a sent message gets replied to.
1626 static void wait_message_reply( UINT flags )
1628 MESSAGEQUEUE *queue;
1630 if (!(queue = QUEUE_Current())) return;
1634 unsigned int wake_bits = 0, changed_bits = 0;
1637 SERVER_START_REQ( set_queue_mask )
1639 req->wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
1640 req->changed_mask = req->wake_mask;
1642 if (!wine_server_call( req ))
1644 wake_bits = reply->wake_bits;
1645 changed_bits = reply->changed_bits;
1650 if (wake_bits & QS_SMRESULT) return; /* got a result */
1651 if (wake_bits & QS_SENDMESSAGE)
1653 /* Process the sent message immediately */
1655 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1659 /* now wait for it */
1661 ReleaseThunkLock( &dwlc );
1663 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1664 res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1667 res = WaitForSingleObject( queue->server_queue, INFINITE );
1669 if (dwlc) RestoreThunkLock( dwlc );
1673 /***********************************************************************
1674 * put_message_in_queue
1676 * Put a sent message into the destination queue.
1677 * For inter-process message, reply_size is set to expected size of reply data.
1679 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1680 size_t *reply_size )
1682 struct packed_message data;
1684 int i, timeout = -1;
1686 if (info->type != MSG_NOTIFY &&
1687 info->type != MSG_CALLBACK &&
1688 info->type != MSG_POSTED &&
1689 info->timeout != INFINITE)
1690 timeout = info->timeout;
1693 if (info->type == MSG_OTHER_PROCESS)
1695 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1696 if (data.count == -1)
1698 WARN( "cannot pack message %x\n", info->msg );
1702 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
1704 return post_dde_message( dest_tid, &data, info );
1707 SERVER_START_REQ( send_message )
1710 req->type = info->type;
1712 req->win = info->hwnd;
1713 req->msg = info->msg;
1714 req->wparam = info->wparam;
1715 req->lparam = info->lparam;
1716 req->time = GetCurrentTime();
1717 req->timeout = timeout;
1719 if (info->type == MSG_CALLBACK)
1721 req->callback = info->callback;
1722 req->info = info->data;
1725 if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
1726 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1727 if ((res = wine_server_call( req )))
1729 if (res == STATUS_INVALID_PARAMETER)
1730 /* FIXME: find a STATUS_ value for this one */
1731 SetLastError( ERROR_INVALID_THREAD_ID );
1733 SetLastError( RtlNtStatusToDosError(res) );
1741 /***********************************************************************
1744 * Retrieve a message reply from the server.
1746 static LRESULT retrieve_reply( const struct send_message_info *info,
1747 size_t reply_size, LRESULT *result )
1750 void *reply_data = NULL;
1754 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
1756 WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
1760 SERVER_START_REQ( get_message_reply )
1763 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
1764 if (!(status = wine_server_call( req ))) *result = reply->result;
1765 reply_size = wine_server_reply_size( reply );
1768 if (!status && reply_size)
1769 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
1771 if (reply_data) HeapFree( GetProcessHeap(), 0, reply_data );
1773 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1774 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
1775 info->lparam, *result, status );
1777 /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
1778 if (status) SetLastError( RtlNtStatusToDosError(status) );
1783 /***********************************************************************
1784 * send_inter_thread_message
1786 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1791 size_t reply_size = 0;
1793 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
1794 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
1796 if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1798 /* there's no reply to wait for on notify/callback messages */
1799 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1801 locks = WIN_SuspendWndsLock();
1803 wait_message_reply( info->flags );
1804 ret = retrieve_reply( info, reply_size, res_ptr );
1806 WIN_RestoreWndsLock( locks );
1811 /***********************************************************************
1812 * MSG_SendInternalMessageTimeout
1814 * Same as SendMessageTimeoutW but sends the message to a specific thread
1815 * without requiring a window handle. Only works for internal Wine messages.
1817 LRESULT MSG_SendInternalMessageTimeout( DWORD dest_pid, DWORD dest_tid,
1818 UINT msg, WPARAM wparam, LPARAM lparam,
1819 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1821 struct send_message_info info;
1822 LRESULT ret, result;
1824 assert( msg & 0x80000000 ); /* must be an internal Wine message */
1826 info.type = MSG_UNICODE;
1829 info.wparam = wparam;
1830 info.lparam = lparam;
1832 info.timeout = timeout;
1834 if (USER_IsExitingThread( dest_tid )) return 0;
1836 if (dest_tid == GetCurrentThreadId())
1838 result = handle_internal_message( 0, msg, wparam, lparam );
1843 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1844 ret = send_inter_thread_message( dest_tid, &info, &result );
1846 if (ret && res_ptr) *res_ptr = result;
1851 /***********************************************************************
1852 * SendMessageTimeoutW (USER32.@)
1854 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1855 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1857 struct send_message_info info;
1858 DWORD dest_tid, dest_pid;
1859 LRESULT ret, result;
1861 info.type = MSG_UNICODE;
1864 info.wparam = wparam;
1865 info.lparam = lparam;
1867 info.timeout = timeout;
1869 if (is_broadcast(hwnd))
1871 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1872 if (res_ptr) *res_ptr = 1;
1876 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1878 if (USER_IsExitingThread( dest_tid )) return 0;
1880 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1882 if (dest_tid == GetCurrentThreadId())
1884 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1889 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1890 ret = send_inter_thread_message( dest_tid, &info, &result );
1893 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1894 if (ret && res_ptr) *res_ptr = result;
1899 /***********************************************************************
1900 * SendMessageTimeoutA (USER32.@)
1902 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1903 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1905 struct send_message_info info;
1906 DWORD dest_tid, dest_pid;
1907 LRESULT ret, result;
1909 info.type = MSG_ASCII;
1912 info.wparam = wparam;
1913 info.lparam = lparam;
1915 info.timeout = timeout;
1917 if (is_broadcast(hwnd))
1919 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1920 if (res_ptr) *res_ptr = 1;
1924 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1926 if (USER_IsExitingThread( dest_tid )) return 0;
1928 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1930 if (dest_tid == GetCurrentThreadId())
1932 result = call_window_proc( hwnd, msg, wparam, lparam, FALSE, TRUE );
1935 else if (dest_pid == GetCurrentProcessId())
1937 ret = send_inter_thread_message( dest_tid, &info, &result );
1941 /* inter-process message: need to map to Unicode */
1942 info.type = MSG_OTHER_PROCESS;
1943 if (is_unicode_message( info.msg ))
1945 if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1947 ret = send_inter_thread_message( dest_tid, &info, &result );
1948 result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1949 info.lparam, result );
1951 else ret = send_inter_thread_message( dest_tid, &info, &result );
1953 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1954 if (ret && res_ptr) *res_ptr = result;
1959 /***********************************************************************
1960 * SendMessageW (USER32.@)
1962 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1965 SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1970 /***********************************************************************
1971 * SendMessageA (USER32.@)
1973 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1976 SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1981 /***********************************************************************
1982 * SendNotifyMessageA (USER32.@)
1984 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1986 return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1990 /***********************************************************************
1991 * SendNotifyMessageW (USER32.@)
1993 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1995 struct send_message_info info;
1999 if (is_pointer_message(msg))
2001 SetLastError(ERROR_INVALID_PARAMETER);
2005 info.type = MSG_NOTIFY;
2008 info.wparam = wparam;
2009 info.lparam = lparam;
2011 if (is_broadcast(hwnd))
2013 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2017 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2019 if (USER_IsExitingThread( dest_tid )) return TRUE;
2021 if (dest_tid == GetCurrentThreadId())
2023 call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2026 return send_inter_thread_message( dest_tid, &info, &result );
2030 /***********************************************************************
2031 * SendMessageCallbackA (USER32.@)
2033 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2034 SENDASYNCPROC callback, ULONG_PTR data )
2036 return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
2037 lparam, callback, data );
2041 /***********************************************************************
2042 * SendMessageCallbackW (USER32.@)
2044 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2045 SENDASYNCPROC callback, ULONG_PTR data )
2047 struct send_message_info info;
2051 if (is_pointer_message(msg))
2053 SetLastError(ERROR_INVALID_PARAMETER);
2057 info.type = MSG_CALLBACK;
2060 info.wparam = wparam;
2061 info.lparam = lparam;
2062 info.callback = callback;
2065 if (is_broadcast(hwnd))
2067 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2071 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2073 if (USER_IsExitingThread( dest_tid )) return TRUE;
2075 if (dest_tid == GetCurrentThreadId())
2077 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2078 call_sendmsg_callback( callback, hwnd, msg, data, result );
2081 FIXME( "callback will not be called\n" );
2082 return send_inter_thread_message( dest_tid, &info, &result );
2086 /***********************************************************************
2087 * ReplyMessage (USER32.@)
2089 BOOL WINAPI ReplyMessage( LRESULT result )
2091 MESSAGEQUEUE *queue = QUEUE_Current();
2092 struct received_message_info *info = queue->receive_info;
2094 if (!info) return FALSE;
2095 reply_message( info, result, FALSE );
2100 /***********************************************************************
2101 * InSendMessage (USER32.@)
2103 BOOL WINAPI InSendMessage(void)
2105 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
2109 /***********************************************************************
2110 * InSendMessageEx (USER32.@)
2112 DWORD WINAPI InSendMessageEx( LPVOID reserved )
2114 MESSAGEQUEUE *queue = QUEUE_Current();
2115 struct received_message_info *info = queue->receive_info;
2117 if (info) return info->flags;
2118 return ISMEX_NOSEND;
2122 /***********************************************************************
2123 * PostMessageA (USER32.@)
2125 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2127 return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
2131 /***********************************************************************
2132 * PostMessageW (USER32.@)
2134 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2136 struct send_message_info info;
2139 if (is_pointer_message( msg ))
2141 SetLastError( ERROR_INVALID_PARAMETER );
2145 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
2146 hwnd, msg, SPY_GetMsgName(msg, hwnd), wparam, lparam );
2148 info.type = MSG_POSTED;
2151 info.wparam = wparam;
2152 info.lparam = lparam;
2154 if (is_broadcast(hwnd))
2156 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2160 if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2162 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2164 if (USER_IsExitingThread( dest_tid )) return TRUE;
2166 return put_message_in_queue( dest_tid, &info, NULL );
2170 /**********************************************************************
2171 * PostThreadMessageA (USER32.@)
2173 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2175 return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2179 /**********************************************************************
2180 * PostThreadMessageW (USER32.@)
2182 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2184 struct send_message_info info;
2186 if (is_pointer_message( msg ))
2188 SetLastError( ERROR_INVALID_PARAMETER );
2191 if (USER_IsExitingThread( thread )) return TRUE;
2193 info.type = MSG_POSTED;
2196 info.wparam = wparam;
2197 info.lparam = lparam;
2198 return put_message_in_queue( thread, &info, NULL );
2202 /***********************************************************************
2203 * PostQuitMessage (USER32.@)
2205 void WINAPI PostQuitMessage( INT exitCode )
2207 PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
2211 /***********************************************************************
2212 * PeekMessageW (USER32.@)
2214 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2216 MESSAGEQUEUE *queue;
2220 /* check for graphics events */
2221 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2222 USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
2224 hwnd = WIN_GetFullHandle( hwnd );
2225 locks = WIN_SuspendWndsLock();
2227 if (!MSG_peek_message( &msg, hwnd, first, last,
2228 (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2230 if (!(flags & PM_NOYIELD))
2233 ReleaseThunkLock(&count);
2234 if (count) RestoreThunkLock(count);
2236 WIN_RestoreWndsLock( locks );
2240 WIN_RestoreWndsLock( locks );
2242 /* need to fill the window handle for WM_PAINT message */
2243 if (msg.message == WM_PAINT)
2245 if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
2247 msg.message = WM_PAINTICON;
2250 /* clear internal paint flag */
2251 RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
2254 if ((queue = QUEUE_Current()))
2256 queue->GetMessageTimeVal = msg.time;
2257 msg.pt.x = LOWORD( queue->GetMessagePosVal );
2258 msg.pt.y = HIWORD( queue->GetMessagePosVal );
2261 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
2263 /* copy back our internal safe copy of message data to msg_out.
2264 * msg_out is a variable from the *program*, so it can't be used
2265 * internally as it can get "corrupted" by our use of SendMessage()
2266 * (back to the program) inside the message handling itself. */
2272 /***********************************************************************
2273 * PeekMessageA (USER32.@)
2275 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2277 BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2278 if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2283 /***********************************************************************
2284 * GetMessageW (USER32.@)
2286 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2288 MESSAGEQUEUE *queue = QUEUE_Current();
2291 mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2294 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2295 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2296 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2297 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2298 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2299 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2301 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2303 locks = WIN_SuspendWndsLock();
2305 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2307 /* wait until one of the bits is set */
2308 unsigned int wake_bits = 0, changed_bits = 0;
2311 SERVER_START_REQ( set_queue_mask )
2313 req->wake_mask = QS_SENDMESSAGE;
2314 req->changed_mask = mask;
2316 if (!wine_server_call( req ))
2318 wake_bits = reply->wake_bits;
2319 changed_bits = reply->changed_bits;
2324 if (changed_bits & mask) continue;
2325 if (wake_bits & QS_SENDMESSAGE) continue;
2327 TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2328 queue->self, mask, wake_bits, changed_bits );
2330 ReleaseThunkLock( &dwlc );
2331 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2332 USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
2334 WaitForSingleObject( queue->server_queue, INFINITE );
2335 if (dwlc) RestoreThunkLock( dwlc );
2338 WIN_RestoreWndsLock( locks );
2340 return (msg->message != WM_QUIT);
2344 /***********************************************************************
2345 * GetMessageA (USER32.@)
2347 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2349 GetMessageW( msg, hwnd, first, last );
2350 msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2351 return (msg->message != WM_QUIT);
2355 /***********************************************************************
2356 * IsDialogMessage (USER32.@)
2357 * IsDialogMessageA (USER32.@)
2359 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2362 msg.wParam = map_wparam_AtoW( msg.message, msg.wParam );
2363 return IsDialogMessageW( hwndDlg, &msg );
2367 /***********************************************************************
2368 * SetMessageQueue (USER32.@)
2370 BOOL WINAPI SetMessageQueue( INT size )
2372 /* now obsolete the message queue will be expanded dynamically as necessary */
2377 /**********************************************************************
2378 * AttachThreadInput (USER32.@)
2380 * Attaches the input processing mechanism of one thread to that of
2383 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
2387 SERVER_START_REQ( attach_thread_input )
2389 req->tid_from = from;
2391 req->attach = attach;
2392 ret = !wine_server_call_err( req );
2399 /**********************************************************************
2400 * GetGUIThreadInfo (USER32.@)
2402 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
2406 SERVER_START_REQ( get_thread_input )
2409 if ((ret = !wine_server_call_err( req )))
2412 info->hwndActive = reply->active;
2413 info->hwndFocus = reply->focus;
2414 info->hwndCapture = reply->capture;
2415 info->hwndMenuOwner = reply->menu_owner;
2416 info->hwndMoveSize = reply->move_size;
2417 info->hwndCaret = reply->caret;
2418 info->rcCaret.left = reply->rect.left;
2419 info->rcCaret.top = reply->rect.top;
2420 info->rcCaret.right = reply->rect.right;
2421 info->rcCaret.bottom = reply->rect.bottom;
2422 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
2423 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
2424 if (reply->caret) info->flags |= GUI_CARETBLINKING;
2432 /**********************************************************************
2433 * GetKeyState (USER32.@)
2435 * An application calls the GetKeyState function in response to a
2436 * keyboard-input message. This function retrieves the state of the key
2437 * at the time the input message was generated.
2439 SHORT WINAPI GetKeyState(INT vkey)
2443 SERVER_START_REQ( get_key_state )
2445 req->tid = GetCurrentThreadId();
2447 if (!wine_server_call( req )) retval = (signed char)reply->state;
2450 TRACE("key (0x%x) -> %x\n", vkey, retval);
2455 /**********************************************************************
2456 * GetKeyboardState (USER32.@)
2458 BOOL WINAPI GetKeyboardState( LPBYTE state )
2462 TRACE("(%p)\n", state);
2464 memset( state, 0, 256 );
2465 SERVER_START_REQ( get_key_state )
2467 req->tid = GetCurrentThreadId();
2469 wine_server_set_reply( req, state, 256 );
2470 ret = !wine_server_call_err( req );
2477 /**********************************************************************
2478 * SetKeyboardState (USER32.@)
2480 BOOL WINAPI SetKeyboardState( LPBYTE state )
2484 TRACE("(%p)\n", state);
2486 SERVER_START_REQ( set_key_state )
2488 req->tid = GetCurrentThreadId();
2489 wine_server_add_data( req, state, 256 );
2490 ret = !wine_server_call_err( req );