server: Rename the wait_input_idle request to better reflect what it does.
[wine/multimedia.git] / dlls / user / message.c
blob6351ff37a3fd9018d345c1b5aaad8c5c3aa5998e
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 "controls.h"
42 #include "winproc.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(msg);
46 WINE_DECLARE_DEBUG_CHANNEL(relay);
47 WINE_DECLARE_DEBUG_CHANNEL(key);
49 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
50 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
52 #define MAX_PACK_COUNT 4
53 #define MAX_SENDMSG_RECURSION 64
55 #define SYS_TIMER_RATE 55 /* min. timer rate in ms (actually 54.925)*/
57 /* description of the data fields that need to be packed along with a sent message */
58 struct packed_message
60 int count;
61 const void *data[MAX_PACK_COUNT];
62 size_t size[MAX_PACK_COUNT];
65 /* info about the message currently being received by the current thread */
66 struct received_message_info
68 enum message_type type;
69 MSG msg;
70 UINT flags; /* InSendMessageEx return flags */
71 HWINEVENTHOOK hook; /* winevent hook handle */
72 WINEVENTPROC hook_proc; /* winevent hook proc address */
75 /* structure to group all parameters for sent messages of the various kinds */
76 struct send_message_info
78 enum message_type type;
79 DWORD dest_tid;
80 HWND hwnd;
81 UINT msg;
82 WPARAM wparam;
83 LPARAM lparam;
84 UINT flags; /* flags for SendMessageTimeout */
85 UINT timeout; /* timeout for SendMessageTimeout */
86 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
87 ULONG_PTR data; /* callback data */
91 /* flag for messages that contain pointers */
92 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
94 #define SET(msg) (1 << ((msg) & 31))
96 static const unsigned int message_pointer_flags[] =
98 /* 0x00 - 0x1f */
99 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
100 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
101 /* 0x20 - 0x3f */
102 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
103 SET(WM_COMPAREITEM),
104 /* 0x40 - 0x5f */
105 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
106 SET(WM_NOTIFY) | SET(WM_HELP),
107 /* 0x60 - 0x7f */
108 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
109 /* 0x80 - 0x9f */
110 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
111 /* 0xa0 - 0xbf */
112 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
113 /* 0xc0 - 0xdf */
114 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
115 /* 0xe0 - 0xff */
116 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO) | SET(SBM_GETSCROLLBARINFO),
117 /* 0x100 - 0x11f */
119 /* 0x120 - 0x13f */
121 /* 0x140 - 0x15f */
122 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
123 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
124 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
125 /* 0x160 - 0x17f */
127 /* 0x180 - 0x19f */
128 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
129 SET(LB_DIR) | SET(LB_FINDSTRING) |
130 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
131 /* 0x1a0 - 0x1bf */
132 SET(LB_FINDSTRINGEXACT),
133 /* 0x1c0 - 0x1df */
135 /* 0x1e0 - 0x1ff */
137 /* 0x200 - 0x21f */
138 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
139 /* 0x220 - 0x23f */
140 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
141 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
142 /* 0x240 - 0x25f */
144 /* 0x260 - 0x27f */
146 /* 0x280 - 0x29f */
148 /* 0x2a0 - 0x2bf */
150 /* 0x2c0 - 0x2df */
152 /* 0x2e0 - 0x2ff */
154 /* 0x300 - 0x31f */
155 SET(WM_ASKCBFORMATNAME)
158 /* flags for messages that contain Unicode strings */
159 static const unsigned int message_unicode_flags[] =
161 /* 0x00 - 0x1f */
162 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
163 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
164 /* 0x20 - 0x3f */
165 SET(WM_CHARTOITEM),
166 /* 0x40 - 0x5f */
168 /* 0x60 - 0x7f */
170 /* 0x80 - 0x9f */
171 SET(WM_NCCREATE),
172 /* 0xa0 - 0xbf */
174 /* 0xc0 - 0xdf */
175 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
176 /* 0xe0 - 0xff */
178 /* 0x100 - 0x11f */
179 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
180 /* 0x120 - 0x13f */
181 SET(WM_MENUCHAR),
182 /* 0x140 - 0x15f */
183 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
184 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
185 /* 0x160 - 0x17f */
187 /* 0x180 - 0x19f */
188 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
189 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
190 /* 0x1a0 - 0x1bf */
191 SET(LB_FINDSTRINGEXACT),
192 /* 0x1c0 - 0x1df */
194 /* 0x1e0 - 0x1ff */
196 /* 0x200 - 0x21f */
198 /* 0x220 - 0x23f */
199 SET(WM_MDICREATE),
200 /* 0x240 - 0x25f */
202 /* 0x260 - 0x27f */
204 /* 0x280 - 0x29f */
205 SET(WM_IME_CHAR),
206 /* 0x2a0 - 0x2bf */
208 /* 0x2c0 - 0x2df */
210 /* 0x2e0 - 0x2ff */
212 /* 0x300 - 0x31f */
213 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
216 /* check whether a given message type includes pointers */
217 inline static int is_pointer_message( UINT message )
219 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
220 return (message_pointer_flags[message / 32] & SET(message)) != 0;
223 /* check whether a given message type contains Unicode (or ASCII) chars */
224 inline static int is_unicode_message( UINT message )
226 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
227 return (message_unicode_flags[message / 32] & SET(message)) != 0;
230 #undef SET
232 /* add a data field to a packed message */
233 inline static void push_data( struct packed_message *data, const void *ptr, size_t size )
235 data->data[data->count] = ptr;
236 data->size[data->count] = size;
237 data->count++;
240 /* add a string to a packed message */
241 inline static void push_string( struct packed_message *data, LPCWSTR str )
243 push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
246 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
247 inline static void *get_data( void **buffer, size_t size )
249 void *ret = *buffer;
250 *buffer = (char *)*buffer + size;
251 return ret;
254 /* make sure that the buffer contains a valid null-terminated Unicode string */
255 inline static BOOL check_string( LPCWSTR str, size_t size )
257 for (size /= sizeof(WCHAR); size; size--, str++)
258 if (!*str) return TRUE;
259 return FALSE;
262 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
263 inline static void *get_buffer_space( void **buffer, size_t size )
265 void *ret;
267 if (*buffer)
269 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
270 HeapFree( GetProcessHeap(), 0, *buffer );
272 else ret = HeapAlloc( GetProcessHeap(), 0, size );
274 *buffer = ret;
275 return ret;
278 /* retrieve a string pointer from packed data */
279 inline static LPWSTR get_string( void **buffer )
281 return get_data( buffer, (strlenW( (LPWSTR)*buffer ) + 1) * sizeof(WCHAR) );
284 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
285 inline static BOOL combobox_has_strings( HWND hwnd )
287 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
288 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
291 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
292 inline static BOOL listbox_has_strings( HWND hwnd )
294 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
295 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
298 /* check whether message is in the range of keyboard messages */
299 inline static BOOL is_keyboard_message( UINT message )
301 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
304 /* check whether message is in the range of mouse messages */
305 inline static BOOL is_mouse_message( UINT message )
307 return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
308 (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
311 /* check whether message matches the specified hwnd filter */
312 inline static BOOL check_hwnd_filter( const MSG *msg, HWND hwnd_filter )
314 if (!hwnd_filter) return TRUE;
315 return (msg->hwnd == hwnd_filter || IsChild( hwnd_filter, msg->hwnd ));
319 /***********************************************************************
320 * broadcast_message_callback
322 * Helper callback for broadcasting messages.
324 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
326 struct send_message_info *info = (struct send_message_info *)lparam;
327 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
328 switch(info->type)
330 case MSG_UNICODE:
331 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
332 info->flags, info->timeout, NULL );
333 break;
334 case MSG_ASCII:
335 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
336 info->flags, info->timeout, NULL );
337 break;
338 case MSG_NOTIFY:
339 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
340 break;
341 case MSG_CALLBACK:
342 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
343 info->callback, info->data );
344 break;
345 case MSG_POSTED:
346 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
347 break;
348 default:
349 ERR( "bad type %d\n", info->type );
350 break;
352 return TRUE;
356 /***********************************************************************
357 * map_wparam_AtoW
359 * Convert the wparam of an ASCII message to Unicode.
361 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
363 switch(message)
365 case WM_CHARTOITEM:
366 case EM_SETPASSWORDCHAR:
367 case WM_CHAR:
368 case WM_DEADCHAR:
369 case WM_SYSCHAR:
370 case WM_SYSDEADCHAR:
371 case WM_MENUCHAR:
373 char ch[2];
374 WCHAR wch[2];
375 ch[0] = (wparam & 0xff);
376 ch[1] = (wparam >> 8);
377 MultiByteToWideChar(CP_ACP, 0, ch, 2, wch, 2);
378 wparam = MAKEWPARAM(wch[0], wch[1]);
380 break;
381 case WM_IME_CHAR:
383 char ch[2];
384 WCHAR wch;
385 ch[0] = (wparam >> 8);
386 ch[1] = (wparam & 0xff);
387 if (ch[0]) MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
388 else MultiByteToWideChar(CP_ACP, 0, &ch[1], 1, &wch, 1);
389 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
391 break;
393 return wparam;
397 /***********************************************************************
398 * map_wparam_WtoA
400 * Convert the wparam of a Unicode message to ASCII.
402 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
404 switch(message)
406 case WM_CHARTOITEM:
407 case EM_SETPASSWORDCHAR:
408 case WM_CHAR:
409 case WM_DEADCHAR:
410 case WM_SYSCHAR:
411 case WM_SYSDEADCHAR:
412 case WM_MENUCHAR:
414 WCHAR wch[2];
415 BYTE ch[2];
416 wch[0] = LOWORD(wparam);
417 wch[1] = HIWORD(wparam);
418 WideCharToMultiByte( CP_ACP, 0, wch, 2, (LPSTR)ch, 2, NULL, NULL );
419 wparam = MAKEWPARAM( ch[0] | (ch[1] << 8), 0 );
421 break;
422 case WM_IME_CHAR:
424 WCHAR wch = LOWORD(wparam);
425 BYTE ch[2];
427 if (WideCharToMultiByte( CP_ACP, 0, &wch, 1, (LPSTR)ch, 2, NULL, NULL ) == 2)
428 wparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wparam) );
429 else
430 wparam = MAKEWPARAM( ch[0], HIWORD(wparam) );
432 break;
434 return wparam;
438 /***********************************************************************
439 * pack_message
441 * Pack a message for sending to another process.
442 * Return the size of the data we expect in the message reply.
443 * Set data->count to -1 if there is an error.
445 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
446 struct packed_message *data )
448 data->count = 0;
449 switch(message)
451 case WM_NCCREATE:
452 case WM_CREATE:
454 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
455 push_data( data, cs, sizeof(*cs) );
456 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
457 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
458 return sizeof(*cs);
460 case WM_GETTEXT:
461 case WM_ASKCBFORMATNAME:
462 return wparam * sizeof(WCHAR);
463 case WM_WININICHANGE:
464 if (lparam) push_string(data, (LPWSTR)lparam );
465 return 0;
466 case WM_SETTEXT:
467 case WM_DEVMODECHANGE:
468 case CB_DIR:
469 case LB_DIR:
470 case LB_ADDFILE:
471 case EM_REPLACESEL:
472 push_string( data, (LPWSTR)lparam );
473 return 0;
474 case WM_GETMINMAXINFO:
475 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
476 return sizeof(MINMAXINFO);
477 case WM_DRAWITEM:
478 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
479 return 0;
480 case WM_MEASUREITEM:
481 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
482 return sizeof(MEASUREITEMSTRUCT);
483 case WM_DELETEITEM:
484 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
485 return 0;
486 case WM_COMPAREITEM:
487 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
488 return 0;
489 case WM_WINDOWPOSCHANGING:
490 case WM_WINDOWPOSCHANGED:
491 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
492 return sizeof(WINDOWPOS);
493 case WM_COPYDATA:
495 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
496 push_data( data, cp, sizeof(*cp) );
497 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
498 return 0;
500 case WM_NOTIFY:
501 /* WM_NOTIFY cannot be sent across processes (MSDN) */
502 data->count = -1;
503 return 0;
504 case WM_HELP:
505 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
506 return 0;
507 case WM_STYLECHANGING:
508 case WM_STYLECHANGED:
509 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
510 return 0;
511 case WM_NCCALCSIZE:
512 if (!wparam)
514 push_data( data, (RECT *)lparam, sizeof(RECT) );
515 return sizeof(RECT);
517 else
519 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
520 push_data( data, nc, sizeof(*nc) );
521 push_data( data, nc->lppos, sizeof(*nc->lppos) );
522 return sizeof(*nc) + sizeof(*nc->lppos);
524 case WM_GETDLGCODE:
525 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
526 return sizeof(MSG);
527 case SBM_SETSCROLLINFO:
528 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
529 return 0;
530 case SBM_GETSCROLLINFO:
531 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
532 return sizeof(SCROLLINFO);
533 case SBM_GETSCROLLBARINFO:
535 const SCROLLBARINFO *info = (const SCROLLBARINFO *)lparam;
536 size_t size = min( info->cbSize, sizeof(SCROLLBARINFO) );
537 push_data( data, info, size );
538 return size;
540 case EM_GETSEL:
541 case SBM_GETRANGE:
542 case CB_GETEDITSEL:
544 size_t size = 0;
545 if (wparam) size += sizeof(DWORD);
546 if (lparam) size += sizeof(DWORD);
547 return size;
549 case EM_GETRECT:
550 case LB_GETITEMRECT:
551 case CB_GETDROPPEDCONTROLRECT:
552 return sizeof(RECT);
553 case EM_SETRECT:
554 case EM_SETRECTNP:
555 push_data( data, (RECT *)lparam, sizeof(RECT) );
556 return 0;
557 case EM_GETLINE:
559 WORD *pw = (WORD *)lparam;
560 push_data( data, pw, sizeof(*pw) );
561 return *pw * sizeof(WCHAR);
563 case EM_SETTABSTOPS:
564 case LB_SETTABSTOPS:
565 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
566 return 0;
567 case CB_ADDSTRING:
568 case CB_INSERTSTRING:
569 case CB_FINDSTRING:
570 case CB_FINDSTRINGEXACT:
571 case CB_SELECTSTRING:
572 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
573 return 0;
574 case CB_GETLBTEXT:
575 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
576 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
577 case LB_ADDSTRING:
578 case LB_INSERTSTRING:
579 case LB_FINDSTRING:
580 case LB_FINDSTRINGEXACT:
581 case LB_SELECTSTRING:
582 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
583 return 0;
584 case LB_GETTEXT:
585 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
586 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
587 case LB_GETSELITEMS:
588 return wparam * sizeof(UINT);
589 case WM_NEXTMENU:
590 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
591 return sizeof(MDINEXTMENU);
592 case WM_SIZING:
593 case WM_MOVING:
594 push_data( data, (RECT *)lparam, sizeof(RECT) );
595 return sizeof(RECT);
596 case WM_MDICREATE:
598 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
599 push_data( data, cs, sizeof(*cs) );
600 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
601 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
602 return sizeof(*cs);
604 case WM_MDIGETACTIVE:
605 if (lparam) return sizeof(BOOL);
606 return 0;
607 case WM_DEVICECHANGE:
609 DEV_BROADCAST_HDR *header = (DEV_BROADCAST_HDR *)lparam;
610 push_data( data, header, header->dbch_size );
611 return 0;
613 case WM_WINE_SETWINDOWPOS:
614 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
615 return 0;
616 case WM_WINE_KEYBOARD_LL_HOOK:
617 push_data( data, (KBDLLHOOKSTRUCT *)lparam, sizeof(KBDLLHOOKSTRUCT) );
618 return 0;
619 case WM_WINE_MOUSE_LL_HOOK:
620 push_data( data, (MSLLHOOKSTRUCT *)lparam, sizeof(MSLLHOOKSTRUCT) );
621 return 0;
622 case WM_NCPAINT:
623 if (wparam <= 1) return 0;
624 FIXME( "WM_NCPAINT hdc packing not supported yet\n" );
625 data->count = -1;
626 return 0;
627 case WM_PAINT:
628 if (!wparam) return 0;
629 /* fall through */
631 /* these contain an HFONT */
632 case WM_SETFONT:
633 case WM_GETFONT:
634 /* these contain an HDC */
635 case WM_ERASEBKGND:
636 case WM_ICONERASEBKGND:
637 case WM_CTLCOLORMSGBOX:
638 case WM_CTLCOLOREDIT:
639 case WM_CTLCOLORLISTBOX:
640 case WM_CTLCOLORBTN:
641 case WM_CTLCOLORDLG:
642 case WM_CTLCOLORSCROLLBAR:
643 case WM_CTLCOLORSTATIC:
644 case WM_PRINT:
645 case WM_PRINTCLIENT:
646 /* these contain an HGLOBAL */
647 case WM_PAINTCLIPBOARD:
648 case WM_SIZECLIPBOARD:
649 /* these contain HICON */
650 case WM_GETICON:
651 case WM_SETICON:
652 case WM_QUERYDRAGICON:
653 case WM_QUERYPARKICON:
654 /* these contain pointers */
655 case WM_DROPOBJECT:
656 case WM_QUERYDROPOBJECT:
657 case WM_DRAGLOOP:
658 case WM_DRAGSELECT:
659 case WM_DRAGMOVE:
660 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
661 data->count = -1;
662 return 0;
664 return 0;
668 /***********************************************************************
669 * unpack_message
671 * Unpack a message received from another process.
673 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
674 void **buffer, size_t size )
676 size_t minsize = 0;
678 switch(message)
680 case WM_NCCREATE:
681 case WM_CREATE:
683 CREATESTRUCTW *cs = *buffer;
684 WCHAR *str = (WCHAR *)(cs + 1);
685 if (size < sizeof(*cs)) return FALSE;
686 size -= sizeof(*cs);
687 if (HIWORD(cs->lpszName))
689 if (!check_string( str, size )) return FALSE;
690 cs->lpszName = str;
691 size -= (strlenW(str) + 1) * sizeof(WCHAR);
692 str += strlenW(str) + 1;
694 if (HIWORD(cs->lpszClass))
696 if (!check_string( str, size )) return FALSE;
697 cs->lpszClass = str;
699 break;
701 case WM_GETTEXT:
702 case WM_ASKCBFORMATNAME:
703 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
704 break;
705 case WM_WININICHANGE:
706 if (!*lparam) return TRUE;
707 /* fall through */
708 case WM_SETTEXT:
709 case WM_DEVMODECHANGE:
710 case CB_DIR:
711 case LB_DIR:
712 case LB_ADDFILE:
713 case EM_REPLACESEL:
714 if (!check_string( *buffer, size )) return FALSE;
715 break;
716 case WM_GETMINMAXINFO:
717 minsize = sizeof(MINMAXINFO);
718 break;
719 case WM_DRAWITEM:
720 minsize = sizeof(DRAWITEMSTRUCT);
721 break;
722 case WM_MEASUREITEM:
723 minsize = sizeof(MEASUREITEMSTRUCT);
724 break;
725 case WM_DELETEITEM:
726 minsize = sizeof(DELETEITEMSTRUCT);
727 break;
728 case WM_COMPAREITEM:
729 minsize = sizeof(COMPAREITEMSTRUCT);
730 break;
731 case WM_WINDOWPOSCHANGING:
732 case WM_WINDOWPOSCHANGED:
733 case WM_WINE_SETWINDOWPOS:
734 minsize = sizeof(WINDOWPOS);
735 break;
736 case WM_COPYDATA:
738 COPYDATASTRUCT *cp = *buffer;
739 if (size < sizeof(*cp)) return FALSE;
740 if (cp->lpData)
742 minsize = sizeof(*cp) + cp->cbData;
743 cp->lpData = cp + 1;
745 break;
747 case WM_NOTIFY:
748 /* WM_NOTIFY cannot be sent across processes (MSDN) */
749 return FALSE;
750 case WM_HELP:
751 minsize = sizeof(HELPINFO);
752 break;
753 case WM_STYLECHANGING:
754 case WM_STYLECHANGED:
755 minsize = sizeof(STYLESTRUCT);
756 break;
757 case WM_NCCALCSIZE:
758 if (!*wparam) minsize = sizeof(RECT);
759 else
761 NCCALCSIZE_PARAMS *nc = *buffer;
762 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
763 nc->lppos = (WINDOWPOS *)(nc + 1);
765 break;
766 case WM_GETDLGCODE:
767 if (!*lparam) return TRUE;
768 minsize = sizeof(MSG);
769 break;
770 case SBM_SETSCROLLINFO:
771 minsize = sizeof(SCROLLINFO);
772 break;
773 case SBM_GETSCROLLINFO:
774 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
775 break;
776 case SBM_GETSCROLLBARINFO:
777 if (!get_buffer_space( buffer, sizeof(SCROLLBARINFO ))) return FALSE;
778 break;
779 case EM_GETSEL:
780 case SBM_GETRANGE:
781 case CB_GETEDITSEL:
782 if (*wparam || *lparam)
784 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
785 if (*wparam) *wparam = (WPARAM)*buffer;
786 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
788 return TRUE;
789 case EM_GETRECT:
790 case LB_GETITEMRECT:
791 case CB_GETDROPPEDCONTROLRECT:
792 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
793 break;
794 case EM_SETRECT:
795 case EM_SETRECTNP:
796 minsize = sizeof(RECT);
797 break;
798 case EM_GETLINE:
800 WORD len;
801 if (size < sizeof(WORD)) return FALSE;
802 len = *(WORD *)*buffer;
803 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
804 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
805 return TRUE;
807 case EM_SETTABSTOPS:
808 case LB_SETTABSTOPS:
809 if (!*wparam) return TRUE;
810 minsize = *wparam * sizeof(UINT);
811 break;
812 case CB_ADDSTRING:
813 case CB_INSERTSTRING:
814 case CB_FINDSTRING:
815 case CB_FINDSTRINGEXACT:
816 case CB_SELECTSTRING:
817 case LB_ADDSTRING:
818 case LB_INSERTSTRING:
819 case LB_FINDSTRING:
820 case LB_FINDSTRINGEXACT:
821 case LB_SELECTSTRING:
822 if (!*buffer) return TRUE;
823 if (!check_string( *buffer, size )) return FALSE;
824 break;
825 case CB_GETLBTEXT:
827 size = sizeof(ULONG_PTR);
828 if (combobox_has_strings( hwnd ))
829 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
830 if (!get_buffer_space( buffer, size )) return FALSE;
831 break;
833 case LB_GETTEXT:
835 size = sizeof(ULONG_PTR);
836 if (listbox_has_strings( hwnd ))
837 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
838 if (!get_buffer_space( buffer, size )) return FALSE;
839 break;
841 case LB_GETSELITEMS:
842 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
843 break;
844 case WM_NEXTMENU:
845 minsize = sizeof(MDINEXTMENU);
846 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
847 break;
848 case WM_SIZING:
849 case WM_MOVING:
850 minsize = sizeof(RECT);
851 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
852 break;
853 case WM_MDICREATE:
855 MDICREATESTRUCTW *cs = *buffer;
856 WCHAR *str = (WCHAR *)(cs + 1);
857 if (size < sizeof(*cs)) return FALSE;
858 size -= sizeof(*cs);
859 if (HIWORD(cs->szTitle))
861 if (!check_string( str, size )) return FALSE;
862 cs->szTitle = str;
863 size -= (strlenW(str) + 1) * sizeof(WCHAR);
864 str += strlenW(str) + 1;
866 if (HIWORD(cs->szClass))
868 if (!check_string( str, size )) return FALSE;
869 cs->szClass = str;
871 break;
873 case WM_MDIGETACTIVE:
874 if (!*lparam) return TRUE;
875 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
876 break;
877 case WM_DEVICECHANGE:
878 minsize = sizeof(DEV_BROADCAST_HDR);
879 break;
880 case WM_WINE_KEYBOARD_LL_HOOK:
881 minsize = sizeof(KBDLLHOOKSTRUCT);
882 break;
883 case WM_WINE_MOUSE_LL_HOOK:
884 minsize = sizeof(MSLLHOOKSTRUCT);
885 break;
886 case WM_NCPAINT:
887 if (*wparam <= 1) return TRUE;
888 FIXME( "WM_NCPAINT hdc unpacking not supported\n" );
889 return FALSE;
890 case WM_PAINT:
891 if (!*wparam) return TRUE;
892 /* fall through */
894 /* these contain an HFONT */
895 case WM_SETFONT:
896 case WM_GETFONT:
897 /* these contain an HDC */
898 case WM_ERASEBKGND:
899 case WM_ICONERASEBKGND:
900 case WM_CTLCOLORMSGBOX:
901 case WM_CTLCOLOREDIT:
902 case WM_CTLCOLORLISTBOX:
903 case WM_CTLCOLORBTN:
904 case WM_CTLCOLORDLG:
905 case WM_CTLCOLORSCROLLBAR:
906 case WM_CTLCOLORSTATIC:
907 case WM_PRINT:
908 case WM_PRINTCLIENT:
909 /* these contain an HGLOBAL */
910 case WM_PAINTCLIPBOARD:
911 case WM_SIZECLIPBOARD:
912 /* these contain HICON */
913 case WM_GETICON:
914 case WM_SETICON:
915 case WM_QUERYDRAGICON:
916 case WM_QUERYPARKICON:
917 /* these contain pointers */
918 case WM_DROPOBJECT:
919 case WM_QUERYDROPOBJECT:
920 case WM_DRAGLOOP:
921 case WM_DRAGSELECT:
922 case WM_DRAGMOVE:
923 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
924 return FALSE;
926 default:
927 return TRUE; /* message doesn't need any unpacking */
930 /* default exit for most messages: check minsize and store buffer in lparam */
931 if (size < minsize) return FALSE;
932 *lparam = (LPARAM)*buffer;
933 return TRUE;
937 /***********************************************************************
938 * pack_reply
940 * Pack a reply to a message for sending to another process.
942 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
943 LRESULT res, struct packed_message *data )
945 data->count = 0;
946 switch(message)
948 case WM_NCCREATE:
949 case WM_CREATE:
950 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
951 break;
952 case WM_GETTEXT:
953 case CB_GETLBTEXT:
954 case LB_GETTEXT:
955 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
956 break;
957 case WM_GETMINMAXINFO:
958 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
959 break;
960 case WM_MEASUREITEM:
961 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
962 break;
963 case WM_WINDOWPOSCHANGING:
964 case WM_WINDOWPOSCHANGED:
965 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
966 break;
967 case WM_GETDLGCODE:
968 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
969 break;
970 case SBM_GETSCROLLINFO:
971 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
972 break;
973 case EM_GETRECT:
974 case LB_GETITEMRECT:
975 case CB_GETDROPPEDCONTROLRECT:
976 case WM_SIZING:
977 case WM_MOVING:
978 push_data( data, (RECT *)lparam, sizeof(RECT) );
979 break;
980 case EM_GETLINE:
982 WORD *ptr = (WORD *)lparam;
983 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
984 break;
986 case LB_GETSELITEMS:
987 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
988 break;
989 case WM_MDIGETACTIVE:
990 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
991 break;
992 case WM_NCCALCSIZE:
993 if (!wparam)
994 push_data( data, (RECT *)lparam, sizeof(RECT) );
995 else
997 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
998 push_data( data, nc, sizeof(*nc) );
999 push_data( data, nc->lppos, sizeof(*nc->lppos) );
1001 break;
1002 case EM_GETSEL:
1003 case SBM_GETRANGE:
1004 case CB_GETEDITSEL:
1005 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
1006 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
1007 break;
1008 case WM_NEXTMENU:
1009 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
1010 break;
1011 case WM_MDICREATE:
1012 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
1013 break;
1014 case WM_ASKCBFORMATNAME:
1015 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
1016 break;
1021 /***********************************************************************
1022 * unpack_reply
1024 * Unpack a message reply received from another process.
1026 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1027 void *buffer, size_t size )
1029 switch(message)
1031 case WM_NCCREATE:
1032 case WM_CREATE:
1034 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
1035 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
1036 memcpy( cs, buffer, min( sizeof(*cs), size ));
1037 cs->lpszName = name; /* restore the original pointers */
1038 cs->lpszClass = class;
1039 break;
1041 case WM_GETTEXT:
1042 case WM_ASKCBFORMATNAME:
1043 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
1044 break;
1045 case WM_GETMINMAXINFO:
1046 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
1047 break;
1048 case WM_MEASUREITEM:
1049 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
1050 break;
1051 case WM_WINDOWPOSCHANGING:
1052 case WM_WINDOWPOSCHANGED:
1053 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
1054 break;
1055 case WM_GETDLGCODE:
1056 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
1057 break;
1058 case SBM_GETSCROLLINFO:
1059 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
1060 break;
1061 case SBM_GETSCROLLBARINFO:
1062 memcpy( (SCROLLBARINFO *)lparam, buffer, min( sizeof(SCROLLBARINFO), size ));
1063 break;
1064 case EM_GETRECT:
1065 case CB_GETDROPPEDCONTROLRECT:
1066 case LB_GETITEMRECT:
1067 case WM_SIZING:
1068 case WM_MOVING:
1069 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1070 break;
1071 case EM_GETLINE:
1072 size = min( size, (size_t)*(WORD *)lparam );
1073 memcpy( (WCHAR *)lparam, buffer, size );
1074 break;
1075 case LB_GETSELITEMS:
1076 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
1077 break;
1078 case LB_GETTEXT:
1079 case CB_GETLBTEXT:
1080 memcpy( (WCHAR *)lparam, buffer, size );
1081 break;
1082 case WM_NEXTMENU:
1083 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
1084 break;
1085 case WM_MDIGETACTIVE:
1086 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
1087 break;
1088 case WM_NCCALCSIZE:
1089 if (!wparam)
1090 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1091 else
1093 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1094 WINDOWPOS *wp = nc->lppos;
1095 memcpy( nc, buffer, min( sizeof(*nc), size ));
1096 if (size > sizeof(*nc))
1098 size -= sizeof(*nc);
1099 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
1101 nc->lppos = wp; /* restore the original pointer */
1103 break;
1104 case EM_GETSEL:
1105 case SBM_GETRANGE:
1106 case CB_GETEDITSEL:
1107 if (wparam)
1109 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1110 if (size <= sizeof(DWORD)) break;
1111 size -= sizeof(DWORD);
1112 buffer = (DWORD *)buffer + 1;
1114 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1115 break;
1116 case WM_MDICREATE:
1118 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
1119 LPCWSTR title = cs->szTitle, class = cs->szClass;
1120 memcpy( cs, buffer, min( sizeof(*cs), size ));
1121 cs->szTitle = title; /* restore the original pointers */
1122 cs->szClass = class;
1123 break;
1125 default:
1126 ERR( "should not happen: unexpected message %x\n", message );
1127 break;
1132 /***********************************************************************
1133 * reply_message
1135 * Send a reply to a sent message.
1137 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1139 struct packed_message data;
1140 int i, replied = info->flags & ISMEX_REPLIED;
1142 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1143 if (!remove && replied) return; /* replied already */
1145 data.count = 0;
1146 info->flags |= ISMEX_REPLIED;
1148 if (info->type == MSG_OTHER_PROCESS && !replied)
1150 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1151 info->msg.lParam, result, &data );
1154 SERVER_START_REQ( reply_message )
1156 req->result = result;
1157 req->remove = remove;
1158 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1159 wine_server_call( req );
1161 SERVER_END_REQ;
1165 /***********************************************************************
1166 * handle_internal_message
1168 * Handle an internal Wine message instead of calling the window proc.
1170 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1172 switch(msg)
1174 case WM_WINE_DESTROYWINDOW:
1175 return WIN_DestroyWindow( hwnd );
1176 case WM_WINE_SETWINDOWPOS:
1177 if (hwnd == GetDesktopWindow()) return 0;
1178 return USER_Driver->pSetWindowPos( (WINDOWPOS *)lparam );
1179 case WM_WINE_SHOWWINDOW:
1180 if (hwnd == GetDesktopWindow()) return 0;
1181 return ShowWindow( hwnd, wparam );
1182 case WM_WINE_SETPARENT:
1183 if (hwnd == GetDesktopWindow()) return 0;
1184 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1185 case WM_WINE_SETWINDOWLONG:
1186 return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1187 case WM_WINE_ENABLEWINDOW:
1188 if (hwnd == GetDesktopWindow()) return 0;
1189 return EnableWindow( hwnd, wparam );
1190 case WM_WINE_SETACTIVEWINDOW:
1191 if (hwnd == GetDesktopWindow()) return 0;
1192 return (LRESULT)SetActiveWindow( (HWND)wparam );
1193 case WM_WINE_KEYBOARD_LL_HOOK:
1194 return HOOK_CallHooks( WH_KEYBOARD_LL, HC_ACTION, wparam, lparam, TRUE );
1195 case WM_WINE_MOUSE_LL_HOOK:
1196 return HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, wparam, lparam, TRUE );
1197 default:
1198 if (msg >= WM_WINE_FIRST_DRIVER_MSG && msg <= WM_WINE_LAST_DRIVER_MSG)
1199 return USER_Driver->pWindowMessage( hwnd, msg, wparam, lparam );
1200 FIXME( "unknown internal message %x\n", msg );
1201 return 0;
1205 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1206 * to the memory handle, we keep track (in the server side) of all pairs of handle
1207 * used (the client passes its value and the content of the memory handle), and
1208 * the server stored both values (the client, and the local one, created after the
1209 * content). When a ACK message is generated, the list of pair is searched for a
1210 * matching pair, so that the client memory handle can be returned.
1212 struct DDE_pair {
1213 HGLOBAL client_hMem;
1214 HGLOBAL server_hMem;
1217 static struct DDE_pair* dde_pairs;
1218 static int dde_num_alloc;
1219 static int dde_num_used;
1221 static CRITICAL_SECTION dde_crst;
1222 static CRITICAL_SECTION_DEBUG critsect_debug =
1224 0, 0, &dde_crst,
1225 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
1226 0, 0, { (DWORD_PTR)(__FILE__ ": dde_crst") }
1228 static CRITICAL_SECTION dde_crst = { &critsect_debug, -1, 0, 0, 0, 0 };
1230 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1232 int i;
1233 #define GROWBY 4
1235 EnterCriticalSection(&dde_crst);
1237 /* now remember the pair of hMem on both sides */
1238 if (dde_num_used == dde_num_alloc)
1240 struct DDE_pair* tmp;
1241 if (dde_pairs)
1242 tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1243 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1244 else
1245 tmp = HeapAlloc( GetProcessHeap(), 0,
1246 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1248 if (!tmp)
1250 LeaveCriticalSection(&dde_crst);
1251 return FALSE;
1253 dde_pairs = tmp;
1254 /* zero out newly allocated part */
1255 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1256 dde_num_alloc += GROWBY;
1258 #undef GROWBY
1259 for (i = 0; i < dde_num_alloc; i++)
1261 if (dde_pairs[i].server_hMem == 0)
1263 dde_pairs[i].client_hMem = chm;
1264 dde_pairs[i].server_hMem = shm;
1265 dde_num_used++;
1266 break;
1269 LeaveCriticalSection(&dde_crst);
1270 return TRUE;
1273 static HGLOBAL dde_get_pair(HGLOBAL shm)
1275 int i;
1276 HGLOBAL ret = 0;
1278 EnterCriticalSection(&dde_crst);
1279 for (i = 0; i < dde_num_alloc; i++)
1281 if (dde_pairs[i].server_hMem == shm)
1283 /* free this pair */
1284 dde_pairs[i].server_hMem = 0;
1285 dde_num_used--;
1286 ret = dde_pairs[i].client_hMem;
1287 break;
1290 LeaveCriticalSection(&dde_crst);
1291 return ret;
1294 /***********************************************************************
1295 * post_dde_message
1297 * Post a DDE message
1299 static BOOL post_dde_message( struct packed_message *data, const struct send_message_info *info )
1301 void* ptr = NULL;
1302 int size = 0;
1303 UINT_PTR uiLo, uiHi;
1304 LPARAM lp = 0;
1305 HGLOBAL hunlock = 0;
1306 int i;
1307 DWORD res;
1309 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1310 return FALSE;
1312 lp = info->lparam;
1313 switch (info->msg)
1315 /* DDE messages which don't require packing are:
1316 * WM_DDE_INITIATE
1317 * WM_DDE_TERMINATE
1318 * WM_DDE_REQUEST
1319 * WM_DDE_UNADVISE
1321 case WM_DDE_ACK:
1322 if (HIWORD(uiHi))
1324 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1325 HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1326 if (h)
1328 /* send back the value of h on the other side */
1329 push_data( data, &h, sizeof(HGLOBAL) );
1330 lp = uiLo;
1331 TRACE( "send dde-ack %x %08x => %p\n", uiLo, uiHi, h );
1334 else
1336 /* uiHi should contain either an atom or 0 */
1337 TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
1338 lp = MAKELONG( uiLo, uiHi );
1340 break;
1341 case WM_DDE_ADVISE:
1342 case WM_DDE_DATA:
1343 case WM_DDE_POKE:
1344 size = 0;
1345 if (uiLo)
1347 size = GlobalSize( (HGLOBAL)uiLo ) ;
1348 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1349 (info->msg == WM_DDE_DATA && size < sizeof(DDEDATA)) ||
1350 (info->msg == WM_DDE_POKE && size < sizeof(DDEPOKE))
1352 return FALSE;
1354 else if (info->msg != WM_DDE_DATA) return FALSE;
1356 lp = uiHi;
1357 if (uiLo)
1359 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1361 DDEDATA *dde_data = (DDEDATA *)ptr;
1362 TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
1363 dde_data->unused, dde_data->fResponse, dde_data->fRelease,
1364 dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
1365 push_data( data, ptr, size );
1366 hunlock = (HGLOBAL)uiLo;
1369 TRACE( "send ddepack %u %x\n", size, uiHi );
1370 break;
1371 case WM_DDE_EXECUTE:
1372 if (info->lparam)
1374 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1376 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1377 /* so that the other side can send it back on ACK */
1378 lp = info->lparam;
1379 hunlock = (HGLOBAL)info->lparam;
1382 break;
1384 SERVER_START_REQ( send_message )
1386 req->id = info->dest_tid;
1387 req->type = info->type;
1388 req->flags = 0;
1389 req->win = info->hwnd;
1390 req->msg = info->msg;
1391 req->wparam = info->wparam;
1392 req->lparam = lp;
1393 req->time = GetCurrentTime();
1394 req->timeout = 0;
1395 for (i = 0; i < data->count; i++)
1396 wine_server_add_data( req, data->data[i], data->size[i] );
1397 if ((res = wine_server_call( req )))
1399 if (res == STATUS_INVALID_PARAMETER)
1400 /* FIXME: find a STATUS_ value for this one */
1401 SetLastError( ERROR_INVALID_THREAD_ID );
1402 else
1403 SetLastError( RtlNtStatusToDosError(res) );
1405 else
1406 FreeDDElParam(info->msg, info->lparam);
1408 SERVER_END_REQ;
1409 if (hunlock) GlobalUnlock(hunlock);
1411 return !res;
1414 /***********************************************************************
1415 * unpack_dde_message
1417 * Unpack a posted DDE message received from another process.
1419 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1420 void **buffer, size_t size )
1422 UINT_PTR uiLo, uiHi;
1423 HGLOBAL hMem = 0;
1424 void* ptr;
1426 switch (message)
1428 case WM_DDE_ACK:
1429 if (size)
1431 /* hMem is being passed */
1432 if (size != sizeof(HGLOBAL)) return FALSE;
1433 if (!buffer || !*buffer) return FALSE;
1434 uiLo = *lparam;
1435 memcpy( &hMem, *buffer, size );
1436 uiHi = (UINT_PTR)hMem;
1437 TRACE("recv dde-ack %x mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1439 else
1441 uiLo = LOWORD( *lparam );
1442 uiHi = HIWORD( *lparam );
1443 TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
1445 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1446 break;
1447 case WM_DDE_ADVISE:
1448 case WM_DDE_DATA:
1449 case WM_DDE_POKE:
1450 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1451 uiHi = *lparam;
1452 if (size)
1454 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size )))
1455 return FALSE;
1456 if ((ptr = GlobalLock( hMem )))
1458 memcpy( ptr, *buffer, size );
1459 GlobalUnlock( hMem );
1461 else
1463 GlobalFree( hMem );
1464 return FALSE;
1467 uiLo = (UINT_PTR)hMem;
1469 *lparam = PackDDElParam( message, uiLo, uiHi );
1470 break;
1471 case WM_DDE_EXECUTE:
1472 if (size)
1474 if (!buffer || !*buffer) return FALSE;
1475 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size ))) return FALSE;
1476 if ((ptr = GlobalLock( hMem )))
1478 memcpy( ptr, *buffer, size );
1479 GlobalUnlock( hMem );
1480 TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
1481 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1483 GlobalFree( hMem );
1484 return FALSE;
1487 else
1489 GlobalFree( hMem );
1490 return FALSE;
1492 } else return FALSE;
1493 *lparam = (LPARAM)hMem;
1494 break;
1496 return TRUE;
1499 /***********************************************************************
1500 * call_window_proc
1502 * Call a window procedure and the corresponding hooks.
1504 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1505 BOOL unicode, BOOL same_thread )
1507 struct user_thread_info *thread_info = get_user_thread_info();
1508 LRESULT result = 0;
1509 WNDPROC winproc;
1510 CWPSTRUCT cwp;
1511 CWPRETSTRUCT cwpret;
1513 if (thread_info->recursion_count > MAX_SENDMSG_RECURSION) return 0;
1514 thread_info->recursion_count++;
1516 if (msg & 0x80000000)
1518 result = handle_internal_message( hwnd, msg, wparam, lparam );
1519 goto done;
1522 /* first the WH_CALLWNDPROC hook */
1523 hwnd = WIN_GetFullHandle( hwnd );
1524 cwp.lParam = lparam;
1525 cwp.wParam = wparam;
1526 cwp.message = msg;
1527 cwp.hwnd = hwnd;
1528 HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1530 /* now call the window procedure */
1531 if (unicode)
1533 if (!(winproc = (WNDPROC)GetWindowLongPtrW( hwnd, GWLP_WNDPROC ))) goto done;
1534 result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1536 else
1538 if (!(winproc = (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC ))) goto done;
1539 result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1542 /* and finally the WH_CALLWNDPROCRET hook */
1543 cwpret.lResult = result;
1544 cwpret.lParam = lparam;
1545 cwpret.wParam = wparam;
1546 cwpret.message = msg;
1547 cwpret.hwnd = hwnd;
1548 HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1549 done:
1550 thread_info->recursion_count--;
1551 return result;
1555 /***********************************************************************
1556 * send_parent_notify
1558 * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
1559 * the window has the WS_EX_NOPARENTNOTIFY style.
1561 static void send_parent_notify( HWND hwnd, WORD event, WORD idChild, POINT pt )
1563 /* pt has to be in the client coordinates of the parent window */
1564 MapWindowPoints( 0, hwnd, &pt, 1 );
1565 for (;;)
1567 HWND parent;
1569 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)) break;
1570 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
1571 if (!(parent = GetParent(hwnd))) break;
1572 if (parent == GetDesktopWindow()) break;
1573 MapWindowPoints( hwnd, parent, &pt, 1 );
1574 hwnd = parent;
1575 SendMessageW( hwnd, WM_PARENTNOTIFY,
1576 MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
1581 /***********************************************************************
1582 * accept_hardware_message
1584 * Tell the server we have passed the message to the app
1585 * (even though we may end up dropping it later on)
1587 static void accept_hardware_message( UINT hw_id, BOOL remove, HWND new_hwnd )
1589 SERVER_START_REQ( accept_hardware_message )
1591 req->hw_id = hw_id;
1592 req->remove = remove;
1593 req->new_win = new_hwnd;
1594 if (wine_server_call( req ))
1595 FIXME("Failed to reply to MSG_HARDWARE message. Message may not be removed from queue.\n");
1597 SERVER_END_REQ;
1601 /***********************************************************************
1602 * process_keyboard_message
1604 * returns TRUE if the contents of 'msg' should be passed to the application
1606 static BOOL process_keyboard_message( MSG *msg, UINT hw_id, HWND hwnd_filter,
1607 UINT first, UINT last, BOOL remove )
1609 EVENTMSG event;
1611 /* FIXME: is this really the right place for this hook? */
1612 event.message = msg->message;
1613 event.hwnd = msg->hwnd;
1614 event.time = msg->time;
1615 event.paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
1616 event.paramH = msg->lParam & 0x7FFF;
1617 if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
1618 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1620 /* check message filters */
1621 if (msg->message < first || msg->message > last) return FALSE;
1622 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1624 if (remove)
1626 /* Handle F1 key by sending out WM_HELP message */
1627 if ((msg->message == WM_KEYUP) &&
1628 (msg->wParam == VK_F1) &&
1629 (msg->hwnd != GetDesktopWindow()) &&
1630 !MENU_IsMenuActive())
1632 HELPINFO hi;
1633 hi.cbSize = sizeof(HELPINFO);
1634 hi.iContextType = HELPINFO_WINDOW;
1635 hi.iCtrlId = GetWindowLongPtrA( msg->hwnd, GWLP_ID );
1636 hi.hItemHandle = msg->hwnd;
1637 hi.dwContextId = GetWindowContextHelpId( msg->hwnd );
1638 hi.MousePos = msg->pt;
1639 SendMessageW( msg->hwnd, WM_HELP, 0, (LPARAM)&hi );
1643 if (HOOK_CallHooks( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
1644 LOWORD(msg->wParam), msg->lParam, TRUE ))
1646 /* skip this message */
1647 HOOK_CallHooks( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam, TRUE );
1648 accept_hardware_message( hw_id, TRUE, 0 );
1649 return FALSE;
1651 accept_hardware_message( hw_id, remove, 0 );
1652 return TRUE;
1656 /***********************************************************************
1657 * process_mouse_message
1659 * returns TRUE if the contents of 'msg' should be passed to the application
1661 static BOOL process_mouse_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
1662 UINT first, UINT last, BOOL remove )
1664 static MSG clk_msg;
1666 POINT pt;
1667 UINT message;
1668 INT hittest;
1669 EVENTMSG event;
1670 GUITHREADINFO info;
1671 MOUSEHOOKSTRUCT hook;
1672 BOOL eatMsg;
1674 /* find the window to dispatch this mouse message to */
1676 GetGUIThreadInfo( GetCurrentThreadId(), &info );
1677 if (info.hwndCapture)
1679 hittest = HTCLIENT;
1680 msg->hwnd = info.hwndCapture;
1682 else
1684 msg->hwnd = WINPOS_WindowFromPoint( msg->hwnd, msg->pt, &hittest );
1687 if (!msg->hwnd || !WIN_IsCurrentThread( msg->hwnd ))
1689 accept_hardware_message( hw_id, TRUE, msg->hwnd );
1690 return FALSE;
1693 /* FIXME: is this really the right place for this hook? */
1694 event.message = msg->message;
1695 event.time = msg->time;
1696 event.hwnd = msg->hwnd;
1697 event.paramL = msg->pt.x;
1698 event.paramH = msg->pt.y;
1699 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1701 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1703 pt = msg->pt;
1704 message = msg->message;
1705 /* Note: windows has no concept of a non-client wheel message */
1706 if (message != WM_MOUSEWHEEL)
1708 if (hittest != HTCLIENT)
1710 message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
1711 msg->wParam = hittest;
1713 else
1715 /* coordinates don't get translated while tracking a menu */
1716 /* FIXME: should differentiate popups and top-level menus */
1717 if (!(info.flags & GUI_INMENUMODE))
1718 ScreenToClient( msg->hwnd, &pt );
1721 msg->lParam = MAKELONG( pt.x, pt.y );
1723 /* translate double clicks */
1725 if ((msg->message == WM_LBUTTONDOWN) ||
1726 (msg->message == WM_RBUTTONDOWN) ||
1727 (msg->message == WM_MBUTTONDOWN) ||
1728 (msg->message == WM_XBUTTONDOWN))
1730 BOOL update = remove;
1732 /* translate double clicks -
1733 * note that ...MOUSEMOVEs can slip in between
1734 * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
1736 if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
1737 hittest != HTCLIENT ||
1738 (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
1740 if ((msg->message == clk_msg.message) &&
1741 (msg->hwnd == clk_msg.hwnd) &&
1742 (msg->wParam == clk_msg.wParam) &&
1743 (msg->time - clk_msg.time < GetDoubleClickTime()) &&
1744 (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
1745 (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
1747 message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
1748 if (update)
1750 clk_msg.message = 0; /* clear the double click conditions */
1751 update = FALSE;
1755 if (message < first || message > last) return FALSE;
1756 /* update static double click conditions */
1757 if (update) clk_msg = *msg;
1759 else
1761 if (message < first || message > last) return FALSE;
1764 /* message is accepted now (but may still get dropped) */
1766 hook.pt = msg->pt;
1767 hook.hwnd = msg->hwnd;
1768 hook.wHitTestCode = hittest;
1769 hook.dwExtraInfo = extra_info;
1770 if (HOOK_CallHooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
1771 message, (LPARAM)&hook, TRUE ))
1773 hook.pt = msg->pt;
1774 hook.hwnd = msg->hwnd;
1775 hook.wHitTestCode = hittest;
1776 hook.dwExtraInfo = extra_info;
1777 HOOK_CallHooks( WH_CBT, HCBT_CLICKSKIPPED, message, (LPARAM)&hook, TRUE );
1778 accept_hardware_message( hw_id, TRUE, 0 );
1779 return FALSE;
1782 if ((hittest == HTERROR) || (hittest == HTNOWHERE))
1784 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
1785 MAKELONG( hittest, msg->message ));
1786 accept_hardware_message( hw_id, TRUE, 0 );
1787 return FALSE;
1790 accept_hardware_message( hw_id, remove, 0 );
1792 if (!remove || info.hwndCapture)
1794 msg->message = message;
1795 return TRUE;
1798 eatMsg = FALSE;
1800 if ((msg->message == WM_LBUTTONDOWN) ||
1801 (msg->message == WM_RBUTTONDOWN) ||
1802 (msg->message == WM_MBUTTONDOWN) ||
1803 (msg->message == WM_XBUTTONDOWN))
1805 /* Send the WM_PARENTNOTIFY,
1806 * note that even for double/nonclient clicks
1807 * notification message is still WM_L/M/RBUTTONDOWN.
1809 send_parent_notify( msg->hwnd, msg->message, 0, msg->pt );
1811 /* Activate the window if needed */
1813 if (msg->hwnd != info.hwndActive)
1815 HWND hwndTop = msg->hwnd;
1816 while (hwndTop)
1818 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
1819 hwndTop = GetParent( hwndTop );
1822 if (hwndTop && hwndTop != GetDesktopWindow())
1824 LONG ret = SendMessageW( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
1825 MAKELONG( hittest, msg->message ) );
1826 switch(ret)
1828 case MA_NOACTIVATEANDEAT:
1829 eatMsg = TRUE;
1830 /* fall through */
1831 case MA_NOACTIVATE:
1832 break;
1833 case MA_ACTIVATEANDEAT:
1834 eatMsg = TRUE;
1835 /* fall through */
1836 case MA_ACTIVATE:
1837 case 0:
1838 if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
1839 break;
1840 default:
1841 WARN( "unknown WM_MOUSEACTIVATE code %ld\n", ret );
1842 break;
1848 /* send the WM_SETCURSOR message */
1850 /* Windows sends the normal mouse message as the message parameter
1851 in the WM_SETCURSOR message even if it's non-client mouse message */
1852 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd, MAKELONG( hittest, msg->message ));
1854 msg->message = message;
1855 return !eatMsg;
1859 /***********************************************************************
1860 * process_hardware_message
1862 * Process a hardware message; return TRUE if message should be passed on to the app
1864 static BOOL process_hardware_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
1865 UINT first, UINT last, BOOL remove )
1867 if (is_keyboard_message( msg->message ))
1868 return process_keyboard_message( msg, hw_id, hwnd_filter, first, last, remove );
1870 if (is_mouse_message( msg->message ))
1871 return process_mouse_message( msg, hw_id, extra_info, hwnd_filter, first, last, remove );
1873 ERR( "unknown message type %x\n", msg->message );
1874 return FALSE;
1878 /***********************************************************************
1879 * call_sendmsg_callback
1881 * Call the callback function of SendMessageCallback.
1883 static inline void call_sendmsg_callback( SENDASYNCPROC callback, HWND hwnd, UINT msg,
1884 ULONG_PTR data, LRESULT result )
1886 if (TRACE_ON(relay))
1887 DPRINTF( "%04lx:Call message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1888 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
1889 data, result );
1890 callback( hwnd, msg, data, result );
1891 if (TRACE_ON(relay))
1892 DPRINTF( "%04lx:Ret message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
1893 GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
1894 data, result );
1898 /***********************************************************************
1899 * get_hook_proc
1901 * Retrieve the hook procedure real value for a module-relative proc
1903 static void *get_hook_proc( void *proc, const WCHAR *module )
1905 HMODULE mod;
1907 if (!(mod = GetModuleHandleW(module)))
1909 TRACE( "loading %s\n", debugstr_w(module) );
1910 /* FIXME: the library will never be freed */
1911 if (!(mod = LoadLibraryW(module))) return NULL;
1913 return (char *)mod + (ULONG_PTR)proc;
1917 /***********************************************************************
1918 * peek_message
1920 * Peek for a message matching the given parameters. Return FALSE if none available.
1921 * All pending sent messages are processed before returning.
1923 static BOOL peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1925 LRESULT result;
1926 ULONG_PTR extra_info = 0;
1927 struct user_thread_info *thread_info = get_user_thread_info();
1928 struct received_message_info info, *old_info;
1929 unsigned int hw_id = 0; /* id of previous hardware message */
1931 if (!first && !last) last = ~0;
1933 for (;;)
1935 NTSTATUS res;
1936 void *buffer = NULL;
1937 size_t size = 0, buffer_size = 0;
1939 do /* loop while buffer is too small */
1941 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1942 return FALSE;
1943 SERVER_START_REQ( get_message )
1945 req->flags = flags;
1946 req->get_win = hwnd;
1947 req->get_first = first;
1948 req->get_last = last;
1949 req->hw_id = hw_id;
1950 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1951 if (!(res = wine_server_call( req )))
1953 size = wine_server_reply_size( reply );
1954 info.type = reply->type;
1955 info.msg.hwnd = reply->win;
1956 info.msg.message = reply->msg;
1957 info.msg.wParam = reply->wparam;
1958 info.msg.lParam = reply->lparam;
1959 info.msg.time = reply->time;
1960 info.msg.pt.x = reply->x;
1961 info.msg.pt.y = reply->y;
1962 info.hook = reply->hook;
1963 info.hook_proc = reply->hook_proc;
1964 hw_id = reply->hw_id;
1965 extra_info = reply->info;
1966 thread_info->active_hooks = reply->active_hooks;
1968 else
1970 HeapFree( GetProcessHeap(), 0, buffer );
1971 buffer_size = reply->total;
1974 SERVER_END_REQ;
1975 } while (res == STATUS_BUFFER_OVERFLOW);
1977 if (res) return FALSE;
1979 TRACE( "got type %d msg %x (%s) hwnd %p wp %x lp %lx\n",
1980 info.type, info.msg.message,
1981 (info.type == MSG_WINEVENT) ? "MSG_WINEVENT" : SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1982 info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1984 switch(info.type)
1986 case MSG_ASCII:
1987 case MSG_UNICODE:
1988 info.flags = ISMEX_SEND;
1989 break;
1990 case MSG_NOTIFY:
1991 info.flags = ISMEX_NOTIFY;
1992 break;
1993 case MSG_CALLBACK:
1994 info.flags = ISMEX_CALLBACK;
1995 break;
1996 case MSG_CALLBACK_RESULT:
1997 call_sendmsg_callback( (SENDASYNCPROC)info.msg.wParam, info.msg.hwnd,
1998 info.msg.message, extra_info, info.msg.lParam );
1999 goto next;
2000 case MSG_WINEVENT:
2001 if (size)
2003 WCHAR module[MAX_PATH];
2004 size = min( size, (MAX_PATH - 1) * sizeof(WCHAR) );
2005 memcpy( module, buffer, size );
2006 module[size / sizeof(WCHAR)] = 0;
2007 if (!(info.hook_proc = get_hook_proc( info.hook_proc, module )))
2009 ERR( "invalid winevent hook module name %s\n", debugstr_w(module) );
2010 goto next;
2013 if (TRACE_ON(relay))
2014 DPRINTF( "%04lx:Call winevent proc %p (hook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%lx,tid=%04lx,time=%lx)\n",
2015 GetCurrentThreadId(), info.hook_proc,
2016 info.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2017 info.msg.lParam, extra_info, info.msg.time);
2019 info.hook_proc( info.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2020 info.msg.lParam, extra_info, info.msg.time );
2022 if (TRACE_ON(relay))
2023 DPRINTF( "%04lx:Ret winevent proc %p (hook=%p,event=%x,hwnd=%p,object_id=%x,child_id=%lx,tid=%04lx,time=%lx)\n",
2024 GetCurrentThreadId(), info.hook_proc,
2025 info.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2026 info.msg.lParam, extra_info, info.msg.time);
2027 goto next;
2028 case MSG_OTHER_PROCESS:
2029 info.flags = ISMEX_SEND;
2030 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2031 &info.msg.lParam, &buffer, size ))
2033 /* ignore it */
2034 reply_message( &info, 0, TRUE );
2035 goto next;
2037 break;
2038 case MSG_HARDWARE:
2039 if (!process_hardware_message( &info.msg, hw_id, extra_info,
2040 hwnd, first, last, flags & GET_MSG_REMOVE ))
2042 TRACE("dropping msg %x\n", info.msg.message );
2043 goto next; /* ignore it */
2045 thread_info->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
2046 /* fall through */
2047 case MSG_POSTED:
2048 thread_info->GetMessageExtraInfoVal = extra_info;
2049 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
2051 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2052 &info.msg.lParam, &buffer, size ))
2053 goto next; /* ignore it */
2055 *msg = info.msg;
2056 HeapFree( GetProcessHeap(), 0, buffer );
2057 return TRUE;
2060 /* if we get here, we have a sent message; call the window procedure */
2061 old_info = thread_info->receive_info;
2062 thread_info->receive_info = &info;
2063 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
2064 info.msg.lParam, (info.type != MSG_ASCII), FALSE );
2065 reply_message( &info, result, TRUE );
2066 thread_info->receive_info = old_info;
2067 next:
2068 HeapFree( GetProcessHeap(), 0, buffer );
2073 /***********************************************************************
2074 * process_sent_messages
2076 * Process all pending sent messages.
2078 inline static void process_sent_messages(void)
2080 MSG msg;
2081 peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
2085 /***********************************************************************
2086 * get_server_queue_handle
2088 * Get a handle to the server message queue for the current thread.
2090 static HANDLE get_server_queue_handle(void)
2092 struct user_thread_info *thread_info = get_user_thread_info();
2093 HANDLE ret;
2095 if (!(ret = thread_info->server_queue))
2097 SERVER_START_REQ( get_msg_queue )
2099 wine_server_call( req );
2100 ret = reply->handle;
2102 SERVER_END_REQ;
2103 thread_info->server_queue = ret;
2104 if (!ret) ERR( "Cannot get server thread queue\n" );
2106 return ret;
2110 /***********************************************************************
2111 * wait_message_reply
2113 * Wait until a sent message gets replied to.
2115 static void wait_message_reply( UINT flags )
2117 HANDLE server_queue = get_server_queue_handle();
2119 for (;;)
2121 unsigned int wake_bits = 0, changed_bits = 0;
2122 DWORD dwlc, res;
2124 SERVER_START_REQ( set_queue_mask )
2126 req->wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
2127 req->changed_mask = req->wake_mask;
2128 req->skip_wait = 1;
2129 if (!wine_server_call( req ))
2131 wake_bits = reply->wake_bits;
2132 changed_bits = reply->changed_bits;
2135 SERVER_END_REQ;
2137 if (wake_bits & QS_SMRESULT) return; /* got a result */
2138 if (wake_bits & QS_SENDMESSAGE)
2140 /* Process the sent message immediately */
2141 process_sent_messages();
2142 continue;
2145 /* now wait for it */
2147 ReleaseThunkLock( &dwlc );
2148 res = USER_Driver->pMsgWaitForMultipleObjectsEx( 1, &server_queue,
2149 INFINITE, QS_SENDMESSAGE, 0 );
2150 if (dwlc) RestoreThunkLock( dwlc );
2154 /***********************************************************************
2155 * put_message_in_queue
2157 * Put a sent message into the destination queue.
2158 * For inter-process message, reply_size is set to expected size of reply data.
2160 static BOOL put_message_in_queue( const struct send_message_info *info, size_t *reply_size )
2162 struct packed_message data;
2163 unsigned int res;
2164 int i, timeout = 0;
2166 /* Check for INFINITE timeout for compatibility with Win9x,
2167 * although Windows >= NT does not do so
2169 if (info->type != MSG_NOTIFY &&
2170 info->type != MSG_CALLBACK &&
2171 info->type != MSG_POSTED &&
2172 info->timeout != INFINITE)
2173 timeout = info->timeout;
2175 data.count = 0;
2176 if (info->type == MSG_OTHER_PROCESS)
2178 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
2179 if (data.count == -1)
2181 WARN( "cannot pack message %x\n", info->msg );
2182 return FALSE;
2185 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
2187 return post_dde_message( &data, info );
2190 SERVER_START_REQ( send_message )
2192 req->id = info->dest_tid;
2193 req->type = info->type;
2194 req->flags = 0;
2195 req->win = info->hwnd;
2196 req->msg = info->msg;
2197 req->wparam = info->wparam;
2198 req->lparam = info->lparam;
2199 req->time = GetCurrentTime();
2200 req->timeout = timeout;
2202 if (info->type == MSG_CALLBACK)
2204 req->callback = info->callback;
2205 req->info = info->data;
2208 if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
2209 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
2210 if ((res = wine_server_call( req )))
2212 if (res == STATUS_INVALID_PARAMETER)
2213 /* FIXME: find a STATUS_ value for this one */
2214 SetLastError( ERROR_INVALID_THREAD_ID );
2215 else
2216 SetLastError( RtlNtStatusToDosError(res) );
2219 SERVER_END_REQ;
2220 return !res;
2224 /***********************************************************************
2225 * retrieve_reply
2227 * Retrieve a message reply from the server.
2229 static LRESULT retrieve_reply( const struct send_message_info *info,
2230 size_t reply_size, LRESULT *result )
2232 NTSTATUS status;
2233 void *reply_data = NULL;
2235 if (reply_size)
2237 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
2239 WARN( "no memory for reply, will be truncated\n" );
2240 reply_size = 0;
2243 SERVER_START_REQ( get_message_reply )
2245 req->cancel = 1;
2246 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
2247 if (!(status = wine_server_call( req ))) *result = reply->result;
2248 reply_size = wine_server_reply_size( reply );
2250 SERVER_END_REQ;
2251 if (!status && reply_size)
2252 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
2254 HeapFree( GetProcessHeap(), 0, reply_data );
2256 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
2257 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
2258 info->lparam, *result, status );
2260 /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
2261 if (status) SetLastError( RtlNtStatusToDosError(status) );
2262 return !status;
2266 /***********************************************************************
2267 * send_inter_thread_message
2269 static LRESULT send_inter_thread_message( const struct send_message_info *info, LRESULT *res_ptr )
2271 size_t reply_size = 0;
2273 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
2274 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
2276 USER_CheckNotLock();
2278 if (!put_message_in_queue( info, &reply_size )) return 0;
2280 /* there's no reply to wait for on notify/callback messages */
2281 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
2283 wait_message_reply( info->flags );
2284 return retrieve_reply( info, reply_size, res_ptr );
2288 /***********************************************************************
2289 * MSG_SendInternalMessageTimeout
2291 * Same as SendMessageTimeoutW but sends the message to a specific thread
2292 * without requiring a window handle. Only works for internal Wine messages.
2294 LRESULT MSG_SendInternalMessageTimeout( DWORD dest_pid, DWORD dest_tid,
2295 UINT msg, WPARAM wparam, LPARAM lparam,
2296 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2298 struct send_message_info info;
2299 LRESULT ret, result;
2301 assert( msg & 0x80000000 ); /* must be an internal Wine message */
2303 info.type = MSG_UNICODE;
2304 info.dest_tid = dest_tid;
2305 info.hwnd = 0;
2306 info.msg = msg;
2307 info.wparam = wparam;
2308 info.lparam = lparam;
2309 info.flags = flags;
2310 info.timeout = timeout;
2312 if (USER_IsExitingThread( dest_tid )) return 0;
2314 if (dest_tid == GetCurrentThreadId())
2316 result = handle_internal_message( 0, msg, wparam, lparam );
2317 ret = 1;
2319 else
2321 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
2322 ret = send_inter_thread_message( &info, &result );
2324 if (ret && res_ptr) *res_ptr = result;
2325 return ret;
2329 /***********************************************************************
2330 * SendMessageTimeoutW (USER32.@)
2332 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2333 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2335 struct send_message_info info;
2336 DWORD dest_pid;
2337 LRESULT ret, result;
2339 info.type = MSG_UNICODE;
2340 info.hwnd = hwnd;
2341 info.msg = msg;
2342 info.wparam = wparam;
2343 info.lparam = lparam;
2344 info.flags = flags;
2345 info.timeout = timeout;
2347 if (is_broadcast(hwnd))
2349 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2350 if (res_ptr) *res_ptr = 1;
2351 return 1;
2354 if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
2356 if (USER_IsExitingThread( info.dest_tid )) return 0;
2358 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
2360 if (info.dest_tid == GetCurrentThreadId())
2362 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2363 ret = 1;
2365 else
2367 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
2368 ret = send_inter_thread_message( &info, &result );
2371 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
2372 if (ret && res_ptr) *res_ptr = result;
2373 return ret;
2376 static LRESULT send_inter_thread_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
2377 LRESULT *result, void *arg )
2379 struct send_message_info *info = arg;
2380 info->hwnd = hwnd;
2381 info->msg = msg;
2382 info->wparam = wp;
2383 info->lparam = lp;
2384 return send_inter_thread_message( info, result );
2387 /***********************************************************************
2388 * SendMessageTimeoutA (USER32.@)
2390 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2391 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2393 struct send_message_info info;
2394 DWORD dest_pid;
2395 LRESULT ret, result;
2397 info.type = MSG_ASCII;
2398 info.hwnd = hwnd;
2399 info.msg = msg;
2400 info.wparam = wparam;
2401 info.lparam = lparam;
2402 info.flags = flags;
2403 info.timeout = timeout;
2405 if (is_broadcast(hwnd))
2407 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2408 if (res_ptr) *res_ptr = 1;
2409 return 1;
2412 if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
2414 if (USER_IsExitingThread( info.dest_tid )) return 0;
2416 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
2418 if (info.dest_tid == GetCurrentThreadId())
2420 result = call_window_proc( hwnd, msg, wparam, lparam, FALSE, TRUE );
2421 ret = 1;
2423 else if (dest_pid == GetCurrentProcessId())
2425 ret = send_inter_thread_message( &info, &result );
2427 else
2429 /* inter-process message: need to map to Unicode */
2430 info.type = MSG_OTHER_PROCESS;
2431 if (is_unicode_message( info.msg ))
2432 ret = WINPROC_CallProcAtoW( send_inter_thread_callback, info.hwnd, info.msg,
2433 info.wparam, info.lparam, &result, &info );
2434 else ret = send_inter_thread_message( &info, &result );
2436 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
2437 if (ret && res_ptr) *res_ptr = result;
2438 return ret;
2442 /***********************************************************************
2443 * SendMessageW (USER32.@)
2445 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2447 DWORD_PTR res = 0;
2448 SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, 0, &res );
2449 return res;
2453 /***********************************************************************
2454 * SendMessageA (USER32.@)
2456 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2458 DWORD_PTR res = 0;
2459 SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, 0, &res );
2460 return res;
2464 /***********************************************************************
2465 * SendNotifyMessageA (USER32.@)
2467 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2469 return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
2473 /***********************************************************************
2474 * SendNotifyMessageW (USER32.@)
2476 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2478 struct send_message_info info;
2479 LRESULT result;
2481 if (is_pointer_message(msg))
2483 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2484 return FALSE;
2487 info.type = MSG_NOTIFY;
2488 info.hwnd = hwnd;
2489 info.msg = msg;
2490 info.wparam = wparam;
2491 info.lparam = lparam;
2492 info.flags = 0;
2494 if (is_broadcast(hwnd))
2496 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2497 return TRUE;
2500 if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2502 if (USER_IsExitingThread( info.dest_tid )) return TRUE;
2504 if (info.dest_tid == GetCurrentThreadId())
2506 call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2507 return TRUE;
2509 return send_inter_thread_message( &info, &result );
2513 /***********************************************************************
2514 * SendMessageCallbackA (USER32.@)
2516 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2517 SENDASYNCPROC callback, ULONG_PTR data )
2519 return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
2520 lparam, callback, data );
2524 /***********************************************************************
2525 * SendMessageCallbackW (USER32.@)
2527 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2528 SENDASYNCPROC callback, ULONG_PTR data )
2530 struct send_message_info info;
2531 LRESULT result;
2533 if (is_pointer_message(msg))
2535 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2536 return FALSE;
2539 info.type = MSG_CALLBACK;
2540 info.hwnd = hwnd;
2541 info.msg = msg;
2542 info.wparam = wparam;
2543 info.lparam = lparam;
2544 info.callback = callback;
2545 info.data = data;
2546 info.flags = 0;
2548 if (is_broadcast(hwnd))
2550 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2551 return TRUE;
2554 if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2556 if (USER_IsExitingThread( info.dest_tid )) return TRUE;
2558 if (info.dest_tid == GetCurrentThreadId())
2560 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2561 call_sendmsg_callback( callback, hwnd, msg, data, result );
2562 return TRUE;
2564 FIXME( "callback will not be called\n" );
2565 return send_inter_thread_message( &info, &result );
2569 /***********************************************************************
2570 * ReplyMessage (USER32.@)
2572 BOOL WINAPI ReplyMessage( LRESULT result )
2574 struct received_message_info *info = get_user_thread_info()->receive_info;
2576 if (!info) return FALSE;
2577 reply_message( info, result, FALSE );
2578 return TRUE;
2582 /***********************************************************************
2583 * InSendMessage (USER32.@)
2585 BOOL WINAPI InSendMessage(void)
2587 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
2591 /***********************************************************************
2592 * InSendMessageEx (USER32.@)
2594 DWORD WINAPI InSendMessageEx( LPVOID reserved )
2596 struct received_message_info *info = get_user_thread_info()->receive_info;
2598 if (info) return info->flags;
2599 return ISMEX_NOSEND;
2603 /***********************************************************************
2604 * PostMessageA (USER32.@)
2606 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2608 return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
2612 /***********************************************************************
2613 * PostMessageW (USER32.@)
2615 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2617 struct send_message_info info;
2619 if (is_pointer_message( msg ))
2621 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2622 return FALSE;
2625 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
2626 hwnd, msg, SPY_GetMsgName(msg, hwnd), wparam, lparam );
2628 info.type = MSG_POSTED;
2629 info.hwnd = hwnd;
2630 info.msg = msg;
2631 info.wparam = wparam;
2632 info.lparam = lparam;
2633 info.flags = 0;
2635 if (is_broadcast(hwnd))
2637 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2638 return TRUE;
2641 if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2643 if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2645 if (USER_IsExitingThread( info.dest_tid )) return TRUE;
2647 return put_message_in_queue( &info, NULL );
2651 /**********************************************************************
2652 * PostThreadMessageA (USER32.@)
2654 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2656 return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2660 /**********************************************************************
2661 * PostThreadMessageW (USER32.@)
2663 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2665 struct send_message_info info;
2667 if (is_pointer_message( msg ))
2669 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2670 return FALSE;
2672 if (USER_IsExitingThread( thread )) return TRUE;
2674 info.type = MSG_POSTED;
2675 info.dest_tid = thread;
2676 info.hwnd = 0;
2677 info.msg = msg;
2678 info.wparam = wparam;
2679 info.lparam = lparam;
2680 info.flags = 0;
2681 return put_message_in_queue( &info, NULL );
2685 /***********************************************************************
2686 * PostQuitMessage (USER32.@)
2688 * Posts a quit message to the current thread's message queue.
2690 * PARAMS
2691 * exit_code [I] Exit code to return from message loop.
2693 * RETURNS
2694 * Nothing.
2696 * NOTES
2697 * This function is not the same as calling:
2698 *|PostThreadMessage(GetCurrentThreadId(), WM_QUIT, exit_code, 0);
2699 * It instead sets a flag in the message queue that signals it to generate
2700 * a WM_QUIT message when there are no other pending sent or posted messages
2701 * in the queue.
2703 void WINAPI PostQuitMessage( INT exit_code )
2705 SERVER_START_REQ( post_quit_message )
2707 req->exit_code = exit_code;
2708 wine_server_call( req );
2710 SERVER_END_REQ;
2714 /***********************************************************************
2715 * PeekMessageW (USER32.@)
2717 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2719 struct user_thread_info *thread_info = get_user_thread_info();
2720 MSG msg;
2722 if (HIWORD(flags))
2723 FIXME("PM_QS_xxxx flags (%04x) are not handled\n", flags >> 16);
2725 USER_CheckNotLock();
2727 /* check for graphics events */
2728 USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
2730 hwnd = WIN_GetFullHandle( hwnd );
2732 for (;;)
2734 if (!peek_message( &msg, hwnd, first, last, (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2736 if (!(flags & PM_NOYIELD))
2738 DWORD count;
2739 ReleaseThunkLock(&count);
2740 NtYieldExecution();
2741 if (count) RestoreThunkLock(count);
2743 return FALSE;
2745 if (msg.message & 0x80000000)
2747 if (!(flags & PM_REMOVE))
2749 /* Have to remove the message explicitly.
2750 Do this before handling it, because the message handler may
2751 call PeekMessage again */
2752 peek_message( &msg, msg.hwnd, msg.message, msg.message, GET_MSG_REMOVE );
2754 handle_internal_message( msg.hwnd, msg.message, msg.wParam, msg.lParam );
2756 else break;
2759 thread_info->GetMessageTimeVal = msg.time;
2760 msg.pt.x = LOWORD( thread_info->GetMessagePosVal );
2761 msg.pt.y = HIWORD( thread_info->GetMessagePosVal );
2763 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
2765 /* copy back our internal safe copy of message data to msg_out.
2766 * msg_out is a variable from the *program*, so it can't be used
2767 * internally as it can get "corrupted" by our use of SendMessage()
2768 * (back to the program) inside the message handling itself. */
2769 if (!msg_out)
2771 SetLastError( ERROR_NOACCESS );
2772 return FALSE;
2774 *msg_out = msg;
2775 return TRUE;
2779 /***********************************************************************
2780 * PeekMessageA (USER32.@)
2782 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2784 BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2785 if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2786 return ret;
2790 /***********************************************************************
2791 * GetMessageW (USER32.@)
2793 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2795 HANDLE server_queue = get_server_queue_handle();
2796 int mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2798 if (first || last)
2800 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2801 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2802 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2803 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2804 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2805 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2807 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2809 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2811 /* wait until one of the bits is set */
2812 unsigned int wake_bits = 0, changed_bits = 0;
2813 DWORD dwlc;
2815 SERVER_START_REQ( set_queue_mask )
2817 req->wake_mask = QS_SENDMESSAGE;
2818 req->changed_mask = mask;
2819 req->skip_wait = 1;
2820 if (!wine_server_call( req ))
2822 wake_bits = reply->wake_bits;
2823 changed_bits = reply->changed_bits;
2826 SERVER_END_REQ;
2828 if (changed_bits & mask) continue;
2829 if (wake_bits & QS_SENDMESSAGE) continue;
2831 TRACE( "(%04lx) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2832 GetCurrentThreadId(), mask, wake_bits, changed_bits );
2834 ReleaseThunkLock( &dwlc );
2835 USER_Driver->pMsgWaitForMultipleObjectsEx( 1, &server_queue, INFINITE, QS_ALLINPUT, 0 );
2836 if (dwlc) RestoreThunkLock( dwlc );
2839 return (msg->message != WM_QUIT);
2843 /***********************************************************************
2844 * GetMessageA (USER32.@)
2846 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2848 GetMessageW( msg, hwnd, first, last );
2849 msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2850 return (msg->message != WM_QUIT);
2854 /***********************************************************************
2855 * IsDialogMessageA (USER32.@)
2856 * IsDialogMessage (USER32.@)
2858 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2860 MSG msg = *pmsg;
2861 msg.wParam = map_wparam_AtoW( msg.message, msg.wParam );
2862 return IsDialogMessageW( hwndDlg, &msg );
2866 /***********************************************************************
2867 * TranslateMessage (USER32.@)
2869 * Implementation of TranslateMessage.
2871 * TranslateMessage translates virtual-key messages into character-messages,
2872 * as follows :
2873 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
2874 * ditto replacing WM_* with WM_SYS*
2875 * This produces WM_CHAR messages only for keys mapped to ASCII characters
2876 * by the keyboard driver.
2878 * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
2879 * return value is nonzero, regardless of the translation.
2882 BOOL WINAPI TranslateMessage( const MSG *msg )
2884 UINT message;
2885 WCHAR wp[2];
2886 BYTE state[256];
2888 if (msg->message < WM_KEYFIRST || msg->message > WM_KEYLAST) return FALSE;
2889 if (msg->message != WM_KEYDOWN && msg->message != WM_SYSKEYDOWN) return TRUE;
2891 TRACE_(key)("Translating key %s (%04x), scancode %02x\n",
2892 SPY_GetVKeyName(msg->wParam), msg->wParam, LOBYTE(HIWORD(msg->lParam)));
2894 GetKeyboardState( state );
2895 /* FIXME : should handle ToUnicode yielding 2 */
2896 switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), state, wp, 2, 0))
2898 case 1:
2899 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
2900 TRACE_(key)("1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
2901 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
2902 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
2903 break;
2905 case -1:
2906 message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
2907 TRACE_(key)("-1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
2908 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
2909 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
2910 break;
2912 return TRUE;
2916 /***********************************************************************
2917 * DispatchMessageA (USER32.@)
2919 * See DispatchMessageW.
2921 LONG WINAPI DispatchMessageA( const MSG* msg )
2923 WND * wndPtr;
2924 LONG retval;
2925 WNDPROC winproc;
2927 /* Process timer messages */
2928 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
2930 if (msg->lParam) return CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
2931 msg->message, msg->wParam, GetTickCount() );
2934 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
2936 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2937 return 0;
2939 if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP)
2941 if (IsWindow( msg->hwnd )) SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2942 else SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2943 return 0;
2945 if (wndPtr->tid != GetCurrentThreadId())
2947 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2948 WIN_ReleasePtr( wndPtr );
2949 return 0;
2951 winproc = wndPtr->winproc;
2952 WIN_ReleasePtr( wndPtr );
2954 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
2955 msg->wParam, msg->lParam );
2956 retval = CallWindowProcA( winproc, msg->hwnd, msg->message,
2957 msg->wParam, msg->lParam );
2958 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
2959 msg->wParam, msg->lParam );
2961 if (msg->message == WM_PAINT)
2963 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
2964 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
2965 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
2966 DeleteObject( hrgn );
2968 return retval;
2972 /***********************************************************************
2973 * DispatchMessageW (USER32.@) Process a message
2975 * Process the message specified in the structure *_msg_.
2977 * If the lpMsg parameter points to a WM_TIMER message and the
2978 * parameter of the WM_TIMER message is not NULL, the lParam parameter
2979 * points to the function that is called instead of the window
2980 * procedure.
2982 * The message must be valid.
2984 * RETURNS
2986 * DispatchMessage() returns the result of the window procedure invoked.
2988 * CONFORMANCE
2990 * ECMA-234, Win32
2993 LONG WINAPI DispatchMessageW( const MSG* msg )
2995 WND * wndPtr;
2996 LONG retval;
2997 WNDPROC winproc;
2999 /* Process timer messages */
3000 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3002 if (msg->lParam) return CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
3003 msg->message, msg->wParam, GetTickCount() );
3006 if (!(wndPtr = WIN_GetPtr( msg->hwnd )))
3008 if (msg->hwnd) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3009 return 0;
3011 if (wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP)
3013 if (IsWindow( msg->hwnd )) SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3014 else SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3015 return 0;
3017 if (wndPtr->tid != GetCurrentThreadId())
3019 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3020 WIN_ReleasePtr( wndPtr );
3021 return 0;
3023 winproc = wndPtr->winproc;
3024 WIN_ReleasePtr( wndPtr );
3026 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3027 msg->wParam, msg->lParam );
3028 retval = CallWindowProcW( winproc, msg->hwnd, msg->message,
3029 msg->wParam, msg->lParam );
3030 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3031 msg->wParam, msg->lParam );
3033 if (msg->message == WM_PAINT)
3035 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3036 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3037 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3038 DeleteObject( hrgn );
3040 return retval;
3044 /***********************************************************************
3045 * GetMessagePos (USER.119)
3046 * GetMessagePos (USER32.@)
3048 * The GetMessagePos() function returns a long value representing a
3049 * cursor position, in screen coordinates, when the last message
3050 * retrieved by the GetMessage() function occurs. The x-coordinate is
3051 * in the low-order word of the return value, the y-coordinate is in
3052 * the high-order word. The application can use the MAKEPOINT()
3053 * macro to obtain a POINT structure from the return value.
3055 * For the current cursor position, use GetCursorPos().
3057 * RETURNS
3059 * Cursor position of last message on success, zero on failure.
3061 * CONFORMANCE
3063 * ECMA-234, Win32
3066 DWORD WINAPI GetMessagePos(void)
3068 return get_user_thread_info()->GetMessagePosVal;
3072 /***********************************************************************
3073 * GetMessageTime (USER.120)
3074 * GetMessageTime (USER32.@)
3076 * GetMessageTime() returns the message time for the last message
3077 * retrieved by the function. The time is measured in milliseconds with
3078 * the same offset as GetTickCount().
3080 * Since the tick count wraps, this is only useful for moderately short
3081 * relative time comparisons.
3083 * RETURNS
3085 * Time of last message on success, zero on failure.
3087 LONG WINAPI GetMessageTime(void)
3089 return get_user_thread_info()->GetMessageTimeVal;
3093 /***********************************************************************
3094 * GetMessageExtraInfo (USER.288)
3095 * GetMessageExtraInfo (USER32.@)
3097 LPARAM WINAPI GetMessageExtraInfo(void)
3099 return get_user_thread_info()->GetMessageExtraInfoVal;
3103 /***********************************************************************
3104 * SetMessageExtraInfo (USER32.@)
3106 LPARAM WINAPI SetMessageExtraInfo(LPARAM lParam)
3108 struct user_thread_info *thread_info = get_user_thread_info();
3109 LONG old_value = thread_info->GetMessageExtraInfoVal;
3110 thread_info->GetMessageExtraInfoVal = lParam;
3111 return old_value;
3115 /***********************************************************************
3116 * WaitMessage (USER.112) Suspend thread pending messages
3117 * WaitMessage (USER32.@) Suspend thread pending messages
3119 * WaitMessage() suspends a thread until events appear in the thread's
3120 * queue.
3122 BOOL WINAPI WaitMessage(void)
3124 return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
3128 /***********************************************************************
3129 * MsgWaitForMultipleObjectsEx (USER32.@)
3131 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
3132 DWORD timeout, DWORD mask, DWORD flags )
3134 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
3135 DWORD i, ret, lock;
3137 if (count > MAXIMUM_WAIT_OBJECTS-1)
3139 SetLastError( ERROR_INVALID_PARAMETER );
3140 return WAIT_FAILED;
3143 /* set the queue mask */
3144 SERVER_START_REQ( set_queue_mask )
3146 req->wake_mask = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
3147 req->changed_mask = mask;
3148 req->skip_wait = 0;
3149 wine_server_call( req );
3151 SERVER_END_REQ;
3153 /* Add the thread event to the handle list */
3154 for (i = 0; i < count; i++) handles[i] = pHandles[i];
3155 handles[count] = get_server_queue_handle();
3157 ReleaseThunkLock( &lock );
3158 ret = USER_Driver->pMsgWaitForMultipleObjectsEx( count+1, handles, timeout, mask, flags );
3159 if (ret == count+1) ret = count; /* pretend the msg queue is ready */
3160 if (lock) RestoreThunkLock( lock );
3161 return ret;
3165 /***********************************************************************
3166 * MsgWaitForMultipleObjects (USER32.@)
3168 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
3169 BOOL wait_all, DWORD timeout, DWORD mask )
3171 return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
3172 wait_all ? MWMO_WAITALL : 0 );
3176 /***********************************************************************
3177 * WaitForInputIdle (USER32.@)
3179 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
3181 DWORD start_time, elapsed, ret;
3182 HANDLE idle_event = (HANDLE)-1;
3184 SERVER_START_REQ( get_process_idle_event )
3186 req->handle = hProcess;
3187 if (!(ret = wine_server_call_err( req ))) idle_event = reply->event;
3189 SERVER_END_REQ;
3190 if (ret) return WAIT_FAILED; /* error */
3191 if (!idle_event) return 0; /* no event to wait on */
3193 start_time = GetTickCount();
3194 elapsed = 0;
3196 TRACE("waiting for %p\n", idle_event );
3199 ret = MsgWaitForMultipleObjects ( 1, &idle_event, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
3200 switch (ret)
3202 case WAIT_OBJECT_0+1:
3203 process_sent_messages();
3204 break;
3205 case WAIT_TIMEOUT:
3206 case WAIT_FAILED:
3207 TRACE("timeout or error\n");
3208 return ret;
3209 default:
3210 TRACE("finished\n");
3211 return 0;
3213 if (dwTimeOut != INFINITE)
3215 elapsed = GetTickCount() - start_time;
3216 if (elapsed > dwTimeOut)
3217 break;
3220 while (1);
3222 return WAIT_TIMEOUT;
3226 /***********************************************************************
3227 * UserYield (USER.332)
3229 void WINAPI UserYield16(void)
3231 DWORD count;
3233 /* Handle sent messages */
3234 process_sent_messages();
3236 /* Yield */
3237 ReleaseThunkLock(&count);
3239 if (count)
3241 RestoreThunkLock(count);
3242 /* Handle sent messages again */
3243 process_sent_messages();
3248 /***********************************************************************
3249 * RegisterWindowMessageA (USER32.@)
3250 * RegisterWindowMessage (USER.118)
3252 UINT WINAPI RegisterWindowMessageA( LPCSTR str )
3254 UINT ret = GlobalAddAtomA(str);
3255 TRACE("%s, ret=%x\n", str, ret);
3256 return ret;
3260 /***********************************************************************
3261 * RegisterWindowMessageW (USER32.@)
3263 UINT WINAPI RegisterWindowMessageW( LPCWSTR str )
3265 UINT ret = GlobalAddAtomW(str);
3266 TRACE("%s ret=%x\n", debugstr_w(str), ret);
3267 return ret;
3271 /***********************************************************************
3272 * BroadcastSystemMessageA (USER32.@)
3273 * BroadcastSystemMessage (USER32.@)
3275 LONG WINAPI BroadcastSystemMessageA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
3277 if ((*recipients & BSM_APPLICATIONS) || (*recipients == BSM_ALLCOMPONENTS))
3279 FIXME( "(%08lx,%08lx,%08x,%08x,%08lx): semi-stub!\n", flags, *recipients, msg, wp, lp );
3280 PostMessageA( HWND_BROADCAST, msg, wp, lp );
3281 return 1;
3283 else
3285 FIXME( "(%08lx,%08lx,%08x,%08x,%08lx): stub!\n", flags, *recipients, msg, wp, lp);
3286 return -1;
3291 /***********************************************************************
3292 * BroadcastSystemMessageW (USER32.@)
3294 LONG WINAPI BroadcastSystemMessageW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
3296 if ((*recipients & BSM_APPLICATIONS) || (*recipients == BSM_ALLCOMPONENTS))
3298 FIXME( "(%08lx,%08lx,%08x,%08x,%08lx): semi-stub!\n", flags, *recipients, msg, wp, lp );
3299 PostMessageW( HWND_BROADCAST, msg, wp, lp );
3300 return 1;
3302 else
3304 FIXME( "(%08lx,%08lx,%08x,%08x,%08lx): stub!\n", flags, *recipients, msg, wp, lp );
3305 return -1;
3310 /***********************************************************************
3311 * SetMessageQueue (USER32.@)
3313 BOOL WINAPI SetMessageQueue( INT size )
3315 /* now obsolete the message queue will be expanded dynamically as necessary */
3316 return TRUE;
3320 /***********************************************************************
3321 * MessageBeep (USER32.@)
3323 BOOL WINAPI MessageBeep( UINT i )
3325 BOOL active = TRUE;
3326 SystemParametersInfoA( SPI_GETBEEP, 0, &active, FALSE );
3327 if (active) USER_Driver->pBeep();
3328 return TRUE;
3332 /***********************************************************************
3333 * SetTimer (USER32.@)
3335 UINT_PTR WINAPI SetTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3337 UINT_PTR ret;
3338 WNDPROC winproc = 0;
3340 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, NULL );
3342 SERVER_START_REQ( set_win_timer )
3344 req->win = hwnd;
3345 req->msg = WM_TIMER;
3346 req->id = id;
3347 req->rate = max( timeout, SYS_TIMER_RATE );
3348 req->lparam = (unsigned long)winproc;
3349 if (!wine_server_call_err( req ))
3351 ret = reply->id;
3352 if (!ret) ret = TRUE;
3354 else ret = 0;
3356 SERVER_END_REQ;
3358 TRACE("Added %p %x %p timeout %d\n", hwnd, id, winproc, timeout );
3359 return ret;
3363 /***********************************************************************
3364 * SetSystemTimer (USER32.@)
3366 UINT_PTR WINAPI SetSystemTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3368 UINT_PTR ret;
3369 WNDPROC winproc = 0;
3371 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, NULL );
3373 SERVER_START_REQ( set_win_timer )
3375 req->win = hwnd;
3376 req->msg = WM_SYSTIMER;
3377 req->id = id;
3378 req->rate = max( timeout, SYS_TIMER_RATE );
3379 req->lparam = (unsigned long)winproc;
3380 if (!wine_server_call_err( req ))
3382 ret = reply->id;
3383 if (!ret) ret = TRUE;
3385 else ret = 0;
3387 SERVER_END_REQ;
3389 TRACE("Added %p %x %p timeout %d\n", hwnd, id, winproc, timeout );
3390 return ret;
3394 /***********************************************************************
3395 * KillTimer (USER32.@)
3397 BOOL WINAPI KillTimer( HWND hwnd, UINT_PTR id )
3399 BOOL ret;
3401 TRACE("%p %d\n", hwnd, id );
3403 SERVER_START_REQ( kill_win_timer )
3405 req->win = hwnd;
3406 req->msg = WM_TIMER;
3407 req->id = id;
3408 ret = !wine_server_call_err( req );
3410 SERVER_END_REQ;
3411 return ret;
3415 /***********************************************************************
3416 * KillSystemTimer (USER32.@)
3418 BOOL WINAPI KillSystemTimer( HWND hwnd, UINT_PTR id )
3420 BOOL ret;
3422 TRACE("%p %d\n", hwnd, id );
3424 SERVER_START_REQ( kill_win_timer )
3426 req->win = hwnd;
3427 req->msg = WM_SYSTIMER;
3428 req->id = id;
3429 ret = !wine_server_call_err( req );
3431 SERVER_END_REQ;
3432 return ret;
3436 /**********************************************************************
3437 * GetGUIThreadInfo (USER32.@)
3439 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
3441 BOOL ret;
3443 SERVER_START_REQ( get_thread_input )
3445 req->tid = id;
3446 if ((ret = !wine_server_call_err( req )))
3448 info->flags = 0;
3449 info->hwndActive = reply->active;
3450 info->hwndFocus = reply->focus;
3451 info->hwndCapture = reply->capture;
3452 info->hwndMenuOwner = reply->menu_owner;
3453 info->hwndMoveSize = reply->move_size;
3454 info->hwndCaret = reply->caret;
3455 info->rcCaret.left = reply->rect.left;
3456 info->rcCaret.top = reply->rect.top;
3457 info->rcCaret.right = reply->rect.right;
3458 info->rcCaret.bottom = reply->rect.bottom;
3459 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
3460 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
3461 if (reply->caret) info->flags |= GUI_CARETBLINKING;
3464 SERVER_END_REQ;
3465 return ret;
3469 /******************************************************************
3470 * IsHungAppWindow (USER32.@)
3473 BOOL WINAPI IsHungAppWindow( HWND hWnd )
3475 DWORD_PTR dwResult;
3476 return !SendMessageTimeoutA(hWnd, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 5000, &dwResult);