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