Removed input.h, queue.h, spy.h and sysmetrics.h and moved their
[wine/gsoc_dplay.git] / dlls / user / message.c
blobb55253d5c5d4078433890aa17628b13015dc2fc2
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 * MSG_peek_message
1393 * Peek for a message matching the given parameters. Return FALSE if none available.
1394 * All pending sent messages are processed before returning.
1396 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1398 LRESULT result;
1399 ULONG_PTR extra_info = 0;
1400 MESSAGEQUEUE *queue = QUEUE_Current();
1401 struct received_message_info info, *old_info;
1403 if (!first && !last) last = ~0;
1405 for (;;)
1407 NTSTATUS res;
1408 void *buffer = NULL;
1409 size_t size = 0, buffer_size = 0;
1411 do /* loop while buffer is too small */
1413 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1414 return FALSE;
1415 SERVER_START_REQ( get_message )
1417 req->flags = flags;
1418 req->get_win = hwnd;
1419 req->get_first = first;
1420 req->get_last = last;
1421 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1422 if (!(res = wine_server_call( req )))
1424 size = wine_server_reply_size( reply );
1425 info.type = reply->type;
1426 info.msg.hwnd = reply->win;
1427 info.msg.message = reply->msg;
1428 info.msg.wParam = reply->wparam;
1429 info.msg.lParam = reply->lparam;
1430 info.msg.time = reply->time;
1431 info.msg.pt.x = reply->x;
1432 info.msg.pt.y = reply->y;
1433 extra_info = reply->info;
1435 else
1437 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1438 buffer_size = reply->total;
1441 SERVER_END_REQ;
1442 } while (res == STATUS_BUFFER_OVERFLOW);
1444 if (res) return FALSE;
1446 TRACE( "got type %d msg %x hwnd %p wp %x lp %lx\n",
1447 info.type, info.msg.message, info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1449 switch(info.type)
1451 case MSG_ASCII:
1452 case MSG_UNICODE:
1453 info.flags = ISMEX_SEND;
1454 break;
1455 case MSG_NOTIFY:
1456 info.flags = ISMEX_NOTIFY;
1457 break;
1458 case MSG_CALLBACK:
1459 info.flags = ISMEX_CALLBACK;
1460 break;
1461 case MSG_OTHER_PROCESS:
1462 info.flags = ISMEX_SEND;
1463 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1464 &info.msg.lParam, &buffer, size ))
1466 ERR( "invalid packed message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1467 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
1468 info.msg.wParam, info.msg.lParam, size );
1469 /* ignore it */
1470 reply_message( &info, 0, TRUE );
1471 goto next;
1473 break;
1474 case MSG_HARDWARE_RAW:
1475 if (!MSG_process_raw_hardware_message( &info.msg, extra_info,
1476 hwnd, first, last, flags & GET_MSG_REMOVE ))
1477 goto next;
1478 /* fall through */
1479 case MSG_HARDWARE_COOKED:
1480 if (!MSG_process_cooked_hardware_message( &info.msg, extra_info,
1481 flags & GET_MSG_REMOVE ))
1483 flags |= GET_MSG_REMOVE_LAST;
1484 goto next;
1486 queue->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
1487 /* fall through */
1488 case MSG_POSTED:
1489 queue->GetMessageExtraInfoVal = extra_info;
1490 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
1492 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1493 &info.msg.lParam, &buffer, size ))
1495 ERR( "invalid packed dde-message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1496 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1497 info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
1498 /* ignore it */
1499 continue;
1502 *msg = info.msg;
1503 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1504 return TRUE;
1507 /* if we get here, we have a sent message; call the window procedure */
1508 old_info = queue->receive_info;
1509 queue->receive_info = &info;
1510 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1511 info.msg.lParam, (info.type != MSG_ASCII), FALSE );
1512 reply_message( &info, result, TRUE );
1513 queue->receive_info = old_info;
1514 next:
1515 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1520 /***********************************************************************
1521 * wait_message_reply
1523 * Wait until a sent message gets replied to.
1525 static void wait_message_reply( UINT flags )
1527 MESSAGEQUEUE *queue;
1529 if (!(queue = QUEUE_Current())) return;
1531 for (;;)
1533 unsigned int wake_bits = 0, changed_bits = 0;
1534 DWORD dwlc, res;
1536 SERVER_START_REQ( set_queue_mask )
1538 req->wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
1539 req->changed_mask = req->wake_mask;
1540 req->skip_wait = 1;
1541 if (!wine_server_call( req ))
1543 wake_bits = reply->wake_bits;
1544 changed_bits = reply->changed_bits;
1547 SERVER_END_REQ;
1549 if (wake_bits & QS_SMRESULT) return; /* got a result */
1550 if (wake_bits & QS_SENDMESSAGE)
1552 /* Process the sent message immediately */
1553 MSG msg;
1554 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1555 continue;
1558 /* now wait for it */
1560 ReleaseThunkLock( &dwlc );
1562 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1563 res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1564 INFINITE, 0, 0 );
1565 else
1566 res = WaitForSingleObject( queue->server_queue, INFINITE );
1568 if (dwlc) RestoreThunkLock( dwlc );
1572 /***********************************************************************
1573 * put_message_in_queue
1575 * Put a sent message into the destination queue.
1576 * For inter-process message, reply_size is set to expected size of reply data.
1578 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1579 size_t *reply_size )
1581 struct packed_message data;
1582 unsigned int res;
1583 int i, timeout = -1;
1585 if (info->type != MSG_NOTIFY &&
1586 info->type != MSG_CALLBACK &&
1587 info->type != MSG_POSTED &&
1588 info->timeout != INFINITE)
1589 timeout = info->timeout;
1591 data.count = 0;
1592 if (info->type == MSG_OTHER_PROCESS)
1594 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1595 if (data.count == -1)
1597 WARN( "cannot pack message %x\n", info->msg );
1598 return FALSE;
1601 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
1603 return post_dde_message( dest_tid, &data, info );
1606 SERVER_START_REQ( send_message )
1608 req->id = dest_tid;
1609 req->type = info->type;
1610 req->win = info->hwnd;
1611 req->msg = info->msg;
1612 req->wparam = info->wparam;
1613 req->lparam = info->lparam;
1614 req->time = GetCurrentTime();
1615 req->timeout = timeout;
1616 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1617 if ((res = wine_server_call( req )))
1619 if (res == STATUS_INVALID_PARAMETER)
1620 /* FIXME: find a STATUS_ value for this one */
1621 SetLastError( ERROR_INVALID_THREAD_ID );
1622 else
1623 SetLastError( RtlNtStatusToDosError(res) );
1626 SERVER_END_REQ;
1627 return !res;
1631 /***********************************************************************
1632 * retrieve_reply
1634 * Retrieve a message reply from the server.
1636 static LRESULT retrieve_reply( const struct send_message_info *info,
1637 size_t reply_size, LRESULT *result )
1639 NTSTATUS status;
1640 void *reply_data = NULL;
1642 if (reply_size)
1644 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
1646 WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
1647 reply_size = 0;
1650 SERVER_START_REQ( get_message_reply )
1652 req->cancel = 1;
1653 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
1654 if (!(status = wine_server_call( req ))) *result = reply->result;
1655 reply_size = wine_server_reply_size( reply );
1657 SERVER_END_REQ;
1658 if (!status && reply_size)
1659 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
1661 if (reply_data) HeapFree( GetProcessHeap(), 0, reply_data );
1663 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1664 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
1665 info->lparam, *result, status );
1667 if (!status) return 1;
1668 if (status == STATUS_TIMEOUT) SetLastError(0); /* timeout */
1669 else SetLastError( RtlNtStatusToDosError(status) );
1670 return 0;
1674 /***********************************************************************
1675 * send_inter_thread_message
1677 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1678 LRESULT *res_ptr )
1680 LRESULT ret;
1681 int locks;
1682 size_t reply_size = 0;
1684 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
1685 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
1687 if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1689 /* there's no reply to wait for on notify/callback messages */
1690 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1692 locks = WIN_SuspendWndsLock();
1694 wait_message_reply( info->flags );
1695 ret = retrieve_reply( info, reply_size, res_ptr );
1697 WIN_RestoreWndsLock( locks );
1698 return ret;
1702 /***********************************************************************
1703 * SendMessageTimeoutW (USER32.@)
1705 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1706 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1708 struct send_message_info info;
1709 DWORD dest_tid, dest_pid;
1710 LRESULT ret, result;
1712 info.type = MSG_UNICODE;
1713 info.hwnd = hwnd;
1714 info.msg = msg;
1715 info.wparam = wparam;
1716 info.lparam = lparam;
1717 info.flags = flags;
1718 info.timeout = timeout;
1720 if (is_broadcast(hwnd))
1722 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1723 if (res_ptr) *res_ptr = 1;
1724 return 1;
1727 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1729 if (USER_IsExitingThread( dest_tid )) return 0;
1731 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1733 if (dest_tid == GetCurrentThreadId())
1735 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1736 ret = 1;
1738 else
1740 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1741 ret = send_inter_thread_message( dest_tid, &info, &result );
1744 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1745 if (ret && res_ptr) *res_ptr = result;
1746 return ret;
1750 /***********************************************************************
1751 * SendMessageTimeoutA (USER32.@)
1753 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1754 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1756 struct send_message_info info;
1757 DWORD dest_tid, dest_pid;
1758 LRESULT ret, result;
1760 info.type = MSG_ASCII;
1761 info.hwnd = hwnd;
1762 info.msg = msg;
1763 info.wparam = wparam;
1764 info.lparam = lparam;
1765 info.flags = flags;
1766 info.timeout = timeout;
1768 if (is_broadcast(hwnd))
1770 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1771 if (res_ptr) *res_ptr = 1;
1772 return 1;
1775 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1777 if (USER_IsExitingThread( dest_tid )) return 0;
1779 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1781 if (dest_tid == GetCurrentThreadId())
1783 result = call_window_proc( hwnd, msg, wparam, lparam, FALSE, TRUE );
1784 ret = 1;
1786 else if (dest_pid == GetCurrentProcessId())
1788 ret = send_inter_thread_message( dest_tid, &info, &result );
1790 else
1792 /* inter-process message: need to map to Unicode */
1793 info.type = MSG_OTHER_PROCESS;
1794 if (is_unicode_message( info.msg ))
1796 if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1797 return 0;
1798 ret = send_inter_thread_message( dest_tid, &info, &result );
1799 result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1800 info.lparam, result );
1802 else ret = send_inter_thread_message( dest_tid, &info, &result );
1804 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1805 if (ret && res_ptr) *res_ptr = result;
1806 return ret;
1810 /***********************************************************************
1811 * SendMessageW (USER32.@)
1813 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1815 LRESULT res = 0;
1816 SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1817 return res;
1821 /***********************************************************************
1822 * SendMessageA (USER32.@)
1824 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1826 LRESULT res = 0;
1827 SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1828 return res;
1832 /***********************************************************************
1833 * SendNotifyMessageA (USER32.@)
1835 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1837 return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1841 /***********************************************************************
1842 * SendNotifyMessageW (USER32.@)
1844 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1846 struct send_message_info info;
1847 DWORD dest_tid;
1848 LRESULT result;
1850 if (is_pointer_message(msg))
1852 SetLastError(ERROR_INVALID_PARAMETER);
1853 return FALSE;
1856 info.type = MSG_NOTIFY;
1857 info.hwnd = hwnd;
1858 info.msg = msg;
1859 info.wparam = wparam;
1860 info.lparam = lparam;
1862 if (is_broadcast(hwnd))
1864 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1865 return TRUE;
1868 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1870 if (USER_IsExitingThread( dest_tid )) return TRUE;
1872 if (dest_tid == GetCurrentThreadId())
1874 call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1875 return TRUE;
1877 return send_inter_thread_message( dest_tid, &info, &result );
1881 /***********************************************************************
1882 * SendMessageCallbackA (USER32.@)
1884 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1885 SENDASYNCPROC callback, ULONG_PTR data )
1887 return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
1888 lparam, callback, data );
1892 /***********************************************************************
1893 * SendMessageCallbackW (USER32.@)
1895 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1896 SENDASYNCPROC callback, ULONG_PTR data )
1898 struct send_message_info info;
1899 LRESULT result;
1900 DWORD dest_tid;
1902 if (is_pointer_message(msg))
1904 SetLastError(ERROR_INVALID_PARAMETER);
1905 return FALSE;
1908 info.type = MSG_CALLBACK;
1909 info.hwnd = hwnd;
1910 info.msg = msg;
1911 info.wparam = wparam;
1912 info.lparam = lparam;
1913 info.callback = callback;
1914 info.data = data;
1916 if (is_broadcast(hwnd))
1918 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1919 return TRUE;
1922 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1924 if (USER_IsExitingThread( dest_tid )) return TRUE;
1926 if (dest_tid == GetCurrentThreadId())
1928 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1929 callback( hwnd, msg, data, result );
1930 return TRUE;
1932 FIXME( "callback will not be called\n" );
1933 return send_inter_thread_message( dest_tid, &info, &result );
1937 /***********************************************************************
1938 * ReplyMessage (USER32.@)
1940 BOOL WINAPI ReplyMessage( LRESULT result )
1942 MESSAGEQUEUE *queue = QUEUE_Current();
1943 struct received_message_info *info = queue->receive_info;
1945 if (!info) return FALSE;
1946 reply_message( info, result, FALSE );
1947 return TRUE;
1951 /***********************************************************************
1952 * InSendMessage (USER32.@)
1954 BOOL WINAPI InSendMessage(void)
1956 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
1960 /***********************************************************************
1961 * InSendMessageEx (USER32.@)
1963 DWORD WINAPI InSendMessageEx( LPVOID reserved )
1965 MESSAGEQUEUE *queue = QUEUE_Current();
1966 struct received_message_info *info = queue->receive_info;
1968 if (info) return info->flags;
1969 return ISMEX_NOSEND;
1973 /***********************************************************************
1974 * PostMessageA (USER32.@)
1976 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1978 return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1982 /***********************************************************************
1983 * PostMessageW (USER32.@)
1985 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1987 struct send_message_info info;
1988 DWORD dest_tid;
1990 if (is_pointer_message( msg ))
1992 SetLastError( ERROR_INVALID_PARAMETER );
1993 return FALSE;
1996 info.type = MSG_POSTED;
1997 info.hwnd = hwnd;
1998 info.msg = msg;
1999 info.wparam = wparam;
2000 info.lparam = lparam;
2002 if (is_broadcast(hwnd))
2004 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2005 return TRUE;
2008 if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2010 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2012 if (USER_IsExitingThread( dest_tid )) return TRUE;
2014 return put_message_in_queue( dest_tid, &info, NULL );
2018 /**********************************************************************
2019 * PostThreadMessageA (USER32.@)
2021 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2023 return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2027 /**********************************************************************
2028 * PostThreadMessageW (USER32.@)
2030 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2032 struct send_message_info info;
2034 if (is_pointer_message( msg ))
2036 SetLastError( ERROR_INVALID_PARAMETER );
2037 return FALSE;
2039 if (USER_IsExitingThread( thread )) return TRUE;
2041 info.type = MSG_POSTED;
2042 info.hwnd = 0;
2043 info.msg = msg;
2044 info.wparam = wparam;
2045 info.lparam = lparam;
2046 return put_message_in_queue( thread, &info, NULL );
2050 /***********************************************************************
2051 * PostQuitMessage (USER32.@)
2053 void WINAPI PostQuitMessage( INT exitCode )
2055 PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
2059 /***********************************************************************
2060 * PeekMessageW (USER32.@)
2062 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2064 MESSAGEQUEUE *queue;
2065 MSG msg;
2066 int locks;
2068 /* check for graphics events */
2069 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2070 USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
2072 hwnd = WIN_GetFullHandle( hwnd );
2073 locks = WIN_SuspendWndsLock();
2075 if (!MSG_peek_message( &msg, hwnd, first, last,
2076 (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2078 if (!(flags & PM_NOYIELD))
2080 DWORD count;
2081 ReleaseThunkLock(&count);
2082 if (count) RestoreThunkLock(count);
2084 WIN_RestoreWndsLock( locks );
2085 return FALSE;
2088 WIN_RestoreWndsLock( locks );
2090 /* need to fill the window handle for WM_PAINT message */
2091 if (msg.message == WM_PAINT)
2093 if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
2095 msg.message = WM_PAINTICON;
2096 msg.wParam = 1;
2098 /* clear internal paint flag */
2099 RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
2102 if ((queue = QUEUE_Current()))
2104 queue->GetMessageTimeVal = msg.time;
2105 msg.pt.x = LOWORD( queue->GetMessagePosVal );
2106 msg.pt.y = HIWORD( queue->GetMessagePosVal );
2109 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
2111 /* copy back our internal safe copy of message data to msg_out.
2112 * msg_out is a variable from the *program*, so it can't be used
2113 * internally as it can get "corrupted" by our use of SendMessage()
2114 * (back to the program) inside the message handling itself. */
2115 *msg_out = msg;
2116 return TRUE;
2120 /***********************************************************************
2121 * PeekMessageA (USER32.@)
2123 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2125 BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2126 if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2127 return ret;
2131 /***********************************************************************
2132 * GetMessageW (USER32.@)
2134 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2136 MESSAGEQUEUE *queue = QUEUE_Current();
2137 int mask, locks;
2139 mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2140 if (first || last)
2142 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2143 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2144 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2145 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2146 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2147 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2149 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2151 locks = WIN_SuspendWndsLock();
2153 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2155 /* wait until one of the bits is set */
2156 unsigned int wake_bits = 0, changed_bits = 0;
2157 DWORD dwlc;
2159 SERVER_START_REQ( set_queue_mask )
2161 req->wake_mask = QS_SENDMESSAGE;
2162 req->changed_mask = mask;
2163 req->skip_wait = 1;
2164 if (!wine_server_call( req ))
2166 wake_bits = reply->wake_bits;
2167 changed_bits = reply->changed_bits;
2170 SERVER_END_REQ;
2172 if (changed_bits & mask) continue;
2173 if (wake_bits & QS_SENDMESSAGE) continue;
2175 TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2176 queue->self, mask, wake_bits, changed_bits );
2178 ReleaseThunkLock( &dwlc );
2179 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2180 USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
2181 else
2182 WaitForSingleObject( queue->server_queue, INFINITE );
2183 if (dwlc) RestoreThunkLock( dwlc );
2186 WIN_RestoreWndsLock( locks );
2188 return (msg->message != WM_QUIT);
2192 /***********************************************************************
2193 * GetMessageA (USER32.@)
2195 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2197 GetMessageW( msg, hwnd, first, last );
2198 msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2199 return (msg->message != WM_QUIT);
2203 /***********************************************************************
2204 * IsDialogMessage (USER32.@)
2205 * IsDialogMessageA (USER32.@)
2207 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2209 MSG msg = *pmsg;
2210 msg.wParam = map_wparam_AtoW( msg.message, msg.wParam );
2211 return IsDialogMessageW( hwndDlg, &msg );
2215 /***********************************************************************
2216 * SetMessageQueue (USER32.@)
2218 BOOL WINAPI SetMessageQueue( INT size )
2220 /* now obsolete the message queue will be expanded dynamically as necessary */
2221 return TRUE;
2225 /**********************************************************************
2226 * AttachThreadInput (USER32.@)
2228 * Attaches the input processing mechanism of one thread to that of
2229 * another thread.
2231 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
2233 BOOL ret;
2235 SERVER_START_REQ( attach_thread_input )
2237 req->tid_from = from;
2238 req->tid_to = to;
2239 req->attach = attach;
2240 ret = !wine_server_call_err( req );
2242 SERVER_END_REQ;
2243 return ret;
2247 /**********************************************************************
2248 * GetGUIThreadInfo (USER32.@)
2250 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
2252 BOOL ret;
2254 SERVER_START_REQ( get_thread_input )
2256 req->tid = id;
2257 if ((ret = !wine_server_call_err( req )))
2259 info->flags = 0;
2260 info->hwndActive = reply->active;
2261 info->hwndFocus = reply->focus;
2262 info->hwndCapture = reply->capture;
2263 info->hwndMenuOwner = reply->menu_owner;
2264 info->hwndMoveSize = reply->move_size;
2265 info->hwndCaret = reply->caret;
2266 info->rcCaret.left = reply->rect.left;
2267 info->rcCaret.top = reply->rect.top;
2268 info->rcCaret.right = reply->rect.right;
2269 info->rcCaret.bottom = reply->rect.bottom;
2270 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
2271 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
2272 if (reply->caret) info->flags |= GUI_CARETBLINKING;
2275 SERVER_END_REQ;
2276 return ret;