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