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