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