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