Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / dlls / user / message.c
blob43d1dbb004ed2757ec3c534d17ff623faea54e71
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
27 #include "ntstatus.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winerror.h"
33 #include "winnls.h"
34 #include "dde.h"
35 #include "wine/unicode.h"
36 #include "wine/server.h"
37 #include "message.h"
38 #include "user.h"
39 #include "win.h"
40 #include "winproc.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(msg);
44 WINE_DECLARE_DEBUG_CHANNEL(relay);
46 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
47 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
49 #define MAX_PACK_COUNT 4
51 /* description of the data fields that need to be packed along with a sent message */
52 struct packed_message
54 int count;
55 const void *data[MAX_PACK_COUNT];
56 size_t size[MAX_PACK_COUNT];
59 /* info about the message currently being received by the current thread */
60 struct received_message_info
62 enum message_type type;
63 MSG msg;
64 UINT flags; /* InSendMessageEx return flags */
67 /* structure to group all parameters for sent messages of the various kinds */
68 struct send_message_info
70 enum message_type type;
71 HWND hwnd;
72 UINT msg;
73 WPARAM wparam;
74 LPARAM lparam;
75 UINT flags; /* flags for SendMessageTimeout */
76 UINT timeout; /* timeout for SendMessageTimeout */
77 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
78 ULONG_PTR data; /* callback data */
82 /* flag for messages that contain pointers */
83 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
85 #define SET(msg) (1 << ((msg) & 31))
87 static const unsigned int message_pointer_flags[] =
89 /* 0x00 - 0x1f */
90 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
91 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
92 /* 0x20 - 0x3f */
93 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
94 SET(WM_COMPAREITEM),
95 /* 0x40 - 0x5f */
96 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
97 SET(WM_NOTIFY) | SET(WM_HELP),
98 /* 0x60 - 0x7f */
99 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
100 /* 0x80 - 0x9f */
101 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
102 /* 0xa0 - 0xbf */
103 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
104 /* 0xc0 - 0xdf */
105 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
106 /* 0xe0 - 0xff */
107 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO),
108 /* 0x100 - 0x11f */
110 /* 0x120 - 0x13f */
112 /* 0x140 - 0x15f */
113 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
114 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
115 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
116 /* 0x160 - 0x17f */
118 /* 0x180 - 0x19f */
119 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
120 SET(LB_DIR) | SET(LB_FINDSTRING) |
121 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
122 /* 0x1a0 - 0x1bf */
123 SET(LB_FINDSTRINGEXACT),
124 /* 0x1c0 - 0x1df */
126 /* 0x1e0 - 0x1ff */
128 /* 0x200 - 0x21f */
129 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
130 /* 0x220 - 0x23f */
131 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
132 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
133 /* 0x240 - 0x25f */
135 /* 0x260 - 0x27f */
137 /* 0x280 - 0x29f */
139 /* 0x2a0 - 0x2bf */
141 /* 0x2c0 - 0x2df */
143 /* 0x2e0 - 0x2ff */
145 /* 0x300 - 0x31f */
146 SET(WM_ASKCBFORMATNAME)
149 /* flags for messages that contain Unicode strings */
150 static const unsigned int message_unicode_flags[] =
152 /* 0x00 - 0x1f */
153 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
154 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
155 /* 0x20 - 0x3f */
156 SET(WM_CHARTOITEM),
157 /* 0x40 - 0x5f */
159 /* 0x60 - 0x7f */
161 /* 0x80 - 0x9f */
162 SET(WM_NCCREATE),
163 /* 0xa0 - 0xbf */
165 /* 0xc0 - 0xdf */
166 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
167 /* 0xe0 - 0xff */
169 /* 0x100 - 0x11f */
170 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
171 /* 0x120 - 0x13f */
172 SET(WM_MENUCHAR),
173 /* 0x140 - 0x15f */
174 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
175 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
176 /* 0x160 - 0x17f */
178 /* 0x180 - 0x19f */
179 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
180 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
181 /* 0x1a0 - 0x1bf */
182 SET(LB_FINDSTRINGEXACT),
183 /* 0x1c0 - 0x1df */
185 /* 0x1e0 - 0x1ff */
187 /* 0x200 - 0x21f */
189 /* 0x220 - 0x23f */
190 SET(WM_MDICREATE),
191 /* 0x240 - 0x25f */
193 /* 0x260 - 0x27f */
195 /* 0x280 - 0x29f */
196 SET(WM_IME_CHAR),
197 /* 0x2a0 - 0x2bf */
199 /* 0x2c0 - 0x2df */
201 /* 0x2e0 - 0x2ff */
203 /* 0x300 - 0x31f */
204 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
207 /* check whether a given message type includes pointers */
208 inline static int is_pointer_message( UINT message )
210 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
211 return (message_pointer_flags[message / 32] & SET(message)) != 0;
214 /* check whether a given message type contains Unicode (or ASCII) chars */
215 inline static int is_unicode_message( UINT message )
217 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
218 return (message_unicode_flags[message / 32] & SET(message)) != 0;
221 #undef SET
223 /* add a data field to a packed message */
224 inline static void push_data( struct packed_message *data, const void *ptr, size_t size )
226 data->data[data->count] = ptr;
227 data->size[data->count] = size;
228 data->count++;
231 /* add a string to a packed message */
232 inline static void push_string( struct packed_message *data, LPCWSTR str )
234 push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
237 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
238 inline static void *get_data( void **buffer, size_t size )
240 void *ret = *buffer;
241 *buffer = (char *)*buffer + size;
242 return ret;
245 /* make sure that the buffer contains a valid null-terminated Unicode string */
246 inline static BOOL check_string( LPCWSTR str, size_t size )
248 for (size /= sizeof(WCHAR); size; size--, str++)
249 if (!*str) return TRUE;
250 return FALSE;
253 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
254 inline static void *get_buffer_space( void **buffer, size_t size )
256 void *ret;
257 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
258 HeapFree( GetProcessHeap(), 0, *buffer );
259 *buffer = ret;
260 return ret;
263 /* retrieve a string pointer from packed data */
264 inline static LPWSTR get_string( void **buffer )
266 return get_data( buffer, (strlenW( (LPWSTR)*buffer ) + 1) * sizeof(WCHAR) );
269 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
270 inline static BOOL combobox_has_strings( HWND hwnd )
272 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
273 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
276 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
277 inline static BOOL listbox_has_strings( HWND hwnd )
279 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
280 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
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;
293 switch(info->type)
295 case MSG_UNICODE:
296 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
297 info->flags, info->timeout, NULL );
298 break;
299 case MSG_ASCII:
300 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
301 info->flags, info->timeout, NULL );
302 break;
303 case MSG_NOTIFY:
304 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
305 break;
306 case MSG_CALLBACK:
307 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
308 info->callback, info->data );
309 break;
310 case MSG_POSTED:
311 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
312 break;
313 default:
314 ERR( "bad type %d\n", info->type );
315 break;
317 return TRUE;
321 /***********************************************************************
322 * map_wparam_AtoW
324 * Convert the wparam of an ASCII message to Unicode.
326 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
328 switch(message)
330 case WM_CHARTOITEM:
331 case EM_SETPASSWORDCHAR:
332 case WM_CHAR:
333 case WM_DEADCHAR:
334 case WM_SYSCHAR:
335 case WM_SYSDEADCHAR:
336 case WM_MENUCHAR:
338 char ch = LOWORD(wparam);
339 WCHAR wch;
340 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
341 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
343 break;
344 case WM_IME_CHAR:
346 char ch[2];
347 WCHAR wch;
348 ch[0] = (wparam >> 8);
349 ch[1] = (wparam & 0xff);
350 MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
351 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
353 break;
355 return wparam;
359 /***********************************************************************
360 * map_wparam_WtoA
362 * Convert the wparam of a Unicode message to ASCII.
364 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
366 switch(message)
368 case WM_CHARTOITEM:
369 case EM_SETPASSWORDCHAR:
370 case WM_CHAR:
371 case WM_DEADCHAR:
372 case WM_SYSCHAR:
373 case WM_SYSDEADCHAR:
374 case WM_MENUCHAR:
376 WCHAR wch = LOWORD(wparam);
377 BYTE ch;
378 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
379 wparam = MAKEWPARAM( ch, HIWORD(wparam) );
381 break;
382 case WM_IME_CHAR:
384 WCHAR wch = LOWORD(wparam);
385 BYTE ch[2];
387 ch[1] = 0;
388 WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL );
389 wparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wparam) );
391 break;
393 return wparam;
397 /***********************************************************************
398 * pack_message
400 * Pack a message for sending to another process.
401 * Return the size of the data we expect in the message reply.
402 * Set data->count to -1 if there is an error.
404 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
405 struct packed_message *data )
407 data->count = 0;
408 switch(message)
410 case WM_NCCREATE:
411 case WM_CREATE:
413 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
414 push_data( data, cs, sizeof(*cs) );
415 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
416 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
417 return sizeof(*cs);
419 case WM_GETTEXT:
420 case WM_ASKCBFORMATNAME:
421 return wparam * sizeof(WCHAR);
422 case WM_WININICHANGE:
423 if (lparam) push_string(data, (LPWSTR)lparam );
424 return 0;
425 case WM_SETTEXT:
426 case WM_DEVMODECHANGE:
427 case CB_DIR:
428 case LB_DIR:
429 case LB_ADDFILE:
430 case EM_REPLACESEL:
431 push_string( data, (LPWSTR)lparam );
432 return 0;
433 case WM_GETMINMAXINFO:
434 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
435 return sizeof(MINMAXINFO);
436 case WM_DRAWITEM:
437 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
438 return 0;
439 case WM_MEASUREITEM:
440 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
441 return sizeof(MEASUREITEMSTRUCT);
442 case WM_DELETEITEM:
443 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
444 return 0;
445 case WM_COMPAREITEM:
446 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
447 return 0;
448 case WM_WINDOWPOSCHANGING:
449 case WM_WINDOWPOSCHANGED:
450 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
451 return sizeof(WINDOWPOS);
452 case WM_COPYDATA:
454 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
455 push_data( data, cp, sizeof(*cp) );
456 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
457 return 0;
459 case WM_NOTIFY:
460 /* WM_NOTIFY cannot be sent across processes (MSDN) */
461 data->count = -1;
462 return 0;
463 case WM_HELP:
464 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
465 return 0;
466 case WM_STYLECHANGING:
467 case WM_STYLECHANGED:
468 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
469 return 0;
470 case WM_NCCALCSIZE:
471 if (!wparam)
473 push_data( data, (RECT *)lparam, sizeof(RECT) );
474 return sizeof(RECT);
476 else
478 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
479 push_data( data, nc, sizeof(*nc) );
480 push_data( data, nc->lppos, sizeof(*nc->lppos) );
481 return sizeof(*nc) + sizeof(*nc->lppos);
483 case WM_GETDLGCODE:
484 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
485 return sizeof(MSG);
486 case SBM_SETSCROLLINFO:
487 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
488 return 0;
489 case SBM_GETSCROLLINFO:
490 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
491 return sizeof(SCROLLINFO);
492 case EM_GETSEL:
493 case SBM_GETRANGE:
494 case CB_GETEDITSEL:
496 size_t size = 0;
497 if (wparam) size += sizeof(DWORD);
498 if (lparam) size += sizeof(DWORD);
499 return size;
501 case EM_GETRECT:
502 case LB_GETITEMRECT:
503 case CB_GETDROPPEDCONTROLRECT:
504 return sizeof(RECT);
505 case EM_SETRECT:
506 case EM_SETRECTNP:
507 push_data( data, (RECT *)lparam, sizeof(RECT) );
508 return 0;
509 case EM_GETLINE:
511 WORD *pw = (WORD *)lparam;
512 push_data( data, pw, sizeof(*pw) );
513 return *pw * sizeof(WCHAR);
515 case EM_SETTABSTOPS:
516 case LB_SETTABSTOPS:
517 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
518 return 0;
519 case CB_ADDSTRING:
520 case CB_INSERTSTRING:
521 case CB_FINDSTRING:
522 case CB_FINDSTRINGEXACT:
523 case CB_SELECTSTRING:
524 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
525 return 0;
526 case CB_GETLBTEXT:
527 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
528 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
529 case LB_ADDSTRING:
530 case LB_INSERTSTRING:
531 case LB_FINDSTRING:
532 case LB_FINDSTRINGEXACT:
533 case LB_SELECTSTRING:
534 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
535 return 0;
536 case LB_GETTEXT:
537 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
538 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
539 case LB_GETSELITEMS:
540 return wparam * sizeof(UINT);
541 case WM_NEXTMENU:
542 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
543 return sizeof(MDINEXTMENU);
544 case WM_SIZING:
545 case WM_MOVING:
546 push_data( data, (RECT *)lparam, sizeof(RECT) );
547 return sizeof(RECT);
548 case WM_MDICREATE:
550 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
551 push_data( data, cs, sizeof(*cs) );
552 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
553 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
554 return sizeof(*cs);
556 case WM_MDIGETACTIVE:
557 if (lparam) return sizeof(BOOL);
558 return 0;
559 case WM_WINE_SETWINDOWPOS:
560 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
561 return 0;
562 case WM_WINE_KEYBOARD_LL_HOOK:
563 push_data( data, (KBDLLHOOKSTRUCT *)lparam, sizeof(KBDLLHOOKSTRUCT) );
564 return 0;
565 case WM_WINE_MOUSE_LL_HOOK:
566 push_data( data, (MSLLHOOKSTRUCT *)lparam, sizeof(MSLLHOOKSTRUCT) );
567 return 0;
569 /* these contain an HFONT */
570 case WM_SETFONT:
571 case WM_GETFONT:
572 /* these contain an HDC */
573 case WM_PAINT:
574 case WM_ERASEBKGND:
575 case WM_ICONERASEBKGND:
576 case WM_NCPAINT:
577 case WM_CTLCOLORMSGBOX:
578 case WM_CTLCOLOREDIT:
579 case WM_CTLCOLORLISTBOX:
580 case WM_CTLCOLORBTN:
581 case WM_CTLCOLORDLG:
582 case WM_CTLCOLORSCROLLBAR:
583 case WM_CTLCOLORSTATIC:
584 case WM_PRINT:
585 case WM_PRINTCLIENT:
586 /* these contain an HGLOBAL */
587 case WM_PAINTCLIPBOARD:
588 case WM_SIZECLIPBOARD:
589 /* these contain pointers */
590 case WM_DROPOBJECT:
591 case WM_QUERYDROPOBJECT:
592 case WM_DRAGLOOP:
593 case WM_DRAGSELECT:
594 case WM_DRAGMOVE:
595 case WM_DEVICECHANGE:
596 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
597 data->count = -1;
598 return 0;
600 return 0;
604 /***********************************************************************
605 * unpack_message
607 * Unpack a message received from another process.
609 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
610 void **buffer, size_t size )
612 size_t minsize = 0;
614 switch(message)
616 case WM_NCCREATE:
617 case WM_CREATE:
619 CREATESTRUCTW *cs = *buffer;
620 WCHAR *str = (WCHAR *)(cs + 1);
621 if (size < sizeof(*cs)) return FALSE;
622 size -= sizeof(*cs);
623 if (HIWORD(cs->lpszName))
625 if (!check_string( str, size )) return FALSE;
626 cs->lpszName = str;
627 size -= (strlenW(str) + 1) * sizeof(WCHAR);
628 str += strlenW(str) + 1;
630 if (HIWORD(cs->lpszClass))
632 if (!check_string( str, size )) return FALSE;
633 cs->lpszClass = str;
635 break;
637 case WM_GETTEXT:
638 case WM_ASKCBFORMATNAME:
639 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
640 break;
641 case WM_WININICHANGE:
642 if (!*lparam) return TRUE;
643 /* fall through */
644 case WM_SETTEXT:
645 case WM_DEVMODECHANGE:
646 case CB_DIR:
647 case LB_DIR:
648 case LB_ADDFILE:
649 case EM_REPLACESEL:
650 if (!check_string( *buffer, size )) return FALSE;
651 break;
652 case WM_GETMINMAXINFO:
653 minsize = sizeof(MINMAXINFO);
654 break;
655 case WM_DRAWITEM:
656 minsize = sizeof(DRAWITEMSTRUCT);
657 break;
658 case WM_MEASUREITEM:
659 minsize = sizeof(MEASUREITEMSTRUCT);
660 break;
661 case WM_DELETEITEM:
662 minsize = sizeof(DELETEITEMSTRUCT);
663 break;
664 case WM_COMPAREITEM:
665 minsize = sizeof(COMPAREITEMSTRUCT);
666 break;
667 case WM_WINDOWPOSCHANGING:
668 case WM_WINDOWPOSCHANGED:
669 case WM_WINE_SETWINDOWPOS:
670 minsize = sizeof(WINDOWPOS);
671 break;
672 case WM_COPYDATA:
674 COPYDATASTRUCT *cp = *buffer;
675 if (size < sizeof(*cp)) return FALSE;
676 if (cp->lpData)
678 minsize = sizeof(*cp) + cp->cbData;
679 cp->lpData = cp + 1;
681 break;
683 case WM_NOTIFY:
684 /* WM_NOTIFY cannot be sent across processes (MSDN) */
685 return FALSE;
686 case WM_HELP:
687 minsize = sizeof(HELPINFO);
688 break;
689 case WM_STYLECHANGING:
690 case WM_STYLECHANGED:
691 minsize = sizeof(STYLESTRUCT);
692 break;
693 case WM_NCCALCSIZE:
694 if (!*wparam) minsize = sizeof(RECT);
695 else
697 NCCALCSIZE_PARAMS *nc = *buffer;
698 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
699 nc->lppos = (WINDOWPOS *)(nc + 1);
701 break;
702 case WM_GETDLGCODE:
703 if (!*lparam) return TRUE;
704 minsize = sizeof(MSG);
705 break;
706 case SBM_SETSCROLLINFO:
707 minsize = sizeof(SCROLLINFO);
708 break;
709 case SBM_GETSCROLLINFO:
710 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
711 break;
712 case EM_GETSEL:
713 case SBM_GETRANGE:
714 case CB_GETEDITSEL:
715 if (*wparam || *lparam)
717 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
718 if (*wparam) *wparam = (WPARAM)*buffer;
719 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
721 return TRUE;
722 case EM_GETRECT:
723 case LB_GETITEMRECT:
724 case CB_GETDROPPEDCONTROLRECT:
725 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
726 break;
727 case EM_SETRECT:
728 case EM_SETRECTNP:
729 minsize = sizeof(RECT);
730 break;
731 case EM_GETLINE:
733 WORD len;
734 if (size < sizeof(WORD)) return FALSE;
735 len = *(WORD *)*buffer;
736 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
737 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
738 return TRUE;
740 case EM_SETTABSTOPS:
741 case LB_SETTABSTOPS:
742 if (!*wparam) return TRUE;
743 minsize = *wparam * sizeof(UINT);
744 break;
745 case CB_ADDSTRING:
746 case CB_INSERTSTRING:
747 case CB_FINDSTRING:
748 case CB_FINDSTRINGEXACT:
749 case CB_SELECTSTRING:
750 case LB_ADDSTRING:
751 case LB_INSERTSTRING:
752 case LB_FINDSTRING:
753 case LB_FINDSTRINGEXACT:
754 case LB_SELECTSTRING:
755 if (!*buffer) return TRUE;
756 if (!check_string( *buffer, size )) return FALSE;
757 break;
758 case CB_GETLBTEXT:
760 size = sizeof(ULONG_PTR);
761 if (combobox_has_strings( hwnd ))
762 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
763 if (!get_buffer_space( buffer, size )) return FALSE;
764 break;
766 case LB_GETTEXT:
768 size = sizeof(ULONG_PTR);
769 if (listbox_has_strings( hwnd ))
770 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
771 if (!get_buffer_space( buffer, size )) return FALSE;
772 break;
774 case LB_GETSELITEMS:
775 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
776 break;
777 case WM_NEXTMENU:
778 minsize = sizeof(MDINEXTMENU);
779 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
780 break;
781 case WM_SIZING:
782 case WM_MOVING:
783 minsize = sizeof(RECT);
784 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
785 break;
786 case WM_MDICREATE:
788 MDICREATESTRUCTW *cs = *buffer;
789 WCHAR *str = (WCHAR *)(cs + 1);
790 if (size < sizeof(*cs)) return FALSE;
791 size -= sizeof(*cs);
792 if (HIWORD(cs->szTitle))
794 if (!check_string( str, size )) return FALSE;
795 cs->szTitle = str;
796 size -= (strlenW(str) + 1) * sizeof(WCHAR);
797 str += strlenW(str) + 1;
799 if (HIWORD(cs->szClass))
801 if (!check_string( str, size )) return FALSE;
802 cs->szClass = str;
804 break;
806 case WM_MDIGETACTIVE:
807 if (!*lparam) return TRUE;
808 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
809 break;
810 case WM_WINE_KEYBOARD_LL_HOOK:
811 minsize = sizeof(KBDLLHOOKSTRUCT);
812 break;
813 case WM_WINE_MOUSE_LL_HOOK:
814 minsize = sizeof(MSLLHOOKSTRUCT);
815 break;
817 /* these contain an HFONT */
818 case WM_SETFONT:
819 case WM_GETFONT:
820 /* these contain an HDC */
821 case WM_PAINT:
822 case WM_ERASEBKGND:
823 case WM_ICONERASEBKGND:
824 case WM_NCPAINT:
825 case WM_CTLCOLORMSGBOX:
826 case WM_CTLCOLOREDIT:
827 case WM_CTLCOLORLISTBOX:
828 case WM_CTLCOLORBTN:
829 case WM_CTLCOLORDLG:
830 case WM_CTLCOLORSCROLLBAR:
831 case WM_CTLCOLORSTATIC:
832 case WM_PRINT:
833 case WM_PRINTCLIENT:
834 /* these contain an HGLOBAL */
835 case WM_PAINTCLIPBOARD:
836 case WM_SIZECLIPBOARD:
837 /* these contain pointers */
838 case WM_DROPOBJECT:
839 case WM_QUERYDROPOBJECT:
840 case WM_DRAGLOOP:
841 case WM_DRAGSELECT:
842 case WM_DRAGMOVE:
843 case WM_DEVICECHANGE:
844 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
845 return FALSE;
847 default:
848 return TRUE; /* message doesn't need any unpacking */
851 /* default exit for most messages: check minsize and store buffer in lparam */
852 if (size < minsize) return FALSE;
853 *lparam = (LPARAM)*buffer;
854 return TRUE;
858 /***********************************************************************
859 * pack_reply
861 * Pack a reply to a message for sending to another process.
863 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
864 LRESULT res, struct packed_message *data )
866 data->count = 0;
867 switch(message)
869 case WM_NCCREATE:
870 case WM_CREATE:
871 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
872 break;
873 case WM_GETTEXT:
874 case CB_GETLBTEXT:
875 case LB_GETTEXT:
876 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
877 break;
878 case WM_GETMINMAXINFO:
879 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
880 break;
881 case WM_MEASUREITEM:
882 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
883 break;
884 case WM_WINDOWPOSCHANGING:
885 case WM_WINDOWPOSCHANGED:
886 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
887 break;
888 case WM_GETDLGCODE:
889 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
890 break;
891 case SBM_GETSCROLLINFO:
892 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
893 break;
894 case EM_GETRECT:
895 case LB_GETITEMRECT:
896 case CB_GETDROPPEDCONTROLRECT:
897 case WM_SIZING:
898 case WM_MOVING:
899 push_data( data, (RECT *)lparam, sizeof(RECT) );
900 break;
901 case EM_GETLINE:
903 WORD *ptr = (WORD *)lparam;
904 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
905 break;
907 case LB_GETSELITEMS:
908 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
909 break;
910 case WM_MDIGETACTIVE:
911 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
912 break;
913 case WM_NCCALCSIZE:
914 if (!wparam)
915 push_data( data, (RECT *)lparam, sizeof(RECT) );
916 else
918 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
919 push_data( data, nc, sizeof(*nc) );
920 push_data( data, nc->lppos, sizeof(*nc->lppos) );
922 break;
923 case EM_GETSEL:
924 case SBM_GETRANGE:
925 case CB_GETEDITSEL:
926 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
927 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
928 break;
929 case WM_NEXTMENU:
930 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
931 break;
932 case WM_MDICREATE:
933 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
934 break;
935 case WM_ASKCBFORMATNAME:
936 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
937 break;
942 /***********************************************************************
943 * unpack_reply
945 * Unpack a message reply received from another process.
947 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
948 void *buffer, size_t size )
950 switch(message)
952 case WM_NCCREATE:
953 case WM_CREATE:
955 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
956 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
957 memcpy( cs, buffer, min( sizeof(*cs), size ));
958 cs->lpszName = name; /* restore the original pointers */
959 cs->lpszClass = class;
960 break;
962 case WM_GETTEXT:
963 case WM_ASKCBFORMATNAME:
964 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
965 break;
966 case WM_GETMINMAXINFO:
967 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
968 break;
969 case WM_MEASUREITEM:
970 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
971 break;
972 case WM_WINDOWPOSCHANGING:
973 case WM_WINDOWPOSCHANGED:
974 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
975 break;
976 case WM_GETDLGCODE:
977 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
978 break;
979 case SBM_GETSCROLLINFO:
980 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
981 break;
982 case EM_GETRECT:
983 case CB_GETDROPPEDCONTROLRECT:
984 case LB_GETITEMRECT:
985 case WM_SIZING:
986 case WM_MOVING:
987 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
988 break;
989 case EM_GETLINE:
990 size = min( size, (size_t)*(WORD *)lparam );
991 memcpy( (WCHAR *)lparam, buffer, size );
992 break;
993 case LB_GETSELITEMS:
994 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
995 break;
996 case LB_GETTEXT:
997 case CB_GETLBTEXT:
998 memcpy( (WCHAR *)lparam, buffer, size );
999 break;
1000 case WM_NEXTMENU:
1001 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
1002 break;
1003 case WM_MDIGETACTIVE:
1004 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
1005 break;
1006 case WM_NCCALCSIZE:
1007 if (!wparam)
1008 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1009 else
1011 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1012 WINDOWPOS *wp = nc->lppos;
1013 memcpy( nc, buffer, min( sizeof(*nc), size ));
1014 if (size > sizeof(*nc))
1016 size -= sizeof(*nc);
1017 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
1019 nc->lppos = wp; /* restore the original pointer */
1021 break;
1022 case EM_GETSEL:
1023 case SBM_GETRANGE:
1024 case CB_GETEDITSEL:
1025 if (wparam)
1027 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1028 if (size <= sizeof(DWORD)) break;
1029 size -= sizeof(DWORD);
1030 buffer = (DWORD *)buffer + 1;
1032 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1033 break;
1034 case WM_MDICREATE:
1036 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
1037 LPCWSTR title = cs->szTitle, class = cs->szClass;
1038 memcpy( cs, buffer, min( sizeof(*cs), size ));
1039 cs->szTitle = title; /* restore the original pointers */
1040 cs->szClass = class;
1041 break;
1043 default:
1044 ERR( "should not happen: unexpected message %x\n", message );
1045 break;
1050 /***********************************************************************
1051 * reply_message
1053 * Send a reply to a sent message.
1055 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1057 struct packed_message data;
1058 int i, replied = info->flags & ISMEX_REPLIED;
1060 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1061 if (!remove && replied) return; /* replied already */
1063 data.count = 0;
1064 info->flags |= ISMEX_REPLIED;
1066 if (info->type == MSG_OTHER_PROCESS && !replied)
1068 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1069 info->msg.lParam, result, &data );
1072 SERVER_START_REQ( reply_message )
1074 req->type = info->type;
1075 req->result = result;
1076 req->remove = remove;
1077 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1078 wine_server_call( req );
1080 SERVER_END_REQ;
1084 /***********************************************************************
1085 * handle_internal_message
1087 * Handle an internal Wine message instead of calling the window proc.
1089 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1091 if (hwnd == GetDesktopWindow()) return 0;
1092 switch(msg)
1094 case WM_WINE_DESTROYWINDOW:
1095 return WIN_DestroyWindow( hwnd );
1096 case WM_WINE_SETWINDOWPOS:
1097 return USER_Driver.pSetWindowPos( (WINDOWPOS *)lparam );
1098 case WM_WINE_SHOWWINDOW:
1099 return ShowWindow( hwnd, wparam );
1100 case WM_WINE_SETPARENT:
1101 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1102 case WM_WINE_SETWINDOWLONG:
1103 return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1104 case WM_WINE_ENABLEWINDOW:
1105 return EnableWindow( hwnd, wparam );
1106 case WM_WINE_SETACTIVEWINDOW:
1107 return (LRESULT)SetActiveWindow( (HWND)wparam );
1108 case WM_WINE_KEYBOARD_LL_HOOK:
1109 return HOOK_CallHooks( WH_KEYBOARD_LL, HC_ACTION, wparam, lparam, TRUE );
1110 case WM_WINE_MOUSE_LL_HOOK:
1111 return HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, wparam, lparam, TRUE );
1112 default:
1113 FIXME( "unknown internal message %x\n", msg );
1114 return 0;
1118 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1119 * to the memory handle, we keep track (in the server side) of all pairs of handle
1120 * used (the client passes its value and the content of the memory handle), and
1121 * the server stored both values (the client, and the local one, created after the
1122 * content). When a ACK message is generated, the list of pair is searched for a
1123 * matching pair, so that the client memory handle can be returned.
1125 struct DDE_pair {
1126 HGLOBAL client_hMem;
1127 HGLOBAL server_hMem;
1130 static struct DDE_pair* dde_pairs;
1131 static int dde_num_alloc;
1132 static int dde_num_used;
1134 static CRITICAL_SECTION dde_crst;
1135 static CRITICAL_SECTION_DEBUG critsect_debug =
1137 0, 0, &dde_crst,
1138 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
1139 0, 0, { 0, (DWORD)(__FILE__ ": dde_crst") }
1141 static CRITICAL_SECTION dde_crst = { &critsect_debug, -1, 0, 0, 0, 0 };
1143 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1145 int i;
1146 #define GROWBY 4
1148 EnterCriticalSection(&dde_crst);
1150 /* now remember the pair of hMem on both sides */
1151 if (dde_num_used == dde_num_alloc)
1153 struct DDE_pair* tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1154 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1155 if (!tmp)
1157 LeaveCriticalSection(&dde_crst);
1158 return FALSE;
1160 dde_pairs = tmp;
1161 /* zero out newly allocated part */
1162 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1163 dde_num_alloc += GROWBY;
1165 #undef GROWBY
1166 for (i = 0; i < dde_num_alloc; i++)
1168 if (dde_pairs[i].server_hMem == 0)
1170 dde_pairs[i].client_hMem = chm;
1171 dde_pairs[i].server_hMem = shm;
1172 dde_num_used++;
1173 break;
1176 LeaveCriticalSection(&dde_crst);
1177 return TRUE;
1180 static HGLOBAL dde_get_pair(HGLOBAL shm)
1182 int i;
1183 HGLOBAL ret = 0;
1185 EnterCriticalSection(&dde_crst);
1186 for (i = 0; i < dde_num_alloc; i++)
1188 if (dde_pairs[i].server_hMem == shm)
1190 /* free this pair */
1191 dde_pairs[i].server_hMem = 0;
1192 dde_num_used--;
1193 ret = dde_pairs[i].client_hMem;
1194 break;
1197 LeaveCriticalSection(&dde_crst);
1198 return ret;
1201 /***********************************************************************
1202 * post_dde_message
1204 * Post a DDE message
1206 static BOOL post_dde_message( DWORD dest_tid, struct packed_message *data, const struct send_message_info *info )
1208 void* ptr = NULL;
1209 int size = 0;
1210 UINT uiLo, uiHi;
1211 LPARAM lp = 0;
1212 HGLOBAL hunlock = 0;
1213 int i;
1214 DWORD res;
1216 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1217 return FALSE;
1219 lp = info->lparam;
1220 switch (info->msg)
1222 /* DDE messages which don't require packing are:
1223 * WM_DDE_INITIATE
1224 * WM_DDE_TERMINATE
1225 * WM_DDE_REQUEST
1226 * WM_DDE_UNADVISE
1228 case WM_DDE_ACK:
1229 if (HIWORD(uiHi))
1231 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1232 HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1233 if (h)
1235 /* send back the value of h on the other side */
1236 push_data( data, &h, sizeof(HGLOBAL) );
1237 lp = uiLo;
1238 TRACE( "send dde-ack %x %08x => %08lx\n", uiLo, uiHi, (DWORD)h );
1241 else
1243 /* uiHi should contain either an atom or 0 */
1244 TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
1245 lp = MAKELONG( uiLo, uiHi );
1247 break;
1248 case WM_DDE_ADVISE:
1249 case WM_DDE_DATA:
1250 case WM_DDE_POKE:
1251 size = 0;
1252 if (uiLo)
1254 size = GlobalSize( (HGLOBAL)uiLo ) ;
1255 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1256 (info->msg == WM_DDE_DATA && size < sizeof(DDEDATA)) ||
1257 (info->msg == WM_DDE_POKE && size < sizeof(DDEPOKE))
1259 return FALSE;
1261 else if (info->msg != WM_DDE_DATA) return FALSE;
1263 lp = uiHi;
1264 if (uiLo)
1266 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1268 DDEDATA *dde_data = (DDEDATA *)ptr;
1269 TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
1270 dde_data->unused, dde_data->fResponse, dde_data->fRelease,
1271 dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
1272 push_data( data, ptr, size );
1273 hunlock = (HGLOBAL)uiLo;
1276 TRACE( "send ddepack %u %x\n", size, uiHi );
1277 break;
1278 case WM_DDE_EXECUTE:
1279 if (info->lparam)
1281 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1283 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1284 /* so that the other side can send it back on ACK */
1285 lp = info->lparam;
1286 hunlock = (HGLOBAL)info->lparam;
1289 break;
1291 SERVER_START_REQ( send_message )
1293 req->id = dest_tid;
1294 req->type = info->type;
1295 req->flags = 0;
1296 req->win = info->hwnd;
1297 req->msg = info->msg;
1298 req->wparam = info->wparam;
1299 req->lparam = lp;
1300 req->time = GetCurrentTime();
1301 req->timeout = -1;
1302 for (i = 0; i < data->count; i++)
1303 wine_server_add_data( req, data->data[i], data->size[i] );
1304 if ((res = wine_server_call( req )))
1306 if (res == STATUS_INVALID_PARAMETER)
1307 /* FIXME: find a STATUS_ value for this one */
1308 SetLastError( ERROR_INVALID_THREAD_ID );
1309 else
1310 SetLastError( RtlNtStatusToDosError(res) );
1312 else
1313 FreeDDElParam(info->msg, info->lparam);
1315 SERVER_END_REQ;
1316 if (hunlock) GlobalUnlock(hunlock);
1318 return !res;
1321 /***********************************************************************
1322 * unpack_dde_message
1324 * Unpack a posted DDE message received from another process.
1326 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1327 void **buffer, size_t size )
1329 UINT uiLo, uiHi;
1330 HGLOBAL hMem = 0;
1331 void* ptr;
1333 switch (message)
1335 case WM_DDE_ACK:
1336 if (size)
1338 /* hMem is being passed */
1339 if (size != sizeof(HGLOBAL)) return FALSE;
1340 if (!buffer || !*buffer) return FALSE;
1341 uiLo = *lparam;
1342 memcpy( &hMem, *buffer, size );
1343 uiHi = (UINT)hMem;
1344 TRACE("recv dde-ack %x mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1346 else
1348 uiLo = LOWORD( *lparam );
1349 uiHi = HIWORD( *lparam );
1350 TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
1352 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1353 break;
1354 case WM_DDE_ADVISE:
1355 case WM_DDE_DATA:
1356 case WM_DDE_POKE:
1357 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1358 uiHi = *lparam;
1359 TRACE( "recv ddepack %u %x\n", size, uiHi );
1360 if (size)
1362 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1363 if (hMem && (ptr = GlobalLock( hMem )))
1365 memcpy( ptr, *buffer, size );
1366 GlobalUnlock( hMem );
1368 else return FALSE;
1370 uiLo = (UINT)hMem;
1372 *lparam = PackDDElParam( message, uiLo, uiHi );
1373 break;
1374 case WM_DDE_EXECUTE:
1375 if (size)
1377 if (!buffer || !*buffer) return FALSE;
1378 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1379 if (hMem && (ptr = GlobalLock( hMem )))
1381 memcpy( ptr, *buffer, size );
1382 GlobalUnlock( hMem );
1383 TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
1384 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1386 GlobalFree( hMem );
1387 return FALSE;
1390 } else return FALSE;
1391 *lparam = (LPARAM)hMem;
1392 break;
1394 return TRUE;
1397 /***********************************************************************
1398 * call_window_proc
1400 * Call a window procedure and the corresponding hooks.
1402 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1403 BOOL unicode, BOOL same_thread )
1405 LRESULT result = 0;
1406 WNDPROC winproc;
1407 CWPSTRUCT cwp;
1408 CWPRETSTRUCT cwpret;
1409 MESSAGEQUEUE *queue = QUEUE_Current();
1411 if (queue->recursion_count > MAX_SENDMSG_RECURSION) return 0;
1412 queue->recursion_count++;
1414 if (msg & 0x80000000)
1416 result = handle_internal_message( hwnd, msg, wparam, lparam );
1417 goto done;
1420 /* first the WH_CALLWNDPROC hook */
1421 hwnd = WIN_GetFullHandle( hwnd );
1422 cwp.lParam = lparam;
1423 cwp.wParam = wparam;
1424 cwp.message = msg;
1425 cwp.hwnd = hwnd;
1426 HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1428 /* now call the window procedure */
1429 if (unicode)
1431 if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) goto done;
1432 result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1434 else
1436 if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) goto done;
1437 result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1440 /* and finally the WH_CALLWNDPROCRET hook */
1441 cwpret.lResult = result;
1442 cwpret.lParam = lparam;
1443 cwpret.wParam = wparam;
1444 cwpret.message = msg;
1445 cwpret.hwnd = hwnd;
1446 HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1447 done:
1448 queue->recursion_count--;
1449 return result;
1453 /***********************************************************************
1454 * process_hardware_message
1456 * Process a hardware message; return TRUE if message should be passed on to the app
1458 static BOOL process_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd,
1459 UINT first, UINT last, BOOL remove )
1461 BOOL ret;
1463 if (!MSG_process_raw_hardware_message( msg, extra_info, hwnd, first, last, remove ))
1464 return FALSE;
1466 ret = MSG_process_cooked_hardware_message( msg, extra_info, remove );
1468 /* tell the server we have passed it to the app
1469 * (even though we may end up dropping it later on)
1471 SERVER_START_REQ( reply_message )
1473 req->type = MSG_HARDWARE;
1474 req->result = 0;
1475 req->remove = remove || !ret;
1476 wine_server_call( req );
1478 SERVER_END_REQ;
1479 return ret;
1483 /***********************************************************************
1484 * call_sendmsg_callback
1486 * Call the callback function of SendMessageCallback.
1488 static inline void call_sendmsg_callback( SENDASYNCPROC callback, HWND hwnd, UINT msg,
1489 ULONG_PTR data, LRESULT result )
1491 if (TRACE_ON(relay))
1492 DPRINTF( "%04lx:Call message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1493 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
1494 data, result );
1495 callback( hwnd, msg, data, result );
1496 if (TRACE_ON(relay))
1497 DPRINTF( "%04lx:Ret message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1498 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
1499 data, result );
1503 /***********************************************************************
1504 * MSG_peek_message
1506 * Peek for a message matching the given parameters. Return FALSE if none available.
1507 * All pending sent messages are processed before returning.
1509 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1511 LRESULT result;
1512 ULONG_PTR extra_info = 0;
1513 MESSAGEQUEUE *queue = QUEUE_Current();
1514 struct received_message_info info, *old_info;
1516 if (!first && !last) last = ~0;
1518 for (;;)
1520 NTSTATUS res;
1521 void *buffer = NULL;
1522 size_t size = 0, buffer_size = 0;
1524 do /* loop while buffer is too small */
1526 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1527 return FALSE;
1528 SERVER_START_REQ( get_message )
1530 req->flags = flags;
1531 req->get_win = hwnd;
1532 req->get_first = first;
1533 req->get_last = last;
1534 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1535 if (!(res = wine_server_call( req )))
1537 size = wine_server_reply_size( reply );
1538 info.type = reply->type;
1539 info.msg.hwnd = reply->win;
1540 info.msg.message = reply->msg;
1541 info.msg.wParam = reply->wparam;
1542 info.msg.lParam = reply->lparam;
1543 info.msg.time = reply->time;
1544 info.msg.pt.x = reply->x;
1545 info.msg.pt.y = reply->y;
1546 extra_info = reply->info;
1548 else
1550 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1551 buffer_size = reply->total;
1554 SERVER_END_REQ;
1555 } while (res == STATUS_BUFFER_OVERFLOW);
1557 if (res) return FALSE;
1559 TRACE( "got type %d msg %x (%s) hwnd %p wp %x lp %lx\n",
1560 info.type, info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1561 info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1563 switch(info.type)
1565 case MSG_ASCII:
1566 case MSG_UNICODE:
1567 info.flags = ISMEX_SEND;
1568 break;
1569 case MSG_NOTIFY:
1570 info.flags = ISMEX_NOTIFY;
1571 break;
1572 case MSG_CALLBACK:
1573 info.flags = ISMEX_CALLBACK;
1574 break;
1575 case MSG_CALLBACK_RESULT:
1576 call_sendmsg_callback( (SENDASYNCPROC)info.msg.wParam, info.msg.hwnd,
1577 info.msg.message, extra_info, info.msg.lParam );
1578 goto next;
1579 case MSG_OTHER_PROCESS:
1580 info.flags = ISMEX_SEND;
1581 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1582 &info.msg.lParam, &buffer, size ))
1584 ERR( "invalid packed message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1585 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
1586 info.msg.wParam, info.msg.lParam, size );
1587 /* ignore it */
1588 reply_message( &info, 0, TRUE );
1589 goto next;
1591 break;
1592 case MSG_HARDWARE:
1593 if (!process_hardware_message( &info.msg, extra_info,
1594 hwnd, first, last, flags & GET_MSG_REMOVE ))
1596 TRACE("dropping msg %x\n", info.msg.message );
1597 goto next; /* ignore it */
1599 queue->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
1600 /* fall through */
1601 case MSG_POSTED:
1602 queue->GetMessageExtraInfoVal = extra_info;
1603 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
1605 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1606 &info.msg.lParam, &buffer, size ))
1608 ERR( "invalid packed dde-message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1609 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1610 info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
1611 goto next; /* ignore it */
1614 *msg = info.msg;
1615 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1616 return TRUE;
1619 /* if we get here, we have a sent message; call the window procedure */
1620 old_info = queue->receive_info;
1621 queue->receive_info = &info;
1622 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1623 info.msg.lParam, (info.type != MSG_ASCII), FALSE );
1624 reply_message( &info, result, TRUE );
1625 queue->receive_info = old_info;
1626 next:
1627 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1632 /***********************************************************************
1633 * wait_message_reply
1635 * Wait until a sent message gets replied to.
1637 static void wait_message_reply( UINT flags )
1639 MESSAGEQUEUE *queue;
1641 if (!(queue = QUEUE_Current())) return;
1643 for (;;)
1645 unsigned int wake_bits = 0, changed_bits = 0;
1646 DWORD dwlc, res;
1648 SERVER_START_REQ( set_queue_mask )
1650 req->wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
1651 req->changed_mask = req->wake_mask;
1652 req->skip_wait = 1;
1653 if (!wine_server_call( req ))
1655 wake_bits = reply->wake_bits;
1656 changed_bits = reply->changed_bits;
1659 SERVER_END_REQ;
1661 if (wake_bits & QS_SMRESULT) return; /* got a result */
1662 if (wake_bits & QS_SENDMESSAGE)
1664 /* Process the sent message immediately */
1665 MSG msg;
1666 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1667 continue;
1670 /* now wait for it */
1672 ReleaseThunkLock( &dwlc );
1674 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1675 res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1676 INFINITE, 0, 0 );
1677 else
1678 res = WaitForSingleObject( queue->server_queue, INFINITE );
1680 if (dwlc) RestoreThunkLock( dwlc );
1684 /***********************************************************************
1685 * put_message_in_queue
1687 * Put a sent message into the destination queue.
1688 * For inter-process message, reply_size is set to expected size of reply data.
1690 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1691 size_t *reply_size )
1693 struct packed_message data;
1694 unsigned int res;
1695 int i, timeout = -1;
1697 if (info->type != MSG_NOTIFY &&
1698 info->type != MSG_CALLBACK &&
1699 info->type != MSG_POSTED &&
1700 info->timeout != INFINITE)
1701 timeout = info->timeout;
1703 data.count = 0;
1704 if (info->type == MSG_OTHER_PROCESS)
1706 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1707 if (data.count == -1)
1709 WARN( "cannot pack message %x\n", info->msg );
1710 return FALSE;
1713 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
1715 return post_dde_message( dest_tid, &data, info );
1718 SERVER_START_REQ( send_message )
1720 req->id = dest_tid;
1721 req->type = info->type;
1722 req->flags = 0;
1723 req->win = info->hwnd;
1724 req->msg = info->msg;
1725 req->wparam = info->wparam;
1726 req->lparam = info->lparam;
1727 req->time = GetCurrentTime();
1728 req->timeout = timeout;
1730 if (info->type == MSG_CALLBACK)
1732 req->callback = info->callback;
1733 req->info = info->data;
1736 if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
1737 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1738 if ((res = wine_server_call( req )))
1740 if (res == STATUS_INVALID_PARAMETER)
1741 /* FIXME: find a STATUS_ value for this one */
1742 SetLastError( ERROR_INVALID_THREAD_ID );
1743 else
1744 SetLastError( RtlNtStatusToDosError(res) );
1747 SERVER_END_REQ;
1748 return !res;
1752 /***********************************************************************
1753 * retrieve_reply
1755 * Retrieve a message reply from the server.
1757 static LRESULT retrieve_reply( const struct send_message_info *info,
1758 size_t reply_size, LRESULT *result )
1760 NTSTATUS status;
1761 void *reply_data = NULL;
1763 if (reply_size)
1765 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
1767 WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
1768 reply_size = 0;
1771 SERVER_START_REQ( get_message_reply )
1773 req->cancel = 1;
1774 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
1775 if (!(status = wine_server_call( req ))) *result = reply->result;
1776 reply_size = wine_server_reply_size( reply );
1778 SERVER_END_REQ;
1779 if (!status && reply_size)
1780 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
1782 if (reply_data) HeapFree( GetProcessHeap(), 0, reply_data );
1784 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1785 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
1786 info->lparam, *result, status );
1788 /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
1789 if (status) SetLastError( RtlNtStatusToDosError(status) );
1790 return !status;
1794 /***********************************************************************
1795 * send_inter_thread_message
1797 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1798 LRESULT *res_ptr )
1800 LRESULT ret;
1801 int locks;
1802 size_t reply_size = 0;
1804 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
1805 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
1807 if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1809 /* there's no reply to wait for on notify/callback messages */
1810 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1812 locks = WIN_SuspendWndsLock();
1814 wait_message_reply( info->flags );
1815 ret = retrieve_reply( info, reply_size, res_ptr );
1817 WIN_RestoreWndsLock( locks );
1818 return ret;
1822 /***********************************************************************
1823 * MSG_SendInternalMessageTimeout
1825 * Same as SendMessageTimeoutW but sends the message to a specific thread
1826 * without requiring a window handle. Only works for internal Wine messages.
1828 LRESULT MSG_SendInternalMessageTimeout( DWORD dest_pid, DWORD dest_tid,
1829 UINT msg, WPARAM wparam, LPARAM lparam,
1830 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1832 struct send_message_info info;
1833 LRESULT ret, result;
1835 assert( msg & 0x80000000 ); /* must be an internal Wine message */
1837 info.type = MSG_UNICODE;
1838 info.hwnd = 0;
1839 info.msg = msg;
1840 info.wparam = wparam;
1841 info.lparam = lparam;
1842 info.flags = flags;
1843 info.timeout = timeout;
1845 if (USER_IsExitingThread( dest_tid )) return 0;
1847 if (dest_tid == GetCurrentThreadId())
1849 result = handle_internal_message( 0, msg, wparam, lparam );
1850 ret = 1;
1852 else
1854 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1855 ret = send_inter_thread_message( dest_tid, &info, &result );
1857 if (ret && res_ptr) *res_ptr = result;
1858 return ret;
1862 /***********************************************************************
1863 * SendMessageTimeoutW (USER32.@)
1865 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1866 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1868 struct send_message_info info;
1869 DWORD dest_tid, dest_pid;
1870 LRESULT ret, result;
1872 info.type = MSG_UNICODE;
1873 info.hwnd = hwnd;
1874 info.msg = msg;
1875 info.wparam = wparam;
1876 info.lparam = lparam;
1877 info.flags = flags;
1878 info.timeout = timeout;
1880 if (is_broadcast(hwnd))
1882 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1883 if (res_ptr) *res_ptr = 1;
1884 return 1;
1887 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1889 if (USER_IsExitingThread( dest_tid )) return 0;
1891 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1893 if (dest_tid == GetCurrentThreadId())
1895 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1896 ret = 1;
1898 else
1900 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1901 ret = send_inter_thread_message( dest_tid, &info, &result );
1904 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1905 if (ret && res_ptr) *res_ptr = result;
1906 return ret;
1910 /***********************************************************************
1911 * SendMessageTimeoutA (USER32.@)
1913 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1914 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1916 struct send_message_info info;
1917 DWORD dest_tid, dest_pid;
1918 LRESULT ret, result;
1920 info.type = MSG_ASCII;
1921 info.hwnd = hwnd;
1922 info.msg = msg;
1923 info.wparam = wparam;
1924 info.lparam = lparam;
1925 info.flags = flags;
1926 info.timeout = timeout;
1928 if (is_broadcast(hwnd))
1930 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1931 if (res_ptr) *res_ptr = 1;
1932 return 1;
1935 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1937 if (USER_IsExitingThread( dest_tid )) return 0;
1939 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1941 if (dest_tid == GetCurrentThreadId())
1943 result = call_window_proc( hwnd, msg, wparam, lparam, FALSE, TRUE );
1944 ret = 1;
1946 else if (dest_pid == GetCurrentProcessId())
1948 ret = send_inter_thread_message( dest_tid, &info, &result );
1950 else
1952 /* inter-process message: need to map to Unicode */
1953 info.type = MSG_OTHER_PROCESS;
1954 if (is_unicode_message( info.msg ))
1956 if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1957 return 0;
1958 ret = send_inter_thread_message( dest_tid, &info, &result );
1959 result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1960 info.lparam, result );
1962 else ret = send_inter_thread_message( dest_tid, &info, &result );
1964 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1965 if (ret && res_ptr) *res_ptr = result;
1966 return ret;
1970 /***********************************************************************
1971 * SendMessageW (USER32.@)
1973 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1975 LRESULT res = 0;
1976 SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1977 return res;
1981 /***********************************************************************
1982 * SendMessageA (USER32.@)
1984 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1986 LRESULT res = 0;
1987 SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1988 return res;
1992 /***********************************************************************
1993 * SendNotifyMessageA (USER32.@)
1995 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1997 return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
2001 /***********************************************************************
2002 * SendNotifyMessageW (USER32.@)
2004 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2006 struct send_message_info info;
2007 DWORD dest_tid;
2008 LRESULT result;
2010 if (is_pointer_message(msg))
2012 SetLastError(ERROR_INVALID_PARAMETER);
2013 return FALSE;
2016 info.type = MSG_NOTIFY;
2017 info.hwnd = hwnd;
2018 info.msg = msg;
2019 info.wparam = wparam;
2020 info.lparam = lparam;
2022 if (is_broadcast(hwnd))
2024 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2025 return TRUE;
2028 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2030 if (USER_IsExitingThread( dest_tid )) return TRUE;
2032 if (dest_tid == GetCurrentThreadId())
2034 call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2035 return TRUE;
2037 return send_inter_thread_message( dest_tid, &info, &result );
2041 /***********************************************************************
2042 * SendMessageCallbackA (USER32.@)
2044 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2045 SENDASYNCPROC callback, ULONG_PTR data )
2047 return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
2048 lparam, callback, data );
2052 /***********************************************************************
2053 * SendMessageCallbackW (USER32.@)
2055 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2056 SENDASYNCPROC callback, ULONG_PTR data )
2058 struct send_message_info info;
2059 LRESULT result;
2060 DWORD dest_tid;
2062 if (is_pointer_message(msg))
2064 SetLastError(ERROR_INVALID_PARAMETER);
2065 return FALSE;
2068 info.type = MSG_CALLBACK;
2069 info.hwnd = hwnd;
2070 info.msg = msg;
2071 info.wparam = wparam;
2072 info.lparam = lparam;
2073 info.callback = callback;
2074 info.data = data;
2076 if (is_broadcast(hwnd))
2078 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2079 return TRUE;
2082 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2084 if (USER_IsExitingThread( dest_tid )) return TRUE;
2086 if (dest_tid == GetCurrentThreadId())
2088 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2089 call_sendmsg_callback( callback, hwnd, msg, data, result );
2090 return TRUE;
2092 FIXME( "callback will not be called\n" );
2093 return send_inter_thread_message( dest_tid, &info, &result );
2097 /***********************************************************************
2098 * ReplyMessage (USER32.@)
2100 BOOL WINAPI ReplyMessage( LRESULT result )
2102 MESSAGEQUEUE *queue = QUEUE_Current();
2103 struct received_message_info *info = queue->receive_info;
2105 if (!info) return FALSE;
2106 reply_message( info, result, FALSE );
2107 return TRUE;
2111 /***********************************************************************
2112 * InSendMessage (USER32.@)
2114 BOOL WINAPI InSendMessage(void)
2116 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
2120 /***********************************************************************
2121 * InSendMessageEx (USER32.@)
2123 DWORD WINAPI InSendMessageEx( LPVOID reserved )
2125 MESSAGEQUEUE *queue = QUEUE_Current();
2126 struct received_message_info *info = queue->receive_info;
2128 if (info) return info->flags;
2129 return ISMEX_NOSEND;
2133 /***********************************************************************
2134 * PostMessageA (USER32.@)
2136 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2138 return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
2142 /***********************************************************************
2143 * PostMessageW (USER32.@)
2145 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2147 struct send_message_info info;
2148 DWORD dest_tid;
2150 if (is_pointer_message( msg ))
2152 SetLastError( ERROR_INVALID_PARAMETER );
2153 return FALSE;
2156 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
2157 hwnd, msg, SPY_GetMsgName(msg, hwnd), wparam, lparam );
2159 info.type = MSG_POSTED;
2160 info.hwnd = hwnd;
2161 info.msg = msg;
2162 info.wparam = wparam;
2163 info.lparam = lparam;
2165 if (is_broadcast(hwnd))
2167 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2168 return TRUE;
2171 if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2173 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2175 if (USER_IsExitingThread( dest_tid )) return TRUE;
2177 return put_message_in_queue( dest_tid, &info, NULL );
2181 /**********************************************************************
2182 * PostThreadMessageA (USER32.@)
2184 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2186 return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2190 /**********************************************************************
2191 * PostThreadMessageW (USER32.@)
2193 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2195 struct send_message_info info;
2197 if (is_pointer_message( msg ))
2199 SetLastError( ERROR_INVALID_PARAMETER );
2200 return FALSE;
2202 if (USER_IsExitingThread( thread )) return TRUE;
2204 info.type = MSG_POSTED;
2205 info.hwnd = 0;
2206 info.msg = msg;
2207 info.wparam = wparam;
2208 info.lparam = lparam;
2209 return put_message_in_queue( thread, &info, NULL );
2213 /***********************************************************************
2214 * PostQuitMessage (USER32.@)
2216 void WINAPI PostQuitMessage( INT exitCode )
2218 PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
2222 /***********************************************************************
2223 * PeekMessageW (USER32.@)
2225 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2227 MESSAGEQUEUE *queue;
2228 MSG msg;
2229 int locks;
2231 /* check for graphics events */
2232 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2233 USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
2235 hwnd = WIN_GetFullHandle( hwnd );
2236 locks = WIN_SuspendWndsLock();
2238 if (!MSG_peek_message( &msg, hwnd, first, last,
2239 (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2241 if (!(flags & PM_NOYIELD))
2243 DWORD count;
2244 ReleaseThunkLock(&count);
2245 if (count) RestoreThunkLock(count);
2247 WIN_RestoreWndsLock( locks );
2248 return FALSE;
2251 WIN_RestoreWndsLock( locks );
2253 /* need to fill the window handle for WM_PAINT message */
2254 if (msg.message == WM_PAINT)
2256 if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
2258 msg.message = WM_PAINTICON;
2259 msg.wParam = 1;
2261 /* clear internal paint flag */
2262 RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
2265 if ((queue = QUEUE_Current()))
2267 queue->GetMessageTimeVal = msg.time;
2268 msg.pt.x = LOWORD( queue->GetMessagePosVal );
2269 msg.pt.y = HIWORD( queue->GetMessagePosVal );
2272 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
2274 /* copy back our internal safe copy of message data to msg_out.
2275 * msg_out is a variable from the *program*, so it can't be used
2276 * internally as it can get "corrupted" by our use of SendMessage()
2277 * (back to the program) inside the message handling itself. */
2278 *msg_out = msg;
2279 return TRUE;
2283 /***********************************************************************
2284 * PeekMessageA (USER32.@)
2286 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2288 BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2289 if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2290 return ret;
2294 /***********************************************************************
2295 * GetMessageW (USER32.@)
2297 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2299 MESSAGEQUEUE *queue = QUEUE_Current();
2300 int mask, locks;
2302 mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2303 if (first || last)
2305 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2306 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2307 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2308 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2309 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2310 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2312 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2314 locks = WIN_SuspendWndsLock();
2316 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2318 /* wait until one of the bits is set */
2319 unsigned int wake_bits = 0, changed_bits = 0;
2320 DWORD dwlc;
2322 SERVER_START_REQ( set_queue_mask )
2324 req->wake_mask = QS_SENDMESSAGE;
2325 req->changed_mask = mask;
2326 req->skip_wait = 1;
2327 if (!wine_server_call( req ))
2329 wake_bits = reply->wake_bits;
2330 changed_bits = reply->changed_bits;
2333 SERVER_END_REQ;
2335 if (changed_bits & mask) continue;
2336 if (wake_bits & QS_SENDMESSAGE) continue;
2338 TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2339 queue->self, mask, wake_bits, changed_bits );
2341 ReleaseThunkLock( &dwlc );
2342 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2343 USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
2344 else
2345 WaitForSingleObject( queue->server_queue, INFINITE );
2346 if (dwlc) RestoreThunkLock( dwlc );
2349 WIN_RestoreWndsLock( locks );
2351 return (msg->message != WM_QUIT);
2355 /***********************************************************************
2356 * GetMessageA (USER32.@)
2358 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2360 GetMessageW( msg, hwnd, first, last );
2361 msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2362 return (msg->message != WM_QUIT);
2366 /***********************************************************************
2367 * IsDialogMessage (USER32.@)
2368 * IsDialogMessageA (USER32.@)
2370 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2372 MSG msg = *pmsg;
2373 msg.wParam = map_wparam_AtoW( msg.message, msg.wParam );
2374 return IsDialogMessageW( hwndDlg, &msg );
2378 /***********************************************************************
2379 * SetMessageQueue (USER32.@)
2381 BOOL WINAPI SetMessageQueue( INT size )
2383 /* now obsolete the message queue will be expanded dynamically as necessary */
2384 return TRUE;
2388 /**********************************************************************
2389 * AttachThreadInput (USER32.@)
2391 * Attaches the input processing mechanism of one thread to that of
2392 * another thread.
2394 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
2396 BOOL ret;
2398 SERVER_START_REQ( attach_thread_input )
2400 req->tid_from = from;
2401 req->tid_to = to;
2402 req->attach = attach;
2403 ret = !wine_server_call_err( req );
2405 SERVER_END_REQ;
2406 return ret;
2410 /**********************************************************************
2411 * GetGUIThreadInfo (USER32.@)
2413 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
2415 BOOL ret;
2417 SERVER_START_REQ( get_thread_input )
2419 req->tid = id;
2420 if ((ret = !wine_server_call_err( req )))
2422 info->flags = 0;
2423 info->hwndActive = reply->active;
2424 info->hwndFocus = reply->focus;
2425 info->hwndCapture = reply->capture;
2426 info->hwndMenuOwner = reply->menu_owner;
2427 info->hwndMoveSize = reply->move_size;
2428 info->hwndCaret = reply->caret;
2429 info->rcCaret.left = reply->rect.left;
2430 info->rcCaret.top = reply->rect.top;
2431 info->rcCaret.right = reply->rect.right;
2432 info->rcCaret.bottom = reply->rect.bottom;
2433 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
2434 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
2435 if (reply->caret) info->flags |= GUI_CARETBLINKING;
2438 SERVER_END_REQ;
2439 return ret;
2443 /**********************************************************************
2444 * GetKeyState (USER32.@)
2446 * An application calls the GetKeyState function in response to a
2447 * keyboard-input message. This function retrieves the state of the key
2448 * at the time the input message was generated.
2450 SHORT WINAPI GetKeyState(INT vkey)
2452 SHORT retval = 0;
2454 SERVER_START_REQ( get_key_state )
2456 req->tid = GetCurrentThreadId();
2457 req->key = vkey;
2458 if (!wine_server_call( req )) retval = (signed char)reply->state;
2460 SERVER_END_REQ;
2461 TRACE("key (0x%x) -> %x\n", vkey, retval);
2462 return retval;
2466 /**********************************************************************
2467 * GetKeyboardState (USER32.@)
2469 BOOL WINAPI GetKeyboardState( LPBYTE state )
2471 BOOL ret;
2473 TRACE("(%p)\n", state);
2475 memset( state, 0, 256 );
2476 SERVER_START_REQ( get_key_state )
2478 req->tid = GetCurrentThreadId();
2479 req->key = -1;
2480 wine_server_set_reply( req, state, 256 );
2481 ret = !wine_server_call_err( req );
2483 SERVER_END_REQ;
2484 return ret;
2488 /**********************************************************************
2489 * SetKeyboardState (USER32.@)
2491 BOOL WINAPI SetKeyboardState( LPBYTE state )
2493 BOOL ret;
2495 TRACE("(%p)\n", state);
2497 SERVER_START_REQ( set_key_state )
2499 req->tid = GetCurrentThreadId();
2500 wine_server_add_data( req, state, 256 );
2501 ret = !wine_server_call_err( req );
2503 SERVER_END_REQ;
2504 return ret;