Fixed a race condition on RPC worker thread creation, and a typo.
[wine] / dlls / user / message.c
1 /*
2  * Window messaging support
3  *
4  * Copyright 2001 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winerror.h"
28 #include "winnls.h"
29 #include "dde.h"
30 #include "wine/unicode.h"
31 #include "wine/server.h"
32 #include "message.h"
33 #include "user.h"
34 #include "win.h"
35 #include "winproc.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(msg);
39
40 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
41 #define WM_NCMOUSELAST  WM_NCMBUTTONDBLCLK
42
43 #define MAX_PACK_COUNT 4
44
45 /* description of the data fields that need to be packed along with a sent message */
46 struct packed_message
47 {
48     int         count;
49     const void *data[MAX_PACK_COUNT];
50     size_t      size[MAX_PACK_COUNT];
51 };
52
53 /* info about the message currently being received by the current thread */
54 struct received_message_info
55 {
56     enum message_type type;
57     MSG               msg;
58     UINT              flags;  /* InSendMessageEx return flags */
59 };
60
61 /* structure to group all parameters for sent messages of the various kinds */
62 struct send_message_info
63 {
64     enum message_type type;
65     HWND              hwnd;
66     UINT              msg;
67     WPARAM            wparam;
68     LPARAM            lparam;
69     UINT              flags;      /* flags for SendMessageTimeout */
70     UINT              timeout;    /* timeout for SendMessageTimeout */
71     SENDASYNCPROC     callback;   /* callback function for SendMessageCallback */
72     ULONG_PTR         data;       /* callback data */
73 };
74
75
76 /* flag for messages that contain pointers */
77 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
78
79 #define SET(msg) (1 << ((msg) & 31))
80
81 static const unsigned int message_pointer_flags[] =
82 {
83     /* 0x00 - 0x1f */
84     SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
85     SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
86     /* 0x20 - 0x3f */
87     SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
88     SET(WM_COMPAREITEM),
89     /* 0x40 - 0x5f */
90     SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
91     SET(WM_NOTIFY) | SET(WM_HELP),
92     /* 0x60 - 0x7f */
93     SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
94     /* 0x80 - 0x9f */
95     SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
96     /* 0xa0 - 0xbf */
97     SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
98     /* 0xc0 - 0xdf */
99     SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
100     /* 0xe0 - 0xff */
101     SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO),
102     /* 0x100 - 0x11f */
103     0,
104     /* 0x120 - 0x13f */
105     0,
106     /* 0x140 - 0x15f */
107     SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
108     SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
109     SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
110     /* 0x160 - 0x17f */
111     0,
112     /* 0x180 - 0x19f */
113     SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
114     SET(LB_DIR) | SET(LB_FINDSTRING) |
115     SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
116     /* 0x1a0 - 0x1bf */
117     SET(LB_FINDSTRINGEXACT),
118     /* 0x1c0 - 0x1df */
119     0,
120     /* 0x1e0 - 0x1ff */
121     0,
122     /* 0x200 - 0x21f */
123     SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
124     /* 0x220 - 0x23f */
125     SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
126     SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
127     /* 0x240 - 0x25f */
128     0,
129     /* 0x260 - 0x27f */
130     0,
131     /* 0x280 - 0x29f */
132     0,
133     /* 0x2a0 - 0x2bf */
134     0,
135     /* 0x2c0 - 0x2df */
136     0,
137     /* 0x2e0 - 0x2ff */
138     0,
139     /* 0x300 - 0x31f */
140     SET(WM_ASKCBFORMATNAME)
141 };
142
143 /* flags for messages that contain Unicode strings */
144 static const unsigned int message_unicode_flags[] =
145 {
146     /* 0x00 - 0x1f */
147     SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
148     SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
149     /* 0x20 - 0x3f */
150     SET(WM_CHARTOITEM),
151     /* 0x40 - 0x5f */
152     0,
153     /* 0x60 - 0x7f */
154     0,
155     /* 0x80 - 0x9f */
156     SET(WM_NCCREATE),
157     /* 0xa0 - 0xbf */
158     0,
159     /* 0xc0 - 0xdf */
160     SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
161     /* 0xe0 - 0xff */
162     0,
163     /* 0x100 - 0x11f */
164     SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
165     /* 0x120 - 0x13f */
166     SET(WM_MENUCHAR),
167     /* 0x140 - 0x15f */
168     SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
169     SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
170     /* 0x160 - 0x17f */
171     0,
172     /* 0x180 - 0x19f */
173     SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
174     SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
175     /* 0x1a0 - 0x1bf */
176     SET(LB_FINDSTRINGEXACT),
177     /* 0x1c0 - 0x1df */
178     0,
179     /* 0x1e0 - 0x1ff */
180     0,
181     /* 0x200 - 0x21f */
182     0,
183     /* 0x220 - 0x23f */
184     SET(WM_MDICREATE),
185     /* 0x240 - 0x25f */
186     0,
187     /* 0x260 - 0x27f */
188     0,
189     /* 0x280 - 0x29f */
190     0,
191     /* 0x2a0 - 0x2bf */
192     0,
193     /* 0x2c0 - 0x2df */
194     0,
195     /* 0x2e0 - 0x2ff */
196     0,
197     /* 0x300 - 0x31f */
198     SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
199 };
200
201 /* check whether a given message type includes pointers */
202 inline static int is_pointer_message( UINT message )
203 {
204     if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
205     return (message_pointer_flags[message / 32] & SET(message)) != 0;
206 }
207
208 /* check whether a given message type contains Unicode (or ASCII) chars */
209 inline static int is_unicode_message( UINT message )
210 {
211     if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
212     return (message_unicode_flags[message / 32] & SET(message)) != 0;
213 }
214
215 #undef SET
216
217 /* add a data field to a packed message */
218 inline static void push_data( struct packed_message *data, const void *ptr, size_t size )
219 {
220     data->data[data->count] = ptr;
221     data->size[data->count] = size;
222     data->count++;
223 }
224
225 /* add a string to a packed message */
226 inline static void push_string( struct packed_message *data, LPCWSTR str )
227 {
228     push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
229 }
230
231 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
232 inline static void *get_data( void **buffer, size_t size )
233 {
234     void *ret = *buffer;
235     *buffer = (char *)*buffer + size;
236     return ret;
237 }
238
239 /* make sure that the buffer contains a valid null-terminated Unicode string */
240 inline static BOOL check_string( LPCWSTR str, size_t size )
241 {
242     for (size /= sizeof(WCHAR); size; size--, str++)
243         if (!*str) return TRUE;
244     return FALSE;
245 }
246
247 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
248 inline static void *get_buffer_space( void **buffer, size_t size )
249 {
250     void *ret;
251     if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
252         HeapFree( GetProcessHeap(), 0, *buffer );
253     *buffer = ret;
254     return ret;
255 }
256
257 /* retrieve a string pointer from packed data */
258 inline static LPWSTR get_string( void **buffer )
259 {
260     return get_data( buffer, (strlenW( (LPWSTR)*buffer ) + 1) * sizeof(WCHAR) );
261 }
262
263 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
264 inline static BOOL combobox_has_strings( HWND hwnd )
265 {
266     DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
267     return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
268 }
269
270 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
271 inline static BOOL listbox_has_strings( HWND hwnd )
272 {
273     DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
274     return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
275 }
276
277 /* check if hwnd is a broadcast magic handle */
278 inline static BOOL is_broadcast( HWND hwnd )
279 {
280     return (hwnd == HWND_BROADCAST || hwnd == HWND_TOPMOST);
281 }
282
283
284 /***********************************************************************
285  *              broadcast_message_callback
286  *
287  * Helper callback for broadcasting messages.
288  */
289 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
290 {
291     struct send_message_info *info = (struct send_message_info *)lparam;
292     if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
293     switch(info->type)
294     {
295     case MSG_UNICODE:
296         SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
297                              info->flags, info->timeout, NULL );
298         break;
299     case MSG_ASCII:
300         SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
301                              info->flags, info->timeout, NULL );
302         break;
303     case MSG_NOTIFY:
304         SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
305         break;
306     case MSG_CALLBACK:
307         SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
308                               info->callback, info->data );
309         break;
310     case MSG_POSTED:
311         PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
312         break;
313     default:
314         ERR( "bad type %d\n", info->type );
315         break;
316     }
317     return TRUE;
318 }
319
320
321 /***********************************************************************
322  *              map_wparam_AtoW
323  *
324  * Convert the wparam of an ASCII message to Unicode.
325  */
326 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
327 {
328     if (message == WM_CHARTOITEM ||
329         message == EM_SETPASSWORDCHAR ||
330         message == WM_CHAR ||
331         message == WM_DEADCHAR ||
332         message == WM_SYSCHAR ||
333         message == WM_SYSDEADCHAR ||
334         message == WM_MENUCHAR)
335     {
336         char ch = LOWORD(wparam);
337         WCHAR wch;
338         MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
339         wparam = MAKEWPARAM( wch, HIWORD(wparam) );
340     }
341     return wparam;
342 }
343
344
345 /***********************************************************************
346  *              map_wparam_WtoA
347  *
348  * Convert the wparam of a Unicode message to ASCII.
349  */
350 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
351 {
352     if (message == WM_CHARTOITEM ||
353         message == EM_SETPASSWORDCHAR ||
354         message == WM_CHAR ||
355         message == WM_DEADCHAR ||
356         message == WM_SYSCHAR ||
357         message == WM_SYSDEADCHAR ||
358         message == WM_MENUCHAR)
359     {
360         WCHAR wch = LOWORD(wparam);
361         char ch;
362         WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
363         wparam = MAKEWPARAM( (unsigned char)ch, HIWORD(wparam) );
364     }
365     return wparam;
366 }
367
368
369 /***********************************************************************
370  *              pack_message
371  *
372  * Pack a message for sending to another process.
373  * Return the size of the data we expect in the message reply.
374  * Set data->count to -1 if there is an error.
375  */
376 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
377                             struct packed_message *data )
378 {
379     data->count = 0;
380     switch(message)
381     {
382     case WM_NCCREATE:
383     case WM_CREATE:
384     {
385         CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
386         push_data( data, cs, sizeof(*cs) );
387         if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
388         if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
389         return sizeof(*cs);
390     }
391     case WM_GETTEXT:
392     case WM_ASKCBFORMATNAME:
393         return wparam * sizeof(WCHAR);
394     case WM_SETTEXT:
395     case WM_WININICHANGE:
396     case WM_DEVMODECHANGE:
397     case CB_DIR:
398     case LB_DIR:
399     case LB_ADDFILE:
400     case EM_REPLACESEL:
401         push_string( data, (LPWSTR)lparam );
402         return 0;
403     case WM_GETMINMAXINFO:
404         push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
405         return sizeof(MINMAXINFO);
406     case WM_DRAWITEM:
407         push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
408         return 0;
409     case WM_MEASUREITEM:
410         push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
411         return sizeof(MEASUREITEMSTRUCT);
412     case WM_DELETEITEM:
413         push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
414         return 0;
415     case WM_COMPAREITEM:
416         push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
417         return 0;
418     case WM_WINDOWPOSCHANGING:
419     case WM_WINDOWPOSCHANGED:
420         push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
421         return sizeof(WINDOWPOS);
422     case WM_COPYDATA:
423     {
424         COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
425         push_data( data, cp, sizeof(*cp) );
426         if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
427         return 0;
428     }
429     case WM_NOTIFY:
430         /* WM_NOTIFY cannot be sent across processes (MSDN) */
431         data->count = -1;
432         return 0;
433     case WM_HELP:
434         push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
435         return 0;
436     case WM_STYLECHANGING:
437     case WM_STYLECHANGED:
438         push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
439         return 0;
440     case WM_NCCALCSIZE:
441         if (!wparam)
442         {
443             push_data( data, (RECT *)lparam, sizeof(RECT) );
444             return sizeof(RECT);
445         }
446         else
447         {
448             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
449             push_data( data, nc, sizeof(*nc) );
450             push_data( data, nc->lppos, sizeof(*nc->lppos) );
451             return sizeof(*nc) + sizeof(*nc->lppos);
452         }
453     case WM_GETDLGCODE:
454         if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
455         return sizeof(MSG);
456     case SBM_SETSCROLLINFO:
457         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
458         return 0;
459     case SBM_GETSCROLLINFO:
460         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
461         return sizeof(SCROLLINFO);
462     case EM_GETSEL:
463     case SBM_GETRANGE:
464     case CB_GETEDITSEL:
465     {
466         size_t size = 0;
467         if (wparam) size += sizeof(DWORD);
468         if (lparam) size += sizeof(DWORD);
469         return size;
470     }
471     case EM_GETRECT:
472     case LB_GETITEMRECT:
473     case CB_GETDROPPEDCONTROLRECT:
474         return sizeof(RECT);
475     case EM_SETRECT:
476     case EM_SETRECTNP:
477         push_data( data, (RECT *)lparam, sizeof(RECT) );
478         return 0;
479     case EM_GETLINE:
480     {
481         WORD *pw = (WORD *)lparam;
482         push_data( data, pw, sizeof(*pw) );
483         return *pw * sizeof(WCHAR);
484     }
485     case EM_SETTABSTOPS:
486     case LB_SETTABSTOPS:
487         if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
488         return 0;
489     case CB_ADDSTRING:
490     case CB_INSERTSTRING:
491     case CB_FINDSTRING:
492     case CB_FINDSTRINGEXACT:
493     case CB_SELECTSTRING:
494         if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
495         return 0;
496     case CB_GETLBTEXT:
497         if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
498         return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
499     case LB_ADDSTRING:
500     case LB_INSERTSTRING:
501     case LB_FINDSTRING:
502     case LB_FINDSTRINGEXACT:
503     case LB_SELECTSTRING:
504         if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
505         return 0;
506     case LB_GETTEXT:
507         if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
508         return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
509     case LB_GETSELITEMS:
510         return wparam * sizeof(UINT);
511     case WM_NEXTMENU:
512         push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
513         return sizeof(MDINEXTMENU);
514     case WM_SIZING:
515     case WM_MOVING:
516         push_data( data, (RECT *)lparam, sizeof(RECT) );
517         return sizeof(RECT);
518     case WM_MDICREATE:
519     {
520         MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
521         push_data( data, cs, sizeof(*cs) );
522         if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
523         if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
524         return sizeof(*cs);
525     }
526     case WM_MDIGETACTIVE:
527         if (lparam) return sizeof(BOOL);
528         return 0;
529     case WM_WINE_SETWINDOWPOS:
530         push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
531         return 0;
532
533     /* these contain an HFONT */
534     case WM_SETFONT:
535     case WM_GETFONT:
536     /* these contain an HDC */
537     case WM_PAINT:
538     case WM_ERASEBKGND:
539     case WM_ICONERASEBKGND:
540     case WM_NCPAINT:
541     case WM_CTLCOLORMSGBOX:
542     case WM_CTLCOLOREDIT:
543     case WM_CTLCOLORLISTBOX:
544     case WM_CTLCOLORBTN:
545     case WM_CTLCOLORDLG:
546     case WM_CTLCOLORSCROLLBAR:
547     case WM_CTLCOLORSTATIC:
548     case WM_PRINT:
549     case WM_PRINTCLIENT:
550     /* these contain an HGLOBAL */
551     case WM_PAINTCLIPBOARD:
552     case WM_SIZECLIPBOARD:
553     /* these contain pointers */
554     case WM_DROPOBJECT:
555     case WM_QUERYDROPOBJECT:
556     case WM_DRAGLOOP:
557     case WM_DRAGSELECT:
558     case WM_DRAGMOVE:
559     case WM_DEVICECHANGE:
560         FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
561         data->count = -1;
562         return 0;
563     }
564     return 0;
565 }
566
567
568 /***********************************************************************
569  *              unpack_message
570  *
571  * Unpack a message received from another process.
572  */
573 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
574                             void **buffer, size_t size )
575 {
576     size_t minsize = 0;
577
578     switch(message)
579     {
580     case WM_NCCREATE:
581     case WM_CREATE:
582     {
583         CREATESTRUCTW *cs = *buffer;
584         WCHAR *str = (WCHAR *)(cs + 1);
585         if (size < sizeof(*cs)) return FALSE;
586         size -= sizeof(*cs);
587         if (HIWORD(cs->lpszName))
588         {
589             if (!check_string( str, size )) return FALSE;
590             cs->lpszName = str;
591             size -= (strlenW(str) + 1) * sizeof(WCHAR);
592             str += strlenW(str) + 1;
593         }
594         if (HIWORD(cs->lpszClass))
595         {
596             if (!check_string( str, size )) return FALSE;
597             cs->lpszClass = str;
598         }
599         break;
600     }
601     case WM_GETTEXT:
602     case WM_ASKCBFORMATNAME:
603         if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
604         break;
605     case WM_SETTEXT:
606     case WM_WININICHANGE:
607     case WM_DEVMODECHANGE:
608     case CB_DIR:
609     case LB_DIR:
610     case LB_ADDFILE:
611     case EM_REPLACESEL:
612         if (!check_string( *buffer, size )) return FALSE;
613         break;
614     case WM_GETMINMAXINFO:
615         minsize = sizeof(MINMAXINFO);
616         break;
617     case WM_DRAWITEM:
618         minsize = sizeof(DRAWITEMSTRUCT);
619         break;
620     case WM_MEASUREITEM:
621         minsize = sizeof(MEASUREITEMSTRUCT);
622         break;
623     case WM_DELETEITEM:
624         minsize = sizeof(DELETEITEMSTRUCT);
625         break;
626     case WM_COMPAREITEM:
627         minsize = sizeof(COMPAREITEMSTRUCT);
628         break;
629     case WM_WINDOWPOSCHANGING:
630     case WM_WINDOWPOSCHANGED:
631     case WM_WINE_SETWINDOWPOS:
632         minsize = sizeof(WINDOWPOS);
633         break;
634     case WM_COPYDATA:
635     {
636         COPYDATASTRUCT *cp = *buffer;
637         if (size < sizeof(*cp)) return FALSE;
638         if (cp->lpData)
639         {
640             minsize = sizeof(*cp) + cp->cbData;
641             cp->lpData = cp + 1;
642         }
643         break;
644     }
645     case WM_NOTIFY:
646         /* WM_NOTIFY cannot be sent across processes (MSDN) */
647         return FALSE;
648     case WM_HELP:
649         minsize = sizeof(HELPINFO);
650         break;
651     case WM_STYLECHANGING:
652     case WM_STYLECHANGED:
653         minsize = sizeof(STYLESTRUCT);
654         break;
655     case WM_NCCALCSIZE:
656         if (!*wparam) minsize = sizeof(RECT);
657         else
658         {
659             NCCALCSIZE_PARAMS *nc = *buffer;
660             if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
661             nc->lppos = (WINDOWPOS *)(nc + 1);
662         }
663         break;
664     case WM_GETDLGCODE:
665         if (!*lparam) return TRUE;
666         minsize = sizeof(MSG);
667         break;
668     case SBM_SETSCROLLINFO:
669         minsize = sizeof(SCROLLINFO);
670         break;
671     case SBM_GETSCROLLINFO:
672         if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
673         break;
674     case EM_GETSEL:
675     case SBM_GETRANGE:
676     case CB_GETEDITSEL:
677         if (*wparam || *lparam)
678         {
679             if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
680             if (*wparam) *wparam = (WPARAM)*buffer;
681             if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
682         }
683         return TRUE;
684     case EM_GETRECT:
685     case LB_GETITEMRECT:
686     case CB_GETDROPPEDCONTROLRECT:
687         if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
688         break;
689     case EM_SETRECT:
690     case EM_SETRECTNP:
691         minsize = sizeof(RECT);
692         break;
693     case EM_GETLINE:
694     {
695         WORD len;
696         if (size < sizeof(WORD)) return FALSE;
697         len = *(WORD *)*buffer;
698         if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
699         *lparam = (LPARAM)*buffer + sizeof(WORD);  /* don't erase WORD at start of buffer */
700         return TRUE;
701     }
702     case EM_SETTABSTOPS:
703     case LB_SETTABSTOPS:
704         if (!*wparam) return TRUE;
705         minsize = *wparam * sizeof(UINT);
706         break;
707     case CB_ADDSTRING:
708     case CB_INSERTSTRING:
709     case CB_FINDSTRING:
710     case CB_FINDSTRINGEXACT:
711     case CB_SELECTSTRING:
712     case LB_ADDSTRING:
713     case LB_INSERTSTRING:
714     case LB_FINDSTRING:
715     case LB_FINDSTRINGEXACT:
716     case LB_SELECTSTRING:
717         if (!*buffer) return TRUE;
718         if (!check_string( *buffer, size )) return FALSE;
719         break;
720     case CB_GETLBTEXT:
721     {
722         size = sizeof(ULONG_PTR);
723         if (combobox_has_strings( hwnd ))
724             size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
725         if (!get_buffer_space( buffer, size )) return FALSE;
726         break;
727     }
728     case LB_GETTEXT:
729     {
730         size = sizeof(ULONG_PTR);
731         if (listbox_has_strings( hwnd ))
732             size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
733         if (!get_buffer_space( buffer, size )) return FALSE;
734         break;
735     }
736     case LB_GETSELITEMS:
737         if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
738         break;
739     case WM_NEXTMENU:
740         minsize = sizeof(MDINEXTMENU);
741         if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
742         break;
743     case WM_SIZING:
744     case WM_MOVING:
745         minsize = sizeof(RECT);
746         if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
747         break;
748     case WM_MDICREATE:
749     {
750         MDICREATESTRUCTW *cs = *buffer;
751         WCHAR *str = (WCHAR *)(cs + 1);
752         if (size < sizeof(*cs)) return FALSE;
753         size -= sizeof(*cs);
754         if (HIWORD(cs->szTitle))
755         {
756             if (!check_string( str, size )) return FALSE;
757             cs->szTitle = str;
758             size -= (strlenW(str) + 1) * sizeof(WCHAR);
759             str += strlenW(str) + 1;
760         }
761         if (HIWORD(cs->szClass))
762         {
763             if (!check_string( str, size )) return FALSE;
764             cs->szClass = str;
765         }
766         break;
767     }
768     case WM_MDIGETACTIVE:
769         if (!*lparam) return TRUE;
770         if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
771         break;
772     /* these contain an HFONT */
773     case WM_SETFONT:
774     case WM_GETFONT:
775     /* these contain an HDC */
776     case WM_PAINT:
777     case WM_ERASEBKGND:
778     case WM_ICONERASEBKGND:
779     case WM_NCPAINT:
780     case WM_CTLCOLORMSGBOX:
781     case WM_CTLCOLOREDIT:
782     case WM_CTLCOLORLISTBOX:
783     case WM_CTLCOLORBTN:
784     case WM_CTLCOLORDLG:
785     case WM_CTLCOLORSCROLLBAR:
786     case WM_CTLCOLORSTATIC:
787     case WM_PRINT:
788     case WM_PRINTCLIENT:
789     /* these contain an HGLOBAL */
790     case WM_PAINTCLIPBOARD:
791     case WM_SIZECLIPBOARD:
792     /* these contain pointers */
793     case WM_DROPOBJECT:
794     case WM_QUERYDROPOBJECT:
795     case WM_DRAGLOOP:
796     case WM_DRAGSELECT:
797     case WM_DRAGMOVE:
798     case WM_DEVICECHANGE:
799         FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
800         return FALSE;
801
802     default:
803         return TRUE; /* message doesn't need any unpacking */
804     }
805
806     /* default exit for most messages: check minsize and store buffer in lparam */
807     if (size < minsize) return FALSE;
808     *lparam = (LPARAM)*buffer;
809     return TRUE;
810 }
811
812
813 /***********************************************************************
814  *              pack_reply
815  *
816  * Pack a reply to a message for sending to another process.
817  */
818 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
819                         LRESULT res, struct packed_message *data )
820 {
821     data->count = 0;
822     switch(message)
823     {
824     case WM_NCCREATE:
825     case WM_CREATE:
826         push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
827         break;
828     case WM_GETTEXT:
829     case CB_GETLBTEXT:
830     case LB_GETTEXT:
831         push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
832         break;
833     case WM_GETMINMAXINFO:
834         push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
835         break;
836     case WM_MEASUREITEM:
837         push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
838         break;
839     case WM_WINDOWPOSCHANGING:
840     case WM_WINDOWPOSCHANGED:
841         push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
842         break;
843     case WM_GETDLGCODE:
844         if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
845         break;
846     case SBM_GETSCROLLINFO:
847         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
848         break;
849     case EM_GETRECT:
850     case LB_GETITEMRECT:
851     case CB_GETDROPPEDCONTROLRECT:
852     case WM_SIZING:
853     case WM_MOVING:
854         push_data( data, (RECT *)lparam, sizeof(RECT) );
855         break;
856     case EM_GETLINE:
857     {
858         WORD *ptr = (WORD *)lparam;
859         push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
860         break;
861     }
862     case LB_GETSELITEMS:
863         push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
864         break;
865     case WM_MDIGETACTIVE:
866         if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
867         break;
868     case WM_NCCALCSIZE:
869         if (!wparam)
870             push_data( data, (RECT *)lparam, sizeof(RECT) );
871         else
872         {
873             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
874             push_data( data, nc, sizeof(*nc) );
875             push_data( data, nc->lppos, sizeof(*nc->lppos) );
876         }
877         break;
878     case EM_GETSEL:
879     case SBM_GETRANGE:
880     case CB_GETEDITSEL:
881         if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
882         if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
883         break;
884     case WM_NEXTMENU:
885         push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
886         break;
887     case WM_MDICREATE:
888         push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
889         break;
890     case WM_ASKCBFORMATNAME:
891         push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
892         break;
893     }
894 }
895
896
897 /***********************************************************************
898  *              unpack_reply
899  *
900  * Unpack a message reply received from another process.
901  */
902 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
903                           void *buffer, size_t size )
904 {
905     switch(message)
906     {
907     case WM_NCCREATE:
908     case WM_CREATE:
909     {
910         CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
911         LPCWSTR name = cs->lpszName, class = cs->lpszClass;
912         memcpy( cs, buffer, min( sizeof(*cs), size ));
913         cs->lpszName = name;  /* restore the original pointers */
914         cs->lpszClass = class;
915         break;
916     }
917     case WM_GETTEXT:
918     case WM_ASKCBFORMATNAME:
919         memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
920         break;
921     case WM_GETMINMAXINFO:
922         memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
923         break;
924     case WM_MEASUREITEM:
925         memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
926         break;
927     case WM_WINDOWPOSCHANGING:
928     case WM_WINDOWPOSCHANGED:
929         memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
930         break;
931     case WM_GETDLGCODE:
932         if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
933         break;
934     case SBM_GETSCROLLINFO:
935         memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
936         break;
937     case EM_GETRECT:
938     case CB_GETDROPPEDCONTROLRECT:
939     case LB_GETITEMRECT:
940     case WM_SIZING:
941     case WM_MOVING:
942         memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
943         break;
944     case EM_GETLINE:
945         size = min( size, (size_t)*(WORD *)lparam );
946         memcpy( (WCHAR *)lparam, buffer, size );
947         break;
948     case LB_GETSELITEMS:
949         memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
950         break;
951     case LB_GETTEXT:
952     case CB_GETLBTEXT:
953         memcpy( (WCHAR *)lparam, buffer, size );
954         break;
955     case WM_NEXTMENU:
956         memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
957         break;
958     case WM_MDIGETACTIVE:
959         if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
960         break;
961     case WM_NCCALCSIZE:
962         if (!wparam)
963             memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
964         else
965         {
966             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
967             WINDOWPOS *wp = nc->lppos;
968             memcpy( nc, buffer, min( sizeof(*nc), size ));
969             if (size > sizeof(*nc))
970             {
971                 size -= sizeof(*nc);
972                 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
973             }
974             nc->lppos = wp;  /* restore the original pointer */
975         }
976         break;
977     case EM_GETSEL:
978     case SBM_GETRANGE:
979     case CB_GETEDITSEL:
980         if (wparam)
981         {
982             memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
983             if (size <= sizeof(DWORD)) break;
984             size -= sizeof(DWORD);
985             buffer = (DWORD *)buffer + 1;
986         }
987         if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
988         break;
989     case WM_MDICREATE:
990     {
991         MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
992         LPCWSTR title = cs->szTitle, class = cs->szClass;
993         memcpy( cs, buffer, min( sizeof(*cs), size ));
994         cs->szTitle = title;  /* restore the original pointers */
995         cs->szClass = class;
996         break;
997     }
998     default:
999         ERR( "should not happen: unexpected message %x\n", message );
1000         break;
1001     }
1002 }
1003
1004
1005 /***********************************************************************
1006  *           reply_message
1007  *
1008  * Send a reply to a sent message.
1009  */
1010 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1011 {
1012     struct packed_message data;
1013     int i, replied = info->flags & ISMEX_REPLIED;
1014
1015     if (info->flags & ISMEX_NOTIFY) return;  /* notify messages don't get replies */
1016     if (!remove && replied) return;  /* replied already */
1017
1018     data.count = 0;
1019     info->flags |= ISMEX_REPLIED;
1020
1021     if (info->type == MSG_OTHER_PROCESS && !replied)
1022     {
1023         pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1024                     info->msg.lParam, result, &data );
1025     }
1026
1027     SERVER_START_REQ( reply_message )
1028     {
1029         req->type   = info->type;
1030         req->result = result;
1031         req->remove = remove;
1032         for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1033         wine_server_call( req );
1034     }
1035     SERVER_END_REQ;
1036 }
1037
1038
1039 /***********************************************************************
1040  *           handle_internal_message
1041  *
1042  * Handle an internal Wine message instead of calling the window proc.
1043  */
1044 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1045 {
1046     if (hwnd == GetDesktopWindow()) return 0;
1047     switch(msg)
1048     {
1049     case WM_WINE_DESTROYWINDOW:
1050         return WIN_DestroyWindow( hwnd );
1051     case WM_WINE_SETWINDOWPOS:
1052         return USER_Driver.pSetWindowPos( (WINDOWPOS *)lparam );
1053     case WM_WINE_SHOWWINDOW:
1054         return ShowWindow( hwnd, wparam );
1055     case WM_WINE_SETPARENT:
1056         return (LRESULT)SetParent( hwnd, (HWND)wparam );
1057     case WM_WINE_SETWINDOWLONG:
1058         return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1059     case WM_WINE_ENABLEWINDOW:
1060         return EnableWindow( hwnd, wparam );
1061     case WM_WINE_SETACTIVEWINDOW:
1062         return (LRESULT)SetActiveWindow( (HWND)wparam );
1063     default:
1064         FIXME( "unknown internal message %x\n", msg );
1065         return 0;
1066     }
1067 }
1068
1069 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1070  * to the memory handle, we keep track (in the server side) of all pairs of handle
1071  * used (the client passes its value and the content of the memory handle), and
1072  * the server stored both values (the client, and the local one, created after the
1073  * content). When a ACK message is generated, the list of pair is searched for a
1074  * matching pair, so that the client memory handle can be returned.
1075  */
1076 struct DDE_pair {
1077     HGLOBAL     client_hMem;
1078     HGLOBAL     server_hMem;
1079 };
1080
1081 static      struct DDE_pair*    dde_pairs;
1082 static      int                 dde_num_alloc;
1083 static      int                 dde_num_used;
1084 static      CRITICAL_SECTION    dde_crst = CRITICAL_SECTION_INIT("Raw_DDE_CritSect");
1085
1086 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1087 {
1088     int  i;
1089 #define GROWBY  4
1090
1091     EnterCriticalSection(&dde_crst);
1092
1093     /* now remember the pair of hMem on both sides */
1094     if (dde_num_used == dde_num_alloc)
1095     {
1096         struct DDE_pair* tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1097                                             (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1098         if (!tmp)
1099         {
1100             LeaveCriticalSection(&dde_crst);
1101             return FALSE;
1102         }
1103         dde_pairs = tmp;
1104         /* zero out newly allocated part */
1105         memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1106         dde_num_alloc += GROWBY;
1107     }
1108 #undef GROWBY
1109     for (i = 0; i < dde_num_alloc; i++)
1110     {
1111         if (dde_pairs[i].server_hMem == 0)
1112         {
1113             dde_pairs[i].client_hMem = chm;
1114             dde_pairs[i].server_hMem = shm;
1115             dde_num_used++;
1116             break;
1117         }
1118     }
1119     LeaveCriticalSection(&dde_crst);
1120     return TRUE;
1121 }
1122
1123 static HGLOBAL dde_get_pair(HGLOBAL shm)
1124 {
1125     int  i;
1126     HGLOBAL     ret = 0;
1127
1128     EnterCriticalSection(&dde_crst);
1129     for (i = 0; i < dde_num_alloc; i++)
1130     {
1131         if (dde_pairs[i].server_hMem == shm)
1132         {
1133             /* free this pair */
1134             dde_pairs[i].server_hMem = 0;
1135             dde_num_used--;
1136             ret = dde_pairs[i].client_hMem;
1137             break;
1138         }
1139     }
1140     LeaveCriticalSection(&dde_crst);
1141     return ret;
1142 }
1143
1144 /***********************************************************************
1145  *              post_dde_message
1146  *
1147  * Post a DDE messag
1148  */
1149 static BOOL post_dde_message( DWORD dest_tid, struct packed_message *data, const struct send_message_info *info )
1150 {
1151     void*       ptr = NULL;
1152     int         size = 0;
1153     UINT        uiLo, uiHi;
1154     LPARAM      lp = 0;
1155     HGLOBAL     hunlock = 0;
1156     int         i;
1157     DWORD       res;
1158
1159     if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1160         return FALSE;
1161
1162     lp = info->lparam;
1163     switch (info->msg)
1164     {
1165         /* DDE messages which don't require packing are:
1166          * WM_DDE_INITIATE
1167          * WM_DDE_TERMINATE
1168          * WM_DDE_REQUEST
1169          * WM_DDE_UNADVISE
1170          */
1171     case WM_DDE_ACK:
1172         if (HIWORD(uiHi))
1173         {
1174             /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1175             HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1176             if (h)
1177             {
1178                 /* send back the value of h on the other side */
1179                 push_data( data, &h, sizeof(HGLOBAL) );
1180                 lp = uiLo;
1181                 TRACE( "send dde-ack %x %08x => %08lx\n", uiLo, uiHi, (DWORD)h );
1182             }
1183         }
1184         else
1185         {
1186             /* uiHi should contain either an atom or 0 */
1187             TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
1188             lp = MAKELONG( uiLo, uiHi );
1189         }
1190         break;
1191     case WM_DDE_ADVISE:
1192     case WM_DDE_DATA:
1193     case WM_DDE_POKE:
1194         size = 0;
1195         if (uiLo)
1196         {
1197             size = GlobalSize( (HGLOBAL)uiLo ) ;
1198             if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1199                 (info->msg == WM_DDE_DATA   && size < sizeof(DDEDATA))   ||
1200                 (info->msg == WM_DDE_POKE   && size < sizeof(DDEPOKE))
1201                 )
1202             return FALSE;
1203         }
1204         else if (info->msg != WM_DDE_DATA) return FALSE;
1205
1206         lp = uiHi;
1207         if (uiLo)
1208         {
1209             if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1210             {
1211                 push_data( data, ptr, size );
1212                 hunlock = (HGLOBAL)uiLo;
1213             }
1214         }
1215         TRACE( "send ddepack %u %x\n", size, uiHi );
1216         break;
1217     case WM_DDE_EXECUTE:
1218         if (info->lparam)
1219         {
1220             if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1221             {
1222                 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1223                 /* so that the other side can send it back on ACK */
1224                 lp = info->lparam;
1225                 hunlock = (HGLOBAL)info->lparam;
1226             }
1227         }
1228         break;
1229     }
1230     SERVER_START_REQ( send_message )
1231     {
1232         req->id      = dest_tid;
1233         req->type    = info->type;
1234         req->win     = info->hwnd;
1235         req->msg     = info->msg;
1236         req->wparam  = info->wparam;
1237         req->lparam  = lp;
1238         req->time    = GetCurrentTime();
1239         req->timeout = -1;
1240         for (i = 0; i < data->count; i++)
1241             wine_server_add_data( req, data->data[i], data->size[i] );
1242         if ((res = wine_server_call( req )))
1243         {
1244             if (res == STATUS_INVALID_PARAMETER)
1245                 /* FIXME: find a STATUS_ value for this one */
1246                 SetLastError( ERROR_INVALID_THREAD_ID );
1247             else
1248                 SetLastError( RtlNtStatusToDosError(res) );
1249         }
1250         else
1251             FreeDDElParam(info->msg, info->lparam);
1252     }
1253     SERVER_END_REQ;
1254     if (hunlock) GlobalUnlock(hunlock);
1255
1256     return !res;
1257 }
1258
1259 /***********************************************************************
1260  *              unpack_dde_message
1261  *
1262  * Unpack a posted DDE message received from another process.
1263  */
1264 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1265                                 void **buffer, size_t size )
1266 {
1267     UINT        uiLo, uiHi;
1268     HGLOBAL     hMem = 0;
1269     void*       ptr;
1270
1271     switch (message)
1272     {
1273     case WM_DDE_ACK:
1274         if (size)
1275         {
1276             /* hMem is being passed */
1277             if (size != sizeof(HGLOBAL)) return FALSE;
1278             if (!buffer || !*buffer) return FALSE;
1279             uiLo = *lparam;
1280             memcpy( &hMem, *buffer, size );
1281             uiHi = (UINT)hMem;
1282             TRACE("recv dde-ack %u mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1283         }
1284         else
1285         {
1286             uiLo = LOWORD( *lparam );
1287             uiHi = HIWORD( *lparam );
1288             TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
1289         }
1290         *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1291         break;
1292     case WM_DDE_ADVISE:
1293     case WM_DDE_DATA:
1294     case WM_DDE_POKE:
1295         if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1296         uiHi = *lparam;
1297         TRACE( "recv ddepack %u %x\n", size, uiHi );
1298         if (size)
1299         {
1300             hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1301             if (hMem && (ptr = GlobalLock( hMem )))
1302             {
1303                 memcpy( ptr, *buffer, size );
1304                 GlobalUnlock( hMem );
1305             }
1306             else return FALSE;
1307         }
1308         uiLo = (UINT)hMem;
1309
1310         *lparam = PackDDElParam( message, uiLo, uiHi );
1311         break;
1312     case WM_DDE_EXECUTE:
1313         if (size)
1314         {
1315             if (!buffer || !*buffer) return FALSE;
1316             hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1317             if (hMem && (ptr = GlobalLock( hMem )))
1318             {
1319                 memcpy( ptr, *buffer, size );
1320                 GlobalUnlock( hMem );
1321                 TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
1322                 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1323                 {
1324                     GlobalFree( hMem );
1325                     return FALSE;
1326                 }
1327             }
1328         } else return FALSE;
1329         *lparam = (LPARAM)hMem;
1330         break;
1331     }
1332     return TRUE;
1333 }
1334
1335 /***********************************************************************
1336  *           call_window_proc
1337  *
1338  * Call a window procedure and the corresponding hooks.
1339  */
1340 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1341                                  BOOL unicode, BOOL same_thread )
1342 {
1343     LRESULT result = 0;
1344     WNDPROC winproc;
1345     CWPSTRUCT cwp;
1346     CWPRETSTRUCT cwpret;
1347     MESSAGEQUEUE *queue = QUEUE_Current();
1348
1349     if (queue->recursion_count > MAX_SENDMSG_RECURSION) return 0;
1350     queue->recursion_count++;
1351
1352     if (msg & 0x80000000)
1353     {
1354         result = handle_internal_message( hwnd, msg, wparam, lparam );
1355         goto done;
1356     }
1357
1358     /* first the WH_CALLWNDPROC hook */
1359     hwnd = WIN_GetFullHandle( hwnd );
1360     cwp.lParam  = lparam;
1361     cwp.wParam  = wparam;
1362     cwp.message = msg;
1363     cwp.hwnd    = hwnd;
1364     HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1365
1366     /* now call the window procedure */
1367     if (unicode)
1368     {
1369         if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) goto done;
1370         result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1371     }
1372     else
1373     {
1374         if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) goto done;
1375         result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1376     }
1377
1378     /* and finally the WH_CALLWNDPROCRET hook */
1379     cwpret.lResult = result;
1380     cwpret.lParam  = lparam;
1381     cwpret.wParam  = wparam;
1382     cwpret.message = msg;
1383     cwpret.hwnd    = hwnd;
1384     HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1385  done:
1386     queue->recursion_count--;
1387     return result;
1388 }
1389
1390
1391 /***********************************************************************
1392  *           process_hardware_message
1393  *
1394  * Process a hardware message; return TRUE if message should be passed on to the app
1395  */
1396 static BOOL process_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd,
1397                                       UINT first, UINT last, BOOL remove )
1398 {
1399     BOOL ret;
1400
1401     if (!MSG_process_raw_hardware_message( msg, extra_info, hwnd, first, last, remove ))
1402         return FALSE;
1403
1404     ret = MSG_process_cooked_hardware_message( msg, extra_info, remove );
1405
1406     /* tell the server we have passed it to the app
1407      * (even though we may end up dropping it later on)
1408      */
1409     SERVER_START_REQ( reply_message )
1410     {
1411         req->type   = MSG_HARDWARE;
1412         req->result = 0;
1413         req->remove = remove || !ret;
1414         wine_server_call( req );
1415     }
1416     SERVER_END_REQ;
1417     return ret;
1418 }
1419
1420
1421 /***********************************************************************
1422  *           MSG_peek_message
1423  *
1424  * Peek for a message matching the given parameters. Return FALSE if none available.
1425  * All pending sent messages are processed before returning.
1426  */
1427 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1428 {
1429     LRESULT result;
1430     ULONG_PTR extra_info = 0;
1431     MESSAGEQUEUE *queue = QUEUE_Current();
1432     struct received_message_info info, *old_info;
1433
1434     if (!first && !last) last = ~0;
1435
1436     for (;;)
1437     {
1438         NTSTATUS res;
1439         void *buffer = NULL;
1440         size_t size = 0, buffer_size = 0;
1441
1442         do  /* loop while buffer is too small */
1443         {
1444             if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1445                 return FALSE;
1446             SERVER_START_REQ( get_message )
1447             {
1448                 req->flags     = flags;
1449                 req->get_win   = hwnd;
1450                 req->get_first = first;
1451                 req->get_last  = last;
1452                 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1453                 if (!(res = wine_server_call( req )))
1454                 {
1455                     size = wine_server_reply_size( reply );
1456                     info.type        = reply->type;
1457                     info.msg.hwnd    = reply->win;
1458                     info.msg.message = reply->msg;
1459                     info.msg.wParam  = reply->wparam;
1460                     info.msg.lParam  = reply->lparam;
1461                     info.msg.time    = reply->time;
1462                     info.msg.pt.x    = reply->x;
1463                     info.msg.pt.y    = reply->y;
1464                     extra_info       = reply->info;
1465                 }
1466                 else
1467                 {
1468                     if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1469                     buffer_size = reply->total;
1470                 }
1471             }
1472             SERVER_END_REQ;
1473         } while (res == STATUS_BUFFER_OVERFLOW);
1474
1475         if (res) return FALSE;
1476
1477         TRACE( "got type %d msg %x hwnd %p wp %x lp %lx\n",
1478                info.type, info.msg.message, info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1479
1480         switch(info.type)
1481         {
1482         case MSG_ASCII:
1483         case MSG_UNICODE:
1484             info.flags = ISMEX_SEND;
1485             break;
1486         case MSG_NOTIFY:
1487             info.flags = ISMEX_NOTIFY;
1488             break;
1489         case MSG_CALLBACK:
1490             info.flags = ISMEX_CALLBACK;
1491             break;
1492         case MSG_OTHER_PROCESS:
1493             info.flags = ISMEX_SEND;
1494             if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1495                                  &info.msg.lParam, &buffer, size ))
1496             {
1497                 ERR( "invalid packed message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1498                      info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
1499                      info.msg.wParam, info.msg.lParam, size );
1500                 /* ignore it */
1501                 reply_message( &info, 0, TRUE );
1502                 goto next;
1503             }
1504             break;
1505         case MSG_HARDWARE:
1506             if (!process_hardware_message( &info.msg, extra_info,
1507                                            hwnd, first, last, flags & GET_MSG_REMOVE ))
1508             {
1509                 TRACE("dropping msg %x\n", info.msg.message );
1510                 goto next;  /* ignore it */
1511             }
1512             queue->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
1513             /* fall through */
1514         case MSG_POSTED:
1515             queue->GetMessageExtraInfoVal = extra_info;
1516             if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
1517             {
1518                 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1519                                          &info.msg.lParam, &buffer, size ))
1520                 {
1521                     ERR( "invalid packed dde-message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1522                          info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1523                          info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
1524                     goto next;  /* ignore it */
1525                 }
1526             }
1527             *msg = info.msg;
1528             if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1529             return TRUE;
1530         }
1531
1532         /* if we get here, we have a sent message; call the window procedure */
1533         old_info = queue->receive_info;
1534         queue->receive_info = &info;
1535         result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1536                                    info.msg.lParam, (info.type != MSG_ASCII), FALSE );
1537         reply_message( &info, result, TRUE );
1538         queue->receive_info = old_info;
1539     next:
1540         if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1541     }
1542 }
1543
1544
1545 /***********************************************************************
1546  *           wait_message_reply
1547  *
1548  * Wait until a sent message gets replied to.
1549  */
1550 static void wait_message_reply( UINT flags )
1551 {
1552     MESSAGEQUEUE *queue;
1553
1554     if (!(queue = QUEUE_Current())) return;
1555
1556     for (;;)
1557     {
1558         unsigned int wake_bits = 0, changed_bits = 0;
1559         DWORD dwlc, res;
1560
1561         SERVER_START_REQ( set_queue_mask )
1562         {
1563             req->wake_mask    = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
1564             req->changed_mask = req->wake_mask;
1565             req->skip_wait    = 1;
1566             if (!wine_server_call( req ))
1567             {
1568                 wake_bits    = reply->wake_bits;
1569                 changed_bits = reply->changed_bits;
1570             }
1571         }
1572         SERVER_END_REQ;
1573
1574         if (wake_bits & QS_SMRESULT) return;  /* got a result */
1575         if (wake_bits & QS_SENDMESSAGE)
1576         {
1577             /* Process the sent message immediately */
1578             MSG msg;
1579             MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1580             continue;
1581         }
1582
1583         /* now wait for it */
1584
1585         ReleaseThunkLock( &dwlc );
1586
1587         if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1588             res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1589                                                             INFINITE, 0, 0 );
1590         else
1591             res = WaitForSingleObject( queue->server_queue, INFINITE );
1592
1593         if (dwlc) RestoreThunkLock( dwlc );
1594     }
1595 }
1596
1597 /***********************************************************************
1598  *              put_message_in_queue
1599  *
1600  * Put a sent message into the destination queue.
1601  * For inter-process message, reply_size is set to expected size of reply data.
1602  */
1603 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1604                                   size_t *reply_size )
1605 {
1606     struct packed_message data;
1607     unsigned int res;
1608     int i, timeout = -1;
1609
1610     if (info->type != MSG_NOTIFY &&
1611         info->type != MSG_CALLBACK &&
1612         info->type != MSG_POSTED &&
1613         info->timeout != INFINITE)
1614         timeout = info->timeout;
1615
1616     data.count = 0;
1617     if (info->type == MSG_OTHER_PROCESS)
1618     {
1619         *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1620         if (data.count == -1)
1621         {
1622             WARN( "cannot pack message %x\n", info->msg );
1623             return FALSE;
1624         }
1625     }
1626     else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
1627     {
1628         return post_dde_message( dest_tid, &data, info );
1629     }
1630
1631     SERVER_START_REQ( send_message )
1632     {
1633         req->id      = dest_tid;
1634         req->type    = info->type;
1635         req->win     = info->hwnd;
1636         req->msg     = info->msg;
1637         req->wparam  = info->wparam;
1638         req->lparam  = info->lparam;
1639         req->time    = GetCurrentTime();
1640         req->timeout = timeout;
1641         for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1642         if ((res = wine_server_call( req )))
1643         {
1644             if (res == STATUS_INVALID_PARAMETER)
1645                 /* FIXME: find a STATUS_ value for this one */
1646                 SetLastError( ERROR_INVALID_THREAD_ID );
1647             else
1648                 SetLastError( RtlNtStatusToDosError(res) );
1649         }
1650     }
1651     SERVER_END_REQ;
1652     return !res;
1653 }
1654
1655
1656 /***********************************************************************
1657  *              retrieve_reply
1658  *
1659  * Retrieve a message reply from the server.
1660  */
1661 static LRESULT retrieve_reply( const struct send_message_info *info,
1662                                size_t reply_size, LRESULT *result )
1663 {
1664     NTSTATUS status;
1665     void *reply_data = NULL;
1666
1667     if (reply_size)
1668     {
1669         if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
1670         {
1671             WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
1672             reply_size = 0;
1673         }
1674     }
1675     SERVER_START_REQ( get_message_reply )
1676     {
1677         req->cancel = 1;
1678         if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
1679         if (!(status = wine_server_call( req ))) *result = reply->result;
1680         reply_size = wine_server_reply_size( reply );
1681     }
1682     SERVER_END_REQ;
1683     if (!status && reply_size)
1684         unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
1685
1686     if (reply_data) HeapFree( GetProcessHeap(), 0, reply_data );
1687
1688     TRACE( "hwnd %p msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1689            info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
1690            info->lparam, *result, status );
1691
1692     if (!status) return 1;
1693     if (status == STATUS_TIMEOUT) SetLastError(0);  /* timeout */
1694     else SetLastError( RtlNtStatusToDosError(status) );
1695     return 0;
1696 }
1697
1698
1699 /***********************************************************************
1700  *              send_inter_thread_message
1701  */
1702 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1703                                           LRESULT *res_ptr )
1704 {
1705     LRESULT ret;
1706     int locks;
1707     size_t reply_size = 0;
1708
1709     TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
1710            info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
1711
1712     if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1713
1714     /* there's no reply to wait for on notify/callback messages */
1715     if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1716
1717     locks = WIN_SuspendWndsLock();
1718
1719     wait_message_reply( info->flags );
1720     ret = retrieve_reply( info, reply_size, res_ptr );
1721
1722     WIN_RestoreWndsLock( locks );
1723     return ret;
1724 }
1725
1726
1727 /***********************************************************************
1728  *              SendMessageTimeoutW  (USER32.@)
1729  */
1730 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1731                                     UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1732 {
1733     struct send_message_info info;
1734     DWORD dest_tid, dest_pid;
1735     LRESULT ret, result;
1736
1737     info.type    = MSG_UNICODE;
1738     info.hwnd    = hwnd;
1739     info.msg     = msg;
1740     info.wparam  = wparam;
1741     info.lparam  = lparam;
1742     info.flags   = flags;
1743     info.timeout = timeout;
1744
1745     if (is_broadcast(hwnd))
1746     {
1747         EnumWindows( broadcast_message_callback, (LPARAM)&info );
1748         if (res_ptr) *res_ptr = 1;
1749         return 1;
1750     }
1751
1752     if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1753
1754     if (USER_IsExitingThread( dest_tid )) return 0;
1755
1756     SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1757
1758     if (dest_tid == GetCurrentThreadId())
1759     {
1760         result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1761         ret = 1;
1762     }
1763     else
1764     {
1765         if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1766         ret = send_inter_thread_message( dest_tid, &info, &result );
1767     }
1768
1769     SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1770     if (ret && res_ptr) *res_ptr = result;
1771     return ret;
1772 }
1773
1774
1775 /***********************************************************************
1776  *              SendMessageTimeoutA  (USER32.@)
1777  */
1778 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1779                                     UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1780 {
1781     struct send_message_info info;
1782     DWORD dest_tid, dest_pid;
1783     LRESULT ret, result;
1784
1785     info.type    = MSG_ASCII;
1786     info.hwnd    = hwnd;
1787     info.msg     = msg;
1788     info.wparam  = wparam;
1789     info.lparam  = lparam;
1790     info.flags   = flags;
1791     info.timeout = timeout;
1792
1793     if (is_broadcast(hwnd))
1794     {
1795         EnumWindows( broadcast_message_callback, (LPARAM)&info );
1796         if (res_ptr) *res_ptr = 1;
1797         return 1;
1798     }
1799
1800     if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1801
1802     if (USER_IsExitingThread( dest_tid )) return 0;
1803
1804     SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1805
1806     if (dest_tid == GetCurrentThreadId())
1807     {
1808         result = call_window_proc( hwnd, msg, wparam, lparam, FALSE, TRUE );
1809         ret = 1;
1810     }
1811     else if (dest_pid == GetCurrentProcessId())
1812     {
1813         ret = send_inter_thread_message( dest_tid, &info, &result );
1814     }
1815     else
1816     {
1817         /* inter-process message: need to map to Unicode */
1818         info.type = MSG_OTHER_PROCESS;
1819         if (is_unicode_message( info.msg ))
1820         {
1821             if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1822                 return 0;
1823             ret = send_inter_thread_message( dest_tid, &info, &result );
1824             result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1825                                                info.lparam, result );
1826         }
1827         else ret = send_inter_thread_message( dest_tid, &info, &result );
1828     }
1829     SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1830     if (ret && res_ptr) *res_ptr = result;
1831     return ret;
1832 }
1833
1834
1835 /***********************************************************************
1836  *              SendMessageW  (USER32.@)
1837  */
1838 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1839 {
1840     LRESULT res = 0;
1841     SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1842     return res;
1843 }
1844
1845
1846 /***********************************************************************
1847  *              SendMessageA  (USER32.@)
1848  */
1849 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1850 {
1851     LRESULT res = 0;
1852     SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1853     return res;
1854 }
1855
1856
1857 /***********************************************************************
1858  *              SendNotifyMessageA  (USER32.@)
1859  */
1860 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1861 {
1862     return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1863 }
1864
1865
1866 /***********************************************************************
1867  *              SendNotifyMessageW  (USER32.@)
1868  */
1869 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1870 {
1871     struct send_message_info info;
1872     DWORD dest_tid;
1873     LRESULT result;
1874
1875     if (is_pointer_message(msg))
1876     {
1877         SetLastError(ERROR_INVALID_PARAMETER);
1878         return FALSE;
1879     }
1880
1881     info.type    = MSG_NOTIFY;
1882     info.hwnd    = hwnd;
1883     info.msg     = msg;
1884     info.wparam  = wparam;
1885     info.lparam  = lparam;
1886
1887     if (is_broadcast(hwnd))
1888     {
1889         EnumWindows( broadcast_message_callback, (LPARAM)&info );
1890         return TRUE;
1891     }
1892
1893     if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1894
1895     if (USER_IsExitingThread( dest_tid )) return TRUE;
1896
1897     if (dest_tid == GetCurrentThreadId())
1898     {
1899         call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1900         return TRUE;
1901     }
1902     return send_inter_thread_message( dest_tid, &info, &result );
1903 }
1904
1905
1906 /***********************************************************************
1907  *              SendMessageCallbackA  (USER32.@)
1908  */
1909 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1910                                   SENDASYNCPROC callback, ULONG_PTR data )
1911 {
1912     return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
1913                                  lparam, callback, data );
1914 }
1915
1916
1917 /***********************************************************************
1918  *              SendMessageCallbackW  (USER32.@)
1919  */
1920 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1921                                   SENDASYNCPROC callback, ULONG_PTR data )
1922 {
1923     struct send_message_info info;
1924     LRESULT result;
1925     DWORD dest_tid;
1926
1927     if (is_pointer_message(msg))
1928     {
1929         SetLastError(ERROR_INVALID_PARAMETER);
1930         return FALSE;
1931     }
1932
1933     info.type     = MSG_CALLBACK;
1934     info.hwnd     = hwnd;
1935     info.msg      = msg;
1936     info.wparam   = wparam;
1937     info.lparam   = lparam;
1938     info.callback = callback;
1939     info.data     = data;
1940
1941     if (is_broadcast(hwnd))
1942     {
1943         EnumWindows( broadcast_message_callback, (LPARAM)&info );
1944         return TRUE;
1945     }
1946
1947     if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1948
1949     if (USER_IsExitingThread( dest_tid )) return TRUE;
1950
1951     if (dest_tid == GetCurrentThreadId())
1952     {
1953         result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1954         callback( hwnd, msg, data, result );
1955         return TRUE;
1956     }
1957     FIXME( "callback will not be called\n" );
1958     return send_inter_thread_message( dest_tid, &info, &result );
1959 }
1960
1961
1962 /***********************************************************************
1963  *              ReplyMessage  (USER32.@)
1964  */
1965 BOOL WINAPI ReplyMessage( LRESULT result )
1966 {
1967     MESSAGEQUEUE *queue = QUEUE_Current();
1968     struct received_message_info *info = queue->receive_info;
1969
1970     if (!info) return FALSE;
1971     reply_message( info, result, FALSE );
1972     return TRUE;
1973 }
1974
1975
1976 /***********************************************************************
1977  *              InSendMessage  (USER32.@)
1978  */
1979 BOOL WINAPI InSendMessage(void)
1980 {
1981     return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
1982 }
1983
1984
1985 /***********************************************************************
1986  *              InSendMessageEx  (USER32.@)
1987  */
1988 DWORD WINAPI InSendMessageEx( LPVOID reserved )
1989 {
1990     MESSAGEQUEUE *queue = QUEUE_Current();
1991     struct received_message_info *info = queue->receive_info;
1992
1993     if (info) return info->flags;
1994     return ISMEX_NOSEND;
1995 }
1996
1997
1998 /***********************************************************************
1999  *              PostMessageA  (USER32.@)
2000  */
2001 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2002 {
2003     return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
2004 }
2005
2006
2007 /***********************************************************************
2008  *              PostMessageW  (USER32.@)
2009  */
2010 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2011 {
2012     struct send_message_info info;
2013     DWORD dest_tid;
2014
2015     if (is_pointer_message( msg ))
2016     {
2017         SetLastError( ERROR_INVALID_PARAMETER );
2018         return FALSE;
2019     }
2020
2021     info.type   = MSG_POSTED;
2022     info.hwnd   = hwnd;
2023     info.msg    = msg;
2024     info.wparam = wparam;
2025     info.lparam = lparam;
2026
2027     if (is_broadcast(hwnd))
2028     {
2029         EnumWindows( broadcast_message_callback, (LPARAM)&info );
2030         return TRUE;
2031     }
2032
2033     if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2034
2035     if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2036
2037     if (USER_IsExitingThread( dest_tid )) return TRUE;
2038
2039     return put_message_in_queue( dest_tid, &info, NULL );
2040 }
2041
2042
2043 /**********************************************************************
2044  *              PostThreadMessageA  (USER32.@)
2045  */
2046 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2047 {
2048     return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2049 }
2050
2051
2052 /**********************************************************************
2053  *              PostThreadMessageW  (USER32.@)
2054  */
2055 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2056 {
2057     struct send_message_info info;
2058
2059     if (is_pointer_message( msg ))
2060     {
2061         SetLastError( ERROR_INVALID_PARAMETER );
2062         return FALSE;
2063     }
2064     if (USER_IsExitingThread( thread )) return TRUE;
2065
2066     info.type   = MSG_POSTED;
2067     info.hwnd   = 0;
2068     info.msg    = msg;
2069     info.wparam = wparam;
2070     info.lparam = lparam;
2071     return put_message_in_queue( thread, &info, NULL );
2072 }
2073
2074
2075 /***********************************************************************
2076  *              PostQuitMessage  (USER32.@)
2077  */
2078 void WINAPI PostQuitMessage( INT exitCode )
2079 {
2080     PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
2081 }
2082
2083
2084 /***********************************************************************
2085  *              PeekMessageW  (USER32.@)
2086  */
2087 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2088 {
2089     MESSAGEQUEUE *queue;
2090     MSG msg;
2091     int locks;
2092
2093     /* check for graphics events */
2094     if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2095         USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
2096
2097     hwnd = WIN_GetFullHandle( hwnd );
2098     locks = WIN_SuspendWndsLock();
2099
2100     if (!MSG_peek_message( &msg, hwnd, first, last,
2101                            (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2102     {
2103         if (!(flags & PM_NOYIELD))
2104         {
2105             DWORD count;
2106             ReleaseThunkLock(&count);
2107             if (count) RestoreThunkLock(count);
2108         }
2109         WIN_RestoreWndsLock( locks );
2110         return FALSE;
2111     }
2112
2113     WIN_RestoreWndsLock( locks );
2114
2115     /* need to fill the window handle for WM_PAINT message */
2116     if (msg.message == WM_PAINT)
2117     {
2118         if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
2119         {
2120             msg.message = WM_PAINTICON;
2121             msg.wParam = 1;
2122         }
2123         /* clear internal paint flag */
2124         RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
2125     }
2126
2127     if ((queue = QUEUE_Current()))
2128     {
2129         queue->GetMessageTimeVal = msg.time;
2130         msg.pt.x = LOWORD( queue->GetMessagePosVal );
2131         msg.pt.y = HIWORD( queue->GetMessagePosVal );
2132     }
2133
2134     HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
2135
2136     /* copy back our internal safe copy of message data to msg_out.
2137      * msg_out is a variable from the *program*, so it can't be used
2138      * internally as it can get "corrupted" by our use of SendMessage()
2139      * (back to the program) inside the message handling itself. */
2140     *msg_out = msg;
2141     return TRUE;
2142 }
2143
2144
2145 /***********************************************************************
2146  *              PeekMessageA  (USER32.@)
2147  */
2148 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2149 {
2150     BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2151     if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2152     return ret;
2153 }
2154
2155
2156 /***********************************************************************
2157  *              GetMessageW  (USER32.@)
2158  */
2159 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2160 {
2161     MESSAGEQUEUE *queue = QUEUE_Current();
2162     int mask, locks;
2163
2164     mask = QS_POSTMESSAGE | QS_SENDMESSAGE;  /* Always selected */
2165     if (first || last)
2166     {
2167         if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2168         if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2169              ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2170         if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2171         if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2172         if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2173     }
2174     else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2175
2176     locks = WIN_SuspendWndsLock();
2177
2178     while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2179     {
2180         /* wait until one of the bits is set */
2181         unsigned int wake_bits = 0, changed_bits = 0;
2182         DWORD dwlc;
2183
2184         SERVER_START_REQ( set_queue_mask )
2185         {
2186             req->wake_mask    = QS_SENDMESSAGE;
2187             req->changed_mask = mask;
2188             req->skip_wait    = 1;
2189             if (!wine_server_call( req ))
2190             {
2191                 wake_bits    = reply->wake_bits;
2192                 changed_bits = reply->changed_bits;
2193             }
2194         }
2195         SERVER_END_REQ;
2196
2197         if (changed_bits & mask) continue;
2198         if (wake_bits & QS_SENDMESSAGE) continue;
2199
2200         TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2201                queue->self, mask, wake_bits, changed_bits );
2202
2203         ReleaseThunkLock( &dwlc );
2204         if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2205             USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
2206         else
2207             WaitForSingleObject( queue->server_queue, INFINITE );
2208         if (dwlc) RestoreThunkLock( dwlc );
2209     }
2210
2211     WIN_RestoreWndsLock( locks );
2212
2213     return (msg->message != WM_QUIT);
2214 }
2215
2216
2217 /***********************************************************************
2218  *              GetMessageA  (USER32.@)
2219  */
2220 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2221 {
2222     GetMessageW( msg, hwnd, first, last );
2223     msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2224     return (msg->message != WM_QUIT);
2225 }
2226
2227
2228 /***********************************************************************
2229  *              IsDialogMessage  (USER32.@)
2230  *              IsDialogMessageA (USER32.@)
2231  */
2232 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2233 {
2234     MSG msg = *pmsg;
2235     msg.wParam = map_wparam_AtoW( msg.message, msg.wParam );
2236     return IsDialogMessageW( hwndDlg, &msg );
2237 }
2238
2239
2240 /***********************************************************************
2241  *              SetMessageQueue (USER32.@)
2242  */
2243 BOOL WINAPI SetMessageQueue( INT size )
2244 {
2245     /* now obsolete the message queue will be expanded dynamically as necessary */
2246     return TRUE;
2247 }
2248
2249
2250 /**********************************************************************
2251  *              AttachThreadInput (USER32.@)
2252  *
2253  * Attaches the input processing mechanism of one thread to that of
2254  * another thread.
2255  */
2256 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
2257 {
2258     BOOL ret;
2259
2260     SERVER_START_REQ( attach_thread_input )
2261     {
2262         req->tid_from = from;
2263         req->tid_to   = to;
2264         req->attach   = attach;
2265         ret = !wine_server_call_err( req );
2266     }
2267     SERVER_END_REQ;
2268     return ret;
2269 }
2270
2271
2272 /**********************************************************************
2273  *              GetGUIThreadInfo  (USER32.@)
2274  */
2275 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
2276 {
2277     BOOL ret;
2278
2279     SERVER_START_REQ( get_thread_input )
2280     {
2281         req->tid = id;
2282         if ((ret = !wine_server_call_err( req )))
2283         {
2284             info->flags          = 0;
2285             info->hwndActive     = reply->active;
2286             info->hwndFocus      = reply->focus;
2287             info->hwndCapture    = reply->capture;
2288             info->hwndMenuOwner  = reply->menu_owner;
2289             info->hwndMoveSize   = reply->move_size;
2290             info->hwndCaret      = reply->caret;
2291             info->rcCaret.left   = reply->rect.left;
2292             info->rcCaret.top    = reply->rect.top;
2293             info->rcCaret.right  = reply->rect.right;
2294             info->rcCaret.bottom = reply->rect.bottom;
2295             if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
2296             if (reply->move_size) info->flags |= GUI_INMOVESIZE;
2297             if (reply->caret) info->flags |= GUI_CARETBLINKING;
2298         }
2299     }
2300     SERVER_END_REQ;
2301     return ret;
2302 }
2303
2304
2305 /**********************************************************************
2306  *              GetKeyState (USER32.@)
2307  *
2308  * An application calls the GetKeyState function in response to a
2309  * keyboard-input message.  This function retrieves the state of the key
2310  * at the time the input message was generated.
2311  */
2312 SHORT WINAPI GetKeyState(INT vkey)
2313 {
2314     SHORT retval = 0;
2315
2316     SERVER_START_REQ( get_key_state )
2317     {
2318         req->tid = GetCurrentThreadId();
2319         req->key = vkey;
2320         if (!wine_server_call( req )) retval = (signed char)reply->state;
2321     }
2322     SERVER_END_REQ;
2323     TRACE("key (0x%x) -> %x\n", vkey, retval);
2324     return retval;
2325 }
2326
2327
2328 /**********************************************************************
2329  *              GetKeyboardState (USER32.@)
2330  */
2331 BOOL WINAPI GetKeyboardState( LPBYTE state )
2332 {
2333     BOOL ret;
2334
2335     TRACE("(%p)\n", state);
2336
2337     memset( state, 0, 256 );
2338     SERVER_START_REQ( get_key_state )
2339     {
2340         req->tid = GetCurrentThreadId();
2341         req->key = -1;
2342         wine_server_set_reply( req, state, 256 );
2343         ret = !wine_server_call_err( req );
2344     }
2345     SERVER_END_REQ;
2346     return ret;
2347 }
2348
2349
2350 /**********************************************************************
2351  *              SetKeyboardState (USER32.@)
2352  */
2353 BOOL WINAPI SetKeyboardState( LPBYTE state )
2354 {
2355     BOOL ret;
2356
2357     TRACE("(%p)\n", state);
2358
2359     SERVER_START_REQ( set_key_state )
2360     {
2361         req->tid = GetCurrentThreadId();
2362         wine_server_add_data( req, state, 256 );
2363         ret = !wine_server_call_err( req );
2364     }
2365     SERVER_END_REQ;
2366     return ret;
2367 }