winex11: Create a dummy parent window for composite child windows to avoid using...
[wine/wine64.git] / dlls / user32 / message.c
blob542298adf3799e1ec84b6882a661ffc34fdcdc9d
1 /*
2 * Window messaging support
4 * Copyright 2001 Alexandre Julliard
5 * Copyright 2008 Maarten Lankhorst
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winerror.h"
35 #include "winnls.h"
36 #include "dbt.h"
37 #include "dde.h"
38 #include "imm.h"
39 #include "ddk/imm.h"
40 #include "wine/unicode.h"
41 #include "wine/server.h"
42 #include "user_private.h"
43 #include "win.h"
44 #include "controls.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(msg);
48 WINE_DECLARE_DEBUG_CHANNEL(relay);
49 WINE_DECLARE_DEBUG_CHANNEL(key);
51 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
52 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
54 #define MAX_PACK_COUNT 4
55 #define MAX_SENDMSG_RECURSION 64
57 #define SYS_TIMER_RATE 55 /* min. timer rate in ms (actually 54.925)*/
59 /* description of the data fields that need to be packed along with a sent message */
60 struct packed_message
62 int count;
63 const void *data[MAX_PACK_COUNT];
64 size_t size[MAX_PACK_COUNT];
67 /* info about the message currently being received by the current thread */
68 struct received_message_info
70 enum message_type type;
71 MSG msg;
72 UINT flags; /* InSendMessageEx return flags */
75 /* structure to group all parameters for sent messages of the various kinds */
76 struct send_message_info
78 enum message_type type;
79 DWORD dest_tid;
80 HWND hwnd;
81 UINT msg;
82 WPARAM wparam;
83 LPARAM lparam;
84 UINT flags; /* flags for SendMessageTimeout */
85 UINT timeout; /* timeout for SendMessageTimeout */
86 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
87 ULONG_PTR data; /* callback data */
88 enum wm_char_mapping wm_char;
92 /* flag for messages that contain pointers */
93 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
95 #define SET(msg) (1 << ((msg) & 31))
97 static const unsigned int message_pointer_flags[] =
99 /* 0x00 - 0x1f */
100 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
101 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
102 /* 0x20 - 0x3f */
103 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
104 SET(WM_COMPAREITEM),
105 /* 0x40 - 0x5f */
106 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
107 SET(WM_NOTIFY) | SET(WM_HELP),
108 /* 0x60 - 0x7f */
109 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
110 /* 0x80 - 0x9f */
111 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
112 /* 0xa0 - 0xbf */
113 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
114 /* 0xc0 - 0xdf */
115 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
116 /* 0xe0 - 0xff */
117 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO) | SET(SBM_GETSCROLLBARINFO),
118 /* 0x100 - 0x11f */
120 /* 0x120 - 0x13f */
122 /* 0x140 - 0x15f */
123 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
124 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
125 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
126 /* 0x160 - 0x17f */
128 /* 0x180 - 0x19f */
129 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
130 SET(LB_DIR) | SET(LB_FINDSTRING) |
131 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
132 /* 0x1a0 - 0x1bf */
133 SET(LB_FINDSTRINGEXACT),
134 /* 0x1c0 - 0x1df */
136 /* 0x1e0 - 0x1ff */
138 /* 0x200 - 0x21f */
139 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
140 /* 0x220 - 0x23f */
141 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
142 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
143 /* 0x240 - 0x25f */
145 /* 0x260 - 0x27f */
147 /* 0x280 - 0x29f */
149 /* 0x2a0 - 0x2bf */
151 /* 0x2c0 - 0x2df */
153 /* 0x2e0 - 0x2ff */
155 /* 0x300 - 0x31f */
156 SET(WM_ASKCBFORMATNAME)
159 /* flags for messages that contain Unicode strings */
160 static const unsigned int message_unicode_flags[] =
162 /* 0x00 - 0x1f */
163 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
164 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
165 /* 0x20 - 0x3f */
166 SET(WM_CHARTOITEM),
167 /* 0x40 - 0x5f */
169 /* 0x60 - 0x7f */
171 /* 0x80 - 0x9f */
172 SET(WM_NCCREATE),
173 /* 0xa0 - 0xbf */
175 /* 0xc0 - 0xdf */
176 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
177 /* 0xe0 - 0xff */
179 /* 0x100 - 0x11f */
180 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
181 /* 0x120 - 0x13f */
182 SET(WM_MENUCHAR),
183 /* 0x140 - 0x15f */
184 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
185 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
186 /* 0x160 - 0x17f */
188 /* 0x180 - 0x19f */
189 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
190 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
191 /* 0x1a0 - 0x1bf */
192 SET(LB_FINDSTRINGEXACT),
193 /* 0x1c0 - 0x1df */
195 /* 0x1e0 - 0x1ff */
197 /* 0x200 - 0x21f */
199 /* 0x220 - 0x23f */
200 SET(WM_MDICREATE),
201 /* 0x240 - 0x25f */
203 /* 0x260 - 0x27f */
205 /* 0x280 - 0x29f */
206 SET(WM_IME_CHAR),
207 /* 0x2a0 - 0x2bf */
209 /* 0x2c0 - 0x2df */
211 /* 0x2e0 - 0x2ff */
213 /* 0x300 - 0x31f */
214 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
217 /* check whether a given message type includes pointers */
218 static inline int is_pointer_message( UINT message )
220 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
221 return (message_pointer_flags[message / 32] & SET(message)) != 0;
224 /* check whether a given message type contains Unicode (or ASCII) chars */
225 static inline int is_unicode_message( UINT message )
227 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
228 return (message_unicode_flags[message / 32] & SET(message)) != 0;
231 #undef SET
233 /* add a data field to a packed message */
234 static inline void push_data( struct packed_message *data, const void *ptr, size_t size )
236 data->data[data->count] = ptr;
237 data->size[data->count] = size;
238 data->count++;
241 /* add a string to a packed message */
242 static inline void push_string( struct packed_message *data, LPCWSTR str )
244 push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
247 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
248 static inline void *get_data( void **buffer, size_t size )
250 void *ret = *buffer;
251 *buffer = (char *)*buffer + size;
252 return ret;
255 /* make sure that the buffer contains a valid null-terminated Unicode string */
256 static inline BOOL check_string( LPCWSTR str, size_t size )
258 for (size /= sizeof(WCHAR); size; size--, str++)
259 if (!*str) return TRUE;
260 return FALSE;
263 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
264 static inline void *get_buffer_space( void **buffer, size_t size )
266 void *ret;
268 if (*buffer)
270 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
271 HeapFree( GetProcessHeap(), 0, *buffer );
273 else ret = HeapAlloc( GetProcessHeap(), 0, size );
275 *buffer = ret;
276 return ret;
279 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
280 static inline BOOL combobox_has_strings( HWND hwnd )
282 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
283 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
286 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
287 static inline BOOL listbox_has_strings( HWND hwnd )
289 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
290 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
293 /* check whether message is in the range of keyboard messages */
294 static inline BOOL is_keyboard_message( UINT message )
296 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
299 /* check whether message is in the range of mouse messages */
300 static inline BOOL is_mouse_message( UINT message )
302 return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
303 (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
306 /* check whether message matches the specified hwnd filter */
307 static inline BOOL check_hwnd_filter( const MSG *msg, HWND hwnd_filter )
309 if (!hwnd_filter || hwnd_filter == GetDesktopWindow()) return TRUE;
310 return (msg->hwnd == hwnd_filter || IsChild( hwnd_filter, msg->hwnd ));
313 /* check for pending WM_CHAR message with DBCS trailing byte */
314 static inline BOOL get_pending_wmchar( MSG *msg, UINT first, UINT last, BOOL remove )
316 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
318 if (!data || !data->get_msg.message) return FALSE;
319 if ((first || last) && (first > WM_CHAR || last < WM_CHAR)) return FALSE;
320 if (!msg) return FALSE;
321 *msg = data->get_msg;
322 if (remove) data->get_msg.message = 0;
323 return TRUE;
326 /***********************************************************************
327 * broadcast_message_callback
329 * Helper callback for broadcasting messages.
331 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
333 struct send_message_info *info = (struct send_message_info *)lparam;
334 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
335 switch(info->type)
337 case MSG_UNICODE:
338 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
339 info->flags, info->timeout, NULL );
340 break;
341 case MSG_ASCII:
342 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
343 info->flags, info->timeout, NULL );
344 break;
345 case MSG_NOTIFY:
346 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
347 break;
348 case MSG_CALLBACK:
349 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
350 info->callback, info->data );
351 break;
352 case MSG_POSTED:
353 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
354 break;
355 default:
356 ERR( "bad type %d\n", info->type );
357 break;
359 return TRUE;
363 /***********************************************************************
364 * map_wparam_AtoW
366 * Convert the wparam of an ASCII message to Unicode.
368 BOOL map_wparam_AtoW( UINT message, WPARAM *wparam, enum wm_char_mapping mapping )
370 char ch[2];
371 WCHAR wch[2];
373 wch[0] = wch[1] = 0;
374 switch(message)
376 case WM_CHAR:
377 /* WM_CHAR is magic: a DBCS char can be sent/posted as two consecutive WM_CHAR
378 * messages, in which case the first char is stored, and the conversion
379 * to Unicode only takes place once the second char is sent/posted.
381 if (mapping != WMCHAR_MAP_NOMAPPING)
383 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
384 BYTE low = LOBYTE(*wparam);
386 if (HIBYTE(*wparam))
388 ch[0] = low;
389 ch[1] = HIBYTE(*wparam);
390 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
391 TRACE( "map %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
392 if (data) data->lead_byte[mapping] = 0;
394 else if (data && data->lead_byte[mapping])
396 ch[0] = data->lead_byte[mapping];
397 ch[1] = low;
398 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
399 TRACE( "map stored %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
400 data->lead_byte[mapping] = 0;
402 else if (!IsDBCSLeadByte( low ))
404 ch[0] = low;
405 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 1 );
406 TRACE( "map %02x -> %04x\n", (BYTE)ch[0], wch[0] );
407 if (data) data->lead_byte[mapping] = 0;
409 else /* store it and wait for trail byte */
411 if (!data)
413 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) )))
414 return FALSE;
415 get_user_thread_info()->wmchar_data = data;
417 TRACE( "storing lead byte %02x mapping %u\n", low, mapping );
418 data->lead_byte[mapping] = low;
419 return FALSE;
421 *wparam = MAKEWPARAM(wch[0], wch[1]);
422 break;
424 /* else fall through */
425 case WM_CHARTOITEM:
426 case EM_SETPASSWORDCHAR:
427 case WM_DEADCHAR:
428 case WM_SYSCHAR:
429 case WM_SYSDEADCHAR:
430 case WM_MENUCHAR:
431 ch[0] = LOBYTE(*wparam);
432 ch[1] = HIBYTE(*wparam);
433 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
434 *wparam = MAKEWPARAM(wch[0], wch[1]);
435 break;
436 case WM_IME_CHAR:
437 ch[0] = HIBYTE(*wparam);
438 ch[1] = LOBYTE(*wparam);
439 if (ch[0]) RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch, 2 );
440 else RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch + 1, 1 );
441 *wparam = MAKEWPARAM(wch[0], HIWORD(*wparam));
442 break;
444 return TRUE;
448 /***********************************************************************
449 * map_wparam_WtoA
451 * Convert the wparam of a Unicode message to ASCII.
453 static void map_wparam_WtoA( MSG *msg, BOOL remove )
455 BYTE ch[2];
456 WCHAR wch[2];
457 DWORD len;
459 switch(msg->message)
461 case WM_CHAR:
462 if (!HIWORD(msg->wParam))
464 wch[0] = LOWORD(msg->wParam);
465 ch[0] = ch[1] = 0;
466 RtlUnicodeToMultiByteN( (LPSTR)ch, 2, &len, wch, sizeof(wch[0]) );
467 if (len == 2) /* DBCS char */
469 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
470 if (!data)
472 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) ))) return;
473 get_user_thread_info()->wmchar_data = data;
475 if (remove)
477 data->get_msg = *msg;
478 data->get_msg.wParam = ch[1];
480 msg->wParam = ch[0];
481 return;
484 /* else fall through */
485 case WM_CHARTOITEM:
486 case EM_SETPASSWORDCHAR:
487 case WM_DEADCHAR:
488 case WM_SYSCHAR:
489 case WM_SYSDEADCHAR:
490 case WM_MENUCHAR:
491 wch[0] = LOWORD(msg->wParam);
492 wch[1] = HIWORD(msg->wParam);
493 ch[0] = ch[1] = 0;
494 RtlUnicodeToMultiByteN( (LPSTR)ch, 2, NULL, wch, sizeof(wch) );
495 msg->wParam = MAKEWPARAM( ch[0] | (ch[1] << 8), 0 );
496 break;
497 case WM_IME_CHAR:
498 wch[0] = LOWORD(msg->wParam);
499 ch[0] = ch[1] = 0;
500 RtlUnicodeToMultiByteN( (LPSTR)ch, 2, &len, wch, sizeof(wch[0]) );
501 if (len == 2)
502 msg->wParam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(msg->wParam) );
503 else
504 msg->wParam = MAKEWPARAM( ch[0], HIWORD(msg->wParam) );
505 break;
510 /***********************************************************************
511 * pack_message
513 * Pack a message for sending to another process.
514 * Return the size of the data we expect in the message reply.
515 * Set data->count to -1 if there is an error.
517 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
518 struct packed_message *data )
520 data->count = 0;
521 switch(message)
523 case WM_NCCREATE:
524 case WM_CREATE:
526 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
527 push_data( data, cs, sizeof(*cs) );
528 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
529 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
530 return sizeof(*cs);
532 case WM_GETTEXT:
533 case WM_ASKCBFORMATNAME:
534 return wparam * sizeof(WCHAR);
535 case WM_WININICHANGE:
536 if (lparam) push_string(data, (LPWSTR)lparam );
537 return 0;
538 case WM_SETTEXT:
539 case WM_DEVMODECHANGE:
540 case CB_DIR:
541 case LB_DIR:
542 case LB_ADDFILE:
543 case EM_REPLACESEL:
544 push_string( data, (LPWSTR)lparam );
545 return 0;
546 case WM_GETMINMAXINFO:
547 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
548 return sizeof(MINMAXINFO);
549 case WM_DRAWITEM:
550 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
551 return 0;
552 case WM_MEASUREITEM:
553 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
554 return sizeof(MEASUREITEMSTRUCT);
555 case WM_DELETEITEM:
556 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
557 return 0;
558 case WM_COMPAREITEM:
559 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
560 return 0;
561 case WM_WINDOWPOSCHANGING:
562 case WM_WINDOWPOSCHANGED:
563 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
564 return sizeof(WINDOWPOS);
565 case WM_COPYDATA:
567 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
568 push_data( data, cp, sizeof(*cp) );
569 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
570 return 0;
572 case WM_NOTIFY:
573 /* WM_NOTIFY cannot be sent across processes (MSDN) */
574 data->count = -1;
575 return 0;
576 case WM_HELP:
577 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
578 return 0;
579 case WM_STYLECHANGING:
580 case WM_STYLECHANGED:
581 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
582 return 0;
583 case WM_NCCALCSIZE:
584 if (!wparam)
586 push_data( data, (RECT *)lparam, sizeof(RECT) );
587 return sizeof(RECT);
589 else
591 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
592 push_data( data, nc, sizeof(*nc) );
593 push_data( data, nc->lppos, sizeof(*nc->lppos) );
594 return sizeof(*nc) + sizeof(*nc->lppos);
596 case WM_GETDLGCODE:
597 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
598 return sizeof(MSG);
599 case SBM_SETSCROLLINFO:
600 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
601 return 0;
602 case SBM_GETSCROLLINFO:
603 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
604 return sizeof(SCROLLINFO);
605 case SBM_GETSCROLLBARINFO:
607 const SCROLLBARINFO *info = (const SCROLLBARINFO *)lparam;
608 size_t size = min( info->cbSize, sizeof(SCROLLBARINFO) );
609 push_data( data, info, size );
610 return size;
612 case EM_GETSEL:
613 case SBM_GETRANGE:
614 case CB_GETEDITSEL:
616 size_t size = 0;
617 if (wparam) size += sizeof(DWORD);
618 if (lparam) size += sizeof(DWORD);
619 return size;
621 case EM_GETRECT:
622 case LB_GETITEMRECT:
623 case CB_GETDROPPEDCONTROLRECT:
624 return sizeof(RECT);
625 case EM_SETRECT:
626 case EM_SETRECTNP:
627 push_data( data, (RECT *)lparam, sizeof(RECT) );
628 return 0;
629 case EM_GETLINE:
631 WORD *pw = (WORD *)lparam;
632 push_data( data, pw, sizeof(*pw) );
633 return *pw * sizeof(WCHAR);
635 case EM_SETTABSTOPS:
636 case LB_SETTABSTOPS:
637 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
638 return 0;
639 case CB_ADDSTRING:
640 case CB_INSERTSTRING:
641 case CB_FINDSTRING:
642 case CB_FINDSTRINGEXACT:
643 case CB_SELECTSTRING:
644 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
645 return 0;
646 case CB_GETLBTEXT:
647 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
648 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
649 case LB_ADDSTRING:
650 case LB_INSERTSTRING:
651 case LB_FINDSTRING:
652 case LB_FINDSTRINGEXACT:
653 case LB_SELECTSTRING:
654 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
655 return 0;
656 case LB_GETTEXT:
657 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
658 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
659 case LB_GETSELITEMS:
660 return wparam * sizeof(UINT);
661 case WM_NEXTMENU:
662 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
663 return sizeof(MDINEXTMENU);
664 case WM_SIZING:
665 case WM_MOVING:
666 push_data( data, (RECT *)lparam, sizeof(RECT) );
667 return sizeof(RECT);
668 case WM_MDICREATE:
670 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
671 push_data( data, cs, sizeof(*cs) );
672 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
673 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
674 return sizeof(*cs);
676 case WM_MDIGETACTIVE:
677 if (lparam) return sizeof(BOOL);
678 return 0;
679 case WM_DEVICECHANGE:
681 DEV_BROADCAST_HDR *header = (DEV_BROADCAST_HDR *)lparam;
682 push_data( data, header, header->dbch_size );
683 return 0;
685 case WM_WINE_SETWINDOWPOS:
686 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
687 return 0;
688 case WM_WINE_KEYBOARD_LL_HOOK:
690 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
691 push_data( data, h_extra, sizeof(*h_extra) );
692 push_data( data, (LPVOID)h_extra->lparam, sizeof(KBDLLHOOKSTRUCT) );
693 return 0;
695 case WM_WINE_MOUSE_LL_HOOK:
697 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
698 push_data( data, h_extra, sizeof(*h_extra) );
699 push_data( data, (LPVOID)h_extra->lparam, sizeof(MSLLHOOKSTRUCT) );
700 return 0;
702 case WM_NCPAINT:
703 if (wparam <= 1) return 0;
704 FIXME( "WM_NCPAINT hdc packing not supported yet\n" );
705 data->count = -1;
706 return 0;
707 case WM_PAINT:
708 if (!wparam) return 0;
709 /* fall through */
711 /* these contain an HFONT */
712 case WM_SETFONT:
713 case WM_GETFONT:
714 /* these contain an HDC */
715 case WM_ERASEBKGND:
716 case WM_ICONERASEBKGND:
717 case WM_CTLCOLORMSGBOX:
718 case WM_CTLCOLOREDIT:
719 case WM_CTLCOLORLISTBOX:
720 case WM_CTLCOLORBTN:
721 case WM_CTLCOLORDLG:
722 case WM_CTLCOLORSCROLLBAR:
723 case WM_CTLCOLORSTATIC:
724 case WM_PRINT:
725 case WM_PRINTCLIENT:
726 /* these contain an HGLOBAL */
727 case WM_PAINTCLIPBOARD:
728 case WM_SIZECLIPBOARD:
729 /* these contain HICON */
730 case WM_GETICON:
731 case WM_SETICON:
732 case WM_QUERYDRAGICON:
733 case WM_QUERYPARKICON:
734 /* these contain pointers */
735 case WM_DROPOBJECT:
736 case WM_QUERYDROPOBJECT:
737 case WM_DRAGLOOP:
738 case WM_DRAGSELECT:
739 case WM_DRAGMOVE:
740 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
741 data->count = -1;
742 return 0;
744 return 0;
748 /***********************************************************************
749 * unpack_message
751 * Unpack a message received from another process.
753 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
754 void **buffer, size_t size )
756 size_t minsize = 0;
758 switch(message)
760 case WM_NCCREATE:
761 case WM_CREATE:
763 CREATESTRUCTW *cs = *buffer;
764 WCHAR *str = (WCHAR *)(cs + 1);
765 if (size < sizeof(*cs)) return FALSE;
766 size -= sizeof(*cs);
767 if (HIWORD(cs->lpszName))
769 if (!check_string( str, size )) return FALSE;
770 cs->lpszName = str;
771 size -= (strlenW(str) + 1) * sizeof(WCHAR);
772 str += strlenW(str) + 1;
774 if (HIWORD(cs->lpszClass))
776 if (!check_string( str, size )) return FALSE;
777 cs->lpszClass = str;
779 break;
781 case WM_GETTEXT:
782 case WM_ASKCBFORMATNAME:
783 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
784 break;
785 case WM_WININICHANGE:
786 if (!*lparam) return TRUE;
787 /* fall through */
788 case WM_SETTEXT:
789 case WM_DEVMODECHANGE:
790 case CB_DIR:
791 case LB_DIR:
792 case LB_ADDFILE:
793 case EM_REPLACESEL:
794 if (!check_string( *buffer, size )) return FALSE;
795 break;
796 case WM_GETMINMAXINFO:
797 minsize = sizeof(MINMAXINFO);
798 break;
799 case WM_DRAWITEM:
800 minsize = sizeof(DRAWITEMSTRUCT);
801 break;
802 case WM_MEASUREITEM:
803 minsize = sizeof(MEASUREITEMSTRUCT);
804 break;
805 case WM_DELETEITEM:
806 minsize = sizeof(DELETEITEMSTRUCT);
807 break;
808 case WM_COMPAREITEM:
809 minsize = sizeof(COMPAREITEMSTRUCT);
810 break;
811 case WM_WINDOWPOSCHANGING:
812 case WM_WINDOWPOSCHANGED:
813 case WM_WINE_SETWINDOWPOS:
814 minsize = sizeof(WINDOWPOS);
815 break;
816 case WM_COPYDATA:
818 COPYDATASTRUCT *cp = *buffer;
819 if (size < sizeof(*cp)) return FALSE;
820 if (cp->lpData)
822 minsize = sizeof(*cp) + cp->cbData;
823 cp->lpData = cp + 1;
825 break;
827 case WM_NOTIFY:
828 /* WM_NOTIFY cannot be sent across processes (MSDN) */
829 return FALSE;
830 case WM_HELP:
831 minsize = sizeof(HELPINFO);
832 break;
833 case WM_STYLECHANGING:
834 case WM_STYLECHANGED:
835 minsize = sizeof(STYLESTRUCT);
836 break;
837 case WM_NCCALCSIZE:
838 if (!*wparam) minsize = sizeof(RECT);
839 else
841 NCCALCSIZE_PARAMS *nc = *buffer;
842 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
843 nc->lppos = (WINDOWPOS *)(nc + 1);
845 break;
846 case WM_GETDLGCODE:
847 if (!*lparam) return TRUE;
848 minsize = sizeof(MSG);
849 break;
850 case SBM_SETSCROLLINFO:
851 minsize = sizeof(SCROLLINFO);
852 break;
853 case SBM_GETSCROLLINFO:
854 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
855 break;
856 case SBM_GETSCROLLBARINFO:
857 if (!get_buffer_space( buffer, sizeof(SCROLLBARINFO ))) return FALSE;
858 break;
859 case EM_GETSEL:
860 case SBM_GETRANGE:
861 case CB_GETEDITSEL:
862 if (*wparam || *lparam)
864 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
865 if (*wparam) *wparam = (WPARAM)*buffer;
866 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
868 return TRUE;
869 case EM_GETRECT:
870 case LB_GETITEMRECT:
871 case CB_GETDROPPEDCONTROLRECT:
872 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
873 break;
874 case EM_SETRECT:
875 case EM_SETRECTNP:
876 minsize = sizeof(RECT);
877 break;
878 case EM_GETLINE:
880 WORD len;
881 if (size < sizeof(WORD)) return FALSE;
882 len = *(WORD *)*buffer;
883 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
884 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
885 return TRUE;
887 case EM_SETTABSTOPS:
888 case LB_SETTABSTOPS:
889 if (!*wparam) return TRUE;
890 minsize = *wparam * sizeof(UINT);
891 break;
892 case CB_ADDSTRING:
893 case CB_INSERTSTRING:
894 case CB_FINDSTRING:
895 case CB_FINDSTRINGEXACT:
896 case CB_SELECTSTRING:
897 case LB_ADDSTRING:
898 case LB_INSERTSTRING:
899 case LB_FINDSTRING:
900 case LB_FINDSTRINGEXACT:
901 case LB_SELECTSTRING:
902 if (!*buffer) return TRUE;
903 if (!check_string( *buffer, size )) return FALSE;
904 break;
905 case CB_GETLBTEXT:
907 size = sizeof(ULONG_PTR);
908 if (combobox_has_strings( hwnd ))
909 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
910 if (!get_buffer_space( buffer, size )) return FALSE;
911 break;
913 case LB_GETTEXT:
915 size = sizeof(ULONG_PTR);
916 if (listbox_has_strings( hwnd ))
917 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
918 if (!get_buffer_space( buffer, size )) return FALSE;
919 break;
921 case LB_GETSELITEMS:
922 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
923 break;
924 case WM_NEXTMENU:
925 minsize = sizeof(MDINEXTMENU);
926 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
927 break;
928 case WM_SIZING:
929 case WM_MOVING:
930 minsize = sizeof(RECT);
931 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
932 break;
933 case WM_MDICREATE:
935 MDICREATESTRUCTW *cs = *buffer;
936 WCHAR *str = (WCHAR *)(cs + 1);
937 if (size < sizeof(*cs)) return FALSE;
938 size -= sizeof(*cs);
939 if (HIWORD(cs->szTitle))
941 if (!check_string( str, size )) return FALSE;
942 cs->szTitle = str;
943 size -= (strlenW(str) + 1) * sizeof(WCHAR);
944 str += strlenW(str) + 1;
946 if (HIWORD(cs->szClass))
948 if (!check_string( str, size )) return FALSE;
949 cs->szClass = str;
951 break;
953 case WM_MDIGETACTIVE:
954 if (!*lparam) return TRUE;
955 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
956 break;
957 case WM_DEVICECHANGE:
958 minsize = sizeof(DEV_BROADCAST_HDR);
959 break;
960 case WM_WINE_KEYBOARD_LL_HOOK:
961 case WM_WINE_MOUSE_LL_HOOK:
963 struct hook_extra_info *h_extra = (struct hook_extra_info *)*buffer;
965 minsize = sizeof(struct hook_extra_info) +
966 (message == WM_WINE_KEYBOARD_LL_HOOK ? sizeof(KBDLLHOOKSTRUCT)
967 : sizeof(MSLLHOOKSTRUCT));
968 if (size < minsize) return FALSE;
969 h_extra->lparam = (LPARAM)(h_extra + 1);
970 break;
972 case WM_NCPAINT:
973 if (*wparam <= 1) return TRUE;
974 FIXME( "WM_NCPAINT hdc unpacking not supported\n" );
975 return FALSE;
976 case WM_PAINT:
977 if (!*wparam) return TRUE;
978 /* fall through */
980 /* these contain an HFONT */
981 case WM_SETFONT:
982 case WM_GETFONT:
983 /* these contain an HDC */
984 case WM_ERASEBKGND:
985 case WM_ICONERASEBKGND:
986 case WM_CTLCOLORMSGBOX:
987 case WM_CTLCOLOREDIT:
988 case WM_CTLCOLORLISTBOX:
989 case WM_CTLCOLORBTN:
990 case WM_CTLCOLORDLG:
991 case WM_CTLCOLORSCROLLBAR:
992 case WM_CTLCOLORSTATIC:
993 case WM_PRINT:
994 case WM_PRINTCLIENT:
995 /* these contain an HGLOBAL */
996 case WM_PAINTCLIPBOARD:
997 case WM_SIZECLIPBOARD:
998 /* these contain HICON */
999 case WM_GETICON:
1000 case WM_SETICON:
1001 case WM_QUERYDRAGICON:
1002 case WM_QUERYPARKICON:
1003 /* these contain pointers */
1004 case WM_DROPOBJECT:
1005 case WM_QUERYDROPOBJECT:
1006 case WM_DRAGLOOP:
1007 case WM_DRAGSELECT:
1008 case WM_DRAGMOVE:
1009 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
1010 return FALSE;
1012 default:
1013 return TRUE; /* message doesn't need any unpacking */
1016 /* default exit for most messages: check minsize and store buffer in lparam */
1017 if (size < minsize) return FALSE;
1018 *lparam = (LPARAM)*buffer;
1019 return TRUE;
1023 /***********************************************************************
1024 * pack_reply
1026 * Pack a reply to a message for sending to another process.
1028 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1029 LRESULT res, struct packed_message *data )
1031 data->count = 0;
1032 switch(message)
1034 case WM_NCCREATE:
1035 case WM_CREATE:
1036 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
1037 break;
1038 case WM_GETTEXT:
1039 case CB_GETLBTEXT:
1040 case LB_GETTEXT:
1041 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
1042 break;
1043 case WM_GETMINMAXINFO:
1044 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
1045 break;
1046 case WM_MEASUREITEM:
1047 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
1048 break;
1049 case WM_WINDOWPOSCHANGING:
1050 case WM_WINDOWPOSCHANGED:
1051 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
1052 break;
1053 case WM_GETDLGCODE:
1054 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
1055 break;
1056 case SBM_GETSCROLLINFO:
1057 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
1058 break;
1059 case EM_GETRECT:
1060 case LB_GETITEMRECT:
1061 case CB_GETDROPPEDCONTROLRECT:
1062 case WM_SIZING:
1063 case WM_MOVING:
1064 push_data( data, (RECT *)lparam, sizeof(RECT) );
1065 break;
1066 case EM_GETLINE:
1068 WORD *ptr = (WORD *)lparam;
1069 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
1070 break;
1072 case LB_GETSELITEMS:
1073 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
1074 break;
1075 case WM_MDIGETACTIVE:
1076 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
1077 break;
1078 case WM_NCCALCSIZE:
1079 if (!wparam)
1080 push_data( data, (RECT *)lparam, sizeof(RECT) );
1081 else
1083 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1084 push_data( data, nc, sizeof(*nc) );
1085 push_data( data, nc->lppos, sizeof(*nc->lppos) );
1087 break;
1088 case EM_GETSEL:
1089 case SBM_GETRANGE:
1090 case CB_GETEDITSEL:
1091 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
1092 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
1093 break;
1094 case WM_NEXTMENU:
1095 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
1096 break;
1097 case WM_MDICREATE:
1098 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
1099 break;
1100 case WM_ASKCBFORMATNAME:
1101 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
1102 break;
1107 /***********************************************************************
1108 * unpack_reply
1110 * Unpack a message reply received from another process.
1112 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1113 void *buffer, size_t size )
1115 switch(message)
1117 case WM_NCCREATE:
1118 case WM_CREATE:
1120 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
1121 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
1122 memcpy( cs, buffer, min( sizeof(*cs), size ));
1123 cs->lpszName = name; /* restore the original pointers */
1124 cs->lpszClass = class;
1125 break;
1127 case WM_GETTEXT:
1128 case WM_ASKCBFORMATNAME:
1129 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
1130 break;
1131 case WM_GETMINMAXINFO:
1132 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
1133 break;
1134 case WM_MEASUREITEM:
1135 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
1136 break;
1137 case WM_WINDOWPOSCHANGING:
1138 case WM_WINDOWPOSCHANGED:
1139 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
1140 break;
1141 case WM_GETDLGCODE:
1142 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
1143 break;
1144 case SBM_GETSCROLLINFO:
1145 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
1146 break;
1147 case SBM_GETSCROLLBARINFO:
1148 memcpy( (SCROLLBARINFO *)lparam, buffer, min( sizeof(SCROLLBARINFO), size ));
1149 break;
1150 case EM_GETRECT:
1151 case CB_GETDROPPEDCONTROLRECT:
1152 case LB_GETITEMRECT:
1153 case WM_SIZING:
1154 case WM_MOVING:
1155 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1156 break;
1157 case EM_GETLINE:
1158 size = min( size, (size_t)*(WORD *)lparam );
1159 memcpy( (WCHAR *)lparam, buffer, size );
1160 break;
1161 case LB_GETSELITEMS:
1162 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
1163 break;
1164 case LB_GETTEXT:
1165 case CB_GETLBTEXT:
1166 memcpy( (WCHAR *)lparam, buffer, size );
1167 break;
1168 case WM_NEXTMENU:
1169 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
1170 break;
1171 case WM_MDIGETACTIVE:
1172 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
1173 break;
1174 case WM_NCCALCSIZE:
1175 if (!wparam)
1176 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1177 else
1179 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1180 WINDOWPOS *wp = nc->lppos;
1181 memcpy( nc, buffer, min( sizeof(*nc), size ));
1182 if (size > sizeof(*nc))
1184 size -= sizeof(*nc);
1185 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
1187 nc->lppos = wp; /* restore the original pointer */
1189 break;
1190 case EM_GETSEL:
1191 case SBM_GETRANGE:
1192 case CB_GETEDITSEL:
1193 if (wparam)
1195 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1196 if (size <= sizeof(DWORD)) break;
1197 size -= sizeof(DWORD);
1198 buffer = (DWORD *)buffer + 1;
1200 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1201 break;
1202 case WM_MDICREATE:
1204 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
1205 LPCWSTR title = cs->szTitle, class = cs->szClass;
1206 memcpy( cs, buffer, min( sizeof(*cs), size ));
1207 cs->szTitle = title; /* restore the original pointers */
1208 cs->szClass = class;
1209 break;
1211 default:
1212 ERR( "should not happen: unexpected message %x\n", message );
1213 break;
1218 /***********************************************************************
1219 * reply_message
1221 * Send a reply to a sent message.
1223 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1225 struct packed_message data;
1226 int i, replied = info->flags & ISMEX_REPLIED;
1228 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1229 if (!remove && replied) return; /* replied already */
1231 data.count = 0;
1232 info->flags |= ISMEX_REPLIED;
1234 if (info->type == MSG_OTHER_PROCESS && !replied)
1236 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1237 info->msg.lParam, result, &data );
1240 SERVER_START_REQ( reply_message )
1242 req->result = result;
1243 req->remove = remove;
1244 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1245 wine_server_call( req );
1247 SERVER_END_REQ;
1251 /***********************************************************************
1252 * handle_internal_message
1254 * Handle an internal Wine message instead of calling the window proc.
1256 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1258 switch(msg)
1260 case WM_WINE_DESTROYWINDOW:
1261 return WIN_DestroyWindow( hwnd );
1262 case WM_WINE_SETWINDOWPOS:
1263 if (hwnd == GetDesktopWindow()) return 0;
1264 return USER_SetWindowPos( (WINDOWPOS *)lparam );
1265 case WM_WINE_SHOWWINDOW:
1266 if (hwnd == GetDesktopWindow()) return 0;
1267 return ShowWindow( hwnd, wparam );
1268 case WM_WINE_SETPARENT:
1269 if (hwnd == GetDesktopWindow()) return 0;
1270 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1271 case WM_WINE_SETWINDOWLONG:
1272 return WIN_SetWindowLong( hwnd, (short)LOWORD(wparam), HIWORD(wparam), lparam, TRUE );
1273 case WM_WINE_ENABLEWINDOW:
1274 if (hwnd == GetDesktopWindow()) return 0;
1275 return EnableWindow( hwnd, wparam );
1276 case WM_WINE_SETACTIVEWINDOW:
1277 if (hwnd == GetDesktopWindow()) return 0;
1278 return (LRESULT)SetActiveWindow( (HWND)wparam );
1279 case WM_WINE_KEYBOARD_LL_HOOK:
1280 case WM_WINE_MOUSE_LL_HOOK:
1282 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
1284 return call_current_hook( h_extra->handle, HC_ACTION, wparam, h_extra->lparam );
1286 default:
1287 if (msg >= WM_WINE_FIRST_DRIVER_MSG && msg <= WM_WINE_LAST_DRIVER_MSG)
1288 return USER_Driver->pWindowMessage( hwnd, msg, wparam, lparam );
1289 FIXME( "unknown internal message %x\n", msg );
1290 return 0;
1294 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1295 * to the memory handle, we keep track (in the server side) of all pairs of handle
1296 * used (the client passes its value and the content of the memory handle), and
1297 * the server stored both values (the client, and the local one, created after the
1298 * content). When a ACK message is generated, the list of pair is searched for a
1299 * matching pair, so that the client memory handle can be returned.
1301 struct DDE_pair {
1302 HGLOBAL client_hMem;
1303 HGLOBAL server_hMem;
1306 static struct DDE_pair* dde_pairs;
1307 static int dde_num_alloc;
1308 static int dde_num_used;
1310 static CRITICAL_SECTION dde_crst;
1311 static CRITICAL_SECTION_DEBUG critsect_debug =
1313 0, 0, &dde_crst,
1314 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
1315 0, 0, { (DWORD_PTR)(__FILE__ ": dde_crst") }
1317 static CRITICAL_SECTION dde_crst = { &critsect_debug, -1, 0, 0, 0, 0 };
1319 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1321 int i;
1322 #define GROWBY 4
1324 EnterCriticalSection(&dde_crst);
1326 /* now remember the pair of hMem on both sides */
1327 if (dde_num_used == dde_num_alloc)
1329 struct DDE_pair* tmp;
1330 if (dde_pairs)
1331 tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1332 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1333 else
1334 tmp = HeapAlloc( GetProcessHeap(), 0,
1335 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1337 if (!tmp)
1339 LeaveCriticalSection(&dde_crst);
1340 return FALSE;
1342 dde_pairs = tmp;
1343 /* zero out newly allocated part */
1344 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1345 dde_num_alloc += GROWBY;
1347 #undef GROWBY
1348 for (i = 0; i < dde_num_alloc; i++)
1350 if (dde_pairs[i].server_hMem == 0)
1352 dde_pairs[i].client_hMem = chm;
1353 dde_pairs[i].server_hMem = shm;
1354 dde_num_used++;
1355 break;
1358 LeaveCriticalSection(&dde_crst);
1359 return TRUE;
1362 static HGLOBAL dde_get_pair(HGLOBAL shm)
1364 int i;
1365 HGLOBAL ret = 0;
1367 EnterCriticalSection(&dde_crst);
1368 for (i = 0; i < dde_num_alloc; i++)
1370 if (dde_pairs[i].server_hMem == shm)
1372 /* free this pair */
1373 dde_pairs[i].server_hMem = 0;
1374 dde_num_used--;
1375 ret = dde_pairs[i].client_hMem;
1376 break;
1379 LeaveCriticalSection(&dde_crst);
1380 return ret;
1383 /***********************************************************************
1384 * post_dde_message
1386 * Post a DDE message
1388 static BOOL post_dde_message( struct packed_message *data, const struct send_message_info *info )
1390 void* ptr = NULL;
1391 int size = 0;
1392 UINT_PTR uiLo, uiHi;
1393 LPARAM lp = 0;
1394 HGLOBAL hunlock = 0;
1395 int i;
1396 DWORD res;
1398 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1399 return FALSE;
1401 lp = info->lparam;
1402 switch (info->msg)
1404 /* DDE messages which don't require packing are:
1405 * WM_DDE_INITIATE
1406 * WM_DDE_TERMINATE
1407 * WM_DDE_REQUEST
1408 * WM_DDE_UNADVISE
1410 case WM_DDE_ACK:
1411 if (HIWORD(uiHi))
1413 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1414 HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1415 if (h)
1417 /* send back the value of h on the other side */
1418 push_data( data, &h, sizeof(HGLOBAL) );
1419 lp = uiLo;
1420 TRACE( "send dde-ack %lx %08lx => %p\n", uiLo, uiHi, h );
1423 else
1425 /* uiHi should contain either an atom or 0 */
1426 TRACE( "send dde-ack %lx atom=%lx\n", uiLo, uiHi );
1427 lp = MAKELONG( uiLo, uiHi );
1429 break;
1430 case WM_DDE_ADVISE:
1431 case WM_DDE_DATA:
1432 case WM_DDE_POKE:
1433 size = 0;
1434 if (uiLo)
1436 size = GlobalSize( (HGLOBAL)uiLo ) ;
1437 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1438 (info->msg == WM_DDE_DATA && size < sizeof(DDEDATA)) ||
1439 (info->msg == WM_DDE_POKE && size < sizeof(DDEPOKE))
1441 return FALSE;
1443 else if (info->msg != WM_DDE_DATA) return FALSE;
1445 lp = uiHi;
1446 if (uiLo)
1448 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1450 DDEDATA *dde_data = (DDEDATA *)ptr;
1451 TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
1452 dde_data->unused, dde_data->fResponse, dde_data->fRelease,
1453 dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
1454 push_data( data, ptr, size );
1455 hunlock = (HGLOBAL)uiLo;
1458 TRACE( "send ddepack %u %lx\n", size, uiHi );
1459 break;
1460 case WM_DDE_EXECUTE:
1461 if (info->lparam)
1463 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1465 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1466 /* so that the other side can send it back on ACK */
1467 lp = info->lparam;
1468 hunlock = (HGLOBAL)info->lparam;
1471 break;
1473 SERVER_START_REQ( send_message )
1475 req->id = info->dest_tid;
1476 req->type = info->type;
1477 req->flags = 0;
1478 req->win = info->hwnd;
1479 req->msg = info->msg;
1480 req->wparam = info->wparam;
1481 req->lparam = lp;
1482 req->timeout = TIMEOUT_INFINITE;
1483 for (i = 0; i < data->count; i++)
1484 wine_server_add_data( req, data->data[i], data->size[i] );
1485 if ((res = wine_server_call( req )))
1487 if (res == STATUS_INVALID_PARAMETER)
1488 /* FIXME: find a STATUS_ value for this one */
1489 SetLastError( ERROR_INVALID_THREAD_ID );
1490 else
1491 SetLastError( RtlNtStatusToDosError(res) );
1493 else
1494 FreeDDElParam(info->msg, info->lparam);
1496 SERVER_END_REQ;
1497 if (hunlock) GlobalUnlock(hunlock);
1499 return !res;
1502 /***********************************************************************
1503 * unpack_dde_message
1505 * Unpack a posted DDE message received from another process.
1507 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1508 void **buffer, size_t size )
1510 UINT_PTR uiLo, uiHi;
1511 HGLOBAL hMem = 0;
1512 void* ptr;
1514 switch (message)
1516 case WM_DDE_ACK:
1517 if (size)
1519 /* hMem is being passed */
1520 if (size != sizeof(HGLOBAL)) return FALSE;
1521 if (!buffer || !*buffer) return FALSE;
1522 uiLo = *lparam;
1523 memcpy( &hMem, *buffer, size );
1524 uiHi = (UINT_PTR)hMem;
1525 TRACE("recv dde-ack %lx mem=%lx[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1527 else
1529 uiLo = LOWORD( *lparam );
1530 uiHi = HIWORD( *lparam );
1531 TRACE("recv dde-ack %lx atom=%lx\n", uiLo, uiHi);
1533 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1534 break;
1535 case WM_DDE_ADVISE:
1536 case WM_DDE_DATA:
1537 case WM_DDE_POKE:
1538 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1539 uiHi = *lparam;
1540 if (size)
1542 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size )))
1543 return FALSE;
1544 if ((ptr = GlobalLock( hMem )))
1546 memcpy( ptr, *buffer, size );
1547 GlobalUnlock( hMem );
1549 else
1551 GlobalFree( hMem );
1552 return FALSE;
1555 uiLo = (UINT_PTR)hMem;
1557 *lparam = PackDDElParam( message, uiLo, uiHi );
1558 break;
1559 case WM_DDE_EXECUTE:
1560 if (size)
1562 if (!buffer || !*buffer) return FALSE;
1563 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size ))) return FALSE;
1564 if ((ptr = GlobalLock( hMem )))
1566 memcpy( ptr, *buffer, size );
1567 GlobalUnlock( hMem );
1568 TRACE( "exec: pairing c=%08lx s=%p\n", *lparam, hMem );
1569 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1571 GlobalFree( hMem );
1572 return FALSE;
1575 else
1577 GlobalFree( hMem );
1578 return FALSE;
1580 } else return FALSE;
1581 *lparam = (LPARAM)hMem;
1582 break;
1584 return TRUE;
1587 /***********************************************************************
1588 * call_window_proc
1590 * Call a window procedure and the corresponding hooks.
1592 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1593 BOOL unicode, BOOL same_thread, enum wm_char_mapping mapping )
1595 struct user_thread_info *thread_info = get_user_thread_info();
1596 LRESULT result = 0;
1597 CWPSTRUCT cwp;
1598 CWPRETSTRUCT cwpret;
1600 if (thread_info->recursion_count > MAX_SENDMSG_RECURSION) return 0;
1601 thread_info->recursion_count++;
1603 if (msg & 0x80000000)
1605 result = handle_internal_message( hwnd, msg, wparam, lparam );
1606 goto done;
1609 /* first the WH_CALLWNDPROC hook */
1610 hwnd = WIN_GetFullHandle( hwnd );
1611 cwp.lParam = lparam;
1612 cwp.wParam = wparam;
1613 cwp.message = msg;
1614 cwp.hwnd = hwnd;
1615 HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1617 /* now call the window procedure */
1618 if (!WINPROC_call_window( hwnd, msg, wparam, lparam, &result, unicode, mapping )) goto done;
1620 /* and finally the WH_CALLWNDPROCRET hook */
1621 cwpret.lResult = result;
1622 cwpret.lParam = lparam;
1623 cwpret.wParam = wparam;
1624 cwpret.message = msg;
1625 cwpret.hwnd = hwnd;
1626 HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1627 done:
1628 thread_info->recursion_count--;
1629 return result;
1633 /***********************************************************************
1634 * send_parent_notify
1636 * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
1637 * the window has the WS_EX_NOPARENTNOTIFY style.
1639 static void send_parent_notify( HWND hwnd, WORD event, WORD idChild, POINT pt )
1641 /* pt has to be in the client coordinates of the parent window */
1642 MapWindowPoints( 0, hwnd, &pt, 1 );
1643 for (;;)
1645 HWND parent;
1647 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)) break;
1648 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
1649 if (!(parent = GetParent(hwnd))) break;
1650 if (parent == GetDesktopWindow()) break;
1651 MapWindowPoints( hwnd, parent, &pt, 1 );
1652 hwnd = parent;
1653 SendMessageW( hwnd, WM_PARENTNOTIFY,
1654 MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
1659 /***********************************************************************
1660 * accept_hardware_message
1662 * Tell the server we have passed the message to the app
1663 * (even though we may end up dropping it later on)
1665 static void accept_hardware_message( UINT hw_id, BOOL remove, HWND new_hwnd )
1667 SERVER_START_REQ( accept_hardware_message )
1669 req->hw_id = hw_id;
1670 req->remove = remove;
1671 req->new_win = new_hwnd;
1672 if (wine_server_call( req ))
1673 FIXME("Failed to reply to MSG_HARDWARE message. Message may not be removed from queue.\n");
1675 SERVER_END_REQ;
1679 /***********************************************************************
1680 * process_keyboard_message
1682 * returns TRUE if the contents of 'msg' should be passed to the application
1684 static BOOL process_keyboard_message( MSG *msg, UINT hw_id, HWND hwnd_filter,
1685 UINT first, UINT last, BOOL remove )
1687 EVENTMSG event;
1689 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN ||
1690 msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP)
1691 switch (msg->wParam)
1693 case VK_LSHIFT: case VK_RSHIFT:
1694 msg->wParam = VK_SHIFT;
1695 break;
1696 case VK_LCONTROL: case VK_RCONTROL:
1697 msg->wParam = VK_CONTROL;
1698 break;
1699 case VK_LMENU: case VK_RMENU:
1700 msg->wParam = VK_MENU;
1701 break;
1704 /* FIXME: is this really the right place for this hook? */
1705 event.message = msg->message;
1706 event.hwnd = msg->hwnd;
1707 event.time = msg->time;
1708 event.paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
1709 event.paramH = msg->lParam & 0x7FFF;
1710 if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
1711 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1713 /* check message filters */
1714 if (msg->message < first || msg->message > last) return FALSE;
1715 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1717 if (remove)
1719 if((msg->message == WM_KEYDOWN) &&
1720 (msg->hwnd != GetDesktopWindow()))
1722 /* Handle F1 key by sending out WM_HELP message */
1723 if (msg->wParam == VK_F1)
1725 PostMessageW( msg->hwnd, WM_KEYF1, 0, 0 );
1727 else if(msg->wParam >= VK_BROWSER_BACK &&
1728 msg->wParam <= VK_LAUNCH_APP2)
1730 /* FIXME: Process keystate */
1731 SendMessageW(msg->hwnd, WM_APPCOMMAND, (WPARAM)msg->hwnd, MAKELPARAM(0, (FAPPCOMMAND_KEY | (msg->wParam - VK_BROWSER_BACK + 1))));
1734 else if (msg->message == WM_KEYUP)
1736 /* Handle VK_APPS key by posting a WM_CONTEXTMENU message */
1737 if (msg->wParam == VK_APPS && !MENU_IsMenuActive())
1738 PostMessageW(msg->hwnd, WM_CONTEXTMENU, (WPARAM)msg->hwnd, (LPARAM)-1);
1742 if (HOOK_CallHooks( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
1743 LOWORD(msg->wParam), msg->lParam, TRUE ))
1745 /* skip this message */
1746 HOOK_CallHooks( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam, TRUE );
1747 accept_hardware_message( hw_id, TRUE, 0 );
1748 return FALSE;
1750 accept_hardware_message( hw_id, remove, 0 );
1752 if ( msg->message == WM_KEYDOWN || msg->message == WM_KEYUP )
1753 if ( ImmProcessKey(msg->hwnd, GetKeyboardLayout(0), msg->wParam, msg->lParam, 0) )
1754 msg->wParam = VK_PROCESSKEY;
1756 return TRUE;
1760 /***********************************************************************
1761 * process_mouse_message
1763 * returns TRUE if the contents of 'msg' should be passed to the application
1765 static BOOL process_mouse_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
1766 UINT first, UINT last, BOOL remove )
1768 static MSG clk_msg;
1770 POINT pt;
1771 UINT message;
1772 INT hittest;
1773 EVENTMSG event;
1774 GUITHREADINFO info;
1775 MOUSEHOOKSTRUCT hook;
1776 BOOL eatMsg;
1778 /* find the window to dispatch this mouse message to */
1780 GetGUIThreadInfo( GetCurrentThreadId(), &info );
1781 if (info.hwndCapture)
1783 hittest = HTCLIENT;
1784 msg->hwnd = info.hwndCapture;
1786 else
1788 msg->hwnd = WINPOS_WindowFromPoint( msg->hwnd, msg->pt, &hittest );
1791 if (!msg->hwnd || !WIN_IsCurrentThread( msg->hwnd ))
1793 accept_hardware_message( hw_id, TRUE, msg->hwnd );
1794 return FALSE;
1797 /* FIXME: is this really the right place for this hook? */
1798 event.message = msg->message;
1799 event.time = msg->time;
1800 event.hwnd = msg->hwnd;
1801 event.paramL = msg->pt.x;
1802 event.paramH = msg->pt.y;
1803 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1805 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1807 pt = msg->pt;
1808 message = msg->message;
1809 /* Note: windows has no concept of a non-client wheel message */
1810 if (message != WM_MOUSEWHEEL)
1812 if (hittest != HTCLIENT)
1814 message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
1815 msg->wParam = hittest;
1817 else
1819 /* coordinates don't get translated while tracking a menu */
1820 /* FIXME: should differentiate popups and top-level menus */
1821 if (!(info.flags & GUI_INMENUMODE))
1822 ScreenToClient( msg->hwnd, &pt );
1825 msg->lParam = MAKELONG( pt.x, pt.y );
1827 /* translate double clicks */
1829 if ((msg->message == WM_LBUTTONDOWN) ||
1830 (msg->message == WM_RBUTTONDOWN) ||
1831 (msg->message == WM_MBUTTONDOWN) ||
1832 (msg->message == WM_XBUTTONDOWN))
1834 BOOL update = remove;
1836 /* translate double clicks -
1837 * note that ...MOUSEMOVEs can slip in between
1838 * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
1840 if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
1841 hittest != HTCLIENT ||
1842 (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
1844 if ((msg->message == clk_msg.message) &&
1845 (msg->hwnd == clk_msg.hwnd) &&
1846 (msg->wParam == clk_msg.wParam) &&
1847 (msg->time - clk_msg.time < GetDoubleClickTime()) &&
1848 (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
1849 (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
1851 message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
1852 if (update)
1854 clk_msg.message = 0; /* clear the double click conditions */
1855 update = FALSE;
1859 if (message < first || message > last) return FALSE;
1860 /* update static double click conditions */
1861 if (update) clk_msg = *msg;
1863 else
1865 if (message < first || message > last) return FALSE;
1868 /* message is accepted now (but may still get dropped) */
1870 hook.pt = msg->pt;
1871 hook.hwnd = msg->hwnd;
1872 hook.wHitTestCode = hittest;
1873 hook.dwExtraInfo = extra_info;
1874 if (HOOK_CallHooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
1875 message, (LPARAM)&hook, TRUE ))
1877 hook.pt = msg->pt;
1878 hook.hwnd = msg->hwnd;
1879 hook.wHitTestCode = hittest;
1880 hook.dwExtraInfo = extra_info;
1881 HOOK_CallHooks( WH_CBT, HCBT_CLICKSKIPPED, message, (LPARAM)&hook, TRUE );
1882 accept_hardware_message( hw_id, TRUE, 0 );
1883 return FALSE;
1886 if ((hittest == HTERROR) || (hittest == HTNOWHERE))
1888 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
1889 MAKELONG( hittest, msg->message ));
1890 accept_hardware_message( hw_id, TRUE, 0 );
1891 return FALSE;
1894 accept_hardware_message( hw_id, remove, 0 );
1896 if (!remove || info.hwndCapture)
1898 msg->message = message;
1899 return TRUE;
1902 eatMsg = FALSE;
1904 if ((msg->message == WM_LBUTTONDOWN) ||
1905 (msg->message == WM_RBUTTONDOWN) ||
1906 (msg->message == WM_MBUTTONDOWN) ||
1907 (msg->message == WM_XBUTTONDOWN))
1909 /* Send the WM_PARENTNOTIFY,
1910 * note that even for double/nonclient clicks
1911 * notification message is still WM_L/M/RBUTTONDOWN.
1913 send_parent_notify( msg->hwnd, msg->message, 0, msg->pt );
1915 /* Activate the window if needed */
1917 if (msg->hwnd != info.hwndActive)
1919 HWND hwndTop = msg->hwnd;
1920 while (hwndTop)
1922 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
1923 hwndTop = GetParent( hwndTop );
1926 if (hwndTop && hwndTop != GetDesktopWindow())
1928 LONG ret = SendMessageW( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
1929 MAKELONG( hittest, msg->message ) );
1930 switch(ret)
1932 case MA_NOACTIVATEANDEAT:
1933 eatMsg = TRUE;
1934 /* fall through */
1935 case MA_NOACTIVATE:
1936 break;
1937 case MA_ACTIVATEANDEAT:
1938 eatMsg = TRUE;
1939 /* fall through */
1940 case MA_ACTIVATE:
1941 case 0:
1942 if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
1943 break;
1944 default:
1945 WARN( "unknown WM_MOUSEACTIVATE code %d\n", ret );
1946 break;
1952 /* send the WM_SETCURSOR message */
1954 /* Windows sends the normal mouse message as the message parameter
1955 in the WM_SETCURSOR message even if it's non-client mouse message */
1956 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd, MAKELONG( hittest, msg->message ));
1958 msg->message = message;
1959 return !eatMsg;
1963 /***********************************************************************
1964 * process_hardware_message
1966 * Process a hardware message; return TRUE if message should be passed on to the app
1968 static BOOL process_hardware_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
1969 UINT first, UINT last, BOOL remove )
1971 if (is_keyboard_message( msg->message ))
1972 return process_keyboard_message( msg, hw_id, hwnd_filter, first, last, remove );
1974 if (is_mouse_message( msg->message ))
1975 return process_mouse_message( msg, hw_id, extra_info, hwnd_filter, first, last, remove );
1977 ERR( "unknown message type %x\n", msg->message );
1978 return FALSE;
1982 /***********************************************************************
1983 * call_sendmsg_callback
1985 * Call the callback function of SendMessageCallback.
1987 static inline void call_sendmsg_callback( SENDASYNCPROC callback, HWND hwnd, UINT msg,
1988 ULONG_PTR data, LRESULT result )
1990 if (!callback) return;
1992 if (TRACE_ON(relay))
1993 DPRINTF( "%04x:Call message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1994 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
1995 data, result );
1996 callback( hwnd, msg, data, result );
1997 if (TRACE_ON(relay))
1998 DPRINTF( "%04x:Ret message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1999 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
2000 data, result );
2004 /***********************************************************************
2005 * peek_message
2007 * Peek for a message matching the given parameters. Return FALSE if none available.
2008 * All pending sent messages are processed before returning.
2010 static BOOL peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2012 LRESULT result;
2013 ULONG_PTR extra_info = 0;
2014 struct user_thread_info *thread_info = get_user_thread_info();
2015 struct received_message_info info, *old_info;
2016 unsigned int wake_mask, changed_mask = HIWORD(flags);
2017 unsigned int hw_id = 0; /* id of previous hardware message */
2019 if (!first && !last) last = ~0;
2020 if (!changed_mask) changed_mask = QS_ALLINPUT;
2021 wake_mask = changed_mask & (QS_SENDMESSAGE | QS_SMRESULT);
2023 for (;;)
2025 NTSTATUS res;
2026 void *buffer = NULL;
2027 size_t size = 0, buffer_size = 0;
2029 do /* loop while buffer is too small */
2031 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
2032 return FALSE;
2033 SERVER_START_REQ( get_message )
2035 req->flags = flags;
2036 req->get_win = hwnd;
2037 req->get_first = first;
2038 req->get_last = last;
2039 req->hw_id = hw_id;
2040 req->wake_mask = wake_mask;
2041 req->changed_mask = changed_mask;
2042 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
2043 if (!(res = wine_server_call( req )))
2045 size = wine_server_reply_size( reply );
2046 info.type = reply->type;
2047 info.msg.hwnd = reply->win;
2048 info.msg.message = reply->msg;
2049 info.msg.wParam = reply->wparam;
2050 info.msg.lParam = reply->lparam;
2051 info.msg.time = reply->time;
2052 info.msg.pt.x = reply->x;
2053 info.msg.pt.y = reply->y;
2054 hw_id = reply->hw_id;
2055 extra_info = reply->info;
2056 thread_info->active_hooks = reply->active_hooks;
2058 else
2060 HeapFree( GetProcessHeap(), 0, buffer );
2061 buffer_size = reply->total;
2064 SERVER_END_REQ;
2065 } while (res == STATUS_BUFFER_OVERFLOW);
2067 if (res) return FALSE;
2069 TRACE( "got type %d msg %x (%s) hwnd %p wp %lx lp %lx\n",
2070 info.type, info.msg.message,
2071 (info.type == MSG_WINEVENT) ? "MSG_WINEVENT" : SPY_GetMsgName(info.msg.message, info.msg.hwnd),
2072 info.msg.hwnd, info.msg.wParam, info.msg.lParam );
2074 switch(info.type)
2076 case MSG_ASCII:
2077 case MSG_UNICODE:
2078 info.flags = ISMEX_SEND;
2079 break;
2080 case MSG_NOTIFY:
2081 info.flags = ISMEX_NOTIFY;
2082 break;
2083 case MSG_CALLBACK:
2084 info.flags = ISMEX_CALLBACK;
2085 break;
2086 case MSG_CALLBACK_RESULT:
2087 if (size >= sizeof(struct callback_msg_data))
2089 const struct callback_msg_data *data = (const struct callback_msg_data *)buffer;
2090 call_sendmsg_callback( data->callback, info.msg.hwnd,
2091 info.msg.message, data->data, data->result );
2093 goto next;
2094 case MSG_WINEVENT:
2095 if (size >= sizeof(struct winevent_msg_data))
2097 WINEVENTPROC hook_proc;
2098 const struct winevent_msg_data *data = (const struct winevent_msg_data *)buffer;
2100 hook_proc = data->hook_proc;
2101 size -= sizeof(*data);
2102 if (size)
2104 WCHAR module[MAX_PATH];
2106 size = min( size, (MAX_PATH - 1) * sizeof(WCHAR) );
2107 memcpy( module, buffer, size );
2108 module[size / sizeof(WCHAR)] = 0;
2109 if (!(hook_proc = get_hook_proc( hook_proc, module )))
2111 ERR( "invalid winevent hook module name %s\n", debugstr_w(module) );
2112 goto next;
2116 if (TRACE_ON(relay))
2117 DPRINTF( "%04x:Call winevent proc %p (hook=%p,event=%x,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04x,time=%x)\n",
2118 GetCurrentThreadId(), hook_proc,
2119 data->hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2120 info.msg.lParam, data->tid, info.msg.time);
2122 hook_proc( data->hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2123 info.msg.lParam, data->tid, info.msg.time );
2125 if (TRACE_ON(relay))
2126 DPRINTF( "%04x:Ret winevent proc %p (hook=%p,event=%x,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04x,time=%x)\n",
2127 GetCurrentThreadId(), hook_proc,
2128 data->hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2129 info.msg.lParam, data->tid, info.msg.time);
2131 goto next;
2132 case MSG_OTHER_PROCESS:
2133 info.flags = ISMEX_SEND;
2134 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2135 &info.msg.lParam, &buffer, size ))
2137 /* ignore it */
2138 reply_message( &info, 0, TRUE );
2139 goto next;
2141 break;
2142 case MSG_HARDWARE:
2143 if (!process_hardware_message( &info.msg, hw_id, extra_info,
2144 hwnd, first, last, flags & PM_REMOVE ))
2146 TRACE("dropping msg %x\n", info.msg.message );
2147 goto next; /* ignore it */
2149 thread_info->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
2150 /* fall through */
2151 case MSG_POSTED:
2152 thread_info->GetMessageExtraInfoVal = extra_info;
2153 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
2155 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2156 &info.msg.lParam, &buffer, size ))
2157 goto next; /* ignore it */
2159 *msg = info.msg;
2160 HeapFree( GetProcessHeap(), 0, buffer );
2161 return TRUE;
2164 /* if we get here, we have a sent message; call the window procedure */
2165 old_info = thread_info->receive_info;
2166 thread_info->receive_info = &info;
2167 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
2168 info.msg.lParam, (info.type != MSG_ASCII), FALSE,
2169 WMCHAR_MAP_RECVMESSAGE );
2170 reply_message( &info, result, TRUE );
2171 thread_info->receive_info = old_info;
2173 /* if some PM_QS* flags were specified, only handle sent messages from now on */
2174 if (HIWORD(flags)) flags = PM_QS_SENDMESSAGE | LOWORD(flags);
2175 next:
2176 HeapFree( GetProcessHeap(), 0, buffer );
2181 /***********************************************************************
2182 * process_sent_messages
2184 * Process all pending sent messages.
2186 static inline void process_sent_messages(void)
2188 MSG msg;
2189 peek_message( &msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE );
2193 /***********************************************************************
2194 * get_server_queue_handle
2196 * Get a handle to the server message queue for the current thread.
2198 static HANDLE get_server_queue_handle(void)
2200 struct user_thread_info *thread_info = get_user_thread_info();
2201 HANDLE ret;
2203 if (!(ret = thread_info->server_queue))
2205 SERVER_START_REQ( get_msg_queue )
2207 wine_server_call( req );
2208 ret = reply->handle;
2210 SERVER_END_REQ;
2211 thread_info->server_queue = ret;
2212 if (!ret) ERR( "Cannot get server thread queue\n" );
2214 return ret;
2218 /***********************************************************************
2219 * wait_message_reply
2221 * Wait until a sent message gets replied to.
2223 static void wait_message_reply( UINT flags )
2225 HANDLE server_queue = get_server_queue_handle();
2227 for (;;)
2229 unsigned int wake_bits = 0;
2230 DWORD dwlc, res;
2232 SERVER_START_REQ( set_queue_mask )
2234 req->wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
2235 req->changed_mask = req->wake_mask;
2236 req->skip_wait = 1;
2237 if (!wine_server_call( req ))
2238 wake_bits = reply->wake_bits;
2240 SERVER_END_REQ;
2242 if (wake_bits & QS_SMRESULT) return; /* got a result */
2243 if (wake_bits & QS_SENDMESSAGE)
2245 /* Process the sent message immediately */
2246 process_sent_messages();
2247 continue;
2250 /* now wait for it */
2252 ReleaseThunkLock( &dwlc );
2253 res = USER_Driver->pMsgWaitForMultipleObjectsEx( 1, &server_queue,
2254 INFINITE, QS_SENDMESSAGE, 0 );
2255 if (dwlc) RestoreThunkLock( dwlc );
2259 /***********************************************************************
2260 * put_message_in_queue
2262 * Put a sent message into the destination queue.
2263 * For inter-process message, reply_size is set to expected size of reply data.
2265 static BOOL put_message_in_queue( const struct send_message_info *info, size_t *reply_size )
2267 struct packed_message data;
2268 message_data_t msg_data;
2269 unsigned int res;
2270 int i;
2271 timeout_t timeout = TIMEOUT_INFINITE;
2273 /* Check for INFINITE timeout for compatibility with Win9x,
2274 * although Windows >= NT does not do so
2276 if (info->type != MSG_NOTIFY &&
2277 info->type != MSG_CALLBACK &&
2278 info->type != MSG_POSTED &&
2279 info->timeout &&
2280 info->timeout != INFINITE)
2282 /* timeout is signed despite the prototype */
2283 timeout = (timeout_t)max( 0, (int)info->timeout ) * -10000;
2286 data.count = 0;
2287 if (info->type == MSG_OTHER_PROCESS)
2289 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
2290 if (data.count == -1)
2292 WARN( "cannot pack message %x\n", info->msg );
2293 return FALSE;
2296 else if (info->type == MSG_CALLBACK)
2298 msg_data.callback.callback = info->callback;
2299 msg_data.callback.data = info->data;
2300 msg_data.callback.result = 0;
2301 data.data[0] = &msg_data;
2302 data.size[0] = sizeof(msg_data.callback);
2303 data.count = 1;
2305 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
2307 return post_dde_message( &data, info );
2310 SERVER_START_REQ( send_message )
2312 req->id = info->dest_tid;
2313 req->type = info->type;
2314 req->flags = 0;
2315 req->win = info->hwnd;
2316 req->msg = info->msg;
2317 req->wparam = info->wparam;
2318 req->lparam = info->lparam;
2319 req->timeout = timeout;
2321 if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
2322 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
2323 if ((res = wine_server_call( req )))
2325 if (res == STATUS_INVALID_PARAMETER)
2326 /* FIXME: find a STATUS_ value for this one */
2327 SetLastError( ERROR_INVALID_THREAD_ID );
2328 else
2329 SetLastError( RtlNtStatusToDosError(res) );
2332 SERVER_END_REQ;
2333 return !res;
2337 /***********************************************************************
2338 * retrieve_reply
2340 * Retrieve a message reply from the server.
2342 static LRESULT retrieve_reply( const struct send_message_info *info,
2343 size_t reply_size, LRESULT *result )
2345 NTSTATUS status;
2346 void *reply_data = NULL;
2348 if (reply_size)
2350 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
2352 WARN( "no memory for reply, will be truncated\n" );
2353 reply_size = 0;
2356 SERVER_START_REQ( get_message_reply )
2358 req->cancel = 1;
2359 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
2360 if (!(status = wine_server_call( req ))) *result = reply->result;
2361 reply_size = wine_server_reply_size( reply );
2363 SERVER_END_REQ;
2364 if (!status && reply_size)
2365 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
2367 HeapFree( GetProcessHeap(), 0, reply_data );
2369 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx got reply %lx (err=%d)\n",
2370 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
2371 info->lparam, *result, status );
2373 /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
2374 if (status) SetLastError( RtlNtStatusToDosError(status) );
2375 return !status;
2379 /***********************************************************************
2380 * send_inter_thread_message
2382 static LRESULT send_inter_thread_message( const struct send_message_info *info, LRESULT *res_ptr )
2384 size_t reply_size = 0;
2386 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
2387 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
2389 USER_CheckNotLock();
2391 if (!put_message_in_queue( info, &reply_size )) return 0;
2393 /* there's no reply to wait for on notify/callback messages */
2394 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
2396 wait_message_reply( info->flags );
2397 return retrieve_reply( info, reply_size, res_ptr );
2401 /***********************************************************************
2402 * send_inter_thread_callback
2404 static LRESULT send_inter_thread_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
2405 LRESULT *result, void *arg )
2407 struct send_message_info *info = arg;
2408 info->hwnd = hwnd;
2409 info->msg = msg;
2410 info->wparam = wp;
2411 info->lparam = lp;
2412 return send_inter_thread_message( info, result );
2416 /***********************************************************************
2417 * send_message
2419 * Backend implementation of the various SendMessage functions.
2421 static BOOL send_message( struct send_message_info *info, DWORD_PTR *res_ptr, BOOL unicode )
2423 DWORD dest_pid;
2424 BOOL ret;
2425 LRESULT result;
2427 if (is_broadcast(info->hwnd))
2429 EnumWindows( broadcast_message_callback, (LPARAM)info );
2430 if (res_ptr) *res_ptr = 1;
2431 return TRUE;
2434 if (!(info->dest_tid = GetWindowThreadProcessId( info->hwnd, &dest_pid ))) return FALSE;
2436 if (USER_IsExitingThread( info->dest_tid )) return FALSE;
2438 SPY_EnterMessage( SPY_SENDMESSAGE, info->hwnd, info->msg, info->wparam, info->lparam );
2440 if (info->dest_tid == GetCurrentThreadId())
2442 result = call_window_proc( info->hwnd, info->msg, info->wparam, info->lparam,
2443 unicode, TRUE, info->wm_char );
2444 if (info->type == MSG_CALLBACK)
2445 call_sendmsg_callback( info->callback, info->hwnd, info->msg, info->data, result );
2446 ret = TRUE;
2448 else
2450 if (dest_pid != GetCurrentProcessId() && (info->type == MSG_ASCII || info->type == MSG_UNICODE))
2451 info->type = MSG_OTHER_PROCESS;
2453 /* MSG_ASCII can be sent unconverted except for WM_CHAR; everything else needs to be Unicode */
2454 if (!unicode && is_unicode_message( info->msg ) &&
2455 (info->type != MSG_ASCII || info->msg == WM_CHAR))
2456 ret = WINPROC_CallProcAtoW( send_inter_thread_callback, info->hwnd, info->msg,
2457 info->wparam, info->lparam, &result, info, info->wm_char );
2458 else
2459 ret = send_inter_thread_message( info, &result );
2462 SPY_ExitMessage( SPY_RESULT_OK, info->hwnd, info->msg, result, info->wparam, info->lparam );
2463 if (ret && res_ptr) *res_ptr = result;
2464 return ret;
2468 /***********************************************************************
2469 * MSG_SendInternalMessageTimeout
2471 * Same as SendMessageTimeoutW but sends the message to a specific thread
2472 * without requiring a window handle. Only works for internal Wine messages.
2474 LRESULT MSG_SendInternalMessageTimeout( DWORD dest_pid, DWORD dest_tid,
2475 UINT msg, WPARAM wparam, LPARAM lparam,
2476 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2478 struct send_message_info info;
2479 LRESULT ret, result;
2481 assert( msg & 0x80000000 ); /* must be an internal Wine message */
2483 info.type = MSG_UNICODE;
2484 info.dest_tid = dest_tid;
2485 info.hwnd = 0;
2486 info.msg = msg;
2487 info.wparam = wparam;
2488 info.lparam = lparam;
2489 info.flags = flags;
2490 info.timeout = timeout;
2492 if (USER_IsExitingThread( dest_tid )) return 0;
2494 if (dest_tid == GetCurrentThreadId())
2496 result = handle_internal_message( 0, msg, wparam, lparam );
2497 ret = 1;
2499 else
2501 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
2502 ret = send_inter_thread_message( &info, &result );
2504 if (ret && res_ptr) *res_ptr = result;
2505 return ret;
2509 /***********************************************************************
2510 * SendMessageTimeoutW (USER32.@)
2512 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2513 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2515 struct send_message_info info;
2517 info.type = MSG_UNICODE;
2518 info.hwnd = hwnd;
2519 info.msg = msg;
2520 info.wparam = wparam;
2521 info.lparam = lparam;
2522 info.flags = flags;
2523 info.timeout = timeout;
2525 return send_message( &info, res_ptr, TRUE );
2528 /***********************************************************************
2529 * SendMessageTimeoutA (USER32.@)
2531 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2532 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2534 struct send_message_info info;
2536 info.type = MSG_ASCII;
2537 info.hwnd = hwnd;
2538 info.msg = msg;
2539 info.wparam = wparam;
2540 info.lparam = lparam;
2541 info.flags = flags;
2542 info.timeout = timeout;
2543 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
2545 return send_message( &info, res_ptr, FALSE );
2549 /***********************************************************************
2550 * SendMessageW (USER32.@)
2552 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2554 DWORD_PTR res = 0;
2555 struct send_message_info info;
2557 info.type = MSG_UNICODE;
2558 info.hwnd = hwnd;
2559 info.msg = msg;
2560 info.wparam = wparam;
2561 info.lparam = lparam;
2562 info.flags = SMTO_NORMAL;
2563 info.timeout = 0;
2565 send_message( &info, &res, TRUE );
2566 return res;
2570 /***********************************************************************
2571 * SendMessageA (USER32.@)
2573 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2575 DWORD_PTR res = 0;
2576 struct send_message_info info;
2578 info.type = MSG_ASCII;
2579 info.hwnd = hwnd;
2580 info.msg = msg;
2581 info.wparam = wparam;
2582 info.lparam = lparam;
2583 info.flags = SMTO_NORMAL;
2584 info.timeout = 0;
2585 info.wm_char = WMCHAR_MAP_SENDMESSAGE;
2587 send_message( &info, &res, FALSE );
2588 return res;
2592 /***********************************************************************
2593 * SendNotifyMessageA (USER32.@)
2595 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2597 struct send_message_info info;
2599 if (is_pointer_message(msg))
2601 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2602 return FALSE;
2605 info.type = MSG_NOTIFY;
2606 info.hwnd = hwnd;
2607 info.msg = msg;
2608 info.wparam = wparam;
2609 info.lparam = lparam;
2610 info.flags = 0;
2611 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
2613 return send_message( &info, NULL, FALSE );
2617 /***********************************************************************
2618 * SendNotifyMessageW (USER32.@)
2620 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2622 struct send_message_info info;
2624 if (is_pointer_message(msg))
2626 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2627 return FALSE;
2630 info.type = MSG_NOTIFY;
2631 info.hwnd = hwnd;
2632 info.msg = msg;
2633 info.wparam = wparam;
2634 info.lparam = lparam;
2635 info.flags = 0;
2637 return send_message( &info, NULL, TRUE );
2641 /***********************************************************************
2642 * SendMessageCallbackA (USER32.@)
2644 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2645 SENDASYNCPROC callback, ULONG_PTR data )
2647 struct send_message_info info;
2649 if (is_pointer_message(msg))
2651 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2652 return FALSE;
2655 info.type = MSG_CALLBACK;
2656 info.hwnd = hwnd;
2657 info.msg = msg;
2658 info.wparam = wparam;
2659 info.lparam = lparam;
2660 info.callback = callback;
2661 info.data = data;
2662 info.flags = 0;
2663 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
2665 return send_message( &info, NULL, FALSE );
2669 /***********************************************************************
2670 * SendMessageCallbackW (USER32.@)
2672 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2673 SENDASYNCPROC callback, ULONG_PTR data )
2675 struct send_message_info info;
2677 if (is_pointer_message(msg))
2679 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2680 return FALSE;
2683 info.type = MSG_CALLBACK;
2684 info.hwnd = hwnd;
2685 info.msg = msg;
2686 info.wparam = wparam;
2687 info.lparam = lparam;
2688 info.callback = callback;
2689 info.data = data;
2690 info.flags = 0;
2692 return send_message( &info, NULL, TRUE );
2696 /***********************************************************************
2697 * ReplyMessage (USER32.@)
2699 BOOL WINAPI ReplyMessage( LRESULT result )
2701 struct received_message_info *info = get_user_thread_info()->receive_info;
2703 if (!info) return FALSE;
2704 reply_message( info, result, FALSE );
2705 return TRUE;
2709 /***********************************************************************
2710 * InSendMessage (USER32.@)
2712 BOOL WINAPI InSendMessage(void)
2714 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
2718 /***********************************************************************
2719 * InSendMessageEx (USER32.@)
2721 DWORD WINAPI InSendMessageEx( LPVOID reserved )
2723 struct received_message_info *info = get_user_thread_info()->receive_info;
2725 if (info) return info->flags;
2726 return ISMEX_NOSEND;
2730 /***********************************************************************
2731 * PostMessageA (USER32.@)
2733 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2735 if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
2736 return PostMessageW( hwnd, msg, wparam, lparam );
2740 /***********************************************************************
2741 * PostMessageW (USER32.@)
2743 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2745 struct send_message_info info;
2747 if (is_pointer_message( msg ))
2749 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2750 return FALSE;
2753 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
2754 hwnd, msg, SPY_GetMsgName(msg, hwnd), wparam, lparam );
2756 info.type = MSG_POSTED;
2757 info.hwnd = hwnd;
2758 info.msg = msg;
2759 info.wparam = wparam;
2760 info.lparam = lparam;
2761 info.flags = 0;
2763 if (is_broadcast(hwnd))
2765 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2766 return TRUE;
2769 if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2771 if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2773 if (USER_IsExitingThread( info.dest_tid )) return TRUE;
2775 return put_message_in_queue( &info, NULL );
2779 /**********************************************************************
2780 * PostThreadMessageA (USER32.@)
2782 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2784 if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
2785 return PostThreadMessageW( thread, msg, wparam, lparam );
2789 /**********************************************************************
2790 * PostThreadMessageW (USER32.@)
2792 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2794 struct send_message_info info;
2796 if (is_pointer_message( msg ))
2798 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2799 return FALSE;
2801 if (USER_IsExitingThread( thread )) return TRUE;
2803 info.type = MSG_POSTED;
2804 info.dest_tid = thread;
2805 info.hwnd = 0;
2806 info.msg = msg;
2807 info.wparam = wparam;
2808 info.lparam = lparam;
2809 info.flags = 0;
2810 return put_message_in_queue( &info, NULL );
2814 /***********************************************************************
2815 * PostQuitMessage (USER32.@)
2817 * Posts a quit message to the current thread's message queue.
2819 * PARAMS
2820 * exit_code [I] Exit code to return from message loop.
2822 * RETURNS
2823 * Nothing.
2825 * NOTES
2826 * This function is not the same as calling:
2827 *|PostThreadMessage(GetCurrentThreadId(), WM_QUIT, exit_code, 0);
2828 * It instead sets a flag in the message queue that signals it to generate
2829 * a WM_QUIT message when there are no other pending sent or posted messages
2830 * in the queue.
2832 void WINAPI PostQuitMessage( INT exit_code )
2834 SERVER_START_REQ( post_quit_message )
2836 req->exit_code = exit_code;
2837 wine_server_call( req );
2839 SERVER_END_REQ;
2843 /***********************************************************************
2844 * PeekMessageW (USER32.@)
2846 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2848 struct user_thread_info *thread_info = get_user_thread_info();
2849 MSG msg;
2851 USER_CheckNotLock();
2853 /* check for graphics events */
2854 USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
2856 hwnd = WIN_GetFullHandle( hwnd );
2858 for (;;)
2860 if (!peek_message( &msg, hwnd, first, last, flags ))
2862 if (!(flags & PM_NOYIELD))
2864 DWORD count;
2865 ReleaseThunkLock(&count);
2866 NtYieldExecution();
2867 if (count) RestoreThunkLock(count);
2869 return FALSE;
2871 if (msg.message & 0x80000000)
2873 if (!(flags & PM_REMOVE))
2875 /* Have to remove the message explicitly.
2876 Do this before handling it, because the message handler may
2877 call PeekMessage again */
2878 peek_message( &msg, msg.hwnd, msg.message, msg.message, flags | PM_REMOVE );
2880 handle_internal_message( msg.hwnd, msg.message, msg.wParam, msg.lParam );
2882 else break;
2885 thread_info->GetMessageTimeVal = msg.time;
2886 msg.pt.x = (short)LOWORD( thread_info->GetMessagePosVal );
2887 msg.pt.y = (short)HIWORD( thread_info->GetMessagePosVal );
2889 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
2891 /* copy back our internal safe copy of message data to msg_out.
2892 * msg_out is a variable from the *program*, so it can't be used
2893 * internally as it can get "corrupted" by our use of SendMessage()
2894 * (back to the program) inside the message handling itself. */
2895 if (!msg_out)
2897 SetLastError( ERROR_NOACCESS );
2898 return FALSE;
2900 *msg_out = msg;
2901 return TRUE;
2905 /***********************************************************************
2906 * PeekMessageA (USER32.@)
2908 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2910 if (get_pending_wmchar( msg, first, last, (flags & PM_REMOVE) )) return TRUE;
2911 if (!PeekMessageW( msg, hwnd, first, last, flags )) return FALSE;
2912 map_wparam_WtoA( msg, (flags & PM_REMOVE) );
2913 return TRUE;
2917 /***********************************************************************
2918 * GetMessageW (USER32.@)
2920 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2922 HANDLE server_queue = get_server_queue_handle();
2923 int mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2925 if (first || last)
2927 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2928 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2929 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2930 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2931 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2932 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2934 else mask = QS_ALLINPUT;
2936 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE | PM_NOYIELD | (mask << 16) ))
2938 DWORD dwlc;
2940 ReleaseThunkLock( &dwlc );
2941 USER_Driver->pMsgWaitForMultipleObjectsEx( 1, &server_queue, INFINITE, mask, 0 );
2942 if (dwlc) RestoreThunkLock( dwlc );
2945 return (msg->message != WM_QUIT);
2949 /***********************************************************************
2950 * GetMessageA (USER32.@)
2952 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2954 if (get_pending_wmchar( msg, first, last, TRUE )) return TRUE;
2955 GetMessageW( msg, hwnd, first, last );
2956 map_wparam_WtoA( msg, TRUE );
2957 return (msg->message != WM_QUIT);
2961 /***********************************************************************
2962 * IsDialogMessageA (USER32.@)
2963 * IsDialogMessage (USER32.@)
2965 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2967 MSG msg = *pmsg;
2968 map_wparam_AtoW( msg.message, &msg.wParam, WMCHAR_MAP_NOMAPPING );
2969 return IsDialogMessageW( hwndDlg, &msg );
2973 /***********************************************************************
2974 * TranslateMessage (USER32.@)
2976 * Implementation of TranslateMessage.
2978 * TranslateMessage translates virtual-key messages into character-messages,
2979 * as follows :
2980 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
2981 * ditto replacing WM_* with WM_SYS*
2982 * This produces WM_CHAR messages only for keys mapped to ASCII characters
2983 * by the keyboard driver.
2985 * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
2986 * return value is nonzero, regardless of the translation.
2989 BOOL WINAPI TranslateMessage( const MSG *msg )
2991 UINT message;
2992 WCHAR wp[2];
2993 BYTE state[256];
2995 if (msg->message < WM_KEYFIRST || msg->message > WM_KEYLAST) return FALSE;
2996 if (msg->message != WM_KEYDOWN && msg->message != WM_SYSKEYDOWN) return TRUE;
2998 TRACE_(key)("Translating key %s (%04lx), scancode %02x\n",
2999 SPY_GetVKeyName(msg->wParam), msg->wParam, LOBYTE(HIWORD(msg->lParam)));
3001 if ( msg->wParam == VK_PROCESSKEY )
3002 return ImmTranslateMessage(msg->hwnd, msg->message, msg->wParam, msg->lParam);
3004 GetKeyboardState( state );
3005 /* FIXME : should handle ToUnicode yielding 2 */
3006 switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), state, wp, 2, 0))
3008 case 1:
3009 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
3010 TRACE_(key)("1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
3011 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
3012 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
3013 break;
3015 case -1:
3016 message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
3017 TRACE_(key)("-1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
3018 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
3019 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
3020 break;
3022 return TRUE;
3026 /***********************************************************************
3027 * DispatchMessageA (USER32.@)
3029 * See DispatchMessageW.
3031 LRESULT WINAPI DispatchMessageA( const MSG* msg )
3033 LRESULT retval;
3035 /* Process timer messages */
3036 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3038 if (msg->lParam) return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
3039 msg->message, msg->wParam, GetTickCount() );
3041 if (!msg->hwnd) return 0;
3043 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3044 msg->wParam, msg->lParam );
3046 if (!WINPROC_call_window( msg->hwnd, msg->message, msg->wParam, msg->lParam,
3047 &retval, FALSE, WMCHAR_MAP_DISPATCHMESSAGE ))
3049 if (!IsWindow( msg->hwnd )) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3050 else SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3051 retval = 0;
3054 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3055 msg->wParam, msg->lParam );
3057 if (msg->message == WM_PAINT)
3059 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3060 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3061 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3062 DeleteObject( hrgn );
3064 return retval;
3068 /***********************************************************************
3069 * DispatchMessageW (USER32.@) Process a message
3071 * Process the message specified in the structure *_msg_.
3073 * If the lpMsg parameter points to a WM_TIMER message and the
3074 * parameter of the WM_TIMER message is not NULL, the lParam parameter
3075 * points to the function that is called instead of the window
3076 * procedure.
3078 * The message must be valid.
3080 * RETURNS
3082 * DispatchMessage() returns the result of the window procedure invoked.
3084 * CONFORMANCE
3086 * ECMA-234, Win32
3089 LRESULT WINAPI DispatchMessageW( const MSG* msg )
3091 LRESULT retval;
3093 /* Process timer messages */
3094 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3096 if (msg->lParam) return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
3097 msg->message, msg->wParam, GetTickCount() );
3099 if (!msg->hwnd) return 0;
3101 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3102 msg->wParam, msg->lParam );
3104 if (!WINPROC_call_window( msg->hwnd, msg->message, msg->wParam, msg->lParam,
3105 &retval, TRUE, WMCHAR_MAP_DISPATCHMESSAGE ))
3107 if (!IsWindow( msg->hwnd )) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3108 else SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3109 retval = 0;
3112 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3113 msg->wParam, msg->lParam );
3115 if (msg->message == WM_PAINT)
3117 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3118 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3119 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3120 DeleteObject( hrgn );
3122 return retval;
3126 /***********************************************************************
3127 * GetMessagePos (USER.119)
3128 * GetMessagePos (USER32.@)
3130 * The GetMessagePos() function returns a long value representing a
3131 * cursor position, in screen coordinates, when the last message
3132 * retrieved by the GetMessage() function occurs. The x-coordinate is
3133 * in the low-order word of the return value, the y-coordinate is in
3134 * the high-order word. The application can use the MAKEPOINT()
3135 * macro to obtain a POINT structure from the return value.
3137 * For the current cursor position, use GetCursorPos().
3139 * RETURNS
3141 * Cursor position of last message on success, zero on failure.
3143 * CONFORMANCE
3145 * ECMA-234, Win32
3148 DWORD WINAPI GetMessagePos(void)
3150 return get_user_thread_info()->GetMessagePosVal;
3154 /***********************************************************************
3155 * GetMessageTime (USER.120)
3156 * GetMessageTime (USER32.@)
3158 * GetMessageTime() returns the message time for the last message
3159 * retrieved by the function. The time is measured in milliseconds with
3160 * the same offset as GetTickCount().
3162 * Since the tick count wraps, this is only useful for moderately short
3163 * relative time comparisons.
3165 * RETURNS
3167 * Time of last message on success, zero on failure.
3169 LONG WINAPI GetMessageTime(void)
3171 return get_user_thread_info()->GetMessageTimeVal;
3175 /***********************************************************************
3176 * GetMessageExtraInfo (USER.288)
3177 * GetMessageExtraInfo (USER32.@)
3179 LPARAM WINAPI GetMessageExtraInfo(void)
3181 return get_user_thread_info()->GetMessageExtraInfoVal;
3185 /***********************************************************************
3186 * SetMessageExtraInfo (USER32.@)
3188 LPARAM WINAPI SetMessageExtraInfo(LPARAM lParam)
3190 struct user_thread_info *thread_info = get_user_thread_info();
3191 LONG old_value = thread_info->GetMessageExtraInfoVal;
3192 thread_info->GetMessageExtraInfoVal = lParam;
3193 return old_value;
3197 /***********************************************************************
3198 * WaitMessage (USER.112) Suspend thread pending messages
3199 * WaitMessage (USER32.@) Suspend thread pending messages
3201 * WaitMessage() suspends a thread until events appear in the thread's
3202 * queue.
3204 BOOL WINAPI WaitMessage(void)
3206 return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
3210 /***********************************************************************
3211 * MsgWaitForMultipleObjectsEx (USER32.@)
3213 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
3214 DWORD timeout, DWORD mask, DWORD flags )
3216 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
3217 DWORD i, ret, lock;
3219 if (count > MAXIMUM_WAIT_OBJECTS-1)
3221 SetLastError( ERROR_INVALID_PARAMETER );
3222 return WAIT_FAILED;
3225 /* set the queue mask */
3226 SERVER_START_REQ( set_queue_mask )
3228 req->wake_mask = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
3229 req->changed_mask = mask;
3230 req->skip_wait = 0;
3231 wine_server_call( req );
3233 SERVER_END_REQ;
3235 /* add the queue to the handle list */
3236 for (i = 0; i < count; i++) handles[i] = pHandles[i];
3237 handles[count] = get_server_queue_handle();
3239 ReleaseThunkLock( &lock );
3240 ret = USER_Driver->pMsgWaitForMultipleObjectsEx( count+1, handles, timeout, mask, flags );
3241 if (lock) RestoreThunkLock( lock );
3242 return ret;
3246 /***********************************************************************
3247 * MsgWaitForMultipleObjects (USER32.@)
3249 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
3250 BOOL wait_all, DWORD timeout, DWORD mask )
3252 return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
3253 wait_all ? MWMO_WAITALL : 0 );
3257 /***********************************************************************
3258 * WaitForInputIdle (USER32.@)
3260 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
3262 DWORD start_time, elapsed, ret;
3263 HANDLE handles[2];
3265 handles[0] = hProcess;
3266 SERVER_START_REQ( get_process_idle_event )
3268 req->handle = hProcess;
3269 if (!(ret = wine_server_call_err( req ))) handles[1] = reply->event;
3271 SERVER_END_REQ;
3272 if (ret) return WAIT_FAILED; /* error */
3273 if (!handles[1]) return 0; /* no event to wait on */
3275 start_time = GetTickCount();
3276 elapsed = 0;
3278 TRACE("waiting for %p\n", handles[1] );
3281 ret = MsgWaitForMultipleObjects ( 2, handles, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
3282 switch (ret)
3284 case WAIT_OBJECT_0:
3285 return WAIT_FAILED;
3286 case WAIT_OBJECT_0+2:
3287 process_sent_messages();
3288 break;
3289 case WAIT_TIMEOUT:
3290 case WAIT_FAILED:
3291 TRACE("timeout or error\n");
3292 return ret;
3293 default:
3294 TRACE("finished\n");
3295 return 0;
3297 if (dwTimeOut != INFINITE)
3299 elapsed = GetTickCount() - start_time;
3300 if (elapsed > dwTimeOut)
3301 break;
3304 while (1);
3306 return WAIT_TIMEOUT;
3310 /***********************************************************************
3311 * UserYield (USER.332)
3313 void WINAPI UserYield16(void)
3315 DWORD count;
3317 /* Handle sent messages */
3318 process_sent_messages();
3320 /* Yield */
3321 ReleaseThunkLock(&count);
3323 if (count)
3325 RestoreThunkLock(count);
3326 /* Handle sent messages again */
3327 process_sent_messages();
3332 /***********************************************************************
3333 * RegisterWindowMessageA (USER32.@)
3334 * RegisterWindowMessage (USER.118)
3336 UINT WINAPI RegisterWindowMessageA( LPCSTR str )
3338 UINT ret = GlobalAddAtomA(str);
3339 TRACE("%s, ret=%x\n", str, ret);
3340 return ret;
3344 /***********************************************************************
3345 * RegisterWindowMessageW (USER32.@)
3347 UINT WINAPI RegisterWindowMessageW( LPCWSTR str )
3349 UINT ret = GlobalAddAtomW(str);
3350 TRACE("%s ret=%x\n", debugstr_w(str), ret);
3351 return ret;
3354 typedef struct BroadcastParm
3356 DWORD flags;
3357 LPDWORD recipients;
3358 UINT msg;
3359 WPARAM wp;
3360 LPARAM lp;
3361 DWORD success;
3362 HWINSTA winsta;
3363 } BroadcastParm;
3365 static BOOL CALLBACK bcast_childwindow( HWND hw, LPARAM lp )
3367 BroadcastParm *parm = (BroadcastParm*)lp;
3368 DWORD_PTR retval = 0;
3369 LRESULT lresult;
3371 if (parm->flags & BSF_IGNORECURRENTTASK && WIN_IsCurrentProcess(hw))
3373 TRACE("Not telling myself %p\n", hw);
3374 return TRUE;
3377 /* I don't know 100% for sure if this is what Windows does, but it fits the tests */
3378 if (parm->flags & BSF_QUERY)
3380 TRACE("Telling window %p using SendMessageTimeout\n", hw);
3382 /* Not tested for conflicting flags */
3383 if (parm->flags & BSF_FORCEIFHUNG || parm->flags & BSF_NOHANG)
3384 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_ABORTIFHUNG, 2000, &retval );
3385 else if (parm->flags & BSF_NOTIMEOUTIFNOTHUNG)
3386 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NOTIMEOUTIFNOTHUNG, 2000, &retval );
3387 else
3388 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NORMAL, 2000, &retval );
3390 if (!lresult && GetLastError() == ERROR_TIMEOUT)
3392 WARN("Timed out!\n");
3393 if (!(parm->flags & BSF_FORCEIFHUNG))
3394 goto fail;
3396 if (retval == BROADCAST_QUERY_DENY)
3397 goto fail;
3399 return TRUE;
3401 fail:
3402 parm->success = 0;
3403 return FALSE;
3405 else if (parm->flags & BSF_POSTMESSAGE)
3407 TRACE("Telling window %p using PostMessage\n", hw);
3408 PostMessageW( hw, parm->msg, parm->wp, parm->lp );
3410 else
3412 TRACE("Telling window %p using SendNotifyMessage\n", hw);
3413 SendNotifyMessageW( hw, parm->msg, parm->wp, parm->lp );
3416 return TRUE;
3419 static BOOL CALLBACK bcast_desktop( LPWSTR desktop, LPARAM lp )
3421 BOOL ret;
3422 HDESK hdesktop;
3423 BroadcastParm *parm = (BroadcastParm*)lp;
3425 TRACE("desktop: %s\n", debugstr_w( desktop ));
3427 hdesktop = open_winstation_desktop( parm->winsta, desktop, 0, FALSE, DESKTOP_ENUMERATE|DESKTOP_WRITEOBJECTS|STANDARD_RIGHTS_WRITE );
3428 if (!hdesktop)
3430 FIXME("Could not open desktop %s\n", debugstr_w(desktop));
3431 return TRUE;
3434 ret = EnumDesktopWindows( hdesktop, bcast_childwindow, lp );
3435 CloseDesktop(hdesktop);
3436 TRACE("-->%d\n", ret);
3437 return parm->success;
3440 static BOOL CALLBACK bcast_winsta( LPWSTR winsta, LPARAM lp )
3442 BOOL ret;
3443 HWINSTA hwinsta = OpenWindowStationW( winsta, FALSE, WINSTA_ENUMDESKTOPS );
3444 TRACE("hwinsta: %p/%s/%08x\n", hwinsta, debugstr_w( winsta ), GetLastError());
3445 if (!hwinsta)
3446 return TRUE;
3447 ((BroadcastParm *)lp)->winsta = hwinsta;
3448 ret = EnumDesktopsW( hwinsta, bcast_desktop, lp );
3449 CloseWindowStation( hwinsta );
3450 TRACE("-->%d\n", ret);
3451 return ret;
3454 /***********************************************************************
3455 * BroadcastSystemMessageA (USER32.@)
3456 * BroadcastSystemMessage (USER32.@)
3458 LONG WINAPI BroadcastSystemMessageA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
3460 return BroadcastSystemMessageExA( flags, recipients, msg, wp, lp, NULL );
3464 /***********************************************************************
3465 * BroadcastSystemMessageW (USER32.@)
3467 LONG WINAPI BroadcastSystemMessageW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
3469 return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, NULL );
3472 /***********************************************************************
3473 * BroadcastSystemMessageExA (USER32.@)
3475 LONG WINAPI BroadcastSystemMessageExA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
3477 map_wparam_AtoW( msg, &wp, WMCHAR_MAP_NOMAPPING );
3478 return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, NULL );
3482 /***********************************************************************
3483 * BroadcastSystemMessageExW (USER32.@)
3485 LONG WINAPI BroadcastSystemMessageExW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
3487 BroadcastParm parm;
3488 DWORD recips = BSM_ALLCOMPONENTS;
3489 BOOL ret = TRUE;
3490 static const DWORD all_flags = ( BSF_QUERY | BSF_IGNORECURRENTTASK | BSF_FLUSHDISK | BSF_NOHANG
3491 | BSF_POSTMESSAGE | BSF_FORCEIFHUNG | BSF_NOTIMEOUTIFNOTHUNG
3492 | BSF_ALLOWSFW | BSF_SENDNOTIFYMESSAGE | BSF_RETURNHDESK | BSF_LUID );
3494 TRACE("Flags: %08x, recipients: %p(0x%x), msg: %04x, wparam: %08lx, lparam: %08lx\n", flags, recipients,
3495 (recipients ? *recipients : recips), msg, wp, lp);
3497 if (flags & ~all_flags)
3499 SetLastError(ERROR_INVALID_PARAMETER);
3500 return 0;
3503 if (!recipients)
3504 recipients = &recips;
3506 if ( pinfo && flags & BSF_QUERY )
3507 FIXME("Not returning PBSMINFO information yet\n");
3509 parm.flags = flags;
3510 parm.recipients = recipients;
3511 parm.msg = msg;
3512 parm.wp = wp;
3513 parm.lp = lp;
3514 parm.success = TRUE;
3516 if (*recipients & BSM_ALLDESKTOPS || *recipients == BSM_ALLCOMPONENTS)
3517 ret = EnumWindowStationsW(bcast_winsta, (LONG_PTR)&parm);
3518 else if (*recipients & BSM_APPLICATIONS)
3520 EnumWindows(bcast_childwindow, (LONG_PTR)&parm);
3521 ret = parm.success;
3523 else
3524 FIXME("Recipients %08x not supported!\n", *recipients);
3526 return ret;
3529 /***********************************************************************
3530 * SetMessageQueue (USER32.@)
3532 BOOL WINAPI SetMessageQueue( INT size )
3534 /* now obsolete the message queue will be expanded dynamically as necessary */
3535 return TRUE;
3539 /***********************************************************************
3540 * MessageBeep (USER32.@)
3542 BOOL WINAPI MessageBeep( UINT i )
3544 BOOL active = TRUE;
3545 SystemParametersInfoA( SPI_GETBEEP, 0, &active, FALSE );
3546 if (active) USER_Driver->pBeep();
3547 return TRUE;
3551 /***********************************************************************
3552 * SetTimer (USER32.@)
3554 UINT_PTR WINAPI SetTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3556 UINT_PTR ret;
3557 WNDPROC winproc = 0;
3559 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, NULL );
3561 SERVER_START_REQ( set_win_timer )
3563 req->win = hwnd;
3564 req->msg = WM_TIMER;
3565 req->id = id;
3566 req->rate = max( timeout, SYS_TIMER_RATE );
3567 req->lparam = (unsigned long)winproc;
3568 if (!wine_server_call_err( req ))
3570 ret = reply->id;
3571 if (!ret) ret = TRUE;
3573 else ret = 0;
3575 SERVER_END_REQ;
3577 TRACE("Added %p %lx %p timeout %d\n", hwnd, id, winproc, timeout );
3578 return ret;
3582 /***********************************************************************
3583 * SetSystemTimer (USER32.@)
3585 UINT_PTR WINAPI SetSystemTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3587 UINT_PTR ret;
3588 WNDPROC winproc = 0;
3590 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, NULL );
3592 SERVER_START_REQ( set_win_timer )
3594 req->win = hwnd;
3595 req->msg = WM_SYSTIMER;
3596 req->id = id;
3597 req->rate = max( timeout, SYS_TIMER_RATE );
3598 req->lparam = (unsigned long)winproc;
3599 if (!wine_server_call_err( req ))
3601 ret = reply->id;
3602 if (!ret) ret = TRUE;
3604 else ret = 0;
3606 SERVER_END_REQ;
3608 TRACE("Added %p %lx %p timeout %d\n", hwnd, id, winproc, timeout );
3609 return ret;
3613 /***********************************************************************
3614 * KillTimer (USER32.@)
3616 BOOL WINAPI KillTimer( HWND hwnd, UINT_PTR id )
3618 BOOL ret;
3620 SERVER_START_REQ( kill_win_timer )
3622 req->win = hwnd;
3623 req->msg = WM_TIMER;
3624 req->id = id;
3625 ret = !wine_server_call_err( req );
3627 SERVER_END_REQ;
3628 return ret;
3632 /***********************************************************************
3633 * KillSystemTimer (USER32.@)
3635 BOOL WINAPI KillSystemTimer( HWND hwnd, UINT_PTR id )
3637 BOOL ret;
3639 SERVER_START_REQ( kill_win_timer )
3641 req->win = hwnd;
3642 req->msg = WM_SYSTIMER;
3643 req->id = id;
3644 ret = !wine_server_call_err( req );
3646 SERVER_END_REQ;
3647 return ret;
3651 /**********************************************************************
3652 * GetGUIThreadInfo (USER32.@)
3654 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
3656 BOOL ret;
3658 SERVER_START_REQ( get_thread_input )
3660 req->tid = id;
3661 if ((ret = !wine_server_call_err( req )))
3663 info->flags = 0;
3664 info->hwndActive = reply->active;
3665 info->hwndFocus = reply->focus;
3666 info->hwndCapture = reply->capture;
3667 info->hwndMenuOwner = reply->menu_owner;
3668 info->hwndMoveSize = reply->move_size;
3669 info->hwndCaret = reply->caret;
3670 info->rcCaret.left = reply->rect.left;
3671 info->rcCaret.top = reply->rect.top;
3672 info->rcCaret.right = reply->rect.right;
3673 info->rcCaret.bottom = reply->rect.bottom;
3674 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
3675 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
3676 if (reply->caret) info->flags |= GUI_CARETBLINKING;
3679 SERVER_END_REQ;
3680 return ret;
3684 /******************************************************************
3685 * IsHungAppWindow (USER32.@)
3688 BOOL WINAPI IsHungAppWindow( HWND hWnd )
3690 BOOL ret;
3692 SERVER_START_REQ( is_window_hung )
3694 req->win = hWnd;
3695 ret = !wine_server_call_err( req ) && reply->is_hung;
3697 SERVER_END_REQ;
3698 return ret;