jscript: Add Map.prototype.forEach implementation.
[wine.git] / dlls / user32 / message.c
blobf87ef9fb3afc886c2b891471269fa1dd6bc60759
1 /*
2 * Window messaging support
4 * Copyright 2001 Alexandre Julliard
5 * Copyright 2008 Maarten Lankhorst
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <assert.h>
23 #include <stdarg.h>
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
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 "imm.h"
38 #include "ddk/imm.h"
39 #include "wine/server.h"
40 #include "user_private.h"
41 #include "win.h"
42 #include "controls.h"
43 #include "wine/debug.h"
44 #include "wine/exception.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(msg);
47 WINE_DECLARE_DEBUG_CHANNEL(relay);
48 WINE_DECLARE_DEBUG_CHANNEL(key);
50 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
51 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
53 #define MAX_PACK_COUNT 4
55 /* the various structures that can be sent in messages, in platform-independent layout */
56 struct packed_CREATESTRUCTW
58 ULONGLONG lpCreateParams;
59 ULONGLONG hInstance;
60 user_handle_t hMenu;
61 DWORD __pad1;
62 user_handle_t hwndParent;
63 DWORD __pad2;
64 INT cy;
65 INT cx;
66 INT y;
67 INT x;
68 LONG style;
69 ULONGLONG lpszName;
70 ULONGLONG lpszClass;
71 DWORD dwExStyle;
72 DWORD __pad3;
75 struct packed_DRAWITEMSTRUCT
77 UINT CtlType;
78 UINT CtlID;
79 UINT itemID;
80 UINT itemAction;
81 UINT itemState;
82 user_handle_t hwndItem;
83 DWORD __pad1;
84 user_handle_t hDC;
85 DWORD __pad2;
86 RECT rcItem;
87 ULONGLONG itemData;
90 struct packed_MEASUREITEMSTRUCT
92 UINT CtlType;
93 UINT CtlID;
94 UINT itemID;
95 UINT itemWidth;
96 UINT itemHeight;
97 ULONGLONG itemData;
100 struct packed_DELETEITEMSTRUCT
102 UINT CtlType;
103 UINT CtlID;
104 UINT itemID;
105 user_handle_t hwndItem;
106 DWORD __pad;
107 ULONGLONG itemData;
110 struct packed_COMPAREITEMSTRUCT
112 UINT CtlType;
113 UINT CtlID;
114 user_handle_t hwndItem;
115 DWORD __pad1;
116 UINT itemID1;
117 ULONGLONG itemData1;
118 UINT itemID2;
119 ULONGLONG itemData2;
120 DWORD dwLocaleId;
121 DWORD __pad2;
124 struct packed_WINDOWPOS
126 user_handle_t hwnd;
127 DWORD __pad1;
128 user_handle_t hwndInsertAfter;
129 DWORD __pad2;
130 INT x;
131 INT y;
132 INT cx;
133 INT cy;
134 UINT flags;
135 DWORD __pad3;
138 struct packed_COPYDATASTRUCT
140 ULONGLONG dwData;
141 DWORD cbData;
142 ULONGLONG lpData;
145 struct packed_HELPINFO
147 UINT cbSize;
148 INT iContextType;
149 INT iCtrlId;
150 user_handle_t hItemHandle;
151 DWORD __pad;
152 ULONGLONG dwContextId;
153 POINT MousePos;
156 struct packed_NCCALCSIZE_PARAMS
158 RECT rgrc[3];
159 ULONGLONG __pad1;
160 user_handle_t hwnd;
161 DWORD __pad2;
162 user_handle_t hwndInsertAfter;
163 DWORD __pad3;
164 INT x;
165 INT y;
166 INT cx;
167 INT cy;
168 UINT flags;
169 DWORD __pad4;
172 struct packed_MSG
174 user_handle_t hwnd;
175 DWORD __pad1;
176 UINT message;
177 ULONGLONG wParam;
178 ULONGLONG lParam;
179 DWORD time;
180 POINT pt;
181 DWORD __pad2;
184 struct packed_MDINEXTMENU
186 user_handle_t hmenuIn;
187 DWORD __pad1;
188 user_handle_t hmenuNext;
189 DWORD __pad2;
190 user_handle_t hwndNext;
191 DWORD __pad3;
194 struct packed_MDICREATESTRUCTW
196 ULONGLONG szClass;
197 ULONGLONG szTitle;
198 ULONGLONG hOwner;
199 INT x;
200 INT y;
201 INT cx;
202 INT cy;
203 DWORD style;
204 ULONGLONG lParam;
207 struct packed_hook_extra_info
209 user_handle_t handle;
210 DWORD __pad;
211 ULONGLONG lparam;
214 /* the structures are unpacked on top of the packed ones, so make sure they fit */
215 C_ASSERT( sizeof(struct packed_CREATESTRUCTW) >= sizeof(CREATESTRUCTW) );
216 C_ASSERT( sizeof(struct packed_DRAWITEMSTRUCT) >= sizeof(DRAWITEMSTRUCT) );
217 C_ASSERT( sizeof(struct packed_MEASUREITEMSTRUCT) >= sizeof(MEASUREITEMSTRUCT) );
218 C_ASSERT( sizeof(struct packed_DELETEITEMSTRUCT) >= sizeof(DELETEITEMSTRUCT) );
219 C_ASSERT( sizeof(struct packed_COMPAREITEMSTRUCT) >= sizeof(COMPAREITEMSTRUCT) );
220 C_ASSERT( sizeof(struct packed_WINDOWPOS) >= sizeof(WINDOWPOS) );
221 C_ASSERT( sizeof(struct packed_COPYDATASTRUCT) >= sizeof(COPYDATASTRUCT) );
222 C_ASSERT( sizeof(struct packed_HELPINFO) >= sizeof(HELPINFO) );
223 C_ASSERT( sizeof(struct packed_NCCALCSIZE_PARAMS) >= sizeof(NCCALCSIZE_PARAMS) + sizeof(WINDOWPOS) );
224 C_ASSERT( sizeof(struct packed_MSG) >= sizeof(MSG) );
225 C_ASSERT( sizeof(struct packed_MDINEXTMENU) >= sizeof(MDINEXTMENU) );
226 C_ASSERT( sizeof(struct packed_MDICREATESTRUCTW) >= sizeof(MDICREATESTRUCTW) );
227 C_ASSERT( sizeof(struct packed_hook_extra_info) >= sizeof(struct hook_extra_info) );
229 union packed_structs
231 struct packed_CREATESTRUCTW cs;
232 struct packed_DRAWITEMSTRUCT dis;
233 struct packed_MEASUREITEMSTRUCT mis;
234 struct packed_DELETEITEMSTRUCT dls;
235 struct packed_COMPAREITEMSTRUCT cis;
236 struct packed_WINDOWPOS wp;
237 struct packed_COPYDATASTRUCT cds;
238 struct packed_HELPINFO hi;
239 struct packed_NCCALCSIZE_PARAMS ncp;
240 struct packed_MSG msg;
241 struct packed_MDINEXTMENU mnm;
242 struct packed_MDICREATESTRUCTW mcs;
243 struct packed_hook_extra_info hook;
246 /* description of the data fields that need to be packed along with a sent message */
247 struct packed_message
249 union packed_structs ps;
250 int count;
251 const void *data[MAX_PACK_COUNT];
252 size_t size[MAX_PACK_COUNT];
255 /* info about the message currently being received by the current thread */
256 struct received_message_info
258 enum message_type type;
259 MSG msg;
260 UINT flags; /* InSendMessageEx return flags */
263 /* structure to group all parameters for sent messages of the various kinds */
264 struct send_message_info
266 enum message_type type;
267 DWORD dest_tid;
268 HWND hwnd;
269 UINT msg;
270 WPARAM wparam;
271 LPARAM lparam;
272 UINT flags; /* flags for SendMessageTimeout */
273 UINT timeout; /* timeout for SendMessageTimeout */
274 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
275 ULONG_PTR data; /* callback data */
276 enum wm_char_mapping wm_char;
279 static const INPUT_MESSAGE_SOURCE msg_source_unavailable = { IMDT_UNAVAILABLE, IMO_UNAVAILABLE };
282 /* Message class descriptor */
283 const struct builtin_class_descr MESSAGE_builtin_class =
285 L"Message", /* name */
286 0, /* style */
287 WINPROC_MESSAGE, /* proc */
288 0, /* extra */
289 0, /* cursor */
290 0 /* brush */
295 /* flag for messages that contain pointers */
296 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
298 #define SET(msg) (1 << ((msg) & 31))
300 static const unsigned int message_pointer_flags[] =
302 /* 0x00 - 0x1f */
303 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
304 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
305 /* 0x20 - 0x3f */
306 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
307 SET(WM_COMPAREITEM),
308 /* 0x40 - 0x5f */
309 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) | SET(WM_HELP),
310 /* 0x60 - 0x7f */
311 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
312 /* 0x80 - 0x9f */
313 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
314 /* 0xa0 - 0xbf */
315 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
316 /* 0xc0 - 0xdf */
317 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
318 /* 0xe0 - 0xff */
319 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO) | SET(SBM_GETSCROLLBARINFO),
320 /* 0x100 - 0x11f */
322 /* 0x120 - 0x13f */
324 /* 0x140 - 0x15f */
325 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
326 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
327 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
328 /* 0x160 - 0x17f */
330 /* 0x180 - 0x19f */
331 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
332 SET(LB_DIR) | SET(LB_FINDSTRING) |
333 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
334 /* 0x1a0 - 0x1bf */
335 SET(LB_FINDSTRINGEXACT),
336 /* 0x1c0 - 0x1df */
338 /* 0x1e0 - 0x1ff */
340 /* 0x200 - 0x21f */
341 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
342 /* 0x220 - 0x23f */
343 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
344 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
345 /* 0x240 - 0x25f */
347 /* 0x260 - 0x27f */
349 /* 0x280 - 0x29f */
351 /* 0x2a0 - 0x2bf */
353 /* 0x2c0 - 0x2df */
355 /* 0x2e0 - 0x2ff */
357 /* 0x300 - 0x31f */
358 SET(WM_ASKCBFORMATNAME)
361 /* flags for messages that contain Unicode strings */
362 static const unsigned int message_unicode_flags[] =
364 /* 0x00 - 0x1f */
365 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
366 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
367 /* 0x20 - 0x3f */
368 SET(WM_CHARTOITEM),
369 /* 0x40 - 0x5f */
371 /* 0x60 - 0x7f */
373 /* 0x80 - 0x9f */
374 SET(WM_NCCREATE),
375 /* 0xa0 - 0xbf */
377 /* 0xc0 - 0xdf */
378 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
379 /* 0xe0 - 0xff */
381 /* 0x100 - 0x11f */
382 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
383 /* 0x120 - 0x13f */
384 SET(WM_MENUCHAR),
385 /* 0x140 - 0x15f */
386 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
387 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
388 /* 0x160 - 0x17f */
390 /* 0x180 - 0x19f */
391 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
392 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
393 /* 0x1a0 - 0x1bf */
394 SET(LB_FINDSTRINGEXACT),
395 /* 0x1c0 - 0x1df */
397 /* 0x1e0 - 0x1ff */
399 /* 0x200 - 0x21f */
401 /* 0x220 - 0x23f */
402 SET(WM_MDICREATE),
403 /* 0x240 - 0x25f */
405 /* 0x260 - 0x27f */
407 /* 0x280 - 0x29f */
408 SET(WM_IME_CHAR),
409 /* 0x2a0 - 0x2bf */
411 /* 0x2c0 - 0x2df */
413 /* 0x2e0 - 0x2ff */
415 /* 0x300 - 0x31f */
416 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
419 /* check whether a given message type includes pointers */
420 static inline BOOL is_pointer_message( UINT message, WPARAM wparam )
422 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
423 if (message == WM_DEVICECHANGE && !(wparam & 0x8000)) return FALSE;
424 return (message_pointer_flags[message / 32] & SET(message)) != 0;
427 /* check whether a given message type contains Unicode (or ASCII) chars */
428 static inline BOOL is_unicode_message( UINT message )
430 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
431 return (message_unicode_flags[message / 32] & SET(message)) != 0;
434 #undef SET
436 /* add a data field to a packed message */
437 static inline void push_data( struct packed_message *data, const void *ptr, size_t size )
439 data->data[data->count] = ptr;
440 data->size[data->count] = size;
441 data->count++;
444 /* add a string to a packed message */
445 static inline void push_string( struct packed_message *data, LPCWSTR str )
447 push_data( data, str, (lstrlenW(str) + 1) * sizeof(WCHAR) );
450 /* make sure that the buffer contains a valid null-terminated Unicode string */
451 static inline BOOL check_string( LPCWSTR str, size_t size )
453 for (size /= sizeof(WCHAR); size; size--, str++)
454 if (!*str) return TRUE;
455 return FALSE;
458 /* pack a pointer into a 32/64 portable format */
459 static inline ULONGLONG pack_ptr( const void *ptr )
461 return (ULONG_PTR)ptr;
464 /* unpack a potentially 64-bit pointer, returning 0 when truncated */
465 static inline void *unpack_ptr( ULONGLONG ptr64 )
467 if ((ULONG_PTR)ptr64 != ptr64) return 0;
468 return (void *)(ULONG_PTR)ptr64;
471 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
472 static inline void *get_buffer_space( void **buffer, size_t size )
474 void *ret;
476 if (*buffer)
478 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
479 HeapFree( GetProcessHeap(), 0, *buffer );
481 else ret = HeapAlloc( GetProcessHeap(), 0, size );
483 *buffer = ret;
484 return ret;
487 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
488 static inline BOOL combobox_has_strings( HWND hwnd )
490 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
491 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
494 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
495 static inline BOOL listbox_has_strings( HWND hwnd )
497 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
498 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
501 /* check whether message is in the range of keyboard messages */
502 static inline BOOL is_keyboard_message( UINT message )
504 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
507 /* check whether message is in the range of mouse messages */
508 static inline BOOL is_mouse_message( UINT message )
510 return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
511 (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
514 /* check whether message matches the specified hwnd filter */
515 static inline BOOL check_hwnd_filter( const MSG *msg, HWND hwnd_filter )
517 if (!hwnd_filter || hwnd_filter == GetDesktopWindow()) return TRUE;
518 return (msg->hwnd == hwnd_filter || IsChild( hwnd_filter, msg->hwnd ));
521 /* check for pending WM_CHAR message with DBCS trailing byte */
522 static inline BOOL get_pending_wmchar( MSG *msg, UINT first, UINT last, BOOL remove )
524 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
526 if (!data || !data->get_msg.message) return FALSE;
527 if ((first || last) && (first > WM_CHAR || last < WM_CHAR)) return FALSE;
528 if (!msg) return FALSE;
529 *msg = data->get_msg;
530 if (remove) data->get_msg.message = 0;
531 return TRUE;
535 /***********************************************************************
536 * MessageWndProc
538 * Window procedure for "Message" windows (HWND_MESSAGE parent).
540 LRESULT WINAPI MessageWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
542 if (message == WM_NCCREATE) return TRUE;
543 return 0; /* all other messages are ignored */
547 /***********************************************************************
548 * broadcast_message_callback
550 * Helper callback for broadcasting messages.
552 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
554 struct send_message_info *info = (struct send_message_info *)lparam;
555 if ((GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) == WS_CHILD)
556 return TRUE;
557 switch(info->type)
559 case MSG_UNICODE:
560 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
561 info->flags, info->timeout, NULL );
562 break;
563 case MSG_ASCII:
564 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
565 info->flags, info->timeout, NULL );
566 break;
567 case MSG_NOTIFY:
568 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
569 break;
570 case MSG_CALLBACK:
571 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
572 info->callback, info->data );
573 break;
574 case MSG_POSTED:
575 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
576 break;
577 default:
578 ERR( "bad type %d\n", info->type );
579 break;
581 return TRUE;
584 DWORD get_input_codepage( void )
586 DWORD cp;
587 int ret;
588 HKL hkl = GetKeyboardLayout( 0 );
590 ret = GetLocaleInfoW( LOWORD(hkl), LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
591 (WCHAR *)&cp, sizeof(cp) / sizeof(WCHAR) );
592 if (!ret) cp = CP_ACP;
593 return cp;
596 /***********************************************************************
597 * map_wparam_AtoW
599 * Convert the wparam of an ASCII message to Unicode.
601 BOOL map_wparam_AtoW( UINT message, WPARAM *wparam, enum wm_char_mapping mapping )
603 char ch[2];
604 WCHAR wch[2];
605 DWORD cp;
607 wch[0] = wch[1] = 0;
608 switch(message)
610 case WM_CHAR:
611 /* WM_CHAR is magic: a DBCS char can be sent/posted as two consecutive WM_CHAR
612 * messages, in which case the first char is stored, and the conversion
613 * to Unicode only takes place once the second char is sent/posted.
615 if (mapping != WMCHAR_MAP_NOMAPPING)
617 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
618 BYTE low = LOBYTE(*wparam);
619 cp = get_input_codepage();
621 if (HIBYTE(*wparam))
623 ch[0] = low;
624 ch[1] = HIBYTE(*wparam);
625 MultiByteToWideChar( cp, 0, ch, 2, wch, 2 );
626 TRACE( "map %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
627 if (data) data->lead_byte[mapping] = 0;
629 else if (data && data->lead_byte[mapping])
631 ch[0] = data->lead_byte[mapping];
632 ch[1] = low;
633 MultiByteToWideChar( cp, 0, ch, 2, wch, 2 );
634 TRACE( "map stored %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
635 data->lead_byte[mapping] = 0;
637 else if (!IsDBCSLeadByte( low ))
639 ch[0] = low;
640 MultiByteToWideChar( cp, 0, ch, 1, wch, 2 );
641 TRACE( "map %02x -> %04x\n", (BYTE)ch[0], wch[0] );
642 if (data) data->lead_byte[mapping] = 0;
644 else /* store it and wait for trail byte */
646 if (!data)
648 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) )))
649 return FALSE;
650 get_user_thread_info()->wmchar_data = data;
652 TRACE( "storing lead byte %02x mapping %u\n", low, mapping );
653 data->lead_byte[mapping] = low;
654 return FALSE;
656 *wparam = MAKEWPARAM(wch[0], wch[1]);
657 break;
659 /* else fall through */
660 case WM_CHARTOITEM:
661 case EM_SETPASSWORDCHAR:
662 case WM_DEADCHAR:
663 case WM_SYSCHAR:
664 case WM_SYSDEADCHAR:
665 case WM_MENUCHAR:
666 cp = get_input_codepage();
667 ch[0] = LOBYTE(*wparam);
668 ch[1] = HIBYTE(*wparam);
669 MultiByteToWideChar( cp, 0, ch, 2, wch, 2 );
670 *wparam = MAKEWPARAM(wch[0], wch[1]);
671 break;
672 case WM_IME_CHAR:
673 cp = get_input_codepage();
674 ch[0] = HIBYTE(*wparam);
675 ch[1] = LOBYTE(*wparam);
676 if (ch[0]) MultiByteToWideChar( cp, 0, ch, 2, wch, 2 );
677 else MultiByteToWideChar( cp, 0, ch + 1, 1, wch, 1 );
678 *wparam = MAKEWPARAM(wch[0], HIWORD(*wparam));
679 break;
681 return TRUE;
685 /***********************************************************************
686 * map_wparam_WtoA
688 * Convert the wparam of a Unicode message to ASCII.
690 static void map_wparam_WtoA( MSG *msg, BOOL remove )
692 BYTE ch[4];
693 WCHAR wch[2];
694 DWORD len;
695 DWORD cp;
697 switch(msg->message)
699 case WM_CHAR:
700 if (!HIWORD(msg->wParam))
702 cp = get_input_codepage();
703 wch[0] = LOWORD(msg->wParam);
704 ch[0] = ch[1] = 0;
705 len = WideCharToMultiByte( cp, 0, wch, 1, (LPSTR)ch, 2, NULL, NULL );
706 if (len == 2) /* DBCS char */
708 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
709 if (!data)
711 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) ))) return;
712 get_user_thread_info()->wmchar_data = data;
714 if (remove)
716 data->get_msg = *msg;
717 data->get_msg.wParam = ch[1];
719 msg->wParam = ch[0];
720 return;
723 /* else fall through */
724 case WM_CHARTOITEM:
725 case EM_SETPASSWORDCHAR:
726 case WM_DEADCHAR:
727 case WM_SYSCHAR:
728 case WM_SYSDEADCHAR:
729 case WM_MENUCHAR:
730 cp = get_input_codepage();
731 wch[0] = LOWORD(msg->wParam);
732 wch[1] = HIWORD(msg->wParam);
733 ch[0] = ch[1] = 0;
734 WideCharToMultiByte( cp, 0, wch, 2, (LPSTR)ch, 4, NULL, NULL );
735 msg->wParam = MAKEWPARAM( ch[0] | (ch[1] << 8), 0 );
736 break;
737 case WM_IME_CHAR:
738 cp = get_input_codepage();
739 wch[0] = LOWORD(msg->wParam);
740 ch[0] = ch[1] = 0;
741 len = WideCharToMultiByte( cp, 0, wch, 1, (LPSTR)ch, 2, NULL, NULL );
742 if (len == 2)
743 msg->wParam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(msg->wParam) );
744 else
745 msg->wParam = MAKEWPARAM( ch[0], HIWORD(msg->wParam) );
746 break;
751 /***********************************************************************
752 * pack_message
754 * Pack a message for sending to another process.
755 * Return the size of the data we expect in the message reply.
756 * Set data->count to -1 if there is an error.
758 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
759 struct packed_message *data )
761 data->count = 0;
762 switch(message)
764 case WM_NCCREATE:
765 case WM_CREATE:
767 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
768 data->ps.cs.lpCreateParams = pack_ptr( cs->lpCreateParams );
769 data->ps.cs.hInstance = pack_ptr( cs->hInstance );
770 data->ps.cs.hMenu = wine_server_user_handle( cs->hMenu );
771 data->ps.cs.hwndParent = wine_server_user_handle( cs->hwndParent );
772 data->ps.cs.cy = cs->cy;
773 data->ps.cs.cx = cs->cx;
774 data->ps.cs.y = cs->y;
775 data->ps.cs.x = cs->x;
776 data->ps.cs.style = cs->style;
777 data->ps.cs.dwExStyle = cs->dwExStyle;
778 data->ps.cs.lpszName = pack_ptr( cs->lpszName );
779 data->ps.cs.lpszClass = pack_ptr( cs->lpszClass );
780 push_data( data, &data->ps.cs, sizeof(data->ps.cs) );
781 if (!IS_INTRESOURCE(cs->lpszName)) push_string( data, cs->lpszName );
782 if (!IS_INTRESOURCE(cs->lpszClass)) push_string( data, cs->lpszClass );
783 return sizeof(data->ps.cs);
785 case WM_GETTEXT:
786 case WM_ASKCBFORMATNAME:
787 return wparam * sizeof(WCHAR);
788 case WM_WININICHANGE:
789 if (lparam) push_string(data, (LPWSTR)lparam );
790 return 0;
791 case WM_SETTEXT:
792 case WM_DEVMODECHANGE:
793 case CB_DIR:
794 case LB_DIR:
795 case LB_ADDFILE:
796 case EM_REPLACESEL:
797 push_string( data, (LPWSTR)lparam );
798 return 0;
799 case WM_GETMINMAXINFO:
800 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
801 return sizeof(MINMAXINFO);
802 case WM_DRAWITEM:
804 DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)lparam;
805 data->ps.dis.CtlType = dis->CtlType;
806 data->ps.dis.CtlID = dis->CtlID;
807 data->ps.dis.itemID = dis->itemID;
808 data->ps.dis.itemAction = dis->itemAction;
809 data->ps.dis.itemState = dis->itemState;
810 data->ps.dis.hwndItem = wine_server_user_handle( dis->hwndItem );
811 data->ps.dis.hDC = wine_server_user_handle( dis->hDC ); /* FIXME */
812 data->ps.dis.rcItem = dis->rcItem;
813 data->ps.dis.itemData = dis->itemData;
814 push_data( data, &data->ps.dis, sizeof(data->ps.dis) );
815 return 0;
817 case WM_MEASUREITEM:
819 MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lparam;
820 data->ps.mis.CtlType = mis->CtlType;
821 data->ps.mis.CtlID = mis->CtlID;
822 data->ps.mis.itemID = mis->itemID;
823 data->ps.mis.itemWidth = mis->itemWidth;
824 data->ps.mis.itemHeight = mis->itemHeight;
825 data->ps.mis.itemData = mis->itemData;
826 push_data( data, &data->ps.mis, sizeof(data->ps.mis) );
827 return sizeof(data->ps.mis);
829 case WM_DELETEITEM:
831 DELETEITEMSTRUCT *dls = (DELETEITEMSTRUCT *)lparam;
832 data->ps.dls.CtlType = dls->CtlType;
833 data->ps.dls.CtlID = dls->CtlID;
834 data->ps.dls.itemID = dls->itemID;
835 data->ps.dls.hwndItem = wine_server_user_handle( dls->hwndItem );
836 data->ps.dls.itemData = dls->itemData;
837 push_data( data, &data->ps.dls, sizeof(data->ps.dls) );
838 return 0;
840 case WM_COMPAREITEM:
842 COMPAREITEMSTRUCT *cis = (COMPAREITEMSTRUCT *)lparam;
843 data->ps.cis.CtlType = cis->CtlType;
844 data->ps.cis.CtlID = cis->CtlID;
845 data->ps.cis.hwndItem = wine_server_user_handle( cis->hwndItem );
846 data->ps.cis.itemID1 = cis->itemID1;
847 data->ps.cis.itemData1 = cis->itemData1;
848 data->ps.cis.itemID2 = cis->itemID2;
849 data->ps.cis.itemData2 = cis->itemData2;
850 data->ps.cis.dwLocaleId = cis->dwLocaleId;
851 push_data( data, &data->ps.cis, sizeof(data->ps.cis) );
852 return 0;
854 case WM_WINE_SETWINDOWPOS:
855 case WM_WINDOWPOSCHANGING:
856 case WM_WINDOWPOSCHANGED:
858 WINDOWPOS *wp = (WINDOWPOS *)lparam;
859 data->ps.wp.hwnd = wine_server_user_handle( wp->hwnd );
860 data->ps.wp.hwndInsertAfter = wine_server_user_handle( wp->hwndInsertAfter );
861 data->ps.wp.x = wp->x;
862 data->ps.wp.y = wp->y;
863 data->ps.wp.cx = wp->cx;
864 data->ps.wp.cy = wp->cy;
865 data->ps.wp.flags = wp->flags;
866 push_data( data, &data->ps.wp, sizeof(data->ps.wp) );
867 return sizeof(data->ps.wp);
869 case WM_COPYDATA:
871 COPYDATASTRUCT *cds = (COPYDATASTRUCT *)lparam;
872 data->ps.cds.cbData = cds->cbData;
873 data->ps.cds.dwData = cds->dwData;
874 data->ps.cds.lpData = pack_ptr( cds->lpData );
875 push_data( data, &data->ps.cds, sizeof(data->ps.cds) );
876 if (cds->lpData) push_data( data, cds->lpData, cds->cbData );
877 return 0;
879 case WM_NOTIFY:
880 /* WM_NOTIFY cannot be sent across processes (MSDN) */
881 data->count = -1;
882 return 0;
883 case WM_HELP:
885 HELPINFO *hi = (HELPINFO *)lparam;
886 data->ps.hi.iContextType = hi->iContextType;
887 data->ps.hi.iCtrlId = hi->iCtrlId;
888 data->ps.hi.hItemHandle = wine_server_user_handle( hi->hItemHandle );
889 data->ps.hi.dwContextId = hi->dwContextId;
890 data->ps.hi.MousePos = hi->MousePos;
891 push_data( data, &data->ps.hi, sizeof(data->ps.hi) );
892 return 0;
894 case WM_STYLECHANGING:
895 case WM_STYLECHANGED:
896 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
897 return 0;
898 case WM_NCCALCSIZE:
899 if (!wparam)
901 push_data( data, (RECT *)lparam, sizeof(RECT) );
902 return sizeof(RECT);
904 else
906 NCCALCSIZE_PARAMS *ncp = (NCCALCSIZE_PARAMS *)lparam;
907 data->ps.ncp.rgrc[0] = ncp->rgrc[0];
908 data->ps.ncp.rgrc[1] = ncp->rgrc[1];
909 data->ps.ncp.rgrc[2] = ncp->rgrc[2];
910 data->ps.ncp.hwnd = wine_server_user_handle( ncp->lppos->hwnd );
911 data->ps.ncp.hwndInsertAfter = wine_server_user_handle( ncp->lppos->hwndInsertAfter );
912 data->ps.ncp.x = ncp->lppos->x;
913 data->ps.ncp.y = ncp->lppos->y;
914 data->ps.ncp.cx = ncp->lppos->cx;
915 data->ps.ncp.cy = ncp->lppos->cy;
916 data->ps.ncp.flags = ncp->lppos->flags;
917 push_data( data, &data->ps.ncp, sizeof(data->ps.ncp) );
918 return sizeof(data->ps.ncp);
920 case WM_GETDLGCODE:
921 if (lparam)
923 MSG *msg = (MSG *)lparam;
924 data->ps.msg.hwnd = wine_server_user_handle( msg->hwnd );
925 data->ps.msg.message = msg->message;
926 data->ps.msg.wParam = msg->wParam;
927 data->ps.msg.lParam = msg->lParam;
928 data->ps.msg.time = msg->time;
929 data->ps.msg.pt = msg->pt;
930 push_data( data, &data->ps.msg, sizeof(data->ps.msg) );
931 return sizeof(data->ps.msg);
933 return 0;
934 case SBM_SETSCROLLINFO:
935 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
936 return 0;
937 case SBM_GETSCROLLINFO:
938 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
939 return sizeof(SCROLLINFO);
940 case SBM_GETSCROLLBARINFO:
942 const SCROLLBARINFO *info = (const SCROLLBARINFO *)lparam;
943 size_t size = min( info->cbSize, sizeof(SCROLLBARINFO) );
944 push_data( data, info, size );
945 return size;
947 case EM_GETSEL:
948 case SBM_GETRANGE:
949 case CB_GETEDITSEL:
951 size_t size = 0;
952 if (wparam) size += sizeof(DWORD);
953 if (lparam) size += sizeof(DWORD);
954 return size;
956 case EM_GETRECT:
957 case LB_GETITEMRECT:
958 case CB_GETDROPPEDCONTROLRECT:
959 return sizeof(RECT);
960 case EM_SETRECT:
961 case EM_SETRECTNP:
962 push_data( data, (RECT *)lparam, sizeof(RECT) );
963 return 0;
964 case EM_GETLINE:
966 WORD *pw = (WORD *)lparam;
967 push_data( data, pw, sizeof(*pw) );
968 return *pw * sizeof(WCHAR);
970 case EM_SETTABSTOPS:
971 case LB_SETTABSTOPS:
972 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
973 return 0;
974 case CB_ADDSTRING:
975 case CB_INSERTSTRING:
976 case CB_FINDSTRING:
977 case CB_FINDSTRINGEXACT:
978 case CB_SELECTSTRING:
979 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
980 return 0;
981 case CB_GETLBTEXT:
982 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
983 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
984 case LB_ADDSTRING:
985 case LB_INSERTSTRING:
986 case LB_FINDSTRING:
987 case LB_FINDSTRINGEXACT:
988 case LB_SELECTSTRING:
989 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
990 return 0;
991 case LB_GETTEXT:
992 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
993 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
994 case LB_GETSELITEMS:
995 return wparam * sizeof(UINT);
996 case WM_NEXTMENU:
998 MDINEXTMENU *mnm = (MDINEXTMENU *)lparam;
999 data->ps.mnm.hmenuIn = wine_server_user_handle( mnm->hmenuIn );
1000 data->ps.mnm.hmenuNext = wine_server_user_handle( mnm->hmenuNext );
1001 data->ps.mnm.hwndNext = wine_server_user_handle( mnm->hwndNext );
1002 push_data( data, &data->ps.mnm, sizeof(data->ps.mnm) );
1003 return sizeof(data->ps.mnm);
1005 case WM_SIZING:
1006 case WM_MOVING:
1007 push_data( data, (RECT *)lparam, sizeof(RECT) );
1008 return sizeof(RECT);
1009 case WM_MDICREATE:
1011 MDICREATESTRUCTW *mcs = (MDICREATESTRUCTW *)lparam;
1012 data->ps.mcs.szClass = pack_ptr( mcs->szClass );
1013 data->ps.mcs.szTitle = pack_ptr( mcs->szTitle );
1014 data->ps.mcs.hOwner = pack_ptr( mcs->hOwner );
1015 data->ps.mcs.x = mcs->x;
1016 data->ps.mcs.y = mcs->y;
1017 data->ps.mcs.cx = mcs->cx;
1018 data->ps.mcs.cy = mcs->cy;
1019 data->ps.mcs.style = mcs->style;
1020 data->ps.mcs.lParam = mcs->lParam;
1021 push_data( data, &data->ps.mcs, sizeof(data->ps.mcs) );
1022 if (!IS_INTRESOURCE(mcs->szClass)) push_string( data, mcs->szClass );
1023 if (!IS_INTRESOURCE(mcs->szTitle)) push_string( data, mcs->szTitle );
1024 return sizeof(data->ps.mcs);
1026 case WM_MDIGETACTIVE:
1027 if (lparam) return sizeof(BOOL);
1028 return 0;
1029 case WM_DEVICECHANGE:
1031 DEV_BROADCAST_HDR *header = (DEV_BROADCAST_HDR *)lparam;
1032 if ((wparam & 0x8000) && header) push_data( data, header, header->dbch_size );
1033 return 0;
1035 case WM_WINE_KEYBOARD_LL_HOOK:
1037 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
1038 data->ps.hook.handle = wine_server_user_handle( h_extra->handle );
1039 push_data( data, &data->ps.hook, sizeof(data->ps.hook) );
1040 push_data( data, (LPVOID)h_extra->lparam, sizeof(KBDLLHOOKSTRUCT) );
1041 return 0;
1043 case WM_WINE_MOUSE_LL_HOOK:
1045 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
1046 data->ps.hook.handle = wine_server_user_handle( h_extra->handle );
1047 push_data( data, &data->ps.hook, sizeof(data->ps.hook) );
1048 push_data( data, (LPVOID)h_extra->lparam, sizeof(MSLLHOOKSTRUCT) );
1049 return 0;
1051 case WM_NCPAINT:
1052 if (wparam <= 1) return 0;
1053 FIXME( "WM_NCPAINT hdc packing not supported yet\n" );
1054 data->count = -1;
1055 return 0;
1056 case WM_PAINT:
1057 if (!wparam) return 0;
1058 /* fall through */
1060 /* these contain an HFONT */
1061 case WM_SETFONT:
1062 case WM_GETFONT:
1063 /* these contain an HDC */
1064 case WM_ERASEBKGND:
1065 case WM_ICONERASEBKGND:
1066 case WM_CTLCOLORMSGBOX:
1067 case WM_CTLCOLOREDIT:
1068 case WM_CTLCOLORLISTBOX:
1069 case WM_CTLCOLORBTN:
1070 case WM_CTLCOLORDLG:
1071 case WM_CTLCOLORSCROLLBAR:
1072 case WM_CTLCOLORSTATIC:
1073 case WM_PRINT:
1074 case WM_PRINTCLIENT:
1075 /* these contain an HGLOBAL */
1076 case WM_PAINTCLIPBOARD:
1077 case WM_SIZECLIPBOARD:
1078 /* these contain HICON */
1079 case WM_GETICON:
1080 case WM_SETICON:
1081 case WM_QUERYDRAGICON:
1082 case WM_QUERYPARKICON:
1083 /* these contain pointers */
1084 case WM_DROPOBJECT:
1085 case WM_QUERYDROPOBJECT:
1086 case WM_DRAGLOOP:
1087 case WM_DRAGSELECT:
1088 case WM_DRAGMOVE:
1089 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
1090 data->count = -1;
1091 return 0;
1093 return 0;
1097 /***********************************************************************
1098 * unpack_message
1100 * Unpack a message received from another process.
1102 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1103 void **buffer, size_t size )
1105 size_t minsize = 0;
1106 union packed_structs *ps = *buffer;
1108 switch(message)
1110 case WM_NCCREATE:
1111 case WM_CREATE:
1113 CREATESTRUCTW cs;
1114 WCHAR *str = (WCHAR *)(&ps->cs + 1);
1115 if (size < sizeof(ps->cs)) return FALSE;
1116 size -= sizeof(ps->cs);
1117 cs.lpCreateParams = unpack_ptr( ps->cs.lpCreateParams );
1118 cs.hInstance = unpack_ptr( ps->cs.hInstance );
1119 cs.hMenu = wine_server_ptr_handle( ps->cs.hMenu );
1120 cs.hwndParent = wine_server_ptr_handle( ps->cs.hwndParent );
1121 cs.cy = ps->cs.cy;
1122 cs.cx = ps->cs.cx;
1123 cs.y = ps->cs.y;
1124 cs.x = ps->cs.x;
1125 cs.style = ps->cs.style;
1126 cs.dwExStyle = ps->cs.dwExStyle;
1127 cs.lpszName = unpack_ptr( ps->cs.lpszName );
1128 cs.lpszClass = unpack_ptr( ps->cs.lpszClass );
1129 if (ps->cs.lpszName >> 16)
1131 if (!check_string( str, size )) return FALSE;
1132 cs.lpszName = str;
1133 size -= (lstrlenW(str) + 1) * sizeof(WCHAR);
1134 str += lstrlenW(str) + 1;
1136 if (ps->cs.lpszClass >> 16)
1138 if (!check_string( str, size )) return FALSE;
1139 cs.lpszClass = str;
1141 memcpy( &ps->cs, &cs, sizeof(cs) );
1142 break;
1144 case WM_GETTEXT:
1145 case WM_ASKCBFORMATNAME:
1146 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
1147 break;
1148 case WM_WININICHANGE:
1149 if (!*lparam) return TRUE;
1150 /* fall through */
1151 case WM_SETTEXT:
1152 case WM_DEVMODECHANGE:
1153 case CB_DIR:
1154 case LB_DIR:
1155 case LB_ADDFILE:
1156 case EM_REPLACESEL:
1157 if (!check_string( *buffer, size )) return FALSE;
1158 break;
1159 case WM_GETMINMAXINFO:
1160 minsize = sizeof(MINMAXINFO);
1161 break;
1162 case WM_DRAWITEM:
1164 DRAWITEMSTRUCT dis;
1165 if (size < sizeof(ps->dis)) return FALSE;
1166 dis.CtlType = ps->dis.CtlType;
1167 dis.CtlID = ps->dis.CtlID;
1168 dis.itemID = ps->dis.itemID;
1169 dis.itemAction = ps->dis.itemAction;
1170 dis.itemState = ps->dis.itemState;
1171 dis.hwndItem = wine_server_ptr_handle( ps->dis.hwndItem );
1172 dis.hDC = wine_server_ptr_handle( ps->dis.hDC );
1173 dis.rcItem = ps->dis.rcItem;
1174 dis.itemData = (ULONG_PTR)unpack_ptr( ps->dis.itemData );
1175 memcpy( &ps->dis, &dis, sizeof(dis) );
1176 break;
1178 case WM_MEASUREITEM:
1180 MEASUREITEMSTRUCT mis;
1181 if (size < sizeof(ps->mis)) return FALSE;
1182 mis.CtlType = ps->mis.CtlType;
1183 mis.CtlID = ps->mis.CtlID;
1184 mis.itemID = ps->mis.itemID;
1185 mis.itemWidth = ps->mis.itemWidth;
1186 mis.itemHeight = ps->mis.itemHeight;
1187 mis.itemData = (ULONG_PTR)unpack_ptr( ps->mis.itemData );
1188 memcpy( &ps->mis, &mis, sizeof(mis) );
1189 break;
1191 case WM_DELETEITEM:
1193 DELETEITEMSTRUCT dls;
1194 if (size < sizeof(ps->dls)) return FALSE;
1195 dls.CtlType = ps->dls.CtlType;
1196 dls.CtlID = ps->dls.CtlID;
1197 dls.itemID = ps->dls.itemID;
1198 dls.hwndItem = wine_server_ptr_handle( ps->dls.hwndItem );
1199 dls.itemData = (ULONG_PTR)unpack_ptr( ps->dls.itemData );
1200 memcpy( &ps->dls, &dls, sizeof(dls) );
1201 break;
1203 case WM_COMPAREITEM:
1205 COMPAREITEMSTRUCT cis;
1206 if (size < sizeof(ps->cis)) return FALSE;
1207 cis.CtlType = ps->cis.CtlType;
1208 cis.CtlID = ps->cis.CtlID;
1209 cis.hwndItem = wine_server_ptr_handle( ps->cis.hwndItem );
1210 cis.itemID1 = ps->cis.itemID1;
1211 cis.itemData1 = (ULONG_PTR)unpack_ptr( ps->cis.itemData1 );
1212 cis.itemID2 = ps->cis.itemID2;
1213 cis.itemData2 = (ULONG_PTR)unpack_ptr( ps->cis.itemData2 );
1214 cis.dwLocaleId = ps->cis.dwLocaleId;
1215 memcpy( &ps->cis, &cis, sizeof(cis) );
1216 break;
1218 case WM_WINDOWPOSCHANGING:
1219 case WM_WINDOWPOSCHANGED:
1220 case WM_WINE_SETWINDOWPOS:
1222 WINDOWPOS wp;
1223 if (size < sizeof(ps->wp)) return FALSE;
1224 wp.hwnd = wine_server_ptr_handle( ps->wp.hwnd );
1225 wp.hwndInsertAfter = wine_server_ptr_handle( ps->wp.hwndInsertAfter );
1226 wp.x = ps->wp.x;
1227 wp.y = ps->wp.y;
1228 wp.cx = ps->wp.cx;
1229 wp.cy = ps->wp.cy;
1230 wp.flags = ps->wp.flags;
1231 memcpy( &ps->wp, &wp, sizeof(wp) );
1232 break;
1234 case WM_COPYDATA:
1236 COPYDATASTRUCT cds;
1237 if (size < sizeof(ps->cds)) return FALSE;
1238 cds.dwData = (ULONG_PTR)unpack_ptr( ps->cds.dwData );
1239 if (ps->cds.lpData)
1241 cds.cbData = ps->cds.cbData;
1242 cds.lpData = &ps->cds + 1;
1243 minsize = sizeof(ps->cds) + cds.cbData;
1245 else
1247 cds.cbData = 0;
1248 cds.lpData = 0;
1250 memcpy( &ps->cds, &cds, sizeof(cds) );
1251 break;
1253 case WM_NOTIFY:
1254 /* WM_NOTIFY cannot be sent across processes (MSDN) */
1255 return FALSE;
1256 case WM_HELP:
1258 HELPINFO hi;
1259 if (size < sizeof(ps->hi)) return FALSE;
1260 hi.cbSize = sizeof(hi);
1261 hi.iContextType = ps->hi.iContextType;
1262 hi.iCtrlId = ps->hi.iCtrlId;
1263 hi.hItemHandle = wine_server_ptr_handle( ps->hi.hItemHandle );
1264 hi.dwContextId = (ULONG_PTR)unpack_ptr( ps->hi.dwContextId );
1265 hi.MousePos = ps->hi.MousePos;
1266 memcpy( &ps->hi, &hi, sizeof(hi) );
1267 break;
1269 case WM_STYLECHANGING:
1270 case WM_STYLECHANGED:
1271 minsize = sizeof(STYLESTRUCT);
1272 break;
1273 case WM_NCCALCSIZE:
1274 if (!*wparam) minsize = sizeof(RECT);
1275 else
1277 NCCALCSIZE_PARAMS ncp;
1278 WINDOWPOS wp;
1279 if (size < sizeof(ps->ncp)) return FALSE;
1280 ncp.rgrc[0] = ps->ncp.rgrc[0];
1281 ncp.rgrc[1] = ps->ncp.rgrc[1];
1282 ncp.rgrc[2] = ps->ncp.rgrc[2];
1283 wp.hwnd = wine_server_ptr_handle( ps->ncp.hwnd );
1284 wp.hwndInsertAfter = wine_server_ptr_handle( ps->ncp.hwndInsertAfter );
1285 wp.x = ps->ncp.x;
1286 wp.y = ps->ncp.y;
1287 wp.cx = ps->ncp.cx;
1288 wp.cy = ps->ncp.cy;
1289 wp.flags = ps->ncp.flags;
1290 ncp.lppos = (WINDOWPOS *)((NCCALCSIZE_PARAMS *)&ps->ncp + 1);
1291 memcpy( &ps->ncp, &ncp, sizeof(ncp) );
1292 *ncp.lppos = wp;
1294 break;
1295 case WM_GETDLGCODE:
1296 if (*lparam)
1298 MSG msg;
1299 if (size < sizeof(ps->msg)) return FALSE;
1300 msg.hwnd = wine_server_ptr_handle( ps->msg.hwnd );
1301 msg.message = ps->msg.message;
1302 msg.wParam = (ULONG_PTR)unpack_ptr( ps->msg.wParam );
1303 msg.lParam = (ULONG_PTR)unpack_ptr( ps->msg.lParam );
1304 msg.time = ps->msg.time;
1305 msg.pt = ps->msg.pt;
1306 memcpy( &ps->msg, &msg, sizeof(msg) );
1307 break;
1309 return TRUE;
1310 case SBM_SETSCROLLINFO:
1311 minsize = sizeof(SCROLLINFO);
1312 break;
1313 case SBM_GETSCROLLINFO:
1314 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
1315 break;
1316 case SBM_GETSCROLLBARINFO:
1317 if (!get_buffer_space( buffer, sizeof(SCROLLBARINFO ))) return FALSE;
1318 break;
1319 case EM_GETSEL:
1320 case SBM_GETRANGE:
1321 case CB_GETEDITSEL:
1322 if (*wparam || *lparam)
1324 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
1325 if (*wparam) *wparam = (WPARAM)*buffer;
1326 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
1328 return TRUE;
1329 case EM_GETRECT:
1330 case LB_GETITEMRECT:
1331 case CB_GETDROPPEDCONTROLRECT:
1332 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
1333 break;
1334 case EM_SETRECT:
1335 case EM_SETRECTNP:
1336 minsize = sizeof(RECT);
1337 break;
1338 case EM_GETLINE:
1340 WORD len;
1341 if (size < sizeof(WORD)) return FALSE;
1342 len = *(WORD *)*buffer;
1343 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
1344 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
1345 return TRUE;
1347 case EM_SETTABSTOPS:
1348 case LB_SETTABSTOPS:
1349 if (!*wparam) return TRUE;
1350 minsize = *wparam * sizeof(UINT);
1351 break;
1352 case CB_ADDSTRING:
1353 case CB_INSERTSTRING:
1354 case CB_FINDSTRING:
1355 case CB_FINDSTRINGEXACT:
1356 case CB_SELECTSTRING:
1357 case LB_ADDSTRING:
1358 case LB_INSERTSTRING:
1359 case LB_FINDSTRING:
1360 case LB_FINDSTRINGEXACT:
1361 case LB_SELECTSTRING:
1362 if (!*buffer) return TRUE;
1363 if (!check_string( *buffer, size )) return FALSE;
1364 break;
1365 case CB_GETLBTEXT:
1367 size = sizeof(ULONG_PTR);
1368 if (combobox_has_strings( hwnd ))
1369 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
1370 if (!get_buffer_space( buffer, size )) return FALSE;
1371 break;
1373 case LB_GETTEXT:
1375 size = sizeof(ULONG_PTR);
1376 if (listbox_has_strings( hwnd ))
1377 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
1378 if (!get_buffer_space( buffer, size )) return FALSE;
1379 break;
1381 case LB_GETSELITEMS:
1382 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
1383 break;
1384 case WM_NEXTMENU:
1386 MDINEXTMENU mnm;
1387 if (size < sizeof(ps->mnm)) return FALSE;
1388 mnm.hmenuIn = wine_server_ptr_handle( ps->mnm.hmenuIn );
1389 mnm.hmenuNext = wine_server_ptr_handle( ps->mnm.hmenuNext );
1390 mnm.hwndNext = wine_server_ptr_handle( ps->mnm.hwndNext );
1391 memcpy( &ps->mnm, &mnm, sizeof(mnm) );
1392 break;
1394 case WM_SIZING:
1395 case WM_MOVING:
1396 minsize = sizeof(RECT);
1397 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
1398 break;
1399 case WM_MDICREATE:
1401 MDICREATESTRUCTW mcs;
1402 WCHAR *str = (WCHAR *)(&ps->mcs + 1);
1403 if (size < sizeof(ps->mcs)) return FALSE;
1404 size -= sizeof(ps->mcs);
1406 mcs.szClass = unpack_ptr( ps->mcs.szClass );
1407 mcs.szTitle = unpack_ptr( ps->mcs.szTitle );
1408 mcs.hOwner = unpack_ptr( ps->mcs.hOwner );
1409 mcs.x = ps->mcs.x;
1410 mcs.y = ps->mcs.y;
1411 mcs.cx = ps->mcs.cx;
1412 mcs.cy = ps->mcs.cy;
1413 mcs.style = ps->mcs.style;
1414 mcs.lParam = (LPARAM)unpack_ptr( ps->mcs.lParam );
1415 if (ps->mcs.szClass >> 16)
1417 if (!check_string( str, size )) return FALSE;
1418 mcs.szClass = str;
1419 size -= (lstrlenW(str) + 1) * sizeof(WCHAR);
1420 str += lstrlenW(str) + 1;
1422 if (ps->mcs.szTitle >> 16)
1424 if (!check_string( str, size )) return FALSE;
1425 mcs.szTitle = str;
1427 memcpy( &ps->mcs, &mcs, sizeof(mcs) );
1428 break;
1430 case WM_MDIGETACTIVE:
1431 if (!*lparam) return TRUE;
1432 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
1433 break;
1434 case WM_DEVICECHANGE:
1435 if (!(*wparam & 0x8000)) return TRUE;
1436 minsize = sizeof(DEV_BROADCAST_HDR);
1437 break;
1438 case WM_WINE_KEYBOARD_LL_HOOK:
1439 case WM_WINE_MOUSE_LL_HOOK:
1441 struct hook_extra_info h_extra;
1442 minsize = sizeof(ps->hook) +
1443 (message == WM_WINE_KEYBOARD_LL_HOOK ? sizeof(KBDLLHOOKSTRUCT)
1444 : sizeof(MSLLHOOKSTRUCT));
1445 if (size < minsize) return FALSE;
1446 h_extra.handle = wine_server_ptr_handle( ps->hook.handle );
1447 h_extra.lparam = (LPARAM)(&ps->hook + 1);
1448 memcpy( &ps->hook, &h_extra, sizeof(h_extra) );
1449 break;
1451 case WM_NCPAINT:
1452 if (*wparam <= 1) return TRUE;
1453 FIXME( "WM_NCPAINT hdc unpacking not supported\n" );
1454 return FALSE;
1455 case WM_PAINT:
1456 if (!*wparam) return TRUE;
1457 /* fall through */
1459 /* these contain an HFONT */
1460 case WM_SETFONT:
1461 case WM_GETFONT:
1462 /* these contain an HDC */
1463 case WM_ERASEBKGND:
1464 case WM_ICONERASEBKGND:
1465 case WM_CTLCOLORMSGBOX:
1466 case WM_CTLCOLOREDIT:
1467 case WM_CTLCOLORLISTBOX:
1468 case WM_CTLCOLORBTN:
1469 case WM_CTLCOLORDLG:
1470 case WM_CTLCOLORSCROLLBAR:
1471 case WM_CTLCOLORSTATIC:
1472 case WM_PRINT:
1473 case WM_PRINTCLIENT:
1474 /* these contain an HGLOBAL */
1475 case WM_PAINTCLIPBOARD:
1476 case WM_SIZECLIPBOARD:
1477 /* these contain HICON */
1478 case WM_GETICON:
1479 case WM_SETICON:
1480 case WM_QUERYDRAGICON:
1481 case WM_QUERYPARKICON:
1482 /* these contain pointers */
1483 case WM_DROPOBJECT:
1484 case WM_QUERYDROPOBJECT:
1485 case WM_DRAGLOOP:
1486 case WM_DRAGSELECT:
1487 case WM_DRAGMOVE:
1488 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
1489 return FALSE;
1491 default:
1492 return TRUE; /* message doesn't need any unpacking */
1495 /* default exit for most messages: check minsize and store buffer in lparam */
1496 if (size < minsize) return FALSE;
1497 *lparam = (LPARAM)*buffer;
1498 return TRUE;
1502 /***********************************************************************
1503 * pack_reply
1505 * Pack a reply to a message for sending to another process.
1507 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1508 LRESULT res, struct packed_message *data )
1510 data->count = 0;
1511 switch(message)
1513 case WM_NCCREATE:
1514 case WM_CREATE:
1516 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
1517 data->ps.cs.lpCreateParams = (ULONG_PTR)cs->lpCreateParams;
1518 data->ps.cs.hInstance = (ULONG_PTR)cs->hInstance;
1519 data->ps.cs.hMenu = wine_server_user_handle( cs->hMenu );
1520 data->ps.cs.hwndParent = wine_server_user_handle( cs->hwndParent );
1521 data->ps.cs.cy = cs->cy;
1522 data->ps.cs.cx = cs->cx;
1523 data->ps.cs.y = cs->y;
1524 data->ps.cs.x = cs->x;
1525 data->ps.cs.style = cs->style;
1526 data->ps.cs.dwExStyle = cs->dwExStyle;
1527 data->ps.cs.lpszName = (ULONG_PTR)cs->lpszName;
1528 data->ps.cs.lpszClass = (ULONG_PTR)cs->lpszClass;
1529 push_data( data, &data->ps.cs, sizeof(data->ps.cs) );
1530 break;
1532 case WM_GETTEXT:
1533 case CB_GETLBTEXT:
1534 case LB_GETTEXT:
1535 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
1536 break;
1537 case WM_GETMINMAXINFO:
1538 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
1539 break;
1540 case WM_MEASUREITEM:
1542 MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lparam;
1543 data->ps.mis.CtlType = mis->CtlType;
1544 data->ps.mis.CtlID = mis->CtlID;
1545 data->ps.mis.itemID = mis->itemID;
1546 data->ps.mis.itemWidth = mis->itemWidth;
1547 data->ps.mis.itemHeight = mis->itemHeight;
1548 data->ps.mis.itemData = mis->itemData;
1549 push_data( data, &data->ps.mis, sizeof(data->ps.mis) );
1550 break;
1552 case WM_WINDOWPOSCHANGING:
1553 case WM_WINDOWPOSCHANGED:
1555 WINDOWPOS *wp = (WINDOWPOS *)lparam;
1556 data->ps.wp.hwnd = wine_server_user_handle( wp->hwnd );
1557 data->ps.wp.hwndInsertAfter = wine_server_user_handle( wp->hwndInsertAfter );
1558 data->ps.wp.x = wp->x;
1559 data->ps.wp.y = wp->y;
1560 data->ps.wp.cx = wp->cx;
1561 data->ps.wp.cy = wp->cy;
1562 data->ps.wp.flags = wp->flags;
1563 push_data( data, &data->ps.wp, sizeof(data->ps.wp) );
1564 break;
1566 case WM_GETDLGCODE:
1567 if (lparam)
1569 MSG *msg = (MSG *)lparam;
1570 data->ps.msg.hwnd = wine_server_user_handle( msg->hwnd );
1571 data->ps.msg.message = msg->message;
1572 data->ps.msg.wParam = msg->wParam;
1573 data->ps.msg.lParam = msg->lParam;
1574 data->ps.msg.time = msg->time;
1575 data->ps.msg.pt = msg->pt;
1576 push_data( data, &data->ps.msg, sizeof(data->ps.msg) );
1578 break;
1579 case SBM_GETSCROLLINFO:
1580 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
1581 break;
1582 case EM_GETRECT:
1583 case LB_GETITEMRECT:
1584 case CB_GETDROPPEDCONTROLRECT:
1585 case WM_SIZING:
1586 case WM_MOVING:
1587 push_data( data, (RECT *)lparam, sizeof(RECT) );
1588 break;
1589 case EM_GETLINE:
1591 WORD *ptr = (WORD *)lparam;
1592 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
1593 break;
1595 case LB_GETSELITEMS:
1596 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
1597 break;
1598 case WM_MDIGETACTIVE:
1599 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
1600 break;
1601 case WM_NCCALCSIZE:
1602 if (!wparam)
1603 push_data( data, (RECT *)lparam, sizeof(RECT) );
1604 else
1606 NCCALCSIZE_PARAMS *ncp = (NCCALCSIZE_PARAMS *)lparam;
1607 data->ps.ncp.rgrc[0] = ncp->rgrc[0];
1608 data->ps.ncp.rgrc[1] = ncp->rgrc[1];
1609 data->ps.ncp.rgrc[2] = ncp->rgrc[2];
1610 data->ps.ncp.hwnd = wine_server_user_handle( ncp->lppos->hwnd );
1611 data->ps.ncp.hwndInsertAfter = wine_server_user_handle( ncp->lppos->hwndInsertAfter );
1612 data->ps.ncp.x = ncp->lppos->x;
1613 data->ps.ncp.y = ncp->lppos->y;
1614 data->ps.ncp.cx = ncp->lppos->cx;
1615 data->ps.ncp.cy = ncp->lppos->cy;
1616 data->ps.ncp.flags = ncp->lppos->flags;
1617 push_data( data, &data->ps.ncp, sizeof(data->ps.ncp) );
1619 break;
1620 case EM_GETSEL:
1621 case SBM_GETRANGE:
1622 case CB_GETEDITSEL:
1623 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
1624 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
1625 break;
1626 case WM_NEXTMENU:
1628 MDINEXTMENU *mnm = (MDINEXTMENU *)lparam;
1629 data->ps.mnm.hmenuIn = wine_server_user_handle( mnm->hmenuIn );
1630 data->ps.mnm.hmenuNext = wine_server_user_handle( mnm->hmenuNext );
1631 data->ps.mnm.hwndNext = wine_server_user_handle( mnm->hwndNext );
1632 push_data( data, &data->ps.mnm, sizeof(data->ps.mnm) );
1633 break;
1635 case WM_MDICREATE:
1637 MDICREATESTRUCTW *mcs = (MDICREATESTRUCTW *)lparam;
1638 data->ps.mcs.szClass = pack_ptr( mcs->szClass );
1639 data->ps.mcs.szTitle = pack_ptr( mcs->szTitle );
1640 data->ps.mcs.hOwner = pack_ptr( mcs->hOwner );
1641 data->ps.mcs.x = mcs->x;
1642 data->ps.mcs.y = mcs->y;
1643 data->ps.mcs.cx = mcs->cx;
1644 data->ps.mcs.cy = mcs->cy;
1645 data->ps.mcs.style = mcs->style;
1646 data->ps.mcs.lParam = mcs->lParam;
1647 push_data( data, &data->ps.mcs, sizeof(data->ps.mcs) );
1648 break;
1650 case WM_ASKCBFORMATNAME:
1651 push_data( data, (WCHAR *)lparam, (lstrlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
1652 break;
1657 /***********************************************************************
1658 * unpack_reply
1660 * Unpack a message reply received from another process.
1662 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1663 void *buffer, size_t size )
1665 union packed_structs *ps = buffer;
1667 switch(message)
1669 case WM_NCCREATE:
1670 case WM_CREATE:
1671 if (size >= sizeof(ps->cs))
1673 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
1674 cs->lpCreateParams = unpack_ptr( ps->cs.lpCreateParams );
1675 cs->hInstance = unpack_ptr( ps->cs.hInstance );
1676 cs->hMenu = wine_server_ptr_handle( ps->cs.hMenu );
1677 cs->hwndParent = wine_server_ptr_handle( ps->cs.hwndParent );
1678 cs->cy = ps->cs.cy;
1679 cs->cx = ps->cs.cx;
1680 cs->y = ps->cs.y;
1681 cs->x = ps->cs.x;
1682 cs->style = ps->cs.style;
1683 cs->dwExStyle = ps->cs.dwExStyle;
1684 /* don't allow changing name and class pointers */
1686 break;
1687 case WM_GETTEXT:
1688 case WM_ASKCBFORMATNAME:
1689 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
1690 break;
1691 case WM_GETMINMAXINFO:
1692 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
1693 break;
1694 case WM_MEASUREITEM:
1695 if (size >= sizeof(ps->mis))
1697 MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lparam;
1698 mis->CtlType = ps->mis.CtlType;
1699 mis->CtlID = ps->mis.CtlID;
1700 mis->itemID = ps->mis.itemID;
1701 mis->itemWidth = ps->mis.itemWidth;
1702 mis->itemHeight = ps->mis.itemHeight;
1703 mis->itemData = (ULONG_PTR)unpack_ptr( ps->mis.itemData );
1705 break;
1706 case WM_WINDOWPOSCHANGING:
1707 case WM_WINDOWPOSCHANGED:
1708 if (size >= sizeof(ps->wp))
1710 WINDOWPOS *wp = (WINDOWPOS *)lparam;
1711 wp->hwnd = wine_server_ptr_handle( ps->wp.hwnd );
1712 wp->hwndInsertAfter = wine_server_ptr_handle( ps->wp.hwndInsertAfter );
1713 wp->x = ps->wp.x;
1714 wp->y = ps->wp.y;
1715 wp->cx = ps->wp.cx;
1716 wp->cy = ps->wp.cy;
1717 wp->flags = ps->wp.flags;
1719 break;
1720 case WM_GETDLGCODE:
1721 if (lparam && size >= sizeof(ps->msg))
1723 MSG *msg = (MSG *)lparam;
1724 msg->hwnd = wine_server_ptr_handle( ps->msg.hwnd );
1725 msg->message = ps->msg.message;
1726 msg->wParam = (ULONG_PTR)unpack_ptr( ps->msg.wParam );
1727 msg->lParam = (ULONG_PTR)unpack_ptr( ps->msg.lParam );
1728 msg->time = ps->msg.time;
1729 msg->pt = ps->msg.pt;
1731 break;
1732 case SBM_GETSCROLLINFO:
1733 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
1734 break;
1735 case SBM_GETSCROLLBARINFO:
1736 memcpy( (SCROLLBARINFO *)lparam, buffer, min( sizeof(SCROLLBARINFO), size ));
1737 break;
1738 case EM_GETRECT:
1739 case CB_GETDROPPEDCONTROLRECT:
1740 case LB_GETITEMRECT:
1741 case WM_SIZING:
1742 case WM_MOVING:
1743 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1744 break;
1745 case EM_GETLINE:
1746 size = min( size, (size_t)*(WORD *)lparam );
1747 memcpy( (WCHAR *)lparam, buffer, size );
1748 break;
1749 case LB_GETSELITEMS:
1750 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
1751 break;
1752 case LB_GETTEXT:
1753 case CB_GETLBTEXT:
1754 memcpy( (WCHAR *)lparam, buffer, size );
1755 break;
1756 case WM_NEXTMENU:
1757 if (size >= sizeof(ps->mnm))
1759 MDINEXTMENU *mnm = (MDINEXTMENU *)lparam;
1760 mnm->hmenuIn = wine_server_ptr_handle( ps->mnm.hmenuIn );
1761 mnm->hmenuNext = wine_server_ptr_handle( ps->mnm.hmenuNext );
1762 mnm->hwndNext = wine_server_ptr_handle( ps->mnm.hwndNext );
1764 break;
1765 case WM_MDIGETACTIVE:
1766 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
1767 break;
1768 case WM_NCCALCSIZE:
1769 if (!wparam)
1770 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1771 else if (size >= sizeof(ps->ncp))
1773 NCCALCSIZE_PARAMS *ncp = (NCCALCSIZE_PARAMS *)lparam;
1774 ncp->rgrc[0] = ps->ncp.rgrc[0];
1775 ncp->rgrc[1] = ps->ncp.rgrc[1];
1776 ncp->rgrc[2] = ps->ncp.rgrc[2];
1777 ncp->lppos->hwnd = wine_server_ptr_handle( ps->ncp.hwnd );
1778 ncp->lppos->hwndInsertAfter = wine_server_ptr_handle( ps->ncp.hwndInsertAfter );
1779 ncp->lppos->x = ps->ncp.x;
1780 ncp->lppos->y = ps->ncp.y;
1781 ncp->lppos->cx = ps->ncp.cx;
1782 ncp->lppos->cy = ps->ncp.cy;
1783 ncp->lppos->flags = ps->ncp.flags;
1785 break;
1786 case EM_GETSEL:
1787 case SBM_GETRANGE:
1788 case CB_GETEDITSEL:
1789 if (wparam)
1791 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1792 if (size <= sizeof(DWORD)) break;
1793 size -= sizeof(DWORD);
1794 buffer = (DWORD *)buffer + 1;
1796 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1797 break;
1798 case WM_MDICREATE:
1799 if (size >= sizeof(ps->mcs))
1801 MDICREATESTRUCTW *mcs = (MDICREATESTRUCTW *)lparam;
1802 mcs->hOwner = unpack_ptr( ps->mcs.hOwner );
1803 mcs->x = ps->mcs.x;
1804 mcs->y = ps->mcs.y;
1805 mcs->cx = ps->mcs.cx;
1806 mcs->cy = ps->mcs.cy;
1807 mcs->style = ps->mcs.style;
1808 mcs->lParam = (LPARAM)unpack_ptr( ps->mcs.lParam );
1809 /* don't allow changing class and title pointers */
1811 break;
1812 default:
1813 ERR( "should not happen: unexpected message %x\n", message );
1814 break;
1819 /***********************************************************************
1820 * reply_message
1822 * Send a reply to a sent message.
1824 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1826 struct packed_message data;
1827 int i, replied = info->flags & ISMEX_REPLIED;
1829 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1830 if (!remove && replied) return; /* replied already */
1832 memset( &data, 0, sizeof(data) );
1833 info->flags |= ISMEX_REPLIED;
1835 if (info->type == MSG_OTHER_PROCESS && !replied)
1837 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1838 info->msg.lParam, result, &data );
1841 SERVER_START_REQ( reply_message )
1843 req->result = result;
1844 req->remove = remove;
1845 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1846 wine_server_call( req );
1848 SERVER_END_REQ;
1852 /***********************************************************************
1853 * handle_internal_message
1855 * Handle an internal Wine message instead of calling the window proc.
1857 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1859 switch(msg)
1861 case WM_WINE_DESTROYWINDOW:
1862 return WIN_DestroyWindow( hwnd );
1863 case WM_WINE_SETWINDOWPOS:
1864 if (is_desktop_window( hwnd )) return 0;
1865 return USER_SetWindowPos( (WINDOWPOS *)lparam, 0, 0 );
1866 case WM_WINE_SHOWWINDOW:
1867 if (is_desktop_window( hwnd )) return 0;
1868 return ShowWindow( hwnd, wparam );
1869 case WM_WINE_SETPARENT:
1870 if (is_desktop_window( hwnd )) return 0;
1871 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1872 case WM_WINE_SETWINDOWLONG:
1873 return WIN_SetWindowLong( hwnd, (short)LOWORD(wparam), HIWORD(wparam), lparam, TRUE );
1874 case WM_WINE_SETSTYLE:
1875 if (is_desktop_window( hwnd )) return 0;
1876 return WIN_SetStyle(hwnd, wparam, lparam);
1877 case WM_WINE_SETACTIVEWINDOW:
1878 if (!wparam && GetForegroundWindow() == hwnd) return 0;
1879 return (LRESULT)SetActiveWindow( (HWND)wparam );
1880 case WM_WINE_KEYBOARD_LL_HOOK:
1881 case WM_WINE_MOUSE_LL_HOOK:
1883 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
1885 return call_current_hook( h_extra->handle, HC_ACTION, wparam, h_extra->lparam );
1887 case WM_WINE_CLIPCURSOR:
1888 if (wparam)
1890 RECT rect;
1891 GetClipCursor( &rect );
1892 return USER_Driver->pClipCursor( &rect );
1894 return USER_Driver->pClipCursor( NULL );
1895 case WM_WINE_UPDATEWINDOWSTATE:
1896 update_window_state( hwnd );
1897 return 0;
1898 default:
1899 if (msg >= WM_WINE_FIRST_DRIVER_MSG && msg <= WM_WINE_LAST_DRIVER_MSG)
1900 return USER_Driver->pWindowMessage( hwnd, msg, wparam, lparam );
1901 FIXME( "unknown internal message %x\n", msg );
1902 return 0;
1906 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1907 * to the memory handle, we keep track (in the server side) of all pairs of handle
1908 * used (the client passes its value and the content of the memory handle), and
1909 * the server stored both values (the client, and the local one, created after the
1910 * content). When a ACK message is generated, the list of pair is searched for a
1911 * matching pair, so that the client memory handle can be returned.
1913 struct DDE_pair {
1914 HGLOBAL client_hMem;
1915 HGLOBAL server_hMem;
1918 static struct DDE_pair* dde_pairs;
1919 static int dde_num_alloc;
1920 static int dde_num_used;
1922 static CRITICAL_SECTION dde_crst;
1923 static CRITICAL_SECTION_DEBUG critsect_debug =
1925 0, 0, &dde_crst,
1926 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
1927 0, 0, { (DWORD_PTR)(__FILE__ ": dde_crst") }
1929 static CRITICAL_SECTION dde_crst = { &critsect_debug, -1, 0, 0, 0, 0 };
1931 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1933 int i;
1934 #define GROWBY 4
1936 EnterCriticalSection(&dde_crst);
1938 /* now remember the pair of hMem on both sides */
1939 if (dde_num_used == dde_num_alloc)
1941 struct DDE_pair* tmp;
1942 if (dde_pairs)
1943 tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1944 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1945 else
1946 tmp = HeapAlloc( GetProcessHeap(), 0,
1947 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1949 if (!tmp)
1951 LeaveCriticalSection(&dde_crst);
1952 return FALSE;
1954 dde_pairs = tmp;
1955 /* zero out newly allocated part */
1956 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1957 dde_num_alloc += GROWBY;
1959 #undef GROWBY
1960 for (i = 0; i < dde_num_alloc; i++)
1962 if (dde_pairs[i].server_hMem == 0)
1964 dde_pairs[i].client_hMem = chm;
1965 dde_pairs[i].server_hMem = shm;
1966 dde_num_used++;
1967 break;
1970 LeaveCriticalSection(&dde_crst);
1971 return TRUE;
1974 static HGLOBAL dde_get_pair(HGLOBAL shm)
1976 int i;
1977 HGLOBAL ret = 0;
1979 EnterCriticalSection(&dde_crst);
1980 for (i = 0; i < dde_num_alloc; i++)
1982 if (dde_pairs[i].server_hMem == shm)
1984 /* free this pair */
1985 dde_pairs[i].server_hMem = 0;
1986 dde_num_used--;
1987 ret = dde_pairs[i].client_hMem;
1988 break;
1991 LeaveCriticalSection(&dde_crst);
1992 return ret;
1995 /***********************************************************************
1996 * post_dde_message
1998 * Post a DDE message
2000 static BOOL post_dde_message( struct packed_message *data, const struct send_message_info *info )
2002 void* ptr = NULL;
2003 int size = 0;
2004 UINT_PTR uiLo, uiHi;
2005 LPARAM lp;
2006 HGLOBAL hunlock = 0;
2007 int i;
2008 DWORD res;
2009 ULONGLONG hpack;
2011 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
2012 return FALSE;
2014 lp = info->lparam;
2015 switch (info->msg)
2017 /* DDE messages which don't require packing are:
2018 * WM_DDE_INITIATE
2019 * WM_DDE_TERMINATE
2020 * WM_DDE_REQUEST
2021 * WM_DDE_UNADVISE
2023 case WM_DDE_ACK:
2024 if (HIWORD(uiHi))
2026 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
2027 HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
2028 if (h)
2030 hpack = pack_ptr( h );
2031 /* send back the value of h on the other side */
2032 push_data( data, &hpack, sizeof(hpack) );
2033 lp = uiLo;
2034 TRACE( "send dde-ack %lx %08lx => %p\n", uiLo, uiHi, h );
2037 else
2039 /* uiHi should contain either an atom or 0 */
2040 TRACE( "send dde-ack %lx atom=%lx\n", uiLo, uiHi );
2041 lp = MAKELONG( uiLo, uiHi );
2043 break;
2044 case WM_DDE_ADVISE:
2045 case WM_DDE_DATA:
2046 case WM_DDE_POKE:
2047 size = 0;
2048 if (uiLo)
2050 size = GlobalSize( (HGLOBAL)uiLo ) ;
2051 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
2052 (info->msg == WM_DDE_DATA && size < FIELD_OFFSET(DDEDATA, Value)) ||
2053 (info->msg == WM_DDE_POKE && size < FIELD_OFFSET(DDEPOKE, Value))
2055 return FALSE;
2057 else if (info->msg != WM_DDE_DATA) return FALSE;
2059 lp = uiHi;
2060 if (uiLo)
2062 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
2064 DDEDATA *dde_data = ptr;
2065 TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
2066 dde_data->unused, dde_data->fResponse, dde_data->fRelease,
2067 dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
2068 push_data( data, ptr, size );
2069 hunlock = (HGLOBAL)uiLo;
2072 TRACE( "send ddepack %u %lx\n", size, uiHi );
2073 break;
2074 case WM_DDE_EXECUTE:
2075 if (info->lparam)
2077 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
2079 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
2080 /* so that the other side can send it back on ACK */
2081 lp = info->lparam;
2082 hunlock = (HGLOBAL)info->lparam;
2085 break;
2087 SERVER_START_REQ( send_message )
2089 req->id = info->dest_tid;
2090 req->type = info->type;
2091 req->flags = 0;
2092 req->win = wine_server_user_handle( info->hwnd );
2093 req->msg = info->msg;
2094 req->wparam = info->wparam;
2095 req->lparam = lp;
2096 req->timeout = TIMEOUT_INFINITE;
2097 for (i = 0; i < data->count; i++)
2098 wine_server_add_data( req, data->data[i], data->size[i] );
2099 if ((res = wine_server_call( req )))
2101 if (res == STATUS_INVALID_PARAMETER)
2102 /* FIXME: find a STATUS_ value for this one */
2103 SetLastError( ERROR_INVALID_THREAD_ID );
2104 else
2105 SetLastError( RtlNtStatusToDosError(res) );
2107 else
2108 FreeDDElParam(info->msg, info->lparam);
2110 SERVER_END_REQ;
2111 if (hunlock) GlobalUnlock(hunlock);
2113 return !res;
2116 /***********************************************************************
2117 * unpack_dde_message
2119 * Unpack a posted DDE message received from another process.
2121 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
2122 void **buffer, size_t size )
2124 UINT_PTR uiLo, uiHi;
2125 HGLOBAL hMem = 0;
2126 void* ptr;
2128 switch (message)
2130 case WM_DDE_ACK:
2131 if (size)
2133 ULONGLONG hpack;
2134 /* hMem is being passed */
2135 if (size != sizeof(hpack)) return FALSE;
2136 if (!buffer || !*buffer) return FALSE;
2137 uiLo = *lparam;
2138 memcpy( &hpack, *buffer, size );
2139 hMem = unpack_ptr( hpack );
2140 uiHi = (UINT_PTR)hMem;
2141 TRACE("recv dde-ack %lx mem=%lx[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
2143 else
2145 uiLo = LOWORD( *lparam );
2146 uiHi = HIWORD( *lparam );
2147 TRACE("recv dde-ack %lx atom=%lx\n", uiLo, uiHi);
2149 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
2150 break;
2151 case WM_DDE_ADVISE:
2152 case WM_DDE_DATA:
2153 case WM_DDE_POKE:
2154 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
2155 uiHi = *lparam;
2156 if (size)
2158 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size )))
2159 return FALSE;
2160 if ((ptr = GlobalLock( hMem )))
2162 memcpy( ptr, *buffer, size );
2163 GlobalUnlock( hMem );
2165 else
2167 GlobalFree( hMem );
2168 return FALSE;
2171 uiLo = (UINT_PTR)hMem;
2173 *lparam = PackDDElParam( message, uiLo, uiHi );
2174 break;
2175 case WM_DDE_EXECUTE:
2176 if (size)
2178 if (!buffer || !*buffer) return FALSE;
2179 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size ))) return FALSE;
2180 if ((ptr = GlobalLock( hMem )))
2182 memcpy( ptr, *buffer, size );
2183 GlobalUnlock( hMem );
2184 TRACE( "exec: pairing c=%08lx s=%p\n", *lparam, hMem );
2185 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
2187 GlobalFree( hMem );
2188 return FALSE;
2191 else
2193 GlobalFree( hMem );
2194 return FALSE;
2196 } else return FALSE;
2197 *lparam = (LPARAM)hMem;
2198 break;
2200 return TRUE;
2203 /***********************************************************************
2204 * call_window_proc
2206 * Call a window procedure and the corresponding hooks.
2208 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2209 BOOL unicode, BOOL same_thread, enum wm_char_mapping mapping )
2211 LRESULT result = 0;
2212 CWPSTRUCT cwp;
2213 CWPRETSTRUCT cwpret;
2215 if (msg & 0x80000000)
2217 result = handle_internal_message( hwnd, msg, wparam, lparam );
2218 goto done;
2221 /* first the WH_CALLWNDPROC hook */
2222 hwnd = WIN_GetFullHandle( hwnd );
2223 cwp.lParam = lparam;
2224 cwp.wParam = wparam;
2225 cwp.message = msg;
2226 cwp.hwnd = hwnd;
2227 HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
2229 /* now call the window procedure */
2230 if (!WINPROC_call_window( hwnd, msg, wparam, lparam, &result, unicode, mapping )) goto done;
2232 /* and finally the WH_CALLWNDPROCRET hook */
2233 cwpret.lResult = result;
2234 cwpret.lParam = lparam;
2235 cwpret.wParam = wparam;
2236 cwpret.message = msg;
2237 cwpret.hwnd = hwnd;
2238 HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
2239 done:
2240 return result;
2244 /***********************************************************************
2245 * send_parent_notify
2247 * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
2248 * the window has the WS_EX_NOPARENTNOTIFY style.
2250 static void send_parent_notify( HWND hwnd, WORD event, WORD idChild, POINT pt )
2252 /* pt has to be in the client coordinates of the parent window */
2253 MapWindowPoints( 0, hwnd, &pt, 1 );
2254 for (;;)
2256 HWND parent;
2258 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)) break;
2259 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
2260 if (!(parent = GetParent(hwnd))) break;
2261 if (parent == GetDesktopWindow()) break;
2262 MapWindowPoints( hwnd, parent, &pt, 1 );
2263 hwnd = parent;
2264 SendMessageW( hwnd, WM_PARENTNOTIFY,
2265 MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
2270 /***********************************************************************
2271 * accept_hardware_message
2273 * Tell the server we have passed the message to the app
2274 * (even though we may end up dropping it later on)
2276 static void accept_hardware_message( UINT hw_id )
2278 SERVER_START_REQ( accept_hardware_message )
2280 req->hw_id = hw_id;
2281 if (wine_server_call( req ))
2282 FIXME("Failed to reply to MSG_HARDWARE message. Message may not be removed from queue.\n");
2284 SERVER_END_REQ;
2288 static BOOL process_rawinput_message( MSG *msg, UINT hw_id, const struct hardware_msg_data *msg_data )
2290 struct rawinput_thread_data *thread_data = rawinput_thread_data();
2291 if (!rawinput_from_hardware_message( thread_data->buffer, msg_data ))
2292 return FALSE;
2294 thread_data->hw_id = hw_id;
2295 msg->lParam = (LPARAM)hw_id;
2296 msg->pt = point_phys_to_win_dpi( msg->hwnd, msg->pt );
2297 return TRUE;
2300 /***********************************************************************
2301 * process_keyboard_message
2303 * returns TRUE if the contents of 'msg' should be passed to the application
2305 static BOOL process_keyboard_message( MSG *msg, UINT hw_id, HWND hwnd_filter,
2306 UINT first, UINT last, BOOL remove )
2308 EVENTMSG event;
2310 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN ||
2311 msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP)
2312 switch (msg->wParam)
2314 case VK_LSHIFT: case VK_RSHIFT:
2315 msg->wParam = VK_SHIFT;
2316 break;
2317 case VK_LCONTROL: case VK_RCONTROL:
2318 msg->wParam = VK_CONTROL;
2319 break;
2320 case VK_LMENU: case VK_RMENU:
2321 msg->wParam = VK_MENU;
2322 break;
2325 /* FIXME: is this really the right place for this hook? */
2326 event.message = msg->message;
2327 event.hwnd = msg->hwnd;
2328 event.time = msg->time;
2329 event.paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
2330 event.paramH = msg->lParam & 0x7FFF;
2331 if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
2332 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
2334 /* check message filters */
2335 if (msg->message < first || msg->message > last) return FALSE;
2336 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
2338 if (remove)
2340 if((msg->message == WM_KEYDOWN) &&
2341 (msg->hwnd != GetDesktopWindow()))
2343 /* Handle F1 key by sending out WM_HELP message */
2344 if (msg->wParam == VK_F1)
2346 PostMessageW( msg->hwnd, WM_KEYF1, 0, 0 );
2348 else if(msg->wParam >= VK_BROWSER_BACK &&
2349 msg->wParam <= VK_LAUNCH_APP2)
2351 /* FIXME: Process keystate */
2352 SendMessageW(msg->hwnd, WM_APPCOMMAND, (WPARAM)msg->hwnd, MAKELPARAM(0, (FAPPCOMMAND_KEY | (msg->wParam - VK_BROWSER_BACK + 1))));
2355 else if (msg->message == WM_KEYUP)
2357 /* Handle VK_APPS key by posting a WM_CONTEXTMENU message */
2358 if (msg->wParam == VK_APPS && !MENU_IsMenuActive())
2359 PostMessageW(msg->hwnd, WM_CONTEXTMENU, (WPARAM)msg->hwnd, -1);
2363 if (HOOK_CallHooks( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
2364 LOWORD(msg->wParam), msg->lParam, TRUE ))
2366 /* skip this message */
2367 HOOK_CallHooks( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam, TRUE );
2368 accept_hardware_message( hw_id );
2369 return FALSE;
2371 if (remove) accept_hardware_message( hw_id );
2372 msg->pt = point_phys_to_win_dpi( msg->hwnd, msg->pt );
2374 if ( remove && msg->message == WM_KEYDOWN )
2375 if (ImmProcessKey(msg->hwnd, GetKeyboardLayout(0), msg->wParam, msg->lParam, 0) )
2376 msg->wParam = VK_PROCESSKEY;
2378 return TRUE;
2382 /***********************************************************************
2383 * process_mouse_message
2385 * returns TRUE if the contents of 'msg' should be passed to the application
2387 static BOOL process_mouse_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
2388 UINT first, UINT last, BOOL remove )
2390 static MSG clk_msg;
2392 POINT pt;
2393 UINT message;
2394 INT hittest;
2395 EVENTMSG event;
2396 GUITHREADINFO info;
2397 MOUSEHOOKSTRUCTEX hook;
2398 BOOL eatMsg;
2399 WPARAM wparam;
2401 /* find the window to dispatch this mouse message to */
2403 info.cbSize = sizeof(info);
2404 GetGUIThreadInfo( GetCurrentThreadId(), &info );
2405 if (info.hwndCapture)
2407 hittest = HTCLIENT;
2408 msg->hwnd = info.hwndCapture;
2410 else
2412 HWND orig = msg->hwnd;
2414 msg->hwnd = WINPOS_WindowFromPoint( msg->hwnd, msg->pt, &hittest );
2415 if (!msg->hwnd) /* As a heuristic, try the next window if it's the owner of orig */
2417 HWND next = GetWindow( orig, GW_HWNDNEXT );
2419 if (next && GetWindow( orig, GW_OWNER ) == next && WIN_IsCurrentThread( next ))
2420 msg->hwnd = WINPOS_WindowFromPoint( next, msg->pt, &hittest );
2424 if (!msg->hwnd || !WIN_IsCurrentThread( msg->hwnd ))
2426 accept_hardware_message( hw_id );
2427 return FALSE;
2430 msg->pt = point_phys_to_win_dpi( msg->hwnd, msg->pt );
2431 SetThreadDpiAwarenessContext( GetWindowDpiAwarenessContext( msg->hwnd ));
2433 /* FIXME: is this really the right place for this hook? */
2434 event.message = msg->message;
2435 event.time = msg->time;
2436 event.hwnd = msg->hwnd;
2437 event.paramL = msg->pt.x;
2438 event.paramH = msg->pt.y;
2439 HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
2441 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
2443 pt = msg->pt;
2444 message = msg->message;
2445 wparam = msg->wParam;
2446 /* Note: windows has no concept of a non-client wheel message */
2447 if (message != WM_MOUSEWHEEL)
2449 if (hittest != HTCLIENT)
2451 message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
2452 wparam = hittest;
2454 else
2456 /* coordinates don't get translated while tracking a menu */
2457 /* FIXME: should differentiate popups and top-level menus */
2458 if (!(info.flags & GUI_INMENUMODE))
2459 ScreenToClient( msg->hwnd, &pt );
2462 msg->lParam = MAKELONG( pt.x, pt.y );
2464 /* translate double clicks */
2466 if ((msg->message == WM_LBUTTONDOWN) ||
2467 (msg->message == WM_RBUTTONDOWN) ||
2468 (msg->message == WM_MBUTTONDOWN) ||
2469 (msg->message == WM_XBUTTONDOWN))
2471 BOOL update = remove;
2473 /* translate double clicks -
2474 * note that ...MOUSEMOVEs can slip in between
2475 * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
2477 if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
2478 hittest != HTCLIENT ||
2479 (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
2481 if ((msg->message == clk_msg.message) &&
2482 (msg->hwnd == clk_msg.hwnd) &&
2483 (msg->wParam == clk_msg.wParam) &&
2484 (msg->time - clk_msg.time < GetDoubleClickTime()) &&
2485 (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
2486 (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
2488 message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
2489 if (update)
2491 clk_msg.message = 0; /* clear the double click conditions */
2492 update = FALSE;
2496 if (message < first || message > last) return FALSE;
2497 /* update static double click conditions */
2498 if (update) clk_msg = *msg;
2500 else
2502 if (message < first || message > last) return FALSE;
2504 msg->wParam = wparam;
2506 /* message is accepted now (but may still get dropped) */
2508 hook.s.pt = msg->pt;
2509 hook.s.hwnd = msg->hwnd;
2510 hook.s.wHitTestCode = hittest;
2511 hook.s.dwExtraInfo = extra_info;
2512 hook.mouseData = msg->wParam;
2513 if (HOOK_CallHooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
2514 message, (LPARAM)&hook, TRUE ))
2516 hook.s.pt = msg->pt;
2517 hook.s.hwnd = msg->hwnd;
2518 hook.s.wHitTestCode = hittest;
2519 hook.s.dwExtraInfo = extra_info;
2520 hook.mouseData = msg->wParam;
2521 HOOK_CallHooks( WH_CBT, HCBT_CLICKSKIPPED, message, (LPARAM)&hook, TRUE );
2522 accept_hardware_message( hw_id );
2523 return FALSE;
2526 if ((hittest == HTERROR) || (hittest == HTNOWHERE))
2528 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
2529 MAKELONG( hittest, msg->message ));
2530 accept_hardware_message( hw_id );
2531 return FALSE;
2534 if (remove) accept_hardware_message( hw_id );
2536 if (!remove || info.hwndCapture)
2538 msg->message = message;
2539 return TRUE;
2542 eatMsg = FALSE;
2544 if ((msg->message == WM_LBUTTONDOWN) ||
2545 (msg->message == WM_RBUTTONDOWN) ||
2546 (msg->message == WM_MBUTTONDOWN) ||
2547 (msg->message == WM_XBUTTONDOWN))
2549 /* Send the WM_PARENTNOTIFY,
2550 * note that even for double/nonclient clicks
2551 * notification message is still WM_L/M/RBUTTONDOWN.
2553 send_parent_notify( msg->hwnd, msg->message, 0, msg->pt );
2555 /* Activate the window if needed */
2557 if (msg->hwnd != info.hwndActive)
2559 HWND hwndTop = GetAncestor( msg->hwnd, GA_ROOT );
2561 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD)
2563 LONG ret = SendMessageW( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
2564 MAKELONG( hittest, msg->message ) );
2565 switch(ret)
2567 case MA_NOACTIVATEANDEAT:
2568 eatMsg = TRUE;
2569 /* fall through */
2570 case MA_NOACTIVATE:
2571 break;
2572 case MA_ACTIVATEANDEAT:
2573 eatMsg = TRUE;
2574 /* fall through */
2575 case MA_ACTIVATE:
2576 case 0:
2577 if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
2578 break;
2579 default:
2580 WARN( "unknown WM_MOUSEACTIVATE code %d\n", ret );
2581 break;
2587 /* send the WM_SETCURSOR message */
2589 /* Windows sends the normal mouse message as the message parameter
2590 in the WM_SETCURSOR message even if it's non-client mouse message */
2591 SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd, MAKELONG( hittest, msg->message ));
2593 msg->message = message;
2594 return !eatMsg;
2598 /***********************************************************************
2599 * process_hardware_message
2601 * Process a hardware message; return TRUE if message should be passed on to the app
2603 static BOOL process_hardware_message( MSG *msg, UINT hw_id, const struct hardware_msg_data *msg_data,
2604 HWND hwnd_filter, UINT first, UINT last, BOOL remove )
2606 DPI_AWARENESS_CONTEXT context;
2607 BOOL ret = FALSE;
2609 get_user_thread_info()->msg_source.deviceType = msg_data->source.device;
2610 get_user_thread_info()->msg_source.originId = msg_data->source.origin;
2612 /* hardware messages are always in physical coords */
2613 context = SetThreadDpiAwarenessContext( DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE );
2615 if (msg->message == WM_INPUT)
2616 ret = process_rawinput_message( msg, hw_id, msg_data );
2617 else if (is_keyboard_message( msg->message ))
2618 ret = process_keyboard_message( msg, hw_id, hwnd_filter, first, last, remove );
2619 else if (is_mouse_message( msg->message ))
2620 ret = process_mouse_message( msg, hw_id, msg_data->info, hwnd_filter, first, last, remove );
2621 else
2622 ERR( "unknown message type %x\n", msg->message );
2623 SetThreadDpiAwarenessContext( context );
2624 return ret;
2628 /***********************************************************************
2629 * call_sendmsg_callback
2631 * Call the callback function of SendMessageCallback.
2633 static inline void call_sendmsg_callback( SENDASYNCPROC callback, HWND hwnd, UINT msg,
2634 ULONG_PTR data, LRESULT result )
2636 if (!callback) return;
2638 TRACE_(relay)( "\1Call message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
2639 callback, hwnd, SPY_GetMsgName( msg, hwnd ), data, result );
2640 callback( hwnd, msg, data, result );
2641 TRACE_(relay)( "\1Ret message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
2642 callback, hwnd, SPY_GetMsgName( msg, hwnd ), data, result );
2646 /***********************************************************************
2647 * peek_message
2649 * Peek for a message matching the given parameters. Return 0 if none are
2650 * available; -1 on error.
2651 * All pending sent messages are processed before returning.
2653 static int peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags, UINT changed_mask )
2655 LRESULT result;
2656 struct user_thread_info *thread_info = get_user_thread_info();
2657 INPUT_MESSAGE_SOURCE prev_source = thread_info->msg_source;
2658 struct received_message_info info, *old_info;
2659 unsigned int hw_id = 0; /* id of previous hardware message */
2660 void *buffer;
2661 size_t buffer_size = 256;
2663 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size ))) return -1;
2665 if (!first && !last) last = ~0;
2666 if (hwnd == HWND_BROADCAST) hwnd = HWND_TOPMOST;
2668 for (;;)
2670 NTSTATUS res;
2671 size_t size = 0;
2672 const message_data_t *msg_data = buffer;
2674 thread_info->msg_source = prev_source;
2676 SERVER_START_REQ( get_message )
2678 req->flags = flags;
2679 req->get_win = wine_server_user_handle( hwnd );
2680 req->get_first = first;
2681 req->get_last = last;
2682 req->hw_id = hw_id;
2683 req->wake_mask = changed_mask & (QS_SENDMESSAGE | QS_SMRESULT);
2684 req->changed_mask = changed_mask;
2685 wine_server_set_reply( req, buffer, buffer_size );
2686 if (!(res = wine_server_call( req )))
2688 size = wine_server_reply_size( reply );
2689 info.type = reply->type;
2690 info.msg.hwnd = wine_server_ptr_handle( reply->win );
2691 info.msg.message = reply->msg;
2692 info.msg.wParam = reply->wparam;
2693 info.msg.lParam = reply->lparam;
2694 info.msg.time = reply->time;
2695 info.msg.pt.x = reply->x;
2696 info.msg.pt.y = reply->y;
2697 hw_id = 0;
2698 thread_info->active_hooks = reply->active_hooks;
2700 else buffer_size = reply->total;
2702 SERVER_END_REQ;
2704 if (res)
2706 HeapFree( GetProcessHeap(), 0, buffer );
2707 if (res == STATUS_PENDING)
2709 thread_info->wake_mask = changed_mask & (QS_SENDMESSAGE | QS_SMRESULT);
2710 thread_info->changed_mask = changed_mask;
2711 return 0;
2713 if (res != STATUS_BUFFER_OVERFLOW)
2715 SetLastError( RtlNtStatusToDosError(res) );
2716 return -1;
2718 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size ))) return -1;
2719 continue;
2722 TRACE( "got type %d msg %x (%s) hwnd %p wp %lx lp %lx\n",
2723 info.type, info.msg.message,
2724 (info.type == MSG_WINEVENT) ? "MSG_WINEVENT" : SPY_GetMsgName(info.msg.message, info.msg.hwnd),
2725 info.msg.hwnd, info.msg.wParam, info.msg.lParam );
2727 switch(info.type)
2729 case MSG_ASCII:
2730 case MSG_UNICODE:
2731 info.flags = ISMEX_SEND;
2732 break;
2733 case MSG_NOTIFY:
2734 info.flags = ISMEX_NOTIFY;
2735 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2736 &info.msg.lParam, &buffer, size ))
2737 continue;
2738 break;
2739 case MSG_CALLBACK:
2740 info.flags = ISMEX_CALLBACK;
2741 break;
2742 case MSG_CALLBACK_RESULT:
2743 if (size >= sizeof(msg_data->callback))
2744 call_sendmsg_callback( wine_server_get_ptr(msg_data->callback.callback),
2745 info.msg.hwnd, info.msg.message,
2746 msg_data->callback.data, msg_data->callback.result );
2747 continue;
2748 case MSG_WINEVENT:
2749 if (size >= sizeof(msg_data->winevent))
2751 WINEVENTPROC hook_proc;
2752 HMODULE free_module = 0;
2754 hook_proc = wine_server_get_ptr( msg_data->winevent.hook_proc );
2755 size -= sizeof(msg_data->winevent);
2756 if (size)
2758 WCHAR module[MAX_PATH];
2760 size = min( size, (MAX_PATH - 1) * sizeof(WCHAR) );
2761 memcpy( module, &msg_data->winevent + 1, size );
2762 module[size / sizeof(WCHAR)] = 0;
2763 if (!(hook_proc = get_hook_proc( hook_proc, module, &free_module )))
2765 ERR( "invalid winevent hook module name %s\n", debugstr_w(module) );
2766 continue;
2770 TRACE_(relay)( "\1Call winevent proc %p (hook=%04x,event=%x,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04x,time=%x)\n",
2771 hook_proc, msg_data->winevent.hook, info.msg.message, info.msg.hwnd,
2772 info.msg.wParam, info.msg.lParam, msg_data->winevent.tid, info.msg.time);
2774 hook_proc( wine_server_ptr_handle( msg_data->winevent.hook ), info.msg.message,
2775 info.msg.hwnd, info.msg.wParam, info.msg.lParam,
2776 msg_data->winevent.tid, info.msg.time );
2778 TRACE_(relay)( "\1Ret winevent proc %p (hook=%04x,event=%x,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04x,time=%x)\n",
2779 hook_proc, msg_data->winevent.hook, info.msg.message, info.msg.hwnd,
2780 info.msg.wParam, info.msg.lParam, msg_data->winevent.tid, info.msg.time);
2782 if (free_module) FreeLibrary(free_module);
2784 continue;
2785 case MSG_HOOK_LL:
2786 info.flags = ISMEX_SEND;
2787 result = 0;
2788 if (info.msg.message == WH_KEYBOARD_LL && size >= sizeof(msg_data->hardware))
2790 KBDLLHOOKSTRUCT hook;
2792 hook.vkCode = LOWORD( info.msg.lParam );
2793 hook.scanCode = HIWORD( info.msg.lParam );
2794 hook.flags = msg_data->hardware.flags;
2795 hook.time = info.msg.time;
2796 hook.dwExtraInfo = msg_data->hardware.info;
2797 TRACE( "calling keyboard LL hook vk %x scan %x flags %x time %u info %lx\n",
2798 hook.vkCode, hook.scanCode, hook.flags, hook.time, hook.dwExtraInfo );
2799 result = HOOK_CallHooks( WH_KEYBOARD_LL, HC_ACTION, info.msg.wParam, (LPARAM)&hook, TRUE );
2801 else if (info.msg.message == WH_MOUSE_LL && size >= sizeof(msg_data->hardware))
2803 MSLLHOOKSTRUCT hook;
2805 hook.pt = info.msg.pt;
2806 hook.mouseData = info.msg.lParam;
2807 hook.flags = msg_data->hardware.flags;
2808 hook.time = info.msg.time;
2809 hook.dwExtraInfo = msg_data->hardware.info;
2810 TRACE( "calling mouse LL hook pos %d,%d data %x flags %x time %u info %lx\n",
2811 hook.pt.x, hook.pt.y, hook.mouseData, hook.flags, hook.time, hook.dwExtraInfo );
2812 result = HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, info.msg.wParam, (LPARAM)&hook, TRUE );
2814 reply_message( &info, result, TRUE );
2815 continue;
2816 case MSG_OTHER_PROCESS:
2817 info.flags = ISMEX_SEND;
2818 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2819 &info.msg.lParam, &buffer, size ))
2821 /* ignore it */
2822 reply_message( &info, 0, TRUE );
2823 continue;
2825 break;
2826 case MSG_HARDWARE:
2827 if (size >= sizeof(msg_data->hardware))
2829 hw_id = msg_data->hardware.hw_id;
2830 if (!process_hardware_message( &info.msg, hw_id, &msg_data->hardware,
2831 hwnd, first, last, flags & PM_REMOVE ))
2833 TRACE("dropping msg %x\n", info.msg.message );
2834 continue; /* ignore it */
2836 *msg = info.msg;
2837 thread_info->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
2838 thread_info->GetMessageTimeVal = info.msg.time;
2839 thread_info->GetMessageExtraInfoVal = msg_data->hardware.info;
2840 HeapFree( GetProcessHeap(), 0, buffer );
2841 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)msg, TRUE );
2842 return 1;
2844 continue;
2845 case MSG_POSTED:
2846 if (info.msg.message & 0x80000000) /* internal message */
2848 if (flags & PM_REMOVE)
2850 handle_internal_message( info.msg.hwnd, info.msg.message,
2851 info.msg.wParam, info.msg.lParam );
2852 /* if this is a nested call return right away */
2853 if (first == info.msg.message && last == info.msg.message)
2855 HeapFree( GetProcessHeap(), 0, buffer );
2856 return 0;
2859 else
2860 peek_message( msg, info.msg.hwnd, info.msg.message,
2861 info.msg.message, flags | PM_REMOVE, changed_mask );
2862 continue;
2864 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
2866 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2867 &info.msg.lParam, &buffer, size ))
2868 continue; /* ignore it */
2870 *msg = info.msg;
2871 msg->pt = point_phys_to_win_dpi( info.msg.hwnd, info.msg.pt );
2872 thread_info->GetMessagePosVal = MAKELONG( msg->pt.x, msg->pt.y );
2873 thread_info->GetMessageTimeVal = info.msg.time;
2874 thread_info->GetMessageExtraInfoVal = 0;
2875 thread_info->msg_source = msg_source_unavailable;
2876 HeapFree( GetProcessHeap(), 0, buffer );
2877 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)msg, TRUE );
2878 return 1;
2881 /* if we get here, we have a sent message; call the window procedure */
2882 old_info = thread_info->receive_info;
2883 thread_info->receive_info = &info;
2884 thread_info->msg_source = msg_source_unavailable;
2885 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
2886 info.msg.lParam, (info.type != MSG_ASCII), FALSE,
2887 WMCHAR_MAP_RECVMESSAGE );
2888 reply_message( &info, result, TRUE );
2889 thread_info->receive_info = old_info;
2891 /* if some PM_QS* flags were specified, only handle sent messages from now on */
2892 if (HIWORD(flags) && !changed_mask) flags = PM_QS_SENDMESSAGE | LOWORD(flags);
2897 /***********************************************************************
2898 * process_sent_messages
2900 * Process all pending sent messages.
2902 static inline void process_sent_messages(void)
2904 MSG msg;
2905 peek_message( &msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE, 0 );
2909 /***********************************************************************
2910 * get_server_queue_handle
2912 * Get a handle to the server message queue for the current thread.
2914 static HANDLE get_server_queue_handle(void)
2916 struct user_thread_info *thread_info = get_user_thread_info();
2917 HANDLE ret;
2919 if (!(ret = thread_info->server_queue))
2921 SERVER_START_REQ( get_msg_queue )
2923 wine_server_call( req );
2924 ret = wine_server_ptr_handle( reply->handle );
2926 SERVER_END_REQ;
2927 thread_info->server_queue = ret;
2928 if (!ret) ERR( "Cannot get server thread queue\n" );
2930 return ret;
2934 /***********************************************************************
2935 * wait_message_reply
2937 * Wait until a sent message gets replied to.
2939 static void wait_message_reply( UINT flags )
2941 struct user_thread_info *thread_info = get_user_thread_info();
2942 HANDLE server_queue = get_server_queue_handle();
2943 unsigned int wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
2945 for (;;)
2947 unsigned int wake_bits = 0;
2949 SERVER_START_REQ( set_queue_mask )
2951 req->wake_mask = wake_mask;
2952 req->changed_mask = wake_mask;
2953 req->skip_wait = 1;
2954 if (!wine_server_call( req )) wake_bits = reply->wake_bits & wake_mask;
2956 SERVER_END_REQ;
2958 thread_info->wake_mask = thread_info->changed_mask = 0;
2960 if (wake_bits & QS_SMRESULT) return; /* got a result */
2961 if (wake_bits & QS_SENDMESSAGE)
2963 /* Process the sent message immediately */
2964 process_sent_messages();
2965 continue;
2968 wow_handlers.wait_message( 1, &server_queue, INFINITE, wake_mask, 0 );
2973 /***********************************************************************
2974 * wait_objects
2976 * Wait for multiple objects including the server queue, with specific queue masks.
2978 static DWORD wait_objects( DWORD count, const HANDLE *handles, DWORD timeout,
2979 DWORD wake_mask, DWORD changed_mask, DWORD flags )
2981 struct user_thread_info *thread_info = get_user_thread_info();
2982 DWORD ret;
2984 assert( count ); /* we must have at least the server queue */
2986 flush_window_surfaces( TRUE );
2988 if (thread_info->wake_mask != wake_mask || thread_info->changed_mask != changed_mask)
2990 SERVER_START_REQ( set_queue_mask )
2992 req->wake_mask = wake_mask;
2993 req->changed_mask = changed_mask;
2994 req->skip_wait = 0;
2995 wine_server_call( req );
2997 SERVER_END_REQ;
2998 thread_info->wake_mask = wake_mask;
2999 thread_info->changed_mask = changed_mask;
3002 ret = wow_handlers.wait_message( count, handles, timeout, changed_mask, flags );
3004 if (ret != WAIT_TIMEOUT) thread_info->wake_mask = thread_info->changed_mask = 0;
3005 return ret;
3009 /***********************************************************************
3010 * put_message_in_queue
3012 * Put a sent message into the destination queue.
3013 * For inter-process message, reply_size is set to expected size of reply data.
3015 static BOOL put_message_in_queue( const struct send_message_info *info, size_t *reply_size )
3017 struct packed_message data;
3018 message_data_t msg_data;
3019 unsigned int res;
3020 int i;
3021 timeout_t timeout = TIMEOUT_INFINITE;
3023 /* Check for INFINITE timeout for compatibility with Win9x,
3024 * although Windows >= NT does not do so
3026 if (info->type != MSG_NOTIFY &&
3027 info->type != MSG_CALLBACK &&
3028 info->type != MSG_POSTED &&
3029 info->timeout &&
3030 info->timeout != INFINITE)
3032 /* timeout is signed despite the prototype */
3033 timeout = (timeout_t)max( 0, (int)info->timeout ) * -10000;
3036 memset( &data, 0, sizeof(data) );
3037 if (info->type == MSG_OTHER_PROCESS || info->type == MSG_NOTIFY)
3039 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
3040 if (data.count == -1)
3042 WARN( "cannot pack message %x\n", info->msg );
3043 return FALSE;
3046 else if (info->type == MSG_CALLBACK)
3048 msg_data.callback.callback = wine_server_client_ptr( info->callback );
3049 msg_data.callback.data = info->data;
3050 msg_data.callback.result = 0;
3051 data.data[0] = &msg_data;
3052 data.size[0] = sizeof(msg_data.callback);
3053 data.count = 1;
3055 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
3057 return post_dde_message( &data, info );
3060 SERVER_START_REQ( send_message )
3062 req->id = info->dest_tid;
3063 req->type = info->type;
3064 req->flags = 0;
3065 req->win = wine_server_user_handle( info->hwnd );
3066 req->msg = info->msg;
3067 req->wparam = info->wparam;
3068 req->lparam = info->lparam;
3069 req->timeout = timeout;
3071 if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
3072 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
3073 if ((res = wine_server_call( req )))
3075 if (res == STATUS_INVALID_PARAMETER)
3076 /* FIXME: find a STATUS_ value for this one */
3077 SetLastError( ERROR_INVALID_THREAD_ID );
3078 else
3079 SetLastError( RtlNtStatusToDosError(res) );
3082 SERVER_END_REQ;
3083 return !res;
3087 /***********************************************************************
3088 * retrieve_reply
3090 * Retrieve a message reply from the server.
3092 static LRESULT retrieve_reply( const struct send_message_info *info,
3093 size_t reply_size, LRESULT *result )
3095 NTSTATUS status;
3096 void *reply_data = NULL;
3098 if (reply_size)
3100 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
3102 WARN( "no memory for reply, will be truncated\n" );
3103 reply_size = 0;
3106 SERVER_START_REQ( get_message_reply )
3108 req->cancel = 1;
3109 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
3110 if (!(status = wine_server_call( req ))) *result = reply->result;
3111 reply_size = wine_server_reply_size( reply );
3113 SERVER_END_REQ;
3114 if (!status && reply_size)
3115 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
3117 HeapFree( GetProcessHeap(), 0, reply_data );
3119 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx got reply %lx (err=%d)\n",
3120 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
3121 info->lparam, *result, status );
3123 /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
3124 if (status) SetLastError( RtlNtStatusToDosError(status) );
3125 return !status;
3129 /***********************************************************************
3130 * send_inter_thread_message
3132 static LRESULT send_inter_thread_message( const struct send_message_info *info, LRESULT *res_ptr )
3134 size_t reply_size = 0;
3136 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
3137 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
3139 USER_CheckNotLock();
3141 if (!put_message_in_queue( info, &reply_size )) return 0;
3143 /* there's no reply to wait for on notify/callback messages */
3144 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
3146 wait_message_reply( info->flags );
3147 return retrieve_reply( info, reply_size, res_ptr );
3151 /***********************************************************************
3152 * send_inter_thread_callback
3154 static LRESULT send_inter_thread_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
3155 LRESULT *result, void *arg )
3157 struct send_message_info *info = arg;
3158 info->hwnd = hwnd;
3159 info->msg = msg;
3160 info->wparam = wp;
3161 info->lparam = lp;
3162 return send_inter_thread_message( info, result );
3165 static BOOL is_message_broadcastable(UINT msg)
3167 return msg < WM_USER || msg >= 0xc000;
3170 /***********************************************************************
3171 * send_message
3173 * Backend implementation of the various SendMessage functions.
3175 static BOOL send_message( struct send_message_info *info, DWORD_PTR *res_ptr, BOOL unicode )
3177 struct user_thread_info *thread_info = get_user_thread_info();
3178 INPUT_MESSAGE_SOURCE prev_source = thread_info->msg_source;
3179 DWORD dest_pid;
3180 BOOL ret;
3181 LRESULT result;
3183 if (is_broadcast(info->hwnd))
3185 if (is_message_broadcastable( info->msg ))
3186 EnumWindows( broadcast_message_callback, (LPARAM)info );
3187 if (res_ptr) *res_ptr = 1;
3188 return TRUE;
3191 if (!(info->dest_tid = GetWindowThreadProcessId( info->hwnd, &dest_pid ))) return FALSE;
3193 if (USER_IsExitingThread( info->dest_tid )) return FALSE;
3195 thread_info->msg_source = msg_source_unavailable;
3196 SPY_EnterMessage( SPY_SENDMESSAGE, info->hwnd, info->msg, info->wparam, info->lparam );
3198 if (info->dest_tid == GetCurrentThreadId())
3200 result = call_window_proc( info->hwnd, info->msg, info->wparam, info->lparam,
3201 unicode, TRUE, info->wm_char );
3202 if (info->type == MSG_CALLBACK)
3203 call_sendmsg_callback( info->callback, info->hwnd, info->msg, info->data, result );
3204 ret = TRUE;
3206 else
3208 if (dest_pid != GetCurrentProcessId() && (info->type == MSG_ASCII || info->type == MSG_UNICODE))
3209 info->type = MSG_OTHER_PROCESS;
3211 /* MSG_ASCII can be sent unconverted except for WM_CHAR; everything else needs to be Unicode */
3212 if (!unicode && is_unicode_message( info->msg ) &&
3213 (info->type != MSG_ASCII || info->msg == WM_CHAR))
3214 ret = WINPROC_CallProcAtoW( send_inter_thread_callback, info->hwnd, info->msg,
3215 info->wparam, info->lparam, &result, info, info->wm_char );
3216 else
3217 ret = send_inter_thread_message( info, &result );
3220 SPY_ExitMessage( SPY_RESULT_OK, info->hwnd, info->msg, result, info->wparam, info->lparam );
3221 thread_info->msg_source = prev_source;
3222 if (ret && res_ptr) *res_ptr = result;
3223 return ret;
3227 /***********************************************************************
3228 * send_hardware_message
3230 NTSTATUS send_hardware_message( HWND hwnd, const INPUT *input, const RAWINPUT *rawinput, UINT flags )
3232 struct user_key_state_info *key_state_info = get_user_thread_info()->key_state;
3233 struct send_message_info info;
3234 int prev_x, prev_y, new_x, new_y;
3235 INT counter = global_key_state_counter;
3236 NTSTATUS ret;
3237 BOOL wait;
3239 info.type = MSG_HARDWARE;
3240 info.dest_tid = 0;
3241 info.hwnd = hwnd;
3242 info.flags = 0;
3243 info.timeout = 0;
3245 SERVER_START_REQ( send_hardware_message )
3247 req->win = wine_server_user_handle( hwnd );
3248 req->flags = flags;
3249 req->input.type = input->type;
3250 switch (input->type)
3252 case INPUT_MOUSE:
3253 req->input.mouse.x = input->u.mi.dx;
3254 req->input.mouse.y = input->u.mi.dy;
3255 req->input.mouse.data = input->u.mi.mouseData;
3256 req->input.mouse.flags = input->u.mi.dwFlags;
3257 req->input.mouse.time = input->u.mi.time;
3258 req->input.mouse.info = input->u.mi.dwExtraInfo;
3259 break;
3260 case INPUT_KEYBOARD:
3261 req->input.kbd.vkey = input->u.ki.wVk;
3262 req->input.kbd.scan = input->u.ki.wScan;
3263 req->input.kbd.flags = input->u.ki.dwFlags;
3264 req->input.kbd.time = input->u.ki.time;
3265 req->input.kbd.info = input->u.ki.dwExtraInfo;
3266 break;
3267 case INPUT_HARDWARE:
3268 req->input.hw.msg = input->u.hi.uMsg;
3269 req->input.hw.lparam = MAKELONG( input->u.hi.wParamL, input->u.hi.wParamH );
3270 break;
3272 if (key_state_info) wine_server_set_reply( req, key_state_info->state,
3273 sizeof(key_state_info->state) );
3274 ret = wine_server_call( req );
3275 wait = reply->wait;
3276 prev_x = reply->prev_x;
3277 prev_y = reply->prev_y;
3278 new_x = reply->new_x;
3279 new_y = reply->new_y;
3281 SERVER_END_REQ;
3283 if (!ret)
3285 if (key_state_info)
3287 key_state_info->time = GetTickCount();
3288 key_state_info->counter = counter;
3290 if ((flags & SEND_HWMSG_INJECTED) && (prev_x != new_x || prev_y != new_y))
3291 USER_Driver->pSetCursorPos( new_x, new_y );
3294 if (wait)
3296 LRESULT ignored;
3297 wait_message_reply( 0 );
3298 retrieve_reply( &info, 0, &ignored );
3300 return ret;
3304 /***********************************************************************
3305 * MSG_SendInternalMessageTimeout
3307 * Same as SendMessageTimeoutW but sends the message to a specific thread
3308 * without requiring a window handle. Only works for internal Wine messages.
3310 LRESULT MSG_SendInternalMessageTimeout( DWORD dest_pid, DWORD dest_tid,
3311 UINT msg, WPARAM wparam, LPARAM lparam,
3312 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
3314 struct send_message_info info;
3315 LRESULT ret, result;
3317 assert( msg & 0x80000000 ); /* must be an internal Wine message */
3319 info.type = MSG_UNICODE;
3320 info.dest_tid = dest_tid;
3321 info.hwnd = 0;
3322 info.msg = msg;
3323 info.wparam = wparam;
3324 info.lparam = lparam;
3325 info.flags = flags;
3326 info.timeout = timeout;
3328 if (USER_IsExitingThread( dest_tid )) return 0;
3330 if (dest_tid == GetCurrentThreadId())
3332 result = handle_internal_message( 0, msg, wparam, lparam );
3333 ret = 1;
3335 else
3337 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
3338 ret = send_inter_thread_message( &info, &result );
3340 if (ret && res_ptr) *res_ptr = result;
3341 return ret;
3345 /***********************************************************************
3346 * SendMessageTimeoutW (USER32.@)
3348 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
3349 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
3351 struct send_message_info info;
3353 info.type = MSG_UNICODE;
3354 info.hwnd = hwnd;
3355 info.msg = msg;
3356 info.wparam = wparam;
3357 info.lparam = lparam;
3358 info.flags = flags;
3359 info.timeout = timeout;
3361 return send_message( &info, res_ptr, TRUE );
3364 /***********************************************************************
3365 * SendMessageTimeoutA (USER32.@)
3367 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
3368 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
3370 struct send_message_info info;
3372 info.type = MSG_ASCII;
3373 info.hwnd = hwnd;
3374 info.msg = msg;
3375 info.wparam = wparam;
3376 info.lparam = lparam;
3377 info.flags = flags;
3378 info.timeout = timeout;
3379 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
3381 return send_message( &info, res_ptr, FALSE );
3385 /***********************************************************************
3386 * SendMessageW (USER32.@)
3388 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
3390 DWORD_PTR res = 0;
3391 struct send_message_info info;
3393 info.type = MSG_UNICODE;
3394 info.hwnd = hwnd;
3395 info.msg = msg;
3396 info.wparam = wparam;
3397 info.lparam = lparam;
3398 info.flags = SMTO_NORMAL;
3399 info.timeout = 0;
3401 send_message( &info, &res, TRUE );
3402 return res;
3406 /***********************************************************************
3407 * SendMessageA (USER32.@)
3409 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
3411 DWORD_PTR res = 0;
3412 struct send_message_info info;
3414 info.type = MSG_ASCII;
3415 info.hwnd = hwnd;
3416 info.msg = msg;
3417 info.wparam = wparam;
3418 info.lparam = lparam;
3419 info.flags = SMTO_NORMAL;
3420 info.timeout = 0;
3421 info.wm_char = WMCHAR_MAP_SENDMESSAGE;
3423 send_message( &info, &res, FALSE );
3424 return res;
3428 /***********************************************************************
3429 * SendNotifyMessageA (USER32.@)
3431 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
3433 struct send_message_info info;
3435 if (is_pointer_message( msg, wparam ))
3437 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3438 return FALSE;
3441 info.type = MSG_NOTIFY;
3442 info.hwnd = hwnd;
3443 info.msg = msg;
3444 info.wparam = wparam;
3445 info.lparam = lparam;
3446 info.flags = 0;
3447 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
3449 return send_message( &info, NULL, FALSE );
3453 /***********************************************************************
3454 * SendNotifyMessageW (USER32.@)
3456 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
3458 struct send_message_info info;
3460 if (is_pointer_message( msg, wparam ))
3462 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3463 return FALSE;
3466 info.type = MSG_NOTIFY;
3467 info.hwnd = hwnd;
3468 info.msg = msg;
3469 info.wparam = wparam;
3470 info.lparam = lparam;
3471 info.flags = 0;
3473 return send_message( &info, NULL, TRUE );
3477 /***********************************************************************
3478 * SendMessageCallbackA (USER32.@)
3480 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
3481 SENDASYNCPROC callback, ULONG_PTR data )
3483 struct send_message_info info;
3485 if (is_pointer_message( msg, wparam ))
3487 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3488 return FALSE;
3491 info.type = MSG_CALLBACK;
3492 info.hwnd = hwnd;
3493 info.msg = msg;
3494 info.wparam = wparam;
3495 info.lparam = lparam;
3496 info.callback = callback;
3497 info.data = data;
3498 info.flags = 0;
3499 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
3501 return send_message( &info, NULL, FALSE );
3505 /***********************************************************************
3506 * SendMessageCallbackW (USER32.@)
3508 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
3509 SENDASYNCPROC callback, ULONG_PTR data )
3511 struct send_message_info info;
3513 if (is_pointer_message( msg, wparam ))
3515 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3516 return FALSE;
3519 info.type = MSG_CALLBACK;
3520 info.hwnd = hwnd;
3521 info.msg = msg;
3522 info.wparam = wparam;
3523 info.lparam = lparam;
3524 info.callback = callback;
3525 info.data = data;
3526 info.flags = 0;
3528 return send_message( &info, NULL, TRUE );
3532 /***********************************************************************
3533 * ReplyMessage (USER32.@)
3535 BOOL WINAPI ReplyMessage( LRESULT result )
3537 struct received_message_info *info = get_user_thread_info()->receive_info;
3539 if (!info) return FALSE;
3540 reply_message( info, result, FALSE );
3541 return TRUE;
3545 /***********************************************************************
3546 * InSendMessage (USER32.@)
3548 BOOL WINAPI InSendMessage(void)
3550 return (InSendMessageEx( NULL ) & (ISMEX_SEND | ISMEX_NOTIFY | ISMEX_CALLBACK)) != 0;
3554 /***********************************************************************
3555 * InSendMessageEx (USER32.@)
3557 DWORD WINAPI InSendMessageEx( LPVOID reserved )
3559 struct received_message_info *info = get_user_thread_info()->receive_info;
3561 if (info) return info->flags;
3562 return ISMEX_NOSEND;
3566 /***********************************************************************
3567 * PostMessageA (USER32.@)
3569 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
3571 if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
3572 return PostMessageW( hwnd, msg, wparam, lparam );
3576 /***********************************************************************
3577 * PostMessageW (USER32.@)
3579 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
3581 struct send_message_info info;
3583 if (is_pointer_message( msg, wparam ))
3585 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3586 return FALSE;
3589 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
3590 hwnd, msg, SPY_GetMsgName(msg, hwnd), wparam, lparam );
3592 info.type = MSG_POSTED;
3593 info.hwnd = hwnd;
3594 info.msg = msg;
3595 info.wparam = wparam;
3596 info.lparam = lparam;
3597 info.flags = 0;
3599 if (is_broadcast(hwnd))
3601 if (is_message_broadcastable( info.msg ))
3602 EnumWindows( broadcast_message_callback, (LPARAM)&info );
3603 return TRUE;
3606 if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
3608 if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
3610 if (USER_IsExitingThread( info.dest_tid )) return TRUE;
3612 return put_message_in_queue( &info, NULL );
3616 /**********************************************************************
3617 * PostThreadMessageA (USER32.@)
3619 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
3621 if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
3622 return PostThreadMessageW( thread, msg, wparam, lparam );
3626 /**********************************************************************
3627 * PostThreadMessageW (USER32.@)
3629 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
3631 struct send_message_info info;
3633 if (is_pointer_message( msg, wparam ))
3635 SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3636 return FALSE;
3638 if (USER_IsExitingThread( thread )) return TRUE;
3640 info.type = MSG_POSTED;
3641 info.dest_tid = thread;
3642 info.hwnd = 0;
3643 info.msg = msg;
3644 info.wparam = wparam;
3645 info.lparam = lparam;
3646 info.flags = 0;
3647 return put_message_in_queue( &info, NULL );
3651 /***********************************************************************
3652 * PostQuitMessage (USER32.@)
3654 * Posts a quit message to the current thread's message queue.
3656 * PARAMS
3657 * exit_code [I] Exit code to return from message loop.
3659 * RETURNS
3660 * Nothing.
3662 * NOTES
3663 * This function is not the same as calling:
3664 *|PostThreadMessage(GetCurrentThreadId(), WM_QUIT, exit_code, 0);
3665 * It instead sets a flag in the message queue that signals it to generate
3666 * a WM_QUIT message when there are no other pending sent or posted messages
3667 * in the queue.
3669 void WINAPI PostQuitMessage( INT exit_code )
3671 SERVER_START_REQ( post_quit_message )
3673 req->exit_code = exit_code;
3674 wine_server_call( req );
3676 SERVER_END_REQ;
3679 /* check for driver events if we detect that the app is not properly consuming messages */
3680 static inline void check_for_driver_events( UINT msg )
3682 if (get_user_thread_info()->message_count > 200)
3684 flush_window_surfaces( FALSE );
3685 USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
3687 else if (msg == WM_TIMER || msg == WM_SYSTIMER)
3689 /* driver events should have priority over timers, so make sure we'll check for them soon */
3690 get_user_thread_info()->message_count += 100;
3692 else get_user_thread_info()->message_count++;
3695 /***********************************************************************
3696 * PeekMessageW (USER32.@)
3698 BOOL WINAPI DECLSPEC_HOTPATCH PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
3700 MSG msg;
3701 int ret;
3703 USER_CheckNotLock();
3704 check_for_driver_events( 0 );
3706 ret = peek_message( &msg, hwnd, first, last, flags, 0 );
3707 if (ret < 0) return FALSE;
3709 if (!ret)
3711 flush_window_surfaces( TRUE );
3712 ret = wow_handlers.wait_message( 0, NULL, 0, QS_ALLINPUT, 0 );
3713 /* if we received driver events, check again for a pending message */
3714 if (ret == WAIT_TIMEOUT || peek_message( &msg, hwnd, first, last, flags, 0 ) <= 0) return FALSE;
3717 check_for_driver_events( msg.message );
3719 /* copy back our internal safe copy of message data to msg_out.
3720 * msg_out is a variable from the *program*, so it can't be used
3721 * internally as it can get "corrupted" by our use of SendMessage()
3722 * (back to the program) inside the message handling itself. */
3723 if (!msg_out)
3725 SetLastError( ERROR_NOACCESS );
3726 return FALSE;
3728 *msg_out = msg;
3729 return TRUE;
3733 /***********************************************************************
3734 * PeekMessageA (USER32.@)
3736 BOOL WINAPI DECLSPEC_HOTPATCH PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
3738 if (get_pending_wmchar( msg, first, last, (flags & PM_REMOVE) )) return TRUE;
3739 if (!PeekMessageW( msg, hwnd, first, last, flags )) return FALSE;
3740 map_wparam_WtoA( msg, (flags & PM_REMOVE) );
3741 return TRUE;
3745 /***********************************************************************
3746 * GetMessageW (USER32.@)
3748 BOOL WINAPI DECLSPEC_HOTPATCH GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
3750 HANDLE server_queue = get_server_queue_handle();
3751 unsigned int mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
3752 int ret;
3754 USER_CheckNotLock();
3755 check_for_driver_events( 0 );
3757 if (first || last)
3759 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
3760 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
3761 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
3762 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
3763 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
3764 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
3766 else mask = QS_ALLINPUT;
3768 while (!(ret = peek_message( msg, hwnd, first, last, PM_REMOVE | (mask << 16), mask )))
3770 wait_objects( 1, &server_queue, INFINITE, mask & (QS_SENDMESSAGE | QS_SMRESULT), mask, 0 );
3772 if (ret < 0) return -1;
3774 check_for_driver_events( msg->message );
3776 return (msg->message != WM_QUIT);
3780 /***********************************************************************
3781 * GetMessageA (USER32.@)
3783 BOOL WINAPI DECLSPEC_HOTPATCH GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
3785 if (get_pending_wmchar( msg, first, last, TRUE )) return TRUE;
3786 if (GetMessageW( msg, hwnd, first, last ) < 0) return -1;
3787 map_wparam_WtoA( msg, TRUE );
3788 return (msg->message != WM_QUIT);
3792 /***********************************************************************
3793 * IsDialogMessageA (USER32.@)
3794 * IsDialogMessage (USER32.@)
3796 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
3798 MSG msg = *pmsg;
3799 map_wparam_AtoW( msg.message, &msg.wParam, WMCHAR_MAP_NOMAPPING );
3800 return IsDialogMessageW( hwndDlg, &msg );
3804 /***********************************************************************
3805 * TranslateMessage (USER32.@)
3807 * Implementation of TranslateMessage.
3809 * TranslateMessage translates virtual-key messages into character-messages,
3810 * as follows :
3811 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
3812 * ditto replacing WM_* with WM_SYS*
3813 * This produces WM_CHAR messages only for keys mapped to ASCII characters
3814 * by the keyboard driver.
3816 * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
3817 * return value is nonzero, regardless of the translation.
3820 BOOL WINAPI TranslateMessage( const MSG *msg )
3822 UINT message;
3823 WCHAR wp[8];
3824 BYTE state[256];
3825 INT len;
3827 if (msg->message < WM_KEYFIRST || msg->message > WM_KEYLAST) return FALSE;
3828 if (msg->message != WM_KEYDOWN && msg->message != WM_SYSKEYDOWN) return TRUE;
3830 TRACE_(key)("Translating key %s (%04lX), scancode %04x\n",
3831 SPY_GetVKeyName(msg->wParam), msg->wParam, HIWORD(msg->lParam));
3833 switch (msg->wParam)
3835 case VK_PACKET:
3836 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
3837 TRACE_(key)("PostMessageW(%p,%s,%04x,%08x)\n",
3838 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), HIWORD(msg->lParam), LOWORD(msg->lParam));
3839 PostMessageW( msg->hwnd, message, HIWORD(msg->lParam), LOWORD(msg->lParam));
3840 return TRUE;
3842 case VK_PROCESSKEY:
3843 return ImmTranslateMessage(msg->hwnd, msg->message, msg->wParam, msg->lParam);
3846 GetKeyboardState( state );
3847 len = ToUnicode(msg->wParam, HIWORD(msg->lParam), state, wp, ARRAY_SIZE(wp), 0);
3848 if (len == -1)
3850 message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
3851 TRACE_(key)("-1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
3852 msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
3853 PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
3855 else if (len > 0)
3857 INT i;
3859 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
3860 TRACE_(key)("%d -> PostMessageW(%p,%s,<x>,%08lx) for <x> in %s\n", len, msg->hwnd,
3861 SPY_GetMsgName(message, msg->hwnd), msg->lParam, debugstr_wn(wp, len));
3862 for (i = 0; i < len; i++)
3863 PostMessageW( msg->hwnd, message, wp[i], msg->lParam );
3865 return TRUE;
3869 /***********************************************************************
3870 * DispatchMessageA (USER32.@)
3872 * See DispatchMessageW.
3874 LRESULT WINAPI DECLSPEC_HOTPATCH DispatchMessageA( const MSG* msg )
3876 LRESULT retval;
3878 /* Process timer messages */
3879 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3881 if (msg->lParam)
3883 __TRY
3885 retval = CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
3886 msg->message, msg->wParam, GetTickCount() );
3888 __EXCEPT_ALL
3890 retval = 0;
3892 __ENDTRY
3893 return retval;
3896 if (!msg->hwnd) return 0;
3898 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3899 msg->wParam, msg->lParam );
3901 if (!WINPROC_call_window( msg->hwnd, msg->message, msg->wParam, msg->lParam,
3902 &retval, FALSE, WMCHAR_MAP_DISPATCHMESSAGE ))
3904 if (!IsWindow( msg->hwnd )) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3905 else SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3906 retval = 0;
3909 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3910 msg->wParam, msg->lParam );
3912 if (msg->message == WM_PAINT)
3914 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3915 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3916 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3917 DeleteObject( hrgn );
3919 return retval;
3923 /***********************************************************************
3924 * DispatchMessageW (USER32.@) Process a message
3926 * Process the message specified in the structure *_msg_.
3928 * If the lpMsg parameter points to a WM_TIMER message and the
3929 * parameter of the WM_TIMER message is not NULL, the lParam parameter
3930 * points to the function that is called instead of the window
3931 * procedure. The function stored in lParam (timer callback) is protected
3932 * from causing page-faults.
3934 * The message must be valid.
3936 * RETURNS
3938 * DispatchMessage() returns the result of the window procedure invoked.
3940 * CONFORMANCE
3942 * ECMA-234, Win32
3945 LRESULT WINAPI DECLSPEC_HOTPATCH DispatchMessageW( const MSG* msg )
3947 LRESULT retval;
3949 /* Process timer messages */
3950 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3952 if (msg->lParam)
3954 __TRY
3956 retval = CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
3957 msg->message, msg->wParam, GetTickCount() );
3959 __EXCEPT_ALL
3961 retval = 0;
3963 __ENDTRY
3964 return retval;
3967 if (!msg->hwnd) return 0;
3969 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3970 msg->wParam, msg->lParam );
3972 if (!WINPROC_call_window( msg->hwnd, msg->message, msg->wParam, msg->lParam,
3973 &retval, TRUE, WMCHAR_MAP_DISPATCHMESSAGE ))
3975 if (!IsWindow( msg->hwnd )) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3976 else SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3977 retval = 0;
3980 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3981 msg->wParam, msg->lParam );
3983 if (msg->message == WM_PAINT)
3985 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3986 HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3987 GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3988 DeleteObject( hrgn );
3990 return retval;
3994 /***********************************************************************
3995 * GetMessagePos (USER.119)
3996 * GetMessagePos (USER32.@)
3998 * The GetMessagePos() function returns a long value representing a
3999 * cursor position, in screen coordinates, when the last message
4000 * retrieved by the GetMessage() function occurs. The x-coordinate is
4001 * in the low-order word of the return value, the y-coordinate is in
4002 * the high-order word. The application can use the MAKEPOINT()
4003 * macro to obtain a POINT structure from the return value.
4005 * For the current cursor position, use GetCursorPos().
4007 * RETURNS
4009 * Cursor position of last message on success, zero on failure.
4011 * CONFORMANCE
4013 * ECMA-234, Win32
4016 DWORD WINAPI GetMessagePos(void)
4018 return get_user_thread_info()->GetMessagePosVal;
4022 /***********************************************************************
4023 * GetMessageTime (USER.120)
4024 * GetMessageTime (USER32.@)
4026 * GetMessageTime() returns the message time for the last message
4027 * retrieved by the function. The time is measured in milliseconds with
4028 * the same offset as GetTickCount().
4030 * Since the tick count wraps, this is only useful for moderately short
4031 * relative time comparisons.
4033 * RETURNS
4035 * Time of last message on success, zero on failure.
4037 LONG WINAPI GetMessageTime(void)
4039 return get_user_thread_info()->GetMessageTimeVal;
4043 /***********************************************************************
4044 * GetMessageExtraInfo (USER.288)
4045 * GetMessageExtraInfo (USER32.@)
4047 LPARAM WINAPI GetMessageExtraInfo(void)
4049 return get_user_thread_info()->GetMessageExtraInfoVal;
4053 /***********************************************************************
4054 * SetMessageExtraInfo (USER32.@)
4056 LPARAM WINAPI SetMessageExtraInfo(LPARAM lParam)
4058 struct user_thread_info *thread_info = get_user_thread_info();
4059 LONG old_value = thread_info->GetMessageExtraInfoVal;
4060 thread_info->GetMessageExtraInfoVal = lParam;
4061 return old_value;
4065 /***********************************************************************
4066 * GetCurrentInputMessageSource (USER32.@)
4068 BOOL WINAPI GetCurrentInputMessageSource( INPUT_MESSAGE_SOURCE *source )
4070 *source = get_user_thread_info()->msg_source;
4071 return TRUE;
4075 /***********************************************************************
4076 * WaitMessage (USER.112) Suspend thread pending messages
4077 * WaitMessage (USER32.@) Suspend thread pending messages
4079 * WaitMessage() suspends a thread until events appear in the thread's
4080 * queue.
4082 BOOL WINAPI WaitMessage(void)
4084 return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
4088 /***********************************************************************
4089 * MsgWaitForMultipleObjectsEx (USER32.@)
4091 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, const HANDLE *pHandles,
4092 DWORD timeout, DWORD mask, DWORD flags )
4094 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
4095 DWORD i;
4097 if (count > MAXIMUM_WAIT_OBJECTS-1)
4099 SetLastError( ERROR_INVALID_PARAMETER );
4100 return WAIT_FAILED;
4103 /* add the queue to the handle list */
4104 for (i = 0; i < count; i++) handles[i] = pHandles[i];
4105 handles[count] = get_server_queue_handle();
4107 return wait_objects( count+1, handles, timeout,
4108 (flags & MWMO_INPUTAVAILABLE) ? mask : 0, mask, flags );
4112 /***********************************************************************
4113 * MsgWaitForMultipleObjects (USER32.@)
4115 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, const HANDLE *handles,
4116 BOOL wait_all, DWORD timeout, DWORD mask )
4118 return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
4119 wait_all ? MWMO_WAITALL : 0 );
4123 /***********************************************************************
4124 * WaitForInputIdle (USER32.@)
4126 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
4128 DWORD start_time, elapsed, ret;
4129 HANDLE handles[2];
4131 handles[0] = hProcess;
4132 SERVER_START_REQ( get_process_idle_event )
4134 req->handle = wine_server_obj_handle( hProcess );
4135 wine_server_call_err( req );
4136 handles[1] = wine_server_ptr_handle( reply->event );
4138 SERVER_END_REQ;
4139 if (!handles[1]) return WAIT_FAILED; /* no event to wait on */
4141 start_time = GetTickCount();
4142 elapsed = 0;
4144 TRACE("waiting for %p\n", handles[1] );
4147 ret = MsgWaitForMultipleObjects ( 2, handles, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
4148 switch (ret)
4150 case WAIT_OBJECT_0:
4151 return 0;
4152 case WAIT_OBJECT_0+2:
4153 process_sent_messages();
4154 break;
4155 case WAIT_TIMEOUT:
4156 case WAIT_FAILED:
4157 TRACE("timeout or error\n");
4158 return ret;
4159 default:
4160 TRACE("finished\n");
4161 return 0;
4163 if (dwTimeOut != INFINITE)
4165 elapsed = GetTickCount() - start_time;
4166 if (elapsed > dwTimeOut)
4167 break;
4170 while (1);
4172 return WAIT_TIMEOUT;
4176 /***********************************************************************
4177 * RegisterWindowMessageA (USER32.@)
4178 * RegisterWindowMessage (USER.118)
4180 UINT WINAPI RegisterWindowMessageA( LPCSTR str )
4182 UINT ret = GlobalAddAtomA(str);
4183 TRACE("%s, ret=%x\n", str, ret);
4184 return ret;
4188 /***********************************************************************
4189 * RegisterWindowMessageW (USER32.@)
4191 UINT WINAPI RegisterWindowMessageW( LPCWSTR str )
4193 UINT ret = GlobalAddAtomW(str);
4194 TRACE("%s ret=%x\n", debugstr_w(str), ret);
4195 return ret;
4198 typedef struct BroadcastParm
4200 DWORD flags;
4201 LPDWORD recipients;
4202 UINT msg;
4203 WPARAM wp;
4204 LPARAM lp;
4205 BOOL success;
4206 HWINSTA winsta;
4207 } BroadcastParm;
4209 static BOOL CALLBACK bcast_childwindow( HWND hw, LPARAM lp )
4211 BroadcastParm *parm = (BroadcastParm*)lp;
4212 DWORD_PTR retval = 0;
4213 LRESULT lresult;
4215 if (parm->flags & BSF_IGNORECURRENTTASK && WIN_IsCurrentProcess(hw))
4217 TRACE("Not telling myself %p\n", hw);
4218 return TRUE;
4221 /* I don't know 100% for sure if this is what Windows does, but it fits the tests */
4222 if (parm->flags & BSF_QUERY)
4224 TRACE("Telling window %p using SendMessageTimeout\n", hw);
4226 /* Not tested for conflicting flags */
4227 if (parm->flags & BSF_FORCEIFHUNG || parm->flags & BSF_NOHANG)
4228 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_ABORTIFHUNG, 2000, &retval );
4229 else if (parm->flags & BSF_NOTIMEOUTIFNOTHUNG)
4230 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NOTIMEOUTIFNOTHUNG, 2000, &retval );
4231 else
4232 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NORMAL, 2000, &retval );
4234 if (!lresult && GetLastError() == ERROR_TIMEOUT)
4236 WARN("Timed out!\n");
4237 if (!(parm->flags & BSF_FORCEIFHUNG))
4238 goto fail;
4240 if (retval == BROADCAST_QUERY_DENY)
4241 goto fail;
4243 return TRUE;
4245 fail:
4246 parm->success = FALSE;
4247 return FALSE;
4249 else if (parm->flags & BSF_POSTMESSAGE)
4251 TRACE("Telling window %p using PostMessage\n", hw);
4252 PostMessageW( hw, parm->msg, parm->wp, parm->lp );
4254 else
4256 TRACE("Telling window %p using SendNotifyMessage\n", hw);
4257 SendNotifyMessageW( hw, parm->msg, parm->wp, parm->lp );
4260 return TRUE;
4263 static BOOL CALLBACK bcast_desktop( LPWSTR desktop, LPARAM lp )
4265 BOOL ret;
4266 HDESK hdesktop;
4267 BroadcastParm *parm = (BroadcastParm*)lp;
4269 TRACE("desktop: %s\n", debugstr_w( desktop ));
4271 hdesktop = open_winstation_desktop( parm->winsta, desktop, 0, FALSE, DESKTOP_ENUMERATE|DESKTOP_WRITEOBJECTS|STANDARD_RIGHTS_WRITE );
4272 if (!hdesktop)
4274 FIXME("Could not open desktop %s\n", debugstr_w(desktop));
4275 return TRUE;
4278 ret = EnumDesktopWindows( hdesktop, bcast_childwindow, lp );
4279 CloseDesktop(hdesktop);
4280 TRACE("-->%d\n", ret);
4281 return parm->success;
4284 static BOOL CALLBACK bcast_winsta( LPWSTR winsta, LPARAM lp )
4286 BOOL ret;
4287 HWINSTA hwinsta = OpenWindowStationW( winsta, FALSE, WINSTA_ENUMDESKTOPS );
4288 TRACE("hwinsta: %p/%s/%08x\n", hwinsta, debugstr_w( winsta ), GetLastError());
4289 if (!hwinsta)
4290 return TRUE;
4291 ((BroadcastParm *)lp)->winsta = hwinsta;
4292 ret = EnumDesktopsW( hwinsta, bcast_desktop, lp );
4293 CloseWindowStation( hwinsta );
4294 TRACE("-->%d\n", ret);
4295 return ret;
4298 /***********************************************************************
4299 * BroadcastSystemMessageA (USER32.@)
4300 * BroadcastSystemMessage (USER32.@)
4302 LONG WINAPI BroadcastSystemMessageA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
4304 return BroadcastSystemMessageExA( flags, recipients, msg, wp, lp, NULL );
4308 /***********************************************************************
4309 * BroadcastSystemMessageW (USER32.@)
4311 LONG WINAPI BroadcastSystemMessageW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
4313 return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, NULL );
4316 /***********************************************************************
4317 * BroadcastSystemMessageExA (USER32.@)
4319 LONG WINAPI BroadcastSystemMessageExA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
4321 map_wparam_AtoW( msg, &wp, WMCHAR_MAP_NOMAPPING );
4322 return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, NULL );
4326 /***********************************************************************
4327 * BroadcastSystemMessageExW (USER32.@)
4329 LONG WINAPI BroadcastSystemMessageExW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
4331 BroadcastParm parm;
4332 DWORD recips = BSM_ALLCOMPONENTS;
4333 BOOL ret = TRUE;
4334 static const DWORD all_flags = ( BSF_QUERY | BSF_IGNORECURRENTTASK | BSF_FLUSHDISK | BSF_NOHANG
4335 | BSF_POSTMESSAGE | BSF_FORCEIFHUNG | BSF_NOTIMEOUTIFNOTHUNG
4336 | BSF_ALLOWSFW | BSF_SENDNOTIFYMESSAGE | BSF_RETURNHDESK | BSF_LUID );
4338 TRACE("Flags: %08x, recipients: %p(0x%x), msg: %04x, wparam: %08lx, lparam: %08lx\n", flags, recipients,
4339 (recipients ? *recipients : recips), msg, wp, lp);
4341 if (flags & ~all_flags)
4343 SetLastError(ERROR_INVALID_PARAMETER);
4344 return 0;
4347 if (!recipients)
4348 recipients = &recips;
4350 if ( pinfo && flags & BSF_QUERY )
4351 FIXME("Not returning PBSMINFO information yet\n");
4353 parm.flags = flags;
4354 parm.recipients = recipients;
4355 parm.msg = msg;
4356 parm.wp = wp;
4357 parm.lp = lp;
4358 parm.success = TRUE;
4360 if (*recipients & BSM_ALLDESKTOPS || *recipients == BSM_ALLCOMPONENTS)
4361 ret = EnumWindowStationsW(bcast_winsta, (LONG_PTR)&parm);
4362 else if (*recipients & BSM_APPLICATIONS)
4364 EnumWindows(bcast_childwindow, (LONG_PTR)&parm);
4365 ret = parm.success;
4367 else
4368 FIXME("Recipients %08x not supported!\n", *recipients);
4370 return ret;
4373 /***********************************************************************
4374 * SetMessageQueue (USER32.@)
4376 BOOL WINAPI SetMessageQueue( INT size )
4378 /* now obsolete the message queue will be expanded dynamically as necessary */
4379 return TRUE;
4383 /***********************************************************************
4384 * MessageBeep (USER32.@)
4386 BOOL WINAPI MessageBeep( UINT i )
4388 BOOL active = TRUE;
4389 SystemParametersInfoA( SPI_GETBEEP, 0, &active, FALSE );
4390 if (active) USER_Driver->pBeep();
4391 return TRUE;
4395 /***********************************************************************
4396 * SetCoalescableTimer (USER32.@)
4398 UINT_PTR WINAPI SetCoalescableTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc, ULONG tolerance )
4400 UINT_PTR ret;
4401 WNDPROC winproc = 0;
4403 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, FALSE );
4405 timeout = min( max( USER_TIMER_MINIMUM, timeout ), USER_TIMER_MAXIMUM );
4407 SERVER_START_REQ( set_win_timer )
4409 req->win = wine_server_user_handle( hwnd );
4410 req->msg = WM_TIMER;
4411 req->id = id;
4412 req->rate = timeout;
4413 req->lparam = (ULONG_PTR)winproc;
4414 if (!wine_server_call_err( req ))
4416 ret = reply->id;
4417 if (!ret) ret = TRUE;
4419 else ret = 0;
4421 SERVER_END_REQ;
4423 TRACE("Added %p %lx %p timeout %d\n", hwnd, id, winproc, timeout );
4424 return ret;
4428 /******************************************************************
4429 * SetTimer (USER32.@)
4431 UINT_PTR WINAPI SetTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
4433 return SetCoalescableTimer( hwnd, id, timeout, proc, TIMERV_DEFAULT_COALESCING );
4437 /***********************************************************************
4438 * SetSystemTimer (USER32.@)
4440 UINT_PTR WINAPI SetSystemTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
4442 UINT_PTR ret;
4443 WNDPROC winproc = 0;
4445 if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, FALSE );
4447 timeout = min( max( USER_TIMER_MINIMUM, timeout ), USER_TIMER_MAXIMUM );
4449 SERVER_START_REQ( set_win_timer )
4451 req->win = wine_server_user_handle( hwnd );
4452 req->msg = WM_SYSTIMER;
4453 req->id = id;
4454 req->rate = timeout;
4455 req->lparam = (ULONG_PTR)winproc;
4456 if (!wine_server_call_err( req ))
4458 ret = reply->id;
4459 if (!ret) ret = TRUE;
4461 else ret = 0;
4463 SERVER_END_REQ;
4465 TRACE("Added %p %lx %p timeout %d\n", hwnd, id, winproc, timeout );
4466 return ret;
4470 /***********************************************************************
4471 * KillTimer (USER32.@)
4473 BOOL WINAPI KillTimer( HWND hwnd, UINT_PTR id )
4475 BOOL ret;
4477 SERVER_START_REQ( kill_win_timer )
4479 req->win = wine_server_user_handle( hwnd );
4480 req->msg = WM_TIMER;
4481 req->id = id;
4482 ret = !wine_server_call_err( req );
4484 SERVER_END_REQ;
4485 return ret;
4489 /***********************************************************************
4490 * KillSystemTimer (USER32.@)
4492 BOOL WINAPI KillSystemTimer( HWND hwnd, UINT_PTR id )
4494 BOOL ret;
4496 SERVER_START_REQ( kill_win_timer )
4498 req->win = wine_server_user_handle( hwnd );
4499 req->msg = WM_SYSTIMER;
4500 req->id = id;
4501 ret = !wine_server_call_err( req );
4503 SERVER_END_REQ;
4504 return ret;
4508 /**********************************************************************
4509 * IsGUIThread (USER32.@)
4511 BOOL WINAPI IsGUIThread( BOOL convert )
4513 FIXME( "%u: stub\n", convert );
4514 return TRUE;
4518 /**********************************************************************
4519 * GetGUIThreadInfo (USER32.@)
4521 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
4523 BOOL ret;
4525 if (info->cbSize != sizeof(*info))
4527 SetLastError( ERROR_INVALID_PARAMETER );
4528 return FALSE;
4531 SERVER_START_REQ( get_thread_input )
4533 req->tid = id;
4534 if ((ret = !wine_server_call_err( req )))
4536 info->flags = 0;
4537 info->hwndActive = wine_server_ptr_handle( reply->active );
4538 info->hwndFocus = wine_server_ptr_handle( reply->focus );
4539 info->hwndCapture = wine_server_ptr_handle( reply->capture );
4540 info->hwndMenuOwner = wine_server_ptr_handle( reply->menu_owner );
4541 info->hwndMoveSize = wine_server_ptr_handle( reply->move_size );
4542 info->hwndCaret = wine_server_ptr_handle( reply->caret );
4543 info->rcCaret.left = reply->rect.left;
4544 info->rcCaret.top = reply->rect.top;
4545 info->rcCaret.right = reply->rect.right;
4546 info->rcCaret.bottom = reply->rect.bottom;
4547 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
4548 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
4549 if (reply->caret) info->flags |= GUI_CARETBLINKING;
4552 SERVER_END_REQ;
4553 return ret;
4557 /******************************************************************
4558 * IsHungAppWindow (USER32.@)
4561 BOOL WINAPI IsHungAppWindow( HWND hWnd )
4563 BOOL ret;
4565 SERVER_START_REQ( is_window_hung )
4567 req->win = wine_server_user_handle( hWnd );
4568 ret = !wine_server_call_err( req ) && reply->is_hung;
4570 SERVER_END_REQ;
4571 return ret;
4574 /******************************************************************
4575 * ChangeWindowMessageFilter (USER32.@)
4577 BOOL WINAPI ChangeWindowMessageFilter( UINT message, DWORD flag )
4579 FIXME( "%x %08x\n", message, flag );
4580 return TRUE;
4583 /******************************************************************
4584 * ChangeWindowMessageFilterEx (USER32.@)
4586 BOOL WINAPI ChangeWindowMessageFilterEx( HWND hwnd, UINT message, DWORD action, CHANGEFILTERSTRUCT *changefilter )
4588 FIXME( "%p %x %d %p\n", hwnd, message, action, changefilter );
4589 return TRUE;