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