user: Allow some Wine internal messages to act on the desktop window.
[wine/wine64.git] / dlls / user / message.c
blob8dc0b3c0aaa7b5016c58f73ffd2c989cf22bcdff
1 /*
2 * Window messaging support
4 * Copyright 2001 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "winerror.h"
34 #include "winnls.h"
35 #include "dde.h"
36 #include "wine/unicode.h"
37 #include "wine/server.h"
38 #include "user_private.h"
39 #include "win.h"
40 #include "winpos.h"
41 #include "controls.h"
42 #include "winproc.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(msg);
46 WINE_DECLARE_DEBUG_CHANNEL(relay);
47 WINE_DECLARE_DEBUG_CHANNEL(key);
49 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
50 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
52 #define MAX_PACK_COUNT 4
53 #define MAX_SENDMSG_RECURSION 64
55 #define SYS_TIMER_RATE 55 /* min. timer rate in ms (actually 54.925)*/
57 /* description of the data fields that need to be packed along with a sent message */
58 struct packed_message
60 int count;
61 const void *data[MAX_PACK_COUNT];
62 size_t size[MAX_PACK_COUNT];
65 /* info about the message currently being received by the current thread */
66 struct received_message_info
68 enum message_type type;
69 MSG msg;
70 UINT flags; /* InSendMessageEx return flags */
71 HWINEVENTHOOK hook; /* winevent hook handle */
72 WINEVENTPROC hook_proc; /* winevent hook proc address */
75 /* structure to group all parameters for sent messages of the various kinds */
76 struct send_message_info
78 enum message_type type;
79 HWND hwnd;
80 UINT msg;
81 WPARAM wparam;
82 LPARAM lparam;
83 UINT flags; /* flags for SendMessageTimeout */
84 UINT timeout; /* timeout for SendMessageTimeout */
85 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
86 ULONG_PTR data; /* callback data */
90 /* flag for messages that contain pointers */
91 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
93 #define SET(msg) (1 << ((msg) & 31))
95 static const unsigned int message_pointer_flags[] =
97 /* 0x00 - 0x1f */
98 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
99 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
100 /* 0x20 - 0x3f */
101 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
102 SET(WM_COMPAREITEM),
103 /* 0x40 - 0x5f */
104 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
105 SET(WM_NOTIFY) | SET(WM_HELP),
106 /* 0x60 - 0x7f */
107 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
108 /* 0x80 - 0x9f */
109 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
110 /* 0xa0 - 0xbf */
111 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
112 /* 0xc0 - 0xdf */
113 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
114 /* 0xe0 - 0xff */
115 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO) | SET(SBM_GETSCROLLBARINFO),
116 /* 0x100 - 0x11f */
118 /* 0x120 - 0x13f */
120 /* 0x140 - 0x15f */
121 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
122 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
123 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
124 /* 0x160 - 0x17f */
126 /* 0x180 - 0x19f */
127 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
128 SET(LB_DIR) | SET(LB_FINDSTRING) |
129 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
130 /* 0x1a0 - 0x1bf */
131 SET(LB_FINDSTRINGEXACT),
132 /* 0x1c0 - 0x1df */
134 /* 0x1e0 - 0x1ff */
136 /* 0x200 - 0x21f */
137 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
138 /* 0x220 - 0x23f */
139 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
140 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
141 /* 0x240 - 0x25f */
143 /* 0x260 - 0x27f */
145 /* 0x280 - 0x29f */
147 /* 0x2a0 - 0x2bf */
149 /* 0x2c0 - 0x2df */
151 /* 0x2e0 - 0x2ff */
153 /* 0x300 - 0x31f */
154 SET(WM_ASKCBFORMATNAME)
157 /* flags for messages that contain Unicode strings */
158 static const unsigned int message_unicode_flags[] =
160 /* 0x00 - 0x1f */
161 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
162 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
163 /* 0x20 - 0x3f */
164 SET(WM_CHARTOITEM),
165 /* 0x40 - 0x5f */
167 /* 0x60 - 0x7f */
169 /* 0x80 - 0x9f */
170 SET(WM_NCCREATE),
171 /* 0xa0 - 0xbf */
173 /* 0xc0 - 0xdf */
174 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
175 /* 0xe0 - 0xff */
177 /* 0x100 - 0x11f */
178 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
179 /* 0x120 - 0x13f */
180 SET(WM_MENUCHAR),
181 /* 0x140 - 0x15f */
182 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
183 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
184 /* 0x160 - 0x17f */
186 /* 0x180 - 0x19f */
187 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
188 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
189 /* 0x1a0 - 0x1bf */
190 SET(LB_FINDSTRINGEXACT),
191 /* 0x1c0 - 0x1df */
193 /* 0x1e0 - 0x1ff */
195 /* 0x200 - 0x21f */
197 /* 0x220 - 0x23f */
198 SET(WM_MDICREATE),
199 /* 0x240 - 0x25f */
201 /* 0x260 - 0x27f */
203 /* 0x280 - 0x29f */
204 SET(WM_IME_CHAR),
205 /* 0x2a0 - 0x2bf */
207 /* 0x2c0 - 0x2df */
209 /* 0x2e0 - 0x2ff */
211 /* 0x300 - 0x31f */
212 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
215 /* check whether a given message type includes pointers */
216 inline static int is_pointer_message( UINT message )
218 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
219 return (message_pointer_flags[message / 32] & SET(message)) != 0;
222 /* check whether a given message type contains Unicode (or ASCII) chars */
223 inline static int is_unicode_message( UINT message )
225 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
226 return (message_unicode_flags[message / 32] & SET(message)) != 0;
229 #undef SET
231 /* add a data field to a packed message */
232 inline static void push_data( struct packed_message *data, const void *ptr, size_t size )
234 data->data[data->count] = ptr;
235 data->size[data->count] = size;
236 data->count++;
239 /* add a string to a packed message */
240 inline static void push_string( struct packed_message *data, LPCWSTR str )
242 push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
245 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
246 inline static void *get_data( void **buffer, size_t size )
248 void *ret = *buffer;
249 *buffer = (char *)*buffer + size;
250 return ret;
253 /* make sure that the buffer contains a valid null-terminated Unicode string */
254 inline static BOOL check_string( LPCWSTR str, size_t size )
256 for (size /= sizeof(WCHAR); size; size--, str++)
257 if (!*str) return TRUE;
258 return FALSE;
261 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
262 inline static void *get_buffer_space( void **buffer, size_t size )
264 void *ret;
266 if (*buffer)
268 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
269 HeapFree( GetProcessHeap(), 0, *buffer );
271 else ret = HeapAlloc( GetProcessHeap(), 0, size );
273 *buffer = ret;
274 return ret;
277 /* retrieve a string pointer from packed data */
278 inline static LPWSTR get_string( void **buffer )
280 return get_data( buffer, (strlenW( (LPWSTR)*buffer ) + 1) * sizeof(WCHAR) );
283 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
284 inline static BOOL combobox_has_strings( HWND hwnd )
286 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
287 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
290 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
291 inline static BOOL listbox_has_strings( HWND hwnd )
293 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
294 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
297 /* check whether message is in the range of keyboard messages */
298 inline static BOOL is_keyboard_message( UINT message )
300 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
303 /* check whether message is in the range of mouse messages */
304 inline static BOOL is_mouse_message( UINT message )
306 return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
307 (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
310 /* check whether message matches the specified hwnd filter */
311 inline static BOOL check_hwnd_filter( const MSG *msg, HWND hwnd_filter )
313 if (!hwnd_filter) return TRUE;
314 return (msg->hwnd == hwnd_filter || IsChild( hwnd_filter, msg->hwnd ));
318 /***********************************************************************
319 * broadcast_message_callback
321 * Helper callback for broadcasting messages.
323 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
325 struct send_message_info *info = (struct send_message_info *)lparam;
326 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
327 switch(info->type)
329 case MSG_UNICODE:
330 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
331 info->flags, info->timeout, NULL );
332 break;
333 case MSG_ASCII:
334 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
335 info->flags, info->timeout, NULL );
336 break;
337 case MSG_NOTIFY:
338 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
339 break;
340 case MSG_CALLBACK:
341 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
342 info->callback, info->data );
343 break;
344 case MSG_POSTED:
345 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
346 break;
347 default:
348 ERR( "bad type %d\n", info->type );
349 break;
351 return TRUE;
355 /***********************************************************************
356 * map_wparam_AtoW
358 * Convert the wparam of an ASCII message to Unicode.
360 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
362 switch(message)
364 case WM_CHARTOITEM:
365 case EM_SETPASSWORDCHAR:
366 case WM_CHAR:
367 case WM_DEADCHAR:
368 case WM_SYSCHAR:
369 case WM_SYSDEADCHAR:
370 case WM_MENUCHAR:
372 char ch[2];
373 WCHAR wch[2];
374 ch[0] = (wparam & 0xff);
375 ch[1] = (wparam >> 8);
376 MultiByteToWideChar(CP_ACP, 0, ch, 2, wch, 2);
377 wparam = MAKEWPARAM(wch[0], wch[1]);
379 break;
380 case WM_IME_CHAR:
382 char ch[2];
383 WCHAR wch;
384 ch[0] = (wparam >> 8);
385 ch[1] = (wparam & 0xff);
386 if (ch[0]) MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
387 else MultiByteToWideChar(CP_ACP, 0, &ch[1], 1, &wch, 1);
388 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
390 break;
392 return wparam;
396 /***********************************************************************
397 * map_wparam_WtoA
399 * Convert the wparam of a Unicode message to ASCII.
401 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
403 switch(message)
405 case WM_CHARTOITEM:
406 case EM_SETPASSWORDCHAR:
407 case WM_CHAR:
408 case WM_DEADCHAR:
409 case WM_SYSCHAR:
410 case WM_SYSDEADCHAR:
411 case WM_MENUCHAR:
413 WCHAR wch[2];
414 BYTE ch[2];
415 wch[0] = LOWORD(wparam);
416 wch[1] = HIWORD(wparam);
417 WideCharToMultiByte( CP_ACP, 0, wch, 2, (LPSTR)ch, 2, NULL, NULL );
418 wparam = MAKEWPARAM( ch[0] | (ch[1] << 8), 0 );
420 break;
421 case WM_IME_CHAR:
423 WCHAR wch = LOWORD(wparam);
424 BYTE ch[2];
426 if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)ch, 2, NULL, NULL ) == 2)
427 wparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wparam) );
428 else
429 wparam = MAKEWPARAM( ch[0], HIWORD(wparam) );
431 break;
433 return wparam;
437 /***********************************************************************
438 * pack_message
440 * Pack a message for sending to another process.
441 * Return the size of the data we expect in the message reply.
442 * Set data->count to -1 if there is an error.
444 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
445 struct packed_message *data )
447 data->count = 0;
448 switch(message)
450 case WM_NCCREATE:
451 case WM_CREATE:
453 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
454 push_data( data, cs, sizeof(*cs) );
455 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
456 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
457 return sizeof(*cs);
459 case WM_GETTEXT:
460 case WM_ASKCBFORMATNAME:
461 return wparam * sizeof(WCHAR);
462 case WM_WININICHANGE:
463 if (lparam) push_string(data, (LPWSTR)lparam );
464 return 0;
465 case WM_SETTEXT:
466 case WM_DEVMODECHANGE:
467 case CB_DIR:
468 case LB_DIR:
469 case LB_ADDFILE:
470 case EM_REPLACESEL:
471 push_string( data, (LPWSTR)lparam );
472 return 0;
473 case WM_GETMINMAXINFO:
474 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
475 return sizeof(MINMAXINFO);
476 case WM_DRAWITEM:
477 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
478 return 0;
479 case WM_MEASUREITEM:
480 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
481 return sizeof(MEASUREITEMSTRUCT);
482 case WM_DELETEITEM:
483 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
484 return 0;
485 case WM_COMPAREITEM:
486 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
487 return 0;
488 case WM_WINDOWPOSCHANGING:
489 case WM_WINDOWPOSCHANGED:
490 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
491 return sizeof(WINDOWPOS);
492 case WM_COPYDATA:
494 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
495 push_data( data, cp, sizeof(*cp) );
496 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
497 return 0;
499 case WM_NOTIFY:
500 /* WM_NOTIFY cannot be sent across processes (MSDN) */
501 data->count = -1;
502 return 0;
503 case WM_HELP:
504 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
505 return 0;
506 case WM_STYLECHANGING:
507 case WM_STYLECHANGED:
508 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
509 return 0;
510 case WM_NCCALCSIZE:
511 if (!wparam)
513 push_data( data, (RECT *)lparam, sizeof(RECT) );
514 return sizeof(RECT);
516 else
518 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
519 push_data( data, nc, sizeof(*nc) );
520 push_data( data, nc->lppos, sizeof(*nc->lppos) );
521 return sizeof(*nc) + sizeof(*nc->lppos);
523 case WM_GETDLGCODE:
524 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
525 return sizeof(MSG);
526 case SBM_SETSCROLLINFO:
527 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
528 return 0;
529 case SBM_GETSCROLLINFO:
530 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
531 return sizeof(SCROLLINFO);
532 case SBM_GETSCROLLBARINFO:
534 const SCROLLBARINFO *info = (const SCROLLBARINFO *)lparam;
535 size_t size = min( info->cbSize, sizeof(SCROLLBARINFO) );
536 push_data( data, info, size );
537 return size;
539 case EM_GETSEL:
540 case SBM_GETRANGE:
541 case CB_GETEDITSEL:
543 size_t size = 0;
544 if (wparam) size += sizeof(DWORD);
545 if (lparam) size += sizeof(DWORD);
546 return size;
548 case EM_GETRECT:
549 case LB_GETITEMRECT:
550 case CB_GETDROPPEDCONTROLRECT:
551 return sizeof(RECT);
552 case EM_SETRECT:
553 case EM_SETRECTNP:
554 push_data( data, (RECT *)lparam, sizeof(RECT) );
555 return 0;
556 case EM_GETLINE:
558 WORD *pw = (WORD *)lparam;
559 push_data( data, pw, sizeof(*pw) );
560 return *pw * sizeof(WCHAR);
562 case EM_SETTABSTOPS:
563 case LB_SETTABSTOPS:
564 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
565 return 0;
566 case CB_ADDSTRING:
567 case CB_INSERTSTRING:
568 case CB_FINDSTRING:
569 case CB_FINDSTRINGEXACT:
570 case CB_SELECTSTRING:
571 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
572 return 0;
573 case CB_GETLBTEXT:
574 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
575 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
576 case LB_ADDSTRING:
577 case LB_INSERTSTRING:
578 case LB_FINDSTRING:
579 case LB_FINDSTRINGEXACT:
580 case LB_SELECTSTRING:
581 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
582 return 0;
583 case LB_GETTEXT:
584 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
585 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
586 case LB_GETSELITEMS:
587 return wparam * sizeof(UINT);
588 case WM_NEXTMENU:
589 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
590 return sizeof(MDINEXTMENU);
591 case WM_SIZING:
592 case WM_MOVING:
593 push_data( data, (RECT *)lparam, sizeof(RECT) );
594 return sizeof(RECT);
595 case WM_MDICREATE:
597 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
598 push_data( data, cs, sizeof(*cs) );
599 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
600 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
601 return sizeof(*cs);
603 case WM_MDIGETACTIVE:
604 if (lparam) return sizeof(BOOL);
605 return 0;
606 case WM_WINE_SETWINDOWPOS:
607 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
608 return 0;
609 case WM_WINE_KEYBOARD_LL_HOOK:
610 push_data( data, (KBDLLHOOKSTRUCT *)lparam, sizeof(KBDLLHOOKSTRUCT) );
611 return 0;
612 case WM_WINE_MOUSE_LL_HOOK:
613 push_data( data, (MSLLHOOKSTRUCT *)lparam, sizeof(MSLLHOOKSTRUCT) );
614 return 0;
615 case WM_PAINT:
616 if (!wparam) return 0;
617 /* fall through */
619 /* these contain an HFONT */
620 case WM_SETFONT:
621 case WM_GETFONT:
622 /* these contain an HDC */
623 case WM_ERASEBKGND:
624 case WM_ICONERASEBKGND:
625 case WM_NCPAINT:
626 case WM_CTLCOLORMSGBOX:
627 case WM_CTLCOLOREDIT:
628 case WM_CTLCOLORLISTBOX:
629 case WM_CTLCOLORBTN:
630 case WM_CTLCOLORDLG:
631 case WM_CTLCOLORSCROLLBAR:
632 case WM_CTLCOLORSTATIC:
633 case WM_PRINT:
634 case WM_PRINTCLIENT:
635 /* these contain an HGLOBAL */
636 case WM_PAINTCLIPBOARD:
637 case WM_SIZECLIPBOARD:
638 /* these contain HICON */
639 case WM_GETICON:
640 case WM_SETICON:
641 case WM_QUERYDRAGICON:
642 case WM_QUERYPARKICON:
643 /* these contain pointers */
644 case WM_DROPOBJECT:
645 case WM_QUERYDROPOBJECT:
646 case WM_DRAGLOOP:
647 case WM_DRAGSELECT:
648 case WM_DRAGMOVE:
649 case WM_DEVICECHANGE:
650 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
651 data->count = -1;
652 return 0;
654 return 0;
658 /***********************************************************************
659 * unpack_message
661 * Unpack a message received from another process.
663 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
664 void **buffer, size_t size )
666 size_t minsize = 0;
668 switch(message)
670 case WM_NCCREATE:
671 case WM_CREATE:
673 CREATESTRUCTW *cs = *buffer;
674 WCHAR *str = (WCHAR *)(cs + 1);
675 if (size < sizeof(*cs)) return FALSE;
676 size -= sizeof(*cs);
677 if (HIWORD(cs->lpszName))
679 if (!check_string( str, size )) return FALSE;
680 cs->lpszName = str;
681 size -= (strlenW(str) + 1) * sizeof(WCHAR);
682 str += strlenW(str) + 1;
684 if (HIWORD(cs->lpszClass))
686 if (!check_string( str, size )) return FALSE;
687 cs->lpszClass = str;
689 break;
691 case WM_GETTEXT:
692 case WM_ASKCBFORMATNAME:
693 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
694 break;
695 case WM_WININICHANGE:
696 if (!*lparam) return TRUE;
697 /* fall through */
698 case WM_SETTEXT:
699 case WM_DEVMODECHANGE:
700 case CB_DIR:
701 case LB_DIR:
702 case LB_ADDFILE:
703 case EM_REPLACESEL:
704 if (!check_string( *buffer, size )) return FALSE;
705 break;
706 case WM_GETMINMAXINFO:
707 minsize = sizeof(MINMAXINFO);
708 break;
709 case WM_DRAWITEM:
710 minsize = sizeof(DRAWITEMSTRUCT);
711 break;
712 case WM_MEASUREITEM:
713 minsize = sizeof(MEASUREITEMSTRUCT);
714 break;
715 case WM_DELETEITEM:
716 minsize = sizeof(DELETEITEMSTRUCT);
717 break;
718 case WM_COMPAREITEM:
719 minsize = sizeof(COMPAREITEMSTRUCT);
720 break;
721 case WM_WINDOWPOSCHANGING:
722 case WM_WINDOWPOSCHANGED:
723 case WM_WINE_SETWINDOWPOS:
724 minsize = sizeof(WINDOWPOS);
725 break;
726 case WM_COPYDATA:
728 COPYDATASTRUCT *cp = *buffer;
729 if (size < sizeof(*cp)) return FALSE;
730 if (cp->lpData)
732 minsize = sizeof(*cp) + cp->cbData;
733 cp->lpData = cp + 1;
735 break;
737 case WM_NOTIFY:
738 /* WM_NOTIFY cannot be sent across processes (MSDN) */
739 return FALSE;
740 case WM_HELP:
741 minsize = sizeof(HELPINFO);
742 break;
743 case WM_STYLECHANGING:
744 case WM_STYLECHANGED:
745 minsize = sizeof(STYLESTRUCT);
746 break;
747 case WM_NCCALCSIZE:
748 if (!*wparam) minsize = sizeof(RECT);
749 else
751 NCCALCSIZE_PARAMS *nc = *buffer;
752 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
753 nc->lppos = (WINDOWPOS *)(nc + 1);
755 break;
756 case WM_GETDLGCODE:
757 if (!*lparam) return TRUE;
758 minsize = sizeof(MSG);
759 break;
760 case SBM_SETSCROLLINFO:
761 minsize = sizeof(SCROLLINFO);
762 break;
763 case SBM_GETSCROLLINFO:
764 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
765 break;
766 case SBM_GETSCROLLBARINFO:
767 if (!get_buffer_space( buffer, sizeof(SCROLLBARINFO ))) return FALSE;
768 break;
769 case EM_GETSEL:
770 case SBM_GETRANGE:
771 case CB_GETEDITSEL:
772 if (*wparam || *lparam)
774 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
775 if (*wparam) *wparam = (WPARAM)*buffer;
776 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
778 return TRUE;
779 case EM_GETRECT:
780 case LB_GETITEMRECT:
781 case CB_GETDROPPEDCONTROLRECT:
782 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
783 break;
784 case EM_SETRECT:
785 case EM_SETRECTNP:
786 minsize = sizeof(RECT);
787 break;
788 case EM_GETLINE:
790 WORD len;
791 if (size < sizeof(WORD)) return FALSE;
792 len = *(WORD *)*buffer;
793 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
794 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
795 return TRUE;
797 case EM_SETTABSTOPS:
798 case LB_SETTABSTOPS:
799 if (!*wparam) return TRUE;
800 minsize = *wparam * sizeof(UINT);
801 break;
802 case CB_ADDSTRING:
803 case CB_INSERTSTRING:
804 case CB_FINDSTRING:
805 case CB_FINDSTRINGEXACT:
806 case CB_SELECTSTRING:
807 case LB_ADDSTRING:
808 case LB_INSERTSTRING:
809 case LB_FINDSTRING:
810 case LB_FINDSTRINGEXACT:
811 case LB_SELECTSTRING:
812 if (!*buffer) return TRUE;
813 if (!check_string( *buffer, size )) return FALSE;
814 break;
815 case CB_GETLBTEXT:
817 size = sizeof(ULONG_PTR);
818 if (combobox_has_strings( hwnd ))
819 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
820 if (!get_buffer_space( buffer, size )) return FALSE;
821 break;
823 case LB_GETTEXT:
825 size = sizeof(ULONG_PTR);
826 if (listbox_has_strings( hwnd ))
827 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
828 if (!get_buffer_space( buffer, size )) return FALSE;
829 break;
831 case LB_GETSELITEMS:
832 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
833 break;
834 case WM_NEXTMENU:
835 minsize = sizeof(MDINEXTMENU);
836 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
837 break;
838 case WM_SIZING:
839 case WM_MOVING:
840 minsize = sizeof(RECT);
841 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
842 break;
843 case WM_MDICREATE:
845 MDICREATESTRUCTW *cs = *buffer;
846 WCHAR *str = (WCHAR *)(cs + 1);
847 if (size < sizeof(*cs)) return FALSE;
848 size -= sizeof(*cs);
849 if (HIWORD(cs->szTitle))
851 if (!check_string( str, size )) return FALSE;
852 cs->szTitle = str;
853 size -= (strlenW(str) + 1) * sizeof(WCHAR);
854 str += strlenW(str) + 1;
856 if (HIWORD(cs->szClass))
858 if (!check_string( str, size )) return FALSE;
859 cs->szClass = str;
861 break;
863 case WM_MDIGETACTIVE:
864 if (!*lparam) return TRUE;
865 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
866 break;
867 case WM_WINE_KEYBOARD_LL_HOOK:
868 minsize = sizeof(KBDLLHOOKSTRUCT);
869 break;
870 case WM_WINE_MOUSE_LL_HOOK:
871 minsize = sizeof(MSLLHOOKSTRUCT);
872 break;
873 case WM_PAINT:
874 if (!*wparam) return TRUE;
875 /* fall through */
877 /* these contain an HFONT */
878 case WM_SETFONT:
879 case WM_GETFONT:
880 /* these contain an HDC */
881 case WM_ERASEBKGND:
882 case WM_ICONERASEBKGND:
883 case WM_NCPAINT:
884 case WM_CTLCOLORMSGBOX:
885 case WM_CTLCOLOREDIT:
886 case WM_CTLCOLORLISTBOX:
887 case WM_CTLCOLORBTN:
888 case WM_CTLCOLORDLG:
889 case WM_CTLCOLORSCROLLBAR:
890 case WM_CTLCOLORSTATIC:
891 case WM_PRINT:
892 case WM_PRINTCLIENT:
893 /* these contain an HGLOBAL */
894 case WM_PAINTCLIPBOARD:
895 case WM_SIZECLIPBOARD:
896 /* these contain HICON */
897 case WM_GETICON:
898 case WM_SETICON:
899 case WM_QUERYDRAGICON:
900 case WM_QUERYPARKICON:
901 /* these contain pointers */
902 case WM_DROPOBJECT:
903 case WM_QUERYDROPOBJECT:
904 case WM_DRAGLOOP:
905 case WM_DRAGSELECT:
906 case WM_DRAGMOVE:
907 case WM_DEVICECHANGE:
908 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
909 return FALSE;
911 default:
912 return TRUE; /* message doesn't need any unpacking */
915 /* default exit for most messages: check minsize and store buffer in lparam */
916 if (size < minsize) return FALSE;
917 *lparam = (LPARAM)*buffer;
918 return TRUE;
922 /***********************************************************************
923 * pack_reply
925 * Pack a reply to a message for sending to another process.
927 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
928 LRESULT res, struct packed_message *data )
930 data->count = 0;
931 switch(message)
933 case WM_NCCREATE:
934 case WM_CREATE:
935 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
936 break;
937 case WM_GETTEXT:
938 case CB_GETLBTEXT:
939 case LB_GETTEXT:
940 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
941 break;
942 case WM_GETMINMAXINFO:
943 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
944 break;
945 case WM_MEASUREITEM:
946 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
947 break;
948 case WM_WINDOWPOSCHANGING:
949 case WM_WINDOWPOSCHANGED:
950 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
951 break;
952 case WM_GETDLGCODE:
953 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
954 break;
955 case SBM_GETSCROLLINFO:
956 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
957 break;
958 case EM_GETRECT:
959 case LB_GETITEMRECT:
960 case CB_GETDROPPEDCONTROLRECT:
961 case WM_SIZING:
962 case WM_MOVING:
963 push_data( data, (RECT *)lparam, sizeof(RECT) );
964 break;
965 case EM_GETLINE:
967 WORD *ptr = (WORD *)lparam;
968 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
969 break;
971 case LB_GETSELITEMS:
972 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
973 break;
974 case WM_MDIGETACTIVE:
975 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
976 break;
977 case WM_NCCALCSIZE:
978 if (!wparam)
979 push_data( data, (RECT *)lparam, sizeof(RECT) );
980 else
982 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
983 push_data( data, nc, sizeof(*nc) );
984 push_data( data, nc->lppos, sizeof(*nc->lppos) );
986 break;
987 case EM_GETSEL:
988 case SBM_GETRANGE:
989 case CB_GETEDITSEL:
990 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
991 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
992 break;
993 case WM_NEXTMENU:
994 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
995 break;
996 case WM_MDICREATE:
997 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
998 break;
999 case WM_ASKCBFORMATNAME:
1000 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
1001 break;
1006 /***********************************************************************
1007 * unpack_reply
1009 * Unpack a message reply received from another process.
1011 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1012 void *buffer, size_t size )
1014 switch(message)
1016 case WM_NCCREATE:
1017 case WM_CREATE:
1019 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
1020 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
1021 memcpy( cs, buffer, min( sizeof(*cs), size ));
1022 cs->lpszName = name; /* restore the original pointers */
1023 cs->lpszClass = class;
1024 break;
1026 case WM_GETTEXT:
1027 case WM_ASKCBFORMATNAME:
1028 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
1029 break;
1030 case WM_GETMINMAXINFO:
1031 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
1032 break;
1033 case WM_MEASUREITEM:
1034 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
1035 break;
1036 case WM_WINDOWPOSCHANGING:
1037 case WM_WINDOWPOSCHANGED:
1038 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
1039 break;
1040 case WM_GETDLGCODE:
1041 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
1042 break;
1043 case SBM_GETSCROLLINFO:
1044 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
1045 break;
1046 case SBM_GETSCROLLBARINFO:
1047 memcpy( (SCROLLBARINFO *)lparam, buffer, min( sizeof(SCROLLBARINFO), size ));
1048 break;
1049 case EM_GETRECT:
1050 case CB_GETDROPPEDCONTROLRECT:
1051 case LB_GETITEMRECT:
1052 case WM_SIZING:
1053 case WM_MOVING:
1054 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1055 break;
1056 case EM_GETLINE:
1057 size = min( size, (size_t)*(WORD *)lparam );
1058 memcpy( (WCHAR *)lparam, buffer, size );
1059 break;
1060 case LB_GETSELITEMS:
1061 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
1062 break;
1063 case LB_GETTEXT:
1064 case CB_GETLBTEXT:
1065 memcpy( (WCHAR *)lparam, buffer, size );
1066 break;
1067 case WM_NEXTMENU:
1068 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
1069 break;
1070 case WM_MDIGETACTIVE:
1071 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
1072 break;
1073 case WM_NCCALCSIZE:
1074 if (!wparam)
1075 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1076 else
1078 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1079 WINDOWPOS *wp = nc->lppos;
1080 memcpy( nc, buffer, min( sizeof(*nc), size ));
1081 if (size > sizeof(*nc))
1083 size -= sizeof(*nc);
1084 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
1086 nc->lppos = wp; /* restore the original pointer */
1088 break;
1089 case EM_GETSEL:
1090 case SBM_GETRANGE:
1091 case CB_GETEDITSEL:
1092 if (wparam)
1094 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1095 if (size <= sizeof(DWORD)) break;
1096 size -= sizeof(DWORD);
1097 buffer = (DWORD *)buffer + 1;
1099 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1100 break;
1101 case WM_MDICREATE:
1103 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
1104 LPCWSTR title = cs->szTitle, class = cs->szClass;
1105 memcpy( cs, buffer, min( sizeof(*cs), size ));
1106 cs->szTitle = title; /* restore the original pointers */
1107 cs->szClass = class;
1108 break;
1110 default:
1111 ERR( "should not happen: unexpected message %x\n", message );
1112 break;
1117 /***********************************************************************
1118 * reply_message
1120 * Send a reply to a sent message.
1122 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1124 struct packed_message data;
1125 int i, replied = info->flags & ISMEX_REPLIED;
1127 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1128 if (!remove && replied) return; /* replied already */
1130 data.count = 0;
1131 info->flags |= ISMEX_REPLIED;
1133 if (info->type == MSG_OTHER_PROCESS && !replied)
1135 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1136 info->msg.lParam, result, &data );
1139 SERVER_START_REQ( reply_message )
1141 req->result = result;
1142 req->remove = remove;
1143 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1144 wine_server_call( req );
1146 SERVER_END_REQ;
1150 /***********************************************************************
1151 * handle_internal_message
1153 * Handle an internal Wine message instead of calling the window proc.
1155 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1157 switch(msg)
1159 case WM_WINE_DESTROYWINDOW:
1160 return WIN_DestroyWindow( hwnd );
1161 case WM_WINE_SETWINDOWPOS:
1162 if (hwnd == GetDesktopWindow()) return 0;
1163 return USER_Driver->pSetWindowPos( (WINDOWPOS *)lparam );
1164 case WM_WINE_SHOWWINDOW:
1165 if (hwnd == GetDesktopWindow()) return 0;
1166 return ShowWindow( hwnd, wparam );
1167 case WM_WINE_SETPARENT:
1168 if (hwnd == GetDesktopWindow()) return 0;
1169 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1170 case WM_WINE_SETWINDOWLONG:
1171 return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1172 case WM_WINE_ENABLEWINDOW:
1173 if (hwnd == GetDesktopWindow()) return 0;
1174 return EnableWindow( hwnd, wparam );
1175 case WM_WINE_SETACTIVEWINDOW:
1176 if (hwnd == GetDesktopWindow()) return 0;
1177 return (LRESULT)SetActiveWindow( (HWND)wparam );
1178 case WM_WINE_KEYBOARD_LL_HOOK:
1179 return HOOK_CallHooks( WH_KEYBOARD_LL, HC_ACTION, wparam, lparam, TRUE );
1180 case WM_WINE_MOUSE_LL_HOOK:
1181 return HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, wparam, lparam, TRUE );
1182 default:
1183 if (msg >= WM_WINE_FIRST_DRIVER_MSG && msg <= WM_WINE_LAST_DRIVER_MSG)
1184 return USER_Driver->pWindowMessage( hwnd, msg, wparam, lparam );
1185 FIXME( "unknown internal message %x\n", msg );
1186 return 0;
1190 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1191 * to the memory handle, we keep track (in the server side) of all pairs of handle
1192 * used (the client passes its value and the content of the memory handle), and
1193 * the server stored both values (the client, and the local one, created after the
1194 * content). When a ACK message is generated, the list of pair is searched for a
1195 * matching pair, so that the client memory handle can be returned.
1197 struct DDE_pair {
1198 HGLOBAL client_hMem;
1199 HGLOBAL server_hMem;
1202 static struct DDE_pair* dde_pairs;
1203 static int dde_num_alloc;
1204 static int dde_num_used;
1206 static CRITICAL_SECTION dde_crst;
1207 static CRITICAL_SECTION_DEBUG critsect_debug =
1209 0, 0, &dde_crst,
1210 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
1211 0, 0, { (DWORD_PTR)(__FILE__ ": dde_crst") }
1213 static CRITICAL_SECTION dde_crst = { &critsect_debug, -1, 0, 0, 0, 0 };
1215 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1217 int i;
1218 #define GROWBY 4
1220 EnterCriticalSection(&dde_crst);
1222 /* now remember the pair of hMem on both sides */
1223 if (dde_num_used == dde_num_alloc)
1225 struct DDE_pair* tmp;
1226 if (dde_pairs)
1227 tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1228 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1229 else
1230 tmp = HeapAlloc( GetProcessHeap(), 0,
1231 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1233 if (!tmp)
1235 LeaveCriticalSection(&dde_crst);
1236 return FALSE;
1238 dde_pairs = tmp;
1239 /* zero out newly allocated part */
1240 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1241 dde_num_alloc += GROWBY;
1243 #undef GROWBY
1244 for (i = 0; i < dde_num_alloc; i++)
1246 if (dde_pairs[i].server_hMem == 0)
1248 dde_pairs[i].client_hMem = chm;
1249 dde_pairs[i].server_hMem = shm;
1250 dde_num_used++;
1251 break;
1254 LeaveCriticalSection(&dde_crst);
1255 return TRUE;
1258 static HGLOBAL dde_get_pair(HGLOBAL shm)
1260 int i;
1261 HGLOBAL ret = 0;
1263 EnterCriticalSection(&dde_crst);
1264 for (i = 0; i < dde_num_alloc; i++)
1266 if (dde_pairs[i].server_hMem == shm)
1268 /* free this pair */
1269 dde_pairs[i].server_hMem = 0;
1270 dde_num_used--;
1271 ret = dde_pairs[i].client_hMem;
1272 break;
1275 LeaveCriticalSection(&dde_crst);
1276 return ret;
1279 /***********************************************************************
1280 * post_dde_message
1282 * Post a DDE message
1284 static BOOL post_dde_message( DWORD dest_tid, struct packed_message *data, const struct send_message_info *info )
1286 void* ptr = NULL;
1287 int size = 0;
1288 UINT_PTR uiLo, uiHi;
1289 LPARAM lp = 0;
1290 HGLOBAL hunlock = 0;
1291 int i;
1292 DWORD res;
1294 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1295 return FALSE;
1297 lp = info->lparam;
1298 switch (info->msg)
1300 /* DDE messages which don't require packing are:
1301 * WM_DDE_INITIATE
1302 * WM_DDE_TERMINATE
1303 * WM_DDE_REQUEST
1304 * WM_DDE_UNADVISE
1306 case WM_DDE_ACK:
1307 if (HIWORD(uiHi))
1309 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1310 HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1311 if (h)
1313 /* send back the value of h on the other side */
1314 push_data( data, &h, sizeof(HGLOBAL) );
1315 lp = uiLo;
1316 TRACE( "send dde-ack %x %08x => %p\n", uiLo, uiHi, h );
1319 else
1321 /* uiHi should contain either an atom or 0 */
1322 TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
1323 lp = MAKELONG( uiLo, uiHi );
1325 break;
1326 case WM_DDE_ADVISE:
1327 case WM_DDE_DATA:
1328 case WM_DDE_POKE:
1329 size = 0;
1330 if (uiLo)
1332 size = GlobalSize( (HGLOBAL)uiLo ) ;
1333 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1334 (info->msg == WM_DDE_DATA && size < sizeof(DDEDATA)) ||
1335 (info->msg == WM_DDE_POKE && size < sizeof(DDEPOKE))
1337 return FALSE;
1339 else if (info->msg != WM_DDE_DATA) return FALSE;
1341 lp = uiHi;
1342 if (uiLo)
1344 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1346 DDEDATA *dde_data = (DDEDATA *)ptr;
1347 TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
1348 dde_data->unused, dde_data->fResponse, dde_data->fRelease,
1349 dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
1350 push_data( data, ptr, size );
1351 hunlock = (HGLOBAL)uiLo;
1354 TRACE( "send ddepack %u %x\n", size, uiHi );
1355 break;
1356 case WM_DDE_EXECUTE:
1357 if (info->lparam)
1359 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1361 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1362 /* so that the other side can send it back on ACK */
1363 lp = info->lparam;
1364 hunlock = (HGLOBAL)info->lparam;
1367 break;
1369 SERVER_START_REQ( send_message )
1371 req->id = dest_tid;
1372 req->type = info->type;
1373 req->flags = 0;
1374 req->win = info->hwnd;
1375 req->msg = info->msg;
1376 req->wparam = info->wparam;
1377 req->lparam = lp;
1378 req->time = GetCurrentTime();
1379 req->timeout = 0;
1380 for (i = 0; i < data->count; i++)
1381 wine_server_add_data( req, data->data[i], data->size[i] );
1382 if ((res = wine_server_call( req )))
1384 if (res == STATUS_INVALID_PARAMETER)
1385 /* FIXME: find a STATUS_ value for this one */
1386 SetLastError( ERROR_INVALID_THREAD_ID );
1387 else
1388 SetLastError( RtlNtStatusToDosError(res) );
1390 else
1391 FreeDDElParam(info->msg, info->lparam);
1393 SERVER_END_REQ;
1394 if (hunlock) GlobalUnlock(hunlock);
1396 return !res;
1399 /***********************************************************************
1400 * unpack_dde_message
1402 * Unpack a posted DDE message received from another process.
1404 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1405 void **buffer, size_t size )
1407 UINT_PTR uiLo, uiHi;
1408 HGLOBAL hMem = 0;
1409 void* ptr;
1411 switch (message)
1413 case WM_DDE_ACK:
1414 if (size)
1416 /* hMem is being passed */
1417 if (size != sizeof(HGLOBAL)) return FALSE;
1418 if (!buffer || !*buffer) return FALSE;
1419 uiLo = *lparam;
1420 memcpy( &hMem, *buffer, size );
1421 uiHi = (UINT_PTR)hMem;
1422 TRACE("recv dde-ack %x mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1424 else
1426 uiLo = LOWORD( *lparam );
1427 uiHi = HIWORD( *lparam );
1428 TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
1430 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1431 break;
1432 case WM_DDE_ADVISE:
1433 case WM_DDE_DATA:
1434 case WM_DDE_POKE:
1435 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1436 uiHi = *lparam;
1437 TRACE( "recv ddepack %u %x\n", size, uiHi );
1438 if (size)
1440 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size )))
1441 return FALSE;
1442 if ((ptr = GlobalLock( hMem )))
1444 memcpy( ptr, *buffer, size );
1445 GlobalUnlock( hMem );
1447 else
1449 GlobalFree( hMem );
1450 return FALSE;
1453 uiLo = (UINT_PTR)hMem;
1455 *lparam = PackDDElParam( message, uiLo, uiHi );
1456 break;
1457 case WM_DDE_EXECUTE:
1458 if (size)
1460 if (!buffer || !*buffer) return FALSE;
1461 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size ))) return FALSE;
1462 if ((ptr = GlobalLock( hMem )))
1464 memcpy( ptr, *buffer, size );
1465 GlobalUnlock( hMem );
1466 TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
1467 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1469 GlobalFree( hMem );
1470 return FALSE;
1473 else
1475 GlobalFree( hMem );
1476 return FALSE;
1478 } else return FALSE;
1479 *lparam = (LPARAM)hMem;
1480 break;
1482 return TRUE;
1485 /***********************************************************************
1486 * call_window_proc
1488 * Call a window procedure and the corresponding hooks.
1490 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1491 BOOL unicode, BOOL same_thread )
1493 struct user_thread_info *thread_info = get_user_thread_info();
1494 LRESULT result = 0;
1495 WNDPROC winproc;
1496 CWPSTRUCT cwp;
1497 CWPRETSTRUCT cwpret;
1499 if (thread_info->recursion_count > MAX_SENDMSG_RECURSION) return 0;
1500 thread_info->recursion_count++;
1502 if (msg & 0x80000000)
1504 result = handle_internal_message( hwnd, msg, wparam, lparam );
1505 goto done;
1508 /* first the WH_CALLWNDPROC hook */
1509 hwnd = WIN_GetFullHandle( hwnd );
1510 cwp.lParam = lparam;
1511 cwp.wParam = wparam;
1512 cwp.message = msg;
1513 cwp.hwnd = hwnd;
1514 HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1516 /* now call the window procedure */
1517 if (unicode)
1519 if (!(winproc = (WNDPROC)GetWindowLongPtrW( hwnd, GWLP_WNDPROC ))) goto done;
1520 result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1522 else
1524 if (!(winproc = (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ))) goto done;
1525 result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1528 /* and finally the WH_CALLWNDPROCRET hook */
1529 cwpret.lResult = result;
1530 cwpret.lParam = lparam;
1531 cwpret.wParam = wparam;
1532 cwpret.message = msg;
1533 cwpret.hwnd = hwnd;
1534 HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1535 done:
1536 thread_info->recursion_count--;
1537 return result;
1541 /***********************************************************************
1542 * send_parent_notify
1544 * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
1545 * the window has the WS_EX_NOPARENTNOTIFY style.
1547 static void send_parent_notify( HWND hwnd, WORD event, WORD idChild, POINT pt )
1549 /* pt has to be in the client coordinates of the parent window */
1550 MapWindowPoints( 0, hwnd, &pt, 1 );
1551 for (;;)
1553 HWND parent;
1555 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)) break;
1556 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
1557 if (!(parent = GetParent(hwnd))) break;
1558 MapWindowPoints( hwnd, parent, &pt, 1 );
1559 hwnd = parent;
1560 SendMessageW( hwnd, WM_PARENTNOTIFY,
1561 MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
1566 /***********************************************************************
1567 * accept_hardware_message
1569 * Tell the server we have passed the message to the app
1570 * (even though we may end up dropping it later on)
1572 static void accept_hardware_message( UINT hw_id, BOOL remove, HWND new_hwnd )
1574 SERVER_START_REQ( accept_hardware_message )
1576 req->hw_id = hw_id;
1577 req->remove = remove;
1578 req->new_win = new_hwnd;
1579 if (wine_server_call( req ))
1580 FIXME("Failed to reply to MSG_HARDWARE message. Message may not be removed from queue.\n");
1582 SERVER_END_REQ;
1586 /***********************************************************************
1587 * process_keyboard_message
1589 * returns TRUE if the contents of 'msg' should be passed to the application
1591 static BOOL process_keyboard_message( MSG *msg, UINT hw_id, HWND hwnd_filter,
1592 UINT first, UINT last, BOOL remove )
1594 EVENTMSG event;
1596 /* FIXME: is this really the right place for this hook? */
1597 event.message = msg->message;
1598 event.hwnd = msg->hwnd;
1599 event.time = msg->time;
1600 event.paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
1601 event.paramH = msg->lParam & 0x7FFF;
1602 if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
1603 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1605 /* check message filters */
1606 if (msg->message < first || msg->message > last) return FALSE;
1607 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1609 if (remove)
1611 /* Handle F1 key by sending out WM_HELP message */
1612 if ((msg->message == WM_KEYUP) &&
1613 (msg->wParam == VK_F1) &&
1614 (msg->hwnd != GetDesktopWindow()) &&
1615 !MENU_IsMenuActive())
1617 HELPINFO hi;
1618 hi.cbSize = sizeof(HELPINFO);
1619 hi.iContextType = HELPINFO_WINDOW;
1620 hi.iCtrlId = GetWindowLongPtrA( msg->hwnd, GWLP_ID );
1621 hi.hItemHandle = msg->hwnd;
1622 hi.dwContextId = GetWindowContextHelpId( msg->hwnd );
1623 hi.MousePos = msg->pt;
1624 SendMessageW( msg->hwnd, WM_HELP, 0, (LPARAM)&hi );
1628 if (HOOK_CallHooks( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
1629 LOWORD(msg->wParam), msg->lParam, TRUE ))
1631 /* skip this message */
1632 HOOK_CallHooks( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam, TRUE );
1633 accept_hardware_message( hw_id, TRUE, 0 );
1634 return FALSE;
1636 accept_hardware_message( hw_id, remove, 0 );
1637 return TRUE;
1641 /***********************************************************************
1642 * process_mouse_message
1644 * returns TRUE if the contents of 'msg' should be passed to the application
1646 static BOOL process_mouse_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
1647 UINT first, UINT last, BOOL remove )
1649 static MSG clk_msg;
1651 POINT pt;
1652 UINT message;
1653 INT hittest;
1654 EVENTMSG event;
1655 GUITHREADINFO info;
1656 MOUSEHOOKSTRUCT hook;
1657 BOOL eatMsg;
1659 /* find the window to dispatch this mouse message to */
1661 GetGUIThreadInfo( GetCurrentThreadId(), &info );
1662 if (info.hwndCapture)
1664 hittest = HTCLIENT;
1665 msg->hwnd = info.hwndCapture;
1667 else
1669 msg->hwnd = WINPOS_WindowFromPoint( msg->hwnd, msg->pt, &hittest );
1672 if (!msg->hwnd || !WIN_IsCurrentThread( msg->hwnd ))
1674 accept_hardware_message( hw_id, TRUE, msg->hwnd );
1675 return FALSE;
1678 /* FIXME: is this really the right place for this hook? */
1679 event.message = msg->message;
1680 event.time = msg->time;
1681 event.hwnd = msg->hwnd;
1682 event.paramL = msg->pt.x;
1683 event.paramH = msg->pt.y;
1684 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1686 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1688 pt = msg->pt;
1689 message = msg->message;
1690 /* Note: windows has no concept of a non-client wheel message */
1691 if (message != WM_MOUSEWHEEL)
1693 if (hittest != HTCLIENT)
1695 message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
1696 msg->wParam = hittest;
1698 else
1700 /* coordinates don't get translated while tracking a menu */
1701 /* FIXME: should differentiate popups and top-level menus */
1702 if (!(info.flags & GUI_INMENUMODE))
1703 ScreenToClient( msg->hwnd, &pt );
1706 msg->lParam = MAKELONG( pt.x, pt.y );
1708 /* translate double clicks */
1710 if ((msg->message == WM_LBUTTONDOWN) ||
1711 (msg->message == WM_RBUTTONDOWN) ||
1712 (msg->message == WM_MBUTTONDOWN) ||
1713 (msg->message == WM_XBUTTONDOWN))
1715 BOOL update = remove;
1717 /* translate double clicks -
1718 * note that ...MOUSEMOVEs can slip in between
1719 * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
1721 if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
1722 hittest != HTCLIENT ||
1723 (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
1725 if ((msg->message == clk_msg.message) &&
1726 (msg->hwnd == clk_msg.hwnd) &&
1727 (msg->wParam == clk_msg.wParam) &&
1728 (msg->time - clk_msg.time < GetDoubleClickTime()) &&
1729 (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
1730 (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
1732 message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
1733 if (update)
1735 clk_msg.message = 0; /* clear the double click conditions */
1736 update = FALSE;
1740 if (message < first || message > last) return FALSE;
1741 /* update static double click conditions */
1742 if (update) clk_msg = *msg;
1744 else
1746 if (message < first || message > last) return FALSE;
1749 /* message is accepted now (but may still get dropped) */
1751 hook.pt = msg->pt;
1752 hook.hwnd = msg->hwnd;
1753 hook.wHitTestCode = hittest;
1754 hook.dwExtraInfo = extra_info;
1755 if (HOOK_CallHooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
1756 message, (LPARAM)&hook, TRUE ))
1758 hook.pt = msg->pt;
1759 hook.hwnd = msg->hwnd;
1760 hook.wHitTestCode = hittest;
1761 hook.dwExtraInfo = extra_info;
1762 HOOK_CallHooks( WH_CBT, HCBT_CLICKSKIPPED, message, (LPARAM)&hook, TRUE );
1763 accept_hardware_message( hw_id, TRUE, 0 );
1764 return FALSE;
1767 if ((hittest == HTERROR) || (hittest == HTNOWHERE))
1769 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
1770 MAKELONG( hittest, msg->message ));
1771 accept_hardware_message( hw_id, TRUE, 0 );
1772 return FALSE;
1775 accept_hardware_message( hw_id, remove, 0 );
1777 if (!remove || info.hwndCapture)
1779 msg->message = message;
1780 return TRUE;
1783 eatMsg = FALSE;
1785 if ((msg->message == WM_LBUTTONDOWN) ||
1786 (msg->message == WM_RBUTTONDOWN) ||
1787 (msg->message == WM_MBUTTONDOWN) ||
1788 (msg->message == WM_XBUTTONDOWN))
1790 /* Send the WM_PARENTNOTIFY,
1791 * note that even for double/nonclient clicks
1792 * notification message is still WM_L/M/RBUTTONDOWN.
1794 send_parent_notify( msg->hwnd, msg->message, 0, msg->pt );
1796 /* Activate the window if needed */
1798 if (msg->hwnd != info.hwndActive)
1800 HWND hwndTop = msg->hwnd;
1801 while (hwndTop)
1803 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
1804 hwndTop = GetParent( hwndTop );
1807 if (hwndTop && hwndTop != GetDesktopWindow())
1809 LONG ret = SendMessageW( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
1810 MAKELONG( hittest, msg->message ) );
1811 switch(ret)
1813 case MA_NOACTIVATEANDEAT:
1814 eatMsg = TRUE;
1815 /* fall through */
1816 case MA_NOACTIVATE:
1817 break;
1818 case MA_ACTIVATEANDEAT:
1819 eatMsg = TRUE;
1820 /* fall through */
1821 case MA_ACTIVATE:
1822 case 0:
1823 if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
1824 break;
1825 default:
1826 WARN( "unknown WM_MOUSEACTIVATE code %ld\n", ret );
1827 break;
1833 /* send the WM_SETCURSOR message */
1835 /* Windows sends the normal mouse message as the message parameter
1836 in the WM_SETCURSOR message even if it's non-client mouse message */
1837 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd, MAKELONG( hittest, msg->message ));
1839 msg->message = message;
1840 return !eatMsg;
1844 /***********************************************************************
1845 * process_hardware_message
1847 * Process a hardware message; return TRUE if message should be passed on to the app
1849 static BOOL process_hardware_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
1850 UINT first, UINT last, BOOL remove )
1852 if (is_keyboard_message( msg->message ))
1853 return process_keyboard_message( msg, hw_id, hwnd_filter, first, last, remove );
1855 if (is_mouse_message( msg->message ))
1856 return process_mouse_message( msg, hw_id, extra_info, hwnd_filter, first, last, remove );
1858 ERR( "unknown message type %x\n", msg->message );
1859 return FALSE;
1863 /***********************************************************************
1864 * call_sendmsg_callback
1866 * Call the callback function of SendMessageCallback.
1868 static inline void call_sendmsg_callback( SENDASYNCPROC callback, HWND hwnd, UINT msg,
1869 ULONG_PTR data, LRESULT result )
1871 if (TRACE_ON(relay))
1872 DPRINTF( "%04lx:Call message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1873 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
1874 data, result );
1875 callback( hwnd, msg, data, result );
1876 if (TRACE_ON(relay))
1877 DPRINTF( "%04lx:Ret message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1878 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
1879 data, result );
1883 /***********************************************************************
1884 * get_hook_proc
1886 * Retrieve the hook procedure real value for a module-relative proc
1888 static void *get_hook_proc( void *proc, const WCHAR *module )
1890 HMODULE mod;
1892 if (!(mod = GetModuleHandleW(module)))
1894 TRACE( "loading %s\n", debugstr_w(module) );
1895 /* FIXME: the library will never be freed */
1896 if (!(mod = LoadLibraryW(module))) return NULL;
1898 return (char *)mod + (ULONG_PTR)proc;
1902 /***********************************************************************
1903 * peek_message
1905 * Peek for a message matching the given parameters. Return FALSE if none available.
1906 * All pending sent messages are processed before returning.
1908 static BOOL peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1910 LRESULT result;
1911 ULONG_PTR extra_info = 0;
1912 struct user_thread_info *thread_info = get_user_thread_info();
1913 struct received_message_info info, *old_info;
1914 unsigned int hw_id = 0; /* id of previous hardware message */
1916 if (!first && !last) last = ~0;
1918 for (;;)
1920 NTSTATUS res;
1921 void *buffer = NULL;
1922 size_t size = 0, buffer_size = 0;
1924 do /* loop while buffer is too small */
1926 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1927 return FALSE;
1928 SERVER_START_REQ( get_message )
1930 req->flags = flags;
1931 req->get_win = hwnd;
1932 req->get_first = first;
1933 req->get_last = last;
1934 req->hw_id = hw_id;
1935 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1936 if (!(res = wine_server_call( req )))
1938 size = wine_server_reply_size( reply );
1939 info.type = reply->type;
1940 info.msg.hwnd = reply->win;
1941 info.msg.message = reply->msg;
1942 info.msg.wParam = reply->wparam;
1943 info.msg.lParam = reply->lparam;
1944 info.msg.time = reply->time;
1945 info.msg.pt.x = reply->x;
1946 info.msg.pt.y = reply->y;
1947 info.hook = reply->hook;
1948 info.hook_proc = reply->hook_proc;
1949 hw_id = reply->hw_id;
1950 extra_info = reply->info;
1951 thread_info->active_hooks = reply->active_hooks;
1953 else
1955 HeapFree( GetProcessHeap(), 0, buffer );
1956 buffer_size = reply->total;
1959 SERVER_END_REQ;
1960 } while (res == STATUS_BUFFER_OVERFLOW);
1962 if (res) return FALSE;
1964 TRACE( "got type %d msg %x (%s) hwnd %p wp %x lp %lx\n",
1965 info.type, info.msg.message,
1966 (info.type == MSG_WINEVENT) ? "MSG_WINEVENT" : SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1967 info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1969 switch(info.type)
1971 case MSG_ASCII:
1972 case MSG_UNICODE:
1973 info.flags = ISMEX_SEND;
1974 break;
1975 case MSG_NOTIFY:
1976 info.flags = ISMEX_NOTIFY;
1977 break;
1978 case MSG_CALLBACK:
1979 info.flags = ISMEX_CALLBACK;
1980 break;
1981 case MSG_CALLBACK_RESULT:
1982 call_sendmsg_callback( (SENDASYNCPROC)info.msg.wParam, info.msg.hwnd,
1983 info.msg.message, extra_info, info.msg.lParam );
1984 goto next;
1985 case MSG_WINEVENT:
1986 if (size)
1988 WCHAR module[MAX_PATH];
1989 size = min( size, (MAX_PATH - 1) * sizeof(WCHAR) );
1990 memcpy( module, buffer, size );
1991 module[size / sizeof(WCHAR)] = 0;
1992 if (!(info.hook_proc = get_hook_proc( info.hook_proc, module )))
1994 ERR( "invalid winevent hook module name %s\n", debugstr_w(module) );
1995 goto next;
1998 if (TRACE_ON(relay))
1999 DPRINTF( "%04lx:Call winevent proc %p (hook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%lx,tid=%04lx,time=%lx)\n",
2000 GetCurrentThreadId(), info.hook_proc,
2001 info.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2002 info.msg.lParam, extra_info, info.msg.time);
2004 info.hook_proc( info.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2005 info.msg.lParam, extra_info, info.msg.time );
2007 if (TRACE_ON(relay))
2008 DPRINTF( "%04lx:Ret winevent proc %p (hook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%lx,tid=%04lx,time=%lx)\n",
2009 GetCurrentThreadId(), info.hook_proc,
2010 info.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2011 info.msg.lParam, extra_info, info.msg.time);
2012 goto next;
2013 case MSG_OTHER_PROCESS:
2014 info.flags = ISMEX_SEND;
2015 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2016 &info.msg.lParam, &buffer, size ))
2018 ERR( "invalid packed message %x (%s) hwnd %p wp %x lp %lx size %d\n",
2019 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
2020 info.msg.wParam, info.msg.lParam, size );
2021 /* ignore it */
2022 reply_message( &info, 0, TRUE );
2023 goto next;
2025 break;
2026 case MSG_HARDWARE:
2027 if (!process_hardware_message( &info.msg, hw_id, extra_info,
2028 hwnd, first, last, flags & GET_MSG_REMOVE ))
2030 TRACE("dropping msg %x\n", info.msg.message );
2031 goto next; /* ignore it */
2033 thread_info->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
2034 /* fall through */
2035 case MSG_POSTED:
2036 thread_info->GetMessageExtraInfoVal = extra_info;
2037 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
2039 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2040 &info.msg.lParam, &buffer, size ))
2042 ERR( "invalid packed dde-message %x (%s) hwnd %p wp %x lp %lx size %d\n",
2043 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
2044 info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
2045 goto next; /* ignore it */
2048 *msg = info.msg;
2049 HeapFree( GetProcessHeap(), 0, buffer );
2050 return TRUE;
2053 /* if we get here, we have a sent message; call the window procedure */
2054 old_info = thread_info->receive_info;
2055 thread_info->receive_info = &info;
2056 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
2057 info.msg.lParam, (info.type != MSG_ASCII), FALSE );
2058 reply_message( &info, result, TRUE );
2059 thread_info->receive_info = old_info;
2060 next:
2061 HeapFree( GetProcessHeap(), 0, buffer );
2066 /***********************************************************************
2067 * process_sent_messages
2069 * Process all pending sent messages.
2071 inline static void process_sent_messages(void)
2073 MSG msg;
2074 peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
2078 /***********************************************************************
2079 * get_server_queue_handle
2081 * Get a handle to the server message queue for the current thread.
2083 static HANDLE get_server_queue_handle(void)
2085 struct user_thread_info *thread_info = get_user_thread_info();
2086 HANDLE ret;
2088 if (!(ret = thread_info->server_queue))
2090 SERVER_START_REQ( get_msg_queue )
2092 wine_server_call( req );
2093 ret = reply->handle;
2095 SERVER_END_REQ;
2096 thread_info->server_queue = ret;
2097 if (!ret) ERR( "Cannot get server thread queue\n" );
2099 return ret;
2103 /***********************************************************************
2104 * wait_message_reply
2106 * Wait until a sent message gets replied to.
2108 static void wait_message_reply( UINT flags )
2110 HANDLE server_queue = get_server_queue_handle();
2112 for (;;)
2114 unsigned int wake_bits = 0, changed_bits = 0;
2115 DWORD dwlc, res;
2117 SERVER_START_REQ( set_queue_mask )
2119 req->wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
2120 req->changed_mask = req->wake_mask;
2121 req->skip_wait = 1;
2122 if (!wine_server_call( req ))
2124 wake_bits = reply->wake_bits;
2125 changed_bits = reply->changed_bits;
2128 SERVER_END_REQ;
2130 if (wake_bits & QS_SMRESULT) return; /* got a result */
2131 if (wake_bits & QS_SENDMESSAGE)
2133 /* Process the sent message immediately */
2134 process_sent_messages();
2135 continue;
2138 /* now wait for it */
2140 ReleaseThunkLock( &dwlc );
2141 res = USER_Driver->pMsgWaitForMultipleObjectsEx( 1, &server_queue,
2142 INFINITE, QS_ALLINPUT, 0 );
2143 if (dwlc) RestoreThunkLock( dwlc );
2147 /***********************************************************************
2148 * put_message_in_queue
2150 * Put a sent message into the destination queue.
2151 * For inter-process message, reply_size is set to expected size of reply data.
2153 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
2154 size_t *reply_size )
2156 struct packed_message data;
2157 unsigned int res;
2158 int i, timeout = 0;
2160 /* Check for INFINITE timeout for compatibility with Win9x,
2161 * although Windows >= NT does not do so
2163 if (info->type != MSG_NOTIFY &&
2164 info->type != MSG_CALLBACK &&
2165 info->type != MSG_POSTED &&
2166 info->timeout != INFINITE)
2167 timeout = info->timeout;
2169 data.count = 0;
2170 if (info->type == MSG_OTHER_PROCESS)
2172 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
2173 if (data.count == -1)
2175 WARN( "cannot pack message %x\n", info->msg );
2176 return FALSE;
2179 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
2181 return post_dde_message( dest_tid, &data, info );
2184 SERVER_START_REQ( send_message )
2186 req->id = dest_tid;
2187 req->type = info->type;
2188 req->flags = 0;
2189 req->win = info->hwnd;
2190 req->msg = info->msg;
2191 req->wparam = info->wparam;
2192 req->lparam = info->lparam;
2193 req->time = GetCurrentTime();
2194 req->timeout = timeout;
2196 if (info->type == MSG_CALLBACK)
2198 req->callback = info->callback;
2199 req->info = info->data;
2202 if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
2203 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
2204 if ((res = wine_server_call( req )))
2206 if (res == STATUS_INVALID_PARAMETER)
2207 /* FIXME: find a STATUS_ value for this one */
2208 SetLastError( ERROR_INVALID_THREAD_ID );
2209 else
2210 SetLastError( RtlNtStatusToDosError(res) );
2213 SERVER_END_REQ;
2214 return !res;
2218 /***********************************************************************
2219 * retrieve_reply
2221 * Retrieve a message reply from the server.
2223 static LRESULT retrieve_reply( const struct send_message_info *info,
2224 size_t reply_size, LRESULT *result )
2226 NTSTATUS status;
2227 void *reply_data = NULL;
2229 if (reply_size)
2231 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
2233 WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
2234 reply_size = 0;
2237 SERVER_START_REQ( get_message_reply )
2239 req->cancel = 1;
2240 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
2241 if (!(status = wine_server_call( req ))) *result = reply->result;
2242 reply_size = wine_server_reply_size( reply );
2244 SERVER_END_REQ;
2245 if (!status && reply_size)
2246 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
2248 HeapFree( GetProcessHeap(), 0, reply_data );
2250 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
2251 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
2252 info->lparam, *result, status );
2254 /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
2255 if (status) SetLastError( RtlNtStatusToDosError(status) );
2256 return !status;
2260 /***********************************************************************
2261 * send_inter_thread_message
2263 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
2264 LRESULT *res_ptr )
2266 size_t reply_size = 0;
2268 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
2269 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
2271 USER_CheckNotLock();
2273 if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
2275 /* there's no reply to wait for on notify/callback messages */
2276 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
2278 wait_message_reply( info->flags );
2279 return retrieve_reply( info, reply_size, res_ptr );
2283 /***********************************************************************
2284 * MSG_SendInternalMessageTimeout
2286 * Same as SendMessageTimeoutW but sends the message to a specific thread
2287 * without requiring a window handle. Only works for internal Wine messages.
2289 LRESULT MSG_SendInternalMessageTimeout( DWORD dest_pid, DWORD dest_tid,
2290 UINT msg, WPARAM wparam, LPARAM lparam,
2291 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2293 struct send_message_info info;
2294 LRESULT ret, result;
2296 assert( msg & 0x80000000 ); /* must be an internal Wine message */
2298 info.type = MSG_UNICODE;
2299 info.hwnd = 0;
2300 info.msg = msg;
2301 info.wparam = wparam;
2302 info.lparam = lparam;
2303 info.flags = flags;
2304 info.timeout = timeout;
2306 if (USER_IsExitingThread( dest_tid )) return 0;
2308 if (dest_tid == GetCurrentThreadId())
2310 result = handle_internal_message( 0, msg, wparam, lparam );
2311 ret = 1;
2313 else
2315 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
2316 ret = send_inter_thread_message( dest_tid, &info, &result );
2318 if (ret && res_ptr) *res_ptr = result;
2319 return ret;
2323 /***********************************************************************
2324 * SendMessageTimeoutW (USER32.@)
2326 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2327 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2329 struct send_message_info info;
2330 DWORD dest_tid, dest_pid;
2331 LRESULT ret, result;
2333 info.type = MSG_UNICODE;
2334 info.hwnd = hwnd;
2335 info.msg = msg;
2336 info.wparam = wparam;
2337 info.lparam = lparam;
2338 info.flags = flags;
2339 info.timeout = timeout;
2341 if (is_broadcast(hwnd))
2343 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2344 if (res_ptr) *res_ptr = 1;
2345 return 1;
2348 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
2350 if (USER_IsExitingThread( dest_tid )) return 0;
2352 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
2354 if (dest_tid == GetCurrentThreadId())
2356 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2357 ret = 1;
2359 else
2361 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
2362 ret = send_inter_thread_message( dest_tid, &info, &result );
2365 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
2366 if (ret && res_ptr) *res_ptr = result;
2367 return ret;
2371 /***********************************************************************
2372 * SendMessageTimeoutA (USER32.@)
2374 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2375 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2377 struct send_message_info info;
2378 DWORD dest_tid, dest_pid;
2379 LRESULT ret, result;
2381 info.type = MSG_ASCII;
2382 info.hwnd = hwnd;
2383 info.msg = msg;
2384 info.wparam = wparam;
2385 info.lparam = lparam;
2386 info.flags = flags;
2387 info.timeout = timeout;
2389 if (is_broadcast(hwnd))
2391 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2392 if (res_ptr) *res_ptr = 1;
2393 return 1;
2396 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
2398 if (USER_IsExitingThread( dest_tid )) return 0;
2400 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
2402 if (dest_tid == GetCurrentThreadId())
2404 result = call_window_proc( hwnd, msg, wparam, lparam, FALSE, TRUE );
2405 ret = 1;
2407 else if (dest_pid == GetCurrentProcessId())
2409 ret = send_inter_thread_message( dest_tid, &info, &result );
2411 else
2413 /* inter-process message: need to map to Unicode */
2414 info.type = MSG_OTHER_PROCESS;
2415 if (is_unicode_message( info.msg ))
2417 if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
2418 return 0;
2419 ret = send_inter_thread_message( dest_tid, &info, &result );
2420 result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
2421 info.lparam, result, NULL );
2423 else ret = send_inter_thread_message( dest_tid, &info, &result );
2425 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
2426 if (ret && res_ptr) *res_ptr = result;
2427 return ret;
2431 /***********************************************************************
2432 * SendMessageW (USER32.@)
2434 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2436 DWORD_PTR res = 0;
2437 SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, 0, &res );
2438 return res;
2442 /***********************************************************************
2443 * SendMessageA (USER32.@)
2445 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2447 DWORD_PTR res = 0;
2448 SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, 0, &res );
2449 return res;
2453 /***********************************************************************
2454 * SendNotifyMessageA (USER32.@)
2456 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2458 return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
2462 /***********************************************************************
2463 * SendNotifyMessageW (USER32.@)
2465 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2467 struct send_message_info info;
2468 DWORD dest_tid;
2469 LRESULT result;
2471 if (is_pointer_message(msg))
2473 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2474 return FALSE;
2477 info.type = MSG_NOTIFY;
2478 info.hwnd = hwnd;
2479 info.msg = msg;
2480 info.wparam = wparam;
2481 info.lparam = lparam;
2482 info.flags = 0;
2484 if (is_broadcast(hwnd))
2486 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2487 return TRUE;
2490 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2492 if (USER_IsExitingThread( dest_tid )) return TRUE;
2494 if (dest_tid == GetCurrentThreadId())
2496 call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2497 return TRUE;
2499 return send_inter_thread_message( dest_tid, &info, &result );
2503 /***********************************************************************
2504 * SendMessageCallbackA (USER32.@)
2506 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2507 SENDASYNCPROC callback, ULONG_PTR data )
2509 return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
2510 lparam, callback, data );
2514 /***********************************************************************
2515 * SendMessageCallbackW (USER32.@)
2517 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2518 SENDASYNCPROC callback, ULONG_PTR data )
2520 struct send_message_info info;
2521 LRESULT result;
2522 DWORD dest_tid;
2524 if (is_pointer_message(msg))
2526 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2527 return FALSE;
2530 info.type = MSG_CALLBACK;
2531 info.hwnd = hwnd;
2532 info.msg = msg;
2533 info.wparam = wparam;
2534 info.lparam = lparam;
2535 info.callback = callback;
2536 info.data = data;
2537 info.flags = 0;
2539 if (is_broadcast(hwnd))
2541 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2542 return TRUE;
2545 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2547 if (USER_IsExitingThread( dest_tid )) return TRUE;
2549 if (dest_tid == GetCurrentThreadId())
2551 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2552 call_sendmsg_callback( callback, hwnd, msg, data, result );
2553 return TRUE;
2555 FIXME( "callback will not be called\n" );
2556 return send_inter_thread_message( dest_tid, &info, &result );
2560 /***********************************************************************
2561 * ReplyMessage (USER32.@)
2563 BOOL WINAPI ReplyMessage( LRESULT result )
2565 struct received_message_info *info = get_user_thread_info()->receive_info;
2567 if (!info) return FALSE;
2568 reply_message( info, result, FALSE );
2569 return TRUE;
2573 /***********************************************************************
2574 * InSendMessage (USER32.@)
2576 BOOL WINAPI InSendMessage(void)
2578 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
2582 /***********************************************************************
2583 * InSendMessageEx (USER32.@)
2585 DWORD WINAPI InSendMessageEx( LPVOID reserved )
2587 struct received_message_info *info = get_user_thread_info()->receive_info;
2589 if (info) return info->flags;
2590 return ISMEX_NOSEND;
2594 /***********************************************************************
2595 * PostMessageA (USER32.@)
2597 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2599 return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
2603 /***********************************************************************
2604 * PostMessageW (USER32.@)
2606 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2608 struct send_message_info info;
2609 DWORD dest_tid;
2611 if (is_pointer_message( msg ))
2613 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2614 return FALSE;
2617 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
2618 hwnd, msg, SPY_GetMsgName(msg, hwnd), wparam, lparam );
2620 info.type = MSG_POSTED;
2621 info.hwnd = hwnd;
2622 info.msg = msg;
2623 info.wparam = wparam;
2624 info.lparam = lparam;
2625 info.flags = 0;
2627 if (is_broadcast(hwnd))
2629 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2630 return TRUE;
2633 if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2635 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2637 if (USER_IsExitingThread( dest_tid )) return TRUE;
2639 return put_message_in_queue( dest_tid, &info, NULL );
2643 /**********************************************************************
2644 * PostThreadMessageA (USER32.@)
2646 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2648 return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2652 /**********************************************************************
2653 * PostThreadMessageW (USER32.@)
2655 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2657 struct send_message_info info;
2659 if (is_pointer_message( msg ))
2661 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2662 return FALSE;
2664 if (USER_IsExitingThread( thread )) return TRUE;
2666 info.type = MSG_POSTED;
2667 info.hwnd = 0;
2668 info.msg = msg;
2669 info.wparam = wparam;
2670 info.lparam = lparam;
2671 info.flags = 0;
2672 return put_message_in_queue( thread, &info, NULL );
2676 /***********************************************************************
2677 * PostQuitMessage (USER32.@)
2679 * Posts a quit message to the current thread's message queue.
2681 * PARAMS
2682 * exit_code [I] Exit code to return from message loop.
2684 * RETURNS
2685 * Nothing.
2687 * NOTES
2688 * This function is not the same as calling:
2689 *|PostThreadMessage(GetCurrentThreadId(), WM_QUIT, exit_code, 0);
2690 * It instead sets a flag in the message queue that signals it to generate
2691 * a WM_QUIT message when there are no other pending sent or posted messages
2692 * in the queue.
2694 void WINAPI PostQuitMessage( INT exit_code )
2696 SERVER_START_REQ( post_quit_message )
2698 req->exit_code = exit_code;
2699 wine_server_call( req );
2701 SERVER_END_REQ;
2705 /***********************************************************************
2706 * PeekMessageW (USER32.@)
2708 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2710 struct user_thread_info *thread_info = get_user_thread_info();
2711 MSG msg;
2713 if (HIWORD(flags))
2714 FIXME("PM_QS_xxxx flags (%04x) are not handled\n", flags >> 16);
2716 USER_CheckNotLock();
2718 /* check for graphics events */
2719 USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
2721 hwnd = WIN_GetFullHandle( hwnd );
2723 for (;;)
2725 if (!peek_message( &msg, hwnd, first, last, (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2727 if (!(flags & PM_NOYIELD))
2729 DWORD count;
2730 ReleaseThunkLock(&count);
2731 NtYieldExecution();
2732 if (count) RestoreThunkLock(count);
2734 return FALSE;
2736 if (msg.message & 0x80000000)
2738 if (!(flags & PM_REMOVE))
2740 /* Have to remove the message explicitly.
2741 Do this before handling it, because the message handler may
2742 call PeekMessage again */
2743 peek_message( &msg, msg.hwnd, msg.message, msg.message, GET_MSG_REMOVE );
2745 handle_internal_message( msg.hwnd, msg.message, msg.wParam, msg.lParam );
2747 else break;
2750 thread_info->GetMessageTimeVal = msg.time;
2751 msg.pt.x = LOWORD( thread_info->GetMessagePosVal );
2752 msg.pt.y = HIWORD( thread_info->GetMessagePosVal );
2754 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
2756 /* copy back our internal safe copy of message data to msg_out.
2757 * msg_out is a variable from the *program*, so it can't be used
2758 * internally as it can get "corrupted" by our use of SendMessage()
2759 * (back to the program) inside the message handling itself. */
2760 if (!msg_out)
2762 SetLastError( ERROR_NOACCESS );
2763 return FALSE;
2765 *msg_out = msg;
2766 return TRUE;
2770 /***********************************************************************
2771 * PeekMessageA (USER32.@)
2773 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2775 BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2776 if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2777 return ret;
2781 /***********************************************************************
2782 * GetMessageW (USER32.@)
2784 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2786 HANDLE server_queue = get_server_queue_handle();
2787 int mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2789 if (first || last)
2791 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2792 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2793 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2794 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2795 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2796 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2798 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2800 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2802 /* wait until one of the bits is set */
2803 unsigned int wake_bits = 0, changed_bits = 0;
2804 DWORD dwlc;
2806 SERVER_START_REQ( set_queue_mask )
2808 req->wake_mask = QS_SENDMESSAGE;
2809 req->changed_mask = mask;
2810 req->skip_wait = 1;
2811 if (!wine_server_call( req ))
2813 wake_bits = reply->wake_bits;
2814 changed_bits = reply->changed_bits;
2817 SERVER_END_REQ;
2819 if (changed_bits & mask) continue;
2820 if (wake_bits & QS_SENDMESSAGE) continue;
2822 TRACE( "(%04lx) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2823 GetCurrentThreadId(), mask, wake_bits, changed_bits );
2825 ReleaseThunkLock( &dwlc );
2826 USER_Driver->pMsgWaitForMultipleObjectsEx( 1, &server_queue, INFINITE, QS_ALLINPUT, 0 );
2827 if (dwlc) RestoreThunkLock( dwlc );
2830 return (msg->message != WM_QUIT);
2834 /***********************************************************************
2835 * GetMessageA (USER32.@)
2837 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2839 GetMessageW( msg, hwnd, first, last );
2840 msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2841 return (msg->message != WM_QUIT);
2845 /***********************************************************************
2846 * IsDialogMessageA (USER32.@)
2847 * IsDialogMessage (USER32.@)
2849 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2851 MSG msg = *pmsg;
2852 msg.wParam = map_wparam_AtoW( msg.message, msg.wParam );
2853 return IsDialogMessageW( hwndDlg, &msg );
2857 /***********************************************************************
2858 * TranslateMessage (USER32.@)
2860 * Implementation of TranslateMessage.
2862 * TranslateMessage translates virtual-key messages into character-messages,
2863 * as follows :
2864 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
2865 * ditto replacing WM_* with WM_SYS*
2866 * This produces WM_CHAR messages only for keys mapped to ASCII characters
2867 * by the keyboard driver.
2869 * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
2870 * return value is nonzero, regardless of the translation.
2873 BOOL WINAPI TranslateMessage( const MSG *msg )
2875 UINT message;
2876 WCHAR wp[2];
2877 BYTE state[256];
2879 if (msg->message < WM_KEYFIRST || msg->message > WM_KEYLAST) return FALSE;
2880 if (msg->message != WM_KEYDOWN && msg->message != WM_SYSKEYDOWN) return TRUE;
2882 TRACE_(key)("Translating key %s (%04x), scancode %02x\n",
2883 SPY_GetVKeyName(msg->wParam), msg->wParam, LOBYTE(HIWORD(msg->lParam)));
2885 GetKeyboardState( state );
2886 /* FIXME : should handle ToUnicode yielding 2 */
2887 switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), state, wp, 2, 0))
2889 case 1:
2890 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
2891 TRACE_(key)("1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
2892 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
2893 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
2894 break;
2896 case -1:
2897 message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
2898 TRACE_(key)("-1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
2899 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
2900 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
2901 break;
2903 return TRUE;
2907 /***********************************************************************
2908 * DispatchMessageA (USER32.@)
2910 * See DispatchMessageW.
2912 LONG WINAPI DispatchMessageA( const MSG* msg )
2914 WND * wndPtr;
2915 LONG retval;
2916 WNDPROC winproc;
2918 /* Process timer messages */
2919 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
2921 if (msg->lParam) return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
2922 msg->message, msg->wParam, GetTickCount() );
2925 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
2927 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2928 return 0;
2930 if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP)
2932 if (IsWindow( msg->hwnd )) SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2933 else SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2934 return 0;
2936 if (wndPtr->tid != GetCurrentThreadId())
2938 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2939 WIN_ReleasePtr( wndPtr );
2940 return 0;
2942 winproc = wndPtr->winproc;
2943 WIN_ReleasePtr( wndPtr );
2945 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
2946 msg->wParam, msg->lParam );
2947 retval = CallWindowProcA( winproc, msg->hwnd, msg->message,
2948 msg->wParam, msg->lParam );
2949 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
2950 msg->wParam, msg->lParam );
2952 if (msg->message == WM_PAINT)
2954 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
2955 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
2956 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
2957 DeleteObject( hrgn );
2959 return retval;
2963 /***********************************************************************
2964 * DispatchMessageW (USER32.@) Process a message
2966 * Process the message specified in the structure *_msg_.
2968 * If the lpMsg parameter points to a WM_TIMER message and the
2969 * parameter of the WM_TIMER message is not NULL, the lParam parameter
2970 * points to the function that is called instead of the window
2971 * procedure.
2973 * The message must be valid.
2975 * RETURNS
2977 * DispatchMessage() returns the result of the window procedure invoked.
2979 * CONFORMANCE
2981 * ECMA-234, Win32
2984 LONG WINAPI DispatchMessageW( const MSG* msg )
2986 WND * wndPtr;
2987 LONG retval;
2988 WNDPROC winproc;
2990 /* Process timer messages */
2991 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
2993 if (msg->lParam) return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
2994 msg->message, msg->wParam, GetTickCount() );
2997 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
2999 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3000 return 0;
3002 if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP)
3004 if (IsWindow( msg->hwnd )) SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3005 else SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3006 return 0;
3008 if (wndPtr->tid != GetCurrentThreadId())
3010 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3011 WIN_ReleasePtr( wndPtr );
3012 return 0;
3014 winproc = wndPtr->winproc;
3015 WIN_ReleasePtr( wndPtr );
3017 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3018 msg->wParam, msg->lParam );
3019 retval = CallWindowProcW( winproc, msg->hwnd, msg->message,
3020 msg->wParam, msg->lParam );
3021 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3022 msg->wParam, msg->lParam );
3024 if (msg->message == WM_PAINT)
3026 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3027 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3028 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3029 DeleteObject( hrgn );
3031 return retval;
3035 /***********************************************************************
3036 * GetMessagePos (USER.119)
3037 * GetMessagePos (USER32.@)
3039 * The GetMessagePos() function returns a long value representing a
3040 * cursor position, in screen coordinates, when the last message
3041 * retrieved by the GetMessage() function occurs. The x-coordinate is
3042 * in the low-order word of the return value, the y-coordinate is in
3043 * the high-order word. The application can use the MAKEPOINT()
3044 * macro to obtain a POINT structure from the return value.
3046 * For the current cursor position, use GetCursorPos().
3048 * RETURNS
3050 * Cursor position of last message on success, zero on failure.
3052 * CONFORMANCE
3054 * ECMA-234, Win32
3057 DWORD WINAPI GetMessagePos(void)
3059 return get_user_thread_info()->GetMessagePosVal;
3063 /***********************************************************************
3064 * GetMessageTime (USER.120)
3065 * GetMessageTime (USER32.@)
3067 * GetMessageTime() returns the message time for the last message
3068 * retrieved by the function. The time is measured in milliseconds with
3069 * the same offset as GetTickCount().
3071 * Since the tick count wraps, this is only useful for moderately short
3072 * relative time comparisons.
3074 * RETURNS
3076 * Time of last message on success, zero on failure.
3078 LONG WINAPI GetMessageTime(void)
3080 return get_user_thread_info()->GetMessageTimeVal;
3084 /***********************************************************************
3085 * GetMessageExtraInfo (USER.288)
3086 * GetMessageExtraInfo (USER32.@)
3088 LPARAM WINAPI GetMessageExtraInfo(void)
3090 return get_user_thread_info()->GetMessageExtraInfoVal;
3094 /***********************************************************************
3095 * SetMessageExtraInfo (USER32.@)
3097 LPARAM WINAPI SetMessageExtraInfo(LPARAM lParam)
3099 struct user_thread_info *thread_info = get_user_thread_info();
3100 LONG old_value = thread_info->GetMessageExtraInfoVal;
3101 thread_info->GetMessageExtraInfoVal = lParam;
3102 return old_value;
3106 /***********************************************************************
3107 * WaitMessage (USER.112) Suspend thread pending messages
3108 * WaitMessage (USER32.@) Suspend thread pending messages
3110 * WaitMessage() suspends a thread until events appear in the thread's
3111 * queue.
3113 BOOL WINAPI WaitMessage(void)
3115 return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
3119 /***********************************************************************
3120 * MsgWaitForMultipleObjectsEx (USER32.@)
3122 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
3123 DWORD timeout, DWORD mask, DWORD flags )
3125 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
3126 DWORD i, ret, lock;
3128 if (count > MAXIMUM_WAIT_OBJECTS-1)
3130 SetLastError( ERROR_INVALID_PARAMETER );
3131 return WAIT_FAILED;
3134 /* set the queue mask */
3135 SERVER_START_REQ( set_queue_mask )
3137 req->wake_mask = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
3138 req->changed_mask = mask;
3139 req->skip_wait = 0;
3140 wine_server_call( req );
3142 SERVER_END_REQ;
3144 /* Add the thread event to the handle list */
3145 for (i = 0; i < count; i++) handles[i] = pHandles[i];
3146 handles[count] = get_server_queue_handle();
3148 ReleaseThunkLock( &lock );
3149 ret = USER_Driver->pMsgWaitForMultipleObjectsEx( count+1, handles, timeout, mask, flags );
3150 if (ret == count+1) ret = count; /* pretend the msg queue is ready */
3151 if (lock) RestoreThunkLock( lock );
3152 return ret;
3156 /***********************************************************************
3157 * MsgWaitForMultipleObjects (USER32.@)
3159 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
3160 BOOL wait_all, DWORD timeout, DWORD mask )
3162 return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
3163 wait_all ? MWMO_WAITALL : 0 );
3167 /***********************************************************************
3168 * WaitForInputIdle (USER32.@)
3170 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
3172 DWORD start_time, elapsed, ret;
3173 HANDLE idle_event = (HANDLE)-1;
3175 SERVER_START_REQ( wait_input_idle )
3177 req->handle = hProcess;
3178 req->timeout = dwTimeOut;
3179 if (!(ret = wine_server_call_err( req ))) idle_event = reply->event;
3181 SERVER_END_REQ;
3182 if (ret) return WAIT_FAILED; /* error */
3183 if (!idle_event) return 0; /* no event to wait on */
3185 start_time = GetTickCount();
3186 elapsed = 0;
3188 TRACE("waiting for %p\n", idle_event );
3191 ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
3192 switch (ret)
3194 case WAIT_OBJECT_0+1:
3195 process_sent_messages();
3196 break;
3197 case WAIT_TIMEOUT:
3198 case WAIT_FAILED:
3199 TRACE("timeout or error\n");
3200 return ret;
3201 default:
3202 TRACE("finished\n");
3203 return 0;
3205 if (dwTimeOut != INFINITE)
3207 elapsed = GetTickCount() - start_time;
3208 if (elapsed > dwTimeOut)
3209 break;
3212 while (1);
3214 return WAIT_TIMEOUT;
3218 /***********************************************************************
3219 * UserYield (USER.332)
3221 void WINAPI UserYield16(void)
3223 DWORD count;
3225 /* Handle sent messages */
3226 process_sent_messages();
3228 /* Yield */
3229 ReleaseThunkLock(&count);
3231 if (count)
3233 RestoreThunkLock(count);
3234 /* Handle sent messages again */
3235 process_sent_messages();
3240 /***********************************************************************
3241 * RegisterWindowMessageA (USER32.@)
3242 * RegisterWindowMessage (USER.118)
3244 UINT WINAPI RegisterWindowMessageA( LPCSTR str )
3246 UINT ret = GlobalAddAtomA(str);
3247 TRACE("%s, ret=%x\n", str, ret);
3248 return ret;
3252 /***********************************************************************
3253 * RegisterWindowMessageW (USER32.@)
3255 UINT WINAPI RegisterWindowMessageW( LPCWSTR str )
3257 UINT ret = GlobalAddAtomW(str);
3258 TRACE("%s ret=%x\n", debugstr_w(str), ret);
3259 return ret;
3263 /***********************************************************************
3264 * BroadcastSystemMessageA (USER32.@)
3265 * BroadcastSystemMessage (USER32.@)
3267 LONG WINAPI BroadcastSystemMessageA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
3269 if ((*recipients & BSM_APPLICATIONS) || (*recipients == BSM_ALLCOMPONENTS))
3271 FIXME( "(%08lx,%08lx,%08x,%08x,%08lx): semi-stub!\n", flags, *recipients, msg, wp, lp );
3272 PostMessageA( HWND_BROADCAST, msg, wp, lp );
3273 return 1;
3275 else
3277 FIXME( "(%08lx,%08lx,%08x,%08x,%08lx): stub!\n", flags, *recipients, msg, wp, lp);
3278 return -1;
3283 /***********************************************************************
3284 * BroadcastSystemMessageW (USER32.@)
3286 LONG WINAPI BroadcastSystemMessageW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
3288 if ((*recipients & BSM_APPLICATIONS) || (*recipients == BSM_ALLCOMPONENTS))
3290 FIXME( "(%08lx,%08lx,%08x,%08x,%08lx): semi-stub!\n", flags, *recipients, msg, wp, lp );
3291 PostMessageW( HWND_BROADCAST, msg, wp, lp );
3292 return 1;
3294 else
3296 FIXME( "(%08lx,%08lx,%08x,%08x,%08lx): stub!\n", flags, *recipients, msg, wp, lp );
3297 return -1;
3302 /***********************************************************************
3303 * SetMessageQueue (USER32.@)
3305 BOOL WINAPI SetMessageQueue( INT size )
3307 /* now obsolete the message queue will be expanded dynamically as necessary */
3308 return TRUE;
3312 /***********************************************************************
3313 * MessageBeep (USER32.@)
3315 BOOL WINAPI MessageBeep( UINT i )
3317 BOOL active = TRUE;
3318 SystemParametersInfoA( SPI_GETBEEP, 0, &active, FALSE );
3319 if (active) USER_Driver->pBeep();
3320 return TRUE;
3324 /***********************************************************************
3325 * SetTimer (USER32.@)
3327 UINT_PTR WINAPI SetTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3329 UINT_PTR ret;
3330 WNDPROC winproc = 0;
3332 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, WIN_PROC_32A );
3334 SERVER_START_REQ( set_win_timer )
3336 req->win = hwnd;
3337 req->msg = WM_TIMER;
3338 req->id = id;
3339 req->rate = max( timeout, SYS_TIMER_RATE );
3340 req->lparam = (unsigned int)winproc;
3341 if (!wine_server_call_err( req ))
3343 ret = reply->id;
3344 if (!ret) ret = TRUE;
3346 else ret = 0;
3348 SERVER_END_REQ;
3350 TRACE("Added %p %x %p timeout %d\n", hwnd, id, winproc, timeout );
3351 return ret;
3355 /***********************************************************************
3356 * SetSystemTimer (USER32.@)
3358 UINT_PTR WINAPI SetSystemTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3360 UINT_PTR ret;
3361 WNDPROC winproc = 0;
3363 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, WIN_PROC_32A );
3365 SERVER_START_REQ( set_win_timer )
3367 req->win = hwnd;
3368 req->msg = WM_SYSTIMER;
3369 req->id = id;
3370 req->rate = max( timeout, SYS_TIMER_RATE );
3371 req->lparam = (unsigned int)winproc;
3372 if (!wine_server_call_err( req ))
3374 ret = reply->id;
3375 if (!ret) ret = TRUE;
3377 else ret = 0;
3379 SERVER_END_REQ;
3381 TRACE("Added %p %x %p timeout %d\n", hwnd, id, winproc, timeout );
3382 return ret;
3386 /***********************************************************************
3387 * KillTimer (USER32.@)
3389 BOOL WINAPI KillTimer( HWND hwnd, UINT_PTR id )
3391 BOOL ret;
3393 TRACE("%p %d\n", hwnd, id );
3395 SERVER_START_REQ( kill_win_timer )
3397 req->win = hwnd;
3398 req->msg = WM_TIMER;
3399 req->id = id;
3400 ret = !wine_server_call_err( req );
3402 SERVER_END_REQ;
3403 return ret;
3407 /***********************************************************************
3408 * KillSystemTimer (USER32.@)
3410 BOOL WINAPI KillSystemTimer( HWND hwnd, UINT_PTR id )
3412 BOOL ret;
3414 TRACE("%p %d\n", hwnd, id );
3416 SERVER_START_REQ( kill_win_timer )
3418 req->win = hwnd;
3419 req->msg = WM_SYSTIMER;
3420 req->id = id;
3421 ret = !wine_server_call_err( req );
3423 SERVER_END_REQ;
3424 return ret;
3428 /**********************************************************************
3429 * GetGUIThreadInfo (USER32.@)
3431 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
3433 BOOL ret;
3435 SERVER_START_REQ( get_thread_input )
3437 req->tid = id;
3438 if ((ret = !wine_server_call_err( req )))
3440 info->flags = 0;
3441 info->hwndActive = reply->active;
3442 info->hwndFocus = reply->focus;
3443 info->hwndCapture = reply->capture;
3444 info->hwndMenuOwner = reply->menu_owner;
3445 info->hwndMoveSize = reply->move_size;
3446 info->hwndCaret = reply->caret;
3447 info->rcCaret.left = reply->rect.left;
3448 info->rcCaret.top = reply->rect.top;
3449 info->rcCaret.right = reply->rect.right;
3450 info->rcCaret.bottom = reply->rect.bottom;
3451 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
3452 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
3453 if (reply->caret) info->flags |= GUI_CARETBLINKING;
3456 SERVER_END_REQ;
3457 return ret;
3461 /******************************************************************
3462 * IsHungAppWindow (USER32.@)
3465 BOOL WINAPI IsHungAppWindow( HWND hWnd )
3467 DWORD_PTR dwResult;
3468 return !SendMessageTimeoutA(hWnd, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 5000, &dwResult);