user32: Return the cursor position in send_hardware_message and use it to update...
[wine] / dlls / user32 / message.c
1 /*
2  * Window messaging support
3  *
4  * Copyright 2001 Alexandre Julliard
5  * Copyright 2008 Maarten Lankhorst
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winerror.h"
37 #include "winnls.h"
38 #include "dbt.h"
39 #include "dde.h"
40 #include "imm.h"
41 #include "ddk/imm.h"
42 #include "wine/unicode.h"
43 #include "wine/server.h"
44 #include "user_private.h"
45 #include "win.h"
46 #include "controls.h"
47 #include "wine/debug.h"
48 #include "wine/exception.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(msg);
51 WINE_DECLARE_DEBUG_CHANNEL(relay);
52 WINE_DECLARE_DEBUG_CHANNEL(key);
53
54 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
55 #define WM_NCMOUSELAST  (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
56
57 #define MAX_PACK_COUNT 4
58
59 #define SYS_TIMER_RATE  55   /* min. timer rate in ms (actually 54.925)*/
60
61 /* the various structures that can be sent in messages, in platform-independent layout */
62 struct packed_CREATESTRUCTW
63 {
64     ULONGLONG     lpCreateParams;
65     ULONGLONG     hInstance;
66     user_handle_t hMenu;
67     DWORD         __pad1;
68     user_handle_t hwndParent;
69     DWORD         __pad2;
70     INT           cy;
71     INT           cx;
72     INT           y;
73     INT           x;
74     LONG          style;
75     ULONGLONG     lpszName;
76     ULONGLONG     lpszClass;
77     DWORD         dwExStyle;
78     DWORD         __pad3;
79 };
80
81 struct packed_DRAWITEMSTRUCT
82 {
83     UINT          CtlType;
84     UINT          CtlID;
85     UINT          itemID;
86     UINT          itemAction;
87     UINT          itemState;
88     user_handle_t hwndItem;
89     DWORD         __pad1;
90     user_handle_t hDC;
91     DWORD         __pad2;
92     RECT          rcItem;
93     ULONGLONG     itemData;
94 };
95
96 struct packed_MEASUREITEMSTRUCT
97 {
98     UINT          CtlType;
99     UINT          CtlID;
100     UINT          itemID;
101     UINT          itemWidth;
102     UINT          itemHeight;
103     ULONGLONG     itemData;
104 };
105
106 struct packed_DELETEITEMSTRUCT
107 {
108     UINT          CtlType;
109     UINT          CtlID;
110     UINT          itemID;
111     user_handle_t hwndItem;
112     DWORD         __pad;
113     ULONGLONG     itemData;
114 };
115
116 struct packed_COMPAREITEMSTRUCT
117 {
118     UINT          CtlType;
119     UINT          CtlID;
120     user_handle_t hwndItem;
121     DWORD         __pad1;
122     UINT          itemID1;
123     ULONGLONG     itemData1;
124     UINT          itemID2;
125     ULONGLONG     itemData2;
126     DWORD         dwLocaleId;
127     DWORD         __pad2;
128 };
129
130 struct packed_WINDOWPOS
131 {
132     user_handle_t hwnd;
133     DWORD         __pad1;
134     user_handle_t hwndInsertAfter;
135     DWORD         __pad2;
136     INT           x;
137     INT           y;
138     INT           cx;
139     INT           cy;
140     UINT          flags;
141     DWORD         __pad3;
142 };
143
144 struct packed_COPYDATASTRUCT
145 {
146     ULONGLONG dwData;
147     DWORD     cbData;
148     ULONGLONG lpData;
149 };
150
151 struct packed_HELPINFO
152 {
153     UINT          cbSize;
154     INT           iContextType;
155     INT           iCtrlId;
156     user_handle_t hItemHandle;
157     DWORD         __pad;
158     ULONGLONG     dwContextId;
159     POINT         MousePos;
160 };
161
162 struct packed_NCCALCSIZE_PARAMS
163 {
164     RECT          rgrc[3];
165     ULONGLONG     __pad1;
166     user_handle_t hwnd;
167     DWORD         __pad2;
168     user_handle_t hwndInsertAfter;
169     DWORD         __pad3;
170     INT           x;
171     INT           y;
172     INT           cx;
173     INT           cy;
174     UINT          flags;
175     DWORD         __pad4;
176 };
177
178 struct packed_MSG
179 {
180     user_handle_t hwnd;
181     DWORD         __pad1;
182     UINT          message;
183     ULONGLONG     wParam;
184     ULONGLONG     lParam;
185     DWORD         time;
186     POINT         pt;
187     DWORD         __pad2;
188 };
189
190 struct packed_MDINEXTMENU
191 {
192     user_handle_t hmenuIn;
193     DWORD         __pad1;
194     user_handle_t hmenuNext;
195     DWORD         __pad2;
196     user_handle_t hwndNext;
197     DWORD         __pad3;
198 };
199
200 struct packed_MDICREATESTRUCTW
201 {
202     ULONGLONG szClass;
203     ULONGLONG szTitle;
204     ULONGLONG hOwner;
205     INT       x;
206     INT       y;
207     INT       cx;
208     INT       cy;
209     DWORD     style;
210     ULONGLONG lParam;
211 };
212
213 struct packed_hook_extra_info
214 {
215     user_handle_t handle;
216     DWORD         __pad;
217     ULONGLONG     lparam;
218 };
219
220 /* the structures are unpacked on top of the packed ones, so make sure they fit */
221 C_ASSERT( sizeof(struct packed_CREATESTRUCTW) >= sizeof(CREATESTRUCTW) );
222 C_ASSERT( sizeof(struct packed_DRAWITEMSTRUCT) >= sizeof(DRAWITEMSTRUCT) );
223 C_ASSERT( sizeof(struct packed_MEASUREITEMSTRUCT) >= sizeof(MEASUREITEMSTRUCT) );
224 C_ASSERT( sizeof(struct packed_DELETEITEMSTRUCT) >= sizeof(DELETEITEMSTRUCT) );
225 C_ASSERT( sizeof(struct packed_COMPAREITEMSTRUCT) >= sizeof(COMPAREITEMSTRUCT) );
226 C_ASSERT( sizeof(struct packed_WINDOWPOS) >= sizeof(WINDOWPOS) );
227 C_ASSERT( sizeof(struct packed_COPYDATASTRUCT) >= sizeof(COPYDATASTRUCT) );
228 C_ASSERT( sizeof(struct packed_HELPINFO) >= sizeof(HELPINFO) );
229 C_ASSERT( sizeof(struct packed_NCCALCSIZE_PARAMS) >= sizeof(NCCALCSIZE_PARAMS) + sizeof(WINDOWPOS) );
230 C_ASSERT( sizeof(struct packed_MSG) >= sizeof(MSG) );
231 C_ASSERT( sizeof(struct packed_MDINEXTMENU) >= sizeof(MDINEXTMENU) );
232 C_ASSERT( sizeof(struct packed_MDICREATESTRUCTW) >= sizeof(MDICREATESTRUCTW) );
233 C_ASSERT( sizeof(struct packed_hook_extra_info) >= sizeof(struct hook_extra_info) );
234
235 union packed_structs
236 {
237     struct packed_CREATESTRUCTW cs;
238     struct packed_DRAWITEMSTRUCT dis;
239     struct packed_MEASUREITEMSTRUCT mis;
240     struct packed_DELETEITEMSTRUCT dls;
241     struct packed_COMPAREITEMSTRUCT cis;
242     struct packed_WINDOWPOS wp;
243     struct packed_COPYDATASTRUCT cds;
244     struct packed_HELPINFO hi;
245     struct packed_NCCALCSIZE_PARAMS ncp;
246     struct packed_MSG msg;
247     struct packed_MDINEXTMENU mnm;
248     struct packed_MDICREATESTRUCTW mcs;
249     struct packed_hook_extra_info hook;
250 };
251
252 /* description of the data fields that need to be packed along with a sent message */
253 struct packed_message
254 {
255     union packed_structs ps;
256     int                  count;
257     const void          *data[MAX_PACK_COUNT];
258     size_t               size[MAX_PACK_COUNT];
259 };
260
261 /* info about the message currently being received by the current thread */
262 struct received_message_info
263 {
264     enum message_type type;
265     MSG               msg;
266     UINT              flags;  /* InSendMessageEx return flags */
267 };
268
269 /* structure to group all parameters for sent messages of the various kinds */
270 struct send_message_info
271 {
272     enum message_type type;
273     DWORD             dest_tid;
274     HWND              hwnd;
275     UINT              msg;
276     WPARAM            wparam;
277     LPARAM            lparam;
278     UINT              flags;      /* flags for SendMessageTimeout */
279     UINT              timeout;    /* timeout for SendMessageTimeout */
280     SENDASYNCPROC     callback;   /* callback function for SendMessageCallback */
281     ULONG_PTR         data;       /* callback data */
282     enum wm_char_mapping wm_char;
283 };
284
285
286 /* Message class descriptor */
287 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
288
289 const struct builtin_class_descr MESSAGE_builtin_class =
290 {
291     messageW,             /* name */
292     0,                    /* style */
293     WINPROC_MESSAGE,      /* proc */
294     0,                    /* extra */
295     IDC_ARROW,            /* cursor */
296     0                     /* brush */
297 };
298
299
300
301 /* flag for messages that contain pointers */
302 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
303
304 #define SET(msg) (1 << ((msg) & 31))
305
306 static const unsigned int message_pointer_flags[] =
307 {
308     /* 0x00 - 0x1f */
309     SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
310     SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
311     /* 0x20 - 0x3f */
312     SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
313     SET(WM_COMPAREITEM),
314     /* 0x40 - 0x5f */
315     SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
316     SET(WM_NOTIFY) | SET(WM_HELP),
317     /* 0x60 - 0x7f */
318     SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
319     /* 0x80 - 0x9f */
320     SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
321     /* 0xa0 - 0xbf */
322     SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
323     /* 0xc0 - 0xdf */
324     SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
325     /* 0xe0 - 0xff */
326     SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO) | SET(SBM_GETSCROLLBARINFO),
327     /* 0x100 - 0x11f */
328     0,
329     /* 0x120 - 0x13f */
330     0,
331     /* 0x140 - 0x15f */
332     SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
333     SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
334     SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
335     /* 0x160 - 0x17f */
336     0,
337     /* 0x180 - 0x19f */
338     SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
339     SET(LB_DIR) | SET(LB_FINDSTRING) |
340     SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
341     /* 0x1a0 - 0x1bf */
342     SET(LB_FINDSTRINGEXACT),
343     /* 0x1c0 - 0x1df */
344     0,
345     /* 0x1e0 - 0x1ff */
346     0,
347     /* 0x200 - 0x21f */
348     SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
349     /* 0x220 - 0x23f */
350     SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
351     SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
352     /* 0x240 - 0x25f */
353     0,
354     /* 0x260 - 0x27f */
355     0,
356     /* 0x280 - 0x29f */
357     0,
358     /* 0x2a0 - 0x2bf */
359     0,
360     /* 0x2c0 - 0x2df */
361     0,
362     /* 0x2e0 - 0x2ff */
363     0,
364     /* 0x300 - 0x31f */
365     SET(WM_ASKCBFORMATNAME)
366 };
367
368 /* flags for messages that contain Unicode strings */
369 static const unsigned int message_unicode_flags[] =
370 {
371     /* 0x00 - 0x1f */
372     SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
373     SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
374     /* 0x20 - 0x3f */
375     SET(WM_CHARTOITEM),
376     /* 0x40 - 0x5f */
377     0,
378     /* 0x60 - 0x7f */
379     0,
380     /* 0x80 - 0x9f */
381     SET(WM_NCCREATE),
382     /* 0xa0 - 0xbf */
383     0,
384     /* 0xc0 - 0xdf */
385     SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
386     /* 0xe0 - 0xff */
387     0,
388     /* 0x100 - 0x11f */
389     SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
390     /* 0x120 - 0x13f */
391     SET(WM_MENUCHAR),
392     /* 0x140 - 0x15f */
393     SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
394     SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
395     /* 0x160 - 0x17f */
396     0,
397     /* 0x180 - 0x19f */
398     SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
399     SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
400     /* 0x1a0 - 0x1bf */
401     SET(LB_FINDSTRINGEXACT),
402     /* 0x1c0 - 0x1df */
403     0,
404     /* 0x1e0 - 0x1ff */
405     0,
406     /* 0x200 - 0x21f */
407     0,
408     /* 0x220 - 0x23f */
409     SET(WM_MDICREATE),
410     /* 0x240 - 0x25f */
411     0,
412     /* 0x260 - 0x27f */
413     0,
414     /* 0x280 - 0x29f */
415     SET(WM_IME_CHAR),
416     /* 0x2a0 - 0x2bf */
417     0,
418     /* 0x2c0 - 0x2df */
419     0,
420     /* 0x2e0 - 0x2ff */
421     0,
422     /* 0x300 - 0x31f */
423     SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
424 };
425
426 /* check whether a given message type includes pointers */
427 static inline int is_pointer_message( UINT message )
428 {
429     if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
430     return (message_pointer_flags[message / 32] & SET(message)) != 0;
431 }
432
433 /* check whether a given message type contains Unicode (or ASCII) chars */
434 static inline int is_unicode_message( UINT message )
435 {
436     if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
437     return (message_unicode_flags[message / 32] & SET(message)) != 0;
438 }
439
440 #undef SET
441
442 /* add a data field to a packed message */
443 static inline void push_data( struct packed_message *data, const void *ptr, size_t size )
444 {
445     data->data[data->count] = ptr;
446     data->size[data->count] = size;
447     data->count++;
448 }
449
450 /* add a string to a packed message */
451 static inline void push_string( struct packed_message *data, LPCWSTR str )
452 {
453     push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
454 }
455
456 /* make sure that the buffer contains a valid null-terminated Unicode string */
457 static inline BOOL check_string( LPCWSTR str, size_t size )
458 {
459     for (size /= sizeof(WCHAR); size; size--, str++)
460         if (!*str) return TRUE;
461     return FALSE;
462 }
463
464 /* pack a pointer into a 32/64 portable format */
465 static inline ULONGLONG pack_ptr( const void *ptr )
466 {
467     return (ULONG_PTR)ptr;
468 }
469
470 /* unpack a potentially 64-bit pointer, returning 0 when truncated */
471 static inline void *unpack_ptr( ULONGLONG ptr64 )
472 {
473     if ((ULONG_PTR)ptr64 != ptr64) return 0;
474     return (void *)(ULONG_PTR)ptr64;
475 }
476
477 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
478 static inline void *get_buffer_space( void **buffer, size_t size )
479 {
480     void *ret;
481
482     if (*buffer)
483     {
484         if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
485             HeapFree( GetProcessHeap(), 0, *buffer );
486     }
487     else ret = HeapAlloc( GetProcessHeap(), 0, size );
488
489     *buffer = ret;
490     return ret;
491 }
492
493 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
494 static inline BOOL combobox_has_strings( HWND hwnd )
495 {
496     DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
497     return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
498 }
499
500 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
501 static inline BOOL listbox_has_strings( HWND hwnd )
502 {
503     DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
504     return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
505 }
506
507 /* check whether message is in the range of keyboard messages */
508 static inline BOOL is_keyboard_message( UINT message )
509 {
510     return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
511 }
512
513 /* check whether message is in the range of mouse messages */
514 static inline BOOL is_mouse_message( UINT message )
515 {
516     return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
517             (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
518 }
519
520 /* check whether message matches the specified hwnd filter */
521 static inline BOOL check_hwnd_filter( const MSG *msg, HWND hwnd_filter )
522 {
523     if (!hwnd_filter || hwnd_filter == GetDesktopWindow()) return TRUE;
524     return (msg->hwnd == hwnd_filter || IsChild( hwnd_filter, msg->hwnd ));
525 }
526
527 /* check for pending WM_CHAR message with DBCS trailing byte */
528 static inline BOOL get_pending_wmchar( MSG *msg, UINT first, UINT last, BOOL remove )
529 {
530     struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
531
532     if (!data || !data->get_msg.message) return FALSE;
533     if ((first || last) && (first > WM_CHAR || last < WM_CHAR)) return FALSE;
534     if (!msg) return FALSE;
535     *msg = data->get_msg;
536     if (remove) data->get_msg.message = 0;
537     return TRUE;
538 }
539
540
541 /***********************************************************************
542  *           MessageWndProc
543  *
544  * Window procedure for "Message" windows (HWND_MESSAGE parent).
545  */
546 LRESULT WINAPI MessageWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
547 {
548     if (message == WM_NCCREATE) return TRUE;
549     return 0;  /* all other messages are ignored */
550 }
551
552
553 /***********************************************************************
554  *              broadcast_message_callback
555  *
556  * Helper callback for broadcasting messages.
557  */
558 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
559 {
560     struct send_message_info *info = (struct send_message_info *)lparam;
561     if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
562     switch(info->type)
563     {
564     case MSG_UNICODE:
565         SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
566                              info->flags, info->timeout, NULL );
567         break;
568     case MSG_ASCII:
569         SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
570                              info->flags, info->timeout, NULL );
571         break;
572     case MSG_NOTIFY:
573         SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
574         break;
575     case MSG_CALLBACK:
576         SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
577                               info->callback, info->data );
578         break;
579     case MSG_POSTED:
580         PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
581         break;
582     default:
583         ERR( "bad type %d\n", info->type );
584         break;
585     }
586     return TRUE;
587 }
588
589
590 /***********************************************************************
591  *              map_wparam_AtoW
592  *
593  * Convert the wparam of an ASCII message to Unicode.
594  */
595 BOOL map_wparam_AtoW( UINT message, WPARAM *wparam, enum wm_char_mapping mapping )
596 {
597     char ch[2];
598     WCHAR wch[2];
599
600     wch[0] = wch[1] = 0;
601     switch(message)
602     {
603     case WM_CHAR:
604         /* WM_CHAR is magic: a DBCS char can be sent/posted as two consecutive WM_CHAR
605          * messages, in which case the first char is stored, and the conversion
606          * to Unicode only takes place once the second char is sent/posted.
607          */
608         if (mapping != WMCHAR_MAP_NOMAPPING)
609         {
610             struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
611             BYTE low = LOBYTE(*wparam);
612
613             if (HIBYTE(*wparam))
614             {
615                 ch[0] = low;
616                 ch[1] = HIBYTE(*wparam);
617                 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
618                 TRACE( "map %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
619                 if (data) data->lead_byte[mapping] = 0;
620             }
621             else if (data && data->lead_byte[mapping])
622             {
623                 ch[0] = data->lead_byte[mapping];
624                 ch[1] = low;
625                 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
626                 TRACE( "map stored %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
627                 data->lead_byte[mapping] = 0;
628             }
629             else if (!IsDBCSLeadByte( low ))
630             {
631                 ch[0] = low;
632                 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 1 );
633                 TRACE( "map %02x -> %04x\n", (BYTE)ch[0], wch[0] );
634                 if (data) data->lead_byte[mapping] = 0;
635             }
636             else  /* store it and wait for trail byte */
637             {
638                 if (!data)
639                 {
640                     if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) )))
641                         return FALSE;
642                     get_user_thread_info()->wmchar_data = data;
643                 }
644                 TRACE( "storing lead byte %02x mapping %u\n", low, mapping );
645                 data->lead_byte[mapping] = low;
646                 return FALSE;
647             }
648             *wparam = MAKEWPARAM(wch[0], wch[1]);
649             break;
650         }
651         /* else fall through */
652     case WM_CHARTOITEM:
653     case EM_SETPASSWORDCHAR:
654     case WM_DEADCHAR:
655     case WM_SYSCHAR:
656     case WM_SYSDEADCHAR:
657     case WM_MENUCHAR:
658         ch[0] = LOBYTE(*wparam);
659         ch[1] = HIBYTE(*wparam);
660         RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
661         *wparam = MAKEWPARAM(wch[0], wch[1]);
662         break;
663     case WM_IME_CHAR:
664         ch[0] = HIBYTE(*wparam);
665         ch[1] = LOBYTE(*wparam);
666         if (ch[0]) RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch, 2 );
667         else RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch + 1, 1 );
668         *wparam = MAKEWPARAM(wch[0], HIWORD(*wparam));
669         break;
670     }
671     return TRUE;
672 }
673
674
675 /***********************************************************************
676  *              map_wparam_WtoA
677  *
678  * Convert the wparam of a Unicode message to ASCII.
679  */
680 static void map_wparam_WtoA( MSG *msg, BOOL remove )
681 {
682     BYTE ch[2];
683     WCHAR wch[2];
684     DWORD len;
685
686     switch(msg->message)
687     {
688     case WM_CHAR:
689         if (!HIWORD(msg->wParam))
690         {
691             wch[0] = LOWORD(msg->wParam);
692             ch[0] = ch[1] = 0;
693             RtlUnicodeToMultiByteN( (LPSTR)ch, 2, &len, wch, sizeof(wch[0]) );
694             if (len == 2)  /* DBCS char */
695             {
696                 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
697                 if (!data)
698                 {
699                     if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) ))) return;
700                     get_user_thread_info()->wmchar_data = data;
701                 }
702                 if (remove)
703                 {
704                     data->get_msg = *msg;
705                     data->get_msg.wParam = ch[1];
706                 }
707                 msg->wParam = ch[0];
708                 return;
709             }
710         }
711         /* else fall through */
712     case WM_CHARTOITEM:
713     case EM_SETPASSWORDCHAR:
714     case WM_DEADCHAR:
715     case WM_SYSCHAR:
716     case WM_SYSDEADCHAR:
717     case WM_MENUCHAR:
718         wch[0] = LOWORD(msg->wParam);
719         wch[1] = HIWORD(msg->wParam);
720         ch[0] = ch[1] = 0;
721         RtlUnicodeToMultiByteN( (LPSTR)ch, 2, NULL, wch, sizeof(wch) );
722         msg->wParam = MAKEWPARAM( ch[0] | (ch[1] << 8), 0 );
723         break;
724     case WM_IME_CHAR:
725         wch[0] = LOWORD(msg->wParam);
726         ch[0] = ch[1] = 0;
727         RtlUnicodeToMultiByteN( (LPSTR)ch, 2, &len, wch, sizeof(wch[0]) );
728         if (len == 2)
729             msg->wParam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(msg->wParam) );
730         else
731             msg->wParam = MAKEWPARAM( ch[0], HIWORD(msg->wParam) );
732         break;
733     }
734 }
735
736
737 /***********************************************************************
738  *              pack_message
739  *
740  * Pack a message for sending to another process.
741  * Return the size of the data we expect in the message reply.
742  * Set data->count to -1 if there is an error.
743  */
744 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
745                             struct packed_message *data )
746 {
747     data->count = 0;
748     switch(message)
749     {
750     case WM_NCCREATE:
751     case WM_CREATE:
752     {
753         CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
754         data->ps.cs.lpCreateParams = pack_ptr( cs->lpCreateParams );
755         data->ps.cs.hInstance      = pack_ptr( cs->hInstance );
756         data->ps.cs.hMenu          = wine_server_user_handle( cs->hMenu );
757         data->ps.cs.hwndParent     = wine_server_user_handle( cs->hwndParent );
758         data->ps.cs.cy             = cs->cy;
759         data->ps.cs.cx             = cs->cx;
760         data->ps.cs.y              = cs->y;
761         data->ps.cs.x              = cs->x;
762         data->ps.cs.style          = cs->style;
763         data->ps.cs.dwExStyle      = cs->dwExStyle;
764         data->ps.cs.lpszName       = pack_ptr( cs->lpszName );
765         data->ps.cs.lpszClass      = pack_ptr( cs->lpszClass );
766         push_data( data, &data->ps.cs, sizeof(data->ps.cs) );
767         if (!IS_INTRESOURCE(cs->lpszName)) push_string( data, cs->lpszName );
768         if (!IS_INTRESOURCE(cs->lpszClass)) push_string( data, cs->lpszClass );
769         return sizeof(data->ps.cs);
770     }
771     case WM_GETTEXT:
772     case WM_ASKCBFORMATNAME:
773         return wparam * sizeof(WCHAR);
774     case WM_WININICHANGE:
775         if (lparam) push_string(data, (LPWSTR)lparam );
776         return 0;
777     case WM_SETTEXT:
778     case WM_DEVMODECHANGE:
779     case CB_DIR:
780     case LB_DIR:
781     case LB_ADDFILE:
782     case EM_REPLACESEL:
783         push_string( data, (LPWSTR)lparam );
784         return 0;
785     case WM_GETMINMAXINFO:
786         push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
787         return sizeof(MINMAXINFO);
788     case WM_DRAWITEM:
789     {
790         DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)lparam;
791         data->ps.dis.CtlType    = dis->CtlType;
792         data->ps.dis.CtlID      = dis->CtlID;
793         data->ps.dis.itemID     = dis->itemID;
794         data->ps.dis.itemAction = dis->itemAction;
795         data->ps.dis.itemState  = dis->itemState;
796         data->ps.dis.hwndItem   = wine_server_user_handle( dis->hwndItem );
797         data->ps.dis.hDC        = wine_server_user_handle( dis->hDC );  /* FIXME */
798         data->ps.dis.rcItem     = dis->rcItem;
799         data->ps.dis.itemData   = dis->itemData;
800         push_data( data, &data->ps.dis, sizeof(data->ps.dis) );
801         return 0;
802     }
803     case WM_MEASUREITEM:
804     {
805         MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lparam;
806         data->ps.mis.CtlType    = mis->CtlType;
807         data->ps.mis.CtlID      = mis->CtlID;
808         data->ps.mis.itemID     = mis->itemID;
809         data->ps.mis.itemWidth  = mis->itemWidth;
810         data->ps.mis.itemHeight = mis->itemHeight;
811         data->ps.mis.itemData   = mis->itemData;
812         push_data( data, &data->ps.mis, sizeof(data->ps.mis) );
813         return sizeof(data->ps.mis);
814     }
815     case WM_DELETEITEM:
816     {
817         DELETEITEMSTRUCT *dls = (DELETEITEMSTRUCT *)lparam;
818         data->ps.dls.CtlType    = dls->CtlType;
819         data->ps.dls.CtlID      = dls->CtlID;
820         data->ps.dls.itemID     = dls->itemID;
821         data->ps.dls.hwndItem   = wine_server_user_handle( dls->hwndItem );
822         data->ps.dls.itemData   = dls->itemData;
823         push_data( data, &data->ps.dls, sizeof(data->ps.dls) );
824         return 0;
825     }
826     case WM_COMPAREITEM:
827     {
828         COMPAREITEMSTRUCT *cis = (COMPAREITEMSTRUCT *)lparam;
829         data->ps.cis.CtlType    = cis->CtlType;
830         data->ps.cis.CtlID      = cis->CtlID;
831         data->ps.cis.hwndItem   = wine_server_user_handle( cis->hwndItem );
832         data->ps.cis.itemID1    = cis->itemID1;
833         data->ps.cis.itemData1  = cis->itemData1;
834         data->ps.cis.itemID2    = cis->itemID2;
835         data->ps.cis.itemData2  = cis->itemData2;
836         data->ps.cis.dwLocaleId = cis->dwLocaleId;
837         push_data( data, &data->ps.cis, sizeof(data->ps.cis) );
838         return 0;
839     }
840     case WM_WINE_SETWINDOWPOS:
841     case WM_WINDOWPOSCHANGING:
842     case WM_WINDOWPOSCHANGED:
843     {
844         WINDOWPOS *wp = (WINDOWPOS *)lparam;
845         data->ps.wp.hwnd            = wine_server_user_handle( wp->hwnd );
846         data->ps.wp.hwndInsertAfter = wine_server_user_handle( wp->hwndInsertAfter );
847         data->ps.wp.x               = wp->x;
848         data->ps.wp.y               = wp->y;
849         data->ps.wp.cx              = wp->cx;
850         data->ps.wp.cy              = wp->cy;
851         data->ps.wp.flags           = wp->flags;
852         push_data( data, &data->ps.wp, sizeof(data->ps.wp) );
853         return sizeof(data->ps.wp);
854     }
855     case WM_COPYDATA:
856     {
857         COPYDATASTRUCT *cds = (COPYDATASTRUCT *)lparam;
858         data->ps.cds.cbData = cds->cbData;
859         data->ps.cds.dwData = cds->dwData;
860         data->ps.cds.lpData = pack_ptr( cds->lpData );
861         push_data( data, &data->ps.cds, sizeof(data->ps.cds) );
862         if (cds->lpData) push_data( data, cds->lpData, cds->cbData );
863         return 0;
864     }
865     case WM_NOTIFY:
866         /* WM_NOTIFY cannot be sent across processes (MSDN) */
867         data->count = -1;
868         return 0;
869     case WM_HELP:
870     {
871         HELPINFO *hi = (HELPINFO *)lparam;
872         data->ps.hi.iContextType = hi->iContextType;
873         data->ps.hi.iCtrlId      = hi->iCtrlId;
874         data->ps.hi.hItemHandle  = wine_server_user_handle( hi->hItemHandle );
875         data->ps.hi.dwContextId  = hi->dwContextId;
876         data->ps.hi.MousePos     = hi->MousePos;
877         push_data( data, &data->ps.hi, sizeof(data->ps.hi) );
878         return 0;
879     }
880     case WM_STYLECHANGING:
881     case WM_STYLECHANGED:
882         push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
883         return 0;
884     case WM_NCCALCSIZE:
885         if (!wparam)
886         {
887             push_data( data, (RECT *)lparam, sizeof(RECT) );
888             return sizeof(RECT);
889         }
890         else
891         {
892             NCCALCSIZE_PARAMS *ncp = (NCCALCSIZE_PARAMS *)lparam;
893             data->ps.ncp.rgrc[0]         = ncp->rgrc[0];
894             data->ps.ncp.rgrc[1]         = ncp->rgrc[1];
895             data->ps.ncp.rgrc[2]         = ncp->rgrc[2];
896             data->ps.ncp.hwnd            = wine_server_user_handle( ncp->lppos->hwnd );
897             data->ps.ncp.hwndInsertAfter = wine_server_user_handle( ncp->lppos->hwndInsertAfter );
898             data->ps.ncp.x               = ncp->lppos->x;
899             data->ps.ncp.y               = ncp->lppos->y;
900             data->ps.ncp.cx              = ncp->lppos->cx;
901             data->ps.ncp.cy              = ncp->lppos->cy;
902             data->ps.ncp.flags           = ncp->lppos->flags;
903             push_data( data, &data->ps.ncp, sizeof(data->ps.ncp) );
904             return sizeof(data->ps.ncp);
905         }
906     case WM_GETDLGCODE:
907         if (lparam)
908         {
909             MSG *msg = (MSG *)lparam;
910             data->ps.msg.hwnd    = wine_server_user_handle( msg->hwnd );
911             data->ps.msg.message = msg->message;
912             data->ps.msg.wParam  = msg->wParam;
913             data->ps.msg.lParam  = msg->lParam;
914             data->ps.msg.time    = msg->time;
915             data->ps.msg.pt      = msg->pt;
916             push_data( data, &data->ps.msg, sizeof(data->ps.msg) );
917             return sizeof(data->ps.msg);
918         }
919         return 0;
920     case SBM_SETSCROLLINFO:
921         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
922         return 0;
923     case SBM_GETSCROLLINFO:
924         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
925         return sizeof(SCROLLINFO);
926     case SBM_GETSCROLLBARINFO:
927     {
928         const SCROLLBARINFO *info = (const SCROLLBARINFO *)lparam;
929         size_t size = min( info->cbSize, sizeof(SCROLLBARINFO) );
930         push_data( data, info, size );
931         return size;
932     }
933     case EM_GETSEL:
934     case SBM_GETRANGE:
935     case CB_GETEDITSEL:
936     {
937         size_t size = 0;
938         if (wparam) size += sizeof(DWORD);
939         if (lparam) size += sizeof(DWORD);
940         return size;
941     }
942     case EM_GETRECT:
943     case LB_GETITEMRECT:
944     case CB_GETDROPPEDCONTROLRECT:
945         return sizeof(RECT);
946     case EM_SETRECT:
947     case EM_SETRECTNP:
948         push_data( data, (RECT *)lparam, sizeof(RECT) );
949         return 0;
950     case EM_GETLINE:
951     {
952         WORD *pw = (WORD *)lparam;
953         push_data( data, pw, sizeof(*pw) );
954         return *pw * sizeof(WCHAR);
955     }
956     case EM_SETTABSTOPS:
957     case LB_SETTABSTOPS:
958         if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
959         return 0;
960     case CB_ADDSTRING:
961     case CB_INSERTSTRING:
962     case CB_FINDSTRING:
963     case CB_FINDSTRINGEXACT:
964     case CB_SELECTSTRING:
965         if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
966         return 0;
967     case CB_GETLBTEXT:
968         if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
969         return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
970     case LB_ADDSTRING:
971     case LB_INSERTSTRING:
972     case LB_FINDSTRING:
973     case LB_FINDSTRINGEXACT:
974     case LB_SELECTSTRING:
975         if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
976         return 0;
977     case LB_GETTEXT:
978         if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
979         return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
980     case LB_GETSELITEMS:
981         return wparam * sizeof(UINT);
982     case WM_NEXTMENU:
983     {
984         MDINEXTMENU *mnm = (MDINEXTMENU *)lparam;
985         data->ps.mnm.hmenuIn   = wine_server_user_handle( mnm->hmenuIn );
986         data->ps.mnm.hmenuNext = wine_server_user_handle( mnm->hmenuNext );
987         data->ps.mnm.hwndNext  = wine_server_user_handle( mnm->hwndNext );
988         push_data( data, &data->ps.mnm, sizeof(data->ps.mnm) );
989         return sizeof(data->ps.mnm);
990     }
991     case WM_SIZING:
992     case WM_MOVING:
993         push_data( data, (RECT *)lparam, sizeof(RECT) );
994         return sizeof(RECT);
995     case WM_MDICREATE:
996     {
997         MDICREATESTRUCTW *mcs = (MDICREATESTRUCTW *)lparam;
998         data->ps.mcs.szClass = pack_ptr( mcs->szClass );
999         data->ps.mcs.szTitle = pack_ptr( mcs->szTitle );
1000         data->ps.mcs.hOwner  = pack_ptr( mcs->hOwner );
1001         data->ps.mcs.x       = mcs->x;
1002         data->ps.mcs.y       = mcs->y;
1003         data->ps.mcs.cx      = mcs->cx;
1004         data->ps.mcs.cy      = mcs->cy;
1005         data->ps.mcs.style   = mcs->style;
1006         data->ps.mcs.lParam  = mcs->lParam;
1007         push_data( data, &data->ps.mcs, sizeof(data->ps.mcs) );
1008         if (!IS_INTRESOURCE(mcs->szClass)) push_string( data, mcs->szClass );
1009         if (!IS_INTRESOURCE(mcs->szTitle)) push_string( data, mcs->szTitle );
1010         return sizeof(data->ps.mcs);
1011     }
1012     case WM_MDIGETACTIVE:
1013         if (lparam) return sizeof(BOOL);
1014         return 0;
1015     case WM_DEVICECHANGE:
1016     {
1017         DEV_BROADCAST_HDR *header = (DEV_BROADCAST_HDR *)lparam;
1018         push_data( data, header, header->dbch_size );
1019         return 0;
1020     }
1021     case WM_WINE_KEYBOARD_LL_HOOK:
1022     {
1023         struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
1024         data->ps.hook.handle = wine_server_user_handle( h_extra->handle );
1025         push_data( data, &data->ps.hook, sizeof(data->ps.hook) );
1026         push_data( data, (LPVOID)h_extra->lparam, sizeof(KBDLLHOOKSTRUCT) );
1027         return 0;
1028     }
1029     case WM_WINE_MOUSE_LL_HOOK:
1030     {
1031         struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
1032         data->ps.hook.handle = wine_server_user_handle( h_extra->handle );
1033         push_data( data, &data->ps.hook, sizeof(data->ps.hook) );
1034         push_data( data, (LPVOID)h_extra->lparam, sizeof(MSLLHOOKSTRUCT) );
1035         return 0;
1036     }
1037     case WM_NCPAINT:
1038         if (wparam <= 1) return 0;
1039         FIXME( "WM_NCPAINT hdc packing not supported yet\n" );
1040         data->count = -1;
1041         return 0;
1042     case WM_PAINT:
1043         if (!wparam) return 0;
1044         /* fall through */
1045
1046     /* these contain an HFONT */
1047     case WM_SETFONT:
1048     case WM_GETFONT:
1049     /* these contain an HDC */
1050     case WM_ERASEBKGND:
1051     case WM_ICONERASEBKGND:
1052     case WM_CTLCOLORMSGBOX:
1053     case WM_CTLCOLOREDIT:
1054     case WM_CTLCOLORLISTBOX:
1055     case WM_CTLCOLORBTN:
1056     case WM_CTLCOLORDLG:
1057     case WM_CTLCOLORSCROLLBAR:
1058     case WM_CTLCOLORSTATIC:
1059     case WM_PRINT:
1060     case WM_PRINTCLIENT:
1061     /* these contain an HGLOBAL */
1062     case WM_PAINTCLIPBOARD:
1063     case WM_SIZECLIPBOARD:
1064     /* these contain HICON */
1065     case WM_GETICON:
1066     case WM_SETICON:
1067     case WM_QUERYDRAGICON:
1068     case WM_QUERYPARKICON:
1069     /* these contain pointers */
1070     case WM_DROPOBJECT:
1071     case WM_QUERYDROPOBJECT:
1072     case WM_DRAGLOOP:
1073     case WM_DRAGSELECT:
1074     case WM_DRAGMOVE:
1075         FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
1076         data->count = -1;
1077         return 0;
1078     }
1079     return 0;
1080 }
1081
1082
1083 /***********************************************************************
1084  *              unpack_message
1085  *
1086  * Unpack a message received from another process.
1087  */
1088 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1089                             void **buffer, size_t size )
1090 {
1091     size_t minsize = 0;
1092     union packed_structs *ps = *buffer;
1093
1094     switch(message)
1095     {
1096     case WM_NCCREATE:
1097     case WM_CREATE:
1098     {
1099         CREATESTRUCTW cs;
1100         WCHAR *str = (WCHAR *)(&ps->cs + 1);
1101         if (size < sizeof(ps->cs)) return FALSE;
1102         size -= sizeof(ps->cs);
1103         cs.lpCreateParams = unpack_ptr( ps->cs.lpCreateParams );
1104         cs.hInstance      = unpack_ptr( ps->cs.hInstance );
1105         cs.hMenu          = wine_server_ptr_handle( ps->cs.hMenu );
1106         cs.hwndParent     = wine_server_ptr_handle( ps->cs.hwndParent );
1107         cs.cy             = ps->cs.cy;
1108         cs.cx             = ps->cs.cx;
1109         cs.y              = ps->cs.y;
1110         cs.x              = ps->cs.x;
1111         cs.style          = ps->cs.style;
1112         cs.dwExStyle      = ps->cs.dwExStyle;
1113         cs.lpszName       = unpack_ptr( ps->cs.lpszName );
1114         cs.lpszClass      = unpack_ptr( ps->cs.lpszClass );
1115         if (ps->cs.lpszName >> 16)
1116         {
1117             if (!check_string( str, size )) return FALSE;
1118             cs.lpszName = str;
1119             size -= (strlenW(str) + 1) * sizeof(WCHAR);
1120             str += strlenW(str) + 1;
1121         }
1122         if (ps->cs.lpszClass >> 16)
1123         {
1124             if (!check_string( str, size )) return FALSE;
1125             cs.lpszClass = str;
1126         }
1127         memcpy( &ps->cs, &cs, sizeof(cs) );
1128         break;
1129     }
1130     case WM_GETTEXT:
1131     case WM_ASKCBFORMATNAME:
1132         if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
1133         break;
1134     case WM_WININICHANGE:
1135         if (!*lparam) return TRUE;
1136         /* fall through */
1137     case WM_SETTEXT:
1138     case WM_DEVMODECHANGE:
1139     case CB_DIR:
1140     case LB_DIR:
1141     case LB_ADDFILE:
1142     case EM_REPLACESEL:
1143         if (!check_string( *buffer, size )) return FALSE;
1144         break;
1145     case WM_GETMINMAXINFO:
1146         minsize = sizeof(MINMAXINFO);
1147         break;
1148     case WM_DRAWITEM:
1149     {
1150         DRAWITEMSTRUCT dis;
1151         if (size < sizeof(ps->dis)) return FALSE;
1152         dis.CtlType    = ps->dis.CtlType;
1153         dis.CtlID      = ps->dis.CtlID;
1154         dis.itemID     = ps->dis.itemID;
1155         dis.itemAction = ps->dis.itemAction;
1156         dis.itemState  = ps->dis.itemState;
1157         dis.hwndItem   = wine_server_ptr_handle( ps->dis.hwndItem );
1158         dis.hDC        = wine_server_ptr_handle( ps->dis.hDC );
1159         dis.rcItem     = ps->dis.rcItem;
1160         dis.itemData   = (ULONG_PTR)unpack_ptr( ps->dis.itemData );
1161         memcpy( &ps->dis, &dis, sizeof(dis) );
1162         break;
1163     }
1164     case WM_MEASUREITEM:
1165     {
1166         MEASUREITEMSTRUCT mis;
1167         if (size < sizeof(ps->mis)) return FALSE;
1168         mis.CtlType    = ps->mis.CtlType;
1169         mis.CtlID      = ps->mis.CtlID;
1170         mis.itemID     = ps->mis.itemID;
1171         mis.itemWidth  = ps->mis.itemWidth;
1172         mis.itemHeight = ps->mis.itemHeight;
1173         mis.itemData   = (ULONG_PTR)unpack_ptr( ps->mis.itemData );
1174         memcpy( &ps->mis, &mis, sizeof(mis) );
1175         break;
1176     }
1177     case WM_DELETEITEM:
1178     {
1179         DELETEITEMSTRUCT dls;
1180         if (size < sizeof(ps->dls)) return FALSE;
1181         dls.CtlType    = ps->dls.CtlType;
1182         dls.CtlID      = ps->dls.CtlID;
1183         dls.itemID     = ps->dls.itemID;
1184         dls.hwndItem   = wine_server_ptr_handle( ps->dls.hwndItem );
1185         dls.itemData   = (ULONG_PTR)unpack_ptr( ps->dls.itemData );
1186         memcpy( &ps->dls, &dls, sizeof(dls) );
1187         break;
1188     }
1189     case WM_COMPAREITEM:
1190     {
1191         COMPAREITEMSTRUCT cis;
1192         if (size < sizeof(ps->cis)) return FALSE;
1193         cis.CtlType    = ps->cis.CtlType;
1194         cis.CtlID      = ps->cis.CtlID;
1195         cis.hwndItem   = wine_server_ptr_handle( ps->cis.hwndItem );
1196         cis.itemID1    = ps->cis.itemID1;
1197         cis.itemData1  = (ULONG_PTR)unpack_ptr( ps->cis.itemData1 );
1198         cis.itemID2    = ps->cis.itemID2;
1199         cis.itemData2  = (ULONG_PTR)unpack_ptr( ps->cis.itemData2 );
1200         cis.dwLocaleId = ps->cis.dwLocaleId;
1201         memcpy( &ps->cis, &cis, sizeof(cis) );
1202         break;
1203     }
1204     case WM_WINDOWPOSCHANGING:
1205     case WM_WINDOWPOSCHANGED:
1206     case WM_WINE_SETWINDOWPOS:
1207     {
1208         WINDOWPOS wp;
1209         if (size < sizeof(ps->wp)) return FALSE;
1210         wp.hwnd            = wine_server_ptr_handle( ps->wp.hwnd );
1211         wp.hwndInsertAfter = wine_server_ptr_handle( ps->wp.hwndInsertAfter );
1212         wp.x               = ps->wp.x;
1213         wp.y               = ps->wp.y;
1214         wp.cx              = ps->wp.cx;
1215         wp.cy              = ps->wp.cy;
1216         wp.flags           = ps->wp.flags;
1217         memcpy( &ps->wp, &wp, sizeof(wp) );
1218         break;
1219     }
1220     case WM_COPYDATA:
1221     {
1222         COPYDATASTRUCT cds;
1223         if (size < sizeof(ps->cds)) return FALSE;
1224         cds.dwData = (ULONG_PTR)unpack_ptr( ps->cds.dwData );
1225         if (ps->cds.lpData)
1226         {
1227             cds.cbData = ps->cds.cbData;
1228             cds.lpData = &ps->cds + 1;
1229             minsize = sizeof(ps->cds) + cds.cbData;
1230         }
1231         else
1232         {
1233             cds.cbData = 0;
1234             cds.lpData = 0;
1235         }
1236         memcpy( &ps->cds, &cds, sizeof(cds) );
1237         break;
1238     }
1239     case WM_NOTIFY:
1240         /* WM_NOTIFY cannot be sent across processes (MSDN) */
1241         return FALSE;
1242     case WM_HELP:
1243     {
1244         HELPINFO hi;
1245         if (size < sizeof(ps->hi)) return FALSE;
1246         hi.cbSize       = sizeof(hi);
1247         hi.iContextType = ps->hi.iContextType;
1248         hi.iCtrlId      = ps->hi.iCtrlId;
1249         hi.hItemHandle  = wine_server_ptr_handle( ps->hi.hItemHandle );
1250         hi.dwContextId  = (ULONG_PTR)unpack_ptr( ps->hi.dwContextId );
1251         hi.MousePos     = ps->hi.MousePos;
1252         memcpy( &ps->hi, &hi, sizeof(hi) );
1253         break;
1254     }
1255     case WM_STYLECHANGING:
1256     case WM_STYLECHANGED:
1257         minsize = sizeof(STYLESTRUCT);
1258         break;
1259     case WM_NCCALCSIZE:
1260         if (!*wparam) minsize = sizeof(RECT);
1261         else
1262         {
1263             NCCALCSIZE_PARAMS ncp;
1264             WINDOWPOS wp;
1265             if (size < sizeof(ps->ncp)) return FALSE;
1266             ncp.rgrc[0]        = ps->ncp.rgrc[0];
1267             ncp.rgrc[1]        = ps->ncp.rgrc[1];
1268             ncp.rgrc[2]        = ps->ncp.rgrc[2];
1269             wp.hwnd            = wine_server_ptr_handle( ps->ncp.hwnd );
1270             wp.hwndInsertAfter = wine_server_ptr_handle( ps->ncp.hwndInsertAfter );
1271             wp.x               = ps->ncp.x;
1272             wp.y               = ps->ncp.y;
1273             wp.cx              = ps->ncp.cx;
1274             wp.cy              = ps->ncp.cy;
1275             wp.flags           = ps->ncp.flags;
1276             ncp.lppos = (WINDOWPOS *)((NCCALCSIZE_PARAMS *)&ps->ncp + 1);
1277             memcpy( &ps->ncp, &ncp, sizeof(ncp) );
1278             *ncp.lppos = wp;
1279         }
1280         break;
1281     case WM_GETDLGCODE:
1282         if (*lparam)
1283         {
1284             MSG msg;
1285             if (size < sizeof(ps->msg)) return FALSE;
1286             msg.hwnd    = wine_server_ptr_handle( ps->msg.hwnd );
1287             msg.message = ps->msg.message;
1288             msg.wParam  = (ULONG_PTR)unpack_ptr( ps->msg.wParam );
1289             msg.lParam  = (ULONG_PTR)unpack_ptr( ps->msg.lParam );
1290             msg.time    = ps->msg.time;
1291             msg.pt      = ps->msg.pt;
1292             memcpy( &ps->msg, &msg, sizeof(msg) );
1293             break;
1294         }
1295         return TRUE;
1296     case SBM_SETSCROLLINFO:
1297         minsize = sizeof(SCROLLINFO);
1298         break;
1299     case SBM_GETSCROLLINFO:
1300         if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
1301         break;
1302     case SBM_GETSCROLLBARINFO:
1303         if (!get_buffer_space( buffer, sizeof(SCROLLBARINFO ))) return FALSE;
1304         break;
1305     case EM_GETSEL:
1306     case SBM_GETRANGE:
1307     case CB_GETEDITSEL:
1308         if (*wparam || *lparam)
1309         {
1310             if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
1311             if (*wparam) *wparam = (WPARAM)*buffer;
1312             if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
1313         }
1314         return TRUE;
1315     case EM_GETRECT:
1316     case LB_GETITEMRECT:
1317     case CB_GETDROPPEDCONTROLRECT:
1318         if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
1319         break;
1320     case EM_SETRECT:
1321     case EM_SETRECTNP:
1322         minsize = sizeof(RECT);
1323         break;
1324     case EM_GETLINE:
1325     {
1326         WORD len;
1327         if (size < sizeof(WORD)) return FALSE;
1328         len = *(WORD *)*buffer;
1329         if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
1330         *lparam = (LPARAM)*buffer + sizeof(WORD);  /* don't erase WORD at start of buffer */
1331         return TRUE;
1332     }
1333     case EM_SETTABSTOPS:
1334     case LB_SETTABSTOPS:
1335         if (!*wparam) return TRUE;
1336         minsize = *wparam * sizeof(UINT);
1337         break;
1338     case CB_ADDSTRING:
1339     case CB_INSERTSTRING:
1340     case CB_FINDSTRING:
1341     case CB_FINDSTRINGEXACT:
1342     case CB_SELECTSTRING:
1343     case LB_ADDSTRING:
1344     case LB_INSERTSTRING:
1345     case LB_FINDSTRING:
1346     case LB_FINDSTRINGEXACT:
1347     case LB_SELECTSTRING:
1348         if (!*buffer) return TRUE;
1349         if (!check_string( *buffer, size )) return FALSE;
1350         break;
1351     case CB_GETLBTEXT:
1352     {
1353         size = sizeof(ULONG_PTR);
1354         if (combobox_has_strings( hwnd ))
1355             size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
1356         if (!get_buffer_space( buffer, size )) return FALSE;
1357         break;
1358     }
1359     case LB_GETTEXT:
1360     {
1361         size = sizeof(ULONG_PTR);
1362         if (listbox_has_strings( hwnd ))
1363             size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
1364         if (!get_buffer_space( buffer, size )) return FALSE;
1365         break;
1366     }
1367     case LB_GETSELITEMS:
1368         if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
1369         break;
1370     case WM_NEXTMENU:
1371     {
1372         MDINEXTMENU mnm;
1373         if (size < sizeof(ps->mnm)) return FALSE;
1374         mnm.hmenuIn   = wine_server_ptr_handle( ps->mnm.hmenuIn );
1375         mnm.hmenuNext = wine_server_ptr_handle( ps->mnm.hmenuNext );
1376         mnm.hwndNext  = wine_server_ptr_handle( ps->mnm.hwndNext );
1377         memcpy( &ps->mnm, &mnm, sizeof(mnm) );
1378         break;
1379     }
1380     case WM_SIZING:
1381     case WM_MOVING:
1382         minsize = sizeof(RECT);
1383         if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
1384         break;
1385     case WM_MDICREATE:
1386     {
1387         MDICREATESTRUCTW mcs;
1388         WCHAR *str = (WCHAR *)(&ps->mcs + 1);
1389         if (size < sizeof(ps->mcs)) return FALSE;
1390         size -= sizeof(ps->mcs);
1391
1392         mcs.szClass = unpack_ptr( ps->mcs.szClass );
1393         mcs.szTitle = unpack_ptr( ps->mcs.szTitle );
1394         mcs.hOwner  = unpack_ptr( ps->mcs.hOwner );
1395         mcs.x       = ps->mcs.x;
1396         mcs.y       = ps->mcs.y;
1397         mcs.cx      = ps->mcs.cx;
1398         mcs.cy      = ps->mcs.cy;
1399         mcs.style   = ps->mcs.style;
1400         mcs.lParam  = (LPARAM)unpack_ptr( ps->mcs.lParam );
1401         if (ps->mcs.szClass >> 16)
1402         {
1403             if (!check_string( str, size )) return FALSE;
1404             mcs.szClass = str;
1405             size -= (strlenW(str) + 1) * sizeof(WCHAR);
1406             str += strlenW(str) + 1;
1407         }
1408         if (ps->mcs.szTitle >> 16)
1409         {
1410             if (!check_string( str, size )) return FALSE;
1411             mcs.szTitle = str;
1412         }
1413         memcpy( &ps->mcs, &mcs, sizeof(mcs) );
1414         break;
1415     }
1416     case WM_MDIGETACTIVE:
1417         if (!*lparam) return TRUE;
1418         if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
1419         break;
1420     case WM_DEVICECHANGE:
1421         minsize = sizeof(DEV_BROADCAST_HDR);
1422         break;
1423     case WM_WINE_KEYBOARD_LL_HOOK:
1424     case WM_WINE_MOUSE_LL_HOOK:
1425     {
1426         struct hook_extra_info h_extra;
1427         minsize = sizeof(ps->hook) +
1428                   (message == WM_WINE_KEYBOARD_LL_HOOK ? sizeof(KBDLLHOOKSTRUCT)
1429                                                        : sizeof(MSLLHOOKSTRUCT));
1430         if (size < minsize) return FALSE;
1431         h_extra.handle = wine_server_ptr_handle( ps->hook.handle );
1432         h_extra.lparam = (LPARAM)(&ps->hook + 1);
1433         memcpy( &ps->hook, &h_extra, sizeof(h_extra) );
1434         break;
1435     }
1436     case WM_NCPAINT:
1437         if (*wparam <= 1) return TRUE;
1438         FIXME( "WM_NCPAINT hdc unpacking not supported\n" );
1439         return FALSE;
1440     case WM_PAINT:
1441         if (!*wparam) return TRUE;
1442         /* fall through */
1443
1444     /* these contain an HFONT */
1445     case WM_SETFONT:
1446     case WM_GETFONT:
1447     /* these contain an HDC */
1448     case WM_ERASEBKGND:
1449     case WM_ICONERASEBKGND:
1450     case WM_CTLCOLORMSGBOX:
1451     case WM_CTLCOLOREDIT:
1452     case WM_CTLCOLORLISTBOX:
1453     case WM_CTLCOLORBTN:
1454     case WM_CTLCOLORDLG:
1455     case WM_CTLCOLORSCROLLBAR:
1456     case WM_CTLCOLORSTATIC:
1457     case WM_PRINT:
1458     case WM_PRINTCLIENT:
1459     /* these contain an HGLOBAL */
1460     case WM_PAINTCLIPBOARD:
1461     case WM_SIZECLIPBOARD:
1462     /* these contain HICON */
1463     case WM_GETICON:
1464     case WM_SETICON:
1465     case WM_QUERYDRAGICON:
1466     case WM_QUERYPARKICON:
1467     /* these contain pointers */
1468     case WM_DROPOBJECT:
1469     case WM_QUERYDROPOBJECT:
1470     case WM_DRAGLOOP:
1471     case WM_DRAGSELECT:
1472     case WM_DRAGMOVE:
1473         FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
1474         return FALSE;
1475
1476     default:
1477         return TRUE; /* message doesn't need any unpacking */
1478     }
1479
1480     /* default exit for most messages: check minsize and store buffer in lparam */
1481     if (size < minsize) return FALSE;
1482     *lparam = (LPARAM)*buffer;
1483     return TRUE;
1484 }
1485
1486
1487 /***********************************************************************
1488  *              pack_reply
1489  *
1490  * Pack a reply to a message for sending to another process.
1491  */
1492 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1493                         LRESULT res, struct packed_message *data )
1494 {
1495     data->count = 0;
1496     switch(message)
1497     {
1498     case WM_NCCREATE:
1499     case WM_CREATE:
1500     {
1501         CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
1502         data->ps.cs.lpCreateParams = (ULONG_PTR)cs->lpCreateParams;
1503         data->ps.cs.hInstance      = (ULONG_PTR)cs->hInstance;
1504         data->ps.cs.hMenu          = wine_server_user_handle( cs->hMenu );
1505         data->ps.cs.hwndParent     = wine_server_user_handle( cs->hwndParent );
1506         data->ps.cs.cy             = cs->cy;
1507         data->ps.cs.cx             = cs->cx;
1508         data->ps.cs.y              = cs->y;
1509         data->ps.cs.x              = cs->x;
1510         data->ps.cs.style          = cs->style;
1511         data->ps.cs.dwExStyle      = cs->dwExStyle;
1512         data->ps.cs.lpszName       = (ULONG_PTR)cs->lpszName;
1513         data->ps.cs.lpszClass      = (ULONG_PTR)cs->lpszClass;
1514         push_data( data, &data->ps.cs, sizeof(data->ps.cs) );
1515         break;
1516     }
1517     case WM_GETTEXT:
1518     case CB_GETLBTEXT:
1519     case LB_GETTEXT:
1520         push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
1521         break;
1522     case WM_GETMINMAXINFO:
1523         push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
1524         break;
1525     case WM_MEASUREITEM:
1526     {
1527         MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lparam;
1528         data->ps.mis.CtlType    = mis->CtlType;
1529         data->ps.mis.CtlID      = mis->CtlID;
1530         data->ps.mis.itemID     = mis->itemID;
1531         data->ps.mis.itemWidth  = mis->itemWidth;
1532         data->ps.mis.itemHeight = mis->itemHeight;
1533         data->ps.mis.itemData   = mis->itemData;
1534         push_data( data, &data->ps.mis, sizeof(data->ps.mis) );
1535         break;
1536     }
1537     case WM_WINDOWPOSCHANGING:
1538     case WM_WINDOWPOSCHANGED:
1539     {
1540         WINDOWPOS *wp = (WINDOWPOS *)lparam;
1541         data->ps.wp.hwnd            = wine_server_user_handle( wp->hwnd );
1542         data->ps.wp.hwndInsertAfter = wine_server_user_handle( wp->hwndInsertAfter );
1543         data->ps.wp.x               = wp->x;
1544         data->ps.wp.y               = wp->y;
1545         data->ps.wp.cx              = wp->cx;
1546         data->ps.wp.cy              = wp->cy;
1547         data->ps.wp.flags           = wp->flags;
1548         push_data( data, &data->ps.wp, sizeof(data->ps.wp) );
1549         break;
1550     }
1551     case WM_GETDLGCODE:
1552         if (lparam)
1553         {
1554             MSG *msg = (MSG *)lparam;
1555             data->ps.msg.hwnd    = wine_server_user_handle( msg->hwnd );
1556             data->ps.msg.message = msg->message;
1557             data->ps.msg.wParam  = msg->wParam;
1558             data->ps.msg.lParam  = msg->lParam;
1559             data->ps.msg.time    = msg->time;
1560             data->ps.msg.pt      = msg->pt;
1561             push_data( data, &data->ps.msg, sizeof(data->ps.msg) );
1562         }
1563         break;
1564     case SBM_GETSCROLLINFO:
1565         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
1566         break;
1567     case EM_GETRECT:
1568     case LB_GETITEMRECT:
1569     case CB_GETDROPPEDCONTROLRECT:
1570     case WM_SIZING:
1571     case WM_MOVING:
1572         push_data( data, (RECT *)lparam, sizeof(RECT) );
1573         break;
1574     case EM_GETLINE:
1575     {
1576         WORD *ptr = (WORD *)lparam;
1577         push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
1578         break;
1579     }
1580     case LB_GETSELITEMS:
1581         push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
1582         break;
1583     case WM_MDIGETACTIVE:
1584         if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
1585         break;
1586     case WM_NCCALCSIZE:
1587         if (!wparam)
1588             push_data( data, (RECT *)lparam, sizeof(RECT) );
1589         else
1590         {
1591             NCCALCSIZE_PARAMS *ncp = (NCCALCSIZE_PARAMS *)lparam;
1592             data->ps.ncp.rgrc[0]         = ncp->rgrc[0];
1593             data->ps.ncp.rgrc[1]         = ncp->rgrc[1];
1594             data->ps.ncp.rgrc[2]         = ncp->rgrc[2];
1595             data->ps.ncp.hwnd            = wine_server_user_handle( ncp->lppos->hwnd );
1596             data->ps.ncp.hwndInsertAfter = wine_server_user_handle( ncp->lppos->hwndInsertAfter );
1597             data->ps.ncp.x               = ncp->lppos->x;
1598             data->ps.ncp.y               = ncp->lppos->y;
1599             data->ps.ncp.cx              = ncp->lppos->cx;
1600             data->ps.ncp.cy              = ncp->lppos->cy;
1601             data->ps.ncp.flags           = ncp->lppos->flags;
1602             push_data( data, &data->ps.ncp, sizeof(data->ps.ncp) );
1603         }
1604         break;
1605     case EM_GETSEL:
1606     case SBM_GETRANGE:
1607     case CB_GETEDITSEL:
1608         if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
1609         if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
1610         break;
1611     case WM_NEXTMENU:
1612     {
1613         MDINEXTMENU *mnm = (MDINEXTMENU *)lparam;
1614         data->ps.mnm.hmenuIn   = wine_server_user_handle( mnm->hmenuIn );
1615         data->ps.mnm.hmenuNext = wine_server_user_handle( mnm->hmenuNext );
1616         data->ps.mnm.hwndNext  = wine_server_user_handle( mnm->hwndNext );
1617         push_data( data, &data->ps.mnm, sizeof(data->ps.mnm) );
1618         break;
1619     }
1620     case WM_MDICREATE:
1621     {
1622         MDICREATESTRUCTW *mcs = (MDICREATESTRUCTW *)lparam;
1623         data->ps.mcs.szClass = pack_ptr( mcs->szClass );
1624         data->ps.mcs.szTitle = pack_ptr( mcs->szTitle );
1625         data->ps.mcs.hOwner  = pack_ptr( mcs->hOwner );
1626         data->ps.mcs.x       = mcs->x;
1627         data->ps.mcs.y       = mcs->y;
1628         data->ps.mcs.cx      = mcs->cx;
1629         data->ps.mcs.cy      = mcs->cy;
1630         data->ps.mcs.style   = mcs->style;
1631         data->ps.mcs.lParam  = mcs->lParam;
1632         push_data( data, &data->ps.mcs, sizeof(data->ps.mcs) );
1633         break;
1634     }
1635     case WM_ASKCBFORMATNAME:
1636         push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
1637         break;
1638     }
1639 }
1640
1641
1642 /***********************************************************************
1643  *              unpack_reply
1644  *
1645  * Unpack a message reply received from another process.
1646  */
1647 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1648                           void *buffer, size_t size )
1649 {
1650     union packed_structs *ps = buffer;
1651
1652     switch(message)
1653     {
1654     case WM_NCCREATE:
1655     case WM_CREATE:
1656         if (size >= sizeof(ps->cs))
1657         {
1658             CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
1659             cs->lpCreateParams = unpack_ptr( ps->cs.lpCreateParams );
1660             cs->hInstance      = unpack_ptr( ps->cs.hInstance );
1661             cs->hMenu          = wine_server_ptr_handle( ps->cs.hMenu );
1662             cs->hwndParent     = wine_server_ptr_handle( ps->cs.hwndParent );
1663             cs->cy             = ps->cs.cy;
1664             cs->cx             = ps->cs.cx;
1665             cs->y              = ps->cs.y;
1666             cs->x              = ps->cs.x;
1667             cs->style          = ps->cs.style;
1668             cs->dwExStyle      = ps->cs.dwExStyle;
1669             /* don't allow changing name and class pointers */
1670         }
1671         break;
1672     case WM_GETTEXT:
1673     case WM_ASKCBFORMATNAME:
1674         memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
1675         break;
1676     case WM_GETMINMAXINFO:
1677         memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
1678         break;
1679     case WM_MEASUREITEM:
1680         if (size >= sizeof(ps->mis))
1681         {
1682             MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lparam;
1683             mis->CtlType    = ps->mis.CtlType;
1684             mis->CtlID      = ps->mis.CtlID;
1685             mis->itemID     = ps->mis.itemID;
1686             mis->itemWidth  = ps->mis.itemWidth;
1687             mis->itemHeight = ps->mis.itemHeight;
1688             mis->itemData   = (ULONG_PTR)unpack_ptr( ps->mis.itemData );
1689         }
1690         break;
1691     case WM_WINDOWPOSCHANGING:
1692     case WM_WINDOWPOSCHANGED:
1693         if (size >= sizeof(ps->wp))
1694         {
1695             WINDOWPOS *wp = (WINDOWPOS *)lparam;
1696             wp->hwnd            = wine_server_ptr_handle( ps->wp.hwnd );
1697             wp->hwndInsertAfter = wine_server_ptr_handle( ps->wp.hwndInsertAfter );
1698             wp->x               = ps->wp.x;
1699             wp->y               = ps->wp.y;
1700             wp->cx              = ps->wp.cx;
1701             wp->cy              = ps->wp.cy;
1702             wp->flags           = ps->wp.flags;
1703         }
1704         break;
1705     case WM_GETDLGCODE:
1706         if (lparam && size >= sizeof(ps->msg))
1707         {
1708             MSG *msg = (MSG *)lparam;
1709             msg->hwnd    = wine_server_ptr_handle( ps->msg.hwnd );
1710             msg->message = ps->msg.message;
1711             msg->wParam  = (ULONG_PTR)unpack_ptr( ps->msg.wParam );
1712             msg->lParam  = (ULONG_PTR)unpack_ptr( ps->msg.lParam );
1713             msg->time    = ps->msg.time;
1714             msg->pt      = ps->msg.pt;
1715         }
1716         break;
1717     case SBM_GETSCROLLINFO:
1718         memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
1719         break;
1720     case SBM_GETSCROLLBARINFO:
1721         memcpy( (SCROLLBARINFO *)lparam, buffer, min( sizeof(SCROLLBARINFO), size ));
1722         break;
1723     case EM_GETRECT:
1724     case CB_GETDROPPEDCONTROLRECT:
1725     case LB_GETITEMRECT:
1726     case WM_SIZING:
1727     case WM_MOVING:
1728         memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1729         break;
1730     case EM_GETLINE:
1731         size = min( size, (size_t)*(WORD *)lparam );
1732         memcpy( (WCHAR *)lparam, buffer, size );
1733         break;
1734     case LB_GETSELITEMS:
1735         memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
1736         break;
1737     case LB_GETTEXT:
1738     case CB_GETLBTEXT:
1739         memcpy( (WCHAR *)lparam, buffer, size );
1740         break;
1741     case WM_NEXTMENU:
1742         if (size >= sizeof(ps->mnm))
1743         {
1744             MDINEXTMENU *mnm = (MDINEXTMENU *)lparam;
1745             mnm->hmenuIn   = wine_server_ptr_handle( ps->mnm.hmenuIn );
1746             mnm->hmenuNext = wine_server_ptr_handle( ps->mnm.hmenuNext );
1747             mnm->hwndNext  = wine_server_ptr_handle( ps->mnm.hwndNext );
1748         }
1749         break;
1750     case WM_MDIGETACTIVE:
1751         if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
1752         break;
1753     case WM_NCCALCSIZE:
1754         if (!wparam)
1755             memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1756         else if (size >= sizeof(ps->ncp))
1757         {
1758             NCCALCSIZE_PARAMS *ncp = (NCCALCSIZE_PARAMS *)lparam;
1759             ncp->rgrc[0]                = ps->ncp.rgrc[0];
1760             ncp->rgrc[1]                = ps->ncp.rgrc[1];
1761             ncp->rgrc[2]                = ps->ncp.rgrc[2];
1762             ncp->lppos->hwnd            = wine_server_ptr_handle( ps->ncp.hwnd );
1763             ncp->lppos->hwndInsertAfter = wine_server_ptr_handle( ps->ncp.hwndInsertAfter );
1764             ncp->lppos->x               = ps->ncp.x;
1765             ncp->lppos->y               = ps->ncp.y;
1766             ncp->lppos->cx              = ps->ncp.cx;
1767             ncp->lppos->cy              = ps->ncp.cy;
1768             ncp->lppos->flags           = ps->ncp.flags;
1769         }
1770         break;
1771     case EM_GETSEL:
1772     case SBM_GETRANGE:
1773     case CB_GETEDITSEL:
1774         if (wparam)
1775         {
1776             memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1777             if (size <= sizeof(DWORD)) break;
1778             size -= sizeof(DWORD);
1779             buffer = (DWORD *)buffer + 1;
1780         }
1781         if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1782         break;
1783     case WM_MDICREATE:
1784         if (size >= sizeof(ps->mcs))
1785         {
1786             MDICREATESTRUCTW *mcs = (MDICREATESTRUCTW *)lparam;
1787             mcs->hOwner  = unpack_ptr( ps->mcs.hOwner );
1788             mcs->x       = ps->mcs.x;
1789             mcs->y       = ps->mcs.y;
1790             mcs->cx      = ps->mcs.cx;
1791             mcs->cy      = ps->mcs.cy;
1792             mcs->style   = ps->mcs.style;
1793             mcs->lParam  = (LPARAM)unpack_ptr( ps->mcs.lParam );
1794             /* don't allow changing class and title pointers */
1795         }
1796         break;
1797     default:
1798         ERR( "should not happen: unexpected message %x\n", message );
1799         break;
1800     }
1801 }
1802
1803
1804 /***********************************************************************
1805  *           reply_message
1806  *
1807  * Send a reply to a sent message.
1808  */
1809 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1810 {
1811     struct packed_message data;
1812     int i, replied = info->flags & ISMEX_REPLIED;
1813
1814     if (info->flags & ISMEX_NOTIFY) return;  /* notify messages don't get replies */
1815     if (!remove && replied) return;  /* replied already */
1816
1817     memset( &data, 0, sizeof(data) );
1818     info->flags |= ISMEX_REPLIED;
1819
1820     if (info->type == MSG_OTHER_PROCESS && !replied)
1821     {
1822         pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1823                     info->msg.lParam, result, &data );
1824     }
1825
1826     SERVER_START_REQ( reply_message )
1827     {
1828         req->result = result;
1829         req->remove = remove;
1830         for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1831         wine_server_call( req );
1832     }
1833     SERVER_END_REQ;
1834 }
1835
1836
1837 /***********************************************************************
1838  *           handle_internal_message
1839  *
1840  * Handle an internal Wine message instead of calling the window proc.
1841  */
1842 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1843 {
1844     switch(msg)
1845     {
1846     case WM_WINE_DESTROYWINDOW:
1847         return WIN_DestroyWindow( hwnd );
1848     case WM_WINE_SETWINDOWPOS:
1849         if (is_desktop_window( hwnd )) return 0;
1850         return USER_SetWindowPos( (WINDOWPOS *)lparam );
1851     case WM_WINE_SHOWWINDOW:
1852         if (is_desktop_window( hwnd )) return 0;
1853         return ShowWindow( hwnd, wparam );
1854     case WM_WINE_SETPARENT:
1855         if (is_desktop_window( hwnd )) return 0;
1856         return (LRESULT)SetParent( hwnd, (HWND)wparam );
1857     case WM_WINE_SETWINDOWLONG:
1858         return WIN_SetWindowLong( hwnd, (short)LOWORD(wparam), HIWORD(wparam), lparam, TRUE );
1859     case WM_WINE_ENABLEWINDOW:
1860         if (is_desktop_window( hwnd )) return 0;
1861         return EnableWindow( hwnd, wparam );
1862     case WM_WINE_SETACTIVEWINDOW:
1863         if (is_desktop_window( hwnd )) return 0;
1864         return (LRESULT)SetActiveWindow( (HWND)wparam );
1865     case WM_WINE_KEYBOARD_LL_HOOK:
1866     case WM_WINE_MOUSE_LL_HOOK:
1867     {
1868         struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
1869
1870         return call_current_hook( h_extra->handle, HC_ACTION, wparam, h_extra->lparam );
1871     }
1872     case WM_WINE_CLIPCURSOR:
1873         if (wparam)
1874         {
1875             RECT rect;
1876             GetClipCursor( &rect );
1877             return USER_Driver->pClipCursor( &rect );
1878         }
1879         return USER_Driver->pClipCursor( NULL );
1880     default:
1881         if (msg >= WM_WINE_FIRST_DRIVER_MSG && msg <= WM_WINE_LAST_DRIVER_MSG)
1882             return USER_Driver->pWindowMessage( hwnd, msg, wparam, lparam );
1883         FIXME( "unknown internal message %x\n", msg );
1884         return 0;
1885     }
1886 }
1887
1888 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1889  * to the memory handle, we keep track (in the server side) of all pairs of handle
1890  * used (the client passes its value and the content of the memory handle), and
1891  * the server stored both values (the client, and the local one, created after the
1892  * content). When a ACK message is generated, the list of pair is searched for a
1893  * matching pair, so that the client memory handle can be returned.
1894  */
1895 struct DDE_pair {
1896     HGLOBAL     client_hMem;
1897     HGLOBAL     server_hMem;
1898 };
1899
1900 static      struct DDE_pair*    dde_pairs;
1901 static      int                 dde_num_alloc;
1902 static      int                 dde_num_used;
1903
1904 static CRITICAL_SECTION dde_crst;
1905 static CRITICAL_SECTION_DEBUG critsect_debug =
1906 {
1907     0, 0, &dde_crst,
1908     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
1909       0, 0, { (DWORD_PTR)(__FILE__ ": dde_crst") }
1910 };
1911 static CRITICAL_SECTION dde_crst = { &critsect_debug, -1, 0, 0, 0, 0 };
1912
1913 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1914 {
1915     int  i;
1916 #define GROWBY  4
1917
1918     EnterCriticalSection(&dde_crst);
1919
1920     /* now remember the pair of hMem on both sides */
1921     if (dde_num_used == dde_num_alloc)
1922     {
1923         struct DDE_pair* tmp;
1924         if (dde_pairs)
1925             tmp  = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1926                                             (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1927         else
1928             tmp  = HeapAlloc( GetProcessHeap(), 0, 
1929                                             (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1930
1931         if (!tmp)
1932         {
1933             LeaveCriticalSection(&dde_crst);
1934             return FALSE;
1935         }
1936         dde_pairs = tmp;
1937         /* zero out newly allocated part */
1938         memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1939         dde_num_alloc += GROWBY;
1940     }
1941 #undef GROWBY
1942     for (i = 0; i < dde_num_alloc; i++)
1943     {
1944         if (dde_pairs[i].server_hMem == 0)
1945         {
1946             dde_pairs[i].client_hMem = chm;
1947             dde_pairs[i].server_hMem = shm;
1948             dde_num_used++;
1949             break;
1950         }
1951     }
1952     LeaveCriticalSection(&dde_crst);
1953     return TRUE;
1954 }
1955
1956 static HGLOBAL dde_get_pair(HGLOBAL shm)
1957 {
1958     int  i;
1959     HGLOBAL     ret = 0;
1960
1961     EnterCriticalSection(&dde_crst);
1962     for (i = 0; i < dde_num_alloc; i++)
1963     {
1964         if (dde_pairs[i].server_hMem == shm)
1965         {
1966             /* free this pair */
1967             dde_pairs[i].server_hMem = 0;
1968             dde_num_used--;
1969             ret = dde_pairs[i].client_hMem;
1970             break;
1971         }
1972     }
1973     LeaveCriticalSection(&dde_crst);
1974     return ret;
1975 }
1976
1977 /***********************************************************************
1978  *              post_dde_message
1979  *
1980  * Post a DDE message
1981  */
1982 static BOOL post_dde_message( struct packed_message *data, const struct send_message_info *info )
1983 {
1984     void*       ptr = NULL;
1985     int         size = 0;
1986     UINT_PTR    uiLo, uiHi;
1987     LPARAM      lp = 0;
1988     HGLOBAL     hunlock = 0;
1989     int         i;
1990     DWORD       res;
1991
1992     if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1993         return FALSE;
1994
1995     lp = info->lparam;
1996     switch (info->msg)
1997     {
1998         /* DDE messages which don't require packing are:
1999          * WM_DDE_INITIATE
2000          * WM_DDE_TERMINATE
2001          * WM_DDE_REQUEST
2002          * WM_DDE_UNADVISE
2003          */
2004     case WM_DDE_ACK:
2005         if (HIWORD(uiHi))
2006         {
2007             /* uiHi should contain a hMem from WM_DDE_EXECUTE */
2008             HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
2009             if (h)
2010             {
2011                 ULONGLONG hpack = pack_ptr( h );
2012                 /* send back the value of h on the other side */
2013                 push_data( data, &hpack, sizeof(hpack) );
2014                 lp = uiLo;
2015                 TRACE( "send dde-ack %lx %08lx => %p\n", uiLo, uiHi, h );
2016             }
2017         }
2018         else
2019         {
2020             /* uiHi should contain either an atom or 0 */
2021             TRACE( "send dde-ack %lx atom=%lx\n", uiLo, uiHi );
2022             lp = MAKELONG( uiLo, uiHi );
2023         }
2024         break;
2025     case WM_DDE_ADVISE:
2026     case WM_DDE_DATA:
2027     case WM_DDE_POKE:
2028         size = 0;
2029         if (uiLo)
2030         {
2031             size = GlobalSize( (HGLOBAL)uiLo ) ;
2032             if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
2033                 (info->msg == WM_DDE_DATA   && size < FIELD_OFFSET(DDEDATA, Value)) ||
2034                 (info->msg == WM_DDE_POKE   && size < FIELD_OFFSET(DDEPOKE, Value))
2035                 )
2036             return FALSE;
2037         }
2038         else if (info->msg != WM_DDE_DATA) return FALSE;
2039
2040         lp = uiHi;
2041         if (uiLo)
2042         {
2043             if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
2044             {
2045                 DDEDATA *dde_data = ptr;
2046                 TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
2047                        dde_data->unused, dde_data->fResponse, dde_data->fRelease,
2048                        dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
2049                 push_data( data, ptr, size );
2050                 hunlock = (HGLOBAL)uiLo;
2051             }
2052         }
2053         TRACE( "send ddepack %u %lx\n", size, uiHi );
2054         break;
2055     case WM_DDE_EXECUTE:
2056         if (info->lparam)
2057         {
2058             if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
2059             {
2060                 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
2061                 /* so that the other side can send it back on ACK */
2062                 lp = info->lparam;
2063                 hunlock = (HGLOBAL)info->lparam;
2064             }
2065         }
2066         break;
2067     }
2068     SERVER_START_REQ( send_message )
2069     {
2070         req->id      = info->dest_tid;
2071         req->type    = info->type;
2072         req->flags   = 0;
2073         req->win     = wine_server_user_handle( info->hwnd );
2074         req->msg     = info->msg;
2075         req->wparam  = info->wparam;
2076         req->lparam  = lp;
2077         req->timeout = TIMEOUT_INFINITE;
2078         for (i = 0; i < data->count; i++)
2079             wine_server_add_data( req, data->data[i], data->size[i] );
2080         if ((res = wine_server_call( req )))
2081         {
2082             if (res == STATUS_INVALID_PARAMETER)
2083                 /* FIXME: find a STATUS_ value for this one */
2084                 SetLastError( ERROR_INVALID_THREAD_ID );
2085             else
2086                 SetLastError( RtlNtStatusToDosError(res) );
2087         }
2088         else
2089             FreeDDElParam(info->msg, info->lparam);
2090     }
2091     SERVER_END_REQ;
2092     if (hunlock) GlobalUnlock(hunlock);
2093
2094     return !res;
2095 }
2096
2097 /***********************************************************************
2098  *              unpack_dde_message
2099  *
2100  * Unpack a posted DDE message received from another process.
2101  */
2102 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
2103                                 void **buffer, size_t size )
2104 {
2105     UINT_PTR    uiLo, uiHi;
2106     HGLOBAL     hMem = 0;
2107     void*       ptr;
2108
2109     switch (message)
2110     {
2111     case WM_DDE_ACK:
2112         if (size)
2113         {
2114             ULONGLONG hpack;
2115             /* hMem is being passed */
2116             if (size != sizeof(hpack)) return FALSE;
2117             if (!buffer || !*buffer) return FALSE;
2118             uiLo = *lparam;
2119             memcpy( &hpack, *buffer, size );
2120             hMem = unpack_ptr( hpack );
2121             uiHi = (UINT_PTR)hMem;
2122             TRACE("recv dde-ack %lx mem=%lx[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
2123         }
2124         else
2125         {
2126             uiLo = LOWORD( *lparam );
2127             uiHi = HIWORD( *lparam );
2128             TRACE("recv dde-ack %lx atom=%lx\n", uiLo, uiHi);
2129         }
2130         *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
2131         break;
2132     case WM_DDE_ADVISE:
2133     case WM_DDE_DATA:
2134     case WM_DDE_POKE:
2135         if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
2136         uiHi = *lparam;
2137         if (size)
2138         {
2139             if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size )))
2140                 return FALSE;
2141             if ((ptr = GlobalLock( hMem )))
2142             {
2143                 memcpy( ptr, *buffer, size );
2144                 GlobalUnlock( hMem );
2145             }
2146             else
2147             {
2148                 GlobalFree( hMem );
2149                 return FALSE;
2150             }
2151         }
2152         uiLo = (UINT_PTR)hMem;
2153
2154         *lparam = PackDDElParam( message, uiLo, uiHi );
2155         break;
2156     case WM_DDE_EXECUTE:
2157         if (size)
2158         {
2159             if (!buffer || !*buffer) return FALSE;
2160             if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size ))) return FALSE;
2161             if ((ptr = GlobalLock( hMem )))
2162             {
2163                 memcpy( ptr, *buffer, size );
2164                 GlobalUnlock( hMem );
2165                 TRACE( "exec: pairing c=%08lx s=%p\n", *lparam, hMem );
2166                 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
2167                 {
2168                     GlobalFree( hMem );
2169                     return FALSE;
2170                 }
2171             }
2172             else
2173             {
2174                 GlobalFree( hMem );
2175                 return FALSE;
2176             }
2177         } else return FALSE;
2178         *lparam = (LPARAM)hMem;
2179         break;
2180     }
2181     return TRUE;
2182 }
2183
2184 /***********************************************************************
2185  *           call_window_proc
2186  *
2187  * Call a window procedure and the corresponding hooks.
2188  */
2189 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2190                                  BOOL unicode, BOOL same_thread, enum wm_char_mapping mapping )
2191 {
2192     LRESULT result = 0;
2193     CWPSTRUCT cwp;
2194     CWPRETSTRUCT cwpret;
2195
2196     if (msg & 0x80000000)
2197     {
2198         result = handle_internal_message( hwnd, msg, wparam, lparam );
2199         goto done;
2200     }
2201
2202     /* first the WH_CALLWNDPROC hook */
2203     hwnd = WIN_GetFullHandle( hwnd );
2204     cwp.lParam  = lparam;
2205     cwp.wParam  = wparam;
2206     cwp.message = msg;
2207     cwp.hwnd    = hwnd;
2208     HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
2209
2210     /* now call the window procedure */
2211     if (!WINPROC_call_window( hwnd, msg, wparam, lparam, &result, unicode, mapping )) goto done;
2212
2213     /* and finally the WH_CALLWNDPROCRET hook */
2214     cwpret.lResult = result;
2215     cwpret.lParam  = lparam;
2216     cwpret.wParam  = wparam;
2217     cwpret.message = msg;
2218     cwpret.hwnd    = hwnd;
2219     HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
2220  done:
2221     return result;
2222 }
2223
2224
2225 /***********************************************************************
2226  *           send_parent_notify
2227  *
2228  * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
2229  * the window has the WS_EX_NOPARENTNOTIFY style.
2230  */
2231 static void send_parent_notify( HWND hwnd, WORD event, WORD idChild, POINT pt )
2232 {
2233     /* pt has to be in the client coordinates of the parent window */
2234     MapWindowPoints( 0, hwnd, &pt, 1 );
2235     for (;;)
2236     {
2237         HWND parent;
2238
2239         if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)) break;
2240         if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
2241         if (!(parent = GetParent(hwnd))) break;
2242         if (parent == GetDesktopWindow()) break;
2243         MapWindowPoints( hwnd, parent, &pt, 1 );
2244         hwnd = parent;
2245         SendMessageW( hwnd, WM_PARENTNOTIFY,
2246                       MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
2247     }
2248 }
2249
2250
2251 /***********************************************************************
2252  *          accept_hardware_message
2253  *
2254  * Tell the server we have passed the message to the app
2255  * (even though we may end up dropping it later on)
2256  */
2257 static void accept_hardware_message( UINT hw_id, BOOL remove, HWND new_hwnd )
2258 {
2259     SERVER_START_REQ( accept_hardware_message )
2260     {
2261         req->hw_id   = hw_id;
2262         req->remove  = remove;
2263         req->new_win = wine_server_user_handle( new_hwnd );
2264         if (wine_server_call( req ))
2265             FIXME("Failed to reply to MSG_HARDWARE message. Message may not be removed from queue.\n");
2266     }
2267     SERVER_END_REQ;
2268 }
2269
2270
2271 /***********************************************************************
2272  *          process_keyboard_message
2273  *
2274  * returns TRUE if the contents of 'msg' should be passed to the application
2275  */
2276 static BOOL process_keyboard_message( MSG *msg, UINT hw_id, HWND hwnd_filter,
2277                                       UINT first, UINT last, BOOL remove )
2278 {
2279     EVENTMSG event;
2280
2281     if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN ||
2282         msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP)
2283         switch (msg->wParam)
2284         {
2285             case VK_LSHIFT: case VK_RSHIFT:
2286                 msg->wParam = VK_SHIFT;
2287                 break;
2288             case VK_LCONTROL: case VK_RCONTROL:
2289                 msg->wParam = VK_CONTROL;
2290                 break;
2291             case VK_LMENU: case VK_RMENU:
2292                 msg->wParam = VK_MENU;
2293                 break;
2294         }
2295
2296     /* FIXME: is this really the right place for this hook? */
2297     event.message = msg->message;
2298     event.hwnd    = msg->hwnd;
2299     event.time    = msg->time;
2300     event.paramL  = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
2301     event.paramH  = msg->lParam & 0x7FFF;
2302     if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
2303     HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
2304
2305     /* check message filters */
2306     if (msg->message < first || msg->message > last) return FALSE;
2307     if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
2308
2309     if (remove)
2310     {
2311         if((msg->message == WM_KEYDOWN) &&
2312            (msg->hwnd != GetDesktopWindow()))
2313         {
2314             /* Handle F1 key by sending out WM_HELP message */
2315             if (msg->wParam == VK_F1)
2316             {
2317                 PostMessageW( msg->hwnd, WM_KEYF1, 0, 0 );
2318             }
2319             else if(msg->wParam >= VK_BROWSER_BACK &&
2320                     msg->wParam <= VK_LAUNCH_APP2)
2321             {
2322                 /* FIXME: Process keystate */
2323                 SendMessageW(msg->hwnd, WM_APPCOMMAND, (WPARAM)msg->hwnd, MAKELPARAM(0, (FAPPCOMMAND_KEY | (msg->wParam - VK_BROWSER_BACK + 1))));
2324             }
2325         }
2326         else if (msg->message == WM_KEYUP)
2327         {
2328             /* Handle VK_APPS key by posting a WM_CONTEXTMENU message */
2329             if (msg->wParam == VK_APPS && !MENU_IsMenuActive())
2330                 PostMessageW(msg->hwnd, WM_CONTEXTMENU, (WPARAM)msg->hwnd, -1);
2331         }
2332     }
2333
2334     if (HOOK_CallHooks( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
2335                         LOWORD(msg->wParam), msg->lParam, TRUE ))
2336     {
2337         /* skip this message */
2338         HOOK_CallHooks( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam, TRUE );
2339         accept_hardware_message( hw_id, TRUE, 0 );
2340         return FALSE;
2341     }
2342     accept_hardware_message( hw_id, remove, 0 );
2343
2344     if ( msg->message == WM_KEYDOWN || msg->message == WM_KEYUP )
2345         if ( ImmProcessKey(msg->hwnd, GetKeyboardLayout(0), msg->wParam, msg->lParam, 0) )
2346             msg->wParam = VK_PROCESSKEY;
2347
2348     return TRUE;
2349 }
2350
2351
2352 /***********************************************************************
2353  *          process_mouse_message
2354  *
2355  * returns TRUE if the contents of 'msg' should be passed to the application
2356  */
2357 static BOOL process_mouse_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
2358                                    UINT first, UINT last, BOOL remove )
2359 {
2360     static MSG clk_msg;
2361
2362     POINT pt;
2363     UINT message;
2364     INT hittest;
2365     EVENTMSG event;
2366     GUITHREADINFO info;
2367     MOUSEHOOKSTRUCT hook;
2368     BOOL eatMsg;
2369
2370     /* find the window to dispatch this mouse message to */
2371
2372     info.cbSize = sizeof(info);
2373     GetGUIThreadInfo( GetCurrentThreadId(), &info );
2374     if (info.hwndCapture)
2375     {
2376         hittest = HTCLIENT;
2377         msg->hwnd = info.hwndCapture;
2378     }
2379     else
2380     {
2381         msg->hwnd = WINPOS_WindowFromPoint( msg->hwnd, msg->pt, &hittest );
2382     }
2383
2384     if (!msg->hwnd || !WIN_IsCurrentThread( msg->hwnd ))
2385     {
2386         accept_hardware_message( hw_id, TRUE, msg->hwnd );
2387         return FALSE;
2388     }
2389
2390     /* FIXME: is this really the right place for this hook? */
2391     event.message = msg->message;
2392     event.time    = msg->time;
2393     event.hwnd    = msg->hwnd;
2394     event.paramL  = msg->pt.x;
2395     event.paramH  = msg->pt.y;
2396     HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
2397
2398     if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
2399
2400     pt = msg->pt;
2401     message = msg->message;
2402     /* Note: windows has no concept of a non-client wheel message */
2403     if (message != WM_MOUSEWHEEL)
2404     {
2405         if (hittest != HTCLIENT)
2406         {
2407             message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
2408             msg->wParam = hittest;
2409         }
2410         else
2411         {
2412             /* coordinates don't get translated while tracking a menu */
2413             /* FIXME: should differentiate popups and top-level menus */
2414             if (!(info.flags & GUI_INMENUMODE))
2415                 ScreenToClient( msg->hwnd, &pt );
2416         }
2417     }
2418     msg->lParam = MAKELONG( pt.x, pt.y );
2419
2420     /* translate double clicks */
2421
2422     if ((msg->message == WM_LBUTTONDOWN) ||
2423         (msg->message == WM_RBUTTONDOWN) ||
2424         (msg->message == WM_MBUTTONDOWN) ||
2425         (msg->message == WM_XBUTTONDOWN))
2426     {
2427         BOOL update = remove;
2428
2429         /* translate double clicks -
2430          * note that ...MOUSEMOVEs can slip in between
2431          * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
2432
2433         if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
2434             hittest != HTCLIENT ||
2435             (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
2436         {
2437            if ((msg->message == clk_msg.message) &&
2438                (msg->hwnd == clk_msg.hwnd) &&
2439                (msg->wParam == clk_msg.wParam) &&
2440                (msg->time - clk_msg.time < GetDoubleClickTime()) &&
2441                (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
2442                (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
2443            {
2444                message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
2445                if (update)
2446                {
2447                    clk_msg.message = 0;  /* clear the double click conditions */
2448                    update = FALSE;
2449                }
2450            }
2451         }
2452         if (message < first || message > last) return FALSE;
2453         /* update static double click conditions */
2454         if (update) clk_msg = *msg;
2455     }
2456     else
2457     {
2458         if (message < first || message > last) return FALSE;
2459     }
2460
2461     /* message is accepted now (but may still get dropped) */
2462
2463     hook.pt           = msg->pt;
2464     hook.hwnd         = msg->hwnd;
2465     hook.wHitTestCode = hittest;
2466     hook.dwExtraInfo  = extra_info;
2467     if (HOOK_CallHooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
2468                         message, (LPARAM)&hook, TRUE ))
2469     {
2470         hook.pt           = msg->pt;
2471         hook.hwnd         = msg->hwnd;
2472         hook.wHitTestCode = hittest;
2473         hook.dwExtraInfo  = extra_info;
2474         HOOK_CallHooks( WH_CBT, HCBT_CLICKSKIPPED, message, (LPARAM)&hook, TRUE );
2475         accept_hardware_message( hw_id, TRUE, 0 );
2476         return FALSE;
2477     }
2478
2479     if ((hittest == HTERROR) || (hittest == HTNOWHERE))
2480     {
2481         SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
2482                       MAKELONG( hittest, msg->message ));
2483         accept_hardware_message( hw_id, TRUE, 0 );
2484         return FALSE;
2485     }
2486
2487     accept_hardware_message( hw_id, remove, 0 );
2488
2489     if (!remove || info.hwndCapture)
2490     {
2491         msg->message = message;
2492         return TRUE;
2493     }
2494
2495     eatMsg = FALSE;
2496
2497     if ((msg->message == WM_LBUTTONDOWN) ||
2498         (msg->message == WM_RBUTTONDOWN) ||
2499         (msg->message == WM_MBUTTONDOWN) ||
2500         (msg->message == WM_XBUTTONDOWN))
2501     {
2502         /* Send the WM_PARENTNOTIFY,
2503          * note that even for double/nonclient clicks
2504          * notification message is still WM_L/M/RBUTTONDOWN.
2505          */
2506         send_parent_notify( msg->hwnd, msg->message, 0, msg->pt );
2507
2508         /* Activate the window if needed */
2509
2510         if (msg->hwnd != info.hwndActive)
2511         {
2512             HWND hwndTop = msg->hwnd;
2513             while (hwndTop)
2514             {
2515                 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
2516                 hwndTop = GetParent( hwndTop );
2517             }
2518
2519             if (hwndTop && hwndTop != GetDesktopWindow())
2520             {
2521                 LONG ret = SendMessageW( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
2522                                          MAKELONG( hittest, msg->message ) );
2523                 switch(ret)
2524                 {
2525                 case MA_NOACTIVATEANDEAT:
2526                     eatMsg = TRUE;
2527                     /* fall through */
2528                 case MA_NOACTIVATE:
2529                     break;
2530                 case MA_ACTIVATEANDEAT:
2531                     eatMsg = TRUE;
2532                     /* fall through */
2533                 case MA_ACTIVATE:
2534                 case 0:
2535                     if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
2536                     break;
2537                 default:
2538                     WARN( "unknown WM_MOUSEACTIVATE code %d\n", ret );
2539                     break;
2540                 }
2541             }
2542         }
2543     }
2544
2545     /* send the WM_SETCURSOR message */
2546
2547     /* Windows sends the normal mouse message as the message parameter
2548        in the WM_SETCURSOR message even if it's non-client mouse message */
2549     SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd, MAKELONG( hittest, msg->message ));
2550
2551     msg->message = message;
2552     return !eatMsg;
2553 }
2554
2555
2556 /***********************************************************************
2557  *           process_hardware_message
2558  *
2559  * Process a hardware message; return TRUE if message should be passed on to the app
2560  */
2561 static BOOL process_hardware_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
2562                                       UINT first, UINT last, BOOL remove )
2563 {
2564     if (is_keyboard_message( msg->message ))
2565         return process_keyboard_message( msg, hw_id, hwnd_filter, first, last, remove );
2566
2567     if (is_mouse_message( msg->message ))
2568         return process_mouse_message( msg, hw_id, extra_info, hwnd_filter, first, last, remove );
2569
2570     ERR( "unknown message type %x\n", msg->message );
2571     return FALSE;
2572 }
2573
2574
2575 /***********************************************************************
2576  *           call_sendmsg_callback
2577  *
2578  * Call the callback function of SendMessageCallback.
2579  */
2580 static inline void call_sendmsg_callback( SENDASYNCPROC callback, HWND hwnd, UINT msg,
2581                                           ULONG_PTR data, LRESULT result )
2582 {
2583     if (!callback) return;
2584
2585     if (TRACE_ON(relay))
2586         DPRINTF( "%04x:Call message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
2587                  GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
2588                  data, result );
2589     callback( hwnd, msg, data, result );
2590     if (TRACE_ON(relay))
2591         DPRINTF( "%04x:Ret  message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
2592                  GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
2593                  data, result );
2594 }
2595
2596
2597 /***********************************************************************
2598  *           peek_message
2599  *
2600  * Peek for a message matching the given parameters. Return FALSE if none available.
2601  * All pending sent messages are processed before returning.
2602  */
2603 static BOOL peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags, UINT changed_mask )
2604 {
2605     LRESULT result;
2606     struct user_thread_info *thread_info = get_user_thread_info();
2607     struct received_message_info info, *old_info;
2608     unsigned int hw_id = 0;  /* id of previous hardware message */
2609     void *buffer;
2610     size_t buffer_size = 256;
2611
2612     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size ))) return FALSE;
2613
2614     if (!first && !last) last = ~0;
2615     if (hwnd == HWND_BROADCAST) hwnd = HWND_TOPMOST;
2616
2617     for (;;)
2618     {
2619         NTSTATUS res;
2620         size_t size = 0;
2621         const message_data_t *msg_data = buffer;
2622
2623         SERVER_START_REQ( get_message )
2624         {
2625             req->flags     = flags;
2626             req->get_win   = wine_server_user_handle( hwnd );
2627             req->get_first = first;
2628             req->get_last  = last;
2629             req->hw_id     = hw_id;
2630             req->wake_mask = changed_mask & (QS_SENDMESSAGE | QS_SMRESULT);
2631             req->changed_mask = changed_mask;
2632             wine_server_set_reply( req, buffer, buffer_size );
2633             if (!(res = wine_server_call( req )))
2634             {
2635                 size = wine_server_reply_size( reply );
2636                 info.type        = reply->type;
2637                 info.msg.hwnd    = wine_server_ptr_handle( reply->win );
2638                 info.msg.message = reply->msg;
2639                 info.msg.wParam  = reply->wparam;
2640                 info.msg.lParam  = reply->lparam;
2641                 info.msg.time    = reply->time;
2642                 info.msg.pt.x    = 0;
2643                 info.msg.pt.y    = 0;
2644                 hw_id            = 0;
2645                 thread_info->active_hooks = reply->active_hooks;
2646             }
2647             else buffer_size = reply->total;
2648         }
2649         SERVER_END_REQ;
2650
2651         if (res)
2652         {
2653             HeapFree( GetProcessHeap(), 0, buffer );
2654             if (res != STATUS_BUFFER_OVERFLOW) return FALSE;
2655             if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size ))) return FALSE;
2656             continue;
2657         }
2658
2659         TRACE( "got type %d msg %x (%s) hwnd %p wp %lx lp %lx\n",
2660                info.type, info.msg.message,
2661                (info.type == MSG_WINEVENT) ? "MSG_WINEVENT" : SPY_GetMsgName(info.msg.message, info.msg.hwnd),
2662                info.msg.hwnd, info.msg.wParam, info.msg.lParam );
2663
2664         switch(info.type)
2665         {
2666         case MSG_ASCII:
2667         case MSG_UNICODE:
2668             info.flags = ISMEX_SEND;
2669             break;
2670         case MSG_NOTIFY:
2671             info.flags = ISMEX_NOTIFY;
2672             break;
2673         case MSG_CALLBACK:
2674             info.flags = ISMEX_CALLBACK;
2675             break;
2676         case MSG_CALLBACK_RESULT:
2677             if (size >= sizeof(msg_data->callback))
2678                 call_sendmsg_callback( wine_server_get_ptr(msg_data->callback.callback),
2679                                        info.msg.hwnd, info.msg.message,
2680                                        msg_data->callback.data, msg_data->callback.result );
2681             continue;
2682         case MSG_WINEVENT:
2683             if (size >= sizeof(msg_data->winevent))
2684             {
2685                 WINEVENTPROC hook_proc;
2686
2687                 hook_proc = wine_server_get_ptr( msg_data->winevent.hook_proc );
2688                 size -= sizeof(msg_data->winevent);
2689                 if (size)
2690                 {
2691                     WCHAR module[MAX_PATH];
2692
2693                     size = min( size, (MAX_PATH - 1) * sizeof(WCHAR) );
2694                     memcpy( module, &msg_data->winevent + 1, size );
2695                     module[size / sizeof(WCHAR)] = 0;
2696                     if (!(hook_proc = get_hook_proc( hook_proc, module )))
2697                     {
2698                         ERR( "invalid winevent hook module name %s\n", debugstr_w(module) );
2699                         continue;
2700                     }
2701                 }
2702
2703                 if (TRACE_ON(relay))
2704                     DPRINTF( "%04x:Call winevent proc %p (hook=%04x,event=%x,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04x,time=%x)\n",
2705                              GetCurrentThreadId(), hook_proc,
2706                              msg_data->winevent.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2707                              info.msg.lParam, msg_data->winevent.tid, info.msg.time);
2708
2709                 hook_proc( wine_server_ptr_handle( msg_data->winevent.hook ), info.msg.message,
2710                            info.msg.hwnd, info.msg.wParam, info.msg.lParam,
2711                            msg_data->winevent.tid, info.msg.time );
2712
2713                 if (TRACE_ON(relay))
2714                     DPRINTF( "%04x:Ret  winevent proc %p (hook=%04x,event=%x,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04x,time=%x)\n",
2715                              GetCurrentThreadId(), hook_proc,
2716                              msg_data->winevent.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2717                              info.msg.lParam, msg_data->winevent.tid, info.msg.time);
2718             }
2719             continue;
2720         case MSG_HOOK_LL:
2721             info.flags = ISMEX_SEND;
2722             result = 0;
2723             if (info.msg.message == WH_KEYBOARD_LL && size >= sizeof(msg_data->hardware))
2724             {
2725                 KBDLLHOOKSTRUCT hook;
2726
2727                 hook.vkCode      = LOWORD( info.msg.lParam );
2728                 hook.scanCode    = HIWORD( info.msg.lParam );
2729                 hook.flags       = msg_data->hardware.flags;
2730                 hook.time        = info.msg.time;
2731                 hook.dwExtraInfo = msg_data->hardware.info;
2732                 TRACE( "calling keyboard LL hook vk %x scan %x flags %x time %u info %lx\n",
2733                        hook.vkCode, hook.scanCode, hook.flags, hook.time, hook.dwExtraInfo );
2734                 result = HOOK_CallHooks( WH_KEYBOARD_LL, HC_ACTION, info.msg.wParam, (LPARAM)&hook, TRUE );
2735             }
2736             else if (info.msg.message == WH_MOUSE_LL && size >= sizeof(msg_data->hardware))
2737             {
2738                 MSLLHOOKSTRUCT hook;
2739
2740                 hook.pt.x        = msg_data->hardware.x;
2741                 hook.pt.y        = msg_data->hardware.y;
2742                 hook.mouseData   = info.msg.lParam;
2743                 hook.flags       = msg_data->hardware.flags;
2744                 hook.time        = info.msg.time;
2745                 hook.dwExtraInfo = msg_data->hardware.info;
2746                 TRACE( "calling mouse LL hook pos %d,%d data %x flags %x time %u info %lx\n",
2747                        hook.pt.x, hook.pt.y, hook.mouseData, hook.flags, hook.time, hook.dwExtraInfo );
2748                 result = HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, info.msg.wParam, (LPARAM)&hook, TRUE );
2749             }
2750             reply_message( &info, result, TRUE );
2751             continue;
2752         case MSG_OTHER_PROCESS:
2753             info.flags = ISMEX_SEND;
2754             if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2755                                  &info.msg.lParam, &buffer, size ))
2756             {
2757                 /* ignore it */
2758                 reply_message( &info, 0, TRUE );
2759                 continue;
2760             }
2761             break;
2762         case MSG_HARDWARE:
2763             if (size >= sizeof(msg_data->hardware))
2764             {
2765                 info.msg.pt.x = msg_data->hardware.x;
2766                 info.msg.pt.y = msg_data->hardware.y;
2767                 hw_id         = msg_data->hardware.hw_id;
2768                 if (!process_hardware_message( &info.msg, hw_id, msg_data->hardware.info,
2769                                                hwnd, first, last, flags & PM_REMOVE ))
2770                 {
2771                     TRACE("dropping msg %x\n", info.msg.message );
2772                     continue;  /* ignore it */
2773                 }
2774                 *msg = info.msg;
2775                 thread_info->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
2776                 thread_info->GetMessageTimeVal = info.msg.time;
2777                 thread_info->GetMessageExtraInfoVal = msg_data->hardware.info;
2778                 HeapFree( GetProcessHeap(), 0, buffer );
2779                 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)msg, TRUE );
2780                 return TRUE;
2781             }
2782             continue;
2783         case MSG_POSTED:
2784             if (info.msg.message & 0x80000000)  /* internal message */
2785             {
2786                 if (flags & PM_REMOVE)
2787                 {
2788                     handle_internal_message( info.msg.hwnd, info.msg.message,
2789                                              info.msg.wParam, info.msg.lParam );
2790                     /* if this is a nested call return right away */
2791                     if (first == info.msg.message && last == info.msg.message) return FALSE;
2792                 }
2793                 else
2794                     peek_message( msg, info.msg.hwnd, info.msg.message,
2795                                   info.msg.message, flags | PM_REMOVE, changed_mask );
2796                 continue;
2797             }
2798             if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
2799             {
2800                 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2801                                          &info.msg.lParam, &buffer, size ))
2802                     continue;  /* ignore it */
2803             }
2804             *msg = info.msg;
2805             msg->pt.x = (short)LOWORD( thread_info->GetMessagePosVal );
2806             msg->pt.y = (short)HIWORD( thread_info->GetMessagePosVal );
2807             thread_info->GetMessageTimeVal = info.msg.time;
2808             thread_info->GetMessageExtraInfoVal = 0;
2809             HeapFree( GetProcessHeap(), 0, buffer );
2810             HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)msg, TRUE );
2811             return TRUE;
2812         }
2813
2814         /* if we get here, we have a sent message; call the window procedure */
2815         old_info = thread_info->receive_info;
2816         thread_info->receive_info = &info;
2817         result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
2818                                    info.msg.lParam, (info.type != MSG_ASCII), FALSE,
2819                                    WMCHAR_MAP_RECVMESSAGE );
2820         reply_message( &info, result, TRUE );
2821         thread_info->receive_info = old_info;
2822
2823         /* if some PM_QS* flags were specified, only handle sent messages from now on */
2824         if (HIWORD(flags) && !changed_mask) flags = PM_QS_SENDMESSAGE | LOWORD(flags);
2825     }
2826 }
2827
2828
2829 /***********************************************************************
2830  *           process_sent_messages
2831  *
2832  * Process all pending sent messages.
2833  */
2834 static inline void process_sent_messages(void)
2835 {
2836     MSG msg;
2837     peek_message( &msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE, 0 );
2838 }
2839
2840
2841 /***********************************************************************
2842  *           get_server_queue_handle
2843  *
2844  * Get a handle to the server message queue for the current thread.
2845  */
2846 static HANDLE get_server_queue_handle(void)
2847 {
2848     struct user_thread_info *thread_info = get_user_thread_info();
2849     HANDLE ret;
2850
2851     if (!(ret = thread_info->server_queue))
2852     {
2853         SERVER_START_REQ( get_msg_queue )
2854         {
2855             wine_server_call( req );
2856             ret = wine_server_ptr_handle( reply->handle );
2857         }
2858         SERVER_END_REQ;
2859         thread_info->server_queue = ret;
2860         if (!ret) ERR( "Cannot get server thread queue\n" );
2861     }
2862     return ret;
2863 }
2864
2865
2866 /***********************************************************************
2867  *           wait_message_reply
2868  *
2869  * Wait until a sent message gets replied to.
2870  */
2871 static void wait_message_reply( UINT flags )
2872 {
2873     HANDLE server_queue = get_server_queue_handle();
2874
2875     for (;;)
2876     {
2877         unsigned int wake_bits = 0;
2878
2879         SERVER_START_REQ( set_queue_mask )
2880         {
2881             req->wake_mask    = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
2882             req->changed_mask = req->wake_mask;
2883             req->skip_wait    = 1;
2884             if (!wine_server_call( req ))
2885                 wake_bits = reply->wake_bits;
2886         }
2887         SERVER_END_REQ;
2888
2889         if (wake_bits & QS_SMRESULT) return;  /* got a result */
2890         if (wake_bits & QS_SENDMESSAGE)
2891         {
2892             /* Process the sent message immediately */
2893             process_sent_messages();
2894             continue;
2895         }
2896
2897         wow_handlers.wait_message( 1, &server_queue, INFINITE, QS_SENDMESSAGE, 0 );
2898     }
2899 }
2900
2901 /***********************************************************************
2902  *              put_message_in_queue
2903  *
2904  * Put a sent message into the destination queue.
2905  * For inter-process message, reply_size is set to expected size of reply data.
2906  */
2907 static BOOL put_message_in_queue( const struct send_message_info *info, size_t *reply_size )
2908 {
2909     struct packed_message data;
2910     message_data_t msg_data;
2911     unsigned int res;
2912     int i;
2913     timeout_t timeout = TIMEOUT_INFINITE;
2914
2915     /* Check for INFINITE timeout for compatibility with Win9x,
2916      * although Windows >= NT does not do so
2917      */
2918     if (info->type != MSG_NOTIFY &&
2919         info->type != MSG_CALLBACK &&
2920         info->type != MSG_POSTED &&
2921         info->timeout &&
2922         info->timeout != INFINITE)
2923     {
2924         /* timeout is signed despite the prototype */
2925         timeout = (timeout_t)max( 0, (int)info->timeout ) * -10000;
2926     }
2927
2928     memset( &data, 0, sizeof(data) );
2929     if (info->type == MSG_OTHER_PROCESS)
2930     {
2931         *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
2932         if (data.count == -1)
2933         {
2934             WARN( "cannot pack message %x\n", info->msg );
2935             return FALSE;
2936         }
2937     }
2938     else if (info->type == MSG_CALLBACK)
2939     {
2940         msg_data.callback.callback = wine_server_client_ptr( info->callback );
2941         msg_data.callback.data     = info->data;
2942         msg_data.callback.result   = 0;
2943         data.data[0] = &msg_data;
2944         data.size[0] = sizeof(msg_data.callback);
2945         data.count = 1;
2946     }
2947     else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
2948     {
2949         return post_dde_message( &data, info );
2950     }
2951
2952     SERVER_START_REQ( send_message )
2953     {
2954         req->id      = info->dest_tid;
2955         req->type    = info->type;
2956         req->flags   = 0;
2957         req->win     = wine_server_user_handle( info->hwnd );
2958         req->msg     = info->msg;
2959         req->wparam  = info->wparam;
2960         req->lparam  = info->lparam;
2961         req->timeout = timeout;
2962
2963         if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
2964         for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
2965         if ((res = wine_server_call( req )))
2966         {
2967             if (res == STATUS_INVALID_PARAMETER)
2968                 /* FIXME: find a STATUS_ value for this one */
2969                 SetLastError( ERROR_INVALID_THREAD_ID );
2970             else
2971                 SetLastError( RtlNtStatusToDosError(res) );
2972         }
2973     }
2974     SERVER_END_REQ;
2975     return !res;
2976 }
2977
2978
2979 /***********************************************************************
2980  *              retrieve_reply
2981  *
2982  * Retrieve a message reply from the server.
2983  */
2984 static LRESULT retrieve_reply( const struct send_message_info *info,
2985                                size_t reply_size, LRESULT *result )
2986 {
2987     NTSTATUS status;
2988     void *reply_data = NULL;
2989
2990     if (reply_size)
2991     {
2992         if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
2993         {
2994             WARN( "no memory for reply, will be truncated\n" );
2995             reply_size = 0;
2996         }
2997     }
2998     SERVER_START_REQ( get_message_reply )
2999     {
3000         req->cancel = 1;
3001         if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
3002         if (!(status = wine_server_call( req ))) *result = reply->result;
3003         reply_size = wine_server_reply_size( reply );
3004     }
3005     SERVER_END_REQ;
3006     if (!status && reply_size)
3007         unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
3008
3009     HeapFree( GetProcessHeap(), 0, reply_data );
3010
3011     TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx got reply %lx (err=%d)\n",
3012            info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
3013            info->lparam, *result, status );
3014
3015     /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
3016     if (status) SetLastError( RtlNtStatusToDosError(status) );
3017     return !status;
3018 }
3019
3020
3021 /***********************************************************************
3022  *              send_inter_thread_message
3023  */
3024 static LRESULT send_inter_thread_message( const struct send_message_info *info, LRESULT *res_ptr )
3025 {
3026     size_t reply_size = 0;
3027
3028     TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
3029            info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
3030
3031     USER_CheckNotLock();
3032
3033     if (!put_message_in_queue( info, &reply_size )) return 0;
3034
3035     /* there's no reply to wait for on notify/callback messages */
3036     if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
3037
3038     wait_message_reply( info->flags );
3039     return retrieve_reply( info, reply_size, res_ptr );
3040 }
3041
3042
3043 /***********************************************************************
3044  *              send_inter_thread_callback
3045  */
3046 static LRESULT send_inter_thread_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
3047                                            LRESULT *result, void *arg )
3048 {
3049     struct send_message_info *info = arg;
3050     info->hwnd   = hwnd;
3051     info->msg    = msg;
3052     info->wparam = wp;
3053     info->lparam = lp;
3054     return send_inter_thread_message( info, result );
3055 }
3056
3057
3058 /***********************************************************************
3059  *              send_message
3060  *
3061  * Backend implementation of the various SendMessage functions.
3062  */
3063 static BOOL send_message( struct send_message_info *info, DWORD_PTR *res_ptr, BOOL unicode )
3064 {
3065     DWORD dest_pid;
3066     BOOL ret;
3067     LRESULT result;
3068
3069     if (is_broadcast(info->hwnd))
3070     {
3071         EnumWindows( broadcast_message_callback, (LPARAM)info );
3072         if (res_ptr) *res_ptr = 1;
3073         return TRUE;
3074     }
3075
3076     if (!(info->dest_tid = GetWindowThreadProcessId( info->hwnd, &dest_pid ))) return FALSE;
3077
3078     if (USER_IsExitingThread( info->dest_tid )) return FALSE;
3079
3080     SPY_EnterMessage( SPY_SENDMESSAGE, info->hwnd, info->msg, info->wparam, info->lparam );
3081
3082     if (info->dest_tid == GetCurrentThreadId())
3083     {
3084         result = call_window_proc( info->hwnd, info->msg, info->wparam, info->lparam,
3085                                    unicode, TRUE, info->wm_char );
3086         if (info->type == MSG_CALLBACK)
3087             call_sendmsg_callback( info->callback, info->hwnd, info->msg, info->data, result );
3088         ret = TRUE;
3089     }
3090     else
3091     {
3092         if (dest_pid != GetCurrentProcessId() && (info->type == MSG_ASCII || info->type == MSG_UNICODE))
3093             info->type = MSG_OTHER_PROCESS;
3094
3095         /* MSG_ASCII can be sent unconverted except for WM_CHAR; everything else needs to be Unicode */
3096         if (!unicode && is_unicode_message( info->msg ) &&
3097             (info->type != MSG_ASCII || info->msg == WM_CHAR))
3098             ret = WINPROC_CallProcAtoW( send_inter_thread_callback, info->hwnd, info->msg,
3099                                         info->wparam, info->lparam, &result, info, info->wm_char );
3100         else
3101             ret = send_inter_thread_message( info, &result );
3102     }
3103
3104     SPY_ExitMessage( SPY_RESULT_OK, info->hwnd, info->msg, result, info->wparam, info->lparam );
3105     if (ret && res_ptr) *res_ptr = result;
3106     return ret;
3107 }
3108
3109
3110 /***********************************************************************
3111  *              send_hardware_message
3112  */
3113 NTSTATUS send_hardware_message( HWND hwnd, const INPUT *input, UINT flags )
3114 {
3115     struct user_thread_info *thread_info = get_user_thread_info();
3116     struct send_message_info info;
3117     int prev_x, prev_y, new_x, new_y;
3118     NTSTATUS ret;
3119     BOOL wait;
3120
3121     info.type     = MSG_HARDWARE;
3122     info.dest_tid = 0;
3123     info.hwnd     = hwnd;
3124     info.flags    = 0;
3125     info.timeout  = 0;
3126
3127     SERVER_START_REQ( send_hardware_message )
3128     {
3129         req->win        = wine_server_user_handle( hwnd );
3130         req->flags      = flags;
3131         req->input.type = input->type;
3132         switch (input->type)
3133         {
3134         case INPUT_MOUSE:
3135             req->input.mouse.x     = input->u.mi.dx;
3136             req->input.mouse.y     = input->u.mi.dy;
3137             req->input.mouse.data  = input->u.mi.mouseData;
3138             req->input.mouse.flags = input->u.mi.dwFlags;
3139             req->input.mouse.time  = input->u.mi.time;
3140             req->input.mouse.info  = input->u.mi.dwExtraInfo;
3141             break;
3142         case INPUT_KEYBOARD:
3143             req->input.kbd.vkey  = input->u.ki.wVk;
3144             req->input.kbd.scan  = input->u.ki.wScan;
3145             req->input.kbd.flags = input->u.ki.dwFlags;
3146             req->input.kbd.time  = input->u.ki.time;
3147             req->input.kbd.info  = input->u.ki.dwExtraInfo;
3148             break;
3149         case INPUT_HARDWARE:
3150             req->input.hw.msg    = input->u.hi.uMsg;
3151             req->input.hw.lparam = MAKELONG( input->u.hi.wParamL, input->u.hi.wParamH );
3152             break;
3153         }
3154         if (thread_info->key_state) wine_server_set_reply( req, thread_info->key_state, 256 );
3155         ret = wine_server_call( req );
3156         wait = reply->wait;
3157         prev_x = reply->prev_x;
3158         prev_y = reply->prev_y;
3159         new_x  = reply->new_x;
3160         new_y  = reply->new_y;
3161     }
3162     SERVER_END_REQ;
3163
3164     if (!ret)
3165     {
3166         if (thread_info->key_state) thread_info->key_state_time = GetTickCount();
3167         if ((flags & SEND_HWMSG_INJECTED) && (prev_x != new_x || prev_y != new_y))
3168             USER_Driver->pSetCursorPos( new_x, new_y );
3169     }
3170
3171     if (wait)
3172     {
3173         LRESULT ignored;
3174         wait_message_reply( 0 );
3175         retrieve_reply( &info, 0, &ignored );
3176     }
3177     return ret;
3178 }
3179
3180
3181 /***********************************************************************
3182  *              MSG_SendInternalMessageTimeout
3183  *
3184  * Same as SendMessageTimeoutW but sends the message to a specific thread
3185  * without requiring a window handle. Only works for internal Wine messages.
3186  */
3187 LRESULT MSG_SendInternalMessageTimeout( DWORD dest_pid, DWORD dest_tid,
3188                                         UINT msg, WPARAM wparam, LPARAM lparam,
3189                                         UINT flags, UINT timeout, PDWORD_PTR res_ptr )
3190 {
3191     struct send_message_info info;
3192     LRESULT ret, result;
3193
3194     assert( msg & 0x80000000 );  /* must be an internal Wine message */
3195
3196     info.type     = MSG_UNICODE;
3197     info.dest_tid = dest_tid;
3198     info.hwnd     = 0;
3199     info.msg      = msg;
3200     info.wparam   = wparam;
3201     info.lparam   = lparam;
3202     info.flags    = flags;
3203     info.timeout  = timeout;
3204
3205     if (USER_IsExitingThread( dest_tid )) return 0;
3206
3207     if (dest_tid == GetCurrentThreadId())
3208     {
3209         result = handle_internal_message( 0, msg, wparam, lparam );
3210         ret = 1;
3211     }
3212     else
3213     {
3214         if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
3215         ret = send_inter_thread_message( &info, &result );
3216     }
3217     if (ret && res_ptr) *res_ptr = result;
3218     return ret;
3219 }
3220
3221
3222 /***********************************************************************
3223  *              SendMessageTimeoutW  (USER32.@)
3224  */
3225 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
3226                                     UINT flags, UINT timeout, PDWORD_PTR res_ptr )
3227 {
3228     struct send_message_info info;
3229
3230     info.type    = MSG_UNICODE;
3231     info.hwnd    = hwnd;
3232     info.msg     = msg;
3233     info.wparam  = wparam;
3234     info.lparam  = lparam;
3235     info.flags   = flags;
3236     info.timeout = timeout;
3237
3238     return send_message( &info, res_ptr, TRUE );
3239 }
3240
3241 /***********************************************************************
3242  *              SendMessageTimeoutA  (USER32.@)
3243  */
3244 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
3245                                     UINT flags, UINT timeout, PDWORD_PTR res_ptr )
3246 {
3247     struct send_message_info info;
3248
3249     info.type    = MSG_ASCII;
3250     info.hwnd    = hwnd;
3251     info.msg     = msg;
3252     info.wparam  = wparam;
3253     info.lparam  = lparam;
3254     info.flags   = flags;
3255     info.timeout = timeout;
3256     info.wm_char  = WMCHAR_MAP_SENDMESSAGETIMEOUT;
3257
3258     return send_message( &info, res_ptr, FALSE );
3259 }
3260
3261
3262 /***********************************************************************
3263  *              SendMessageW  (USER32.@)
3264  */
3265 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
3266 {
3267     DWORD_PTR res = 0;
3268     struct send_message_info info;
3269
3270     info.type    = MSG_UNICODE;
3271     info.hwnd    = hwnd;
3272     info.msg     = msg;
3273     info.wparam  = wparam;
3274     info.lparam  = lparam;
3275     info.flags   = SMTO_NORMAL;
3276     info.timeout = 0;
3277
3278     send_message( &info, &res, TRUE );
3279     return res;
3280 }
3281
3282
3283 /***********************************************************************
3284  *              SendMessageA  (USER32.@)
3285  */
3286 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
3287 {
3288     DWORD_PTR res = 0;
3289     struct send_message_info info;
3290
3291     info.type    = MSG_ASCII;
3292     info.hwnd    = hwnd;
3293     info.msg     = msg;
3294     info.wparam  = wparam;
3295     info.lparam  = lparam;
3296     info.flags   = SMTO_NORMAL;
3297     info.timeout = 0;
3298     info.wm_char  = WMCHAR_MAP_SENDMESSAGE;
3299
3300     send_message( &info, &res, FALSE );
3301     return res;
3302 }
3303
3304
3305 /***********************************************************************
3306  *              SendNotifyMessageA  (USER32.@)
3307  */
3308 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
3309 {
3310     struct send_message_info info;
3311
3312     if (is_pointer_message(msg))
3313     {
3314         SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3315         return FALSE;
3316     }
3317
3318     info.type    = MSG_NOTIFY;
3319     info.hwnd    = hwnd;
3320     info.msg     = msg;
3321     info.wparam  = wparam;
3322     info.lparam  = lparam;
3323     info.flags   = 0;
3324     info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
3325
3326     return send_message( &info, NULL, FALSE );
3327 }
3328
3329
3330 /***********************************************************************
3331  *              SendNotifyMessageW  (USER32.@)
3332  */
3333 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
3334 {
3335     struct send_message_info info;
3336
3337     if (is_pointer_message(msg))
3338     {
3339         SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3340         return FALSE;
3341     }
3342
3343     info.type    = MSG_NOTIFY;
3344     info.hwnd    = hwnd;
3345     info.msg     = msg;
3346     info.wparam  = wparam;
3347     info.lparam  = lparam;
3348     info.flags   = 0;
3349
3350     return send_message( &info, NULL, TRUE );
3351 }
3352
3353
3354 /***********************************************************************
3355  *              SendMessageCallbackA  (USER32.@)
3356  */
3357 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
3358                                   SENDASYNCPROC callback, ULONG_PTR data )
3359 {
3360     struct send_message_info info;
3361
3362     if (is_pointer_message(msg))
3363     {
3364         SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3365         return FALSE;
3366     }
3367
3368     info.type     = MSG_CALLBACK;
3369     info.hwnd     = hwnd;
3370     info.msg      = msg;
3371     info.wparam   = wparam;
3372     info.lparam   = lparam;
3373     info.callback = callback;
3374     info.data     = data;
3375     info.flags    = 0;
3376     info.wm_char  = WMCHAR_MAP_SENDMESSAGETIMEOUT;
3377
3378     return send_message( &info, NULL, FALSE );
3379 }
3380
3381
3382 /***********************************************************************
3383  *              SendMessageCallbackW  (USER32.@)
3384  */
3385 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
3386                                   SENDASYNCPROC callback, ULONG_PTR data )
3387 {
3388     struct send_message_info info;
3389
3390     if (is_pointer_message(msg))
3391     {
3392         SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3393         return FALSE;
3394     }
3395
3396     info.type     = MSG_CALLBACK;
3397     info.hwnd     = hwnd;
3398     info.msg      = msg;
3399     info.wparam   = wparam;
3400     info.lparam   = lparam;
3401     info.callback = callback;
3402     info.data     = data;
3403     info.flags    = 0;
3404
3405     return send_message( &info, NULL, TRUE );
3406 }
3407
3408
3409 /***********************************************************************
3410  *              ReplyMessage  (USER32.@)
3411  */
3412 BOOL WINAPI ReplyMessage( LRESULT result )
3413 {
3414     struct received_message_info *info = get_user_thread_info()->receive_info;
3415
3416     if (!info) return FALSE;
3417     reply_message( info, result, FALSE );
3418     return TRUE;
3419 }
3420
3421
3422 /***********************************************************************
3423  *              InSendMessage  (USER32.@)
3424  */
3425 BOOL WINAPI InSendMessage(void)
3426 {
3427     return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
3428 }
3429
3430
3431 /***********************************************************************
3432  *              InSendMessageEx  (USER32.@)
3433  */
3434 DWORD WINAPI InSendMessageEx( LPVOID reserved )
3435 {
3436     struct received_message_info *info = get_user_thread_info()->receive_info;
3437
3438     if (info) return info->flags;
3439     return ISMEX_NOSEND;
3440 }
3441
3442
3443 /***********************************************************************
3444  *              PostMessageA  (USER32.@)
3445  */
3446 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
3447 {
3448     if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
3449     return PostMessageW( hwnd, msg, wparam, lparam );
3450 }
3451
3452
3453 /***********************************************************************
3454  *              PostMessageW  (USER32.@)
3455  */
3456 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
3457 {
3458     struct send_message_info info;
3459
3460     if (is_pointer_message( msg ))
3461     {
3462         SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3463         return FALSE;
3464     }
3465
3466     TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
3467            hwnd, msg, SPY_GetMsgName(msg, hwnd), wparam, lparam );
3468
3469     info.type   = MSG_POSTED;
3470     info.hwnd   = hwnd;
3471     info.msg    = msg;
3472     info.wparam = wparam;
3473     info.lparam = lparam;
3474     info.flags  = 0;
3475
3476     if (is_broadcast(hwnd))
3477     {
3478         EnumWindows( broadcast_message_callback, (LPARAM)&info );
3479         return TRUE;
3480     }
3481
3482     if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
3483
3484     if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
3485
3486     if (USER_IsExitingThread( info.dest_tid )) return TRUE;
3487
3488     return put_message_in_queue( &info, NULL );
3489 }
3490
3491
3492 /**********************************************************************
3493  *              PostThreadMessageA  (USER32.@)
3494  */
3495 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
3496 {
3497     if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
3498     return PostThreadMessageW( thread, msg, wparam, lparam );
3499 }
3500
3501
3502 /**********************************************************************
3503  *              PostThreadMessageW  (USER32.@)
3504  */
3505 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
3506 {
3507     struct send_message_info info;
3508
3509     if (is_pointer_message( msg ))
3510     {
3511         SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3512         return FALSE;
3513     }
3514     if (USER_IsExitingThread( thread )) return TRUE;
3515
3516     info.type     = MSG_POSTED;
3517     info.dest_tid = thread;
3518     info.hwnd     = 0;
3519     info.msg      = msg;
3520     info.wparam   = wparam;
3521     info.lparam   = lparam;
3522     info.flags    = 0;
3523     return put_message_in_queue( &info, NULL );
3524 }
3525
3526
3527 /***********************************************************************
3528  *              PostQuitMessage  (USER32.@)
3529  *
3530  * Posts a quit message to the current thread's message queue.
3531  *
3532  * PARAMS
3533  *  exit_code [I] Exit code to return from message loop.
3534  *
3535  * RETURNS
3536  *  Nothing.
3537  *
3538  * NOTES
3539  *  This function is not the same as calling:
3540  *|PostThreadMessage(GetCurrentThreadId(), WM_QUIT, exit_code, 0);
3541  *  It instead sets a flag in the message queue that signals it to generate
3542  *  a WM_QUIT message when there are no other pending sent or posted messages
3543  *  in the queue.
3544  */
3545 void WINAPI PostQuitMessage( INT exit_code )
3546 {
3547     SERVER_START_REQ( post_quit_message )
3548     {
3549         req->exit_code = exit_code;
3550         wine_server_call( req );
3551     }
3552     SERVER_END_REQ;
3553 }
3554
3555
3556 /***********************************************************************
3557  *              PeekMessageW  (USER32.@)
3558  */
3559 BOOL WINAPI DECLSPEC_HOTPATCH PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
3560 {
3561     MSG msg;
3562
3563     USER_CheckNotLock();
3564
3565     /* check for graphics events */
3566     USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
3567
3568     if (!peek_message( &msg, hwnd, first, last, flags, 0 ))
3569     {
3570         if (!(flags & PM_NOYIELD)) wow_handlers.wait_message( 0, NULL, 0, 0, 0 );
3571         return FALSE;
3572     }
3573
3574     /* copy back our internal safe copy of message data to msg_out.
3575      * msg_out is a variable from the *program*, so it can't be used
3576      * internally as it can get "corrupted" by our use of SendMessage()
3577      * (back to the program) inside the message handling itself. */
3578     if (!msg_out)
3579     {
3580         SetLastError( ERROR_NOACCESS );
3581         return FALSE;
3582     }
3583     *msg_out = msg;
3584     return TRUE;
3585 }
3586
3587
3588 /***********************************************************************
3589  *              PeekMessageA  (USER32.@)
3590  */
3591 BOOL WINAPI DECLSPEC_HOTPATCH PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
3592 {
3593     if (get_pending_wmchar( msg, first, last, (flags & PM_REMOVE) )) return TRUE;
3594     if (!PeekMessageW( msg, hwnd, first, last, flags )) return FALSE;
3595     map_wparam_WtoA( msg, (flags & PM_REMOVE) );
3596     return TRUE;
3597 }
3598
3599
3600 /***********************************************************************
3601  *              GetMessageW  (USER32.@)
3602  */
3603 BOOL WINAPI DECLSPEC_HOTPATCH GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
3604 {
3605     HANDLE server_queue = get_server_queue_handle();
3606     unsigned int mask = QS_POSTMESSAGE | QS_SENDMESSAGE;  /* Always selected */
3607
3608     USER_CheckNotLock();
3609
3610     /* check for graphics events */
3611     USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
3612
3613     if (first || last)
3614     {
3615         if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
3616         if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
3617              ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
3618         if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
3619         if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
3620         if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
3621     }
3622     else mask = QS_ALLINPUT;
3623
3624     while (!peek_message( msg, hwnd, first, last, PM_REMOVE | (mask << 16), mask ))
3625     {
3626         wow_handlers.wait_message( 1, &server_queue, INFINITE, mask, 0 );
3627     }
3628
3629     return (msg->message != WM_QUIT);
3630 }
3631
3632
3633 /***********************************************************************
3634  *              GetMessageA  (USER32.@)
3635  */
3636 BOOL WINAPI DECLSPEC_HOTPATCH GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
3637 {
3638     if (get_pending_wmchar( msg, first, last, TRUE )) return TRUE;
3639     GetMessageW( msg, hwnd, first, last );
3640     map_wparam_WtoA( msg, TRUE );
3641     return (msg->message != WM_QUIT);
3642 }
3643
3644
3645 /***********************************************************************
3646  *              IsDialogMessageA (USER32.@)
3647  *              IsDialogMessage  (USER32.@)
3648  */
3649 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
3650 {
3651     MSG msg = *pmsg;
3652     map_wparam_AtoW( msg.message, &msg.wParam, WMCHAR_MAP_NOMAPPING );
3653     return IsDialogMessageW( hwndDlg, &msg );
3654 }
3655
3656
3657 /***********************************************************************
3658  *              TranslateMessage (USER32.@)
3659  *
3660  * Implementation of TranslateMessage.
3661  *
3662  * TranslateMessage translates virtual-key messages into character-messages,
3663  * as follows :
3664  * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
3665  * ditto replacing WM_* with WM_SYS*
3666  * This produces WM_CHAR messages only for keys mapped to ASCII characters
3667  * by the keyboard driver.
3668  *
3669  * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
3670  * return value is nonzero, regardless of the translation.
3671  *
3672  */
3673 BOOL WINAPI TranslateMessage( const MSG *msg )
3674 {
3675     UINT message;
3676     WCHAR wp[2];
3677     BYTE state[256];
3678
3679     if (msg->message < WM_KEYFIRST || msg->message > WM_KEYLAST) return FALSE;
3680     if (msg->message != WM_KEYDOWN && msg->message != WM_SYSKEYDOWN) return TRUE;
3681
3682     TRACE_(key)("Translating key %s (%04lX), scancode %04x\n",
3683                 SPY_GetVKeyName(msg->wParam), msg->wParam, HIWORD(msg->lParam));
3684
3685     switch (msg->wParam)
3686     {
3687     case VK_PACKET:
3688         message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
3689         TRACE_(key)("PostMessageW(%p,%s,%04x,%08x)\n",
3690                     msg->hwnd, SPY_GetMsgName(message, msg->hwnd), HIWORD(msg->lParam), LOWORD(msg->lParam));
3691         PostMessageW( msg->hwnd, message, HIWORD(msg->lParam), LOWORD(msg->lParam));
3692         return TRUE;
3693
3694     case VK_PROCESSKEY:
3695         return ImmTranslateMessage(msg->hwnd, msg->message, msg->wParam, msg->lParam);
3696     }
3697
3698     GetKeyboardState( state );
3699     /* FIXME : should handle ToUnicode yielding 2 */
3700     switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), state, wp, 2, 0))
3701     {
3702     case 1:
3703         message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
3704         TRACE_(key)("1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
3705             msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
3706         PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
3707         break;
3708
3709     case -1:
3710         message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
3711         TRACE_(key)("-1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
3712             msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
3713         PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
3714         break;
3715     }
3716     return TRUE;
3717 }
3718
3719
3720 /***********************************************************************
3721  *              DispatchMessageA (USER32.@)
3722  *
3723  * See DispatchMessageW.
3724  */
3725 LRESULT WINAPI DECLSPEC_HOTPATCH DispatchMessageA( const MSG* msg )
3726 {
3727     LRESULT retval;
3728
3729       /* Process timer messages */
3730     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3731     {
3732         if (msg->lParam)
3733         {
3734             __TRY
3735             {
3736                 retval = CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
3737                                           msg->message, msg->wParam, GetTickCount() );
3738             }
3739             __EXCEPT_PAGE_FAULT
3740             {
3741                 retval = 0;
3742             }
3743             __ENDTRY
3744             return retval;
3745         }
3746     }
3747     if (!msg->hwnd) return 0;
3748
3749     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3750                       msg->wParam, msg->lParam );
3751
3752     if (!WINPROC_call_window( msg->hwnd, msg->message, msg->wParam, msg->lParam,
3753                               &retval, FALSE, WMCHAR_MAP_DISPATCHMESSAGE ))
3754     {
3755         if (!IsWindow( msg->hwnd )) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3756         else SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3757         retval = 0;
3758     }
3759
3760     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3761                      msg->wParam, msg->lParam );
3762
3763     if (msg->message == WM_PAINT)
3764     {
3765         /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3766         HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3767         GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3768         DeleteObject( hrgn );
3769     }
3770     return retval;
3771 }
3772
3773
3774 /***********************************************************************
3775  *              DispatchMessageW (USER32.@) Process a message
3776  *
3777  * Process the message specified in the structure *_msg_.
3778  *
3779  * If the lpMsg parameter points to a WM_TIMER message and the
3780  * parameter of the WM_TIMER message is not NULL, the lParam parameter
3781  * points to the function that is called instead of the window
3782  * procedure. The function stored in lParam (timer callback) is protected
3783  * from causing page-faults.
3784  *
3785  * The message must be valid.
3786  *
3787  * RETURNS
3788  *
3789  *   DispatchMessage() returns the result of the window procedure invoked.
3790  *
3791  * CONFORMANCE
3792  *
3793  *   ECMA-234, Win32
3794  *
3795  */
3796 LRESULT WINAPI DECLSPEC_HOTPATCH DispatchMessageW( const MSG* msg )
3797 {
3798     LRESULT retval;
3799
3800       /* Process timer messages */
3801     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3802     {
3803         if (msg->lParam)
3804         {
3805             __TRY
3806             {
3807                 retval = CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
3808                                           msg->message, msg->wParam, GetTickCount() );
3809             }
3810             __EXCEPT_PAGE_FAULT
3811             {
3812                 retval = 0;
3813             }
3814             __ENDTRY
3815             return retval;
3816         }
3817     }
3818     if (!msg->hwnd) return 0;
3819
3820     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3821                       msg->wParam, msg->lParam );
3822
3823     if (!WINPROC_call_window( msg->hwnd, msg->message, msg->wParam, msg->lParam,
3824                               &retval, TRUE, WMCHAR_MAP_DISPATCHMESSAGE ))
3825     {
3826         if (!IsWindow( msg->hwnd )) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3827         else SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3828         retval = 0;
3829     }
3830
3831     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3832                      msg->wParam, msg->lParam );
3833
3834     if (msg->message == WM_PAINT)
3835     {
3836         /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3837         HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3838         GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3839         DeleteObject( hrgn );
3840     }
3841     return retval;
3842 }
3843
3844
3845 /***********************************************************************
3846  *              GetMessagePos (USER.119)
3847  *              GetMessagePos (USER32.@)
3848  *
3849  * The GetMessagePos() function returns a long value representing a
3850  * cursor position, in screen coordinates, when the last message
3851  * retrieved by the GetMessage() function occurs. The x-coordinate is
3852  * in the low-order word of the return value, the y-coordinate is in
3853  * the high-order word. The application can use the MAKEPOINT()
3854  * macro to obtain a POINT structure from the return value.
3855  *
3856  * For the current cursor position, use GetCursorPos().
3857  *
3858  * RETURNS
3859  *
3860  * Cursor position of last message on success, zero on failure.
3861  *
3862  * CONFORMANCE
3863  *
3864  * ECMA-234, Win32
3865  *
3866  */
3867 DWORD WINAPI GetMessagePos(void)
3868 {
3869     return get_user_thread_info()->GetMessagePosVal;
3870 }
3871
3872
3873 /***********************************************************************
3874  *              GetMessageTime (USER.120)
3875  *              GetMessageTime (USER32.@)
3876  *
3877  * GetMessageTime() returns the message time for the last message
3878  * retrieved by the function. The time is measured in milliseconds with
3879  * the same offset as GetTickCount().
3880  *
3881  * Since the tick count wraps, this is only useful for moderately short
3882  * relative time comparisons.
3883  *
3884  * RETURNS
3885  *
3886  * Time of last message on success, zero on failure.
3887  */
3888 LONG WINAPI GetMessageTime(void)
3889 {
3890     return get_user_thread_info()->GetMessageTimeVal;
3891 }
3892
3893
3894 /***********************************************************************
3895  *              GetMessageExtraInfo (USER.288)
3896  *              GetMessageExtraInfo (USER32.@)
3897  */
3898 LPARAM WINAPI GetMessageExtraInfo(void)
3899 {
3900     return get_user_thread_info()->GetMessageExtraInfoVal;
3901 }
3902
3903
3904 /***********************************************************************
3905  *              SetMessageExtraInfo (USER32.@)
3906  */
3907 LPARAM WINAPI SetMessageExtraInfo(LPARAM lParam)
3908 {
3909     struct user_thread_info *thread_info = get_user_thread_info();
3910     LONG old_value = thread_info->GetMessageExtraInfoVal;
3911     thread_info->GetMessageExtraInfoVal = lParam;
3912     return old_value;
3913 }
3914
3915
3916 /***********************************************************************
3917  *              WaitMessage (USER.112) Suspend thread pending messages
3918  *              WaitMessage (USER32.@) Suspend thread pending messages
3919  *
3920  * WaitMessage() suspends a thread until events appear in the thread's
3921  * queue.
3922  */
3923 BOOL WINAPI WaitMessage(void)
3924 {
3925     return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
3926 }
3927
3928
3929 /***********************************************************************
3930  *              MsgWaitForMultipleObjectsEx   (USER32.@)
3931  */
3932 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
3933                                           DWORD timeout, DWORD mask, DWORD flags )
3934 {
3935     HANDLE handles[MAXIMUM_WAIT_OBJECTS];
3936     DWORD i;
3937
3938     if (count > MAXIMUM_WAIT_OBJECTS-1)
3939     {
3940         SetLastError( ERROR_INVALID_PARAMETER );
3941         return WAIT_FAILED;
3942     }
3943
3944     /* set the queue mask */
3945     SERVER_START_REQ( set_queue_mask )
3946     {
3947         req->wake_mask    = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
3948         req->changed_mask = mask;
3949         req->skip_wait    = 0;
3950         wine_server_call( req );
3951     }
3952     SERVER_END_REQ;
3953
3954     /* add the queue to the handle list */
3955     for (i = 0; i < count; i++) handles[i] = pHandles[i];
3956     handles[count] = get_server_queue_handle();
3957
3958     return wow_handlers.wait_message( count+1, handles, timeout, mask, flags );
3959 }
3960
3961
3962 /***********************************************************************
3963  *              MsgWaitForMultipleObjects (USER32.@)
3964  */
3965 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
3966                                         BOOL wait_all, DWORD timeout, DWORD mask )
3967 {
3968     return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
3969                                         wait_all ? MWMO_WAITALL : 0 );
3970 }
3971
3972
3973 /***********************************************************************
3974  *              WaitForInputIdle (USER32.@)
3975  */
3976 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
3977 {
3978     DWORD start_time, elapsed, ret;
3979     HANDLE handles[2];
3980
3981     handles[0] = hProcess;
3982     SERVER_START_REQ( get_process_idle_event )
3983     {
3984         req->handle = wine_server_obj_handle( hProcess );
3985         wine_server_call_err( req );
3986         handles[1] = wine_server_ptr_handle( reply->event );
3987     }
3988     SERVER_END_REQ;
3989     if (!handles[1]) return WAIT_FAILED;  /* no event to wait on */
3990
3991     start_time = GetTickCount();
3992     elapsed = 0;
3993
3994     TRACE("waiting for %p\n", handles[1] );
3995     do
3996     {
3997         ret = MsgWaitForMultipleObjects ( 2, handles, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
3998         switch (ret)
3999         {
4000         case WAIT_OBJECT_0:
4001             return 0;
4002         case WAIT_OBJECT_0+2:
4003             process_sent_messages();
4004             break;
4005         case WAIT_TIMEOUT:
4006         case WAIT_FAILED:
4007             TRACE("timeout or error\n");
4008             return ret;
4009         default:
4010             TRACE("finished\n");
4011             return 0;
4012         }
4013         if (dwTimeOut != INFINITE)
4014         {
4015             elapsed = GetTickCount() - start_time;
4016             if (elapsed > dwTimeOut)
4017                 break;
4018         }
4019     }
4020     while (1);
4021
4022     return WAIT_TIMEOUT;
4023 }
4024
4025
4026 /***********************************************************************
4027  *              RegisterWindowMessageA (USER32.@)
4028  *              RegisterWindowMessage (USER.118)
4029  */
4030 UINT WINAPI RegisterWindowMessageA( LPCSTR str )
4031 {
4032     UINT ret = GlobalAddAtomA(str);
4033     TRACE("%s, ret=%x\n", str, ret);
4034     return ret;
4035 }
4036
4037
4038 /***********************************************************************
4039  *              RegisterWindowMessageW (USER32.@)
4040  */
4041 UINT WINAPI RegisterWindowMessageW( LPCWSTR str )
4042 {
4043     UINT ret = GlobalAddAtomW(str);
4044     TRACE("%s ret=%x\n", debugstr_w(str), ret);
4045     return ret;
4046 }
4047
4048 typedef struct BroadcastParm
4049 {
4050     DWORD flags;
4051     LPDWORD recipients;
4052     UINT msg;
4053     WPARAM wp;
4054     LPARAM lp;
4055     DWORD success;
4056     HWINSTA winsta;
4057 } BroadcastParm;
4058
4059 static BOOL CALLBACK bcast_childwindow( HWND hw, LPARAM lp )
4060 {
4061     BroadcastParm *parm = (BroadcastParm*)lp;
4062     DWORD_PTR retval = 0;
4063     LRESULT lresult;
4064
4065     if (parm->flags & BSF_IGNORECURRENTTASK && WIN_IsCurrentProcess(hw))
4066     {
4067         TRACE("Not telling myself %p\n", hw);
4068         return TRUE;
4069     }
4070
4071     /* I don't know 100% for sure if this is what Windows does, but it fits the tests */
4072     if (parm->flags & BSF_QUERY)
4073     {
4074         TRACE("Telling window %p using SendMessageTimeout\n", hw);
4075
4076         /* Not tested for conflicting flags */
4077         if (parm->flags & BSF_FORCEIFHUNG || parm->flags & BSF_NOHANG)
4078             lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_ABORTIFHUNG, 2000, &retval );
4079         else if (parm->flags & BSF_NOTIMEOUTIFNOTHUNG)
4080             lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NOTIMEOUTIFNOTHUNG, 2000, &retval );
4081         else
4082             lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NORMAL, 2000, &retval );
4083
4084         if (!lresult && GetLastError() == ERROR_TIMEOUT)
4085         {
4086             WARN("Timed out!\n");
4087             if (!(parm->flags & BSF_FORCEIFHUNG))
4088                 goto fail;
4089         }
4090         if (retval == BROADCAST_QUERY_DENY)
4091             goto fail;
4092
4093         return TRUE;
4094
4095 fail:
4096         parm->success = 0;
4097         return FALSE;
4098     }
4099     else if (parm->flags & BSF_POSTMESSAGE)
4100     {
4101         TRACE("Telling window %p using PostMessage\n", hw);
4102         PostMessageW( hw, parm->msg, parm->wp, parm->lp );
4103     }
4104     else
4105     {
4106         TRACE("Telling window %p using SendNotifyMessage\n", hw);
4107         SendNotifyMessageW( hw, parm->msg, parm->wp, parm->lp );
4108     }
4109
4110     return TRUE;
4111 }
4112
4113 static BOOL CALLBACK bcast_desktop( LPWSTR desktop, LPARAM lp )
4114 {
4115     BOOL ret;
4116     HDESK hdesktop;
4117     BroadcastParm *parm = (BroadcastParm*)lp;
4118
4119     TRACE("desktop: %s\n", debugstr_w( desktop ));
4120
4121     hdesktop = open_winstation_desktop( parm->winsta, desktop, 0, FALSE, DESKTOP_ENUMERATE|DESKTOP_WRITEOBJECTS|STANDARD_RIGHTS_WRITE );
4122     if (!hdesktop)
4123     {
4124         FIXME("Could not open desktop %s\n", debugstr_w(desktop));
4125         return TRUE;
4126     }
4127
4128     ret = EnumDesktopWindows( hdesktop, bcast_childwindow, lp );
4129     CloseDesktop(hdesktop);
4130     TRACE("-->%d\n", ret);
4131     return parm->success;
4132 }
4133
4134 static BOOL CALLBACK bcast_winsta( LPWSTR winsta, LPARAM lp )
4135 {
4136     BOOL ret;
4137     HWINSTA hwinsta = OpenWindowStationW( winsta, FALSE, WINSTA_ENUMDESKTOPS );
4138     TRACE("hwinsta: %p/%s/%08x\n", hwinsta, debugstr_w( winsta ), GetLastError());
4139     if (!hwinsta)
4140         return TRUE;
4141     ((BroadcastParm *)lp)->winsta = hwinsta;
4142     ret = EnumDesktopsW( hwinsta, bcast_desktop, lp );
4143     CloseWindowStation( hwinsta );
4144     TRACE("-->%d\n", ret);
4145     return ret;
4146 }
4147
4148 /***********************************************************************
4149  *              BroadcastSystemMessageA (USER32.@)
4150  *              BroadcastSystemMessage  (USER32.@)
4151  */
4152 LONG WINAPI BroadcastSystemMessageA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
4153 {
4154     return BroadcastSystemMessageExA( flags, recipients, msg, wp, lp, NULL );
4155 }
4156
4157
4158 /***********************************************************************
4159  *              BroadcastSystemMessageW (USER32.@)
4160  */
4161 LONG WINAPI BroadcastSystemMessageW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
4162 {
4163     return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, NULL );
4164 }
4165
4166 /***********************************************************************
4167  *              BroadcastSystemMessageExA (USER32.@)
4168  */
4169 LONG WINAPI BroadcastSystemMessageExA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
4170 {
4171     map_wparam_AtoW( msg, &wp, WMCHAR_MAP_NOMAPPING );
4172     return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, NULL );
4173 }
4174
4175
4176 /***********************************************************************
4177  *              BroadcastSystemMessageExW (USER32.@)
4178  */
4179 LONG WINAPI BroadcastSystemMessageExW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
4180 {
4181     BroadcastParm parm;
4182     DWORD recips = BSM_ALLCOMPONENTS;
4183     BOOL ret = TRUE;
4184     static const DWORD all_flags = ( BSF_QUERY | BSF_IGNORECURRENTTASK | BSF_FLUSHDISK | BSF_NOHANG
4185                                    | BSF_POSTMESSAGE | BSF_FORCEIFHUNG | BSF_NOTIMEOUTIFNOTHUNG
4186                                    | BSF_ALLOWSFW | BSF_SENDNOTIFYMESSAGE | BSF_RETURNHDESK | BSF_LUID );
4187
4188     TRACE("Flags: %08x, recipients: %p(0x%x), msg: %04x, wparam: %08lx, lparam: %08lx\n", flags, recipients,
4189          (recipients ? *recipients : recips), msg, wp, lp);
4190
4191     if (flags & ~all_flags)
4192     {
4193         SetLastError(ERROR_INVALID_PARAMETER);
4194         return 0;
4195     }
4196
4197     if (!recipients)
4198         recipients = &recips;
4199
4200     if ( pinfo && flags & BSF_QUERY )
4201         FIXME("Not returning PBSMINFO information yet\n");
4202
4203     parm.flags = flags;
4204     parm.recipients = recipients;
4205     parm.msg = msg;
4206     parm.wp = wp;
4207     parm.lp = lp;
4208     parm.success = TRUE;
4209
4210     if (*recipients & BSM_ALLDESKTOPS || *recipients == BSM_ALLCOMPONENTS)
4211         ret = EnumWindowStationsW(bcast_winsta, (LONG_PTR)&parm);
4212     else if (*recipients & BSM_APPLICATIONS)
4213     {
4214         EnumWindows(bcast_childwindow, (LONG_PTR)&parm);
4215         ret = parm.success;
4216     }
4217     else
4218         FIXME("Recipients %08x not supported!\n", *recipients);
4219
4220     return ret;
4221 }
4222
4223 /***********************************************************************
4224  *              SetMessageQueue (USER32.@)
4225  */
4226 BOOL WINAPI SetMessageQueue( INT size )
4227 {
4228     /* now obsolete the message queue will be expanded dynamically as necessary */
4229     return TRUE;
4230 }
4231
4232
4233 /***********************************************************************
4234  *              MessageBeep (USER32.@)
4235  */
4236 BOOL WINAPI MessageBeep( UINT i )
4237 {
4238     BOOL active = TRUE;
4239     SystemParametersInfoA( SPI_GETBEEP, 0, &active, FALSE );
4240     if (active) USER_Driver->pBeep();
4241     return TRUE;
4242 }
4243
4244
4245 /***********************************************************************
4246  *              SetTimer (USER32.@)
4247  */
4248 UINT_PTR WINAPI SetTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
4249 {
4250     UINT_PTR ret;
4251     WNDPROC winproc = 0;
4252
4253     if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, FALSE );
4254
4255     SERVER_START_REQ( set_win_timer )
4256     {
4257         req->win    = wine_server_user_handle( hwnd );
4258         req->msg    = WM_TIMER;
4259         req->id     = id;
4260         req->rate   = max( timeout, SYS_TIMER_RATE );
4261         req->lparam = (ULONG_PTR)winproc;
4262         if (!wine_server_call_err( req ))
4263         {
4264             ret = reply->id;
4265             if (!ret) ret = TRUE;
4266         }
4267         else ret = 0;
4268     }
4269     SERVER_END_REQ;
4270
4271     TRACE("Added %p %lx %p timeout %d\n", hwnd, id, winproc, timeout );
4272     return ret;
4273 }
4274
4275
4276 /***********************************************************************
4277  *              SetSystemTimer (USER32.@)
4278  */
4279 UINT_PTR WINAPI SetSystemTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
4280 {
4281     UINT_PTR ret;
4282     WNDPROC winproc = 0;
4283
4284     if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, FALSE );
4285
4286     SERVER_START_REQ( set_win_timer )
4287     {
4288         req->win    = wine_server_user_handle( hwnd );
4289         req->msg    = WM_SYSTIMER;
4290         req->id     = id;
4291         req->rate   = max( timeout, SYS_TIMER_RATE );
4292         req->lparam = (ULONG_PTR)winproc;
4293         if (!wine_server_call_err( req ))
4294         {
4295             ret = reply->id;
4296             if (!ret) ret = TRUE;
4297         }
4298         else ret = 0;
4299     }
4300     SERVER_END_REQ;
4301
4302     TRACE("Added %p %lx %p timeout %d\n", hwnd, id, winproc, timeout );
4303     return ret;
4304 }
4305
4306
4307 /***********************************************************************
4308  *              KillTimer (USER32.@)
4309  */
4310 BOOL WINAPI KillTimer( HWND hwnd, UINT_PTR id )
4311 {
4312     BOOL ret;
4313
4314     SERVER_START_REQ( kill_win_timer )
4315     {
4316         req->win = wine_server_user_handle( hwnd );
4317         req->msg = WM_TIMER;
4318         req->id  = id;
4319         ret = !wine_server_call_err( req );
4320     }
4321     SERVER_END_REQ;
4322     return ret;
4323 }
4324
4325
4326 /***********************************************************************
4327  *              KillSystemTimer (USER32.@)
4328  */
4329 BOOL WINAPI KillSystemTimer( HWND hwnd, UINT_PTR id )
4330 {
4331     BOOL ret;
4332
4333     SERVER_START_REQ( kill_win_timer )
4334     {
4335         req->win = wine_server_user_handle( hwnd );
4336         req->msg = WM_SYSTIMER;
4337         req->id  = id;
4338         ret = !wine_server_call_err( req );
4339     }
4340     SERVER_END_REQ;
4341     return ret;
4342 }
4343
4344
4345 /**********************************************************************
4346  *              IsGUIThread  (USER32.@)
4347  */
4348 BOOL WINAPI IsGUIThread( BOOL convert )
4349 {
4350     FIXME( "%u: stub\n", convert );
4351     return TRUE;
4352 }
4353
4354
4355 /**********************************************************************
4356  *              GetGUIThreadInfo  (USER32.@)
4357  */
4358 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
4359 {
4360     BOOL ret;
4361
4362     if (info->cbSize != sizeof(*info))
4363     {
4364         SetLastError( ERROR_INVALID_PARAMETER );
4365         return FALSE;
4366     }
4367
4368     SERVER_START_REQ( get_thread_input )
4369     {
4370         req->tid = id;
4371         if ((ret = !wine_server_call_err( req )))
4372         {
4373             info->flags          = 0;
4374             info->hwndActive     = wine_server_ptr_handle( reply->active );
4375             info->hwndFocus      = wine_server_ptr_handle( reply->focus );
4376             info->hwndCapture    = wine_server_ptr_handle( reply->capture );
4377             info->hwndMenuOwner  = wine_server_ptr_handle( reply->menu_owner );
4378             info->hwndMoveSize   = wine_server_ptr_handle( reply->move_size );
4379             info->hwndCaret      = wine_server_ptr_handle( reply->caret );
4380             info->rcCaret.left   = reply->rect.left;
4381             info->rcCaret.top    = reply->rect.top;
4382             info->rcCaret.right  = reply->rect.right;
4383             info->rcCaret.bottom = reply->rect.bottom;
4384             if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
4385             if (reply->move_size) info->flags |= GUI_INMOVESIZE;
4386             if (reply->caret) info->flags |= GUI_CARETBLINKING;
4387         }
4388     }
4389     SERVER_END_REQ;
4390     return ret;
4391 }
4392
4393
4394 /******************************************************************
4395  *              IsHungAppWindow (USER32.@)
4396  *
4397  */
4398 BOOL WINAPI IsHungAppWindow( HWND hWnd )
4399 {
4400     BOOL ret;
4401
4402     SERVER_START_REQ( is_window_hung )
4403     {
4404         req->win = wine_server_user_handle( hWnd );
4405         ret = !wine_server_call_err( req ) && reply->is_hung;
4406     }
4407     SERVER_END_REQ;
4408     return ret;
4409 }
4410
4411 /******************************************************************
4412  *      ChangeWindowMessageFilter (USER32.@)
4413  */
4414 BOOL WINAPI ChangeWindowMessageFilter( UINT message, DWORD flag )
4415 {
4416     FIXME( "%x %08x\n", message, flag );
4417     return TRUE;
4418 }