WINELIB applications (like reaktivate) might have very long (C++)
[wine/wine-kai.git] / dlls / user / message.c
blob8c598961ba74d39a75cd2bdebdbb9170db99b427
1 /*
2 * Window messaging support
4 * Copyright 2001 Alexandre Julliard
5 */
7 #include "winbase.h"
8 #include "wingdi.h"
9 #include "winuser.h"
10 #include "winerror.h"
11 #include "winnls.h"
12 #include "dde.h"
13 #include "wine/unicode.h"
14 #include "wine/server.h"
15 #include "queue.h"
16 #include "input.h"
17 #include "message.h"
18 #include "hook.h"
19 #include "spy.h"
20 #include "user.h"
21 #include "win.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(msg);
26 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
27 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
29 #define MAX_PACK_COUNT 4
31 /* description of the data fields that need to be packed along with a sent message */
32 struct packed_message
34 int count;
35 const void *data[MAX_PACK_COUNT];
36 size_t size[MAX_PACK_COUNT];
39 /* info about the message currently being received by the current thread */
40 struct received_message_info
42 enum message_type type;
43 MSG msg;
44 UINT flags; /* InSendMessageEx return flags */
47 /* structure to group all parameters for sent messages of the various kinds */
48 struct send_message_info
50 enum message_type type;
51 HWND hwnd;
52 UINT msg;
53 WPARAM wparam;
54 LPARAM lparam;
55 UINT flags; /* flags for SendMessageTimeout */
56 UINT timeout; /* timeout for SendMessageTimeout */
57 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
58 ULONG_PTR data; /* callback data */
62 /* flag for messages that contain pointers */
63 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
65 #define SET(msg) (1 << ((msg) & 31))
67 static const unsigned int message_pointer_flags[] =
69 /* 0x00 - 0x1f */
70 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
71 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
72 /* 0x20 - 0x3f */
73 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
74 SET(WM_COMPAREITEM),
75 /* 0x40 - 0x5f */
76 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
77 SET(WM_NOTIFY) | SET(WM_HELP),
78 /* 0x60 - 0x7f */
79 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
80 /* 0x80 - 0x9f */
81 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
82 /* 0xa0 - 0xbf */
83 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
84 /* 0xc0 - 0xdf */
85 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
86 /* 0xe0 - 0xff */
87 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO),
88 /* 0x100 - 0x11f */
90 /* 0x120 - 0x13f */
92 /* 0x140 - 0x15f */
93 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
94 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
95 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
96 /* 0x160 - 0x17f */
98 /* 0x180 - 0x19f */
99 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
100 SET(LB_DIR) | SET(LB_FINDSTRING) |
101 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
102 /* 0x1a0 - 0x1bf */
103 SET(LB_FINDSTRINGEXACT),
104 /* 0x1c0 - 0x1df */
106 /* 0x1e0 - 0x1ff */
108 /* 0x200 - 0x21f */
109 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
110 /* 0x220 - 0x23f */
111 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
112 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
113 /* 0x240 - 0x25f */
115 /* 0x260 - 0x27f */
117 /* 0x280 - 0x29f */
119 /* 0x2a0 - 0x2bf */
121 /* 0x2c0 - 0x2df */
123 /* 0x2e0 - 0x2ff */
125 /* 0x300 - 0x31f */
126 SET(WM_ASKCBFORMATNAME)
129 /* flags for messages that contain Unicode strings */
130 static const unsigned int message_unicode_flags[] =
132 /* 0x00 - 0x1f */
133 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
134 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
135 /* 0x20 - 0x3f */
136 SET(WM_CHARTOITEM),
137 /* 0x40 - 0x5f */
139 /* 0x60 - 0x7f */
141 /* 0x80 - 0x9f */
142 SET(WM_NCCREATE),
143 /* 0xa0 - 0xbf */
145 /* 0xc0 - 0xdf */
146 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
147 /* 0xe0 - 0xff */
149 /* 0x100 - 0x11f */
150 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
151 /* 0x120 - 0x13f */
152 SET(WM_MENUCHAR),
153 /* 0x140 - 0x15f */
154 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
155 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
156 /* 0x160 - 0x17f */
158 /* 0x180 - 0x19f */
159 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
160 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
161 /* 0x1a0 - 0x1bf */
162 SET(LB_FINDSTRINGEXACT),
163 /* 0x1c0 - 0x1df */
165 /* 0x1e0 - 0x1ff */
167 /* 0x200 - 0x21f */
169 /* 0x220 - 0x23f */
170 SET(WM_MDICREATE),
171 /* 0x240 - 0x25f */
173 /* 0x260 - 0x27f */
175 /* 0x280 - 0x29f */
177 /* 0x2a0 - 0x2bf */
179 /* 0x2c0 - 0x2df */
181 /* 0x2e0 - 0x2ff */
183 /* 0x300 - 0x31f */
184 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
187 /* check whether a given message type includes pointers */
188 inline static int is_pointer_message( UINT message )
190 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
191 return (message_pointer_flags[message / 32] & SET(message)) != 0;
194 /* check whether a given message type contains Unicode (or ASCII) chars */
195 inline static int is_unicode_message( UINT message )
197 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
198 return (message_unicode_flags[message / 32] & SET(message)) != 0;
201 #undef SET
203 /* compute the total size of the packed data */
204 inline static size_t get_data_total_size( const struct packed_message *data )
206 int i;
207 size_t total = 0;
208 for (i = 0; i < data->count; i++) total += data->size[i];
209 return total;
212 /* copy all the data of a packed message into a single dest buffer */
213 inline static void copy_all_data( void *dest, const struct packed_message *data )
215 int i;
216 for (i = 0; i < data->count; i++)
218 memcpy( dest, data->data[i], data->size[i] );
219 dest = (char *)dest + data->size[i];
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));
283 /* check if hwnd is a broadcast magic handle */
284 inline static BOOL is_broadcast( HWND hwnd )
286 return (hwnd == HWND_BROADCAST || hwnd == HWND_TOPMOST);
290 /***********************************************************************
291 * broadcast_message_callback
293 * Helper callback for broadcasting messages.
295 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
297 struct send_message_info *info = (struct send_message_info *)lparam;
298 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
299 switch(info->type)
301 case MSG_UNICODE:
302 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
303 info->flags, info->timeout, NULL );
304 break;
305 case MSG_ASCII:
306 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
307 info->flags, info->timeout, NULL );
308 break;
309 case MSG_NOTIFY:
310 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
311 break;
312 case MSG_CALLBACK:
313 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
314 info->callback, info->data );
315 break;
316 case MSG_POSTED:
317 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
318 break;
319 default:
320 ERR( "bad type %d\n", info->type );
321 break;
323 return TRUE;
327 /***********************************************************************
328 * map_wparam_AtoW
330 * Convert the wparam of an ASCII message to Unicode.
332 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
334 if (message == WM_CHARTOITEM ||
335 message == EM_SETPASSWORDCHAR ||
336 message == WM_CHAR ||
337 message == WM_DEADCHAR ||
338 message == WM_SYSCHAR ||
339 message == WM_SYSDEADCHAR ||
340 message == WM_MENUCHAR)
342 char ch = LOWORD(wparam);
343 WCHAR wch;
344 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
345 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
347 return wparam;
351 /***********************************************************************
352 * map_wparam_WtoA
354 * Convert the wparam of a Unicode message to ASCII.
356 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
358 if (message == WM_CHARTOITEM ||
359 message == EM_SETPASSWORDCHAR ||
360 message == WM_CHAR ||
361 message == WM_DEADCHAR ||
362 message == WM_SYSCHAR ||
363 message == WM_SYSDEADCHAR ||
364 message == WM_MENUCHAR)
366 WCHAR wch = LOWORD(wparam);
367 char ch;
368 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
369 wparam = MAKEWPARAM( (unsigned char)ch, HIWORD(wparam) );
371 return wparam;
375 /***********************************************************************
376 * pack_message
378 * Pack a message for sending to another process.
379 * Return the size of the data we expect in the message reply.
380 * Set data->count to -1 if there is an error.
382 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
383 struct packed_message *data )
385 data->count = 0;
386 switch(message)
388 case WM_NCCREATE:
389 case WM_CREATE:
391 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
392 push_data( data, cs, sizeof(*cs) );
393 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
394 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
395 return sizeof(*cs);
397 case WM_GETTEXT:
398 case WM_ASKCBFORMATNAME:
399 return wparam * sizeof(WCHAR);
400 case WM_SETTEXT:
401 case WM_WININICHANGE:
402 case WM_DEVMODECHANGE:
403 case CB_DIR:
404 case CB_FINDSTRING:
405 case CB_FINDSTRINGEXACT:
406 case CB_SELECTSTRING:
407 case LB_DIR:
408 case LB_ADDFILE:
409 case LB_FINDSTRING:
410 case LB_FINDSTRINGEXACT:
411 case LB_SELECTSTRING:
412 case EM_REPLACESEL:
413 push_string( data, (LPWSTR)lparam );
414 return 0;
415 case WM_GETMINMAXINFO:
416 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
417 return sizeof(MINMAXINFO);
418 case WM_DRAWITEM:
419 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
420 return 0;
421 case WM_MEASUREITEM:
422 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
423 return sizeof(MEASUREITEMSTRUCT);
424 case WM_DELETEITEM:
425 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
426 return 0;
427 case WM_COMPAREITEM:
428 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
429 return 0;
430 case WM_WINDOWPOSCHANGING:
431 case WM_WINDOWPOSCHANGED:
432 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
433 return sizeof(WINDOWPOS);
434 case WM_COPYDATA:
436 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
437 push_data( data, cp, sizeof(*cp) );
438 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
439 return 0;
441 case WM_NOTIFY:
442 /* WM_NOTIFY cannot be sent across processes (MSDN) */
443 data->count = -1;
444 return 0;
445 case WM_HELP:
446 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
447 return 0;
448 case WM_STYLECHANGING:
449 case WM_STYLECHANGED:
450 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
451 return 0;
452 case WM_NCCALCSIZE:
453 if (!wparam)
455 push_data( data, (RECT *)lparam, sizeof(RECT) );
456 return sizeof(RECT);
458 else
460 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
461 push_data( data, nc, sizeof(*nc) );
462 push_data( data, nc->lppos, sizeof(*nc->lppos) );
463 return sizeof(*nc) + sizeof(*nc->lppos);
465 case WM_GETDLGCODE:
466 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
467 return sizeof(MSG);
468 case SBM_SETSCROLLINFO:
469 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
470 return 0;
471 case SBM_GETSCROLLINFO:
472 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
473 return sizeof(SCROLLINFO);
474 case EM_GETSEL:
475 case SBM_GETRANGE:
476 case CB_GETEDITSEL:
478 size_t size = 0;
479 if (wparam) size += sizeof(DWORD);
480 if (lparam) size += sizeof(DWORD);
481 return size;
483 case EM_GETRECT:
484 case LB_GETITEMRECT:
485 case CB_GETDROPPEDCONTROLRECT:
486 return sizeof(RECT);
487 case EM_SETRECT:
488 case EM_SETRECTNP:
489 push_data( data, (RECT *)lparam, sizeof(RECT) );
490 return 0;
491 case EM_GETLINE:
493 WORD *pw = (WORD *)lparam;
494 push_data( data, pw, sizeof(*pw) );
495 return *pw * sizeof(WCHAR);
497 case EM_SETTABSTOPS:
498 case LB_SETTABSTOPS:
499 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
500 return 0;
501 case CB_ADDSTRING:
502 case CB_INSERTSTRING:
503 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
504 return 0;
505 case CB_GETLBTEXT:
506 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
507 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
508 case LB_ADDSTRING:
509 case LB_INSERTSTRING:
510 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
511 return 0;
512 case LB_GETTEXT:
513 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
514 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
515 case LB_GETSELITEMS:
516 return wparam * sizeof(UINT);
517 case WM_NEXTMENU:
518 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
519 return sizeof(MDINEXTMENU);
520 case WM_SIZING:
521 case WM_MOVING:
522 push_data( data, (RECT *)lparam, sizeof(RECT) );
523 return sizeof(RECT);
524 case WM_MDICREATE:
526 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
527 push_data( data, cs, sizeof(*cs) );
528 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
529 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
530 return sizeof(*cs);
532 case WM_MDIGETACTIVE:
533 if (lparam) return sizeof(BOOL);
534 return 0;
536 /* these contain an HFONT */
537 case WM_SETFONT:
538 case WM_GETFONT:
539 /* these contain an HDC */
540 case WM_PAINT:
541 case WM_ERASEBKGND:
542 case WM_ICONERASEBKGND:
543 case WM_NCPAINT:
544 case WM_CTLCOLORMSGBOX:
545 case WM_CTLCOLOREDIT:
546 case WM_CTLCOLORLISTBOX:
547 case WM_CTLCOLORBTN:
548 case WM_CTLCOLORDLG:
549 case WM_CTLCOLORSCROLLBAR:
550 case WM_CTLCOLORSTATIC:
551 case WM_PRINT:
552 case WM_PRINTCLIENT:
553 /* these contain an HGLOBAL */
554 case WM_PAINTCLIPBOARD:
555 case WM_SIZECLIPBOARD:
556 case WM_DDE_INITIATE:
557 case WM_DDE_ADVISE:
558 case WM_DDE_UNADVISE:
559 case WM_DDE_DATA:
560 case WM_DDE_REQUEST:
561 case WM_DDE_POKE:
562 case WM_DDE_EXECUTE:
563 /* these contain pointers */
564 case WM_DROPOBJECT:
565 case WM_QUERYDROPOBJECT:
566 case WM_DRAGLOOP:
567 case WM_DRAGSELECT:
568 case WM_DRAGMOVE:
569 case WM_DEVICECHANGE:
570 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
571 data->count = -1;
572 return 0;
574 return 0;
578 /***********************************************************************
579 * unpack_message
581 * Unpack a message received from another process.
583 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
584 void **buffer, size_t size )
586 size_t minsize = 0;
588 switch(message)
590 case WM_NCCREATE:
591 case WM_CREATE:
593 CREATESTRUCTW *cs = *buffer;
594 WCHAR *str = (WCHAR *)(cs + 1);
595 if (size < sizeof(*cs)) return FALSE;
596 size -= sizeof(*cs);
597 if (HIWORD(cs->lpszName))
599 if (!check_string( str, size )) return FALSE;
600 cs->lpszName = str;
601 size -= (strlenW(str) + 1) * sizeof(WCHAR);
602 str += strlenW(str) + 1;
604 if (HIWORD(cs->lpszClass))
606 if (!check_string( str, size )) return FALSE;
607 cs->lpszClass = str;
609 break;
611 case WM_GETTEXT:
612 case WM_ASKCBFORMATNAME:
613 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
614 break;
615 case WM_SETTEXT:
616 case WM_WININICHANGE:
617 case WM_DEVMODECHANGE:
618 case CB_DIR:
619 case CB_FINDSTRING:
620 case CB_FINDSTRINGEXACT:
621 case CB_SELECTSTRING:
622 case LB_DIR:
623 case LB_ADDFILE:
624 case LB_FINDSTRING:
625 case LB_FINDSTRINGEXACT:
626 case LB_SELECTSTRING:
627 case EM_REPLACESEL:
628 if (!check_string( *buffer, size )) return FALSE;
629 break;
630 case WM_GETMINMAXINFO:
631 minsize = sizeof(MINMAXINFO);
632 break;
633 case WM_DRAWITEM:
634 minsize = sizeof(DRAWITEMSTRUCT);
635 break;
636 case WM_MEASUREITEM:
637 minsize = sizeof(MEASUREITEMSTRUCT);
638 break;
639 case WM_DELETEITEM:
640 minsize = sizeof(DELETEITEMSTRUCT);
641 break;
642 case WM_COMPAREITEM:
643 minsize = sizeof(COMPAREITEMSTRUCT);
644 break;
645 case WM_WINDOWPOSCHANGING:
646 case WM_WINDOWPOSCHANGED:
647 minsize = sizeof(WINDOWPOS);
648 break;
649 case WM_COPYDATA:
651 COPYDATASTRUCT *cp = *buffer;
652 if (size < sizeof(*cp)) return FALSE;
653 if (cp->lpData)
655 minsize = sizeof(*cp) + cp->cbData;
656 cp->lpData = cp + 1;
658 break;
660 case WM_NOTIFY:
661 /* WM_NOTIFY cannot be sent across processes (MSDN) */
662 return FALSE;
663 case WM_HELP:
664 minsize = sizeof(HELPINFO);
665 break;
666 case WM_STYLECHANGING:
667 case WM_STYLECHANGED:
668 minsize = sizeof(STYLESTRUCT);
669 break;
670 case WM_NCCALCSIZE:
671 if (!*wparam) minsize = sizeof(RECT);
672 else
674 NCCALCSIZE_PARAMS *nc = *buffer;
675 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
676 nc->lppos = (WINDOWPOS *)(nc + 1);
678 break;
679 case WM_GETDLGCODE:
680 if (!*lparam) return TRUE;
681 minsize = sizeof(MSG);
682 break;
683 case SBM_SETSCROLLINFO:
684 minsize = sizeof(SCROLLINFO);
685 break;
686 case SBM_GETSCROLLINFO:
687 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
688 break;
689 case EM_GETSEL:
690 case SBM_GETRANGE:
691 case CB_GETEDITSEL:
692 if (*wparam || *lparam)
694 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
695 if (*wparam) *wparam = (WPARAM)*buffer;
696 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
698 return TRUE;
699 case EM_GETRECT:
700 case LB_GETITEMRECT:
701 case CB_GETDROPPEDCONTROLRECT:
702 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
703 break;
704 case EM_SETRECT:
705 case EM_SETRECTNP:
706 minsize = sizeof(RECT);
707 break;
708 case EM_GETLINE:
710 WORD len;
711 if (size < sizeof(WORD)) return FALSE;
712 len = *(WORD *)*buffer;
713 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
714 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
715 return TRUE;
717 case EM_SETTABSTOPS:
718 case LB_SETTABSTOPS:
719 if (!*wparam) return TRUE;
720 minsize = *wparam * sizeof(UINT);
721 break;
722 case CB_ADDSTRING:
723 case CB_INSERTSTRING:
724 case LB_ADDSTRING:
725 case LB_INSERTSTRING:
726 if (!*buffer) return TRUE;
727 if (!check_string( *buffer, size )) return FALSE;
728 break;
729 case CB_GETLBTEXT:
731 size = sizeof(ULONG_PTR);
732 if (combobox_has_strings( hwnd ))
733 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
734 if (!get_buffer_space( buffer, size )) return FALSE;
735 break;
737 case LB_GETTEXT:
739 size = sizeof(ULONG_PTR);
740 if (listbox_has_strings( hwnd ))
741 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
742 if (!get_buffer_space( buffer, size )) return FALSE;
743 break;
745 case LB_GETSELITEMS:
746 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
747 break;
748 case WM_NEXTMENU:
749 minsize = sizeof(MDINEXTMENU);
750 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
751 break;
752 case WM_SIZING:
753 case WM_MOVING:
754 minsize = sizeof(RECT);
755 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
756 break;
757 case WM_MDICREATE:
759 MDICREATESTRUCTW *cs = *buffer;
760 WCHAR *str = (WCHAR *)(cs + 1);
761 if (size < sizeof(*cs)) return FALSE;
762 size -= sizeof(*cs);
763 if (HIWORD(cs->szTitle))
765 if (!check_string( str, size )) return FALSE;
766 cs->szTitle = str;
767 size -= (strlenW(str) + 1) * sizeof(WCHAR);
768 str += strlenW(str) + 1;
770 if (HIWORD(cs->szClass))
772 if (!check_string( str, size )) return FALSE;
773 cs->szClass = str;
775 break;
777 case WM_MDIGETACTIVE:
778 if (!*lparam) return TRUE;
779 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
780 break;
782 /* these contain an HFONT */
783 case WM_SETFONT:
784 case WM_GETFONT:
785 /* these contain an HDC */
786 case WM_PAINT:
787 case WM_ERASEBKGND:
788 case WM_ICONERASEBKGND:
789 case WM_NCPAINT:
790 case WM_CTLCOLORMSGBOX:
791 case WM_CTLCOLOREDIT:
792 case WM_CTLCOLORLISTBOX:
793 case WM_CTLCOLORBTN:
794 case WM_CTLCOLORDLG:
795 case WM_CTLCOLORSCROLLBAR:
796 case WM_CTLCOLORSTATIC:
797 case WM_PRINT:
798 case WM_PRINTCLIENT:
799 /* these contain an HGLOBAL */
800 case WM_PAINTCLIPBOARD:
801 case WM_SIZECLIPBOARD:
802 case WM_DDE_INITIATE:
803 case WM_DDE_ADVISE:
804 case WM_DDE_UNADVISE:
805 case WM_DDE_DATA:
806 case WM_DDE_REQUEST:
807 case WM_DDE_POKE:
808 case WM_DDE_EXECUTE:
809 /* these contain pointers */
810 case WM_DROPOBJECT:
811 case WM_QUERYDROPOBJECT:
812 case WM_DRAGLOOP:
813 case WM_DRAGSELECT:
814 case WM_DRAGMOVE:
815 case WM_DEVICECHANGE:
816 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
817 return FALSE;
819 default:
820 return TRUE; /* message doesn't need any unpacking */
823 /* default exit for most messages: check minsize and store buffer in lparam */
824 if (size < minsize) return FALSE;
825 *lparam = (LPARAM)*buffer;
826 return TRUE;
830 /***********************************************************************
831 * pack_reply
833 * Pack a reply to a message for sending to another process.
835 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
836 LRESULT res, struct packed_message *data )
838 data->count = 0;
839 switch(message)
841 case WM_NCCREATE:
842 case WM_CREATE:
843 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
844 break;
845 case WM_GETTEXT:
846 case CB_GETLBTEXT:
847 case LB_GETTEXT:
848 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
849 break;
850 case WM_GETMINMAXINFO:
851 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
852 break;
853 case WM_MEASUREITEM:
854 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
855 break;
856 case WM_WINDOWPOSCHANGING:
857 case WM_WINDOWPOSCHANGED:
858 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
859 break;
860 case WM_GETDLGCODE:
861 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
862 break;
863 case SBM_GETSCROLLINFO:
864 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
865 break;
866 case EM_GETRECT:
867 case LB_GETITEMRECT:
868 case CB_GETDROPPEDCONTROLRECT:
869 case WM_SIZING:
870 case WM_MOVING:
871 push_data( data, (RECT *)lparam, sizeof(RECT) );
872 break;
873 case EM_GETLINE:
875 WORD *ptr = (WORD *)lparam;
876 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
877 break;
879 case LB_GETSELITEMS:
880 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
881 break;
882 case WM_MDIGETACTIVE:
883 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
884 break;
885 case WM_NCCALCSIZE:
886 if (!wparam)
887 push_data( data, (RECT *)lparam, sizeof(RECT) );
888 else
890 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
891 push_data( data, nc, sizeof(*nc) );
892 push_data( data, nc->lppos, sizeof(*nc->lppos) );
894 break;
895 case EM_GETSEL:
896 case SBM_GETRANGE:
897 case CB_GETEDITSEL:
898 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
899 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
900 break;
901 case WM_NEXTMENU:
902 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
903 break;
904 case WM_MDICREATE:
905 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
906 break;
907 case WM_ASKCBFORMATNAME:
908 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
909 break;
914 /***********************************************************************
915 * unpack_reply
917 * Unpack a message reply received from another process.
919 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
920 void *buffer, size_t size )
922 switch(message)
924 case WM_NCCREATE:
925 case WM_CREATE:
927 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
928 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
929 memcpy( cs, buffer, min( sizeof(*cs), size ));
930 cs->lpszName = name; /* restore the original pointers */
931 cs->lpszClass = class;
932 break;
934 case WM_GETTEXT:
935 case WM_ASKCBFORMATNAME:
936 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
937 break;
938 case WM_GETMINMAXINFO:
939 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
940 break;
941 case WM_MEASUREITEM:
942 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
943 break;
944 case WM_WINDOWPOSCHANGING:
945 case WM_WINDOWPOSCHANGED:
946 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
947 break;
948 case WM_GETDLGCODE:
949 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
950 break;
951 case SBM_GETSCROLLINFO:
952 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
953 break;
954 case EM_GETRECT:
955 case CB_GETDROPPEDCONTROLRECT:
956 case LB_GETITEMRECT:
957 case WM_SIZING:
958 case WM_MOVING:
959 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
960 break;
961 case EM_GETLINE:
962 size = min( size, *(WORD *)lparam );
963 memcpy( (WCHAR *)lparam, buffer, size );
964 break;
965 case LB_GETSELITEMS:
966 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
967 break;
968 case LB_GETTEXT:
969 case CB_GETLBTEXT:
970 memcpy( (WCHAR *)lparam, buffer, size );
971 break;
972 case WM_NEXTMENU:
973 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
974 break;
975 case WM_MDIGETACTIVE:
976 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
977 break;
978 case WM_NCCALCSIZE:
979 if (!wparam)
980 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
981 else
983 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
984 WINDOWPOS *wp = nc->lppos;
985 memcpy( nc, buffer, min( sizeof(*nc), size ));
986 if (size > sizeof(*nc))
988 size -= sizeof(*nc);
989 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
991 nc->lppos = wp; /* restore the original pointer */
993 break;
994 case EM_GETSEL:
995 case SBM_GETRANGE:
996 case CB_GETEDITSEL:
997 if (wparam)
999 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1000 if (size <= sizeof(DWORD)) break;
1001 size -= sizeof(DWORD);
1002 buffer = (DWORD *)buffer + 1;
1004 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1005 break;
1006 case WM_MDICREATE:
1008 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
1009 LPCWSTR title = cs->szTitle, class = cs->szClass;
1010 memcpy( cs, buffer, min( sizeof(*cs), size ));
1011 cs->szTitle = title; /* restore the original pointers */
1012 cs->szClass = class;
1013 break;
1015 default:
1016 ERR( "should not happen: unexpected message %x\n", message );
1017 break;
1022 /***********************************************************************
1023 * reply_message
1025 * Send a reply to a sent message.
1027 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1029 struct packed_message data;
1030 int replied = info->flags & ISMEX_REPLIED;
1032 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1033 if (!remove && replied) return; /* replied already */
1035 data.count = 0;
1036 info->flags |= ISMEX_REPLIED;
1038 if (info->type == MSG_OTHER_PROCESS && !replied)
1040 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1041 info->msg.lParam, result, &data );
1042 if (data.count)
1044 size_t total = get_data_total_size( &data );
1046 if (total > REQUEST_MAX_VAR_SIZE)
1048 FIXME( "inter-process msg data size %d not supported yet, expect trouble\n",
1049 total );
1050 total = REQUEST_MAX_VAR_SIZE;
1053 SERVER_START_VAR_REQ( reply_message, total )
1055 req->result = result;
1056 req->remove = remove;
1057 copy_all_data( server_data_ptr(req), &data );
1058 SERVER_CALL();
1060 SERVER_END_VAR_REQ;
1061 return;
1065 SERVER_START_REQ( reply_message )
1067 req->result = result;
1068 req->remove = remove;
1069 SERVER_CALL();
1071 SERVER_END_REQ;
1075 /***********************************************************************
1076 * call_window_proc
1078 * Call a window procedure and the corresponding hooks.
1080 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam, BOOL unicode )
1082 LRESULT result;
1083 WNDPROC winproc;
1085 /* FIXME: should check for exiting queue */
1087 /* first the WH_CALLWNDPROC hook */
1088 if (HOOK_IsHooked( WH_CALLWNDPROC ))
1090 CWPSTRUCT cwp;
1091 cwp.lParam = lparam;
1092 cwp.wParam = wparam;
1093 cwp.message = msg;
1094 cwp.hwnd = WIN_GetFullHandle( hwnd );
1095 if (unicode) HOOK_CallHooksW( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1096 else HOOK_CallHooksA( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1097 lparam = cwp.lParam;
1098 wparam = cwp.wParam;
1099 msg = cwp.message;
1100 hwnd = cwp.hwnd;
1103 /* now call the window procedure */
1104 if (unicode)
1106 if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) return 0;
1107 result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1109 else
1111 if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) return 0;
1112 result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1115 /* and finally the WH_CALLWNDPROCRET hook */
1116 if (HOOK_IsHooked( WH_CALLWNDPROCRET ))
1118 CWPRETSTRUCT cwp;
1119 cwp.lResult = result;
1120 cwp.lParam = lparam;
1121 cwp.wParam = wparam;
1122 cwp.message = msg;
1123 cwp.hwnd = WIN_GetFullHandle( hwnd );
1124 if (unicode) HOOK_CallHooksW( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
1125 else HOOK_CallHooksA( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
1127 return result;
1131 /***********************************************************************
1132 * MSG_peek_message
1134 * Peek for a message matching the given parameters. Return FALSE if none available.
1135 * All pending sent messages are processed before returning.
1137 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1139 BOOL ret;
1140 LRESULT result;
1141 ULONG_PTR extra_info = 0;
1142 MESSAGEQUEUE *queue = QUEUE_Current();
1143 struct received_message_info info, *old_info;
1145 if (!first && !last) last = ~0;
1147 for (;;)
1149 void *buffer = NULL;
1150 size_t size = 0;
1152 SERVER_START_VAR_REQ( get_message, REQUEST_MAX_VAR_SIZE )
1154 req->flags = flags;
1155 req->get_win = hwnd;
1156 req->get_first = first;
1157 req->get_last = last;
1158 if ((ret = !SERVER_CALL()))
1160 info.type = req->type;
1161 info.msg.hwnd = req->win;
1162 info.msg.message = req->msg;
1163 info.msg.wParam = req->wparam;
1164 info.msg.lParam = req->lparam;
1165 info.msg.time = req->time;
1166 info.msg.pt.x = req->x;
1167 info.msg.pt.y = req->y;
1168 extra_info = req->info;
1170 if ((size = server_data_size(req)))
1172 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
1174 ERR("out of memory for message data\n");
1175 ret = FALSE;
1177 else memcpy( buffer, server_data_ptr(req), size );
1181 SERVER_END_VAR_REQ;
1183 if (!ret) return FALSE; /* no message available */
1185 TRACE( "got type %d msg %x hwnd %x wp %x lp %lx\n",
1186 info.type, info.msg.message, info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1188 switch(info.type)
1190 case MSG_ASCII:
1191 case MSG_UNICODE:
1192 info.flags = ISMEX_SEND;
1193 break;
1194 case MSG_NOTIFY:
1195 info.flags = ISMEX_NOTIFY;
1196 break;
1197 case MSG_CALLBACK:
1198 info.flags = ISMEX_CALLBACK;
1199 break;
1200 case MSG_OTHER_PROCESS:
1201 info.flags = ISMEX_SEND;
1202 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1203 &info.msg.lParam, &buffer, size ))
1205 ERR( "invalid packed message %x (%s) hwnd %x wp %x lp %lx size %d\n",
1206 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
1207 info.msg.wParam, info.msg.lParam, size );
1208 /* ignore it */
1209 reply_message( &info, 0, TRUE );
1210 continue;
1212 break;
1213 case MSG_POSTED:
1214 goto got_one;
1215 case MSG_HARDWARE_RAW:
1216 if (!MSG_process_raw_hardware_message( &info.msg, extra_info,
1217 hwnd, first, last, flags & GET_MSG_REMOVE ))
1218 continue;
1219 /* fall through */
1220 case MSG_HARDWARE_COOKED:
1221 if (!MSG_process_cooked_hardware_message( &info.msg, flags & GET_MSG_REMOVE ))
1223 flags |= GET_MSG_REMOVE_LAST;
1224 continue;
1226 queue->GetMessageExtraInfoVal = extra_info;
1227 goto got_one;
1230 /* if we get here, we have a sent message; call the window procedure */
1231 old_info = queue->receive_info;
1232 queue->receive_info = &info;
1233 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1234 info.msg.lParam, (info.type != MSG_ASCII) );
1235 reply_message( &info, result, TRUE );
1236 queue->receive_info = old_info;
1238 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1241 got_one:
1242 *msg = info.msg;
1243 return TRUE;
1247 /***********************************************************************
1248 * wait_message_reply
1250 * Wait until a sent message gets replied to.
1252 static void wait_message_reply( UINT flags )
1254 MESSAGEQUEUE *queue;
1256 if (!(queue = QUEUE_Current())) return;
1258 for (;;)
1260 unsigned int wake_bits = 0, changed_bits = 0;
1261 DWORD dwlc, res;
1263 SERVER_START_REQ( set_queue_mask )
1265 req->wake_mask = (flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE;
1266 req->changed_mask = QS_SMRESULT | req->wake_mask;
1267 req->skip_wait = 1;
1268 if (!SERVER_CALL())
1270 wake_bits = req->wake_bits;
1271 changed_bits = req->changed_bits;
1274 SERVER_END_REQ;
1276 if (changed_bits & QS_SMRESULT) return; /* got a result */
1277 if (wake_bits & QS_SENDMESSAGE)
1279 /* Process the sent message immediately */
1280 MSG msg;
1281 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1282 continue;
1285 /* now wait for it */
1287 ReleaseThunkLock( &dwlc );
1289 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1290 res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1291 INFINITE, 0, 0 );
1292 else
1293 res = WaitForSingleObject( queue->server_queue, INFINITE );
1295 if (dwlc) RestoreThunkLock( dwlc );
1299 /***********************************************************************
1300 * put_message_in_queue
1302 * Put a sent message into the destination queue.
1303 * For inter-process message, reply_size is set to expected size of reply data.
1305 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1306 size_t *reply_size )
1308 unsigned int res;
1309 int timeout = -1;
1311 /* FIXME: should check for exiting queue */
1313 if (info->type != MSG_NOTIFY &&
1314 info->type != MSG_CALLBACK &&
1315 info->type != MSG_POSTED &&
1316 info->timeout != INFINITE)
1317 timeout = info->timeout;
1319 if (info->type == MSG_OTHER_PROCESS)
1321 struct packed_message data;
1322 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1324 if (data.count == -1)
1326 WARN( "cannot pack message %x\n", info->msg );
1327 return FALSE;
1330 if (data.size[0]) /* need to send extra data along with the message */
1332 size_t total = get_data_total_size( &data );
1334 if (total > REQUEST_MAX_VAR_SIZE)
1336 FIXME( "inter-process msg data size %d not supported yet, expect trouble\n",
1337 total );
1338 total = REQUEST_MAX_VAR_SIZE;
1341 SERVER_START_VAR_REQ( send_message, total )
1343 req->id = (void *)dest_tid;
1344 req->type = MSG_OTHER_PROCESS;
1345 req->win = info->hwnd;
1346 req->msg = info->msg;
1347 req->wparam = info->wparam;
1348 req->lparam = info->lparam;
1349 req->time = GetCurrentTime();
1350 req->timeout = timeout;
1351 copy_all_data( server_data_ptr(req), &data );
1352 res = SERVER_CALL();
1354 SERVER_END_VAR_REQ;
1355 goto done;
1359 /* no extra data, or not inter-process message */
1360 SERVER_START_REQ( send_message )
1362 req->id = (void *)dest_tid;
1363 req->type = info->type;
1364 req->win = info->hwnd;
1365 req->msg = info->msg;
1366 req->wparam = info->wparam;
1367 req->lparam = info->lparam;
1368 req->time = GetCurrentTime();
1369 req->timeout = timeout;
1370 res = SERVER_CALL();
1372 SERVER_END_REQ;
1374 done:
1375 if (res)
1377 if (res == STATUS_INVALID_PARAMETER)
1378 /* FIXME: find a STATUS_ value for this one */
1379 SetLastError( ERROR_INVALID_THREAD_ID );
1380 else
1381 SetLastError( RtlNtStatusToDosError(res) );
1383 return !res;
1387 /***********************************************************************
1388 * retrieve_reply
1390 * Retrieve a message reply from the server.
1392 static LRESULT retrieve_reply( const struct send_message_info *info,
1393 size_t reply_size, LRESULT *result )
1395 NTSTATUS status;
1397 if (reply_size)
1399 if (reply_size > REQUEST_MAX_VAR_SIZE)
1401 WARN( "reply_size %d too large, reply may be truncated\n", reply_size );
1402 reply_size = REQUEST_MAX_VAR_SIZE;
1404 SERVER_START_VAR_REQ( get_message_reply, reply_size )
1406 req->cancel = 1;
1407 if (!(status = SERVER_CALL()))
1409 *result = req->result;
1410 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam,
1411 server_data_ptr(req), server_data_size(req) );
1414 SERVER_END_VAR_REQ;
1416 else
1418 SERVER_START_REQ( get_message_reply )
1420 req->cancel = 1;
1421 if (!(status = SERVER_CALL())) *result = req->result;
1423 SERVER_END_REQ;
1426 TRACE( "hwnd %x msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1427 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
1428 info->lparam, *result, status );
1430 if (!status) return 1;
1431 if (status == STATUS_TIMEOUT) SetLastError(0); /* timeout */
1432 else SetLastError( RtlNtStatusToDosError(status) );
1433 return 0;
1437 /***********************************************************************
1438 * send_inter_thread_message
1440 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1441 LRESULT *res_ptr )
1443 LRESULT ret;
1444 int locks;
1445 size_t reply_size = 0;
1447 TRACE( "hwnd %x msg %x (%s) wp %x lp %lx\n",
1448 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
1450 if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1452 /* there's no reply to wait for on notify/callback messages */
1453 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1455 locks = WIN_SuspendWndsLock();
1457 wait_message_reply( info->flags );
1458 ret = retrieve_reply( info, reply_size, res_ptr );
1460 WIN_RestoreWndsLock( locks );
1461 return ret;
1465 /***********************************************************************
1466 * SendMessageTimeoutW (USER32.@)
1468 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1469 UINT flags, UINT timeout, LPDWORD res_ptr )
1471 struct send_message_info info;
1472 DWORD dest_tid, dest_pid;
1473 LRESULT ret, result;
1475 info.type = MSG_UNICODE;
1476 info.hwnd = hwnd;
1477 info.msg = msg;
1478 info.wparam = wparam;
1479 info.lparam = lparam;
1480 info.flags = flags;
1481 info.timeout = timeout;
1483 if (is_broadcast(hwnd))
1485 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1486 if (res_ptr) *res_ptr = 1;
1487 return 1;
1490 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1492 dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid );
1494 if (dest_tid == GetCurrentThreadId())
1496 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1497 ret = 1;
1499 else
1501 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1502 ret = send_inter_thread_message( dest_tid, &info, &result );
1505 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1506 if (ret && res_ptr) *res_ptr = result;
1507 return ret;
1511 /***********************************************************************
1512 * SendMessageTimeoutA (USER32.@)
1514 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1515 UINT flags, UINT timeout, LPDWORD res_ptr )
1517 struct send_message_info info;
1518 DWORD dest_tid, dest_pid;
1519 LRESULT ret, result;
1521 info.type = MSG_ASCII;
1522 info.hwnd = hwnd;
1523 info.msg = msg;
1524 info.wparam = wparam;
1525 info.lparam = lparam;
1526 info.flags = flags;
1527 info.timeout = timeout;
1529 if (is_broadcast(hwnd))
1531 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1532 if (res_ptr) *res_ptr = 1;
1533 return 1;
1536 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1538 dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid );
1540 if (dest_tid == GetCurrentThreadId())
1542 result = call_window_proc( hwnd, msg, wparam, lparam, FALSE );
1543 ret = 1;
1545 else if (dest_pid == GetCurrentProcessId())
1547 ret = send_inter_thread_message( dest_tid, &info, &result );
1549 else
1551 /* inter-process message: need to map to Unicode */
1552 info.type = MSG_OTHER_PROCESS;
1553 if (is_unicode_message( info.msg ))
1555 if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1556 return 0;
1557 ret = send_inter_thread_message( dest_tid, &info, &result );
1558 result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1559 info.lparam, result );
1561 else ret = send_inter_thread_message( dest_tid, &info, &result );
1563 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1564 if (ret && res_ptr) *res_ptr = result;
1565 return ret;
1569 /***********************************************************************
1570 * SendMessageW (USER32.@)
1572 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1574 LRESULT res = 0;
1575 SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1576 return res;
1580 /***********************************************************************
1581 * SendMessageA (USER32.@)
1583 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1585 LRESULT res = 0;
1586 SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1587 return res;
1591 /***********************************************************************
1592 * SendNotifyMessageA (USER32.@)
1594 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1596 return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1600 /***********************************************************************
1601 * SendNotifyMessageW (USER32.@)
1603 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1605 struct send_message_info info;
1606 DWORD dest_tid;
1607 LRESULT result;
1609 if (is_pointer_message(msg))
1611 SetLastError(ERROR_INVALID_PARAMETER);
1612 return FALSE;
1615 info.type = MSG_NOTIFY;
1616 info.hwnd = hwnd;
1617 info.msg = msg;
1618 info.wparam = wparam;
1619 info.lparam = lparam;
1621 if (is_broadcast(hwnd))
1623 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1624 return TRUE;
1627 dest_tid = GetWindowThreadProcessId( hwnd, NULL );
1629 if (dest_tid == GetCurrentThreadId())
1631 call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1632 return TRUE;
1634 return send_inter_thread_message( dest_tid, &info, &result );
1638 /***********************************************************************
1639 * SendMessageCallbackA (USER32.@)
1641 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1642 SENDASYNCPROC callback, ULONG_PTR data )
1644 return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
1645 lparam, callback, data );
1649 /***********************************************************************
1650 * SendMessageCallbackW (USER32.@)
1652 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1653 SENDASYNCPROC callback, ULONG_PTR data )
1655 struct send_message_info info;
1656 LRESULT result;
1657 DWORD dest_tid;
1659 if (is_pointer_message(msg))
1661 SetLastError(ERROR_INVALID_PARAMETER);
1662 return FALSE;
1665 info.type = MSG_CALLBACK;
1666 info.hwnd = hwnd;
1667 info.msg = msg;
1668 info.wparam = wparam;
1669 info.lparam = lparam;
1670 info.callback = callback;
1671 info.data = data;
1673 if (is_broadcast(hwnd))
1675 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1676 return TRUE;
1679 dest_tid = GetWindowThreadProcessId( hwnd, NULL );
1681 if (dest_tid == GetCurrentThreadId())
1683 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1684 callback( hwnd, msg, data, result );
1685 return TRUE;
1687 FIXME( "callback will not be called\n" );
1688 return send_inter_thread_message( dest_tid, &info, &result );
1692 /***********************************************************************
1693 * ReplyMessage (USER32.@)
1695 BOOL WINAPI ReplyMessage( LRESULT result )
1697 MESSAGEQUEUE *queue = QUEUE_Current();
1698 struct received_message_info *info = queue->receive_info;
1700 if (!info) return FALSE;
1701 reply_message( info, result, FALSE );
1702 return TRUE;
1706 /***********************************************************************
1707 * InSendMessage (USER32.@)
1709 BOOL WINAPI InSendMessage(void)
1711 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
1715 /***********************************************************************
1716 * InSendMessageEx (USER32.@)
1718 DWORD WINAPI InSendMessageEx( LPVOID reserved )
1720 MESSAGEQUEUE *queue = QUEUE_Current();
1721 struct received_message_info *info = queue->receive_info;
1723 if (info) return info->flags;
1724 return ISMEX_NOSEND;
1728 /***********************************************************************
1729 * PostMessageA (USER32.@)
1731 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1733 return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1737 /***********************************************************************
1738 * PostMessageW (USER32.@)
1740 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1742 struct send_message_info info;
1744 if (is_pointer_message( msg ))
1746 SetLastError( ERROR_INVALID_PARAMETER );
1747 return FALSE;
1750 info.type = MSG_POSTED;
1751 info.hwnd = hwnd;
1752 info.msg = msg;
1753 info.wparam = wparam;
1754 info.lparam = lparam;
1756 if (is_broadcast(hwnd))
1758 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1759 return TRUE;
1761 return put_message_in_queue( GetWindowThreadProcessId( hwnd, NULL ), &info, NULL );
1765 /**********************************************************************
1766 * PostThreadMessageA (USER32.@)
1768 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
1770 return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
1774 /**********************************************************************
1775 * PostThreadMessageW (USER32.@)
1777 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
1779 struct send_message_info info;
1781 if (is_pointer_message( msg ))
1783 SetLastError( ERROR_INVALID_PARAMETER );
1784 return FALSE;
1787 info.type = MSG_POSTED;
1788 info.hwnd = 0;
1789 info.msg = msg;
1790 info.wparam = wparam;
1791 info.lparam = lparam;
1792 return put_message_in_queue( thread, &info, NULL );
1796 /***********************************************************************
1797 * PostQuitMessage (USER32.@)
1799 void WINAPI PostQuitMessage( INT exitCode )
1801 PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
1805 /***********************************************************************
1806 * PeekMessageW (USER32.@)
1808 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
1810 MESSAGEQUEUE *queue;
1811 MSG msg;
1812 int locks;
1814 /* check for graphics events */
1815 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1816 USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
1818 hwnd = WIN_GetFullHandle( hwnd );
1819 locks = WIN_SuspendWndsLock();
1821 if (!MSG_peek_message( &msg, hwnd, first, last,
1822 (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
1824 /* FIXME: should be done before checking for hw events */
1825 MSG_JournalPlayBackMsg();
1827 if (!(flags & PM_NOYIELD))
1829 DWORD count;
1830 ReleaseThunkLock(&count);
1831 if (count) RestoreThunkLock(count);
1833 WIN_RestoreWndsLock( locks );
1834 return FALSE;
1837 WIN_RestoreWndsLock( locks );
1839 /* need to fill the window handle for WM_PAINT message */
1840 if (msg.message == WM_PAINT)
1842 if (!(msg.hwnd = WIN_FindWinToRepaint( hwnd ))) return FALSE;
1844 if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
1846 msg.message = WM_PAINTICON;
1847 msg.wParam = 1;
1850 /* check hwnd filter */
1851 if (hwnd && msg.hwnd != hwnd && !IsChild( hwnd, msg.hwnd )) return FALSE;
1853 /* clear internal paint flag */
1854 RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
1857 if ((queue = QUEUE_Current()))
1859 queue->GetMessageTimeVal = msg.time;
1860 queue->GetMessagePosVal = MAKELONG( msg.pt.x, msg.pt.y );
1863 HOOK_CallHooksW( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg );
1865 /* copy back our internal safe copy of message data to msg_out.
1866 * msg_out is a variable from the *program*, so it can't be used
1867 * internally as it can get "corrupted" by our use of SendMessage()
1868 * (back to the program) inside the message handling itself. */
1869 *msg_out = msg;
1870 return TRUE;
1874 /***********************************************************************
1875 * PeekMessageA (USER32.@)
1877 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
1879 BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
1880 if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
1881 return ret;
1885 /***********************************************************************
1886 * GetMessageW (USER32.@)
1888 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
1890 MESSAGEQUEUE *queue = QUEUE_Current();
1891 int mask, locks;
1893 mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
1894 if (first || last)
1896 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
1897 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
1898 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
1899 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
1900 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
1901 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
1903 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
1905 locks = WIN_SuspendWndsLock();
1907 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
1909 /* wait until one of the bits is set */
1910 unsigned int wake_bits = 0, changed_bits = 0;
1911 DWORD dwlc;
1913 SERVER_START_REQ( set_queue_mask )
1915 req->wake_mask = QS_SENDMESSAGE;
1916 req->changed_mask = mask;
1917 req->skip_wait = 1;
1918 if (!SERVER_CALL())
1920 wake_bits = req->wake_bits;
1921 changed_bits = req->changed_bits;
1924 SERVER_END_REQ;
1926 if (changed_bits & mask) continue;
1927 if (wake_bits & QS_SENDMESSAGE) continue;
1929 TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
1930 queue->self, mask, wake_bits, changed_bits );
1932 ReleaseThunkLock( &dwlc );
1933 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1934 USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
1935 else
1936 WaitForSingleObject( queue->server_queue, INFINITE );
1937 if (dwlc) RestoreThunkLock( dwlc );
1940 WIN_RestoreWndsLock( locks );
1942 return (msg->message != WM_QUIT);
1946 /***********************************************************************
1947 * GetMessageA (USER32.@)
1949 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
1951 GetMessageW( msg, hwnd, first, last );
1952 msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
1953 return (msg->message != WM_QUIT);
1957 /***********************************************************************
1958 * SetMessageQueue (USER32.@)
1960 BOOL WINAPI SetMessageQueue( INT size )
1962 /* now obsolete the message queue will be expanded dynamically as necessary */
1963 return TRUE;