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 )
328 if (message == WM_CHARTOITEM ||
329 message == EM_SETPASSWORDCHAR ||
330 message == WM_CHAR ||
331 message == WM_DEADCHAR ||
332 message == WM_SYSCHAR ||
333 message == WM_SYSDEADCHAR ||
334 message == WM_MENUCHAR)
336 char ch = LOWORD(wparam);
338 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
339 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
345 /***********************************************************************
348 * Convert the wparam of a Unicode message to ASCII.
350 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
352 if (message == WM_CHARTOITEM ||
353 message == EM_SETPASSWORDCHAR ||
354 message == WM_CHAR ||
355 message == WM_DEADCHAR ||
356 message == WM_SYSCHAR ||
357 message == WM_SYSDEADCHAR ||
358 message == WM_MENUCHAR)
360 WCHAR wch = LOWORD(wparam);
362 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
363 wparam = MAKEWPARAM( (unsigned char)ch, HIWORD(wparam) );
369 /***********************************************************************
372 * Pack a message for sending to another process.
373 * Return the size of the data we expect in the message reply.
374 * Set data->count to -1 if there is an error.
376 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
377 struct packed_message *data )
385 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
386 push_data( data, cs, sizeof(*cs) );
387 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
388 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
392 case WM_ASKCBFORMATNAME:
393 return wparam * sizeof(WCHAR);
395 case WM_WININICHANGE:
396 case WM_DEVMODECHANGE:
401 push_string( data, (LPWSTR)lparam );
403 case WM_GETMINMAXINFO:
404 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
405 return sizeof(MINMAXINFO);
407 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
410 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
411 return sizeof(MEASUREITEMSTRUCT);
413 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
416 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
418 case WM_WINDOWPOSCHANGING:
419 case WM_WINDOWPOSCHANGED:
420 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
421 return sizeof(WINDOWPOS);
424 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
425 push_data( data, cp, sizeof(*cp) );
426 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
430 /* WM_NOTIFY cannot be sent across processes (MSDN) */
434 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
436 case WM_STYLECHANGING:
437 case WM_STYLECHANGED:
438 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
443 push_data( data, (RECT *)lparam, sizeof(RECT) );
448 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
449 push_data( data, nc, sizeof(*nc) );
450 push_data( data, nc->lppos, sizeof(*nc->lppos) );
451 return sizeof(*nc) + sizeof(*nc->lppos);
454 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
456 case SBM_SETSCROLLINFO:
457 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
459 case SBM_GETSCROLLINFO:
460 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
461 return sizeof(SCROLLINFO);
467 if (wparam) size += sizeof(DWORD);
468 if (lparam) size += sizeof(DWORD);
473 case CB_GETDROPPEDCONTROLRECT:
477 push_data( data, (RECT *)lparam, sizeof(RECT) );
481 WORD *pw = (WORD *)lparam;
482 push_data( data, pw, sizeof(*pw) );
483 return *pw * sizeof(WCHAR);
487 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
490 case CB_INSERTSTRING:
492 case CB_FINDSTRINGEXACT:
493 case CB_SELECTSTRING:
494 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
497 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
498 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
500 case LB_INSERTSTRING:
502 case LB_FINDSTRINGEXACT:
503 case LB_SELECTSTRING:
504 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
507 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
508 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
510 return wparam * sizeof(UINT);
512 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
513 return sizeof(MDINEXTMENU);
516 push_data( data, (RECT *)lparam, sizeof(RECT) );
520 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
521 push_data( data, cs, sizeof(*cs) );
522 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
523 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
526 case WM_MDIGETACTIVE:
527 if (lparam) return sizeof(BOOL);
529 case WM_WINE_SETWINDOWPOS:
530 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
533 /* these contain an HFONT */
536 /* these contain an HDC */
539 case WM_ICONERASEBKGND:
541 case WM_CTLCOLORMSGBOX:
542 case WM_CTLCOLOREDIT:
543 case WM_CTLCOLORLISTBOX:
546 case WM_CTLCOLORSCROLLBAR:
547 case WM_CTLCOLORSTATIC:
550 /* these contain an HGLOBAL */
551 case WM_PAINTCLIPBOARD:
552 case WM_SIZECLIPBOARD:
553 /* these contain pointers */
555 case WM_QUERYDROPOBJECT:
559 case WM_DEVICECHANGE:
560 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
568 /***********************************************************************
571 * Unpack a message received from another process.
573 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
574 void **buffer, size_t size )
583 CREATESTRUCTW *cs = *buffer;
584 WCHAR *str = (WCHAR *)(cs + 1);
585 if (size < sizeof(*cs)) return FALSE;
587 if (HIWORD(cs->lpszName))
589 if (!check_string( str, size )) return FALSE;
591 size -= (strlenW(str) + 1) * sizeof(WCHAR);
592 str += strlenW(str) + 1;
594 if (HIWORD(cs->lpszClass))
596 if (!check_string( str, size )) return FALSE;
602 case WM_ASKCBFORMATNAME:
603 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
606 case WM_WININICHANGE:
607 case WM_DEVMODECHANGE:
612 if (!check_string( *buffer, size )) return FALSE;
614 case WM_GETMINMAXINFO:
615 minsize = sizeof(MINMAXINFO);
618 minsize = sizeof(DRAWITEMSTRUCT);
621 minsize = sizeof(MEASUREITEMSTRUCT);
624 minsize = sizeof(DELETEITEMSTRUCT);
627 minsize = sizeof(COMPAREITEMSTRUCT);
629 case WM_WINDOWPOSCHANGING:
630 case WM_WINDOWPOSCHANGED:
631 case WM_WINE_SETWINDOWPOS:
632 minsize = sizeof(WINDOWPOS);
636 COPYDATASTRUCT *cp = *buffer;
637 if (size < sizeof(*cp)) return FALSE;
640 minsize = sizeof(*cp) + cp->cbData;
646 /* WM_NOTIFY cannot be sent across processes (MSDN) */
649 minsize = sizeof(HELPINFO);
651 case WM_STYLECHANGING:
652 case WM_STYLECHANGED:
653 minsize = sizeof(STYLESTRUCT);
656 if (!*wparam) minsize = sizeof(RECT);
659 NCCALCSIZE_PARAMS *nc = *buffer;
660 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
661 nc->lppos = (WINDOWPOS *)(nc + 1);
665 if (!*lparam) return TRUE;
666 minsize = sizeof(MSG);
668 case SBM_SETSCROLLINFO:
669 minsize = sizeof(SCROLLINFO);
671 case SBM_GETSCROLLINFO:
672 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
677 if (*wparam || *lparam)
679 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
680 if (*wparam) *wparam = (WPARAM)*buffer;
681 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
686 case CB_GETDROPPEDCONTROLRECT:
687 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
691 minsize = sizeof(RECT);
696 if (size < sizeof(WORD)) return FALSE;
697 len = *(WORD *)*buffer;
698 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
699 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
704 if (!*wparam) return TRUE;
705 minsize = *wparam * sizeof(UINT);
708 case CB_INSERTSTRING:
710 case CB_FINDSTRINGEXACT:
711 case CB_SELECTSTRING:
713 case LB_INSERTSTRING:
715 case LB_FINDSTRINGEXACT:
716 case LB_SELECTSTRING:
717 if (!*buffer) return TRUE;
718 if (!check_string( *buffer, size )) return FALSE;
722 size = sizeof(ULONG_PTR);
723 if (combobox_has_strings( hwnd ))
724 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
725 if (!get_buffer_space( buffer, size )) return FALSE;
730 size = sizeof(ULONG_PTR);
731 if (listbox_has_strings( hwnd ))
732 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
733 if (!get_buffer_space( buffer, size )) return FALSE;
737 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
740 minsize = sizeof(MDINEXTMENU);
741 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
745 minsize = sizeof(RECT);
746 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
750 MDICREATESTRUCTW *cs = *buffer;
751 WCHAR *str = (WCHAR *)(cs + 1);
752 if (size < sizeof(*cs)) return FALSE;
754 if (HIWORD(cs->szTitle))
756 if (!check_string( str, size )) return FALSE;
758 size -= (strlenW(str) + 1) * sizeof(WCHAR);
759 str += strlenW(str) + 1;
761 if (HIWORD(cs->szClass))
763 if (!check_string( str, size )) return FALSE;
768 case WM_MDIGETACTIVE:
769 if (!*lparam) return TRUE;
770 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
772 /* these contain an HFONT */
775 /* these contain an HDC */
778 case WM_ICONERASEBKGND:
780 case WM_CTLCOLORMSGBOX:
781 case WM_CTLCOLOREDIT:
782 case WM_CTLCOLORLISTBOX:
785 case WM_CTLCOLORSCROLLBAR:
786 case WM_CTLCOLORSTATIC:
789 /* these contain an HGLOBAL */
790 case WM_PAINTCLIPBOARD:
791 case WM_SIZECLIPBOARD:
792 /* these contain pointers */
794 case WM_QUERYDROPOBJECT:
798 case WM_DEVICECHANGE:
799 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
803 return TRUE; /* message doesn't need any unpacking */
806 /* default exit for most messages: check minsize and store buffer in lparam */
807 if (size < minsize) return FALSE;
808 *lparam = (LPARAM)*buffer;
813 /***********************************************************************
816 * Pack a reply to a message for sending to another process.
818 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
819 LRESULT res, struct packed_message *data )
826 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
831 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
833 case WM_GETMINMAXINFO:
834 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
837 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
839 case WM_WINDOWPOSCHANGING:
840 case WM_WINDOWPOSCHANGED:
841 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
844 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
846 case SBM_GETSCROLLINFO:
847 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
851 case CB_GETDROPPEDCONTROLRECT:
854 push_data( data, (RECT *)lparam, sizeof(RECT) );
858 WORD *ptr = (WORD *)lparam;
859 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
863 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
865 case WM_MDIGETACTIVE:
866 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
870 push_data( data, (RECT *)lparam, sizeof(RECT) );
873 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
874 push_data( data, nc, sizeof(*nc) );
875 push_data( data, nc->lppos, sizeof(*nc->lppos) );
881 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
882 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
885 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
888 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
890 case WM_ASKCBFORMATNAME:
891 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
897 /***********************************************************************
900 * Unpack a message reply received from another process.
902 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
903 void *buffer, size_t size )
910 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
911 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
912 memcpy( cs, buffer, min( sizeof(*cs), size ));
913 cs->lpszName = name; /* restore the original pointers */
914 cs->lpszClass = class;
918 case WM_ASKCBFORMATNAME:
919 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
921 case WM_GETMINMAXINFO:
922 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
925 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
927 case WM_WINDOWPOSCHANGING:
928 case WM_WINDOWPOSCHANGED:
929 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
932 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
934 case SBM_GETSCROLLINFO:
935 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
938 case CB_GETDROPPEDCONTROLRECT:
942 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
945 size = min( size, (size_t)*(WORD *)lparam );
946 memcpy( (WCHAR *)lparam, buffer, size );
949 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
953 memcpy( (WCHAR *)lparam, buffer, size );
956 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
958 case WM_MDIGETACTIVE:
959 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
963 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
966 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
967 WINDOWPOS *wp = nc->lppos;
968 memcpy( nc, buffer, min( sizeof(*nc), size ));
969 if (size > sizeof(*nc))
972 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
974 nc->lppos = wp; /* restore the original pointer */
982 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
983 if (size <= sizeof(DWORD)) break;
984 size -= sizeof(DWORD);
985 buffer = (DWORD *)buffer + 1;
987 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
991 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
992 LPCWSTR title = cs->szTitle, class = cs->szClass;
993 memcpy( cs, buffer, min( sizeof(*cs), size ));
994 cs->szTitle = title; /* restore the original pointers */
999 ERR( "should not happen: unexpected message %x\n", message );
1005 /***********************************************************************
1008 * Send a reply to a sent message.
1010 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1012 struct packed_message data;
1013 int i, replied = info->flags & ISMEX_REPLIED;
1015 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1016 if (!remove && replied) return; /* replied already */
1019 info->flags |= ISMEX_REPLIED;
1021 if (info->type == MSG_OTHER_PROCESS && !replied)
1023 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1024 info->msg.lParam, result, &data );
1027 SERVER_START_REQ( reply_message )
1029 req->result = result;
1030 req->remove = remove;
1031 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1032 wine_server_call( req );
1038 /***********************************************************************
1039 * handle_internal_message
1041 * Handle an internal Wine message instead of calling the window proc.
1043 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1045 if (hwnd == GetDesktopWindow()) return 0;
1048 case WM_WINE_DESTROYWINDOW:
1049 return WIN_DestroyWindow( hwnd );
1050 case WM_WINE_SETWINDOWPOS:
1051 return USER_Driver.pSetWindowPos( (WINDOWPOS *)lparam );
1052 case WM_WINE_SHOWWINDOW:
1053 return ShowWindow( hwnd, wparam );
1054 case WM_WINE_SETPARENT:
1055 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1056 case WM_WINE_SETWINDOWLONG:
1057 return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1058 case WM_WINE_ENABLEWINDOW:
1059 return EnableWindow( hwnd, wparam );
1060 case WM_WINE_SETACTIVEWINDOW:
1061 return (LRESULT)SetActiveWindow( (HWND)wparam );
1063 FIXME( "unknown internal message %x\n", msg );
1068 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1069 * to the memory handle, we keep track (in the server side) of all pairs of handle
1070 * used (the client passes its value and the content of the memory handle), and
1071 * the server stored both values (the client, and the local one, created after the
1072 * content). When a ACK message is generated, the list of pair is searched for a
1073 * matching pair, so that the client memory handle can be returned.
1076 HGLOBAL client_hMem;
1077 HGLOBAL server_hMem;
1080 static struct DDE_pair* dde_pairs;
1081 static int dde_num_alloc;
1082 static int dde_num_used;
1083 static CRITICAL_SECTION dde_crst = CRITICAL_SECTION_INIT("Raw_DDE_CritSect");
1085 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1090 EnterCriticalSection(&dde_crst);
1092 /* now remember the pair of hMem on both sides */
1093 if (dde_num_used == dde_num_alloc)
1095 struct DDE_pair* tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1096 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1099 LeaveCriticalSection(&dde_crst);
1103 /* zero out newly allocated part */
1104 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1105 dde_num_alloc += GROWBY;
1108 for (i = 0; i < dde_num_alloc; i++)
1110 if (dde_pairs[i].server_hMem == 0)
1112 dde_pairs[i].client_hMem = chm;
1113 dde_pairs[i].server_hMem = shm;
1118 LeaveCriticalSection(&dde_crst);
1122 static HGLOBAL dde_get_pair(HGLOBAL shm)
1127 EnterCriticalSection(&dde_crst);
1128 for (i = 0; i < dde_num_alloc; i++)
1130 if (dde_pairs[i].server_hMem == shm)
1132 /* free this pair */
1133 dde_pairs[i].server_hMem = 0;
1135 ret = dde_pairs[i].client_hMem;
1139 LeaveCriticalSection(&dde_crst);
1143 /***********************************************************************
1148 static BOOL post_dde_message( DWORD dest_tid, struct packed_message *data, const struct send_message_info *info )
1154 HGLOBAL hunlock = 0;
1158 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1164 /* DDE messages which don't require packing are:
1173 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1174 HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1177 /* send back the value of h on the other side */
1178 push_data( data, &h, sizeof(HGLOBAL) );
1180 TRACE( "send dde-ack %x %08x => %08lx\n", uiLo, uiHi, (DWORD)h );
1185 /* uiHi should contain either an atom or 0 */
1186 TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
1187 lp = MAKELONG( uiLo, uiHi );
1196 size = GlobalSize( (HGLOBAL)uiLo ) ;
1197 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1198 (info->msg == WM_DDE_DATA && size < sizeof(DDEDATA)) ||
1199 (info->msg == WM_DDE_POKE && size < sizeof(DDEPOKE))
1203 else if (info->msg != WM_DDE_DATA) return FALSE;
1208 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1210 push_data( data, ptr, size );
1211 hunlock = (HGLOBAL)uiLo;
1214 TRACE( "send ddepack %u %x\n", size, uiHi );
1216 case WM_DDE_EXECUTE:
1219 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1221 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1222 /* so that the other side can send it back on ACK */
1224 hunlock = (HGLOBAL)info->lparam;
1229 SERVER_START_REQ( send_message )
1232 req->type = info->type;
1233 req->win = info->hwnd;
1234 req->msg = info->msg;
1235 req->wparam = info->wparam;
1237 req->time = GetCurrentTime();
1239 for (i = 0; i < data->count; i++)
1240 wine_server_add_data( req, data->data[i], data->size[i] );
1241 if ((res = wine_server_call( req )))
1243 if (res == STATUS_INVALID_PARAMETER)
1244 /* FIXME: find a STATUS_ value for this one */
1245 SetLastError( ERROR_INVALID_THREAD_ID );
1247 SetLastError( RtlNtStatusToDosError(res) );
1250 FreeDDElParam(info->msg, info->lparam);
1253 if (hunlock) GlobalUnlock(hunlock);
1258 /***********************************************************************
1259 * unpack_dde_message
1261 * Unpack a posted DDE message received from another process.
1263 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1264 void **buffer, size_t size )
1275 /* hMem is being passed */
1276 if (size != sizeof(HGLOBAL)) return FALSE;
1277 if (!buffer || !*buffer) return FALSE;
1279 memcpy( &hMem, *buffer, size );
1281 TRACE("recv dde-ack %u mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1285 uiLo = LOWORD( *lparam );
1286 uiHi = HIWORD( *lparam );
1287 TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
1289 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1294 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1296 TRACE( "recv ddepack %u %x\n", size, uiHi );
1299 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1300 if (hMem && (ptr = GlobalLock( hMem )))
1302 memcpy( ptr, *buffer, size );
1303 GlobalUnlock( hMem );
1309 *lparam = PackDDElParam( message, uiLo, uiHi );
1311 case WM_DDE_EXECUTE:
1314 if (!buffer || !*buffer) return FALSE;
1315 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1316 if (hMem && (ptr = GlobalLock( hMem )))
1318 memcpy( ptr, *buffer, size );
1319 GlobalUnlock( hMem );
1320 TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
1321 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1327 } else return FALSE;
1328 *lparam = (LPARAM)hMem;
1334 /***********************************************************************
1337 * Call a window procedure and the corresponding hooks.
1339 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1340 BOOL unicode, BOOL same_thread )
1345 CWPRETSTRUCT cwpret;
1346 MESSAGEQUEUE *queue = QUEUE_Current();
1348 if (queue->recursion_count > MAX_SENDMSG_RECURSION) return 0;
1349 queue->recursion_count++;
1351 if (msg & 0x80000000)
1353 result = handle_internal_message( hwnd, msg, wparam, lparam );
1357 /* first the WH_CALLWNDPROC hook */
1358 hwnd = WIN_GetFullHandle( hwnd );
1359 cwp.lParam = lparam;
1360 cwp.wParam = wparam;
1363 HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1365 /* now call the window procedure */
1368 if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) goto done;
1369 result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1373 if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) goto done;
1374 result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1377 /* and finally the WH_CALLWNDPROCRET hook */
1378 cwpret.lResult = result;
1379 cwpret.lParam = lparam;
1380 cwpret.wParam = wparam;
1381 cwpret.message = msg;
1383 HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1385 queue->recursion_count--;
1390 /***********************************************************************
1391 * process_hardware_message
1393 * Process a hardware message; return TRUE if message should be passed on to the app
1395 static BOOL process_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd,
1396 UINT first, UINT last, BOOL remove )
1400 if (!MSG_process_raw_hardware_message( msg, extra_info, hwnd, first, last, remove ))
1403 ret = MSG_process_cooked_hardware_message( msg, extra_info, remove );
1405 /* tell the server we have passed it to the app
1406 * (even though we may end up dropping it later on)
1408 SERVER_START_REQ( reply_message )
1411 req->remove = remove || !ret;
1412 wine_server_call( req );
1419 /***********************************************************************
1422 * Peek for a message matching the given parameters. Return FALSE if none available.
1423 * All pending sent messages are processed before returning.
1425 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1428 ULONG_PTR extra_info = 0;
1429 MESSAGEQUEUE *queue = QUEUE_Current();
1430 struct received_message_info info, *old_info;
1432 if (!first && !last) last = ~0;
1437 void *buffer = NULL;
1438 size_t size = 0, buffer_size = 0;
1440 do /* loop while buffer is too small */
1442 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1444 SERVER_START_REQ( get_message )
1447 req->get_win = hwnd;
1448 req->get_first = first;
1449 req->get_last = last;
1450 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1451 if (!(res = wine_server_call( req )))
1453 size = wine_server_reply_size( reply );
1454 info.type = reply->type;
1455 info.msg.hwnd = reply->win;
1456 info.msg.message = reply->msg;
1457 info.msg.wParam = reply->wparam;
1458 info.msg.lParam = reply->lparam;
1459 info.msg.time = reply->time;
1460 info.msg.pt.x = reply->x;
1461 info.msg.pt.y = reply->y;
1462 extra_info = reply->info;
1466 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1467 buffer_size = reply->total;
1471 } while (res == STATUS_BUFFER_OVERFLOW);
1473 if (res) return FALSE;
1475 TRACE( "got type %d msg %x hwnd %p wp %x lp %lx\n",
1476 info.type, info.msg.message, info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1482 info.flags = ISMEX_SEND;
1485 info.flags = ISMEX_NOTIFY;
1488 info.flags = ISMEX_CALLBACK;
1490 case MSG_OTHER_PROCESS:
1491 info.flags = ISMEX_SEND;
1492 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1493 &info.msg.lParam, &buffer, size ))
1495 ERR( "invalid packed message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1496 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
1497 info.msg.wParam, info.msg.lParam, size );
1499 reply_message( &info, 0, TRUE );
1504 if (!process_hardware_message( &info.msg, extra_info,
1505 hwnd, first, last, flags & GET_MSG_REMOVE ))
1507 TRACE("dropping msg %x\n", info.msg.message );
1508 goto next; /* ignore it */
1510 queue->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
1513 queue->GetMessageExtraInfoVal = extra_info;
1514 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
1516 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1517 &info.msg.lParam, &buffer, size ))
1519 ERR( "invalid packed dde-message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1520 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1521 info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
1522 goto next; /* ignore it */
1526 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1530 /* if we get here, we have a sent message; call the window procedure */
1531 old_info = queue->receive_info;
1532 queue->receive_info = &info;
1533 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1534 info.msg.lParam, (info.type != MSG_ASCII), FALSE );
1535 reply_message( &info, result, TRUE );
1536 queue->receive_info = old_info;
1538 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1543 /***********************************************************************
1544 * wait_message_reply
1546 * Wait until a sent message gets replied to.
1548 static void wait_message_reply( UINT flags )
1550 MESSAGEQUEUE *queue;
1552 if (!(queue = QUEUE_Current())) return;
1556 unsigned int wake_bits = 0, changed_bits = 0;
1559 SERVER_START_REQ( set_queue_mask )
1561 req->wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
1562 req->changed_mask = req->wake_mask;
1564 if (!wine_server_call( req ))
1566 wake_bits = reply->wake_bits;
1567 changed_bits = reply->changed_bits;
1572 if (wake_bits & QS_SMRESULT) return; /* got a result */
1573 if (wake_bits & QS_SENDMESSAGE)
1575 /* Process the sent message immediately */
1577 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1581 /* now wait for it */
1583 ReleaseThunkLock( &dwlc );
1585 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1586 res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1589 res = WaitForSingleObject( queue->server_queue, INFINITE );
1591 if (dwlc) RestoreThunkLock( dwlc );
1595 /***********************************************************************
1596 * put_message_in_queue
1598 * Put a sent message into the destination queue.
1599 * For inter-process message, reply_size is set to expected size of reply data.
1601 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1602 size_t *reply_size )
1604 struct packed_message data;
1606 int i, timeout = -1;
1608 if (info->type != MSG_NOTIFY &&
1609 info->type != MSG_CALLBACK &&
1610 info->type != MSG_POSTED &&
1611 info->timeout != INFINITE)
1612 timeout = info->timeout;
1615 if (info->type == MSG_OTHER_PROCESS)
1617 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1618 if (data.count == -1)
1620 WARN( "cannot pack message %x\n", info->msg );
1624 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
1626 return post_dde_message( dest_tid, &data, info );
1629 SERVER_START_REQ( send_message )
1632 req->type = info->type;
1633 req->win = info->hwnd;
1634 req->msg = info->msg;
1635 req->wparam = info->wparam;
1636 req->lparam = info->lparam;
1637 req->time = GetCurrentTime();
1638 req->timeout = timeout;
1639 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1640 if ((res = wine_server_call( req )))
1642 if (res == STATUS_INVALID_PARAMETER)
1643 /* FIXME: find a STATUS_ value for this one */
1644 SetLastError( ERROR_INVALID_THREAD_ID );
1646 SetLastError( RtlNtStatusToDosError(res) );
1654 /***********************************************************************
1657 * Retrieve a message reply from the server.
1659 static LRESULT retrieve_reply( const struct send_message_info *info,
1660 size_t reply_size, LRESULT *result )
1663 void *reply_data = NULL;
1667 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
1669 WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
1673 SERVER_START_REQ( get_message_reply )
1676 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
1677 if (!(status = wine_server_call( req ))) *result = reply->result;
1678 reply_size = wine_server_reply_size( reply );
1681 if (!status && reply_size)
1682 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
1684 if (reply_data) HeapFree( GetProcessHeap(), 0, reply_data );
1686 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1687 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
1688 info->lparam, *result, status );
1690 if (!status) return 1;
1691 if (status == STATUS_TIMEOUT) SetLastError(0); /* timeout */
1692 else SetLastError( RtlNtStatusToDosError(status) );
1697 /***********************************************************************
1698 * send_inter_thread_message
1700 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1705 size_t reply_size = 0;
1707 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
1708 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
1710 if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1712 /* there's no reply to wait for on notify/callback messages */
1713 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1715 locks = WIN_SuspendWndsLock();
1717 wait_message_reply( info->flags );
1718 ret = retrieve_reply( info, reply_size, res_ptr );
1720 WIN_RestoreWndsLock( locks );
1725 /***********************************************************************
1726 * SendMessageTimeoutW (USER32.@)
1728 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1729 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1731 struct send_message_info info;
1732 DWORD dest_tid, dest_pid;
1733 LRESULT ret, result;
1735 info.type = MSG_UNICODE;
1738 info.wparam = wparam;
1739 info.lparam = lparam;
1741 info.timeout = timeout;
1743 if (is_broadcast(hwnd))
1745 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1746 if (res_ptr) *res_ptr = 1;
1750 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1752 if (USER_IsExitingThread( dest_tid )) return 0;
1754 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1756 if (dest_tid == GetCurrentThreadId())
1758 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1763 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1764 ret = send_inter_thread_message( dest_tid, &info, &result );
1767 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1768 if (ret && res_ptr) *res_ptr = result;
1773 /***********************************************************************
1774 * SendMessageTimeoutA (USER32.@)
1776 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1777 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1779 struct send_message_info info;
1780 DWORD dest_tid, dest_pid;
1781 LRESULT ret, result;
1783 info.type = MSG_ASCII;
1786 info.wparam = wparam;
1787 info.lparam = lparam;
1789 info.timeout = timeout;
1791 if (is_broadcast(hwnd))
1793 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1794 if (res_ptr) *res_ptr = 1;
1798 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1800 if (USER_IsExitingThread( dest_tid )) return 0;
1802 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1804 if (dest_tid == GetCurrentThreadId())
1806 result = call_window_proc( hwnd, msg, wparam, lparam, FALSE, TRUE );
1809 else if (dest_pid == GetCurrentProcessId())
1811 ret = send_inter_thread_message( dest_tid, &info, &result );
1815 /* inter-process message: need to map to Unicode */
1816 info.type = MSG_OTHER_PROCESS;
1817 if (is_unicode_message( info.msg ))
1819 if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1821 ret = send_inter_thread_message( dest_tid, &info, &result );
1822 result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1823 info.lparam, result );
1825 else ret = send_inter_thread_message( dest_tid, &info, &result );
1827 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1828 if (ret && res_ptr) *res_ptr = result;
1833 /***********************************************************************
1834 * SendMessageW (USER32.@)
1836 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1839 SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1844 /***********************************************************************
1845 * SendMessageA (USER32.@)
1847 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1850 SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1855 /***********************************************************************
1856 * SendNotifyMessageA (USER32.@)
1858 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1860 return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1864 /***********************************************************************
1865 * SendNotifyMessageW (USER32.@)
1867 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1869 struct send_message_info info;
1873 if (is_pointer_message(msg))
1875 SetLastError(ERROR_INVALID_PARAMETER);
1879 info.type = MSG_NOTIFY;
1882 info.wparam = wparam;
1883 info.lparam = lparam;
1885 if (is_broadcast(hwnd))
1887 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1891 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1893 if (USER_IsExitingThread( dest_tid )) return TRUE;
1895 if (dest_tid == GetCurrentThreadId())
1897 call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1900 return send_inter_thread_message( dest_tid, &info, &result );
1904 /***********************************************************************
1905 * SendMessageCallbackA (USER32.@)
1907 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1908 SENDASYNCPROC callback, ULONG_PTR data )
1910 return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
1911 lparam, callback, data );
1915 /***********************************************************************
1916 * SendMessageCallbackW (USER32.@)
1918 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1919 SENDASYNCPROC callback, ULONG_PTR data )
1921 struct send_message_info info;
1925 if (is_pointer_message(msg))
1927 SetLastError(ERROR_INVALID_PARAMETER);
1931 info.type = MSG_CALLBACK;
1934 info.wparam = wparam;
1935 info.lparam = lparam;
1936 info.callback = callback;
1939 if (is_broadcast(hwnd))
1941 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1945 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1947 if (USER_IsExitingThread( dest_tid )) return TRUE;
1949 if (dest_tid == GetCurrentThreadId())
1951 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1952 callback( hwnd, msg, data, result );
1955 FIXME( "callback will not be called\n" );
1956 return send_inter_thread_message( dest_tid, &info, &result );
1960 /***********************************************************************
1961 * ReplyMessage (USER32.@)
1963 BOOL WINAPI ReplyMessage( LRESULT result )
1965 MESSAGEQUEUE *queue = QUEUE_Current();
1966 struct received_message_info *info = queue->receive_info;
1968 if (!info) return FALSE;
1969 reply_message( info, result, FALSE );
1974 /***********************************************************************
1975 * InSendMessage (USER32.@)
1977 BOOL WINAPI InSendMessage(void)
1979 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
1983 /***********************************************************************
1984 * InSendMessageEx (USER32.@)
1986 DWORD WINAPI InSendMessageEx( LPVOID reserved )
1988 MESSAGEQUEUE *queue = QUEUE_Current();
1989 struct received_message_info *info = queue->receive_info;
1991 if (info) return info->flags;
1992 return ISMEX_NOSEND;
1996 /***********************************************************************
1997 * PostMessageA (USER32.@)
1999 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2001 return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
2005 /***********************************************************************
2006 * PostMessageW (USER32.@)
2008 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2010 struct send_message_info info;
2013 if (is_pointer_message( msg ))
2015 SetLastError( ERROR_INVALID_PARAMETER );
2019 info.type = MSG_POSTED;
2022 info.wparam = wparam;
2023 info.lparam = lparam;
2025 if (is_broadcast(hwnd))
2027 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2031 if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2033 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2035 if (USER_IsExitingThread( dest_tid )) return TRUE;
2037 return put_message_in_queue( dest_tid, &info, NULL );
2041 /**********************************************************************
2042 * PostThreadMessageA (USER32.@)
2044 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2046 return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2050 /**********************************************************************
2051 * PostThreadMessageW (USER32.@)
2053 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2055 struct send_message_info info;
2057 if (is_pointer_message( msg ))
2059 SetLastError( ERROR_INVALID_PARAMETER );
2062 if (USER_IsExitingThread( thread )) return TRUE;
2064 info.type = MSG_POSTED;
2067 info.wparam = wparam;
2068 info.lparam = lparam;
2069 return put_message_in_queue( thread, &info, NULL );
2073 /***********************************************************************
2074 * PostQuitMessage (USER32.@)
2076 void WINAPI PostQuitMessage( INT exitCode )
2078 PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
2082 /***********************************************************************
2083 * PeekMessageW (USER32.@)
2085 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2087 MESSAGEQUEUE *queue;
2091 /* check for graphics events */
2092 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2093 USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
2095 hwnd = WIN_GetFullHandle( hwnd );
2096 locks = WIN_SuspendWndsLock();
2098 if (!MSG_peek_message( &msg, hwnd, first, last,
2099 (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2101 if (!(flags & PM_NOYIELD))
2104 ReleaseThunkLock(&count);
2105 if (count) RestoreThunkLock(count);
2107 WIN_RestoreWndsLock( locks );
2111 WIN_RestoreWndsLock( locks );
2113 /* need to fill the window handle for WM_PAINT message */
2114 if (msg.message == WM_PAINT)
2116 if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
2118 msg.message = WM_PAINTICON;
2121 /* clear internal paint flag */
2122 RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
2125 if ((queue = QUEUE_Current()))
2127 queue->GetMessageTimeVal = msg.time;
2128 msg.pt.x = LOWORD( queue->GetMessagePosVal );
2129 msg.pt.y = HIWORD( queue->GetMessagePosVal );
2132 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
2134 /* copy back our internal safe copy of message data to msg_out.
2135 * msg_out is a variable from the *program*, so it can't be used
2136 * internally as it can get "corrupted" by our use of SendMessage()
2137 * (back to the program) inside the message handling itself. */
2143 /***********************************************************************
2144 * PeekMessageA (USER32.@)
2146 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2148 BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2149 if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2154 /***********************************************************************
2155 * GetMessageW (USER32.@)
2157 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2159 MESSAGEQUEUE *queue = QUEUE_Current();
2162 mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2165 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2166 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2167 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2168 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2169 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2170 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2172 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2174 locks = WIN_SuspendWndsLock();
2176 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2178 /* wait until one of the bits is set */
2179 unsigned int wake_bits = 0, changed_bits = 0;
2182 SERVER_START_REQ( set_queue_mask )
2184 req->wake_mask = QS_SENDMESSAGE;
2185 req->changed_mask = mask;
2187 if (!wine_server_call( req ))
2189 wake_bits = reply->wake_bits;
2190 changed_bits = reply->changed_bits;
2195 if (changed_bits & mask) continue;
2196 if (wake_bits & QS_SENDMESSAGE) continue;
2198 TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2199 queue->self, mask, wake_bits, changed_bits );
2201 ReleaseThunkLock( &dwlc );
2202 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2203 USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
2205 WaitForSingleObject( queue->server_queue, INFINITE );
2206 if (dwlc) RestoreThunkLock( dwlc );
2209 WIN_RestoreWndsLock( locks );
2211 return (msg->message != WM_QUIT);
2215 /***********************************************************************
2216 * GetMessageA (USER32.@)
2218 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2220 GetMessageW( msg, hwnd, first, last );
2221 msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2222 return (msg->message != WM_QUIT);
2226 /***********************************************************************
2227 * IsDialogMessage (USER32.@)
2228 * IsDialogMessageA (USER32.@)
2230 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2233 msg.wParam = map_wparam_AtoW( msg.message, msg.wParam );
2234 return IsDialogMessageW( hwndDlg, &msg );
2238 /***********************************************************************
2239 * SetMessageQueue (USER32.@)
2241 BOOL WINAPI SetMessageQueue( INT size )
2243 /* now obsolete the message queue will be expanded dynamically as necessary */
2248 /**********************************************************************
2249 * AttachThreadInput (USER32.@)
2251 * Attaches the input processing mechanism of one thread to that of
2254 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
2258 SERVER_START_REQ( attach_thread_input )
2260 req->tid_from = from;
2262 req->attach = attach;
2263 ret = !wine_server_call_err( req );
2270 /**********************************************************************
2271 * GetGUIThreadInfo (USER32.@)
2273 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
2277 SERVER_START_REQ( get_thread_input )
2280 if ((ret = !wine_server_call_err( req )))
2283 info->hwndActive = reply->active;
2284 info->hwndFocus = reply->focus;
2285 info->hwndCapture = reply->capture;
2286 info->hwndMenuOwner = reply->menu_owner;
2287 info->hwndMoveSize = reply->move_size;
2288 info->hwndCaret = reply->caret;
2289 info->rcCaret.left = reply->rect.left;
2290 info->rcCaret.top = reply->rect.top;
2291 info->rcCaret.right = reply->rect.right;
2292 info->rcCaret.bottom = reply->rect.bottom;
2293 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
2294 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
2295 if (reply->caret) info->flags |= GUI_CARETBLINKING;
2303 /**********************************************************************
2304 * GetKeyState (USER32.@)
2306 * An application calls the GetKeyState function in response to a
2307 * keyboard-input message. This function retrieves the state of the key
2308 * at the time the input message was generated.
2310 SHORT WINAPI GetKeyState(INT vkey)
2314 SERVER_START_REQ( get_key_state )
2316 req->tid = GetCurrentThreadId();
2318 if (!wine_server_call( req )) retval = (signed char)reply->state;
2321 TRACE("key (0x%x) -> %x\n", vkey, retval);
2326 /**********************************************************************
2327 * GetKeyboardState (USER32.@)
2329 BOOL WINAPI GetKeyboardState( LPBYTE state )
2333 TRACE("(%p)\n", state);
2335 memset( state, 0, 256 );
2336 SERVER_START_REQ( get_key_state )
2338 req->tid = GetCurrentThreadId();
2340 wine_server_set_reply( req, state, 256 );
2341 ret = !wine_server_call_err( req );
2348 /**********************************************************************
2349 * SetKeyboardState (USER32.@)
2351 BOOL WINAPI SetKeyboardState( LPBYTE state )
2355 TRACE("(%p)\n", state);
2357 memset( state, 0, 256 );
2358 SERVER_START_REQ( set_key_state )
2360 req->tid = GetCurrentThreadId();
2361 wine_server_add_data( req, state, 256 );
2362 ret = !wine_server_call_err( req );