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