Store the hardware messages in the thread input structure, not in the
[wine/multimedia.git] / dlls / user / message.c
blob01a48fb15872d381c5627b8f5f7be3cfd9bb75d8
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 "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winerror.h"
28 #include "winnls.h"
29 #include "dde.h"
30 #include "wine/unicode.h"
31 #include "wine/server.h"
32 #include "message.h"
33 #include "user.h"
34 #include "win.h"
35 #include "winproc.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msg);
40 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
41 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
43 #define MAX_PACK_COUNT 4
45 /* description of the data fields that need to be packed along with a sent message */
46 struct packed_message
48 int count;
49 const void *data[MAX_PACK_COUNT];
50 size_t size[MAX_PACK_COUNT];
53 /* info about the message currently being received by the current thread */
54 struct received_message_info
56 enum message_type type;
57 MSG msg;
58 UINT flags; /* InSendMessageEx return flags */
61 /* structure to group all parameters for sent messages of the various kinds */
62 struct send_message_info
64 enum message_type type;
65 HWND hwnd;
66 UINT msg;
67 WPARAM wparam;
68 LPARAM lparam;
69 UINT flags; /* flags for SendMessageTimeout */
70 UINT timeout; /* timeout for SendMessageTimeout */
71 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
72 ULONG_PTR data; /* callback data */
76 /* flag for messages that contain pointers */
77 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
79 #define SET(msg) (1 << ((msg) & 31))
81 static const unsigned int message_pointer_flags[] =
83 /* 0x00 - 0x1f */
84 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
85 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
86 /* 0x20 - 0x3f */
87 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
88 SET(WM_COMPAREITEM),
89 /* 0x40 - 0x5f */
90 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
91 SET(WM_NOTIFY) | SET(WM_HELP),
92 /* 0x60 - 0x7f */
93 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
94 /* 0x80 - 0x9f */
95 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
96 /* 0xa0 - 0xbf */
97 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
98 /* 0xc0 - 0xdf */
99 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
100 /* 0xe0 - 0xff */
101 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO),
102 /* 0x100 - 0x11f */
104 /* 0x120 - 0x13f */
106 /* 0x140 - 0x15f */
107 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
108 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
109 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
110 /* 0x160 - 0x17f */
112 /* 0x180 - 0x19f */
113 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
114 SET(LB_DIR) | SET(LB_FINDSTRING) |
115 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
116 /* 0x1a0 - 0x1bf */
117 SET(LB_FINDSTRINGEXACT),
118 /* 0x1c0 - 0x1df */
120 /* 0x1e0 - 0x1ff */
122 /* 0x200 - 0x21f */
123 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
124 /* 0x220 - 0x23f */
125 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
126 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
127 /* 0x240 - 0x25f */
129 /* 0x260 - 0x27f */
131 /* 0x280 - 0x29f */
133 /* 0x2a0 - 0x2bf */
135 /* 0x2c0 - 0x2df */
137 /* 0x2e0 - 0x2ff */
139 /* 0x300 - 0x31f */
140 SET(WM_ASKCBFORMATNAME)
143 /* flags for messages that contain Unicode strings */
144 static const unsigned int message_unicode_flags[] =
146 /* 0x00 - 0x1f */
147 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
148 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
149 /* 0x20 - 0x3f */
150 SET(WM_CHARTOITEM),
151 /* 0x40 - 0x5f */
153 /* 0x60 - 0x7f */
155 /* 0x80 - 0x9f */
156 SET(WM_NCCREATE),
157 /* 0xa0 - 0xbf */
159 /* 0xc0 - 0xdf */
160 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
161 /* 0xe0 - 0xff */
163 /* 0x100 - 0x11f */
164 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
165 /* 0x120 - 0x13f */
166 SET(WM_MENUCHAR),
167 /* 0x140 - 0x15f */
168 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
169 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
170 /* 0x160 - 0x17f */
172 /* 0x180 - 0x19f */
173 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
174 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
175 /* 0x1a0 - 0x1bf */
176 SET(LB_FINDSTRINGEXACT),
177 /* 0x1c0 - 0x1df */
179 /* 0x1e0 - 0x1ff */
181 /* 0x200 - 0x21f */
183 /* 0x220 - 0x23f */
184 SET(WM_MDICREATE),
185 /* 0x240 - 0x25f */
187 /* 0x260 - 0x27f */
189 /* 0x280 - 0x29f */
191 /* 0x2a0 - 0x2bf */
193 /* 0x2c0 - 0x2df */
195 /* 0x2e0 - 0x2ff */
197 /* 0x300 - 0x31f */
198 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
201 /* check whether a given message type includes pointers */
202 inline static int is_pointer_message( UINT message )
204 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
205 return (message_pointer_flags[message / 32] & SET(message)) != 0;
208 /* check whether a given message type contains Unicode (or ASCII) chars */
209 inline static int is_unicode_message( UINT message )
211 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
212 return (message_unicode_flags[message / 32] & SET(message)) != 0;
215 #undef SET
217 /* add a data field to a packed message */
218 inline static void push_data( struct packed_message *data, const void *ptr, size_t size )
220 data->data[data->count] = ptr;
221 data->size[data->count] = size;
222 data->count++;
225 /* add a string to a packed message */
226 inline static void push_string( struct packed_message *data, LPCWSTR str )
228 push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
231 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
232 inline static void *get_data( void **buffer, size_t size )
234 void *ret = *buffer;
235 *buffer = (char *)*buffer + size;
236 return ret;
239 /* make sure that the buffer contains a valid null-terminated Unicode string */
240 inline static BOOL check_string( LPCWSTR str, size_t size )
242 for (size /= sizeof(WCHAR); size; size--, str++)
243 if (!*str) return TRUE;
244 return FALSE;
247 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
248 inline static void *get_buffer_space( void **buffer, size_t size )
250 void *ret;
251 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
252 HeapFree( GetProcessHeap(), 0, *buffer );
253 *buffer = ret;
254 return ret;
257 /* retrieve a string pointer from packed data */
258 inline static LPWSTR get_string( void **buffer )
260 return get_data( buffer, (strlenW( (LPWSTR)*buffer ) + 1) * sizeof(WCHAR) );
263 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
264 inline static BOOL combobox_has_strings( HWND hwnd )
266 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
267 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
270 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
271 inline static BOOL listbox_has_strings( HWND hwnd )
273 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
274 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
277 /* check if hwnd is a broadcast magic handle */
278 inline static BOOL is_broadcast( HWND hwnd )
280 return (hwnd == HWND_BROADCAST || hwnd == HWND_TOPMOST);
284 /***********************************************************************
285 * broadcast_message_callback
287 * Helper callback for broadcasting messages.
289 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
291 struct send_message_info *info = (struct send_message_info *)lparam;
292 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
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 if (message == WM_CHARTOITEM ||
329 message == EM_SETPASSWORDCHAR ||
330 message == WM_CHAR ||
331 message == WM_DEADCHAR ||
332 message == WM_SYSCHAR ||
333 message == WM_SYSDEADCHAR ||
334 message == WM_MENUCHAR)
336 char ch = LOWORD(wparam);
337 WCHAR wch;
338 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
339 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
341 return wparam;
345 /***********************************************************************
346 * map_wparam_WtoA
348 * Convert the wparam of a Unicode message to ASCII.
350 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
352 if (message == WM_CHARTOITEM ||
353 message == EM_SETPASSWORDCHAR ||
354 message == WM_CHAR ||
355 message == WM_DEADCHAR ||
356 message == WM_SYSCHAR ||
357 message == WM_SYSDEADCHAR ||
358 message == WM_MENUCHAR)
360 WCHAR wch = LOWORD(wparam);
361 char ch;
362 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
363 wparam = MAKEWPARAM( (unsigned char)ch, HIWORD(wparam) );
365 return wparam;
369 /***********************************************************************
370 * pack_message
372 * Pack a message for sending to another process.
373 * Return the size of the data we expect in the message reply.
374 * Set data->count to -1 if there is an error.
376 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
377 struct packed_message *data )
379 data->count = 0;
380 switch(message)
382 case WM_NCCREATE:
383 case WM_CREATE:
385 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
386 push_data( data, cs, sizeof(*cs) );
387 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
388 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
389 return sizeof(*cs);
391 case WM_GETTEXT:
392 case WM_ASKCBFORMATNAME:
393 return wparam * sizeof(WCHAR);
394 case WM_SETTEXT:
395 case WM_WININICHANGE:
396 case WM_DEVMODECHANGE:
397 case CB_DIR:
398 case LB_DIR:
399 case LB_ADDFILE:
400 case EM_REPLACESEL:
401 push_string( data, (LPWSTR)lparam );
402 return 0;
403 case WM_GETMINMAXINFO:
404 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
405 return sizeof(MINMAXINFO);
406 case WM_DRAWITEM:
407 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
408 return 0;
409 case WM_MEASUREITEM:
410 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
411 return sizeof(MEASUREITEMSTRUCT);
412 case WM_DELETEITEM:
413 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
414 return 0;
415 case WM_COMPAREITEM:
416 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
417 return 0;
418 case WM_WINDOWPOSCHANGING:
419 case WM_WINDOWPOSCHANGED:
420 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
421 return sizeof(WINDOWPOS);
422 case WM_COPYDATA:
424 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
425 push_data( data, cp, sizeof(*cp) );
426 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
427 return 0;
429 case WM_NOTIFY:
430 /* WM_NOTIFY cannot be sent across processes (MSDN) */
431 data->count = -1;
432 return 0;
433 case WM_HELP:
434 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
435 return 0;
436 case WM_STYLECHANGING:
437 case WM_STYLECHANGED:
438 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
439 return 0;
440 case WM_NCCALCSIZE:
441 if (!wparam)
443 push_data( data, (RECT *)lparam, sizeof(RECT) );
444 return sizeof(RECT);
446 else
448 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
449 push_data( data, nc, sizeof(*nc) );
450 push_data( data, nc->lppos, sizeof(*nc->lppos) );
451 return sizeof(*nc) + sizeof(*nc->lppos);
453 case WM_GETDLGCODE:
454 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
455 return sizeof(MSG);
456 case SBM_SETSCROLLINFO:
457 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
458 return 0;
459 case SBM_GETSCROLLINFO:
460 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
461 return sizeof(SCROLLINFO);
462 case EM_GETSEL:
463 case SBM_GETRANGE:
464 case CB_GETEDITSEL:
466 size_t size = 0;
467 if (wparam) size += sizeof(DWORD);
468 if (lparam) size += sizeof(DWORD);
469 return size;
471 case EM_GETRECT:
472 case LB_GETITEMRECT:
473 case CB_GETDROPPEDCONTROLRECT:
474 return sizeof(RECT);
475 case EM_SETRECT:
476 case EM_SETRECTNP:
477 push_data( data, (RECT *)lparam, sizeof(RECT) );
478 return 0;
479 case EM_GETLINE:
481 WORD *pw = (WORD *)lparam;
482 push_data( data, pw, sizeof(*pw) );
483 return *pw * sizeof(WCHAR);
485 case EM_SETTABSTOPS:
486 case LB_SETTABSTOPS:
487 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
488 return 0;
489 case CB_ADDSTRING:
490 case CB_INSERTSTRING:
491 case CB_FINDSTRING:
492 case CB_FINDSTRINGEXACT:
493 case CB_SELECTSTRING:
494 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
495 return 0;
496 case CB_GETLBTEXT:
497 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
498 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
499 case LB_ADDSTRING:
500 case LB_INSERTSTRING:
501 case LB_FINDSTRING:
502 case LB_FINDSTRINGEXACT:
503 case LB_SELECTSTRING:
504 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
505 return 0;
506 case LB_GETTEXT:
507 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
508 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
509 case LB_GETSELITEMS:
510 return wparam * sizeof(UINT);
511 case WM_NEXTMENU:
512 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
513 return sizeof(MDINEXTMENU);
514 case WM_SIZING:
515 case WM_MOVING:
516 push_data( data, (RECT *)lparam, sizeof(RECT) );
517 return sizeof(RECT);
518 case WM_MDICREATE:
520 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
521 push_data( data, cs, sizeof(*cs) );
522 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
523 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
524 return sizeof(*cs);
526 case WM_MDIGETACTIVE:
527 if (lparam) return sizeof(BOOL);
528 return 0;
529 case WM_WINE_SETWINDOWPOS:
530 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
531 return 0;
533 /* these contain an HFONT */
534 case WM_SETFONT:
535 case WM_GETFONT:
536 /* these contain an HDC */
537 case WM_PAINT:
538 case WM_ERASEBKGND:
539 case WM_ICONERASEBKGND:
540 case WM_NCPAINT:
541 case WM_CTLCOLORMSGBOX:
542 case WM_CTLCOLOREDIT:
543 case WM_CTLCOLORLISTBOX:
544 case WM_CTLCOLORBTN:
545 case WM_CTLCOLORDLG:
546 case WM_CTLCOLORSCROLLBAR:
547 case WM_CTLCOLORSTATIC:
548 case WM_PRINT:
549 case WM_PRINTCLIENT:
550 /* these contain an HGLOBAL */
551 case WM_PAINTCLIPBOARD:
552 case WM_SIZECLIPBOARD:
553 /* these contain pointers */
554 case WM_DROPOBJECT:
555 case WM_QUERYDROPOBJECT:
556 case WM_DRAGLOOP:
557 case WM_DRAGSELECT:
558 case WM_DRAGMOVE:
559 case WM_DEVICECHANGE:
560 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
561 data->count = -1;
562 return 0;
564 return 0;
568 /***********************************************************************
569 * unpack_message
571 * Unpack a message received from another process.
573 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
574 void **buffer, size_t size )
576 size_t minsize = 0;
578 switch(message)
580 case WM_NCCREATE:
581 case WM_CREATE:
583 CREATESTRUCTW *cs = *buffer;
584 WCHAR *str = (WCHAR *)(cs + 1);
585 if (size < sizeof(*cs)) return FALSE;
586 size -= sizeof(*cs);
587 if (HIWORD(cs->lpszName))
589 if (!check_string( str, size )) return FALSE;
590 cs->lpszName = str;
591 size -= (strlenW(str) + 1) * sizeof(WCHAR);
592 str += strlenW(str) + 1;
594 if (HIWORD(cs->lpszClass))
596 if (!check_string( str, size )) return FALSE;
597 cs->lpszClass = str;
599 break;
601 case WM_GETTEXT:
602 case WM_ASKCBFORMATNAME:
603 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
604 break;
605 case WM_SETTEXT:
606 case WM_WININICHANGE:
607 case WM_DEVMODECHANGE:
608 case CB_DIR:
609 case LB_DIR:
610 case LB_ADDFILE:
611 case EM_REPLACESEL:
612 if (!check_string( *buffer, size )) return FALSE;
613 break;
614 case WM_GETMINMAXINFO:
615 minsize = sizeof(MINMAXINFO);
616 break;
617 case WM_DRAWITEM:
618 minsize = sizeof(DRAWITEMSTRUCT);
619 break;
620 case WM_MEASUREITEM:
621 minsize = sizeof(MEASUREITEMSTRUCT);
622 break;
623 case WM_DELETEITEM:
624 minsize = sizeof(DELETEITEMSTRUCT);
625 break;
626 case WM_COMPAREITEM:
627 minsize = sizeof(COMPAREITEMSTRUCT);
628 break;
629 case WM_WINDOWPOSCHANGING:
630 case WM_WINDOWPOSCHANGED:
631 case WM_WINE_SETWINDOWPOS:
632 minsize = sizeof(WINDOWPOS);
633 break;
634 case WM_COPYDATA:
636 COPYDATASTRUCT *cp = *buffer;
637 if (size < sizeof(*cp)) return FALSE;
638 if (cp->lpData)
640 minsize = sizeof(*cp) + cp->cbData;
641 cp->lpData = cp + 1;
643 break;
645 case WM_NOTIFY:
646 /* WM_NOTIFY cannot be sent across processes (MSDN) */
647 return FALSE;
648 case WM_HELP:
649 minsize = sizeof(HELPINFO);
650 break;
651 case WM_STYLECHANGING:
652 case WM_STYLECHANGED:
653 minsize = sizeof(STYLESTRUCT);
654 break;
655 case WM_NCCALCSIZE:
656 if (!*wparam) minsize = sizeof(RECT);
657 else
659 NCCALCSIZE_PARAMS *nc = *buffer;
660 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
661 nc->lppos = (WINDOWPOS *)(nc + 1);
663 break;
664 case WM_GETDLGCODE:
665 if (!*lparam) return TRUE;
666 minsize = sizeof(MSG);
667 break;
668 case SBM_SETSCROLLINFO:
669 minsize = sizeof(SCROLLINFO);
670 break;
671 case SBM_GETSCROLLINFO:
672 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
673 break;
674 case EM_GETSEL:
675 case SBM_GETRANGE:
676 case CB_GETEDITSEL:
677 if (*wparam || *lparam)
679 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
680 if (*wparam) *wparam = (WPARAM)*buffer;
681 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
683 return TRUE;
684 case EM_GETRECT:
685 case LB_GETITEMRECT:
686 case CB_GETDROPPEDCONTROLRECT:
687 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
688 break;
689 case EM_SETRECT:
690 case EM_SETRECTNP:
691 minsize = sizeof(RECT);
692 break;
693 case EM_GETLINE:
695 WORD len;
696 if (size < sizeof(WORD)) return FALSE;
697 len = *(WORD *)*buffer;
698 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
699 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
700 return TRUE;
702 case EM_SETTABSTOPS:
703 case LB_SETTABSTOPS:
704 if (!*wparam) return TRUE;
705 minsize = *wparam * sizeof(UINT);
706 break;
707 case CB_ADDSTRING:
708 case CB_INSERTSTRING:
709 case CB_FINDSTRING:
710 case CB_FINDSTRINGEXACT:
711 case CB_SELECTSTRING:
712 case LB_ADDSTRING:
713 case LB_INSERTSTRING:
714 case LB_FINDSTRING:
715 case LB_FINDSTRINGEXACT:
716 case LB_SELECTSTRING:
717 if (!*buffer) return TRUE;
718 if (!check_string( *buffer, size )) return FALSE;
719 break;
720 case CB_GETLBTEXT:
722 size = sizeof(ULONG_PTR);
723 if (combobox_has_strings( hwnd ))
724 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
725 if (!get_buffer_space( buffer, size )) return FALSE;
726 break;
728 case LB_GETTEXT:
730 size = sizeof(ULONG_PTR);
731 if (listbox_has_strings( hwnd ))
732 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
733 if (!get_buffer_space( buffer, size )) return FALSE;
734 break;
736 case LB_GETSELITEMS:
737 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
738 break;
739 case WM_NEXTMENU:
740 minsize = sizeof(MDINEXTMENU);
741 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
742 break;
743 case WM_SIZING:
744 case WM_MOVING:
745 minsize = sizeof(RECT);
746 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
747 break;
748 case WM_MDICREATE:
750 MDICREATESTRUCTW *cs = *buffer;
751 WCHAR *str = (WCHAR *)(cs + 1);
752 if (size < sizeof(*cs)) return FALSE;
753 size -= sizeof(*cs);
754 if (HIWORD(cs->szTitle))
756 if (!check_string( str, size )) return FALSE;
757 cs->szTitle = str;
758 size -= (strlenW(str) + 1) * sizeof(WCHAR);
759 str += strlenW(str) + 1;
761 if (HIWORD(cs->szClass))
763 if (!check_string( str, size )) return FALSE;
764 cs->szClass = str;
766 break;
768 case WM_MDIGETACTIVE:
769 if (!*lparam) return TRUE;
770 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
771 break;
772 /* these contain an HFONT */
773 case WM_SETFONT:
774 case WM_GETFONT:
775 /* these contain an HDC */
776 case WM_PAINT:
777 case WM_ERASEBKGND:
778 case WM_ICONERASEBKGND:
779 case WM_NCPAINT:
780 case WM_CTLCOLORMSGBOX:
781 case WM_CTLCOLOREDIT:
782 case WM_CTLCOLORLISTBOX:
783 case WM_CTLCOLORBTN:
784 case WM_CTLCOLORDLG:
785 case WM_CTLCOLORSCROLLBAR:
786 case WM_CTLCOLORSTATIC:
787 case WM_PRINT:
788 case WM_PRINTCLIENT:
789 /* these contain an HGLOBAL */
790 case WM_PAINTCLIPBOARD:
791 case WM_SIZECLIPBOARD:
792 /* these contain pointers */
793 case WM_DROPOBJECT:
794 case WM_QUERYDROPOBJECT:
795 case WM_DRAGLOOP:
796 case WM_DRAGSELECT:
797 case WM_DRAGMOVE:
798 case WM_DEVICECHANGE:
799 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
800 return FALSE;
802 default:
803 return TRUE; /* message doesn't need any unpacking */
806 /* default exit for most messages: check minsize and store buffer in lparam */
807 if (size < minsize) return FALSE;
808 *lparam = (LPARAM)*buffer;
809 return TRUE;
813 /***********************************************************************
814 * pack_reply
816 * Pack a reply to a message for sending to another process.
818 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
819 LRESULT res, struct packed_message *data )
821 data->count = 0;
822 switch(message)
824 case WM_NCCREATE:
825 case WM_CREATE:
826 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
827 break;
828 case WM_GETTEXT:
829 case CB_GETLBTEXT:
830 case LB_GETTEXT:
831 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
832 break;
833 case WM_GETMINMAXINFO:
834 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
835 break;
836 case WM_MEASUREITEM:
837 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
838 break;
839 case WM_WINDOWPOSCHANGING:
840 case WM_WINDOWPOSCHANGED:
841 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
842 break;
843 case WM_GETDLGCODE:
844 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
845 break;
846 case SBM_GETSCROLLINFO:
847 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
848 break;
849 case EM_GETRECT:
850 case LB_GETITEMRECT:
851 case CB_GETDROPPEDCONTROLRECT:
852 case WM_SIZING:
853 case WM_MOVING:
854 push_data( data, (RECT *)lparam, sizeof(RECT) );
855 break;
856 case EM_GETLINE:
858 WORD *ptr = (WORD *)lparam;
859 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
860 break;
862 case LB_GETSELITEMS:
863 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
864 break;
865 case WM_MDIGETACTIVE:
866 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
867 break;
868 case WM_NCCALCSIZE:
869 if (!wparam)
870 push_data( data, (RECT *)lparam, sizeof(RECT) );
871 else
873 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
874 push_data( data, nc, sizeof(*nc) );
875 push_data( data, nc->lppos, sizeof(*nc->lppos) );
877 break;
878 case EM_GETSEL:
879 case SBM_GETRANGE:
880 case CB_GETEDITSEL:
881 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
882 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
883 break;
884 case WM_NEXTMENU:
885 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
886 break;
887 case WM_MDICREATE:
888 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
889 break;
890 case WM_ASKCBFORMATNAME:
891 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
892 break;
897 /***********************************************************************
898 * unpack_reply
900 * Unpack a message reply received from another process.
902 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
903 void *buffer, size_t size )
905 switch(message)
907 case WM_NCCREATE:
908 case WM_CREATE:
910 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
911 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
912 memcpy( cs, buffer, min( sizeof(*cs), size ));
913 cs->lpszName = name; /* restore the original pointers */
914 cs->lpszClass = class;
915 break;
917 case WM_GETTEXT:
918 case WM_ASKCBFORMATNAME:
919 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
920 break;
921 case WM_GETMINMAXINFO:
922 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
923 break;
924 case WM_MEASUREITEM:
925 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
926 break;
927 case WM_WINDOWPOSCHANGING:
928 case WM_WINDOWPOSCHANGED:
929 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
930 break;
931 case WM_GETDLGCODE:
932 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
933 break;
934 case SBM_GETSCROLLINFO:
935 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
936 break;
937 case EM_GETRECT:
938 case CB_GETDROPPEDCONTROLRECT:
939 case LB_GETITEMRECT:
940 case WM_SIZING:
941 case WM_MOVING:
942 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
943 break;
944 case EM_GETLINE:
945 size = min( size, (size_t)*(WORD *)lparam );
946 memcpy( (WCHAR *)lparam, buffer, size );
947 break;
948 case LB_GETSELITEMS:
949 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
950 break;
951 case LB_GETTEXT:
952 case CB_GETLBTEXT:
953 memcpy( (WCHAR *)lparam, buffer, size );
954 break;
955 case WM_NEXTMENU:
956 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
957 break;
958 case WM_MDIGETACTIVE:
959 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
960 break;
961 case WM_NCCALCSIZE:
962 if (!wparam)
963 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
964 else
966 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
967 WINDOWPOS *wp = nc->lppos;
968 memcpy( nc, buffer, min( sizeof(*nc), size ));
969 if (size > sizeof(*nc))
971 size -= sizeof(*nc);
972 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
974 nc->lppos = wp; /* restore the original pointer */
976 break;
977 case EM_GETSEL:
978 case SBM_GETRANGE:
979 case CB_GETEDITSEL:
980 if (wparam)
982 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
983 if (size <= sizeof(DWORD)) break;
984 size -= sizeof(DWORD);
985 buffer = (DWORD *)buffer + 1;
987 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
988 break;
989 case WM_MDICREATE:
991 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
992 LPCWSTR title = cs->szTitle, class = cs->szClass;
993 memcpy( cs, buffer, min( sizeof(*cs), size ));
994 cs->szTitle = title; /* restore the original pointers */
995 cs->szClass = class;
996 break;
998 default:
999 ERR( "should not happen: unexpected message %x\n", message );
1000 break;
1005 /***********************************************************************
1006 * reply_message
1008 * Send a reply to a sent message.
1010 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1012 struct packed_message data;
1013 int i, replied = info->flags & ISMEX_REPLIED;
1015 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1016 if (!remove && replied) return; /* replied already */
1018 data.count = 0;
1019 info->flags |= ISMEX_REPLIED;
1021 if (info->type == MSG_OTHER_PROCESS && !replied)
1023 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1024 info->msg.lParam, result, &data );
1027 SERVER_START_REQ( reply_message )
1029 req->result = result;
1030 req->remove = remove;
1031 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1032 wine_server_call( req );
1034 SERVER_END_REQ;
1038 /***********************************************************************
1039 * handle_internal_message
1041 * Handle an internal Wine message instead of calling the window proc.
1043 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1045 if (hwnd == GetDesktopWindow()) return 0;
1046 switch(msg)
1048 case WM_WINE_DESTROYWINDOW:
1049 return WIN_DestroyWindow( hwnd );
1050 case WM_WINE_SETWINDOWPOS:
1051 return USER_Driver.pSetWindowPos( (WINDOWPOS *)lparam );
1052 case WM_WINE_SHOWWINDOW:
1053 return ShowWindow( hwnd, wparam );
1054 case WM_WINE_SETPARENT:
1055 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1056 case WM_WINE_SETWINDOWLONG:
1057 return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1058 case WM_WINE_ENABLEWINDOW:
1059 return EnableWindow( hwnd, wparam );
1060 case WM_WINE_SETACTIVEWINDOW:
1061 return (LRESULT)SetActiveWindow( (HWND)wparam );
1062 default:
1063 FIXME( "unknown internal message %x\n", msg );
1064 return 0;
1068 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1069 * to the memory handle, we keep track (in the server side) of all pairs of handle
1070 * used (the client passes its value and the content of the memory handle), and
1071 * the server stored both values (the client, and the local one, created after the
1072 * content). When a ACK message is generated, the list of pair is searched for a
1073 * matching pair, so that the client memory handle can be returned.
1075 struct DDE_pair {
1076 HGLOBAL client_hMem;
1077 HGLOBAL server_hMem;
1080 static struct DDE_pair* dde_pairs;
1081 static int dde_num_alloc;
1082 static int dde_num_used;
1083 static CRITICAL_SECTION dde_crst = CRITICAL_SECTION_INIT("Raw_DDE_CritSect");
1085 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1087 int i;
1088 #define GROWBY 4
1090 EnterCriticalSection(&dde_crst);
1092 /* now remember the pair of hMem on both sides */
1093 if (dde_num_used == dde_num_alloc)
1095 struct DDE_pair* tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1096 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1097 if (!tmp)
1099 LeaveCriticalSection(&dde_crst);
1100 return FALSE;
1102 dde_pairs = tmp;
1103 /* zero out newly allocated part */
1104 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1105 dde_num_alloc += GROWBY;
1107 #undef GROWBY
1108 for (i = 0; i < dde_num_alloc; i++)
1110 if (dde_pairs[i].server_hMem == 0)
1112 dde_pairs[i].client_hMem = chm;
1113 dde_pairs[i].server_hMem = shm;
1114 dde_num_used++;
1115 break;
1118 LeaveCriticalSection(&dde_crst);
1119 return TRUE;
1122 static HGLOBAL dde_get_pair(HGLOBAL shm)
1124 int i;
1125 HGLOBAL ret = 0;
1127 EnterCriticalSection(&dde_crst);
1128 for (i = 0; i < dde_num_alloc; i++)
1130 if (dde_pairs[i].server_hMem == shm)
1132 /* free this pair */
1133 dde_pairs[i].server_hMem = 0;
1134 dde_num_used--;
1135 ret = dde_pairs[i].client_hMem;
1136 break;
1139 LeaveCriticalSection(&dde_crst);
1140 return ret;
1143 /***********************************************************************
1144 * post_dde_message
1146 * Post a DDE messag
1148 static BOOL post_dde_message( DWORD dest_tid, struct packed_message *data, const struct send_message_info *info )
1150 void* ptr = NULL;
1151 int size = 0;
1152 UINT uiLo, uiHi;
1153 LPARAM lp = 0;
1154 HGLOBAL hunlock = 0;
1155 int i;
1156 DWORD res;
1158 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1159 return FALSE;
1161 lp = info->lparam;
1162 switch (info->msg)
1164 /* DDE messages which don't require packing are:
1165 * WM_DDE_INITIATE
1166 * WM_DDE_TERMINATE
1167 * WM_DDE_REQUEST
1168 * WM_DDE_UNADVISE
1170 case WM_DDE_ACK:
1171 if (HIWORD(uiHi))
1173 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1174 HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1175 if (h)
1177 /* send back the value of h on the other side */
1178 push_data( data, &h, sizeof(HGLOBAL) );
1179 lp = uiLo;
1180 TRACE( "send dde-ack %x %08x => %08lx\n", uiLo, uiHi, (DWORD)h );
1183 else
1185 /* uiHi should contain either an atom or 0 */
1186 TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
1187 lp = MAKELONG( uiLo, uiHi );
1189 break;
1190 case WM_DDE_ADVISE:
1191 case WM_DDE_DATA:
1192 case WM_DDE_POKE:
1193 size = 0;
1194 if (uiLo)
1196 size = GlobalSize( (HGLOBAL)uiLo ) ;
1197 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1198 (info->msg == WM_DDE_DATA && size < sizeof(DDEDATA)) ||
1199 (info->msg == WM_DDE_POKE && size < sizeof(DDEPOKE))
1201 return FALSE;
1203 else if (info->msg != WM_DDE_DATA) return FALSE;
1205 lp = uiHi;
1206 if (uiLo)
1208 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1210 push_data( data, ptr, size );
1211 hunlock = (HGLOBAL)uiLo;
1214 TRACE( "send ddepack %u %x\n", size, uiHi );
1215 break;
1216 case WM_DDE_EXECUTE:
1217 if (info->lparam)
1219 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1221 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1222 /* so that the other side can send it back on ACK */
1223 lp = info->lparam;
1224 hunlock = (HGLOBAL)info->lparam;
1227 break;
1229 SERVER_START_REQ( send_message )
1231 req->id = dest_tid;
1232 req->type = info->type;
1233 req->win = info->hwnd;
1234 req->msg = info->msg;
1235 req->wparam = info->wparam;
1236 req->lparam = lp;
1237 req->time = GetCurrentTime();
1238 req->timeout = -1;
1239 for (i = 0; i < data->count; i++)
1240 wine_server_add_data( req, data->data[i], data->size[i] );
1241 if ((res = wine_server_call( req )))
1243 if (res == STATUS_INVALID_PARAMETER)
1244 /* FIXME: find a STATUS_ value for this one */
1245 SetLastError( ERROR_INVALID_THREAD_ID );
1246 else
1247 SetLastError( RtlNtStatusToDosError(res) );
1249 else
1250 FreeDDElParam(info->msg, info->lparam);
1252 SERVER_END_REQ;
1253 if (hunlock) GlobalUnlock(hunlock);
1255 return !res;
1258 /***********************************************************************
1259 * unpack_dde_message
1261 * Unpack a posted DDE message received from another process.
1263 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1264 void **buffer, size_t size )
1266 UINT uiLo, uiHi;
1267 HGLOBAL hMem = 0;
1268 void* ptr;
1270 switch (message)
1272 case WM_DDE_ACK:
1273 if (size)
1275 /* hMem is being passed */
1276 if (size != sizeof(HGLOBAL)) return FALSE;
1277 if (!buffer || !*buffer) return FALSE;
1278 uiLo = *lparam;
1279 memcpy( &hMem, *buffer, size );
1280 uiHi = (UINT)hMem;
1281 TRACE("recv dde-ack %u mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1283 else
1285 uiLo = LOWORD( *lparam );
1286 uiHi = HIWORD( *lparam );
1287 TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
1289 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1290 break;
1291 case WM_DDE_ADVISE:
1292 case WM_DDE_DATA:
1293 case WM_DDE_POKE:
1294 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1295 uiHi = *lparam;
1296 TRACE( "recv ddepack %u %x\n", size, uiHi );
1297 if (size)
1299 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1300 if (hMem && (ptr = GlobalLock( hMem )))
1302 memcpy( ptr, *buffer, size );
1303 GlobalUnlock( hMem );
1305 else return FALSE;
1307 uiLo = (UINT)hMem;
1309 *lparam = PackDDElParam( message, uiLo, uiHi );
1310 break;
1311 case WM_DDE_EXECUTE:
1312 if (size)
1314 if (!buffer || !*buffer) return FALSE;
1315 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1316 if (hMem && (ptr = GlobalLock( hMem )))
1318 memcpy( ptr, *buffer, size );
1319 GlobalUnlock( hMem );
1320 TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
1321 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1323 GlobalFree( hMem );
1324 return FALSE;
1327 } else return FALSE;
1328 *lparam = (LPARAM)hMem;
1329 break;
1331 return TRUE;
1334 /***********************************************************************
1335 * call_window_proc
1337 * Call a window procedure and the corresponding hooks.
1339 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1340 BOOL unicode, BOOL same_thread )
1342 LRESULT result = 0;
1343 WNDPROC winproc;
1344 CWPSTRUCT cwp;
1345 CWPRETSTRUCT cwpret;
1346 MESSAGEQUEUE *queue = QUEUE_Current();
1348 if (queue->recursion_count > MAX_SENDMSG_RECURSION) return 0;
1349 queue->recursion_count++;
1351 if (msg & 0x80000000)
1353 result = handle_internal_message( hwnd, msg, wparam, lparam );
1354 goto done;
1357 /* first the WH_CALLWNDPROC hook */
1358 hwnd = WIN_GetFullHandle( hwnd );
1359 cwp.lParam = lparam;
1360 cwp.wParam = wparam;
1361 cwp.message = msg;
1362 cwp.hwnd = hwnd;
1363 HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1365 /* now call the window procedure */
1366 if (unicode)
1368 if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) goto done;
1369 result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1371 else
1373 if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) goto done;
1374 result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1377 /* and finally the WH_CALLWNDPROCRET hook */
1378 cwpret.lResult = result;
1379 cwpret.lParam = lparam;
1380 cwpret.wParam = wparam;
1381 cwpret.message = msg;
1382 cwpret.hwnd = hwnd;
1383 HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1384 done:
1385 queue->recursion_count--;
1386 return result;
1390 /***********************************************************************
1391 * process_hardware_message
1393 * Process a hardware message; return TRUE if message should be passed on to the app
1395 static BOOL process_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd,
1396 UINT first, UINT last, BOOL remove )
1398 BOOL ret;
1400 if (!MSG_process_raw_hardware_message( msg, extra_info, hwnd, first, last, remove ))
1401 return FALSE;
1403 ret = MSG_process_cooked_hardware_message( msg, extra_info, remove );
1405 /* tell the server we have passed it to the app
1406 * (even though we may end up dropping it later on)
1408 SERVER_START_REQ( reply_message )
1410 req->result = 0;
1411 req->remove = remove || !ret;
1412 wine_server_call( req );
1414 SERVER_END_REQ;
1415 return ret;
1419 /***********************************************************************
1420 * MSG_peek_message
1422 * Peek for a message matching the given parameters. Return FALSE if none available.
1423 * All pending sent messages are processed before returning.
1425 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1427 LRESULT result;
1428 ULONG_PTR extra_info = 0;
1429 MESSAGEQUEUE *queue = QUEUE_Current();
1430 struct received_message_info info, *old_info;
1432 if (!first && !last) last = ~0;
1434 for (;;)
1436 NTSTATUS res;
1437 void *buffer = NULL;
1438 size_t size = 0, buffer_size = 0;
1440 do /* loop while buffer is too small */
1442 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1443 return FALSE;
1444 SERVER_START_REQ( get_message )
1446 req->flags = flags;
1447 req->get_win = hwnd;
1448 req->get_first = first;
1449 req->get_last = last;
1450 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1451 if (!(res = wine_server_call( req )))
1453 size = wine_server_reply_size( reply );
1454 info.type = reply->type;
1455 info.msg.hwnd = reply->win;
1456 info.msg.message = reply->msg;
1457 info.msg.wParam = reply->wparam;
1458 info.msg.lParam = reply->lparam;
1459 info.msg.time = reply->time;
1460 info.msg.pt.x = reply->x;
1461 info.msg.pt.y = reply->y;
1462 extra_info = reply->info;
1464 else
1466 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1467 buffer_size = reply->total;
1470 SERVER_END_REQ;
1471 } while (res == STATUS_BUFFER_OVERFLOW);
1473 if (res) return FALSE;
1475 TRACE( "got type %d msg %x hwnd %p wp %x lp %lx\n",
1476 info.type, info.msg.message, info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1478 switch(info.type)
1480 case MSG_ASCII:
1481 case MSG_UNICODE:
1482 info.flags = ISMEX_SEND;
1483 break;
1484 case MSG_NOTIFY:
1485 info.flags = ISMEX_NOTIFY;
1486 break;
1487 case MSG_CALLBACK:
1488 info.flags = ISMEX_CALLBACK;
1489 break;
1490 case MSG_OTHER_PROCESS:
1491 info.flags = ISMEX_SEND;
1492 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1493 &info.msg.lParam, &buffer, size ))
1495 ERR( "invalid packed message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1496 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
1497 info.msg.wParam, info.msg.lParam, size );
1498 /* ignore it */
1499 reply_message( &info, 0, TRUE );
1500 goto next;
1502 break;
1503 case MSG_HARDWARE:
1504 if (!process_hardware_message( &info.msg, extra_info,
1505 hwnd, first, last, flags & GET_MSG_REMOVE ))
1507 TRACE("dropping msg %x\n", info.msg.message );
1508 goto next; /* ignore it */
1510 queue->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
1511 /* fall through */
1512 case MSG_POSTED:
1513 queue->GetMessageExtraInfoVal = extra_info;
1514 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
1516 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1517 &info.msg.lParam, &buffer, size ))
1519 ERR( "invalid packed dde-message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1520 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1521 info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
1522 goto next; /* ignore it */
1525 *msg = info.msg;
1526 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1527 return TRUE;
1530 /* if we get here, we have a sent message; call the window procedure */
1531 old_info = queue->receive_info;
1532 queue->receive_info = &info;
1533 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1534 info.msg.lParam, (info.type != MSG_ASCII), FALSE );
1535 reply_message( &info, result, TRUE );
1536 queue->receive_info = old_info;
1537 next:
1538 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1543 /***********************************************************************
1544 * wait_message_reply
1546 * Wait until a sent message gets replied to.
1548 static void wait_message_reply( UINT flags )
1550 MESSAGEQUEUE *queue;
1552 if (!(queue = QUEUE_Current())) return;
1554 for (;;)
1556 unsigned int wake_bits = 0, changed_bits = 0;
1557 DWORD dwlc, res;
1559 SERVER_START_REQ( set_queue_mask )
1561 req->wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
1562 req->changed_mask = req->wake_mask;
1563 req->skip_wait = 1;
1564 if (!wine_server_call( req ))
1566 wake_bits = reply->wake_bits;
1567 changed_bits = reply->changed_bits;
1570 SERVER_END_REQ;
1572 if (wake_bits & QS_SMRESULT) return; /* got a result */
1573 if (wake_bits & QS_SENDMESSAGE)
1575 /* Process the sent message immediately */
1576 MSG msg;
1577 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1578 continue;
1581 /* now wait for it */
1583 ReleaseThunkLock( &dwlc );
1585 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1586 res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1587 INFINITE, 0, 0 );
1588 else
1589 res = WaitForSingleObject( queue->server_queue, INFINITE );
1591 if (dwlc) RestoreThunkLock( dwlc );
1595 /***********************************************************************
1596 * put_message_in_queue
1598 * Put a sent message into the destination queue.
1599 * For inter-process message, reply_size is set to expected size of reply data.
1601 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1602 size_t *reply_size )
1604 struct packed_message data;
1605 unsigned int res;
1606 int i, timeout = -1;
1608 if (info->type != MSG_NOTIFY &&
1609 info->type != MSG_CALLBACK &&
1610 info->type != MSG_POSTED &&
1611 info->timeout != INFINITE)
1612 timeout = info->timeout;
1614 data.count = 0;
1615 if (info->type == MSG_OTHER_PROCESS)
1617 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1618 if (data.count == -1)
1620 WARN( "cannot pack message %x\n", info->msg );
1621 return FALSE;
1624 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
1626 return post_dde_message( dest_tid, &data, info );
1629 SERVER_START_REQ( send_message )
1631 req->id = dest_tid;
1632 req->type = info->type;
1633 req->win = info->hwnd;
1634 req->msg = info->msg;
1635 req->wparam = info->wparam;
1636 req->lparam = info->lparam;
1637 req->time = GetCurrentTime();
1638 req->timeout = timeout;
1639 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1640 if ((res = wine_server_call( req )))
1642 if (res == STATUS_INVALID_PARAMETER)
1643 /* FIXME: find a STATUS_ value for this one */
1644 SetLastError( ERROR_INVALID_THREAD_ID );
1645 else
1646 SetLastError( RtlNtStatusToDosError(res) );
1649 SERVER_END_REQ;
1650 return !res;
1654 /***********************************************************************
1655 * retrieve_reply
1657 * Retrieve a message reply from the server.
1659 static LRESULT retrieve_reply( const struct send_message_info *info,
1660 size_t reply_size, LRESULT *result )
1662 NTSTATUS status;
1663 void *reply_data = NULL;
1665 if (reply_size)
1667 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
1669 WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
1670 reply_size = 0;
1673 SERVER_START_REQ( get_message_reply )
1675 req->cancel = 1;
1676 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
1677 if (!(status = wine_server_call( req ))) *result = reply->result;
1678 reply_size = wine_server_reply_size( reply );
1680 SERVER_END_REQ;
1681 if (!status && reply_size)
1682 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
1684 if (reply_data) HeapFree( GetProcessHeap(), 0, reply_data );
1686 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1687 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
1688 info->lparam, *result, status );
1690 if (!status) return 1;
1691 if (status == STATUS_TIMEOUT) SetLastError(0); /* timeout */
1692 else SetLastError( RtlNtStatusToDosError(status) );
1693 return 0;
1697 /***********************************************************************
1698 * send_inter_thread_message
1700 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1701 LRESULT *res_ptr )
1703 LRESULT ret;
1704 int locks;
1705 size_t reply_size = 0;
1707 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
1708 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
1710 if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1712 /* there's no reply to wait for on notify/callback messages */
1713 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1715 locks = WIN_SuspendWndsLock();
1717 wait_message_reply( info->flags );
1718 ret = retrieve_reply( info, reply_size, res_ptr );
1720 WIN_RestoreWndsLock( locks );
1721 return ret;
1725 /***********************************************************************
1726 * SendMessageTimeoutW (USER32.@)
1728 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1729 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1731 struct send_message_info info;
1732 DWORD dest_tid, dest_pid;
1733 LRESULT ret, result;
1735 info.type = MSG_UNICODE;
1736 info.hwnd = hwnd;
1737 info.msg = msg;
1738 info.wparam = wparam;
1739 info.lparam = lparam;
1740 info.flags = flags;
1741 info.timeout = timeout;
1743 if (is_broadcast(hwnd))
1745 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1746 if (res_ptr) *res_ptr = 1;
1747 return 1;
1750 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1752 if (USER_IsExitingThread( dest_tid )) return 0;
1754 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1756 if (dest_tid == GetCurrentThreadId())
1758 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1759 ret = 1;
1761 else
1763 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1764 ret = send_inter_thread_message( dest_tid, &info, &result );
1767 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1768 if (ret && res_ptr) *res_ptr = result;
1769 return ret;
1773 /***********************************************************************
1774 * SendMessageTimeoutA (USER32.@)
1776 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1777 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1779 struct send_message_info info;
1780 DWORD dest_tid, dest_pid;
1781 LRESULT ret, result;
1783 info.type = MSG_ASCII;
1784 info.hwnd = hwnd;
1785 info.msg = msg;
1786 info.wparam = wparam;
1787 info.lparam = lparam;
1788 info.flags = flags;
1789 info.timeout = timeout;
1791 if (is_broadcast(hwnd))
1793 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1794 if (res_ptr) *res_ptr = 1;
1795 return 1;
1798 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1800 if (USER_IsExitingThread( dest_tid )) return 0;
1802 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1804 if (dest_tid == GetCurrentThreadId())
1806 result = call_window_proc( hwnd, msg, wparam, lparam, FALSE, TRUE );
1807 ret = 1;
1809 else if (dest_pid == GetCurrentProcessId())
1811 ret = send_inter_thread_message( dest_tid, &info, &result );
1813 else
1815 /* inter-process message: need to map to Unicode */
1816 info.type = MSG_OTHER_PROCESS;
1817 if (is_unicode_message( info.msg ))
1819 if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1820 return 0;
1821 ret = send_inter_thread_message( dest_tid, &info, &result );
1822 result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1823 info.lparam, result );
1825 else ret = send_inter_thread_message( dest_tid, &info, &result );
1827 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1828 if (ret && res_ptr) *res_ptr = result;
1829 return ret;
1833 /***********************************************************************
1834 * SendMessageW (USER32.@)
1836 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1838 LRESULT res = 0;
1839 SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1840 return res;
1844 /***********************************************************************
1845 * SendMessageA (USER32.@)
1847 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1849 LRESULT res = 0;
1850 SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1851 return res;
1855 /***********************************************************************
1856 * SendNotifyMessageA (USER32.@)
1858 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1860 return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1864 /***********************************************************************
1865 * SendNotifyMessageW (USER32.@)
1867 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1869 struct send_message_info info;
1870 DWORD dest_tid;
1871 LRESULT result;
1873 if (is_pointer_message(msg))
1875 SetLastError(ERROR_INVALID_PARAMETER);
1876 return FALSE;
1879 info.type = MSG_NOTIFY;
1880 info.hwnd = hwnd;
1881 info.msg = msg;
1882 info.wparam = wparam;
1883 info.lparam = lparam;
1885 if (is_broadcast(hwnd))
1887 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1888 return TRUE;
1891 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1893 if (USER_IsExitingThread( dest_tid )) return TRUE;
1895 if (dest_tid == GetCurrentThreadId())
1897 call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1898 return TRUE;
1900 return send_inter_thread_message( dest_tid, &info, &result );
1904 /***********************************************************************
1905 * SendMessageCallbackA (USER32.@)
1907 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1908 SENDASYNCPROC callback, ULONG_PTR data )
1910 return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
1911 lparam, callback, data );
1915 /***********************************************************************
1916 * SendMessageCallbackW (USER32.@)
1918 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1919 SENDASYNCPROC callback, ULONG_PTR data )
1921 struct send_message_info info;
1922 LRESULT result;
1923 DWORD dest_tid;
1925 if (is_pointer_message(msg))
1927 SetLastError(ERROR_INVALID_PARAMETER);
1928 return FALSE;
1931 info.type = MSG_CALLBACK;
1932 info.hwnd = hwnd;
1933 info.msg = msg;
1934 info.wparam = wparam;
1935 info.lparam = lparam;
1936 info.callback = callback;
1937 info.data = data;
1939 if (is_broadcast(hwnd))
1941 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1942 return TRUE;
1945 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1947 if (USER_IsExitingThread( dest_tid )) return TRUE;
1949 if (dest_tid == GetCurrentThreadId())
1951 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1952 callback( hwnd, msg, data, result );
1953 return TRUE;
1955 FIXME( "callback will not be called\n" );
1956 return send_inter_thread_message( dest_tid, &info, &result );
1960 /***********************************************************************
1961 * ReplyMessage (USER32.@)
1963 BOOL WINAPI ReplyMessage( LRESULT result )
1965 MESSAGEQUEUE *queue = QUEUE_Current();
1966 struct received_message_info *info = queue->receive_info;
1968 if (!info) return FALSE;
1969 reply_message( info, result, FALSE );
1970 return TRUE;
1974 /***********************************************************************
1975 * InSendMessage (USER32.@)
1977 BOOL WINAPI InSendMessage(void)
1979 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
1983 /***********************************************************************
1984 * InSendMessageEx (USER32.@)
1986 DWORD WINAPI InSendMessageEx( LPVOID reserved )
1988 MESSAGEQUEUE *queue = QUEUE_Current();
1989 struct received_message_info *info = queue->receive_info;
1991 if (info) return info->flags;
1992 return ISMEX_NOSEND;
1996 /***********************************************************************
1997 * PostMessageA (USER32.@)
1999 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2001 return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
2005 /***********************************************************************
2006 * PostMessageW (USER32.@)
2008 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2010 struct send_message_info info;
2011 DWORD dest_tid;
2013 if (is_pointer_message( msg ))
2015 SetLastError( ERROR_INVALID_PARAMETER );
2016 return FALSE;
2019 info.type = MSG_POSTED;
2020 info.hwnd = hwnd;
2021 info.msg = msg;
2022 info.wparam = wparam;
2023 info.lparam = lparam;
2025 if (is_broadcast(hwnd))
2027 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2028 return TRUE;
2031 if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2033 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2035 if (USER_IsExitingThread( dest_tid )) return TRUE;
2037 return put_message_in_queue( dest_tid, &info, NULL );
2041 /**********************************************************************
2042 * PostThreadMessageA (USER32.@)
2044 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2046 return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2050 /**********************************************************************
2051 * PostThreadMessageW (USER32.@)
2053 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2055 struct send_message_info info;
2057 if (is_pointer_message( msg ))
2059 SetLastError( ERROR_INVALID_PARAMETER );
2060 return FALSE;
2062 if (USER_IsExitingThread( thread )) return TRUE;
2064 info.type = MSG_POSTED;
2065 info.hwnd = 0;
2066 info.msg = msg;
2067 info.wparam = wparam;
2068 info.lparam = lparam;
2069 return put_message_in_queue( thread, &info, NULL );
2073 /***********************************************************************
2074 * PostQuitMessage (USER32.@)
2076 void WINAPI PostQuitMessage( INT exitCode )
2078 PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
2082 /***********************************************************************
2083 * PeekMessageW (USER32.@)
2085 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2087 MESSAGEQUEUE *queue;
2088 MSG msg;
2089 int locks;
2091 /* check for graphics events */
2092 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2093 USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
2095 hwnd = WIN_GetFullHandle( hwnd );
2096 locks = WIN_SuspendWndsLock();
2098 if (!MSG_peek_message( &msg, hwnd, first, last,
2099 (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2101 if (!(flags & PM_NOYIELD))
2103 DWORD count;
2104 ReleaseThunkLock(&count);
2105 if (count) RestoreThunkLock(count);
2107 WIN_RestoreWndsLock( locks );
2108 return FALSE;
2111 WIN_RestoreWndsLock( locks );
2113 /* need to fill the window handle for WM_PAINT message */
2114 if (msg.message == WM_PAINT)
2116 if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
2118 msg.message = WM_PAINTICON;
2119 msg.wParam = 1;
2121 /* clear internal paint flag */
2122 RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
2125 if ((queue = QUEUE_Current()))
2127 queue->GetMessageTimeVal = msg.time;
2128 msg.pt.x = LOWORD( queue->GetMessagePosVal );
2129 msg.pt.y = HIWORD( queue->GetMessagePosVal );
2132 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
2134 /* copy back our internal safe copy of message data to msg_out.
2135 * msg_out is a variable from the *program*, so it can't be used
2136 * internally as it can get "corrupted" by our use of SendMessage()
2137 * (back to the program) inside the message handling itself. */
2138 *msg_out = msg;
2139 return TRUE;
2143 /***********************************************************************
2144 * PeekMessageA (USER32.@)
2146 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2148 BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2149 if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2150 return ret;
2154 /***********************************************************************
2155 * GetMessageW (USER32.@)
2157 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2159 MESSAGEQUEUE *queue = QUEUE_Current();
2160 int mask, locks;
2162 mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2163 if (first || last)
2165 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2166 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2167 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2168 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2169 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2170 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2172 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2174 locks = WIN_SuspendWndsLock();
2176 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2178 /* wait until one of the bits is set */
2179 unsigned int wake_bits = 0, changed_bits = 0;
2180 DWORD dwlc;
2182 SERVER_START_REQ( set_queue_mask )
2184 req->wake_mask = QS_SENDMESSAGE;
2185 req->changed_mask = mask;
2186 req->skip_wait = 1;
2187 if (!wine_server_call( req ))
2189 wake_bits = reply->wake_bits;
2190 changed_bits = reply->changed_bits;
2193 SERVER_END_REQ;
2195 if (changed_bits & mask) continue;
2196 if (wake_bits & QS_SENDMESSAGE) continue;
2198 TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2199 queue->self, mask, wake_bits, changed_bits );
2201 ReleaseThunkLock( &dwlc );
2202 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2203 USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
2204 else
2205 WaitForSingleObject( queue->server_queue, INFINITE );
2206 if (dwlc) RestoreThunkLock( dwlc );
2209 WIN_RestoreWndsLock( locks );
2211 return (msg->message != WM_QUIT);
2215 /***********************************************************************
2216 * GetMessageA (USER32.@)
2218 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2220 GetMessageW( msg, hwnd, first, last );
2221 msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2222 return (msg->message != WM_QUIT);
2226 /***********************************************************************
2227 * IsDialogMessage (USER32.@)
2228 * IsDialogMessageA (USER32.@)
2230 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2232 MSG msg = *pmsg;
2233 msg.wParam = map_wparam_AtoW( msg.message, msg.wParam );
2234 return IsDialogMessageW( hwndDlg, &msg );
2238 /***********************************************************************
2239 * SetMessageQueue (USER32.@)
2241 BOOL WINAPI SetMessageQueue( INT size )
2243 /* now obsolete the message queue will be expanded dynamically as necessary */
2244 return TRUE;
2248 /**********************************************************************
2249 * AttachThreadInput (USER32.@)
2251 * Attaches the input processing mechanism of one thread to that of
2252 * another thread.
2254 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
2256 BOOL ret;
2258 SERVER_START_REQ( attach_thread_input )
2260 req->tid_from = from;
2261 req->tid_to = to;
2262 req->attach = attach;
2263 ret = !wine_server_call_err( req );
2265 SERVER_END_REQ;
2266 return ret;
2270 /**********************************************************************
2271 * GetGUIThreadInfo (USER32.@)
2273 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
2275 BOOL ret;
2277 SERVER_START_REQ( get_thread_input )
2279 req->tid = id;
2280 if ((ret = !wine_server_call_err( req )))
2282 info->flags = 0;
2283 info->hwndActive = reply->active;
2284 info->hwndFocus = reply->focus;
2285 info->hwndCapture = reply->capture;
2286 info->hwndMenuOwner = reply->menu_owner;
2287 info->hwndMoveSize = reply->move_size;
2288 info->hwndCaret = reply->caret;
2289 info->rcCaret.left = reply->rect.left;
2290 info->rcCaret.top = reply->rect.top;
2291 info->rcCaret.right = reply->rect.right;
2292 info->rcCaret.bottom = reply->rect.bottom;
2293 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
2294 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
2295 if (reply->caret) info->flags |= GUI_CARETBLINKING;
2298 SERVER_END_REQ;
2299 return ret;