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