Release 0.9.14.
[wine/multimedia.git] / dlls / user / message.c
blobc92db6a5c209fc1d8c6018d0c1bc7412c85b5ddb
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "dbt.h"
36 #include "dde.h"
37 #include "wine/unicode.h"
38 #include "wine/server.h"
39 #include "user_private.h"
40 #include "win.h"
41 #include "winpos.h"
42 #include "controls.h"
43 #include "winproc.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(msg);
47 WINE_DECLARE_DEBUG_CHANNEL(relay);
48 WINE_DECLARE_DEBUG_CHANNEL(key);
50 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
51 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
53 #define MAX_PACK_COUNT 4
54 #define MAX_SENDMSG_RECURSION 64
56 #define SYS_TIMER_RATE 55 /* min. timer rate in ms (actually 54.925)*/
58 /* description of the data fields that need to be packed along with a sent message */
59 struct packed_message
61 int count;
62 const void *data[MAX_PACK_COUNT];
63 size_t size[MAX_PACK_COUNT];
66 /* info about the message currently being received by the current thread */
67 struct received_message_info
69 enum message_type type;
70 MSG msg;
71 UINT flags; /* InSendMessageEx return flags */
72 HWINEVENTHOOK hook; /* winevent hook handle */
73 WINEVENTPROC hook_proc; /* winevent hook proc address */
76 /* structure to group all parameters for sent messages of the various kinds */
77 struct send_message_info
79 enum message_type type;
80 DWORD dest_tid;
81 HWND hwnd;
82 UINT msg;
83 WPARAM wparam;
84 LPARAM lparam;
85 UINT flags; /* flags for SendMessageTimeout */
86 UINT timeout; /* timeout for SendMessageTimeout */
87 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
88 ULONG_PTR data; /* callback data */
92 /* flag for messages that contain pointers */
93 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
95 #define SET(msg) (1 << ((msg) & 31))
97 static const unsigned int message_pointer_flags[] =
99 /* 0x00 - 0x1f */
100 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
101 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
102 /* 0x20 - 0x3f */
103 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
104 SET(WM_COMPAREITEM),
105 /* 0x40 - 0x5f */
106 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
107 SET(WM_NOTIFY) | SET(WM_HELP),
108 /* 0x60 - 0x7f */
109 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
110 /* 0x80 - 0x9f */
111 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
112 /* 0xa0 - 0xbf */
113 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
114 /* 0xc0 - 0xdf */
115 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
116 /* 0xe0 - 0xff */
117 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO) | SET(SBM_GETSCROLLBARINFO),
118 /* 0x100 - 0x11f */
120 /* 0x120 - 0x13f */
122 /* 0x140 - 0x15f */
123 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
124 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
125 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
126 /* 0x160 - 0x17f */
128 /* 0x180 - 0x19f */
129 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
130 SET(LB_DIR) | SET(LB_FINDSTRING) |
131 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
132 /* 0x1a0 - 0x1bf */
133 SET(LB_FINDSTRINGEXACT),
134 /* 0x1c0 - 0x1df */
136 /* 0x1e0 - 0x1ff */
138 /* 0x200 - 0x21f */
139 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
140 /* 0x220 - 0x23f */
141 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
142 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
143 /* 0x240 - 0x25f */
145 /* 0x260 - 0x27f */
147 /* 0x280 - 0x29f */
149 /* 0x2a0 - 0x2bf */
151 /* 0x2c0 - 0x2df */
153 /* 0x2e0 - 0x2ff */
155 /* 0x300 - 0x31f */
156 SET(WM_ASKCBFORMATNAME)
159 /* flags for messages that contain Unicode strings */
160 static const unsigned int message_unicode_flags[] =
162 /* 0x00 - 0x1f */
163 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
164 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
165 /* 0x20 - 0x3f */
166 SET(WM_CHARTOITEM),
167 /* 0x40 - 0x5f */
169 /* 0x60 - 0x7f */
171 /* 0x80 - 0x9f */
172 SET(WM_NCCREATE),
173 /* 0xa0 - 0xbf */
175 /* 0xc0 - 0xdf */
176 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
177 /* 0xe0 - 0xff */
179 /* 0x100 - 0x11f */
180 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
181 /* 0x120 - 0x13f */
182 SET(WM_MENUCHAR),
183 /* 0x140 - 0x15f */
184 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
185 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
186 /* 0x160 - 0x17f */
188 /* 0x180 - 0x19f */
189 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
190 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
191 /* 0x1a0 - 0x1bf */
192 SET(LB_FINDSTRINGEXACT),
193 /* 0x1c0 - 0x1df */
195 /* 0x1e0 - 0x1ff */
197 /* 0x200 - 0x21f */
199 /* 0x220 - 0x23f */
200 SET(WM_MDICREATE),
201 /* 0x240 - 0x25f */
203 /* 0x260 - 0x27f */
205 /* 0x280 - 0x29f */
206 SET(WM_IME_CHAR),
207 /* 0x2a0 - 0x2bf */
209 /* 0x2c0 - 0x2df */
211 /* 0x2e0 - 0x2ff */
213 /* 0x300 - 0x31f */
214 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
217 /* check whether a given message type includes pointers */
218 inline static int is_pointer_message( UINT message )
220 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
221 return (message_pointer_flags[message / 32] & SET(message)) != 0;
224 /* check whether a given message type contains Unicode (or ASCII) chars */
225 inline static int is_unicode_message( UINT message )
227 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
228 return (message_unicode_flags[message / 32] & SET(message)) != 0;
231 #undef SET
233 /* add a data field to a packed message */
234 inline static void push_data( struct packed_message *data, const void *ptr, size_t size )
236 data->data[data->count] = ptr;
237 data->size[data->count] = size;
238 data->count++;
241 /* add a string to a packed message */
242 inline static void push_string( struct packed_message *data, LPCWSTR str )
244 push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
247 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
248 inline static void *get_data( void **buffer, size_t size )
250 void *ret = *buffer;
251 *buffer = (char *)*buffer + size;
252 return ret;
255 /* make sure that the buffer contains a valid null-terminated Unicode string */
256 inline static BOOL check_string( LPCWSTR str, size_t size )
258 for (size /= sizeof(WCHAR); size; size--, str++)
259 if (!*str) return TRUE;
260 return FALSE;
263 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
264 inline static void *get_buffer_space( void **buffer, size_t size )
266 void *ret;
268 if (*buffer)
270 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
271 HeapFree( GetProcessHeap(), 0, *buffer );
273 else ret = HeapAlloc( GetProcessHeap(), 0, size );
275 *buffer = ret;
276 return ret;
279 /* retrieve a string pointer from packed data */
280 inline static LPWSTR get_string( void **buffer )
282 return get_data( buffer, (strlenW( (LPWSTR)*buffer ) + 1) * sizeof(WCHAR) );
285 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
286 inline static BOOL combobox_has_strings( HWND hwnd )
288 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
289 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
292 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
293 inline static BOOL listbox_has_strings( HWND hwnd )
295 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
296 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
299 /* check whether message is in the range of keyboard messages */
300 inline static BOOL is_keyboard_message( UINT message )
302 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
305 /* check whether message is in the range of mouse messages */
306 inline static BOOL is_mouse_message( UINT message )
308 return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
309 (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
312 /* check whether message matches the specified hwnd filter */
313 inline static BOOL check_hwnd_filter( const MSG *msg, HWND hwnd_filter )
315 if (!hwnd_filter) return TRUE;
316 return (msg->hwnd == hwnd_filter || IsChild( hwnd_filter, msg->hwnd ));
320 /***********************************************************************
321 * broadcast_message_callback
323 * Helper callback for broadcasting messages.
325 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
327 struct send_message_info *info = (struct send_message_info *)lparam;
328 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
329 switch(info->type)
331 case MSG_UNICODE:
332 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
333 info->flags, info->timeout, NULL );
334 break;
335 case MSG_ASCII:
336 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
337 info->flags, info->timeout, NULL );
338 break;
339 case MSG_NOTIFY:
340 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
341 break;
342 case MSG_CALLBACK:
343 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
344 info->callback, info->data );
345 break;
346 case MSG_POSTED:
347 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
348 break;
349 default:
350 ERR( "bad type %d\n", info->type );
351 break;
353 return TRUE;
357 /***********************************************************************
358 * map_wparam_AtoW
360 * Convert the wparam of an ASCII message to Unicode.
362 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
364 switch(message)
366 case WM_CHARTOITEM:
367 case EM_SETPASSWORDCHAR:
368 case WM_CHAR:
369 case WM_DEADCHAR:
370 case WM_SYSCHAR:
371 case WM_SYSDEADCHAR:
372 case WM_MENUCHAR:
374 char ch[2];
375 WCHAR wch[2];
376 ch[0] = (wparam & 0xff);
377 ch[1] = (wparam >> 8);
378 MultiByteToWideChar(CP_ACP, 0, ch, 2, wch, 2);
379 wparam = MAKEWPARAM(wch[0], wch[1]);
381 break;
382 case WM_IME_CHAR:
384 char ch[2];
385 WCHAR wch;
386 ch[0] = (wparam >> 8);
387 ch[1] = (wparam & 0xff);
388 if (ch[0]) MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
389 else MultiByteToWideChar(CP_ACP, 0, &ch[1], 1, &wch, 1);
390 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
392 break;
394 return wparam;
398 /***********************************************************************
399 * map_wparam_WtoA
401 * Convert the wparam of a Unicode message to ASCII.
403 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
405 switch(message)
407 case WM_CHARTOITEM:
408 case EM_SETPASSWORDCHAR:
409 case WM_CHAR:
410 case WM_DEADCHAR:
411 case WM_SYSCHAR:
412 case WM_SYSDEADCHAR:
413 case WM_MENUCHAR:
415 WCHAR wch[2];
416 BYTE ch[2];
417 wch[0] = LOWORD(wparam);
418 wch[1] = HIWORD(wparam);
419 WideCharToMultiByte( CP_ACP, 0, wch, 2, (LPSTR)ch, 2, NULL, NULL );
420 wparam = MAKEWPARAM( ch[0] | (ch[1] << 8), 0 );
422 break;
423 case WM_IME_CHAR:
425 WCHAR wch = LOWORD(wparam);
426 BYTE ch[2];
428 if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)ch, 2, NULL, NULL ) == 2)
429 wparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wparam) );
430 else
431 wparam = MAKEWPARAM( ch[0], HIWORD(wparam) );
433 break;
435 return wparam;
439 /***********************************************************************
440 * pack_message
442 * Pack a message for sending to another process.
443 * Return the size of the data we expect in the message reply.
444 * Set data->count to -1 if there is an error.
446 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
447 struct packed_message *data )
449 data->count = 0;
450 switch(message)
452 case WM_NCCREATE:
453 case WM_CREATE:
455 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
456 push_data( data, cs, sizeof(*cs) );
457 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
458 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
459 return sizeof(*cs);
461 case WM_GETTEXT:
462 case WM_ASKCBFORMATNAME:
463 return wparam * sizeof(WCHAR);
464 case WM_WININICHANGE:
465 if (lparam) push_string(data, (LPWSTR)lparam );
466 return 0;
467 case WM_SETTEXT:
468 case WM_DEVMODECHANGE:
469 case CB_DIR:
470 case LB_DIR:
471 case LB_ADDFILE:
472 case EM_REPLACESEL:
473 push_string( data, (LPWSTR)lparam );
474 return 0;
475 case WM_GETMINMAXINFO:
476 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
477 return sizeof(MINMAXINFO);
478 case WM_DRAWITEM:
479 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
480 return 0;
481 case WM_MEASUREITEM:
482 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
483 return sizeof(MEASUREITEMSTRUCT);
484 case WM_DELETEITEM:
485 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
486 return 0;
487 case WM_COMPAREITEM:
488 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
489 return 0;
490 case WM_WINDOWPOSCHANGING:
491 case WM_WINDOWPOSCHANGED:
492 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
493 return sizeof(WINDOWPOS);
494 case WM_COPYDATA:
496 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
497 push_data( data, cp, sizeof(*cp) );
498 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
499 return 0;
501 case WM_NOTIFY:
502 /* WM_NOTIFY cannot be sent across processes (MSDN) */
503 data->count = -1;
504 return 0;
505 case WM_HELP:
506 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
507 return 0;
508 case WM_STYLECHANGING:
509 case WM_STYLECHANGED:
510 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
511 return 0;
512 case WM_NCCALCSIZE:
513 if (!wparam)
515 push_data( data, (RECT *)lparam, sizeof(RECT) );
516 return sizeof(RECT);
518 else
520 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
521 push_data( data, nc, sizeof(*nc) );
522 push_data( data, nc->lppos, sizeof(*nc->lppos) );
523 return sizeof(*nc) + sizeof(*nc->lppos);
525 case WM_GETDLGCODE:
526 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
527 return sizeof(MSG);
528 case SBM_SETSCROLLINFO:
529 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
530 return 0;
531 case SBM_GETSCROLLINFO:
532 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
533 return sizeof(SCROLLINFO);
534 case SBM_GETSCROLLBARINFO:
536 const SCROLLBARINFO *info = (const SCROLLBARINFO *)lparam;
537 size_t size = min( info->cbSize, sizeof(SCROLLBARINFO) );
538 push_data( data, info, size );
539 return size;
541 case EM_GETSEL:
542 case SBM_GETRANGE:
543 case CB_GETEDITSEL:
545 size_t size = 0;
546 if (wparam) size += sizeof(DWORD);
547 if (lparam) size += sizeof(DWORD);
548 return size;
550 case EM_GETRECT:
551 case LB_GETITEMRECT:
552 case CB_GETDROPPEDCONTROLRECT:
553 return sizeof(RECT);
554 case EM_SETRECT:
555 case EM_SETRECTNP:
556 push_data( data, (RECT *)lparam, sizeof(RECT) );
557 return 0;
558 case EM_GETLINE:
560 WORD *pw = (WORD *)lparam;
561 push_data( data, pw, sizeof(*pw) );
562 return *pw * sizeof(WCHAR);
564 case EM_SETTABSTOPS:
565 case LB_SETTABSTOPS:
566 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
567 return 0;
568 case CB_ADDSTRING:
569 case CB_INSERTSTRING:
570 case CB_FINDSTRING:
571 case CB_FINDSTRINGEXACT:
572 case CB_SELECTSTRING:
573 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
574 return 0;
575 case CB_GETLBTEXT:
576 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
577 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
578 case LB_ADDSTRING:
579 case LB_INSERTSTRING:
580 case LB_FINDSTRING:
581 case LB_FINDSTRINGEXACT:
582 case LB_SELECTSTRING:
583 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
584 return 0;
585 case LB_GETTEXT:
586 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
587 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
588 case LB_GETSELITEMS:
589 return wparam * sizeof(UINT);
590 case WM_NEXTMENU:
591 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
592 return sizeof(MDINEXTMENU);
593 case WM_SIZING:
594 case WM_MOVING:
595 push_data( data, (RECT *)lparam, sizeof(RECT) );
596 return sizeof(RECT);
597 case WM_MDICREATE:
599 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
600 push_data( data, cs, sizeof(*cs) );
601 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
602 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
603 return sizeof(*cs);
605 case WM_MDIGETACTIVE:
606 if (lparam) return sizeof(BOOL);
607 return 0;
608 case WM_DEVICECHANGE:
610 DEV_BROADCAST_HDR *header = (DEV_BROADCAST_HDR *)lparam;
611 push_data( data, header, header->dbch_size );
612 return 0;
614 case WM_WINE_SETWINDOWPOS:
615 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
616 return 0;
617 case WM_WINE_KEYBOARD_LL_HOOK:
618 push_data( data, (KBDLLHOOKSTRUCT *)lparam, sizeof(KBDLLHOOKSTRUCT) );
619 return 0;
620 case WM_WINE_MOUSE_LL_HOOK:
621 push_data( data, (MSLLHOOKSTRUCT *)lparam, sizeof(MSLLHOOKSTRUCT) );
622 return 0;
623 case WM_NCPAINT:
624 if (wparam <= 1) return 0;
625 FIXME( "WM_NCPAINT hdc packing not supported yet\n" );
626 data->count = -1;
627 return 0;
628 case WM_PAINT:
629 if (!wparam) return 0;
630 /* fall through */
632 /* these contain an HFONT */
633 case WM_SETFONT:
634 case WM_GETFONT:
635 /* these contain an HDC */
636 case WM_ERASEBKGND:
637 case WM_ICONERASEBKGND:
638 case WM_CTLCOLORMSGBOX:
639 case WM_CTLCOLOREDIT:
640 case WM_CTLCOLORLISTBOX:
641 case WM_CTLCOLORBTN:
642 case WM_CTLCOLORDLG:
643 case WM_CTLCOLORSCROLLBAR:
644 case WM_CTLCOLORSTATIC:
645 case WM_PRINT:
646 case WM_PRINTCLIENT:
647 /* these contain an HGLOBAL */
648 case WM_PAINTCLIPBOARD:
649 case WM_SIZECLIPBOARD:
650 /* these contain HICON */
651 case WM_GETICON:
652 case WM_SETICON:
653 case WM_QUERYDRAGICON:
654 case WM_QUERYPARKICON:
655 /* these contain pointers */
656 case WM_DROPOBJECT:
657 case WM_QUERYDROPOBJECT:
658 case WM_DRAGLOOP:
659 case WM_DRAGSELECT:
660 case WM_DRAGMOVE:
661 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
662 data->count = -1;
663 return 0;
665 return 0;
669 /***********************************************************************
670 * unpack_message
672 * Unpack a message received from another process.
674 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
675 void **buffer, size_t size )
677 size_t minsize = 0;
679 switch(message)
681 case WM_NCCREATE:
682 case WM_CREATE:
684 CREATESTRUCTW *cs = *buffer;
685 WCHAR *str = (WCHAR *)(cs + 1);
686 if (size < sizeof(*cs)) return FALSE;
687 size -= sizeof(*cs);
688 if (HIWORD(cs->lpszName))
690 if (!check_string( str, size )) return FALSE;
691 cs->lpszName = str;
692 size -= (strlenW(str) + 1) * sizeof(WCHAR);
693 str += strlenW(str) + 1;
695 if (HIWORD(cs->lpszClass))
697 if (!check_string( str, size )) return FALSE;
698 cs->lpszClass = str;
700 break;
702 case WM_GETTEXT:
703 case WM_ASKCBFORMATNAME:
704 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
705 break;
706 case WM_WININICHANGE:
707 if (!*lparam) return TRUE;
708 /* fall through */
709 case WM_SETTEXT:
710 case WM_DEVMODECHANGE:
711 case CB_DIR:
712 case LB_DIR:
713 case LB_ADDFILE:
714 case EM_REPLACESEL:
715 if (!check_string( *buffer, size )) return FALSE;
716 break;
717 case WM_GETMINMAXINFO:
718 minsize = sizeof(MINMAXINFO);
719 break;
720 case WM_DRAWITEM:
721 minsize = sizeof(DRAWITEMSTRUCT);
722 break;
723 case WM_MEASUREITEM:
724 minsize = sizeof(MEASUREITEMSTRUCT);
725 break;
726 case WM_DELETEITEM:
727 minsize = sizeof(DELETEITEMSTRUCT);
728 break;
729 case WM_COMPAREITEM:
730 minsize = sizeof(COMPAREITEMSTRUCT);
731 break;
732 case WM_WINDOWPOSCHANGING:
733 case WM_WINDOWPOSCHANGED:
734 case WM_WINE_SETWINDOWPOS:
735 minsize = sizeof(WINDOWPOS);
736 break;
737 case WM_COPYDATA:
739 COPYDATASTRUCT *cp = *buffer;
740 if (size < sizeof(*cp)) return FALSE;
741 if (cp->lpData)
743 minsize = sizeof(*cp) + cp->cbData;
744 cp->lpData = cp + 1;
746 break;
748 case WM_NOTIFY:
749 /* WM_NOTIFY cannot be sent across processes (MSDN) */
750 return FALSE;
751 case WM_HELP:
752 minsize = sizeof(HELPINFO);
753 break;
754 case WM_STYLECHANGING:
755 case WM_STYLECHANGED:
756 minsize = sizeof(STYLESTRUCT);
757 break;
758 case WM_NCCALCSIZE:
759 if (!*wparam) minsize = sizeof(RECT);
760 else
762 NCCALCSIZE_PARAMS *nc = *buffer;
763 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
764 nc->lppos = (WINDOWPOS *)(nc + 1);
766 break;
767 case WM_GETDLGCODE:
768 if (!*lparam) return TRUE;
769 minsize = sizeof(MSG);
770 break;
771 case SBM_SETSCROLLINFO:
772 minsize = sizeof(SCROLLINFO);
773 break;
774 case SBM_GETSCROLLINFO:
775 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
776 break;
777 case SBM_GETSCROLLBARINFO:
778 if (!get_buffer_space( buffer, sizeof(SCROLLBARINFO ))) return FALSE;
779 break;
780 case EM_GETSEL:
781 case SBM_GETRANGE:
782 case CB_GETEDITSEL:
783 if (*wparam || *lparam)
785 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
786 if (*wparam) *wparam = (WPARAM)*buffer;
787 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
789 return TRUE;
790 case EM_GETRECT:
791 case LB_GETITEMRECT:
792 case CB_GETDROPPEDCONTROLRECT:
793 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
794 break;
795 case EM_SETRECT:
796 case EM_SETRECTNP:
797 minsize = sizeof(RECT);
798 break;
799 case EM_GETLINE:
801 WORD len;
802 if (size < sizeof(WORD)) return FALSE;
803 len = *(WORD *)*buffer;
804 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
805 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
806 return TRUE;
808 case EM_SETTABSTOPS:
809 case LB_SETTABSTOPS:
810 if (!*wparam) return TRUE;
811 minsize = *wparam * sizeof(UINT);
812 break;
813 case CB_ADDSTRING:
814 case CB_INSERTSTRING:
815 case CB_FINDSTRING:
816 case CB_FINDSTRINGEXACT:
817 case CB_SELECTSTRING:
818 case LB_ADDSTRING:
819 case LB_INSERTSTRING:
820 case LB_FINDSTRING:
821 case LB_FINDSTRINGEXACT:
822 case LB_SELECTSTRING:
823 if (!*buffer) return TRUE;
824 if (!check_string( *buffer, size )) return FALSE;
825 break;
826 case CB_GETLBTEXT:
828 size = sizeof(ULONG_PTR);
829 if (combobox_has_strings( hwnd ))
830 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
831 if (!get_buffer_space( buffer, size )) return FALSE;
832 break;
834 case LB_GETTEXT:
836 size = sizeof(ULONG_PTR);
837 if (listbox_has_strings( hwnd ))
838 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
839 if (!get_buffer_space( buffer, size )) return FALSE;
840 break;
842 case LB_GETSELITEMS:
843 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
844 break;
845 case WM_NEXTMENU:
846 minsize = sizeof(MDINEXTMENU);
847 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
848 break;
849 case WM_SIZING:
850 case WM_MOVING:
851 minsize = sizeof(RECT);
852 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
853 break;
854 case WM_MDICREATE:
856 MDICREATESTRUCTW *cs = *buffer;
857 WCHAR *str = (WCHAR *)(cs + 1);
858 if (size < sizeof(*cs)) return FALSE;
859 size -= sizeof(*cs);
860 if (HIWORD(cs->szTitle))
862 if (!check_string( str, size )) return FALSE;
863 cs->szTitle = str;
864 size -= (strlenW(str) + 1) * sizeof(WCHAR);
865 str += strlenW(str) + 1;
867 if (HIWORD(cs->szClass))
869 if (!check_string( str, size )) return FALSE;
870 cs->szClass = str;
872 break;
874 case WM_MDIGETACTIVE:
875 if (!*lparam) return TRUE;
876 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
877 break;
878 case WM_DEVICECHANGE:
879 minsize = sizeof(DEV_BROADCAST_HDR);
880 break;
881 case WM_WINE_KEYBOARD_LL_HOOK:
882 minsize = sizeof(KBDLLHOOKSTRUCT);
883 break;
884 case WM_WINE_MOUSE_LL_HOOK:
885 minsize = sizeof(MSLLHOOKSTRUCT);
886 break;
887 case WM_NCPAINT:
888 if (*wparam <= 1) return TRUE;
889 FIXME( "WM_NCPAINT hdc unpacking not supported\n" );
890 return FALSE;
891 case WM_PAINT:
892 if (!*wparam) return TRUE;
893 /* fall through */
895 /* these contain an HFONT */
896 case WM_SETFONT:
897 case WM_GETFONT:
898 /* these contain an HDC */
899 case WM_ERASEBKGND:
900 case WM_ICONERASEBKGND:
901 case WM_CTLCOLORMSGBOX:
902 case WM_CTLCOLOREDIT:
903 case WM_CTLCOLORLISTBOX:
904 case WM_CTLCOLORBTN:
905 case WM_CTLCOLORDLG:
906 case WM_CTLCOLORSCROLLBAR:
907 case WM_CTLCOLORSTATIC:
908 case WM_PRINT:
909 case WM_PRINTCLIENT:
910 /* these contain an HGLOBAL */
911 case WM_PAINTCLIPBOARD:
912 case WM_SIZECLIPBOARD:
913 /* these contain HICON */
914 case WM_GETICON:
915 case WM_SETICON:
916 case WM_QUERYDRAGICON:
917 case WM_QUERYPARKICON:
918 /* these contain pointers */
919 case WM_DROPOBJECT:
920 case WM_QUERYDROPOBJECT:
921 case WM_DRAGLOOP:
922 case WM_DRAGSELECT:
923 case WM_DRAGMOVE:
924 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
925 return FALSE;
927 default:
928 return TRUE; /* message doesn't need any unpacking */
931 /* default exit for most messages: check minsize and store buffer in lparam */
932 if (size < minsize) return FALSE;
933 *lparam = (LPARAM)*buffer;
934 return TRUE;
938 /***********************************************************************
939 * pack_reply
941 * Pack a reply to a message for sending to another process.
943 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
944 LRESULT res, struct packed_message *data )
946 data->count = 0;
947 switch(message)
949 case WM_NCCREATE:
950 case WM_CREATE:
951 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
952 break;
953 case WM_GETTEXT:
954 case CB_GETLBTEXT:
955 case LB_GETTEXT:
956 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
957 break;
958 case WM_GETMINMAXINFO:
959 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
960 break;
961 case WM_MEASUREITEM:
962 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
963 break;
964 case WM_WINDOWPOSCHANGING:
965 case WM_WINDOWPOSCHANGED:
966 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
967 break;
968 case WM_GETDLGCODE:
969 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
970 break;
971 case SBM_GETSCROLLINFO:
972 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
973 break;
974 case EM_GETRECT:
975 case LB_GETITEMRECT:
976 case CB_GETDROPPEDCONTROLRECT:
977 case WM_SIZING:
978 case WM_MOVING:
979 push_data( data, (RECT *)lparam, sizeof(RECT) );
980 break;
981 case EM_GETLINE:
983 WORD *ptr = (WORD *)lparam;
984 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
985 break;
987 case LB_GETSELITEMS:
988 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
989 break;
990 case WM_MDIGETACTIVE:
991 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
992 break;
993 case WM_NCCALCSIZE:
994 if (!wparam)
995 push_data( data, (RECT *)lparam, sizeof(RECT) );
996 else
998 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
999 push_data( data, nc, sizeof(*nc) );
1000 push_data( data, nc->lppos, sizeof(*nc->lppos) );
1002 break;
1003 case EM_GETSEL:
1004 case SBM_GETRANGE:
1005 case CB_GETEDITSEL:
1006 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
1007 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
1008 break;
1009 case WM_NEXTMENU:
1010 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
1011 break;
1012 case WM_MDICREATE:
1013 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
1014 break;
1015 case WM_ASKCBFORMATNAME:
1016 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
1017 break;
1022 /***********************************************************************
1023 * unpack_reply
1025 * Unpack a message reply received from another process.
1027 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1028 void *buffer, size_t size )
1030 switch(message)
1032 case WM_NCCREATE:
1033 case WM_CREATE:
1035 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
1036 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
1037 memcpy( cs, buffer, min( sizeof(*cs), size ));
1038 cs->lpszName = name; /* restore the original pointers */
1039 cs->lpszClass = class;
1040 break;
1042 case WM_GETTEXT:
1043 case WM_ASKCBFORMATNAME:
1044 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
1045 break;
1046 case WM_GETMINMAXINFO:
1047 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
1048 break;
1049 case WM_MEASUREITEM:
1050 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
1051 break;
1052 case WM_WINDOWPOSCHANGING:
1053 case WM_WINDOWPOSCHANGED:
1054 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
1055 break;
1056 case WM_GETDLGCODE:
1057 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
1058 break;
1059 case SBM_GETSCROLLINFO:
1060 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
1061 break;
1062 case SBM_GETSCROLLBARINFO:
1063 memcpy( (SCROLLBARINFO *)lparam, buffer, min( sizeof(SCROLLBARINFO), size ));
1064 break;
1065 case EM_GETRECT:
1066 case CB_GETDROPPEDCONTROLRECT:
1067 case LB_GETITEMRECT:
1068 case WM_SIZING:
1069 case WM_MOVING:
1070 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1071 break;
1072 case EM_GETLINE:
1073 size = min( size, (size_t)*(WORD *)lparam );
1074 memcpy( (WCHAR *)lparam, buffer, size );
1075 break;
1076 case LB_GETSELITEMS:
1077 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
1078 break;
1079 case LB_GETTEXT:
1080 case CB_GETLBTEXT:
1081 memcpy( (WCHAR *)lparam, buffer, size );
1082 break;
1083 case WM_NEXTMENU:
1084 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
1085 break;
1086 case WM_MDIGETACTIVE:
1087 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
1088 break;
1089 case WM_NCCALCSIZE:
1090 if (!wparam)
1091 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1092 else
1094 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1095 WINDOWPOS *wp = nc->lppos;
1096 memcpy( nc, buffer, min( sizeof(*nc), size ));
1097 if (size > sizeof(*nc))
1099 size -= sizeof(*nc);
1100 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
1102 nc->lppos = wp; /* restore the original pointer */
1104 break;
1105 case EM_GETSEL:
1106 case SBM_GETRANGE:
1107 case CB_GETEDITSEL:
1108 if (wparam)
1110 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1111 if (size <= sizeof(DWORD)) break;
1112 size -= sizeof(DWORD);
1113 buffer = (DWORD *)buffer + 1;
1115 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1116 break;
1117 case WM_MDICREATE:
1119 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
1120 LPCWSTR title = cs->szTitle, class = cs->szClass;
1121 memcpy( cs, buffer, min( sizeof(*cs), size ));
1122 cs->szTitle = title; /* restore the original pointers */
1123 cs->szClass = class;
1124 break;
1126 default:
1127 ERR( "should not happen: unexpected message %x\n", message );
1128 break;
1133 /***********************************************************************
1134 * reply_message
1136 * Send a reply to a sent message.
1138 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1140 struct packed_message data;
1141 int i, replied = info->flags & ISMEX_REPLIED;
1143 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1144 if (!remove && replied) return; /* replied already */
1146 data.count = 0;
1147 info->flags |= ISMEX_REPLIED;
1149 if (info->type == MSG_OTHER_PROCESS && !replied)
1151 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1152 info->msg.lParam, result, &data );
1155 SERVER_START_REQ( reply_message )
1157 req->result = result;
1158 req->remove = remove;
1159 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1160 wine_server_call( req );
1162 SERVER_END_REQ;
1166 /***********************************************************************
1167 * handle_internal_message
1169 * Handle an internal Wine message instead of calling the window proc.
1171 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1173 switch(msg)
1175 case WM_WINE_DESTROYWINDOW:
1176 return WIN_DestroyWindow( hwnd );
1177 case WM_WINE_SETWINDOWPOS:
1178 if (hwnd == GetDesktopWindow()) return 0;
1179 return USER_Driver->pSetWindowPos( (WINDOWPOS *)lparam );
1180 case WM_WINE_SHOWWINDOW:
1181 if (hwnd == GetDesktopWindow()) return 0;
1182 return ShowWindow( hwnd, wparam );
1183 case WM_WINE_SETPARENT:
1184 if (hwnd == GetDesktopWindow()) return 0;
1185 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1186 case WM_WINE_SETWINDOWLONG:
1187 return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1188 case WM_WINE_ENABLEWINDOW:
1189 if (hwnd == GetDesktopWindow()) return 0;
1190 return EnableWindow( hwnd, wparam );
1191 case WM_WINE_SETACTIVEWINDOW:
1192 if (hwnd == GetDesktopWindow()) return 0;
1193 return (LRESULT)SetActiveWindow( (HWND)wparam );
1194 case WM_WINE_KEYBOARD_LL_HOOK:
1195 return HOOK_CallHooks( WH_KEYBOARD_LL, HC_ACTION, wparam, lparam, TRUE );
1196 case WM_WINE_MOUSE_LL_HOOK:
1197 return HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, wparam, lparam, TRUE );
1198 default:
1199 if (msg >= WM_WINE_FIRST_DRIVER_MSG && msg <= WM_WINE_LAST_DRIVER_MSG)
1200 return USER_Driver->pWindowMessage( hwnd, msg, wparam, lparam );
1201 FIXME( "unknown internal message %x\n", msg );
1202 return 0;
1206 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1207 * to the memory handle, we keep track (in the server side) of all pairs of handle
1208 * used (the client passes its value and the content of the memory handle), and
1209 * the server stored both values (the client, and the local one, created after the
1210 * content). When a ACK message is generated, the list of pair is searched for a
1211 * matching pair, so that the client memory handle can be returned.
1213 struct DDE_pair {
1214 HGLOBAL client_hMem;
1215 HGLOBAL server_hMem;
1218 static struct DDE_pair* dde_pairs;
1219 static int dde_num_alloc;
1220 static int dde_num_used;
1222 static CRITICAL_SECTION dde_crst;
1223 static CRITICAL_SECTION_DEBUG critsect_debug =
1225 0, 0, &dde_crst,
1226 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
1227 0, 0, { (DWORD_PTR)(__FILE__ ": dde_crst") }
1229 static CRITICAL_SECTION dde_crst = { &critsect_debug, -1, 0, 0, 0, 0 };
1231 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1233 int i;
1234 #define GROWBY 4
1236 EnterCriticalSection(&dde_crst);
1238 /* now remember the pair of hMem on both sides */
1239 if (dde_num_used == dde_num_alloc)
1241 struct DDE_pair* tmp;
1242 if (dde_pairs)
1243 tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1244 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1245 else
1246 tmp = HeapAlloc( GetProcessHeap(), 0,
1247 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1249 if (!tmp)
1251 LeaveCriticalSection(&dde_crst);
1252 return FALSE;
1254 dde_pairs = tmp;
1255 /* zero out newly allocated part */
1256 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1257 dde_num_alloc += GROWBY;
1259 #undef GROWBY
1260 for (i = 0; i < dde_num_alloc; i++)
1262 if (dde_pairs[i].server_hMem == 0)
1264 dde_pairs[i].client_hMem = chm;
1265 dde_pairs[i].server_hMem = shm;
1266 dde_num_used++;
1267 break;
1270 LeaveCriticalSection(&dde_crst);
1271 return TRUE;
1274 static HGLOBAL dde_get_pair(HGLOBAL shm)
1276 int i;
1277 HGLOBAL ret = 0;
1279 EnterCriticalSection(&dde_crst);
1280 for (i = 0; i < dde_num_alloc; i++)
1282 if (dde_pairs[i].server_hMem == shm)
1284 /* free this pair */
1285 dde_pairs[i].server_hMem = 0;
1286 dde_num_used--;
1287 ret = dde_pairs[i].client_hMem;
1288 break;
1291 LeaveCriticalSection(&dde_crst);
1292 return ret;
1295 /***********************************************************************
1296 * post_dde_message
1298 * Post a DDE message
1300 static BOOL post_dde_message( struct packed_message *data, const struct send_message_info *info )
1302 void* ptr = NULL;
1303 int size = 0;
1304 UINT_PTR uiLo, uiHi;
1305 LPARAM lp = 0;
1306 HGLOBAL hunlock = 0;
1307 int i;
1308 DWORD res;
1310 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1311 return FALSE;
1313 lp = info->lparam;
1314 switch (info->msg)
1316 /* DDE messages which don't require packing are:
1317 * WM_DDE_INITIATE
1318 * WM_DDE_TERMINATE
1319 * WM_DDE_REQUEST
1320 * WM_DDE_UNADVISE
1322 case WM_DDE_ACK:
1323 if (HIWORD(uiHi))
1325 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1326 HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1327 if (h)
1329 /* send back the value of h on the other side */
1330 push_data( data, &h, sizeof(HGLOBAL) );
1331 lp = uiLo;
1332 TRACE( "send dde-ack %x %08x => %p\n", uiLo, uiHi, h );
1335 else
1337 /* uiHi should contain either an atom or 0 */
1338 TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
1339 lp = MAKELONG( uiLo, uiHi );
1341 break;
1342 case WM_DDE_ADVISE:
1343 case WM_DDE_DATA:
1344 case WM_DDE_POKE:
1345 size = 0;
1346 if (uiLo)
1348 size = GlobalSize( (HGLOBAL)uiLo ) ;
1349 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1350 (info->msg == WM_DDE_DATA && size < sizeof(DDEDATA)) ||
1351 (info->msg == WM_DDE_POKE && size < sizeof(DDEPOKE))
1353 return FALSE;
1355 else if (info->msg != WM_DDE_DATA) return FALSE;
1357 lp = uiHi;
1358 if (uiLo)
1360 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1362 DDEDATA *dde_data = (DDEDATA *)ptr;
1363 TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
1364 dde_data->unused, dde_data->fResponse, dde_data->fRelease,
1365 dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
1366 push_data( data, ptr, size );
1367 hunlock = (HGLOBAL)uiLo;
1370 TRACE( "send ddepack %u %x\n", size, uiHi );
1371 break;
1372 case WM_DDE_EXECUTE:
1373 if (info->lparam)
1375 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1377 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1378 /* so that the other side can send it back on ACK */
1379 lp = info->lparam;
1380 hunlock = (HGLOBAL)info->lparam;
1383 break;
1385 SERVER_START_REQ( send_message )
1387 req->id = info->dest_tid;
1388 req->type = info->type;
1389 req->flags = 0;
1390 req->win = info->hwnd;
1391 req->msg = info->msg;
1392 req->wparam = info->wparam;
1393 req->lparam = lp;
1394 req->time = GetCurrentTime();
1395 req->timeout = 0;
1396 for (i = 0; i < data->count; i++)
1397 wine_server_add_data( req, data->data[i], data->size[i] );
1398 if ((res = wine_server_call( req )))
1400 if (res == STATUS_INVALID_PARAMETER)
1401 /* FIXME: find a STATUS_ value for this one */
1402 SetLastError( ERROR_INVALID_THREAD_ID );
1403 else
1404 SetLastError( RtlNtStatusToDosError(res) );
1406 else
1407 FreeDDElParam(info->msg, info->lparam);
1409 SERVER_END_REQ;
1410 if (hunlock) GlobalUnlock(hunlock);
1412 return !res;
1415 /***********************************************************************
1416 * unpack_dde_message
1418 * Unpack a posted DDE message received from another process.
1420 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1421 void **buffer, size_t size )
1423 UINT_PTR uiLo, uiHi;
1424 HGLOBAL hMem = 0;
1425 void* ptr;
1427 switch (message)
1429 case WM_DDE_ACK:
1430 if (size)
1432 /* hMem is being passed */
1433 if (size != sizeof(HGLOBAL)) return FALSE;
1434 if (!buffer || !*buffer) return FALSE;
1435 uiLo = *lparam;
1436 memcpy( &hMem, *buffer, size );
1437 uiHi = (UINT_PTR)hMem;
1438 TRACE("recv dde-ack %x mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1440 else
1442 uiLo = LOWORD( *lparam );
1443 uiHi = HIWORD( *lparam );
1444 TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
1446 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1447 break;
1448 case WM_DDE_ADVISE:
1449 case WM_DDE_DATA:
1450 case WM_DDE_POKE:
1451 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1452 uiHi = *lparam;
1453 TRACE( "recv ddepack %u %x\n", size, uiHi );
1454 if (size)
1456 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size )))
1457 return FALSE;
1458 if ((ptr = GlobalLock( hMem )))
1460 memcpy( ptr, *buffer, size );
1461 GlobalUnlock( hMem );
1463 else
1465 GlobalFree( hMem );
1466 return FALSE;
1469 uiLo = (UINT_PTR)hMem;
1471 *lparam = PackDDElParam( message, uiLo, uiHi );
1472 break;
1473 case WM_DDE_EXECUTE:
1474 if (size)
1476 if (!buffer || !*buffer) return FALSE;
1477 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size ))) return FALSE;
1478 if ((ptr = GlobalLock( hMem )))
1480 memcpy( ptr, *buffer, size );
1481 GlobalUnlock( hMem );
1482 TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
1483 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1485 GlobalFree( hMem );
1486 return FALSE;
1489 else
1491 GlobalFree( hMem );
1492 return FALSE;
1494 } else return FALSE;
1495 *lparam = (LPARAM)hMem;
1496 break;
1498 return TRUE;
1501 /***********************************************************************
1502 * call_window_proc
1504 * Call a window procedure and the corresponding hooks.
1506 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1507 BOOL unicode, BOOL same_thread )
1509 struct user_thread_info *thread_info = get_user_thread_info();
1510 LRESULT result = 0;
1511 WNDPROC winproc;
1512 CWPSTRUCT cwp;
1513 CWPRETSTRUCT cwpret;
1515 if (thread_info->recursion_count > MAX_SENDMSG_RECURSION) return 0;
1516 thread_info->recursion_count++;
1518 if (msg & 0x80000000)
1520 result = handle_internal_message( hwnd, msg, wparam, lparam );
1521 goto done;
1524 /* first the WH_CALLWNDPROC hook */
1525 hwnd = WIN_GetFullHandle( hwnd );
1526 cwp.lParam = lparam;
1527 cwp.wParam = wparam;
1528 cwp.message = msg;
1529 cwp.hwnd = hwnd;
1530 HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1532 /* now call the window procedure */
1533 if (unicode)
1535 if (!(winproc = (WNDPROC)GetWindowLongPtrW( hwnd, GWLP_WNDPROC ))) goto done;
1536 result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1538 else
1540 if (!(winproc = (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ))) goto done;
1541 result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1544 /* and finally the WH_CALLWNDPROCRET hook */
1545 cwpret.lResult = result;
1546 cwpret.lParam = lparam;
1547 cwpret.wParam = wparam;
1548 cwpret.message = msg;
1549 cwpret.hwnd = hwnd;
1550 HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1551 done:
1552 thread_info->recursion_count--;
1553 return result;
1557 /***********************************************************************
1558 * send_parent_notify
1560 * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
1561 * the window has the WS_EX_NOPARENTNOTIFY style.
1563 static void send_parent_notify( HWND hwnd, WORD event, WORD idChild, POINT pt )
1565 /* pt has to be in the client coordinates of the parent window */
1566 MapWindowPoints( 0, hwnd, &pt, 1 );
1567 for (;;)
1569 HWND parent;
1571 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)) break;
1572 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
1573 if (!(parent = GetParent(hwnd))) break;
1574 if (parent == GetDesktopWindow()) break;
1575 MapWindowPoints( hwnd, parent, &pt, 1 );
1576 hwnd = parent;
1577 SendMessageW( hwnd, WM_PARENTNOTIFY,
1578 MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
1583 /***********************************************************************
1584 * accept_hardware_message
1586 * Tell the server we have passed the message to the app
1587 * (even though we may end up dropping it later on)
1589 static void accept_hardware_message( UINT hw_id, BOOL remove, HWND new_hwnd )
1591 SERVER_START_REQ( accept_hardware_message )
1593 req->hw_id = hw_id;
1594 req->remove = remove;
1595 req->new_win = new_hwnd;
1596 if (wine_server_call( req ))
1597 FIXME("Failed to reply to MSG_HARDWARE message. Message may not be removed from queue.\n");
1599 SERVER_END_REQ;
1603 /***********************************************************************
1604 * process_keyboard_message
1606 * returns TRUE if the contents of 'msg' should be passed to the application
1608 static BOOL process_keyboard_message( MSG *msg, UINT hw_id, HWND hwnd_filter,
1609 UINT first, UINT last, BOOL remove )
1611 EVENTMSG event;
1613 /* FIXME: is this really the right place for this hook? */
1614 event.message = msg->message;
1615 event.hwnd = msg->hwnd;
1616 event.time = msg->time;
1617 event.paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
1618 event.paramH = msg->lParam & 0x7FFF;
1619 if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
1620 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1622 /* check message filters */
1623 if (msg->message < first || msg->message > last) return FALSE;
1624 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1626 if (remove)
1628 /* Handle F1 key by sending out WM_HELP message */
1629 if ((msg->message == WM_KEYUP) &&
1630 (msg->wParam == VK_F1) &&
1631 (msg->hwnd != GetDesktopWindow()) &&
1632 !MENU_IsMenuActive())
1634 HELPINFO hi;
1635 hi.cbSize = sizeof(HELPINFO);
1636 hi.iContextType = HELPINFO_WINDOW;
1637 hi.iCtrlId = GetWindowLongPtrA( msg->hwnd, GWLP_ID );
1638 hi.hItemHandle = msg->hwnd;
1639 hi.dwContextId = GetWindowContextHelpId( msg->hwnd );
1640 hi.MousePos = msg->pt;
1641 SendMessageW( msg->hwnd, WM_HELP, 0, (LPARAM)&hi );
1645 if (HOOK_CallHooks( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
1646 LOWORD(msg->wParam), msg->lParam, TRUE ))
1648 /* skip this message */
1649 HOOK_CallHooks( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam, TRUE );
1650 accept_hardware_message( hw_id, TRUE, 0 );
1651 return FALSE;
1653 accept_hardware_message( hw_id, remove, 0 );
1654 return TRUE;
1658 /***********************************************************************
1659 * process_mouse_message
1661 * returns TRUE if the contents of 'msg' should be passed to the application
1663 static BOOL process_mouse_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
1664 UINT first, UINT last, BOOL remove )
1666 static MSG clk_msg;
1668 POINT pt;
1669 UINT message;
1670 INT hittest;
1671 EVENTMSG event;
1672 GUITHREADINFO info;
1673 MOUSEHOOKSTRUCT hook;
1674 BOOL eatMsg;
1676 /* find the window to dispatch this mouse message to */
1678 GetGUIThreadInfo( GetCurrentThreadId(), &info );
1679 if (info.hwndCapture)
1681 hittest = HTCLIENT;
1682 msg->hwnd = info.hwndCapture;
1684 else
1686 msg->hwnd = WINPOS_WindowFromPoint( msg->hwnd, msg->pt, &hittest );
1689 if (!msg->hwnd || !WIN_IsCurrentThread( msg->hwnd ))
1691 accept_hardware_message( hw_id, TRUE, msg->hwnd );
1692 return FALSE;
1695 /* FIXME: is this really the right place for this hook? */
1696 event.message = msg->message;
1697 event.time = msg->time;
1698 event.hwnd = msg->hwnd;
1699 event.paramL = msg->pt.x;
1700 event.paramH = msg->pt.y;
1701 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1703 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1705 pt = msg->pt;
1706 message = msg->message;
1707 /* Note: windows has no concept of a non-client wheel message */
1708 if (message != WM_MOUSEWHEEL)
1710 if (hittest != HTCLIENT)
1712 message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
1713 msg->wParam = hittest;
1715 else
1717 /* coordinates don't get translated while tracking a menu */
1718 /* FIXME: should differentiate popups and top-level menus */
1719 if (!(info.flags & GUI_INMENUMODE))
1720 ScreenToClient( msg->hwnd, &pt );
1723 msg->lParam = MAKELONG( pt.x, pt.y );
1725 /* translate double clicks */
1727 if ((msg->message == WM_LBUTTONDOWN) ||
1728 (msg->message == WM_RBUTTONDOWN) ||
1729 (msg->message == WM_MBUTTONDOWN) ||
1730 (msg->message == WM_XBUTTONDOWN))
1732 BOOL update = remove;
1734 /* translate double clicks -
1735 * note that ...MOUSEMOVEs can slip in between
1736 * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
1738 if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
1739 hittest != HTCLIENT ||
1740 (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
1742 if ((msg->message == clk_msg.message) &&
1743 (msg->hwnd == clk_msg.hwnd) &&
1744 (msg->wParam == clk_msg.wParam) &&
1745 (msg->time - clk_msg.time < GetDoubleClickTime()) &&
1746 (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
1747 (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
1749 message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
1750 if (update)
1752 clk_msg.message = 0; /* clear the double click conditions */
1753 update = FALSE;
1757 if (message < first || message > last) return FALSE;
1758 /* update static double click conditions */
1759 if (update) clk_msg = *msg;
1761 else
1763 if (message < first || message > last) return FALSE;
1766 /* message is accepted now (but may still get dropped) */
1768 hook.pt = msg->pt;
1769 hook.hwnd = msg->hwnd;
1770 hook.wHitTestCode = hittest;
1771 hook.dwExtraInfo = extra_info;
1772 if (HOOK_CallHooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
1773 message, (LPARAM)&hook, TRUE ))
1775 hook.pt = msg->pt;
1776 hook.hwnd = msg->hwnd;
1777 hook.wHitTestCode = hittest;
1778 hook.dwExtraInfo = extra_info;
1779 HOOK_CallHooks( WH_CBT, HCBT_CLICKSKIPPED, message, (LPARAM)&hook, TRUE );
1780 accept_hardware_message( hw_id, TRUE, 0 );
1781 return FALSE;
1784 if ((hittest == HTERROR) || (hittest == HTNOWHERE))
1786 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
1787 MAKELONG( hittest, msg->message ));
1788 accept_hardware_message( hw_id, TRUE, 0 );
1789 return FALSE;
1792 accept_hardware_message( hw_id, remove, 0 );
1794 if (!remove || info.hwndCapture)
1796 msg->message = message;
1797 return TRUE;
1800 eatMsg = FALSE;
1802 if ((msg->message == WM_LBUTTONDOWN) ||
1803 (msg->message == WM_RBUTTONDOWN) ||
1804 (msg->message == WM_MBUTTONDOWN) ||
1805 (msg->message == WM_XBUTTONDOWN))
1807 /* Send the WM_PARENTNOTIFY,
1808 * note that even for double/nonclient clicks
1809 * notification message is still WM_L/M/RBUTTONDOWN.
1811 send_parent_notify( msg->hwnd, msg->message, 0, msg->pt );
1813 /* Activate the window if needed */
1815 if (msg->hwnd != info.hwndActive)
1817 HWND hwndTop = msg->hwnd;
1818 while (hwndTop)
1820 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
1821 hwndTop = GetParent( hwndTop );
1824 if (hwndTop && hwndTop != GetDesktopWindow())
1826 LONG ret = SendMessageW( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
1827 MAKELONG( hittest, msg->message ) );
1828 switch(ret)
1830 case MA_NOACTIVATEANDEAT:
1831 eatMsg = TRUE;
1832 /* fall through */
1833 case MA_NOACTIVATE:
1834 break;
1835 case MA_ACTIVATEANDEAT:
1836 eatMsg = TRUE;
1837 /* fall through */
1838 case MA_ACTIVATE:
1839 case 0:
1840 if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
1841 break;
1842 default:
1843 WARN( "unknown WM_MOUSEACTIVATE code %ld\n", ret );
1844 break;
1850 /* send the WM_SETCURSOR message */
1852 /* Windows sends the normal mouse message as the message parameter
1853 in the WM_SETCURSOR message even if it's non-client mouse message */
1854 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd, MAKELONG( hittest, msg->message ));
1856 msg->message = message;
1857 return !eatMsg;
1861 /***********************************************************************
1862 * process_hardware_message
1864 * Process a hardware message; return TRUE if message should be passed on to the app
1866 static BOOL process_hardware_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
1867 UINT first, UINT last, BOOL remove )
1869 if (is_keyboard_message( msg->message ))
1870 return process_keyboard_message( msg, hw_id, hwnd_filter, first, last, remove );
1872 if (is_mouse_message( msg->message ))
1873 return process_mouse_message( msg, hw_id, extra_info, hwnd_filter, first, last, remove );
1875 ERR( "unknown message type %x\n", msg->message );
1876 return FALSE;
1880 /***********************************************************************
1881 * call_sendmsg_callback
1883 * Call the callback function of SendMessageCallback.
1885 static inline void call_sendmsg_callback( SENDASYNCPROC callback, HWND hwnd, UINT msg,
1886 ULONG_PTR data, LRESULT result )
1888 if (TRACE_ON(relay))
1889 DPRINTF( "%04lx:Call message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1890 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
1891 data, result );
1892 callback( hwnd, msg, data, result );
1893 if (TRACE_ON(relay))
1894 DPRINTF( "%04lx:Ret message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1895 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
1896 data, result );
1900 /***********************************************************************
1901 * get_hook_proc
1903 * Retrieve the hook procedure real value for a module-relative proc
1905 static void *get_hook_proc( void *proc, const WCHAR *module )
1907 HMODULE mod;
1909 if (!(mod = GetModuleHandleW(module)))
1911 TRACE( "loading %s\n", debugstr_w(module) );
1912 /* FIXME: the library will never be freed */
1913 if (!(mod = LoadLibraryW(module))) return NULL;
1915 return (char *)mod + (ULONG_PTR)proc;
1919 /***********************************************************************
1920 * peek_message
1922 * Peek for a message matching the given parameters. Return FALSE if none available.
1923 * All pending sent messages are processed before returning.
1925 static BOOL peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1927 LRESULT result;
1928 ULONG_PTR extra_info = 0;
1929 struct user_thread_info *thread_info = get_user_thread_info();
1930 struct received_message_info info, *old_info;
1931 unsigned int hw_id = 0; /* id of previous hardware message */
1933 if (!first && !last) last = ~0;
1935 for (;;)
1937 NTSTATUS res;
1938 void *buffer = NULL;
1939 size_t size = 0, buffer_size = 0;
1941 do /* loop while buffer is too small */
1943 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1944 return FALSE;
1945 SERVER_START_REQ( get_message )
1947 req->flags = flags;
1948 req->get_win = hwnd;
1949 req->get_first = first;
1950 req->get_last = last;
1951 req->hw_id = hw_id;
1952 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1953 if (!(res = wine_server_call( req )))
1955 size = wine_server_reply_size( reply );
1956 info.type = reply->type;
1957 info.msg.hwnd = reply->win;
1958 info.msg.message = reply->msg;
1959 info.msg.wParam = reply->wparam;
1960 info.msg.lParam = reply->lparam;
1961 info.msg.time = reply->time;
1962 info.msg.pt.x = reply->x;
1963 info.msg.pt.y = reply->y;
1964 info.hook = reply->hook;
1965 info.hook_proc = reply->hook_proc;
1966 hw_id = reply->hw_id;
1967 extra_info = reply->info;
1968 thread_info->active_hooks = reply->active_hooks;
1970 else
1972 HeapFree( GetProcessHeap(), 0, buffer );
1973 buffer_size = reply->total;
1976 SERVER_END_REQ;
1977 } while (res == STATUS_BUFFER_OVERFLOW);
1979 if (res) return FALSE;
1981 TRACE( "got type %d msg %x (%s) hwnd %p wp %x lp %lx\n",
1982 info.type, info.msg.message,
1983 (info.type == MSG_WINEVENT) ? "MSG_WINEVENT" : SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1984 info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1986 switch(info.type)
1988 case MSG_ASCII:
1989 case MSG_UNICODE:
1990 info.flags = ISMEX_SEND;
1991 break;
1992 case MSG_NOTIFY:
1993 info.flags = ISMEX_NOTIFY;
1994 break;
1995 case MSG_CALLBACK:
1996 info.flags = ISMEX_CALLBACK;
1997 break;
1998 case MSG_CALLBACK_RESULT:
1999 call_sendmsg_callback( (SENDASYNCPROC)info.msg.wParam, info.msg.hwnd,
2000 info.msg.message, extra_info, info.msg.lParam );
2001 goto next;
2002 case MSG_WINEVENT:
2003 if (size)
2005 WCHAR module[MAX_PATH];
2006 size = min( size, (MAX_PATH - 1) * sizeof(WCHAR) );
2007 memcpy( module, buffer, size );
2008 module[size / sizeof(WCHAR)] = 0;
2009 if (!(info.hook_proc = get_hook_proc( info.hook_proc, module )))
2011 ERR( "invalid winevent hook module name %s\n", debugstr_w(module) );
2012 goto next;
2015 if (TRACE_ON(relay))
2016 DPRINTF( "%04lx:Call winevent proc %p (hook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%lx,tid=%04lx,time=%lx)\n",
2017 GetCurrentThreadId(), info.hook_proc,
2018 info.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2019 info.msg.lParam, extra_info, info.msg.time);
2021 info.hook_proc( info.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2022 info.msg.lParam, extra_info, info.msg.time );
2024 if (TRACE_ON(relay))
2025 DPRINTF( "%04lx:Ret winevent proc %p (hook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%lx,tid=%04lx,time=%lx)\n",
2026 GetCurrentThreadId(), info.hook_proc,
2027 info.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2028 info.msg.lParam, extra_info, info.msg.time);
2029 goto next;
2030 case MSG_OTHER_PROCESS:
2031 info.flags = ISMEX_SEND;
2032 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2033 &info.msg.lParam, &buffer, size ))
2035 ERR( "invalid packed message %x (%s) hwnd %p wp %x lp %lx size %d\n",
2036 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
2037 info.msg.wParam, info.msg.lParam, size );
2038 /* ignore it */
2039 reply_message( &info, 0, TRUE );
2040 goto next;
2042 break;
2043 case MSG_HARDWARE:
2044 if (!process_hardware_message( &info.msg, hw_id, extra_info,
2045 hwnd, first, last, flags & GET_MSG_REMOVE ))
2047 TRACE("dropping msg %x\n", info.msg.message );
2048 goto next; /* ignore it */
2050 thread_info->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
2051 /* fall through */
2052 case MSG_POSTED:
2053 thread_info->GetMessageExtraInfoVal = extra_info;
2054 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
2056 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2057 &info.msg.lParam, &buffer, size ))
2059 ERR( "invalid packed dde-message %x (%s) hwnd %p wp %x lp %lx size %d\n",
2060 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
2061 info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
2062 goto next; /* ignore it */
2065 *msg = info.msg;
2066 HeapFree( GetProcessHeap(), 0, buffer );
2067 return TRUE;
2070 /* if we get here, we have a sent message; call the window procedure */
2071 old_info = thread_info->receive_info;
2072 thread_info->receive_info = &info;
2073 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
2074 info.msg.lParam, (info.type != MSG_ASCII), FALSE );
2075 reply_message( &info, result, TRUE );
2076 thread_info->receive_info = old_info;
2077 next:
2078 HeapFree( GetProcessHeap(), 0, buffer );
2083 /***********************************************************************
2084 * process_sent_messages
2086 * Process all pending sent messages.
2088 inline static void process_sent_messages(void)
2090 MSG msg;
2091 peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
2095 /***********************************************************************
2096 * get_server_queue_handle
2098 * Get a handle to the server message queue for the current thread.
2100 static HANDLE get_server_queue_handle(void)
2102 struct user_thread_info *thread_info = get_user_thread_info();
2103 HANDLE ret;
2105 if (!(ret = thread_info->server_queue))
2107 SERVER_START_REQ( get_msg_queue )
2109 wine_server_call( req );
2110 ret = reply->handle;
2112 SERVER_END_REQ;
2113 thread_info->server_queue = ret;
2114 if (!ret) ERR( "Cannot get server thread queue\n" );
2116 return ret;
2120 /***********************************************************************
2121 * wait_message_reply
2123 * Wait until a sent message gets replied to.
2125 static void wait_message_reply( UINT flags )
2127 HANDLE server_queue = get_server_queue_handle();
2129 for (;;)
2131 unsigned int wake_bits = 0, changed_bits = 0;
2132 DWORD dwlc, res;
2134 SERVER_START_REQ( set_queue_mask )
2136 req->wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
2137 req->changed_mask = req->wake_mask;
2138 req->skip_wait = 1;
2139 if (!wine_server_call( req ))
2141 wake_bits = reply->wake_bits;
2142 changed_bits = reply->changed_bits;
2145 SERVER_END_REQ;
2147 if (wake_bits & QS_SMRESULT) return; /* got a result */
2148 if (wake_bits & QS_SENDMESSAGE)
2150 /* Process the sent message immediately */
2151 process_sent_messages();
2152 continue;
2155 /* now wait for it */
2157 ReleaseThunkLock( &dwlc );
2158 res = USER_Driver->pMsgWaitForMultipleObjectsEx( 1, &server_queue,
2159 INFINITE, QS_SENDMESSAGE, 0 );
2160 if (dwlc) RestoreThunkLock( dwlc );
2164 /***********************************************************************
2165 * put_message_in_queue
2167 * Put a sent message into the destination queue.
2168 * For inter-process message, reply_size is set to expected size of reply data.
2170 static BOOL put_message_in_queue( const struct send_message_info *info, size_t *reply_size )
2172 struct packed_message data;
2173 unsigned int res;
2174 int i, timeout = 0;
2176 /* Check for INFINITE timeout for compatibility with Win9x,
2177 * although Windows >= NT does not do so
2179 if (info->type != MSG_NOTIFY &&
2180 info->type != MSG_CALLBACK &&
2181 info->type != MSG_POSTED &&
2182 info->timeout != INFINITE)
2183 timeout = info->timeout;
2185 data.count = 0;
2186 if (info->type == MSG_OTHER_PROCESS)
2188 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
2189 if (data.count == -1)
2191 WARN( "cannot pack message %x\n", info->msg );
2192 return FALSE;
2195 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
2197 return post_dde_message( &data, info );
2200 SERVER_START_REQ( send_message )
2202 req->id = info->dest_tid;
2203 req->type = info->type;
2204 req->flags = 0;
2205 req->win = info->hwnd;
2206 req->msg = info->msg;
2207 req->wparam = info->wparam;
2208 req->lparam = info->lparam;
2209 req->time = GetCurrentTime();
2210 req->timeout = timeout;
2212 if (info->type == MSG_CALLBACK)
2214 req->callback = info->callback;
2215 req->info = info->data;
2218 if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
2219 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
2220 if ((res = wine_server_call( req )))
2222 if (res == STATUS_INVALID_PARAMETER)
2223 /* FIXME: find a STATUS_ value for this one */
2224 SetLastError( ERROR_INVALID_THREAD_ID );
2225 else
2226 SetLastError( RtlNtStatusToDosError(res) );
2229 SERVER_END_REQ;
2230 return !res;
2234 /***********************************************************************
2235 * retrieve_reply
2237 * Retrieve a message reply from the server.
2239 static LRESULT retrieve_reply( const struct send_message_info *info,
2240 size_t reply_size, LRESULT *result )
2242 NTSTATUS status;
2243 void *reply_data = NULL;
2245 if (reply_size)
2247 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
2249 WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
2250 reply_size = 0;
2253 SERVER_START_REQ( get_message_reply )
2255 req->cancel = 1;
2256 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
2257 if (!(status = wine_server_call( req ))) *result = reply->result;
2258 reply_size = wine_server_reply_size( reply );
2260 SERVER_END_REQ;
2261 if (!status && reply_size)
2262 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
2264 HeapFree( GetProcessHeap(), 0, reply_data );
2266 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
2267 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
2268 info->lparam, *result, status );
2270 /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
2271 if (status) SetLastError( RtlNtStatusToDosError(status) );
2272 return !status;
2276 /***********************************************************************
2277 * send_inter_thread_message
2279 static LRESULT send_inter_thread_message( const struct send_message_info *info, LRESULT *res_ptr )
2281 size_t reply_size = 0;
2283 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
2284 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
2286 USER_CheckNotLock();
2288 if (!put_message_in_queue( info, &reply_size )) return 0;
2290 /* there's no reply to wait for on notify/callback messages */
2291 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
2293 wait_message_reply( info->flags );
2294 return retrieve_reply( info, reply_size, res_ptr );
2298 /***********************************************************************
2299 * MSG_SendInternalMessageTimeout
2301 * Same as SendMessageTimeoutW but sends the message to a specific thread
2302 * without requiring a window handle. Only works for internal Wine messages.
2304 LRESULT MSG_SendInternalMessageTimeout( DWORD dest_pid, DWORD dest_tid,
2305 UINT msg, WPARAM wparam, LPARAM lparam,
2306 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2308 struct send_message_info info;
2309 LRESULT ret, result;
2311 assert( msg & 0x80000000 ); /* must be an internal Wine message */
2313 info.type = MSG_UNICODE;
2314 info.dest_tid = dest_tid;
2315 info.hwnd = 0;
2316 info.msg = msg;
2317 info.wparam = wparam;
2318 info.lparam = lparam;
2319 info.flags = flags;
2320 info.timeout = timeout;
2322 if (USER_IsExitingThread( dest_tid )) return 0;
2324 if (dest_tid == GetCurrentThreadId())
2326 result = handle_internal_message( 0, msg, wparam, lparam );
2327 ret = 1;
2329 else
2331 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
2332 ret = send_inter_thread_message( &info, &result );
2334 if (ret && res_ptr) *res_ptr = result;
2335 return ret;
2339 /***********************************************************************
2340 * SendMessageTimeoutW (USER32.@)
2342 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2343 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2345 struct send_message_info info;
2346 DWORD dest_pid;
2347 LRESULT ret, result;
2349 info.type = MSG_UNICODE;
2350 info.hwnd = hwnd;
2351 info.msg = msg;
2352 info.wparam = wparam;
2353 info.lparam = lparam;
2354 info.flags = flags;
2355 info.timeout = timeout;
2357 if (is_broadcast(hwnd))
2359 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2360 if (res_ptr) *res_ptr = 1;
2361 return 1;
2364 if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
2366 if (USER_IsExitingThread( info.dest_tid )) return 0;
2368 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
2370 if (info.dest_tid == GetCurrentThreadId())
2372 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2373 ret = 1;
2375 else
2377 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
2378 ret = send_inter_thread_message( &info, &result );
2381 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
2382 if (ret && res_ptr) *res_ptr = result;
2383 return ret;
2386 static LRESULT send_inter_thread_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
2387 LRESULT *result, void *arg )
2389 struct send_message_info *info = arg;
2390 info->hwnd = hwnd;
2391 info->msg = msg;
2392 info->wparam = wp;
2393 info->lparam = lp;
2394 return send_inter_thread_message( info, result );
2397 /***********************************************************************
2398 * SendMessageTimeoutA (USER32.@)
2400 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2401 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2403 struct send_message_info info;
2404 DWORD dest_pid;
2405 LRESULT ret, result;
2407 info.type = MSG_ASCII;
2408 info.hwnd = hwnd;
2409 info.msg = msg;
2410 info.wparam = wparam;
2411 info.lparam = lparam;
2412 info.flags = flags;
2413 info.timeout = timeout;
2415 if (is_broadcast(hwnd))
2417 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2418 if (res_ptr) *res_ptr = 1;
2419 return 1;
2422 if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
2424 if (USER_IsExitingThread( info.dest_tid )) return 0;
2426 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
2428 if (info.dest_tid == GetCurrentThreadId())
2430 result = call_window_proc( hwnd, msg, wparam, lparam, FALSE, TRUE );
2431 ret = 1;
2433 else if (dest_pid == GetCurrentProcessId())
2435 ret = send_inter_thread_message( &info, &result );
2437 else
2439 /* inter-process message: need to map to Unicode */
2440 info.type = MSG_OTHER_PROCESS;
2441 if (is_unicode_message( info.msg ))
2442 ret = WINPROC_CallProcAtoW( send_inter_thread_callback, info.hwnd, info.msg,
2443 info.wparam, info.lparam, &result, &info );
2444 else ret = send_inter_thread_message( &info, &result );
2446 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
2447 if (ret && res_ptr) *res_ptr = result;
2448 return ret;
2452 /***********************************************************************
2453 * SendMessageW (USER32.@)
2455 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2457 DWORD_PTR res = 0;
2458 SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, 0, &res );
2459 return res;
2463 /***********************************************************************
2464 * SendMessageA (USER32.@)
2466 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2468 DWORD_PTR res = 0;
2469 SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, 0, &res );
2470 return res;
2474 /***********************************************************************
2475 * SendNotifyMessageA (USER32.@)
2477 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2479 return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
2483 /***********************************************************************
2484 * SendNotifyMessageW (USER32.@)
2486 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2488 struct send_message_info info;
2489 LRESULT result;
2491 if (is_pointer_message(msg))
2493 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2494 return FALSE;
2497 info.type = MSG_NOTIFY;
2498 info.hwnd = hwnd;
2499 info.msg = msg;
2500 info.wparam = wparam;
2501 info.lparam = lparam;
2502 info.flags = 0;
2504 if (is_broadcast(hwnd))
2506 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2507 return TRUE;
2510 if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2512 if (USER_IsExitingThread( info.dest_tid )) return TRUE;
2514 if (info.dest_tid == GetCurrentThreadId())
2516 call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2517 return TRUE;
2519 return send_inter_thread_message( &info, &result );
2523 /***********************************************************************
2524 * SendMessageCallbackA (USER32.@)
2526 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2527 SENDASYNCPROC callback, ULONG_PTR data )
2529 return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
2530 lparam, callback, data );
2534 /***********************************************************************
2535 * SendMessageCallbackW (USER32.@)
2537 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2538 SENDASYNCPROC callback, ULONG_PTR data )
2540 struct send_message_info info;
2541 LRESULT result;
2543 if (is_pointer_message(msg))
2545 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2546 return FALSE;
2549 info.type = MSG_CALLBACK;
2550 info.hwnd = hwnd;
2551 info.msg = msg;
2552 info.wparam = wparam;
2553 info.lparam = lparam;
2554 info.callback = callback;
2555 info.data = data;
2556 info.flags = 0;
2558 if (is_broadcast(hwnd))
2560 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2561 return TRUE;
2564 if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2566 if (USER_IsExitingThread( info.dest_tid )) return TRUE;
2568 if (info.dest_tid == GetCurrentThreadId())
2570 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2571 call_sendmsg_callback( callback, hwnd, msg, data, result );
2572 return TRUE;
2574 FIXME( "callback will not be called\n" );
2575 return send_inter_thread_message( &info, &result );
2579 /***********************************************************************
2580 * ReplyMessage (USER32.@)
2582 BOOL WINAPI ReplyMessage( LRESULT result )
2584 struct received_message_info *info = get_user_thread_info()->receive_info;
2586 if (!info) return FALSE;
2587 reply_message( info, result, FALSE );
2588 return TRUE;
2592 /***********************************************************************
2593 * InSendMessage (USER32.@)
2595 BOOL WINAPI InSendMessage(void)
2597 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
2601 /***********************************************************************
2602 * InSendMessageEx (USER32.@)
2604 DWORD WINAPI InSendMessageEx( LPVOID reserved )
2606 struct received_message_info *info = get_user_thread_info()->receive_info;
2608 if (info) return info->flags;
2609 return ISMEX_NOSEND;
2613 /***********************************************************************
2614 * PostMessageA (USER32.@)
2616 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2618 return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
2622 /***********************************************************************
2623 * PostMessageW (USER32.@)
2625 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2627 struct send_message_info info;
2629 if (is_pointer_message( msg ))
2631 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2632 return FALSE;
2635 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
2636 hwnd, msg, SPY_GetMsgName(msg, hwnd), wparam, lparam );
2638 info.type = MSG_POSTED;
2639 info.hwnd = hwnd;
2640 info.msg = msg;
2641 info.wparam = wparam;
2642 info.lparam = lparam;
2643 info.flags = 0;
2645 if (is_broadcast(hwnd))
2647 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2648 return TRUE;
2651 if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2653 if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2655 if (USER_IsExitingThread( info.dest_tid )) return TRUE;
2657 return put_message_in_queue( &info, NULL );
2661 /**********************************************************************
2662 * PostThreadMessageA (USER32.@)
2664 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2666 return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2670 /**********************************************************************
2671 * PostThreadMessageW (USER32.@)
2673 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2675 struct send_message_info info;
2677 if (is_pointer_message( msg ))
2679 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2680 return FALSE;
2682 if (USER_IsExitingThread( thread )) return TRUE;
2684 info.type = MSG_POSTED;
2685 info.dest_tid = thread;
2686 info.hwnd = 0;
2687 info.msg = msg;
2688 info.wparam = wparam;
2689 info.lparam = lparam;
2690 info.flags = 0;
2691 return put_message_in_queue( &info, NULL );
2695 /***********************************************************************
2696 * PostQuitMessage (USER32.@)
2698 * Posts a quit message to the current thread's message queue.
2700 * PARAMS
2701 * exit_code [I] Exit code to return from message loop.
2703 * RETURNS
2704 * Nothing.
2706 * NOTES
2707 * This function is not the same as calling:
2708 *|PostThreadMessage(GetCurrentThreadId(), WM_QUIT, exit_code, 0);
2709 * It instead sets a flag in the message queue that signals it to generate
2710 * a WM_QUIT message when there are no other pending sent or posted messages
2711 * in the queue.
2713 void WINAPI PostQuitMessage( INT exit_code )
2715 SERVER_START_REQ( post_quit_message )
2717 req->exit_code = exit_code;
2718 wine_server_call( req );
2720 SERVER_END_REQ;
2724 /***********************************************************************
2725 * PeekMessageW (USER32.@)
2727 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2729 struct user_thread_info *thread_info = get_user_thread_info();
2730 MSG msg;
2732 if (HIWORD(flags))
2733 FIXME("PM_QS_xxxx flags (%04x) are not handled\n", flags >> 16);
2735 USER_CheckNotLock();
2737 /* check for graphics events */
2738 USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
2740 hwnd = WIN_GetFullHandle( hwnd );
2742 for (;;)
2744 if (!peek_message( &msg, hwnd, first, last, (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2746 if (!(flags & PM_NOYIELD))
2748 DWORD count;
2749 ReleaseThunkLock(&count);
2750 NtYieldExecution();
2751 if (count) RestoreThunkLock(count);
2753 return FALSE;
2755 if (msg.message & 0x80000000)
2757 if (!(flags & PM_REMOVE))
2759 /* Have to remove the message explicitly.
2760 Do this before handling it, because the message handler may
2761 call PeekMessage again */
2762 peek_message( &msg, msg.hwnd, msg.message, msg.message, GET_MSG_REMOVE );
2764 handle_internal_message( msg.hwnd, msg.message, msg.wParam, msg.lParam );
2766 else break;
2769 thread_info->GetMessageTimeVal = msg.time;
2770 msg.pt.x = LOWORD( thread_info->GetMessagePosVal );
2771 msg.pt.y = HIWORD( thread_info->GetMessagePosVal );
2773 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
2775 /* copy back our internal safe copy of message data to msg_out.
2776 * msg_out is a variable from the *program*, so it can't be used
2777 * internally as it can get "corrupted" by our use of SendMessage()
2778 * (back to the program) inside the message handling itself. */
2779 if (!msg_out)
2781 SetLastError( ERROR_NOACCESS );
2782 return FALSE;
2784 *msg_out = msg;
2785 return TRUE;
2789 /***********************************************************************
2790 * PeekMessageA (USER32.@)
2792 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2794 BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2795 if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2796 return ret;
2800 /***********************************************************************
2801 * GetMessageW (USER32.@)
2803 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2805 HANDLE server_queue = get_server_queue_handle();
2806 int mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2808 if (first || last)
2810 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2811 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2812 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2813 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2814 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2815 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2817 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2819 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2821 /* wait until one of the bits is set */
2822 unsigned int wake_bits = 0, changed_bits = 0;
2823 DWORD dwlc;
2825 SERVER_START_REQ( set_queue_mask )
2827 req->wake_mask = QS_SENDMESSAGE;
2828 req->changed_mask = mask;
2829 req->skip_wait = 1;
2830 if (!wine_server_call( req ))
2832 wake_bits = reply->wake_bits;
2833 changed_bits = reply->changed_bits;
2836 SERVER_END_REQ;
2838 if (changed_bits & mask) continue;
2839 if (wake_bits & QS_SENDMESSAGE) continue;
2841 TRACE( "(%04lx) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2842 GetCurrentThreadId(), mask, wake_bits, changed_bits );
2844 ReleaseThunkLock( &dwlc );
2845 USER_Driver->pMsgWaitForMultipleObjectsEx( 1, &server_queue, INFINITE, QS_ALLINPUT, 0 );
2846 if (dwlc) RestoreThunkLock( dwlc );
2849 return (msg->message != WM_QUIT);
2853 /***********************************************************************
2854 * GetMessageA (USER32.@)
2856 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2858 GetMessageW( msg, hwnd, first, last );
2859 msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2860 return (msg->message != WM_QUIT);
2864 /***********************************************************************
2865 * IsDialogMessageA (USER32.@)
2866 * IsDialogMessage (USER32.@)
2868 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2870 MSG msg = *pmsg;
2871 msg.wParam = map_wparam_AtoW( msg.message, msg.wParam );
2872 return IsDialogMessageW( hwndDlg, &msg );
2876 /***********************************************************************
2877 * TranslateMessage (USER32.@)
2879 * Implementation of TranslateMessage.
2881 * TranslateMessage translates virtual-key messages into character-messages,
2882 * as follows :
2883 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
2884 * ditto replacing WM_* with WM_SYS*
2885 * This produces WM_CHAR messages only for keys mapped to ASCII characters
2886 * by the keyboard driver.
2888 * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
2889 * return value is nonzero, regardless of the translation.
2892 BOOL WINAPI TranslateMessage( const MSG *msg )
2894 UINT message;
2895 WCHAR wp[2];
2896 BYTE state[256];
2898 if (msg->message < WM_KEYFIRST || msg->message > WM_KEYLAST) return FALSE;
2899 if (msg->message != WM_KEYDOWN && msg->message != WM_SYSKEYDOWN) return TRUE;
2901 TRACE_(key)("Translating key %s (%04x), scancode %02x\n",
2902 SPY_GetVKeyName(msg->wParam), msg->wParam, LOBYTE(HIWORD(msg->lParam)));
2904 GetKeyboardState( state );
2905 /* FIXME : should handle ToUnicode yielding 2 */
2906 switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), state, wp, 2, 0))
2908 case 1:
2909 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
2910 TRACE_(key)("1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
2911 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
2912 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
2913 break;
2915 case -1:
2916 message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
2917 TRACE_(key)("-1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
2918 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
2919 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
2920 break;
2922 return TRUE;
2926 /***********************************************************************
2927 * DispatchMessageA (USER32.@)
2929 * See DispatchMessageW.
2931 LONG WINAPI DispatchMessageA( const MSG* msg )
2933 WND * wndPtr;
2934 LONG retval;
2935 WNDPROC winproc;
2937 /* Process timer messages */
2938 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
2940 if (msg->lParam) return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
2941 msg->message, msg->wParam, GetTickCount() );
2944 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
2946 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2947 return 0;
2949 if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP)
2951 if (IsWindow( msg->hwnd )) SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2952 else SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2953 return 0;
2955 if (wndPtr->tid != GetCurrentThreadId())
2957 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2958 WIN_ReleasePtr( wndPtr );
2959 return 0;
2961 winproc = wndPtr->winproc;
2962 WIN_ReleasePtr( wndPtr );
2964 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
2965 msg->wParam, msg->lParam );
2966 retval = CallWindowProcA( winproc, msg->hwnd, msg->message,
2967 msg->wParam, msg->lParam );
2968 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
2969 msg->wParam, msg->lParam );
2971 if (msg->message == WM_PAINT)
2973 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
2974 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
2975 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
2976 DeleteObject( hrgn );
2978 return retval;
2982 /***********************************************************************
2983 * DispatchMessageW (USER32.@) Process a message
2985 * Process the message specified in the structure *_msg_.
2987 * If the lpMsg parameter points to a WM_TIMER message and the
2988 * parameter of the WM_TIMER message is not NULL, the lParam parameter
2989 * points to the function that is called instead of the window
2990 * procedure.
2992 * The message must be valid.
2994 * RETURNS
2996 * DispatchMessage() returns the result of the window procedure invoked.
2998 * CONFORMANCE
3000 * ECMA-234, Win32
3003 LONG WINAPI DispatchMessageW( const MSG* msg )
3005 WND * wndPtr;
3006 LONG retval;
3007 WNDPROC winproc;
3009 /* Process timer messages */
3010 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3012 if (msg->lParam) return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
3013 msg->message, msg->wParam, GetTickCount() );
3016 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
3018 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3019 return 0;
3021 if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP)
3023 if (IsWindow( msg->hwnd )) SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3024 else SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3025 return 0;
3027 if (wndPtr->tid != GetCurrentThreadId())
3029 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3030 WIN_ReleasePtr( wndPtr );
3031 return 0;
3033 winproc = wndPtr->winproc;
3034 WIN_ReleasePtr( wndPtr );
3036 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3037 msg->wParam, msg->lParam );
3038 retval = CallWindowProcW( winproc, msg->hwnd, msg->message,
3039 msg->wParam, msg->lParam );
3040 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3041 msg->wParam, msg->lParam );
3043 if (msg->message == WM_PAINT)
3045 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3046 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3047 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3048 DeleteObject( hrgn );
3050 return retval;
3054 /***********************************************************************
3055 * GetMessagePos (USER.119)
3056 * GetMessagePos (USER32.@)
3058 * The GetMessagePos() function returns a long value representing a
3059 * cursor position, in screen coordinates, when the last message
3060 * retrieved by the GetMessage() function occurs. The x-coordinate is
3061 * in the low-order word of the return value, the y-coordinate is in
3062 * the high-order word. The application can use the MAKEPOINT()
3063 * macro to obtain a POINT structure from the return value.
3065 * For the current cursor position, use GetCursorPos().
3067 * RETURNS
3069 * Cursor position of last message on success, zero on failure.
3071 * CONFORMANCE
3073 * ECMA-234, Win32
3076 DWORD WINAPI GetMessagePos(void)
3078 return get_user_thread_info()->GetMessagePosVal;
3082 /***********************************************************************
3083 * GetMessageTime (USER.120)
3084 * GetMessageTime (USER32.@)
3086 * GetMessageTime() returns the message time for the last message
3087 * retrieved by the function. The time is measured in milliseconds with
3088 * the same offset as GetTickCount().
3090 * Since the tick count wraps, this is only useful for moderately short
3091 * relative time comparisons.
3093 * RETURNS
3095 * Time of last message on success, zero on failure.
3097 LONG WINAPI GetMessageTime(void)
3099 return get_user_thread_info()->GetMessageTimeVal;
3103 /***********************************************************************
3104 * GetMessageExtraInfo (USER.288)
3105 * GetMessageExtraInfo (USER32.@)
3107 LPARAM WINAPI GetMessageExtraInfo(void)
3109 return get_user_thread_info()->GetMessageExtraInfoVal;
3113 /***********************************************************************
3114 * SetMessageExtraInfo (USER32.@)
3116 LPARAM WINAPI SetMessageExtraInfo(LPARAM lParam)
3118 struct user_thread_info *thread_info = get_user_thread_info();
3119 LONG old_value = thread_info->GetMessageExtraInfoVal;
3120 thread_info->GetMessageExtraInfoVal = lParam;
3121 return old_value;
3125 /***********************************************************************
3126 * WaitMessage (USER.112) Suspend thread pending messages
3127 * WaitMessage (USER32.@) Suspend thread pending messages
3129 * WaitMessage() suspends a thread until events appear in the thread's
3130 * queue.
3132 BOOL WINAPI WaitMessage(void)
3134 return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
3138 /***********************************************************************
3139 * MsgWaitForMultipleObjectsEx (USER32.@)
3141 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
3142 DWORD timeout, DWORD mask, DWORD flags )
3144 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
3145 DWORD i, ret, lock;
3147 if (count > MAXIMUM_WAIT_OBJECTS-1)
3149 SetLastError( ERROR_INVALID_PARAMETER );
3150 return WAIT_FAILED;
3153 /* set the queue mask */
3154 SERVER_START_REQ( set_queue_mask )
3156 req->wake_mask = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
3157 req->changed_mask = mask;
3158 req->skip_wait = 0;
3159 wine_server_call( req );
3161 SERVER_END_REQ;
3163 /* Add the thread event to the handle list */
3164 for (i = 0; i < count; i++) handles[i] = pHandles[i];
3165 handles[count] = get_server_queue_handle();
3167 ReleaseThunkLock( &lock );
3168 ret = USER_Driver->pMsgWaitForMultipleObjectsEx( count+1, handles, timeout, mask, flags );
3169 if (ret == count+1) ret = count; /* pretend the msg queue is ready */
3170 if (lock) RestoreThunkLock( lock );
3171 return ret;
3175 /***********************************************************************
3176 * MsgWaitForMultipleObjects (USER32.@)
3178 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
3179 BOOL wait_all, DWORD timeout, DWORD mask )
3181 return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
3182 wait_all ? MWMO_WAITALL : 0 );
3186 /***********************************************************************
3187 * WaitForInputIdle (USER32.@)
3189 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
3191 DWORD start_time, elapsed, ret;
3192 HANDLE idle_event = (HANDLE)-1;
3194 SERVER_START_REQ( wait_input_idle )
3196 req->handle = hProcess;
3197 req->timeout = dwTimeOut;
3198 if (!(ret = wine_server_call_err( req ))) idle_event = reply->event;
3200 SERVER_END_REQ;
3201 if (ret) return WAIT_FAILED; /* error */
3202 if (!idle_event) return 0; /* no event to wait on */
3204 start_time = GetTickCount();
3205 elapsed = 0;
3207 TRACE("waiting for %p\n", idle_event );
3210 ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
3211 switch (ret)
3213 case WAIT_OBJECT_0+1:
3214 process_sent_messages();
3215 break;
3216 case WAIT_TIMEOUT:
3217 case WAIT_FAILED:
3218 TRACE("timeout or error\n");
3219 return ret;
3220 default:
3221 TRACE("finished\n");
3222 return 0;
3224 if (dwTimeOut != INFINITE)
3226 elapsed = GetTickCount() - start_time;
3227 if (elapsed > dwTimeOut)
3228 break;
3231 while (1);
3233 return WAIT_TIMEOUT;
3237 /***********************************************************************
3238 * UserYield (USER.332)
3240 void WINAPI UserYield16(void)
3242 DWORD count;
3244 /* Handle sent messages */
3245 process_sent_messages();
3247 /* Yield */
3248 ReleaseThunkLock(&count);
3250 if (count)
3252 RestoreThunkLock(count);
3253 /* Handle sent messages again */
3254 process_sent_messages();
3259 /***********************************************************************
3260 * RegisterWindowMessageA (USER32.@)
3261 * RegisterWindowMessage (USER.118)
3263 UINT WINAPI RegisterWindowMessageA( LPCSTR str )
3265 UINT ret = GlobalAddAtomA(str);
3266 TRACE("%s, ret=%x\n", str, ret);
3267 return ret;
3271 /***********************************************************************
3272 * RegisterWindowMessageW (USER32.@)
3274 UINT WINAPI RegisterWindowMessageW( LPCWSTR str )
3276 UINT ret = GlobalAddAtomW(str);
3277 TRACE("%s ret=%x\n", debugstr_w(str), ret);
3278 return ret;
3282 /***********************************************************************
3283 * BroadcastSystemMessageA (USER32.@)
3284 * BroadcastSystemMessage (USER32.@)
3286 LONG WINAPI BroadcastSystemMessageA( 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 PostMessageA( 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 * BroadcastSystemMessageW (USER32.@)
3305 LONG WINAPI BroadcastSystemMessageW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
3307 if ((*recipients & BSM_APPLICATIONS) || (*recipients == BSM_ALLCOMPONENTS))
3309 FIXME( "(%08lx,%08lx,%08x,%08x,%08lx): semi-stub!\n", flags, *recipients, msg, wp, lp );
3310 PostMessageW( HWND_BROADCAST, msg, wp, lp );
3311 return 1;
3313 else
3315 FIXME( "(%08lx,%08lx,%08x,%08x,%08lx): stub!\n", flags, *recipients, msg, wp, lp );
3316 return -1;
3321 /***********************************************************************
3322 * SetMessageQueue (USER32.@)
3324 BOOL WINAPI SetMessageQueue( INT size )
3326 /* now obsolete the message queue will be expanded dynamically as necessary */
3327 return TRUE;
3331 /***********************************************************************
3332 * MessageBeep (USER32.@)
3334 BOOL WINAPI MessageBeep( UINT i )
3336 BOOL active = TRUE;
3337 SystemParametersInfoA( SPI_GETBEEP, 0, &active, FALSE );
3338 if (active) USER_Driver->pBeep();
3339 return TRUE;
3343 /***********************************************************************
3344 * SetTimer (USER32.@)
3346 UINT_PTR WINAPI SetTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3348 UINT_PTR ret;
3349 WNDPROC winproc = 0;
3351 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, NULL );
3353 SERVER_START_REQ( set_win_timer )
3355 req->win = hwnd;
3356 req->msg = WM_TIMER;
3357 req->id = id;
3358 req->rate = max( timeout, SYS_TIMER_RATE );
3359 req->lparam = (unsigned int)winproc;
3360 if (!wine_server_call_err( req ))
3362 ret = reply->id;
3363 if (!ret) ret = TRUE;
3365 else ret = 0;
3367 SERVER_END_REQ;
3369 TRACE("Added %p %x %p timeout %d\n", hwnd, id, winproc, timeout );
3370 return ret;
3374 /***********************************************************************
3375 * SetSystemTimer (USER32.@)
3377 UINT_PTR WINAPI SetSystemTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3379 UINT_PTR ret;
3380 WNDPROC winproc = 0;
3382 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, NULL );
3384 SERVER_START_REQ( set_win_timer )
3386 req->win = hwnd;
3387 req->msg = WM_SYSTIMER;
3388 req->id = id;
3389 req->rate = max( timeout, SYS_TIMER_RATE );
3390 req->lparam = (unsigned int)winproc;
3391 if (!wine_server_call_err( req ))
3393 ret = reply->id;
3394 if (!ret) ret = TRUE;
3396 else ret = 0;
3398 SERVER_END_REQ;
3400 TRACE("Added %p %x %p timeout %d\n", hwnd, id, winproc, timeout );
3401 return ret;
3405 /***********************************************************************
3406 * KillTimer (USER32.@)
3408 BOOL WINAPI KillTimer( HWND hwnd, UINT_PTR id )
3410 BOOL ret;
3412 TRACE("%p %d\n", hwnd, id );
3414 SERVER_START_REQ( kill_win_timer )
3416 req->win = hwnd;
3417 req->msg = WM_TIMER;
3418 req->id = id;
3419 ret = !wine_server_call_err( req );
3421 SERVER_END_REQ;
3422 return ret;
3426 /***********************************************************************
3427 * KillSystemTimer (USER32.@)
3429 BOOL WINAPI KillSystemTimer( HWND hwnd, UINT_PTR id )
3431 BOOL ret;
3433 TRACE("%p %d\n", hwnd, id );
3435 SERVER_START_REQ( kill_win_timer )
3437 req->win = hwnd;
3438 req->msg = WM_SYSTIMER;
3439 req->id = id;
3440 ret = !wine_server_call_err( req );
3442 SERVER_END_REQ;
3443 return ret;
3447 /**********************************************************************
3448 * GetGUIThreadInfo (USER32.@)
3450 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
3452 BOOL ret;
3454 SERVER_START_REQ( get_thread_input )
3456 req->tid = id;
3457 if ((ret = !wine_server_call_err( req )))
3459 info->flags = 0;
3460 info->hwndActive = reply->active;
3461 info->hwndFocus = reply->focus;
3462 info->hwndCapture = reply->capture;
3463 info->hwndMenuOwner = reply->menu_owner;
3464 info->hwndMoveSize = reply->move_size;
3465 info->hwndCaret = reply->caret;
3466 info->rcCaret.left = reply->rect.left;
3467 info->rcCaret.top = reply->rect.top;
3468 info->rcCaret.right = reply->rect.right;
3469 info->rcCaret.bottom = reply->rect.bottom;
3470 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
3471 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
3472 if (reply->caret) info->flags |= GUI_CARETBLINKING;
3475 SERVER_END_REQ;
3476 return ret;
3480 /******************************************************************
3481 * IsHungAppWindow (USER32.@)
3484 BOOL WINAPI IsHungAppWindow( HWND hWnd )
3486 DWORD_PTR dwResult;
3487 return !SendMessageTimeoutA(hWnd, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 5000, &dwResult);