hlink/tests: Add a trailing '\n' to an ok() call.
[wine] / dlls / user32 / message.c
1 /*
2  * Window messaging support
3  *
4  * Copyright 2001 Alexandre Julliard
5  * Copyright 2008 Maarten Lankhorst
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winerror.h"
35 #include "winnls.h"
36 #include "dbt.h"
37 #include "dde.h"
38 #include "imm.h"
39 #include "ddk/imm.h"
40 #include "wine/unicode.h"
41 #include "wine/server.h"
42 #include "user_private.h"
43 #include "win.h"
44 #include "controls.h"
45 #include "wine/debug.h"
46 #include "wine/exception.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(msg);
49 WINE_DECLARE_DEBUG_CHANNEL(relay);
50 WINE_DECLARE_DEBUG_CHANNEL(key);
51
52 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
53 #define WM_NCMOUSELAST  (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
54
55 #define MAX_PACK_COUNT 4
56
57 #define SYS_TIMER_RATE  55   /* min. timer rate in ms (actually 54.925)*/
58
59 /* description of the data fields that need to be packed along with a sent message */
60 struct packed_message
61 {
62     int         count;
63     const void *data[MAX_PACK_COUNT];
64     size_t      size[MAX_PACK_COUNT];
65 };
66
67 /* info about the message currently being received by the current thread */
68 struct received_message_info
69 {
70     enum message_type type;
71     MSG               msg;
72     UINT              flags;  /* InSendMessageEx return flags */
73 };
74
75 /* structure to group all parameters for sent messages of the various kinds */
76 struct send_message_info
77 {
78     enum message_type type;
79     DWORD             dest_tid;
80     HWND              hwnd;
81     UINT              msg;
82     WPARAM            wparam;
83     LPARAM            lparam;
84     UINT              flags;      /* flags for SendMessageTimeout */
85     UINT              timeout;    /* timeout for SendMessageTimeout */
86     SENDASYNCPROC     callback;   /* callback function for SendMessageCallback */
87     ULONG_PTR         data;       /* callback data */
88     enum wm_char_mapping wm_char;
89 };
90
91
92 /* Message class descriptor */
93 static const WCHAR messageW[] = {'M','e','s','s','a','g','e',0};
94
95 const struct builtin_class_descr MESSAGE_builtin_class =
96 {
97     messageW,             /* name */
98     0,                    /* style */
99     WINPROC_MESSAGE,      /* proc */
100     0,                    /* extra */
101     IDC_ARROW,            /* cursor */
102     0                     /* brush */
103 };
104
105
106
107 /* flag for messages that contain pointers */
108 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
109
110 #define SET(msg) (1 << ((msg) & 31))
111
112 static const unsigned int message_pointer_flags[] =
113 {
114     /* 0x00 - 0x1f */
115     SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
116     SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
117     /* 0x20 - 0x3f */
118     SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
119     SET(WM_COMPAREITEM),
120     /* 0x40 - 0x5f */
121     SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
122     SET(WM_NOTIFY) | SET(WM_HELP),
123     /* 0x60 - 0x7f */
124     SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
125     /* 0x80 - 0x9f */
126     SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
127     /* 0xa0 - 0xbf */
128     SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
129     /* 0xc0 - 0xdf */
130     SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
131     /* 0xe0 - 0xff */
132     SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO) | SET(SBM_GETSCROLLBARINFO),
133     /* 0x100 - 0x11f */
134     0,
135     /* 0x120 - 0x13f */
136     0,
137     /* 0x140 - 0x15f */
138     SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
139     SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
140     SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
141     /* 0x160 - 0x17f */
142     0,
143     /* 0x180 - 0x19f */
144     SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
145     SET(LB_DIR) | SET(LB_FINDSTRING) |
146     SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
147     /* 0x1a0 - 0x1bf */
148     SET(LB_FINDSTRINGEXACT),
149     /* 0x1c0 - 0x1df */
150     0,
151     /* 0x1e0 - 0x1ff */
152     0,
153     /* 0x200 - 0x21f */
154     SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
155     /* 0x220 - 0x23f */
156     SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
157     SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
158     /* 0x240 - 0x25f */
159     0,
160     /* 0x260 - 0x27f */
161     0,
162     /* 0x280 - 0x29f */
163     0,
164     /* 0x2a0 - 0x2bf */
165     0,
166     /* 0x2c0 - 0x2df */
167     0,
168     /* 0x2e0 - 0x2ff */
169     0,
170     /* 0x300 - 0x31f */
171     SET(WM_ASKCBFORMATNAME)
172 };
173
174 /* flags for messages that contain Unicode strings */
175 static const unsigned int message_unicode_flags[] =
176 {
177     /* 0x00 - 0x1f */
178     SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
179     SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
180     /* 0x20 - 0x3f */
181     SET(WM_CHARTOITEM),
182     /* 0x40 - 0x5f */
183     0,
184     /* 0x60 - 0x7f */
185     0,
186     /* 0x80 - 0x9f */
187     SET(WM_NCCREATE),
188     /* 0xa0 - 0xbf */
189     0,
190     /* 0xc0 - 0xdf */
191     SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
192     /* 0xe0 - 0xff */
193     0,
194     /* 0x100 - 0x11f */
195     SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
196     /* 0x120 - 0x13f */
197     SET(WM_MENUCHAR),
198     /* 0x140 - 0x15f */
199     SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
200     SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
201     /* 0x160 - 0x17f */
202     0,
203     /* 0x180 - 0x19f */
204     SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
205     SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
206     /* 0x1a0 - 0x1bf */
207     SET(LB_FINDSTRINGEXACT),
208     /* 0x1c0 - 0x1df */
209     0,
210     /* 0x1e0 - 0x1ff */
211     0,
212     /* 0x200 - 0x21f */
213     0,
214     /* 0x220 - 0x23f */
215     SET(WM_MDICREATE),
216     /* 0x240 - 0x25f */
217     0,
218     /* 0x260 - 0x27f */
219     0,
220     /* 0x280 - 0x29f */
221     SET(WM_IME_CHAR),
222     /* 0x2a0 - 0x2bf */
223     0,
224     /* 0x2c0 - 0x2df */
225     0,
226     /* 0x2e0 - 0x2ff */
227     0,
228     /* 0x300 - 0x31f */
229     SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
230 };
231
232 /* check whether a given message type includes pointers */
233 static inline int is_pointer_message( UINT message )
234 {
235     if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
236     return (message_pointer_flags[message / 32] & SET(message)) != 0;
237 }
238
239 /* check whether a given message type contains Unicode (or ASCII) chars */
240 static inline int is_unicode_message( UINT message )
241 {
242     if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
243     return (message_unicode_flags[message / 32] & SET(message)) != 0;
244 }
245
246 #undef SET
247
248 /* add a data field to a packed message */
249 static inline void push_data( struct packed_message *data, const void *ptr, size_t size )
250 {
251     data->data[data->count] = ptr;
252     data->size[data->count] = size;
253     data->count++;
254 }
255
256 /* add a string to a packed message */
257 static inline void push_string( struct packed_message *data, LPCWSTR str )
258 {
259     push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
260 }
261
262 /* make sure that the buffer contains a valid null-terminated Unicode string */
263 static inline BOOL check_string( LPCWSTR str, size_t size )
264 {
265     for (size /= sizeof(WCHAR); size; size--, str++)
266         if (!*str) return TRUE;
267     return FALSE;
268 }
269
270 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
271 static inline void *get_buffer_space( void **buffer, size_t size )
272 {
273     void *ret;
274
275     if (*buffer)
276     {
277         if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
278             HeapFree( GetProcessHeap(), 0, *buffer );
279     }
280     else ret = HeapAlloc( GetProcessHeap(), 0, size );
281
282     *buffer = ret;
283     return ret;
284 }
285
286 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
287 static inline BOOL combobox_has_strings( HWND hwnd )
288 {
289     DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
290     return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
291 }
292
293 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
294 static inline BOOL listbox_has_strings( HWND hwnd )
295 {
296     DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
297     return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
298 }
299
300 /* check whether message is in the range of keyboard messages */
301 static inline BOOL is_keyboard_message( UINT message )
302 {
303     return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
304 }
305
306 /* check whether message is in the range of mouse messages */
307 static inline BOOL is_mouse_message( UINT message )
308 {
309     return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
310             (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
311 }
312
313 /* check whether message matches the specified hwnd filter */
314 static inline BOOL check_hwnd_filter( const MSG *msg, HWND hwnd_filter )
315 {
316     if (!hwnd_filter || hwnd_filter == GetDesktopWindow()) return TRUE;
317     return (msg->hwnd == hwnd_filter || IsChild( hwnd_filter, msg->hwnd ));
318 }
319
320 /* check for pending WM_CHAR message with DBCS trailing byte */
321 static inline BOOL get_pending_wmchar( MSG *msg, UINT first, UINT last, BOOL remove )
322 {
323     struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
324
325     if (!data || !data->get_msg.message) return FALSE;
326     if ((first || last) && (first > WM_CHAR || last < WM_CHAR)) return FALSE;
327     if (!msg) return FALSE;
328     *msg = data->get_msg;
329     if (remove) data->get_msg.message = 0;
330     return TRUE;
331 }
332
333
334 /***********************************************************************
335  *           MessageWndProc
336  *
337  * Window procedure for "Message" windows (HWND_MESSAGE parent).
338  */
339 LRESULT WINAPI MessageWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
340 {
341     if (message == WM_NCCREATE) return TRUE;
342     return 0;  /* all other messages are ignored */
343 }
344
345
346 /***********************************************************************
347  *              broadcast_message_callback
348  *
349  * Helper callback for broadcasting messages.
350  */
351 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
352 {
353     struct send_message_info *info = (struct send_message_info *)lparam;
354     if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
355     switch(info->type)
356     {
357     case MSG_UNICODE:
358         SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
359                              info->flags, info->timeout, NULL );
360         break;
361     case MSG_ASCII:
362         SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
363                              info->flags, info->timeout, NULL );
364         break;
365     case MSG_NOTIFY:
366         SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
367         break;
368     case MSG_CALLBACK:
369         SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
370                               info->callback, info->data );
371         break;
372     case MSG_POSTED:
373         PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
374         break;
375     default:
376         ERR( "bad type %d\n", info->type );
377         break;
378     }
379     return TRUE;
380 }
381
382
383 /***********************************************************************
384  *              map_wparam_AtoW
385  *
386  * Convert the wparam of an ASCII message to Unicode.
387  */
388 BOOL map_wparam_AtoW( UINT message, WPARAM *wparam, enum wm_char_mapping mapping )
389 {
390     char ch[2];
391     WCHAR wch[2];
392
393     wch[0] = wch[1] = 0;
394     switch(message)
395     {
396     case WM_CHAR:
397         /* WM_CHAR is magic: a DBCS char can be sent/posted as two consecutive WM_CHAR
398          * messages, in which case the first char is stored, and the conversion
399          * to Unicode only takes place once the second char is sent/posted.
400          */
401         if (mapping != WMCHAR_MAP_NOMAPPING)
402         {
403             struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
404             BYTE low = LOBYTE(*wparam);
405
406             if (HIBYTE(*wparam))
407             {
408                 ch[0] = low;
409                 ch[1] = HIBYTE(*wparam);
410                 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
411                 TRACE( "map %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
412                 if (data) data->lead_byte[mapping] = 0;
413             }
414             else if (data && data->lead_byte[mapping])
415             {
416                 ch[0] = data->lead_byte[mapping];
417                 ch[1] = low;
418                 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
419                 TRACE( "map stored %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
420                 data->lead_byte[mapping] = 0;
421             }
422             else if (!IsDBCSLeadByte( low ))
423             {
424                 ch[0] = low;
425                 RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 1 );
426                 TRACE( "map %02x -> %04x\n", (BYTE)ch[0], wch[0] );
427                 if (data) data->lead_byte[mapping] = 0;
428             }
429             else  /* store it and wait for trail byte */
430             {
431                 if (!data)
432                 {
433                     if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) )))
434                         return FALSE;
435                     get_user_thread_info()->wmchar_data = data;
436                 }
437                 TRACE( "storing lead byte %02x mapping %u\n", low, mapping );
438                 data->lead_byte[mapping] = low;
439                 return FALSE;
440             }
441             *wparam = MAKEWPARAM(wch[0], wch[1]);
442             break;
443         }
444         /* else fall through */
445     case WM_CHARTOITEM:
446     case EM_SETPASSWORDCHAR:
447     case WM_DEADCHAR:
448     case WM_SYSCHAR:
449     case WM_SYSDEADCHAR:
450     case WM_MENUCHAR:
451         ch[0] = LOBYTE(*wparam);
452         ch[1] = HIBYTE(*wparam);
453         RtlMultiByteToUnicodeN( wch, sizeof(wch), NULL, ch, 2 );
454         *wparam = MAKEWPARAM(wch[0], wch[1]);
455         break;
456     case WM_IME_CHAR:
457         ch[0] = HIBYTE(*wparam);
458         ch[1] = LOBYTE(*wparam);
459         if (ch[0]) RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch, 2 );
460         else RtlMultiByteToUnicodeN( wch, sizeof(wch[0]), NULL, ch + 1, 1 );
461         *wparam = MAKEWPARAM(wch[0], HIWORD(*wparam));
462         break;
463     }
464     return TRUE;
465 }
466
467
468 /***********************************************************************
469  *              map_wparam_WtoA
470  *
471  * Convert the wparam of a Unicode message to ASCII.
472  */
473 static void map_wparam_WtoA( MSG *msg, BOOL remove )
474 {
475     BYTE ch[2];
476     WCHAR wch[2];
477     DWORD len;
478
479     switch(msg->message)
480     {
481     case WM_CHAR:
482         if (!HIWORD(msg->wParam))
483         {
484             wch[0] = LOWORD(msg->wParam);
485             ch[0] = ch[1] = 0;
486             RtlUnicodeToMultiByteN( (LPSTR)ch, 2, &len, wch, sizeof(wch[0]) );
487             if (len == 2)  /* DBCS char */
488             {
489                 struct wm_char_mapping_data *data = get_user_thread_info()->wmchar_data;
490                 if (!data)
491                 {
492                     if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) ))) return;
493                     get_user_thread_info()->wmchar_data = data;
494                 }
495                 if (remove)
496                 {
497                     data->get_msg = *msg;
498                     data->get_msg.wParam = ch[1];
499                 }
500                 msg->wParam = ch[0];
501                 return;
502             }
503         }
504         /* else fall through */
505     case WM_CHARTOITEM:
506     case EM_SETPASSWORDCHAR:
507     case WM_DEADCHAR:
508     case WM_SYSCHAR:
509     case WM_SYSDEADCHAR:
510     case WM_MENUCHAR:
511         wch[0] = LOWORD(msg->wParam);
512         wch[1] = HIWORD(msg->wParam);
513         ch[0] = ch[1] = 0;
514         RtlUnicodeToMultiByteN( (LPSTR)ch, 2, NULL, wch, sizeof(wch) );
515         msg->wParam = MAKEWPARAM( ch[0] | (ch[1] << 8), 0 );
516         break;
517     case WM_IME_CHAR:
518         wch[0] = LOWORD(msg->wParam);
519         ch[0] = ch[1] = 0;
520         RtlUnicodeToMultiByteN( (LPSTR)ch, 2, &len, wch, sizeof(wch[0]) );
521         if (len == 2)
522             msg->wParam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(msg->wParam) );
523         else
524             msg->wParam = MAKEWPARAM( ch[0], HIWORD(msg->wParam) );
525         break;
526     }
527 }
528
529
530 /***********************************************************************
531  *              pack_message
532  *
533  * Pack a message for sending to another process.
534  * Return the size of the data we expect in the message reply.
535  * Set data->count to -1 if there is an error.
536  */
537 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
538                             struct packed_message *data )
539 {
540     data->count = 0;
541     switch(message)
542     {
543     case WM_NCCREATE:
544     case WM_CREATE:
545     {
546         CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
547         push_data( data, cs, sizeof(*cs) );
548         if (!IS_INTRESOURCE(cs->lpszName)) push_string( data, cs->lpszName );
549         if (!IS_INTRESOURCE(cs->lpszClass)) push_string( data, cs->lpszClass );
550         return sizeof(*cs);
551     }
552     case WM_GETTEXT:
553     case WM_ASKCBFORMATNAME:
554         return wparam * sizeof(WCHAR);
555     case WM_WININICHANGE:
556         if (lparam) push_string(data, (LPWSTR)lparam );
557         return 0;
558     case WM_SETTEXT:
559     case WM_DEVMODECHANGE:
560     case CB_DIR:
561     case LB_DIR:
562     case LB_ADDFILE:
563     case EM_REPLACESEL:
564         push_string( data, (LPWSTR)lparam );
565         return 0;
566     case WM_GETMINMAXINFO:
567         push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
568         return sizeof(MINMAXINFO);
569     case WM_DRAWITEM:
570         push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
571         return 0;
572     case WM_MEASUREITEM:
573         push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
574         return sizeof(MEASUREITEMSTRUCT);
575     case WM_DELETEITEM:
576         push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
577         return 0;
578     case WM_COMPAREITEM:
579         push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
580         return 0;
581     case WM_WINDOWPOSCHANGING:
582     case WM_WINDOWPOSCHANGED:
583         push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
584         return sizeof(WINDOWPOS);
585     case WM_COPYDATA:
586     {
587         COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
588         push_data( data, cp, sizeof(*cp) );
589         if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
590         return 0;
591     }
592     case WM_NOTIFY:
593         /* WM_NOTIFY cannot be sent across processes (MSDN) */
594         data->count = -1;
595         return 0;
596     case WM_HELP:
597         push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
598         return 0;
599     case WM_STYLECHANGING:
600     case WM_STYLECHANGED:
601         push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
602         return 0;
603     case WM_NCCALCSIZE:
604         if (!wparam)
605         {
606             push_data( data, (RECT *)lparam, sizeof(RECT) );
607             return sizeof(RECT);
608         }
609         else
610         {
611             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
612             push_data( data, nc, sizeof(*nc) );
613             push_data( data, nc->lppos, sizeof(*nc->lppos) );
614             return sizeof(*nc) + sizeof(*nc->lppos);
615         }
616     case WM_GETDLGCODE:
617         if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
618         return sizeof(MSG);
619     case SBM_SETSCROLLINFO:
620         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
621         return 0;
622     case SBM_GETSCROLLINFO:
623         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
624         return sizeof(SCROLLINFO);
625     case SBM_GETSCROLLBARINFO:
626     {
627         const SCROLLBARINFO *info = (const SCROLLBARINFO *)lparam;
628         size_t size = min( info->cbSize, sizeof(SCROLLBARINFO) );
629         push_data( data, info, size );
630         return size;
631     }
632     case EM_GETSEL:
633     case SBM_GETRANGE:
634     case CB_GETEDITSEL:
635     {
636         size_t size = 0;
637         if (wparam) size += sizeof(DWORD);
638         if (lparam) size += sizeof(DWORD);
639         return size;
640     }
641     case EM_GETRECT:
642     case LB_GETITEMRECT:
643     case CB_GETDROPPEDCONTROLRECT:
644         return sizeof(RECT);
645     case EM_SETRECT:
646     case EM_SETRECTNP:
647         push_data( data, (RECT *)lparam, sizeof(RECT) );
648         return 0;
649     case EM_GETLINE:
650     {
651         WORD *pw = (WORD *)lparam;
652         push_data( data, pw, sizeof(*pw) );
653         return *pw * sizeof(WCHAR);
654     }
655     case EM_SETTABSTOPS:
656     case LB_SETTABSTOPS:
657         if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
658         return 0;
659     case CB_ADDSTRING:
660     case CB_INSERTSTRING:
661     case CB_FINDSTRING:
662     case CB_FINDSTRINGEXACT:
663     case CB_SELECTSTRING:
664         if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
665         return 0;
666     case CB_GETLBTEXT:
667         if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
668         return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
669     case LB_ADDSTRING:
670     case LB_INSERTSTRING:
671     case LB_FINDSTRING:
672     case LB_FINDSTRINGEXACT:
673     case LB_SELECTSTRING:
674         if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
675         return 0;
676     case LB_GETTEXT:
677         if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
678         return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
679     case LB_GETSELITEMS:
680         return wparam * sizeof(UINT);
681     case WM_NEXTMENU:
682         push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
683         return sizeof(MDINEXTMENU);
684     case WM_SIZING:
685     case WM_MOVING:
686         push_data( data, (RECT *)lparam, sizeof(RECT) );
687         return sizeof(RECT);
688     case WM_MDICREATE:
689     {
690         MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
691         push_data( data, cs, sizeof(*cs) );
692         if (!IS_INTRESOURCE(cs->szTitle)) push_string( data, cs->szTitle );
693         if (!IS_INTRESOURCE(cs->szClass)) push_string( data, cs->szClass );
694         return sizeof(*cs);
695     }
696     case WM_MDIGETACTIVE:
697         if (lparam) return sizeof(BOOL);
698         return 0;
699     case WM_DEVICECHANGE:
700     {
701         DEV_BROADCAST_HDR *header = (DEV_BROADCAST_HDR *)lparam;
702         push_data( data, header, header->dbch_size );
703         return 0;
704     }
705     case WM_WINE_SETWINDOWPOS:
706         push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
707         return 0;
708     case WM_WINE_KEYBOARD_LL_HOOK:
709     {
710         struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
711         push_data( data, h_extra, sizeof(*h_extra) );
712         push_data( data, (LPVOID)h_extra->lparam, sizeof(KBDLLHOOKSTRUCT) );
713         return 0;
714     }
715     case WM_WINE_MOUSE_LL_HOOK:
716     {
717         struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
718         push_data( data, h_extra, sizeof(*h_extra) );
719         push_data( data, (LPVOID)h_extra->lparam, sizeof(MSLLHOOKSTRUCT) );
720         return 0;
721     }
722     case WM_NCPAINT:
723         if (wparam <= 1) return 0;
724         FIXME( "WM_NCPAINT hdc packing not supported yet\n" );
725         data->count = -1;
726         return 0;
727     case WM_PAINT:
728         if (!wparam) return 0;
729         /* fall through */
730
731     /* these contain an HFONT */
732     case WM_SETFONT:
733     case WM_GETFONT:
734     /* these contain an HDC */
735     case WM_ERASEBKGND:
736     case WM_ICONERASEBKGND:
737     case WM_CTLCOLORMSGBOX:
738     case WM_CTLCOLOREDIT:
739     case WM_CTLCOLORLISTBOX:
740     case WM_CTLCOLORBTN:
741     case WM_CTLCOLORDLG:
742     case WM_CTLCOLORSCROLLBAR:
743     case WM_CTLCOLORSTATIC:
744     case WM_PRINT:
745     case WM_PRINTCLIENT:
746     /* these contain an HGLOBAL */
747     case WM_PAINTCLIPBOARD:
748     case WM_SIZECLIPBOARD:
749     /* these contain HICON */
750     case WM_GETICON:
751     case WM_SETICON:
752     case WM_QUERYDRAGICON:
753     case WM_QUERYPARKICON:
754     /* these contain pointers */
755     case WM_DROPOBJECT:
756     case WM_QUERYDROPOBJECT:
757     case WM_DRAGLOOP:
758     case WM_DRAGSELECT:
759     case WM_DRAGMOVE:
760         FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
761         data->count = -1;
762         return 0;
763     }
764     return 0;
765 }
766
767
768 /***********************************************************************
769  *              unpack_message
770  *
771  * Unpack a message received from another process.
772  */
773 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
774                             void **buffer, size_t size )
775 {
776     size_t minsize = 0;
777
778     switch(message)
779     {
780     case WM_NCCREATE:
781     case WM_CREATE:
782     {
783         CREATESTRUCTW *cs = *buffer;
784         WCHAR *str = (WCHAR *)(cs + 1);
785         if (size < sizeof(*cs)) return FALSE;
786         size -= sizeof(*cs);
787         if (!IS_INTRESOURCE(cs->lpszName))
788         {
789             if (!check_string( str, size )) return FALSE;
790             cs->lpszName = str;
791             size -= (strlenW(str) + 1) * sizeof(WCHAR);
792             str += strlenW(str) + 1;
793         }
794         if (!IS_INTRESOURCE(cs->lpszClass))
795         {
796             if (!check_string( str, size )) return FALSE;
797             cs->lpszClass = str;
798         }
799         break;
800     }
801     case WM_GETTEXT:
802     case WM_ASKCBFORMATNAME:
803         if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
804         break;
805     case WM_WININICHANGE:
806         if (!*lparam) return TRUE;
807         /* fall through */
808     case WM_SETTEXT:
809     case WM_DEVMODECHANGE:
810     case CB_DIR:
811     case LB_DIR:
812     case LB_ADDFILE:
813     case EM_REPLACESEL:
814         if (!check_string( *buffer, size )) return FALSE;
815         break;
816     case WM_GETMINMAXINFO:
817         minsize = sizeof(MINMAXINFO);
818         break;
819     case WM_DRAWITEM:
820         minsize = sizeof(DRAWITEMSTRUCT);
821         break;
822     case WM_MEASUREITEM:
823         minsize = sizeof(MEASUREITEMSTRUCT);
824         break;
825     case WM_DELETEITEM:
826         minsize = sizeof(DELETEITEMSTRUCT);
827         break;
828     case WM_COMPAREITEM:
829         minsize = sizeof(COMPAREITEMSTRUCT);
830         break;
831     case WM_WINDOWPOSCHANGING:
832     case WM_WINDOWPOSCHANGED:
833     case WM_WINE_SETWINDOWPOS:
834         minsize = sizeof(WINDOWPOS);
835         break;
836     case WM_COPYDATA:
837     {
838         COPYDATASTRUCT *cp = *buffer;
839         if (size < sizeof(*cp)) return FALSE;
840         if (cp->lpData)
841         {
842             minsize = sizeof(*cp) + cp->cbData;
843             cp->lpData = cp + 1;
844         }
845         break;
846     }
847     case WM_NOTIFY:
848         /* WM_NOTIFY cannot be sent across processes (MSDN) */
849         return FALSE;
850     case WM_HELP:
851         minsize = sizeof(HELPINFO);
852         break;
853     case WM_STYLECHANGING:
854     case WM_STYLECHANGED:
855         minsize = sizeof(STYLESTRUCT);
856         break;
857     case WM_NCCALCSIZE:
858         if (!*wparam) minsize = sizeof(RECT);
859         else
860         {
861             NCCALCSIZE_PARAMS *nc = *buffer;
862             if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
863             nc->lppos = (WINDOWPOS *)(nc + 1);
864         }
865         break;
866     case WM_GETDLGCODE:
867         if (!*lparam) return TRUE;
868         minsize = sizeof(MSG);
869         break;
870     case SBM_SETSCROLLINFO:
871         minsize = sizeof(SCROLLINFO);
872         break;
873     case SBM_GETSCROLLINFO:
874         if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
875         break;
876     case SBM_GETSCROLLBARINFO:
877         if (!get_buffer_space( buffer, sizeof(SCROLLBARINFO ))) return FALSE;
878         break;
879     case EM_GETSEL:
880     case SBM_GETRANGE:
881     case CB_GETEDITSEL:
882         if (*wparam || *lparam)
883         {
884             if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
885             if (*wparam) *wparam = (WPARAM)*buffer;
886             if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
887         }
888         return TRUE;
889     case EM_GETRECT:
890     case LB_GETITEMRECT:
891     case CB_GETDROPPEDCONTROLRECT:
892         if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
893         break;
894     case EM_SETRECT:
895     case EM_SETRECTNP:
896         minsize = sizeof(RECT);
897         break;
898     case EM_GETLINE:
899     {
900         WORD len;
901         if (size < sizeof(WORD)) return FALSE;
902         len = *(WORD *)*buffer;
903         if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
904         *lparam = (LPARAM)*buffer + sizeof(WORD);  /* don't erase WORD at start of buffer */
905         return TRUE;
906     }
907     case EM_SETTABSTOPS:
908     case LB_SETTABSTOPS:
909         if (!*wparam) return TRUE;
910         minsize = *wparam * sizeof(UINT);
911         break;
912     case CB_ADDSTRING:
913     case CB_INSERTSTRING:
914     case CB_FINDSTRING:
915     case CB_FINDSTRINGEXACT:
916     case CB_SELECTSTRING:
917     case LB_ADDSTRING:
918     case LB_INSERTSTRING:
919     case LB_FINDSTRING:
920     case LB_FINDSTRINGEXACT:
921     case LB_SELECTSTRING:
922         if (!*buffer) return TRUE;
923         if (!check_string( *buffer, size )) return FALSE;
924         break;
925     case CB_GETLBTEXT:
926     {
927         size = sizeof(ULONG_PTR);
928         if (combobox_has_strings( hwnd ))
929             size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
930         if (!get_buffer_space( buffer, size )) return FALSE;
931         break;
932     }
933     case LB_GETTEXT:
934     {
935         size = sizeof(ULONG_PTR);
936         if (listbox_has_strings( hwnd ))
937             size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
938         if (!get_buffer_space( buffer, size )) return FALSE;
939         break;
940     }
941     case LB_GETSELITEMS:
942         if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
943         break;
944     case WM_NEXTMENU:
945         minsize = sizeof(MDINEXTMENU);
946         if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
947         break;
948     case WM_SIZING:
949     case WM_MOVING:
950         minsize = sizeof(RECT);
951         if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
952         break;
953     case WM_MDICREATE:
954     {
955         MDICREATESTRUCTW *cs = *buffer;
956         WCHAR *str = (WCHAR *)(cs + 1);
957         if (size < sizeof(*cs)) return FALSE;
958         size -= sizeof(*cs);
959         if (!IS_INTRESOURCE(cs->szTitle))
960         {
961             if (!check_string( str, size )) return FALSE;
962             cs->szTitle = str;
963             size -= (strlenW(str) + 1) * sizeof(WCHAR);
964             str += strlenW(str) + 1;
965         }
966         if (!IS_INTRESOURCE(cs->szClass))
967         {
968             if (!check_string( str, size )) return FALSE;
969             cs->szClass = str;
970         }
971         break;
972     }
973     case WM_MDIGETACTIVE:
974         if (!*lparam) return TRUE;
975         if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
976         break;
977     case WM_DEVICECHANGE:
978         minsize = sizeof(DEV_BROADCAST_HDR);
979         break;
980     case WM_WINE_KEYBOARD_LL_HOOK:
981     case WM_WINE_MOUSE_LL_HOOK:
982     {
983         struct hook_extra_info *h_extra = *buffer;
984
985         minsize = sizeof(struct hook_extra_info) +
986                   (message == WM_WINE_KEYBOARD_LL_HOOK ? sizeof(KBDLLHOOKSTRUCT)
987                                                        : sizeof(MSLLHOOKSTRUCT));
988         if (size < minsize) return FALSE;
989         h_extra->lparam = (LPARAM)(h_extra + 1);
990         break;
991     }
992     case WM_NCPAINT:
993         if (*wparam <= 1) return TRUE;
994         FIXME( "WM_NCPAINT hdc unpacking not supported\n" );
995         return FALSE;
996     case WM_PAINT:
997         if (!*wparam) return TRUE;
998         /* fall through */
999
1000     /* these contain an HFONT */
1001     case WM_SETFONT:
1002     case WM_GETFONT:
1003     /* these contain an HDC */
1004     case WM_ERASEBKGND:
1005     case WM_ICONERASEBKGND:
1006     case WM_CTLCOLORMSGBOX:
1007     case WM_CTLCOLOREDIT:
1008     case WM_CTLCOLORLISTBOX:
1009     case WM_CTLCOLORBTN:
1010     case WM_CTLCOLORDLG:
1011     case WM_CTLCOLORSCROLLBAR:
1012     case WM_CTLCOLORSTATIC:
1013     case WM_PRINT:
1014     case WM_PRINTCLIENT:
1015     /* these contain an HGLOBAL */
1016     case WM_PAINTCLIPBOARD:
1017     case WM_SIZECLIPBOARD:
1018     /* these contain HICON */
1019     case WM_GETICON:
1020     case WM_SETICON:
1021     case WM_QUERYDRAGICON:
1022     case WM_QUERYPARKICON:
1023     /* these contain pointers */
1024     case WM_DROPOBJECT:
1025     case WM_QUERYDROPOBJECT:
1026     case WM_DRAGLOOP:
1027     case WM_DRAGSELECT:
1028     case WM_DRAGMOVE:
1029         FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
1030         return FALSE;
1031
1032     default:
1033         return TRUE; /* message doesn't need any unpacking */
1034     }
1035
1036     /* default exit for most messages: check minsize and store buffer in lparam */
1037     if (size < minsize) return FALSE;
1038     *lparam = (LPARAM)*buffer;
1039     return TRUE;
1040 }
1041
1042
1043 /***********************************************************************
1044  *              pack_reply
1045  *
1046  * Pack a reply to a message for sending to another process.
1047  */
1048 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1049                         LRESULT res, struct packed_message *data )
1050 {
1051     data->count = 0;
1052     switch(message)
1053     {
1054     case WM_NCCREATE:
1055     case WM_CREATE:
1056         push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
1057         break;
1058     case WM_GETTEXT:
1059     case CB_GETLBTEXT:
1060     case LB_GETTEXT:
1061         push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
1062         break;
1063     case WM_GETMINMAXINFO:
1064         push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
1065         break;
1066     case WM_MEASUREITEM:
1067         push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
1068         break;
1069     case WM_WINDOWPOSCHANGING:
1070     case WM_WINDOWPOSCHANGED:
1071         push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
1072         break;
1073     case WM_GETDLGCODE:
1074         if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
1075         break;
1076     case SBM_GETSCROLLINFO:
1077         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
1078         break;
1079     case EM_GETRECT:
1080     case LB_GETITEMRECT:
1081     case CB_GETDROPPEDCONTROLRECT:
1082     case WM_SIZING:
1083     case WM_MOVING:
1084         push_data( data, (RECT *)lparam, sizeof(RECT) );
1085         break;
1086     case EM_GETLINE:
1087     {
1088         WORD *ptr = (WORD *)lparam;
1089         push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
1090         break;
1091     }
1092     case LB_GETSELITEMS:
1093         push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
1094         break;
1095     case WM_MDIGETACTIVE:
1096         if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
1097         break;
1098     case WM_NCCALCSIZE:
1099         if (!wparam)
1100             push_data( data, (RECT *)lparam, sizeof(RECT) );
1101         else
1102         {
1103             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1104             push_data( data, nc, sizeof(*nc) );
1105             push_data( data, nc->lppos, sizeof(*nc->lppos) );
1106         }
1107         break;
1108     case EM_GETSEL:
1109     case SBM_GETRANGE:
1110     case CB_GETEDITSEL:
1111         if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
1112         if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
1113         break;
1114     case WM_NEXTMENU:
1115         push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
1116         break;
1117     case WM_MDICREATE:
1118         push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
1119         break;
1120     case WM_ASKCBFORMATNAME:
1121         push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
1122         break;
1123     }
1124 }
1125
1126
1127 /***********************************************************************
1128  *              unpack_reply
1129  *
1130  * Unpack a message reply received from another process.
1131  */
1132 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1133                           void *buffer, size_t size )
1134 {
1135     switch(message)
1136     {
1137     case WM_NCCREATE:
1138     case WM_CREATE:
1139     {
1140         CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
1141         LPCWSTR name = cs->lpszName, class = cs->lpszClass;
1142         memcpy( cs, buffer, min( sizeof(*cs), size ));
1143         cs->lpszName = name;  /* restore the original pointers */
1144         cs->lpszClass = class;
1145         break;
1146     }
1147     case WM_GETTEXT:
1148     case WM_ASKCBFORMATNAME:
1149         memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
1150         break;
1151     case WM_GETMINMAXINFO:
1152         memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
1153         break;
1154     case WM_MEASUREITEM:
1155         memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
1156         break;
1157     case WM_WINDOWPOSCHANGING:
1158     case WM_WINDOWPOSCHANGED:
1159         memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
1160         break;
1161     case WM_GETDLGCODE:
1162         if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
1163         break;
1164     case SBM_GETSCROLLINFO:
1165         memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
1166         break;
1167     case SBM_GETSCROLLBARINFO:
1168         memcpy( (SCROLLBARINFO *)lparam, buffer, min( sizeof(SCROLLBARINFO), size ));
1169         break;
1170     case EM_GETRECT:
1171     case CB_GETDROPPEDCONTROLRECT:
1172     case LB_GETITEMRECT:
1173     case WM_SIZING:
1174     case WM_MOVING:
1175         memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1176         break;
1177     case EM_GETLINE:
1178         size = min( size, (size_t)*(WORD *)lparam );
1179         memcpy( (WCHAR *)lparam, buffer, size );
1180         break;
1181     case LB_GETSELITEMS:
1182         memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
1183         break;
1184     case LB_GETTEXT:
1185     case CB_GETLBTEXT:
1186         memcpy( (WCHAR *)lparam, buffer, size );
1187         break;
1188     case WM_NEXTMENU:
1189         memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
1190         break;
1191     case WM_MDIGETACTIVE:
1192         if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
1193         break;
1194     case WM_NCCALCSIZE:
1195         if (!wparam)
1196             memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1197         else
1198         {
1199             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1200             WINDOWPOS *wp = nc->lppos;
1201             memcpy( nc, buffer, min( sizeof(*nc), size ));
1202             if (size > sizeof(*nc))
1203             {
1204                 size -= sizeof(*nc);
1205                 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
1206             }
1207             nc->lppos = wp;  /* restore the original pointer */
1208         }
1209         break;
1210     case EM_GETSEL:
1211     case SBM_GETRANGE:
1212     case CB_GETEDITSEL:
1213         if (wparam)
1214         {
1215             memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1216             if (size <= sizeof(DWORD)) break;
1217             size -= sizeof(DWORD);
1218             buffer = (DWORD *)buffer + 1;
1219         }
1220         if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1221         break;
1222     case WM_MDICREATE:
1223     {
1224         MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
1225         LPCWSTR title = cs->szTitle, class = cs->szClass;
1226         memcpy( cs, buffer, min( sizeof(*cs), size ));
1227         cs->szTitle = title;  /* restore the original pointers */
1228         cs->szClass = class;
1229         break;
1230     }
1231     default:
1232         ERR( "should not happen: unexpected message %x\n", message );
1233         break;
1234     }
1235 }
1236
1237
1238 /***********************************************************************
1239  *           reply_message
1240  *
1241  * Send a reply to a sent message.
1242  */
1243 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1244 {
1245     struct packed_message data;
1246     int i, replied = info->flags & ISMEX_REPLIED;
1247
1248     if (info->flags & ISMEX_NOTIFY) return;  /* notify messages don't get replies */
1249     if (!remove && replied) return;  /* replied already */
1250
1251     data.count = 0;
1252     info->flags |= ISMEX_REPLIED;
1253
1254     if (info->type == MSG_OTHER_PROCESS && !replied)
1255     {
1256         pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1257                     info->msg.lParam, result, &data );
1258     }
1259
1260     SERVER_START_REQ( reply_message )
1261     {
1262         req->result = result;
1263         req->remove = remove;
1264         for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1265         wine_server_call( req );
1266     }
1267     SERVER_END_REQ;
1268 }
1269
1270
1271 /***********************************************************************
1272  *           handle_internal_message
1273  *
1274  * Handle an internal Wine message instead of calling the window proc.
1275  */
1276 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1277 {
1278     switch(msg)
1279     {
1280     case WM_WINE_DESTROYWINDOW:
1281         return WIN_DestroyWindow( hwnd );
1282     case WM_WINE_SETWINDOWPOS:
1283         if (is_desktop_window( hwnd )) return 0;
1284         return USER_SetWindowPos( (WINDOWPOS *)lparam );
1285     case WM_WINE_SHOWWINDOW:
1286         if (is_desktop_window( hwnd )) return 0;
1287         return ShowWindow( hwnd, wparam );
1288     case WM_WINE_SETPARENT:
1289         if (is_desktop_window( hwnd )) return 0;
1290         return (LRESULT)SetParent( hwnd, (HWND)wparam );
1291     case WM_WINE_SETWINDOWLONG:
1292         return WIN_SetWindowLong( hwnd, (short)LOWORD(wparam), HIWORD(wparam), lparam, TRUE );
1293     case WM_WINE_ENABLEWINDOW:
1294         if (is_desktop_window( hwnd )) return 0;
1295         return EnableWindow( hwnd, wparam );
1296     case WM_WINE_SETACTIVEWINDOW:
1297         if (is_desktop_window( hwnd )) return 0;
1298         return (LRESULT)SetActiveWindow( (HWND)wparam );
1299     case WM_WINE_KEYBOARD_LL_HOOK:
1300     case WM_WINE_MOUSE_LL_HOOK:
1301     {
1302         struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
1303
1304         return call_current_hook( h_extra->handle, HC_ACTION, wparam, h_extra->lparam );
1305     }
1306     default:
1307         if (msg >= WM_WINE_FIRST_DRIVER_MSG && msg <= WM_WINE_LAST_DRIVER_MSG)
1308             return USER_Driver->pWindowMessage( hwnd, msg, wparam, lparam );
1309         FIXME( "unknown internal message %x\n", msg );
1310         return 0;
1311     }
1312 }
1313
1314 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1315  * to the memory handle, we keep track (in the server side) of all pairs of handle
1316  * used (the client passes its value and the content of the memory handle), and
1317  * the server stored both values (the client, and the local one, created after the
1318  * content). When a ACK message is generated, the list of pair is searched for a
1319  * matching pair, so that the client memory handle can be returned.
1320  */
1321 struct DDE_pair {
1322     HGLOBAL     client_hMem;
1323     HGLOBAL     server_hMem;
1324 };
1325
1326 static      struct DDE_pair*    dde_pairs;
1327 static      int                 dde_num_alloc;
1328 static      int                 dde_num_used;
1329
1330 static CRITICAL_SECTION dde_crst;
1331 static CRITICAL_SECTION_DEBUG critsect_debug =
1332 {
1333     0, 0, &dde_crst,
1334     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
1335       0, 0, { (DWORD_PTR)(__FILE__ ": dde_crst") }
1336 };
1337 static CRITICAL_SECTION dde_crst = { &critsect_debug, -1, 0, 0, 0, 0 };
1338
1339 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1340 {
1341     int  i;
1342 #define GROWBY  4
1343
1344     EnterCriticalSection(&dde_crst);
1345
1346     /* now remember the pair of hMem on both sides */
1347     if (dde_num_used == dde_num_alloc)
1348     {
1349         struct DDE_pair* tmp;
1350         if (dde_pairs)
1351             tmp  = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1352                                             (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1353         else
1354             tmp  = HeapAlloc( GetProcessHeap(), 0, 
1355                                             (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1356
1357         if (!tmp)
1358         {
1359             LeaveCriticalSection(&dde_crst);
1360             return FALSE;
1361         }
1362         dde_pairs = tmp;
1363         /* zero out newly allocated part */
1364         memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1365         dde_num_alloc += GROWBY;
1366     }
1367 #undef GROWBY
1368     for (i = 0; i < dde_num_alloc; i++)
1369     {
1370         if (dde_pairs[i].server_hMem == 0)
1371         {
1372             dde_pairs[i].client_hMem = chm;
1373             dde_pairs[i].server_hMem = shm;
1374             dde_num_used++;
1375             break;
1376         }
1377     }
1378     LeaveCriticalSection(&dde_crst);
1379     return TRUE;
1380 }
1381
1382 static HGLOBAL dde_get_pair(HGLOBAL shm)
1383 {
1384     int  i;
1385     HGLOBAL     ret = 0;
1386
1387     EnterCriticalSection(&dde_crst);
1388     for (i = 0; i < dde_num_alloc; i++)
1389     {
1390         if (dde_pairs[i].server_hMem == shm)
1391         {
1392             /* free this pair */
1393             dde_pairs[i].server_hMem = 0;
1394             dde_num_used--;
1395             ret = dde_pairs[i].client_hMem;
1396             break;
1397         }
1398     }
1399     LeaveCriticalSection(&dde_crst);
1400     return ret;
1401 }
1402
1403 /***********************************************************************
1404  *              post_dde_message
1405  *
1406  * Post a DDE message
1407  */
1408 static BOOL post_dde_message( struct packed_message *data, const struct send_message_info *info )
1409 {
1410     void*       ptr = NULL;
1411     int         size = 0;
1412     UINT_PTR    uiLo, uiHi;
1413     LPARAM      lp = 0;
1414     HGLOBAL     hunlock = 0;
1415     int         i;
1416     DWORD       res;
1417
1418     if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1419         return FALSE;
1420
1421     lp = info->lparam;
1422     switch (info->msg)
1423     {
1424         /* DDE messages which don't require packing are:
1425          * WM_DDE_INITIATE
1426          * WM_DDE_TERMINATE
1427          * WM_DDE_REQUEST
1428          * WM_DDE_UNADVISE
1429          */
1430     case WM_DDE_ACK:
1431         if (HIWORD(uiHi))
1432         {
1433             /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1434             HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1435             if (h)
1436             {
1437                 /* send back the value of h on the other side */
1438                 push_data( data, &h, sizeof(HGLOBAL) );
1439                 lp = uiLo;
1440                 TRACE( "send dde-ack %lx %08lx => %p\n", uiLo, uiHi, h );
1441             }
1442         }
1443         else
1444         {
1445             /* uiHi should contain either an atom or 0 */
1446             TRACE( "send dde-ack %lx atom=%lx\n", uiLo, uiHi );
1447             lp = MAKELONG( uiLo, uiHi );
1448         }
1449         break;
1450     case WM_DDE_ADVISE:
1451     case WM_DDE_DATA:
1452     case WM_DDE_POKE:
1453         size = 0;
1454         if (uiLo)
1455         {
1456             size = GlobalSize( (HGLOBAL)uiLo ) ;
1457             if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1458                 (info->msg == WM_DDE_DATA   && size < FIELD_OFFSET(DDEDATA, Value)) ||
1459                 (info->msg == WM_DDE_POKE   && size < FIELD_OFFSET(DDEPOKE, Value))
1460                 )
1461             return FALSE;
1462         }
1463         else if (info->msg != WM_DDE_DATA) return FALSE;
1464
1465         lp = uiHi;
1466         if (uiLo)
1467         {
1468             if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1469             {
1470                 DDEDATA *dde_data = ptr;
1471                 TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
1472                        dde_data->unused, dde_data->fResponse, dde_data->fRelease,
1473                        dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
1474                 push_data( data, ptr, size );
1475                 hunlock = (HGLOBAL)uiLo;
1476             }
1477         }
1478         TRACE( "send ddepack %u %lx\n", size, uiHi );
1479         break;
1480     case WM_DDE_EXECUTE:
1481         if (info->lparam)
1482         {
1483             if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1484             {
1485                 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1486                 /* so that the other side can send it back on ACK */
1487                 lp = info->lparam;
1488                 hunlock = (HGLOBAL)info->lparam;
1489             }
1490         }
1491         break;
1492     }
1493     SERVER_START_REQ( send_message )
1494     {
1495         req->id      = info->dest_tid;
1496         req->type    = info->type;
1497         req->flags   = 0;
1498         req->win     = wine_server_user_handle( info->hwnd );
1499         req->msg     = info->msg;
1500         req->wparam  = info->wparam;
1501         req->lparam  = lp;
1502         req->timeout = TIMEOUT_INFINITE;
1503         for (i = 0; i < data->count; i++)
1504             wine_server_add_data( req, data->data[i], data->size[i] );
1505         if ((res = wine_server_call( req )))
1506         {
1507             if (res == STATUS_INVALID_PARAMETER)
1508                 /* FIXME: find a STATUS_ value for this one */
1509                 SetLastError( ERROR_INVALID_THREAD_ID );
1510             else
1511                 SetLastError( RtlNtStatusToDosError(res) );
1512         }
1513         else
1514             FreeDDElParam(info->msg, info->lparam);
1515     }
1516     SERVER_END_REQ;
1517     if (hunlock) GlobalUnlock(hunlock);
1518
1519     return !res;
1520 }
1521
1522 /***********************************************************************
1523  *              unpack_dde_message
1524  *
1525  * Unpack a posted DDE message received from another process.
1526  */
1527 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1528                                 void **buffer, size_t size )
1529 {
1530     UINT_PTR    uiLo, uiHi;
1531     HGLOBAL     hMem = 0;
1532     void*       ptr;
1533
1534     switch (message)
1535     {
1536     case WM_DDE_ACK:
1537         if (size)
1538         {
1539             /* hMem is being passed */
1540             if (size != sizeof(HGLOBAL)) return FALSE;
1541             if (!buffer || !*buffer) return FALSE;
1542             uiLo = *lparam;
1543             memcpy( &hMem, *buffer, size );
1544             uiHi = (UINT_PTR)hMem;
1545             TRACE("recv dde-ack %lx mem=%lx[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1546         }
1547         else
1548         {
1549             uiLo = LOWORD( *lparam );
1550             uiHi = HIWORD( *lparam );
1551             TRACE("recv dde-ack %lx atom=%lx\n", uiLo, uiHi);
1552         }
1553         *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1554         break;
1555     case WM_DDE_ADVISE:
1556     case WM_DDE_DATA:
1557     case WM_DDE_POKE:
1558         if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1559         uiHi = *lparam;
1560         if (size)
1561         {
1562             if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size )))
1563                 return FALSE;
1564             if ((ptr = GlobalLock( hMem )))
1565             {
1566                 memcpy( ptr, *buffer, size );
1567                 GlobalUnlock( hMem );
1568             }
1569             else
1570             {
1571                 GlobalFree( hMem );
1572                 return FALSE;
1573             }
1574         }
1575         uiLo = (UINT_PTR)hMem;
1576
1577         *lparam = PackDDElParam( message, uiLo, uiHi );
1578         break;
1579     case WM_DDE_EXECUTE:
1580         if (size)
1581         {
1582             if (!buffer || !*buffer) return FALSE;
1583             if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size ))) return FALSE;
1584             if ((ptr = GlobalLock( hMem )))
1585             {
1586                 memcpy( ptr, *buffer, size );
1587                 GlobalUnlock( hMem );
1588                 TRACE( "exec: pairing c=%08lx s=%p\n", *lparam, hMem );
1589                 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1590                 {
1591                     GlobalFree( hMem );
1592                     return FALSE;
1593                 }
1594             }
1595             else
1596             {
1597                 GlobalFree( hMem );
1598                 return FALSE;
1599             }
1600         } else return FALSE;
1601         *lparam = (LPARAM)hMem;
1602         break;
1603     }
1604     return TRUE;
1605 }
1606
1607 /***********************************************************************
1608  *           call_window_proc
1609  *
1610  * Call a window procedure and the corresponding hooks.
1611  */
1612 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1613                                  BOOL unicode, BOOL same_thread, enum wm_char_mapping mapping )
1614 {
1615     LRESULT result = 0;
1616     CWPSTRUCT cwp;
1617     CWPRETSTRUCT cwpret;
1618
1619     if (msg & 0x80000000)
1620     {
1621         result = handle_internal_message( hwnd, msg, wparam, lparam );
1622         goto done;
1623     }
1624
1625     /* first the WH_CALLWNDPROC hook */
1626     hwnd = WIN_GetFullHandle( hwnd );
1627     cwp.lParam  = lparam;
1628     cwp.wParam  = wparam;
1629     cwp.message = msg;
1630     cwp.hwnd    = hwnd;
1631     HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1632
1633     /* now call the window procedure */
1634     if (!WINPROC_call_window( hwnd, msg, wparam, lparam, &result, unicode, mapping )) goto done;
1635
1636     /* and finally the WH_CALLWNDPROCRET hook */
1637     cwpret.lResult = result;
1638     cwpret.lParam  = lparam;
1639     cwpret.wParam  = wparam;
1640     cwpret.message = msg;
1641     cwpret.hwnd    = hwnd;
1642     HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1643  done:
1644     return result;
1645 }
1646
1647
1648 /***********************************************************************
1649  *           send_parent_notify
1650  *
1651  * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
1652  * the window has the WS_EX_NOPARENTNOTIFY style.
1653  */
1654 static void send_parent_notify( HWND hwnd, WORD event, WORD idChild, POINT pt )
1655 {
1656     /* pt has to be in the client coordinates of the parent window */
1657     MapWindowPoints( 0, hwnd, &pt, 1 );
1658     for (;;)
1659     {
1660         HWND parent;
1661
1662         if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CHILD)) break;
1663         if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
1664         if (!(parent = GetParent(hwnd))) break;
1665         if (parent == GetDesktopWindow()) break;
1666         MapWindowPoints( hwnd, parent, &pt, 1 );
1667         hwnd = parent;
1668         SendMessageW( hwnd, WM_PARENTNOTIFY,
1669                       MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
1670     }
1671 }
1672
1673
1674 /***********************************************************************
1675  *          accept_hardware_message
1676  *
1677  * Tell the server we have passed the message to the app
1678  * (even though we may end up dropping it later on)
1679  */
1680 static void accept_hardware_message( UINT hw_id, BOOL remove, HWND new_hwnd )
1681 {
1682     SERVER_START_REQ( accept_hardware_message )
1683     {
1684         req->hw_id   = hw_id;
1685         req->remove  = remove;
1686         req->new_win = wine_server_user_handle( new_hwnd );
1687         if (wine_server_call( req ))
1688             FIXME("Failed to reply to MSG_HARDWARE message. Message may not be removed from queue.\n");
1689     }
1690     SERVER_END_REQ;
1691 }
1692
1693
1694 /***********************************************************************
1695  *          process_keyboard_message
1696  *
1697  * returns TRUE if the contents of 'msg' should be passed to the application
1698  */
1699 static BOOL process_keyboard_message( MSG *msg, UINT hw_id, HWND hwnd_filter,
1700                                       UINT first, UINT last, BOOL remove )
1701 {
1702     EVENTMSG event;
1703
1704     if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN ||
1705         msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP)
1706         switch (msg->wParam)
1707         {
1708             case VK_LSHIFT: case VK_RSHIFT:
1709                 msg->wParam = VK_SHIFT;
1710                 break;
1711             case VK_LCONTROL: case VK_RCONTROL:
1712                 msg->wParam = VK_CONTROL;
1713                 break;
1714             case VK_LMENU: case VK_RMENU:
1715                 msg->wParam = VK_MENU;
1716                 break;
1717         }
1718
1719     /* FIXME: is this really the right place for this hook? */
1720     event.message = msg->message;
1721     event.hwnd    = msg->hwnd;
1722     event.time    = msg->time;
1723     event.paramL  = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
1724     event.paramH  = msg->lParam & 0x7FFF;
1725     if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
1726     HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1727
1728     /* check message filters */
1729     if (msg->message < first || msg->message > last) return FALSE;
1730     if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1731
1732     if (remove)
1733     {
1734         if((msg->message == WM_KEYDOWN) &&
1735            (msg->hwnd != GetDesktopWindow()))
1736         {
1737             /* Handle F1 key by sending out WM_HELP message */
1738             if (msg->wParam == VK_F1)
1739             {
1740                 PostMessageW( msg->hwnd, WM_KEYF1, 0, 0 );
1741             }
1742             else if(msg->wParam >= VK_BROWSER_BACK &&
1743                     msg->wParam <= VK_LAUNCH_APP2)
1744             {
1745                 /* FIXME: Process keystate */
1746                 SendMessageW(msg->hwnd, WM_APPCOMMAND, (WPARAM)msg->hwnd, MAKELPARAM(0, (FAPPCOMMAND_KEY | (msg->wParam - VK_BROWSER_BACK + 1))));
1747             }
1748         }
1749         else if (msg->message == WM_KEYUP)
1750         {
1751             /* Handle VK_APPS key by posting a WM_CONTEXTMENU message */
1752             if (msg->wParam == VK_APPS && !MENU_IsMenuActive())
1753                 PostMessageW(msg->hwnd, WM_CONTEXTMENU, (WPARAM)msg->hwnd, -1);
1754         }
1755     }
1756
1757     if (HOOK_CallHooks( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
1758                         LOWORD(msg->wParam), msg->lParam, TRUE ))
1759     {
1760         /* skip this message */
1761         HOOK_CallHooks( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam, TRUE );
1762         accept_hardware_message( hw_id, TRUE, 0 );
1763         return FALSE;
1764     }
1765     accept_hardware_message( hw_id, remove, 0 );
1766
1767     if ( msg->message == WM_KEYDOWN || msg->message == WM_KEYUP )
1768         if ( ImmProcessKey(msg->hwnd, GetKeyboardLayout(0), msg->wParam, msg->lParam, 0) )
1769             msg->wParam = VK_PROCESSKEY;
1770
1771     return TRUE;
1772 }
1773
1774
1775 /***********************************************************************
1776  *          process_mouse_message
1777  *
1778  * returns TRUE if the contents of 'msg' should be passed to the application
1779  */
1780 static BOOL process_mouse_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
1781                                    UINT first, UINT last, BOOL remove )
1782 {
1783     static MSG clk_msg;
1784
1785     POINT pt;
1786     UINT message;
1787     INT hittest;
1788     EVENTMSG event;
1789     GUITHREADINFO info;
1790     MOUSEHOOKSTRUCT hook;
1791     BOOL eatMsg;
1792
1793     /* find the window to dispatch this mouse message to */
1794
1795     GetGUIThreadInfo( GetCurrentThreadId(), &info );
1796     if (info.hwndCapture)
1797     {
1798         hittest = HTCLIENT;
1799         msg->hwnd = info.hwndCapture;
1800     }
1801     else
1802     {
1803         msg->hwnd = WINPOS_WindowFromPoint( msg->hwnd, msg->pt, &hittest );
1804     }
1805
1806     if (!msg->hwnd || !WIN_IsCurrentThread( msg->hwnd ))
1807     {
1808         accept_hardware_message( hw_id, TRUE, msg->hwnd );
1809         return FALSE;
1810     }
1811
1812     /* FIXME: is this really the right place for this hook? */
1813     event.message = msg->message;
1814     event.time    = msg->time;
1815     event.hwnd    = msg->hwnd;
1816     event.paramL  = msg->pt.x;
1817     event.paramH  = msg->pt.y;
1818     HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, TRUE );
1819
1820     if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
1821
1822     pt = msg->pt;
1823     message = msg->message;
1824     /* Note: windows has no concept of a non-client wheel message */
1825     if (message != WM_MOUSEWHEEL)
1826     {
1827         if (hittest != HTCLIENT)
1828         {
1829             message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
1830             msg->wParam = hittest;
1831         }
1832         else
1833         {
1834             /* coordinates don't get translated while tracking a menu */
1835             /* FIXME: should differentiate popups and top-level menus */
1836             if (!(info.flags & GUI_INMENUMODE))
1837                 ScreenToClient( msg->hwnd, &pt );
1838         }
1839     }
1840     msg->lParam = MAKELONG( pt.x, pt.y );
1841
1842     /* translate double clicks */
1843
1844     if ((msg->message == WM_LBUTTONDOWN) ||
1845         (msg->message == WM_RBUTTONDOWN) ||
1846         (msg->message == WM_MBUTTONDOWN) ||
1847         (msg->message == WM_XBUTTONDOWN))
1848     {
1849         BOOL update = remove;
1850
1851         /* translate double clicks -
1852          * note that ...MOUSEMOVEs can slip in between
1853          * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
1854
1855         if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
1856             hittest != HTCLIENT ||
1857             (GetClassLongA( msg->hwnd, GCL_STYLE ) & CS_DBLCLKS))
1858         {
1859            if ((msg->message == clk_msg.message) &&
1860                (msg->hwnd == clk_msg.hwnd) &&
1861                (msg->wParam == clk_msg.wParam) &&
1862                (msg->time - clk_msg.time < GetDoubleClickTime()) &&
1863                (abs(msg->pt.x - clk_msg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
1864                (abs(msg->pt.y - clk_msg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
1865            {
1866                message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
1867                if (update)
1868                {
1869                    clk_msg.message = 0;  /* clear the double click conditions */
1870                    update = FALSE;
1871                }
1872            }
1873         }
1874         if (message < first || message > last) return FALSE;
1875         /* update static double click conditions */
1876         if (update) clk_msg = *msg;
1877     }
1878     else
1879     {
1880         if (message < first || message > last) return FALSE;
1881     }
1882
1883     /* message is accepted now (but may still get dropped) */
1884
1885     hook.pt           = msg->pt;
1886     hook.hwnd         = msg->hwnd;
1887     hook.wHitTestCode = hittest;
1888     hook.dwExtraInfo  = extra_info;
1889     if (HOOK_CallHooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE,
1890                         message, (LPARAM)&hook, TRUE ))
1891     {
1892         hook.pt           = msg->pt;
1893         hook.hwnd         = msg->hwnd;
1894         hook.wHitTestCode = hittest;
1895         hook.dwExtraInfo  = extra_info;
1896         HOOK_CallHooks( WH_CBT, HCBT_CLICKSKIPPED, message, (LPARAM)&hook, TRUE );
1897         accept_hardware_message( hw_id, TRUE, 0 );
1898         return FALSE;
1899     }
1900
1901     if ((hittest == HTERROR) || (hittest == HTNOWHERE))
1902     {
1903         SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd,
1904                       MAKELONG( hittest, msg->message ));
1905         accept_hardware_message( hw_id, TRUE, 0 );
1906         return FALSE;
1907     }
1908
1909     accept_hardware_message( hw_id, remove, 0 );
1910
1911     if (!remove || info.hwndCapture)
1912     {
1913         msg->message = message;
1914         return TRUE;
1915     }
1916
1917     eatMsg = FALSE;
1918
1919     if ((msg->message == WM_LBUTTONDOWN) ||
1920         (msg->message == WM_RBUTTONDOWN) ||
1921         (msg->message == WM_MBUTTONDOWN) ||
1922         (msg->message == WM_XBUTTONDOWN))
1923     {
1924         /* Send the WM_PARENTNOTIFY,
1925          * note that even for double/nonclient clicks
1926          * notification message is still WM_L/M/RBUTTONDOWN.
1927          */
1928         send_parent_notify( msg->hwnd, msg->message, 0, msg->pt );
1929
1930         /* Activate the window if needed */
1931
1932         if (msg->hwnd != info.hwndActive)
1933         {
1934             HWND hwndTop = msg->hwnd;
1935             while (hwndTop)
1936             {
1937                 if ((GetWindowLongW( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD) break;
1938                 hwndTop = GetParent( hwndTop );
1939             }
1940
1941             if (hwndTop && hwndTop != GetDesktopWindow())
1942             {
1943                 LONG ret = SendMessageW( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
1944                                          MAKELONG( hittest, msg->message ) );
1945                 switch(ret)
1946                 {
1947                 case MA_NOACTIVATEANDEAT:
1948                     eatMsg = TRUE;
1949                     /* fall through */
1950                 case MA_NOACTIVATE:
1951                     break;
1952                 case MA_ACTIVATEANDEAT:
1953                     eatMsg = TRUE;
1954                     /* fall through */
1955                 case MA_ACTIVATE:
1956                 case 0:
1957                     if (!FOCUS_MouseActivate( hwndTop )) eatMsg = TRUE;
1958                     break;
1959                 default:
1960                     WARN( "unknown WM_MOUSEACTIVATE code %d\n", ret );
1961                     break;
1962                 }
1963             }
1964         }
1965     }
1966
1967     /* send the WM_SETCURSOR message */
1968
1969     /* Windows sends the normal mouse message as the message parameter
1970        in the WM_SETCURSOR message even if it's non-client mouse message */
1971     SendMessageW( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd, MAKELONG( hittest, msg->message ));
1972
1973     msg->message = message;
1974     return !eatMsg;
1975 }
1976
1977
1978 /***********************************************************************
1979  *           process_hardware_message
1980  *
1981  * Process a hardware message; return TRUE if message should be passed on to the app
1982  */
1983 static BOOL process_hardware_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
1984                                       UINT first, UINT last, BOOL remove )
1985 {
1986     if (is_keyboard_message( msg->message ))
1987         return process_keyboard_message( msg, hw_id, hwnd_filter, first, last, remove );
1988
1989     if (is_mouse_message( msg->message ))
1990         return process_mouse_message( msg, hw_id, extra_info, hwnd_filter, first, last, remove );
1991
1992     ERR( "unknown message type %x\n", msg->message );
1993     return FALSE;
1994 }
1995
1996
1997 /***********************************************************************
1998  *           call_sendmsg_callback
1999  *
2000  * Call the callback function of SendMessageCallback.
2001  */
2002 static inline void call_sendmsg_callback( SENDASYNCPROC callback, HWND hwnd, UINT msg,
2003                                           ULONG_PTR data, LRESULT result )
2004 {
2005     if (!callback) return;
2006
2007     if (TRACE_ON(relay))
2008         DPRINTF( "%04x:Call message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
2009                  GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
2010                  data, result );
2011     callback( hwnd, msg, data, result );
2012     if (TRACE_ON(relay))
2013         DPRINTF( "%04x:Ret  message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
2014                  GetCurrentThreadId(), callback, hwnd, SPY_GetMsgName( msg, hwnd ),
2015                  data, result );
2016 }
2017
2018
2019 /***********************************************************************
2020  *           peek_message
2021  *
2022  * Peek for a message matching the given parameters. Return FALSE if none available.
2023  * All pending sent messages are processed before returning.
2024  */
2025 static BOOL peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags, UINT changed_mask )
2026 {
2027     LRESULT result;
2028     struct user_thread_info *thread_info = get_user_thread_info();
2029     struct received_message_info info, *old_info;
2030     unsigned int hw_id = 0;  /* id of previous hardware message */
2031     void *buffer;
2032     size_t buffer_size = 256;
2033
2034     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size ))) return FALSE;
2035
2036     if (!first && !last) last = ~0;
2037     if (hwnd == HWND_BROADCAST) hwnd = HWND_TOPMOST;
2038
2039     for (;;)
2040     {
2041         NTSTATUS res;
2042         size_t size = 0;
2043         const message_data_t *msg_data = buffer;
2044
2045         SERVER_START_REQ( get_message )
2046         {
2047             req->flags     = flags;
2048             req->get_win   = wine_server_user_handle( hwnd );
2049             req->get_first = first;
2050             req->get_last  = last;
2051             req->hw_id     = hw_id;
2052             req->wake_mask = changed_mask & (QS_SENDMESSAGE | QS_SMRESULT);
2053             req->changed_mask = changed_mask;
2054             wine_server_set_reply( req, buffer, buffer_size );
2055             if (!(res = wine_server_call( req )))
2056             {
2057                 size = wine_server_reply_size( reply );
2058                 info.type        = reply->type;
2059                 info.msg.hwnd    = wine_server_ptr_handle( reply->win );
2060                 info.msg.message = reply->msg;
2061                 info.msg.wParam  = reply->wparam;
2062                 info.msg.lParam  = reply->lparam;
2063                 info.msg.time    = reply->time;
2064                 info.msg.pt.x    = 0;
2065                 info.msg.pt.y    = 0;
2066                 hw_id            = 0;
2067                 thread_info->active_hooks = reply->active_hooks;
2068             }
2069             else buffer_size = reply->total;
2070         }
2071         SERVER_END_REQ;
2072
2073         if (res)
2074         {
2075             HeapFree( GetProcessHeap(), 0, buffer );
2076             if (res != STATUS_BUFFER_OVERFLOW) return FALSE;
2077             if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size ))) return FALSE;
2078             continue;
2079         }
2080
2081         TRACE( "got type %d msg %x (%s) hwnd %p wp %lx lp %lx\n",
2082                info.type, info.msg.message,
2083                (info.type == MSG_WINEVENT) ? "MSG_WINEVENT" : SPY_GetMsgName(info.msg.message, info.msg.hwnd),
2084                info.msg.hwnd, info.msg.wParam, info.msg.lParam );
2085
2086         switch(info.type)
2087         {
2088         case MSG_ASCII:
2089         case MSG_UNICODE:
2090             info.flags = ISMEX_SEND;
2091             break;
2092         case MSG_NOTIFY:
2093             info.flags = ISMEX_NOTIFY;
2094             break;
2095         case MSG_CALLBACK:
2096             info.flags = ISMEX_CALLBACK;
2097             break;
2098         case MSG_CALLBACK_RESULT:
2099             if (size >= sizeof(msg_data->callback))
2100                 call_sendmsg_callback( wine_server_get_ptr(msg_data->callback.callback),
2101                                        info.msg.hwnd, info.msg.message,
2102                                        msg_data->callback.data, msg_data->callback.result );
2103             continue;
2104         case MSG_WINEVENT:
2105             if (size >= sizeof(msg_data->winevent))
2106             {
2107                 WINEVENTPROC hook_proc;
2108
2109                 hook_proc = wine_server_get_ptr( msg_data->winevent.hook_proc );
2110                 size -= sizeof(msg_data->winevent);
2111                 if (size)
2112                 {
2113                     WCHAR module[MAX_PATH];
2114
2115                     size = min( size, (MAX_PATH - 1) * sizeof(WCHAR) );
2116                     memcpy( module, &msg_data->winevent + 1, size );
2117                     module[size / sizeof(WCHAR)] = 0;
2118                     if (!(hook_proc = get_hook_proc( hook_proc, module )))
2119                     {
2120                         ERR( "invalid winevent hook module name %s\n", debugstr_w(module) );
2121                         continue;
2122                     }
2123                 }
2124
2125                 if (TRACE_ON(relay))
2126                     DPRINTF( "%04x:Call winevent proc %p (hook=%04x,event=%x,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04x,time=%x)\n",
2127                              GetCurrentThreadId(), hook_proc,
2128                              msg_data->winevent.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2129                              info.msg.lParam, msg_data->winevent.tid, info.msg.time);
2130
2131                 hook_proc( wine_server_ptr_handle( msg_data->winevent.hook ), info.msg.message,
2132                            info.msg.hwnd, info.msg.wParam, info.msg.lParam,
2133                            msg_data->winevent.tid, info.msg.time );
2134
2135                 if (TRACE_ON(relay))
2136                     DPRINTF( "%04x:Ret  winevent proc %p (hook=%04x,event=%x,hwnd=%p,object_id=%lx,child_id=%lx,tid=%04x,time=%x)\n",
2137                              GetCurrentThreadId(), hook_proc,
2138                              msg_data->winevent.hook, info.msg.message, info.msg.hwnd, info.msg.wParam,
2139                              info.msg.lParam, msg_data->winevent.tid, info.msg.time);
2140             }
2141             continue;
2142         case MSG_OTHER_PROCESS:
2143             info.flags = ISMEX_SEND;
2144             if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2145                                  &info.msg.lParam, &buffer, size ))
2146             {
2147                 /* ignore it */
2148                 reply_message( &info, 0, TRUE );
2149                 continue;
2150             }
2151             break;
2152         case MSG_HARDWARE:
2153             if (size >= sizeof(msg_data->hardware))
2154             {
2155                 info.msg.pt.x = msg_data->hardware.x;
2156                 info.msg.pt.y = msg_data->hardware.y;
2157                 hw_id         = msg_data->hardware.hw_id;
2158                 if (!process_hardware_message( &info.msg, hw_id, msg_data->hardware.info,
2159                                                hwnd, first, last, flags & PM_REMOVE ))
2160                 {
2161                     TRACE("dropping msg %x\n", info.msg.message );
2162                     continue;  /* ignore it */
2163                 }
2164                 *msg = info.msg;
2165                 thread_info->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
2166                 thread_info->GetMessageTimeVal = info.msg.time;
2167                 thread_info->GetMessageExtraInfoVal = msg_data->hardware.info;
2168                 HeapFree( GetProcessHeap(), 0, buffer );
2169                 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)msg, TRUE );
2170                 return TRUE;
2171             }
2172             continue;
2173         case MSG_POSTED:
2174             if (info.msg.message & 0x80000000)  /* internal message */
2175             {
2176                 if (flags & PM_REMOVE)
2177                 {
2178                     handle_internal_message( info.msg.hwnd, info.msg.message,
2179                                              info.msg.wParam, info.msg.lParam );
2180                     /* if this is a nested call return right away */
2181                     if (first == info.msg.message && last == info.msg.message) return FALSE;
2182                 }
2183                 else
2184                     peek_message( msg, info.msg.hwnd, info.msg.message,
2185                                   info.msg.message, flags | PM_REMOVE, changed_mask );
2186                 continue;
2187             }
2188             if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
2189             {
2190                 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2191                                          &info.msg.lParam, &buffer, size ))
2192                     continue;  /* ignore it */
2193             }
2194             *msg = info.msg;
2195             msg->pt.x = (short)LOWORD( thread_info->GetMessagePosVal );
2196             msg->pt.y = (short)HIWORD( thread_info->GetMessagePosVal );
2197             thread_info->GetMessageTimeVal = info.msg.time;
2198             thread_info->GetMessageExtraInfoVal = 0;
2199             HeapFree( GetProcessHeap(), 0, buffer );
2200             HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)msg, TRUE );
2201             return TRUE;
2202         }
2203
2204         /* if we get here, we have a sent message; call the window procedure */
2205         old_info = thread_info->receive_info;
2206         thread_info->receive_info = &info;
2207         result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
2208                                    info.msg.lParam, (info.type != MSG_ASCII), FALSE,
2209                                    WMCHAR_MAP_RECVMESSAGE );
2210         reply_message( &info, result, TRUE );
2211         thread_info->receive_info = old_info;
2212
2213         /* if some PM_QS* flags were specified, only handle sent messages from now on */
2214         if (HIWORD(flags) && !changed_mask) flags = PM_QS_SENDMESSAGE | LOWORD(flags);
2215     }
2216 }
2217
2218
2219 /***********************************************************************
2220  *           process_sent_messages
2221  *
2222  * Process all pending sent messages.
2223  */
2224 static inline void process_sent_messages(void)
2225 {
2226     MSG msg;
2227     peek_message( &msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE, 0 );
2228 }
2229
2230
2231 /***********************************************************************
2232  *           get_server_queue_handle
2233  *
2234  * Get a handle to the server message queue for the current thread.
2235  */
2236 static HANDLE get_server_queue_handle(void)
2237 {
2238     struct user_thread_info *thread_info = get_user_thread_info();
2239     HANDLE ret;
2240
2241     if (!(ret = thread_info->server_queue))
2242     {
2243         SERVER_START_REQ( get_msg_queue )
2244         {
2245             wine_server_call( req );
2246             ret = wine_server_ptr_handle( reply->handle );
2247         }
2248         SERVER_END_REQ;
2249         thread_info->server_queue = ret;
2250         if (!ret) ERR( "Cannot get server thread queue\n" );
2251     }
2252     return ret;
2253 }
2254
2255
2256 /***********************************************************************
2257  *           wait_message_reply
2258  *
2259  * Wait until a sent message gets replied to.
2260  */
2261 static void wait_message_reply( UINT flags )
2262 {
2263     HANDLE server_queue = get_server_queue_handle();
2264
2265     for (;;)
2266     {
2267         unsigned int wake_bits = 0;
2268
2269         SERVER_START_REQ( set_queue_mask )
2270         {
2271             req->wake_mask    = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
2272             req->changed_mask = req->wake_mask;
2273             req->skip_wait    = 1;
2274             if (!wine_server_call( req ))
2275                 wake_bits = reply->wake_bits;
2276         }
2277         SERVER_END_REQ;
2278
2279         if (wake_bits & QS_SMRESULT) return;  /* got a result */
2280         if (wake_bits & QS_SENDMESSAGE)
2281         {
2282             /* Process the sent message immediately */
2283             process_sent_messages();
2284             continue;
2285         }
2286
2287         wow_handlers.wait_message( 1, &server_queue, INFINITE, QS_SENDMESSAGE, 0 );
2288     }
2289 }
2290
2291 /***********************************************************************
2292  *              put_message_in_queue
2293  *
2294  * Put a sent message into the destination queue.
2295  * For inter-process message, reply_size is set to expected size of reply data.
2296  */
2297 static BOOL put_message_in_queue( const struct send_message_info *info, size_t *reply_size )
2298 {
2299     struct packed_message data;
2300     message_data_t msg_data;
2301     unsigned int res;
2302     int i;
2303     timeout_t timeout = TIMEOUT_INFINITE;
2304
2305     /* Check for INFINITE timeout for compatibility with Win9x,
2306      * although Windows >= NT does not do so
2307      */
2308     if (info->type != MSG_NOTIFY &&
2309         info->type != MSG_CALLBACK &&
2310         info->type != MSG_POSTED &&
2311         info->timeout &&
2312         info->timeout != INFINITE)
2313     {
2314         /* timeout is signed despite the prototype */
2315         timeout = (timeout_t)max( 0, (int)info->timeout ) * -10000;
2316     }
2317
2318     data.count = 0;
2319     if (info->type == MSG_OTHER_PROCESS)
2320     {
2321         *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
2322         if (data.count == -1)
2323         {
2324             WARN( "cannot pack message %x\n", info->msg );
2325             return FALSE;
2326         }
2327     }
2328     else if (info->type == MSG_CALLBACK)
2329     {
2330         msg_data.callback.callback = wine_server_client_ptr( info->callback );
2331         msg_data.callback.data     = info->data;
2332         msg_data.callback.result   = 0;
2333         data.data[0] = &msg_data;
2334         data.size[0] = sizeof(msg_data.callback);
2335         data.count = 1;
2336     }
2337     else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
2338     {
2339         return post_dde_message( &data, info );
2340     }
2341
2342     SERVER_START_REQ( send_message )
2343     {
2344         req->id      = info->dest_tid;
2345         req->type    = info->type;
2346         req->flags   = 0;
2347         req->win     = wine_server_user_handle( info->hwnd );
2348         req->msg     = info->msg;
2349         req->wparam  = info->wparam;
2350         req->lparam  = info->lparam;
2351         req->timeout = timeout;
2352
2353         if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
2354         for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
2355         if ((res = wine_server_call( req )))
2356         {
2357             if (res == STATUS_INVALID_PARAMETER)
2358                 /* FIXME: find a STATUS_ value for this one */
2359                 SetLastError( ERROR_INVALID_THREAD_ID );
2360             else
2361                 SetLastError( RtlNtStatusToDosError(res) );
2362         }
2363     }
2364     SERVER_END_REQ;
2365     return !res;
2366 }
2367
2368
2369 /***********************************************************************
2370  *              retrieve_reply
2371  *
2372  * Retrieve a message reply from the server.
2373  */
2374 static LRESULT retrieve_reply( const struct send_message_info *info,
2375                                size_t reply_size, LRESULT *result )
2376 {
2377     NTSTATUS status;
2378     void *reply_data = NULL;
2379
2380     if (reply_size)
2381     {
2382         if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
2383         {
2384             WARN( "no memory for reply, will be truncated\n" );
2385             reply_size = 0;
2386         }
2387     }
2388     SERVER_START_REQ( get_message_reply )
2389     {
2390         req->cancel = 1;
2391         if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
2392         if (!(status = wine_server_call( req ))) *result = reply->result;
2393         reply_size = wine_server_reply_size( reply );
2394     }
2395     SERVER_END_REQ;
2396     if (!status && reply_size)
2397         unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
2398
2399     HeapFree( GetProcessHeap(), 0, reply_data );
2400
2401     TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx got reply %lx (err=%d)\n",
2402            info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
2403            info->lparam, *result, status );
2404
2405     /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
2406     if (status) SetLastError( RtlNtStatusToDosError(status) );
2407     return !status;
2408 }
2409
2410
2411 /***********************************************************************
2412  *              send_inter_thread_message
2413  */
2414 static LRESULT send_inter_thread_message( const struct send_message_info *info, LRESULT *res_ptr )
2415 {
2416     size_t reply_size = 0;
2417
2418     TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
2419            info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
2420
2421     USER_CheckNotLock();
2422
2423     if (!put_message_in_queue( info, &reply_size )) return 0;
2424
2425     /* there's no reply to wait for on notify/callback messages */
2426     if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
2427
2428     wait_message_reply( info->flags );
2429     return retrieve_reply( info, reply_size, res_ptr );
2430 }
2431
2432
2433 /***********************************************************************
2434  *              send_inter_thread_callback
2435  */
2436 static LRESULT send_inter_thread_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
2437                                            LRESULT *result, void *arg )
2438 {
2439     struct send_message_info *info = arg;
2440     info->hwnd   = hwnd;
2441     info->msg    = msg;
2442     info->wparam = wp;
2443     info->lparam = lp;
2444     return send_inter_thread_message( info, result );
2445 }
2446
2447
2448 /***********************************************************************
2449  *              send_message
2450  *
2451  * Backend implementation of the various SendMessage functions.
2452  */
2453 static BOOL send_message( struct send_message_info *info, DWORD_PTR *res_ptr, BOOL unicode )
2454 {
2455     DWORD dest_pid;
2456     BOOL ret;
2457     LRESULT result;
2458
2459     if (is_broadcast(info->hwnd))
2460     {
2461         EnumWindows( broadcast_message_callback, (LPARAM)info );
2462         if (res_ptr) *res_ptr = 1;
2463         return TRUE;
2464     }
2465
2466     if (!(info->dest_tid = GetWindowThreadProcessId( info->hwnd, &dest_pid ))) return FALSE;
2467
2468     if (USER_IsExitingThread( info->dest_tid )) return FALSE;
2469
2470     SPY_EnterMessage( SPY_SENDMESSAGE, info->hwnd, info->msg, info->wparam, info->lparam );
2471
2472     if (info->dest_tid == GetCurrentThreadId())
2473     {
2474         result = call_window_proc( info->hwnd, info->msg, info->wparam, info->lparam,
2475                                    unicode, TRUE, info->wm_char );
2476         if (info->type == MSG_CALLBACK)
2477             call_sendmsg_callback( info->callback, info->hwnd, info->msg, info->data, result );
2478         ret = TRUE;
2479     }
2480     else
2481     {
2482         if (dest_pid != GetCurrentProcessId() && (info->type == MSG_ASCII || info->type == MSG_UNICODE))
2483             info->type = MSG_OTHER_PROCESS;
2484
2485         /* MSG_ASCII can be sent unconverted except for WM_CHAR; everything else needs to be Unicode */
2486         if (!unicode && is_unicode_message( info->msg ) &&
2487             (info->type != MSG_ASCII || info->msg == WM_CHAR))
2488             ret = WINPROC_CallProcAtoW( send_inter_thread_callback, info->hwnd, info->msg,
2489                                         info->wparam, info->lparam, &result, info, info->wm_char );
2490         else
2491             ret = send_inter_thread_message( info, &result );
2492     }
2493
2494     SPY_ExitMessage( SPY_RESULT_OK, info->hwnd, info->msg, result, info->wparam, info->lparam );
2495     if (ret && res_ptr) *res_ptr = result;
2496     return ret;
2497 }
2498
2499
2500 /***********************************************************************
2501  *              MSG_SendInternalMessageTimeout
2502  *
2503  * Same as SendMessageTimeoutW but sends the message to a specific thread
2504  * without requiring a window handle. Only works for internal Wine messages.
2505  */
2506 LRESULT MSG_SendInternalMessageTimeout( DWORD dest_pid, DWORD dest_tid,
2507                                         UINT msg, WPARAM wparam, LPARAM lparam,
2508                                         UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2509 {
2510     struct send_message_info info;
2511     LRESULT ret, result;
2512
2513     assert( msg & 0x80000000 );  /* must be an internal Wine message */
2514
2515     info.type     = MSG_UNICODE;
2516     info.dest_tid = dest_tid;
2517     info.hwnd     = 0;
2518     info.msg      = msg;
2519     info.wparam   = wparam;
2520     info.lparam   = lparam;
2521     info.flags    = flags;
2522     info.timeout  = timeout;
2523
2524     if (USER_IsExitingThread( dest_tid )) return 0;
2525
2526     if (dest_tid == GetCurrentThreadId())
2527     {
2528         result = handle_internal_message( 0, msg, wparam, lparam );
2529         ret = 1;
2530     }
2531     else
2532     {
2533         if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
2534         ret = send_inter_thread_message( &info, &result );
2535     }
2536     if (ret && res_ptr) *res_ptr = result;
2537     return ret;
2538 }
2539
2540
2541 /***********************************************************************
2542  *              SendMessageTimeoutW  (USER32.@)
2543  */
2544 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2545                                     UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2546 {
2547     struct send_message_info info;
2548
2549     info.type    = MSG_UNICODE;
2550     info.hwnd    = hwnd;
2551     info.msg     = msg;
2552     info.wparam  = wparam;
2553     info.lparam  = lparam;
2554     info.flags   = flags;
2555     info.timeout = timeout;
2556
2557     return send_message( &info, res_ptr, TRUE );
2558 }
2559
2560 /***********************************************************************
2561  *              SendMessageTimeoutA  (USER32.@)
2562  */
2563 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2564                                     UINT flags, UINT timeout, PDWORD_PTR res_ptr )
2565 {
2566     struct send_message_info info;
2567
2568     info.type    = MSG_ASCII;
2569     info.hwnd    = hwnd;
2570     info.msg     = msg;
2571     info.wparam  = wparam;
2572     info.lparam  = lparam;
2573     info.flags   = flags;
2574     info.timeout = timeout;
2575     info.wm_char  = WMCHAR_MAP_SENDMESSAGETIMEOUT;
2576
2577     return send_message( &info, res_ptr, FALSE );
2578 }
2579
2580
2581 /***********************************************************************
2582  *              SendMessageW  (USER32.@)
2583  */
2584 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2585 {
2586     DWORD_PTR res = 0;
2587     struct send_message_info info;
2588
2589     info.type    = MSG_UNICODE;
2590     info.hwnd    = hwnd;
2591     info.msg     = msg;
2592     info.wparam  = wparam;
2593     info.lparam  = lparam;
2594     info.flags   = SMTO_NORMAL;
2595     info.timeout = 0;
2596
2597     send_message( &info, &res, TRUE );
2598     return res;
2599 }
2600
2601
2602 /***********************************************************************
2603  *              SendMessageA  (USER32.@)
2604  */
2605 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2606 {
2607     DWORD_PTR res = 0;
2608     struct send_message_info info;
2609
2610     info.type    = MSG_ASCII;
2611     info.hwnd    = hwnd;
2612     info.msg     = msg;
2613     info.wparam  = wparam;
2614     info.lparam  = lparam;
2615     info.flags   = SMTO_NORMAL;
2616     info.timeout = 0;
2617     info.wm_char  = WMCHAR_MAP_SENDMESSAGE;
2618
2619     send_message( &info, &res, FALSE );
2620     return res;
2621 }
2622
2623
2624 /***********************************************************************
2625  *              SendNotifyMessageA  (USER32.@)
2626  */
2627 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2628 {
2629     struct send_message_info info;
2630
2631     if (is_pointer_message(msg))
2632     {
2633         SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2634         return FALSE;
2635     }
2636
2637     info.type    = MSG_NOTIFY;
2638     info.hwnd    = hwnd;
2639     info.msg     = msg;
2640     info.wparam  = wparam;
2641     info.lparam  = lparam;
2642     info.flags   = 0;
2643     info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
2644
2645     return send_message( &info, NULL, FALSE );
2646 }
2647
2648
2649 /***********************************************************************
2650  *              SendNotifyMessageW  (USER32.@)
2651  */
2652 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2653 {
2654     struct send_message_info info;
2655
2656     if (is_pointer_message(msg))
2657     {
2658         SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2659         return FALSE;
2660     }
2661
2662     info.type    = MSG_NOTIFY;
2663     info.hwnd    = hwnd;
2664     info.msg     = msg;
2665     info.wparam  = wparam;
2666     info.lparam  = lparam;
2667     info.flags   = 0;
2668
2669     return send_message( &info, NULL, TRUE );
2670 }
2671
2672
2673 /***********************************************************************
2674  *              SendMessageCallbackA  (USER32.@)
2675  */
2676 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2677                                   SENDASYNCPROC callback, ULONG_PTR data )
2678 {
2679     struct send_message_info info;
2680
2681     if (is_pointer_message(msg))
2682     {
2683         SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2684         return FALSE;
2685     }
2686
2687     info.type     = MSG_CALLBACK;
2688     info.hwnd     = hwnd;
2689     info.msg      = msg;
2690     info.wparam   = wparam;
2691     info.lparam   = lparam;
2692     info.callback = callback;
2693     info.data     = data;
2694     info.flags    = 0;
2695     info.wm_char  = WMCHAR_MAP_SENDMESSAGETIMEOUT;
2696
2697     return send_message( &info, NULL, FALSE );
2698 }
2699
2700
2701 /***********************************************************************
2702  *              SendMessageCallbackW  (USER32.@)
2703  */
2704 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2705                                   SENDASYNCPROC callback, ULONG_PTR data )
2706 {
2707     struct send_message_info info;
2708
2709     if (is_pointer_message(msg))
2710     {
2711         SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2712         return FALSE;
2713     }
2714
2715     info.type     = MSG_CALLBACK;
2716     info.hwnd     = hwnd;
2717     info.msg      = msg;
2718     info.wparam   = wparam;
2719     info.lparam   = lparam;
2720     info.callback = callback;
2721     info.data     = data;
2722     info.flags    = 0;
2723
2724     return send_message( &info, NULL, TRUE );
2725 }
2726
2727
2728 /***********************************************************************
2729  *              ReplyMessage  (USER32.@)
2730  */
2731 BOOL WINAPI ReplyMessage( LRESULT result )
2732 {
2733     struct received_message_info *info = get_user_thread_info()->receive_info;
2734
2735     if (!info) return FALSE;
2736     reply_message( info, result, FALSE );
2737     return TRUE;
2738 }
2739
2740
2741 /***********************************************************************
2742  *              InSendMessage  (USER32.@)
2743  */
2744 BOOL WINAPI InSendMessage(void)
2745 {
2746     return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
2747 }
2748
2749
2750 /***********************************************************************
2751  *              InSendMessageEx  (USER32.@)
2752  */
2753 DWORD WINAPI InSendMessageEx( LPVOID reserved )
2754 {
2755     struct received_message_info *info = get_user_thread_info()->receive_info;
2756
2757     if (info) return info->flags;
2758     return ISMEX_NOSEND;
2759 }
2760
2761
2762 /***********************************************************************
2763  *              PostMessageA  (USER32.@)
2764  */
2765 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2766 {
2767     if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
2768     return PostMessageW( hwnd, msg, wparam, lparam );
2769 }
2770
2771
2772 /***********************************************************************
2773  *              PostMessageW  (USER32.@)
2774  */
2775 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2776 {
2777     struct send_message_info info;
2778
2779     if (is_pointer_message( msg ))
2780     {
2781         SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2782         return FALSE;
2783     }
2784
2785     TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
2786            hwnd, msg, SPY_GetMsgName(msg, hwnd), wparam, lparam );
2787
2788     info.type   = MSG_POSTED;
2789     info.hwnd   = hwnd;
2790     info.msg    = msg;
2791     info.wparam = wparam;
2792     info.lparam = lparam;
2793     info.flags  = 0;
2794
2795     if (is_broadcast(hwnd))
2796     {
2797         EnumWindows( broadcast_message_callback, (LPARAM)&info );
2798         return TRUE;
2799     }
2800
2801     if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2802
2803     if (!(info.dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2804
2805     if (USER_IsExitingThread( info.dest_tid )) return TRUE;
2806
2807     return put_message_in_queue( &info, NULL );
2808 }
2809
2810
2811 /**********************************************************************
2812  *              PostThreadMessageA  (USER32.@)
2813  */
2814 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2815 {
2816     if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
2817     return PostThreadMessageW( thread, msg, wparam, lparam );
2818 }
2819
2820
2821 /**********************************************************************
2822  *              PostThreadMessageW  (USER32.@)
2823  */
2824 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2825 {
2826     struct send_message_info info;
2827
2828     if (is_pointer_message( msg ))
2829     {
2830         SetLastError( ERROR_MESSAGE_SYNC_ONLY );
2831         return FALSE;
2832     }
2833     if (USER_IsExitingThread( thread )) return TRUE;
2834
2835     info.type     = MSG_POSTED;
2836     info.dest_tid = thread;
2837     info.hwnd     = 0;
2838     info.msg      = msg;
2839     info.wparam   = wparam;
2840     info.lparam   = lparam;
2841     info.flags    = 0;
2842     return put_message_in_queue( &info, NULL );
2843 }
2844
2845
2846 /***********************************************************************
2847  *              PostQuitMessage  (USER32.@)
2848  *
2849  * Posts a quit message to the current thread's message queue.
2850  *
2851  * PARAMS
2852  *  exit_code [I] Exit code to return from message loop.
2853  *
2854  * RETURNS
2855  *  Nothing.
2856  *
2857  * NOTES
2858  *  This function is not the same as calling:
2859  *|PostThreadMessage(GetCurrentThreadId(), WM_QUIT, exit_code, 0);
2860  *  It instead sets a flag in the message queue that signals it to generate
2861  *  a WM_QUIT message when there are no other pending sent or posted messages
2862  *  in the queue.
2863  */
2864 void WINAPI PostQuitMessage( INT exit_code )
2865 {
2866     SERVER_START_REQ( post_quit_message )
2867     {
2868         req->exit_code = exit_code;
2869         wine_server_call( req );
2870     }
2871     SERVER_END_REQ;
2872 }
2873
2874
2875 /***********************************************************************
2876  *              PeekMessageW  (USER32.@)
2877  */
2878 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2879 {
2880     MSG msg;
2881
2882     USER_CheckNotLock();
2883
2884     /* check for graphics events */
2885     USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
2886
2887     if (!peek_message( &msg, hwnd, first, last, flags, 0 ))
2888     {
2889         if (!(flags & PM_NOYIELD)) wow_handlers.wait_message( 0, NULL, 0, 0, 0 );
2890         return FALSE;
2891     }
2892
2893     /* copy back our internal safe copy of message data to msg_out.
2894      * msg_out is a variable from the *program*, so it can't be used
2895      * internally as it can get "corrupted" by our use of SendMessage()
2896      * (back to the program) inside the message handling itself. */
2897     if (!msg_out)
2898     {
2899         SetLastError( ERROR_NOACCESS );
2900         return FALSE;
2901     }
2902     *msg_out = msg;
2903     return TRUE;
2904 }
2905
2906
2907 /***********************************************************************
2908  *              PeekMessageA  (USER32.@)
2909  */
2910 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2911 {
2912     if (get_pending_wmchar( msg, first, last, (flags & PM_REMOVE) )) return TRUE;
2913     if (!PeekMessageW( msg, hwnd, first, last, flags )) return FALSE;
2914     map_wparam_WtoA( msg, (flags & PM_REMOVE) );
2915     return TRUE;
2916 }
2917
2918
2919 /***********************************************************************
2920  *              GetMessageW  (USER32.@)
2921  */
2922 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2923 {
2924     HANDLE server_queue = get_server_queue_handle();
2925     unsigned int mask = QS_POSTMESSAGE | QS_SENDMESSAGE;  /* Always selected */
2926
2927     USER_CheckNotLock();
2928
2929     /* check for graphics events */
2930     USER_Driver->pMsgWaitForMultipleObjectsEx( 0, NULL, 0, QS_ALLINPUT, 0 );
2931
2932     if (first || last)
2933     {
2934         if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2935         if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2936              ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2937         if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2938         if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2939         if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2940     }
2941     else mask = QS_ALLINPUT;
2942
2943     while (!peek_message( msg, hwnd, first, last, PM_REMOVE | (mask << 16), mask ))
2944     {
2945         wow_handlers.wait_message( 1, &server_queue, INFINITE, mask, 0 );
2946     }
2947
2948     return (msg->message != WM_QUIT);
2949 }
2950
2951
2952 /***********************************************************************
2953  *              GetMessageA  (USER32.@)
2954  */
2955 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2956 {
2957     if (get_pending_wmchar( msg, first, last, TRUE )) return TRUE;
2958     GetMessageW( msg, hwnd, first, last );
2959     map_wparam_WtoA( msg, TRUE );
2960     return (msg->message != WM_QUIT);
2961 }
2962
2963
2964 /***********************************************************************
2965  *              IsDialogMessageA (USER32.@)
2966  *              IsDialogMessage  (USER32.@)
2967  */
2968 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2969 {
2970     MSG msg = *pmsg;
2971     map_wparam_AtoW( msg.message, &msg.wParam, WMCHAR_MAP_NOMAPPING );
2972     return IsDialogMessageW( hwndDlg, &msg );
2973 }
2974
2975
2976 /***********************************************************************
2977  *              TranslateMessage (USER32.@)
2978  *
2979  * Implementation of TranslateMessage.
2980  *
2981  * TranslateMessage translates virtual-key messages into character-messages,
2982  * as follows :
2983  * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
2984  * ditto replacing WM_* with WM_SYS*
2985  * This produces WM_CHAR messages only for keys mapped to ASCII characters
2986  * by the keyboard driver.
2987  *
2988  * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
2989  * return value is nonzero, regardless of the translation.
2990  *
2991  */
2992 BOOL WINAPI TranslateMessage( const MSG *msg )
2993 {
2994     UINT message;
2995     WCHAR wp[2];
2996     BYTE state[256];
2997
2998     if (msg->message < WM_KEYFIRST || msg->message > WM_KEYLAST) return FALSE;
2999     if (msg->message != WM_KEYDOWN && msg->message != WM_SYSKEYDOWN) return TRUE;
3000
3001     TRACE_(key)("Translating key %s (%04lX), scancode %04x\n",
3002                 SPY_GetVKeyName(msg->wParam), msg->wParam, HIWORD(msg->lParam));
3003
3004     switch (msg->wParam)
3005     {
3006     case VK_PACKET:
3007         message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
3008         TRACE_(key)("PostMessageW(%p,%s,%04x,%08x)\n",
3009                     msg->hwnd, SPY_GetMsgName(message, msg->hwnd), HIWORD(msg->lParam), LOWORD(msg->lParam));
3010         PostMessageW( msg->hwnd, message, HIWORD(msg->lParam), LOWORD(msg->lParam));
3011         return TRUE;
3012
3013     case VK_PROCESSKEY:
3014         return ImmTranslateMessage(msg->hwnd, msg->message, msg->wParam, msg->lParam);
3015     }
3016
3017     GetKeyboardState( state );
3018     /* FIXME : should handle ToUnicode yielding 2 */
3019     switch (ToUnicode(msg->wParam, HIWORD(msg->lParam), state, wp, 2, 0))
3020     {
3021     case 1:
3022         message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
3023         TRACE_(key)("1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
3024             msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
3025         PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
3026         break;
3027
3028     case -1:
3029         message = (msg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
3030         TRACE_(key)("-1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
3031             msg->hwnd, SPY_GetMsgName(message, msg->hwnd), wp[0], msg->lParam);
3032         PostMessageW( msg->hwnd, message, wp[0], msg->lParam );
3033         break;
3034     }
3035     return TRUE;
3036 }
3037
3038
3039 /***********************************************************************
3040  *              DispatchMessageA (USER32.@)
3041  *
3042  * See DispatchMessageW.
3043  */
3044 LRESULT WINAPI DECLSPEC_HOTPATCH DispatchMessageA( const MSG* msg )
3045 {
3046     LRESULT retval;
3047
3048       /* Process timer messages */
3049     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3050     {
3051         if (msg->lParam)
3052         {
3053             __TRY
3054             {
3055                 retval = CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
3056                                           msg->message, msg->wParam, GetTickCount() );
3057             }
3058             __EXCEPT_PAGE_FAULT
3059             {
3060                 retval = 0;
3061             }
3062             __ENDTRY
3063             return retval;
3064         }
3065     }
3066     if (!msg->hwnd) return 0;
3067
3068     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3069                       msg->wParam, msg->lParam );
3070
3071     if (!WINPROC_call_window( msg->hwnd, msg->message, msg->wParam, msg->lParam,
3072                               &retval, FALSE, WMCHAR_MAP_DISPATCHMESSAGE ))
3073     {
3074         if (!IsWindow( msg->hwnd )) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3075         else SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3076         retval = 0;
3077     }
3078
3079     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3080                      msg->wParam, msg->lParam );
3081
3082     if (msg->message == WM_PAINT)
3083     {
3084         /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3085         HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3086         GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3087         DeleteObject( hrgn );
3088     }
3089     return retval;
3090 }
3091
3092
3093 /***********************************************************************
3094  *              DispatchMessageW (USER32.@) Process a message
3095  *
3096  * Process the message specified in the structure *_msg_.
3097  *
3098  * If the lpMsg parameter points to a WM_TIMER message and the
3099  * parameter of the WM_TIMER message is not NULL, the lParam parameter
3100  * points to the function that is called instead of the window
3101  * procedure. The function stored in lParam (timer callback) is protected
3102  * from causing page-faults.
3103  *
3104  * The message must be valid.
3105  *
3106  * RETURNS
3107  *
3108  *   DispatchMessage() returns the result of the window procedure invoked.
3109  *
3110  * CONFORMANCE
3111  *
3112  *   ECMA-234, Win32
3113  *
3114  */
3115 LRESULT WINAPI DECLSPEC_HOTPATCH DispatchMessageW( const MSG* msg )
3116 {
3117     LRESULT retval;
3118
3119       /* Process timer messages */
3120     if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
3121     {
3122         if (msg->lParam)
3123         {
3124             __TRY
3125             {
3126                 retval = CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
3127                                           msg->message, msg->wParam, GetTickCount() );
3128             }
3129             __EXCEPT_PAGE_FAULT
3130             {
3131                 retval = 0;
3132             }
3133             __ENDTRY
3134             return retval;
3135         }
3136     }
3137     if (!msg->hwnd) return 0;
3138
3139     SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message,
3140                       msg->wParam, msg->lParam );
3141
3142     if (!WINPROC_call_window( msg->hwnd, msg->message, msg->wParam, msg->lParam,
3143                               &retval, TRUE, WMCHAR_MAP_DISPATCHMESSAGE ))
3144     {
3145         if (!IsWindow( msg->hwnd )) SetLastError( ERROR_INVALID_WINDOW_HANDLE );
3146         else SetLastError( ERROR_MESSAGE_SYNC_ONLY );
3147         retval = 0;
3148     }
3149
3150     SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval,
3151                      msg->wParam, msg->lParam );
3152
3153     if (msg->message == WM_PAINT)
3154     {
3155         /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3156         HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );
3157         GetUpdateRgn( msg->hwnd, hrgn, TRUE );
3158         DeleteObject( hrgn );
3159     }
3160     return retval;
3161 }
3162
3163
3164 /***********************************************************************
3165  *              GetMessagePos (USER.119)
3166  *              GetMessagePos (USER32.@)
3167  *
3168  * The GetMessagePos() function returns a long value representing a
3169  * cursor position, in screen coordinates, when the last message
3170  * retrieved by the GetMessage() function occurs. The x-coordinate is
3171  * in the low-order word of the return value, the y-coordinate is in
3172  * the high-order word. The application can use the MAKEPOINT()
3173  * macro to obtain a POINT structure from the return value.
3174  *
3175  * For the current cursor position, use GetCursorPos().
3176  *
3177  * RETURNS
3178  *
3179  * Cursor position of last message on success, zero on failure.
3180  *
3181  * CONFORMANCE
3182  *
3183  * ECMA-234, Win32
3184  *
3185  */
3186 DWORD WINAPI GetMessagePos(void)
3187 {
3188     return get_user_thread_info()->GetMessagePosVal;
3189 }
3190
3191
3192 /***********************************************************************
3193  *              GetMessageTime (USER.120)
3194  *              GetMessageTime (USER32.@)
3195  *
3196  * GetMessageTime() returns the message time for the last message
3197  * retrieved by the function. The time is measured in milliseconds with
3198  * the same offset as GetTickCount().
3199  *
3200  * Since the tick count wraps, this is only useful for moderately short
3201  * relative time comparisons.
3202  *
3203  * RETURNS
3204  *
3205  * Time of last message on success, zero on failure.
3206  */
3207 LONG WINAPI GetMessageTime(void)
3208 {
3209     return get_user_thread_info()->GetMessageTimeVal;
3210 }
3211
3212
3213 /***********************************************************************
3214  *              GetMessageExtraInfo (USER.288)
3215  *              GetMessageExtraInfo (USER32.@)
3216  */
3217 LPARAM WINAPI GetMessageExtraInfo(void)
3218 {
3219     return get_user_thread_info()->GetMessageExtraInfoVal;
3220 }
3221
3222
3223 /***********************************************************************
3224  *              SetMessageExtraInfo (USER32.@)
3225  */
3226 LPARAM WINAPI SetMessageExtraInfo(LPARAM lParam)
3227 {
3228     struct user_thread_info *thread_info = get_user_thread_info();
3229     LONG old_value = thread_info->GetMessageExtraInfoVal;
3230     thread_info->GetMessageExtraInfoVal = lParam;
3231     return old_value;
3232 }
3233
3234
3235 /***********************************************************************
3236  *              WaitMessage (USER.112) Suspend thread pending messages
3237  *              WaitMessage (USER32.@) Suspend thread pending messages
3238  *
3239  * WaitMessage() suspends a thread until events appear in the thread's
3240  * queue.
3241  */
3242 BOOL WINAPI WaitMessage(void)
3243 {
3244     return (MsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED);
3245 }
3246
3247
3248 /***********************************************************************
3249  *              MsgWaitForMultipleObjectsEx   (USER32.@)
3250  */
3251 DWORD WINAPI MsgWaitForMultipleObjectsEx( DWORD count, CONST HANDLE *pHandles,
3252                                           DWORD timeout, DWORD mask, DWORD flags )
3253 {
3254     HANDLE handles[MAXIMUM_WAIT_OBJECTS];
3255     DWORD i;
3256
3257     if (count > MAXIMUM_WAIT_OBJECTS-1)
3258     {
3259         SetLastError( ERROR_INVALID_PARAMETER );
3260         return WAIT_FAILED;
3261     }
3262
3263     /* set the queue mask */
3264     SERVER_START_REQ( set_queue_mask )
3265     {
3266         req->wake_mask    = (flags & MWMO_INPUTAVAILABLE) ? mask : 0;
3267         req->changed_mask = mask;
3268         req->skip_wait    = 0;
3269         wine_server_call( req );
3270     }
3271     SERVER_END_REQ;
3272
3273     /* add the queue to the handle list */
3274     for (i = 0; i < count; i++) handles[i] = pHandles[i];
3275     handles[count] = get_server_queue_handle();
3276
3277     return wow_handlers.wait_message( count+1, handles, timeout, mask, flags );
3278 }
3279
3280
3281 /***********************************************************************
3282  *              MsgWaitForMultipleObjects (USER32.@)
3283  */
3284 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, CONST HANDLE *handles,
3285                                         BOOL wait_all, DWORD timeout, DWORD mask )
3286 {
3287     return MsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
3288                                         wait_all ? MWMO_WAITALL : 0 );
3289 }
3290
3291
3292 /***********************************************************************
3293  *              WaitForInputIdle (USER32.@)
3294  */
3295 DWORD WINAPI WaitForInputIdle( HANDLE hProcess, DWORD dwTimeOut )
3296 {
3297     DWORD start_time, elapsed, ret;
3298     HANDLE handles[2];
3299
3300     handles[0] = hProcess;
3301     SERVER_START_REQ( get_process_idle_event )
3302     {
3303         req->handle = wine_server_obj_handle( hProcess );
3304         wine_server_call_err( req );
3305         handles[1] = wine_server_ptr_handle( reply->event );
3306     }
3307     SERVER_END_REQ;
3308     if (!handles[1]) return WAIT_FAILED;  /* no event to wait on */
3309
3310     start_time = GetTickCount();
3311     elapsed = 0;
3312
3313     TRACE("waiting for %p\n", handles[1] );
3314     do
3315     {
3316         ret = MsgWaitForMultipleObjects ( 2, handles, FALSE, dwTimeOut - elapsed, QS_SENDMESSAGE );
3317         switch (ret)
3318         {
3319         case WAIT_OBJECT_0:
3320             return 0;
3321         case WAIT_OBJECT_0+2:
3322             process_sent_messages();
3323             break;
3324         case WAIT_TIMEOUT:
3325         case WAIT_FAILED:
3326             TRACE("timeout or error\n");
3327             return ret;
3328         default:
3329             TRACE("finished\n");
3330             return 0;
3331         }
3332         if (dwTimeOut != INFINITE)
3333         {
3334             elapsed = GetTickCount() - start_time;
3335             if (elapsed > dwTimeOut)
3336                 break;
3337         }
3338     }
3339     while (1);
3340
3341     return WAIT_TIMEOUT;
3342 }
3343
3344
3345 /***********************************************************************
3346  *              RegisterWindowMessageA (USER32.@)
3347  *              RegisterWindowMessage (USER.118)
3348  */
3349 UINT WINAPI RegisterWindowMessageA( LPCSTR str )
3350 {
3351     UINT ret = GlobalAddAtomA(str);
3352     TRACE("%s, ret=%x\n", str, ret);
3353     return ret;
3354 }
3355
3356
3357 /***********************************************************************
3358  *              RegisterWindowMessageW (USER32.@)
3359  */
3360 UINT WINAPI RegisterWindowMessageW( LPCWSTR str )
3361 {
3362     UINT ret = GlobalAddAtomW(str);
3363     TRACE("%s ret=%x\n", debugstr_w(str), ret);
3364     return ret;
3365 }
3366
3367 typedef struct BroadcastParm
3368 {
3369     DWORD flags;
3370     LPDWORD recipients;
3371     UINT msg;
3372     WPARAM wp;
3373     LPARAM lp;
3374     DWORD success;
3375     HWINSTA winsta;
3376 } BroadcastParm;
3377
3378 static BOOL CALLBACK bcast_childwindow( HWND hw, LPARAM lp )
3379 {
3380     BroadcastParm *parm = (BroadcastParm*)lp;
3381     DWORD_PTR retval = 0;
3382     LRESULT lresult;
3383
3384     if (parm->flags & BSF_IGNORECURRENTTASK && WIN_IsCurrentProcess(hw))
3385     {
3386         TRACE("Not telling myself %p\n", hw);
3387         return TRUE;
3388     }
3389
3390     /* I don't know 100% for sure if this is what Windows does, but it fits the tests */
3391     if (parm->flags & BSF_QUERY)
3392     {
3393         TRACE("Telling window %p using SendMessageTimeout\n", hw);
3394
3395         /* Not tested for conflicting flags */
3396         if (parm->flags & BSF_FORCEIFHUNG || parm->flags & BSF_NOHANG)
3397             lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_ABORTIFHUNG, 2000, &retval );
3398         else if (parm->flags & BSF_NOTIMEOUTIFNOTHUNG)
3399             lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NOTIMEOUTIFNOTHUNG, 2000, &retval );
3400         else
3401             lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NORMAL, 2000, &retval );
3402
3403         if (!lresult && GetLastError() == ERROR_TIMEOUT)
3404         {
3405             WARN("Timed out!\n");
3406             if (!(parm->flags & BSF_FORCEIFHUNG))
3407                 goto fail;
3408         }
3409         if (retval == BROADCAST_QUERY_DENY)
3410             goto fail;
3411
3412         return TRUE;
3413
3414 fail:
3415         parm->success = 0;
3416         return FALSE;
3417     }
3418     else if (parm->flags & BSF_POSTMESSAGE)
3419     {
3420         TRACE("Telling window %p using PostMessage\n", hw);
3421         PostMessageW( hw, parm->msg, parm->wp, parm->lp );
3422     }
3423     else
3424     {
3425         TRACE("Telling window %p using SendNotifyMessage\n", hw);
3426         SendNotifyMessageW( hw, parm->msg, parm->wp, parm->lp );
3427     }
3428
3429     return TRUE;
3430 }
3431
3432 static BOOL CALLBACK bcast_desktop( LPWSTR desktop, LPARAM lp )
3433 {
3434     BOOL ret;
3435     HDESK hdesktop;
3436     BroadcastParm *parm = (BroadcastParm*)lp;
3437
3438     TRACE("desktop: %s\n", debugstr_w( desktop ));
3439
3440     hdesktop = open_winstation_desktop( parm->winsta, desktop, 0, FALSE, DESKTOP_ENUMERATE|DESKTOP_WRITEOBJECTS|STANDARD_RIGHTS_WRITE );
3441     if (!hdesktop)
3442     {
3443         FIXME("Could not open desktop %s\n", debugstr_w(desktop));
3444         return TRUE;
3445     }
3446
3447     ret = EnumDesktopWindows( hdesktop, bcast_childwindow, lp );
3448     CloseDesktop(hdesktop);
3449     TRACE("-->%d\n", ret);
3450     return parm->success;
3451 }
3452
3453 static BOOL CALLBACK bcast_winsta( LPWSTR winsta, LPARAM lp )
3454 {
3455     BOOL ret;
3456     HWINSTA hwinsta = OpenWindowStationW( winsta, FALSE, WINSTA_ENUMDESKTOPS );
3457     TRACE("hwinsta: %p/%s/%08x\n", hwinsta, debugstr_w( winsta ), GetLastError());
3458     if (!hwinsta)
3459         return TRUE;
3460     ((BroadcastParm *)lp)->winsta = hwinsta;
3461     ret = EnumDesktopsW( hwinsta, bcast_desktop, lp );
3462     CloseWindowStation( hwinsta );
3463     TRACE("-->%d\n", ret);
3464     return ret;
3465 }
3466
3467 /***********************************************************************
3468  *              BroadcastSystemMessageA (USER32.@)
3469  *              BroadcastSystemMessage  (USER32.@)
3470  */
3471 LONG WINAPI BroadcastSystemMessageA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
3472 {
3473     return BroadcastSystemMessageExA( flags, recipients, msg, wp, lp, NULL );
3474 }
3475
3476
3477 /***********************************************************************
3478  *              BroadcastSystemMessageW (USER32.@)
3479  */
3480 LONG WINAPI BroadcastSystemMessageW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
3481 {
3482     return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, NULL );
3483 }
3484
3485 /***********************************************************************
3486  *              BroadcastSystemMessageExA (USER32.@)
3487  */
3488 LONG WINAPI BroadcastSystemMessageExA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
3489 {
3490     map_wparam_AtoW( msg, &wp, WMCHAR_MAP_NOMAPPING );
3491     return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, NULL );
3492 }
3493
3494
3495 /***********************************************************************
3496  *              BroadcastSystemMessageExW (USER32.@)
3497  */
3498 LONG WINAPI BroadcastSystemMessageExW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
3499 {
3500     BroadcastParm parm;
3501     DWORD recips = BSM_ALLCOMPONENTS;
3502     BOOL ret = TRUE;
3503     static const DWORD all_flags = ( BSF_QUERY | BSF_IGNORECURRENTTASK | BSF_FLUSHDISK | BSF_NOHANG
3504                                    | BSF_POSTMESSAGE | BSF_FORCEIFHUNG | BSF_NOTIMEOUTIFNOTHUNG
3505                                    | BSF_ALLOWSFW | BSF_SENDNOTIFYMESSAGE | BSF_RETURNHDESK | BSF_LUID );
3506
3507     TRACE("Flags: %08x, recipients: %p(0x%x), msg: %04x, wparam: %08lx, lparam: %08lx\n", flags, recipients,
3508          (recipients ? *recipients : recips), msg, wp, lp);
3509
3510     if (flags & ~all_flags)
3511     {
3512         SetLastError(ERROR_INVALID_PARAMETER);
3513         return 0;
3514     }
3515
3516     if (!recipients)
3517         recipients = &recips;
3518
3519     if ( pinfo && flags & BSF_QUERY )
3520         FIXME("Not returning PBSMINFO information yet\n");
3521
3522     parm.flags = flags;
3523     parm.recipients = recipients;
3524     parm.msg = msg;
3525     parm.wp = wp;
3526     parm.lp = lp;
3527     parm.success = TRUE;
3528
3529     if (*recipients & BSM_ALLDESKTOPS || *recipients == BSM_ALLCOMPONENTS)
3530         ret = EnumWindowStationsW(bcast_winsta, (LONG_PTR)&parm);
3531     else if (*recipients & BSM_APPLICATIONS)
3532     {
3533         EnumWindows(bcast_childwindow, (LONG_PTR)&parm);
3534         ret = parm.success;
3535     }
3536     else
3537         FIXME("Recipients %08x not supported!\n", *recipients);
3538
3539     return ret;
3540 }
3541
3542 /***********************************************************************
3543  *              SetMessageQueue (USER32.@)
3544  */
3545 BOOL WINAPI SetMessageQueue( INT size )
3546 {
3547     /* now obsolete the message queue will be expanded dynamically as necessary */
3548     return TRUE;
3549 }
3550
3551
3552 /***********************************************************************
3553  *              MessageBeep (USER32.@)
3554  */
3555 BOOL WINAPI MessageBeep( UINT i )
3556 {
3557     BOOL active = TRUE;
3558     SystemParametersInfoA( SPI_GETBEEP, 0, &active, FALSE );
3559     if (active) USER_Driver->pBeep();
3560     return TRUE;
3561 }
3562
3563
3564 /***********************************************************************
3565  *              SetTimer (USER32.@)
3566  */
3567 UINT_PTR WINAPI SetTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3568 {
3569     UINT_PTR ret;
3570     WNDPROC winproc = 0;
3571
3572     if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, FALSE );
3573
3574     SERVER_START_REQ( set_win_timer )
3575     {
3576         req->win    = wine_server_user_handle( hwnd );
3577         req->msg    = WM_TIMER;
3578         req->id     = id;
3579         req->rate   = max( timeout, SYS_TIMER_RATE );
3580         req->lparam = (ULONG_PTR)winproc;
3581         if (!wine_server_call_err( req ))
3582         {
3583             ret = reply->id;
3584             if (!ret) ret = TRUE;
3585         }
3586         else ret = 0;
3587     }
3588     SERVER_END_REQ;
3589
3590     TRACE("Added %p %lx %p timeout %d\n", hwnd, id, winproc, timeout );
3591     return ret;
3592 }
3593
3594
3595 /***********************************************************************
3596  *              SetSystemTimer (USER32.@)
3597  */
3598 UINT_PTR WINAPI SetSystemTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
3599 {
3600     UINT_PTR ret;
3601     WNDPROC winproc = 0;
3602
3603     if (proc) winproc = WINPROC_AllocProc( (WNDPROC)proc, FALSE );
3604
3605     SERVER_START_REQ( set_win_timer )
3606     {
3607         req->win    = wine_server_user_handle( hwnd );
3608         req->msg    = WM_SYSTIMER;
3609         req->id     = id;
3610         req->rate   = max( timeout, SYS_TIMER_RATE );
3611         req->lparam = (ULONG_PTR)winproc;
3612         if (!wine_server_call_err( req ))
3613         {
3614             ret = reply->id;
3615             if (!ret) ret = TRUE;
3616         }
3617         else ret = 0;
3618     }
3619     SERVER_END_REQ;
3620
3621     TRACE("Added %p %lx %p timeout %d\n", hwnd, id, winproc, timeout );
3622     return ret;
3623 }
3624
3625
3626 /***********************************************************************
3627  *              KillTimer (USER32.@)
3628  */
3629 BOOL WINAPI KillTimer( HWND hwnd, UINT_PTR id )
3630 {
3631     BOOL ret;
3632
3633     SERVER_START_REQ( kill_win_timer )
3634     {
3635         req->win = wine_server_user_handle( hwnd );
3636         req->msg = WM_TIMER;
3637         req->id  = id;
3638         ret = !wine_server_call_err( req );
3639     }
3640     SERVER_END_REQ;
3641     return ret;
3642 }
3643
3644
3645 /***********************************************************************
3646  *              KillSystemTimer (USER32.@)
3647  */
3648 BOOL WINAPI KillSystemTimer( HWND hwnd, UINT_PTR id )
3649 {
3650     BOOL ret;
3651
3652     SERVER_START_REQ( kill_win_timer )
3653     {
3654         req->win = wine_server_user_handle( hwnd );
3655         req->msg = WM_SYSTIMER;
3656         req->id  = id;
3657         ret = !wine_server_call_err( req );
3658     }
3659     SERVER_END_REQ;
3660     return ret;
3661 }
3662
3663
3664 /**********************************************************************
3665  *              GetGUIThreadInfo  (USER32.@)
3666  */
3667 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
3668 {
3669     BOOL ret;
3670
3671     SERVER_START_REQ( get_thread_input )
3672     {
3673         req->tid = id;
3674         if ((ret = !wine_server_call_err( req )))
3675         {
3676             info->flags          = 0;
3677             info->hwndActive     = wine_server_ptr_handle( reply->active );
3678             info->hwndFocus      = wine_server_ptr_handle( reply->focus );
3679             info->hwndCapture    = wine_server_ptr_handle( reply->capture );
3680             info->hwndMenuOwner  = wine_server_ptr_handle( reply->menu_owner );
3681             info->hwndMoveSize   = wine_server_ptr_handle( reply->move_size );
3682             info->hwndCaret      = wine_server_ptr_handle( reply->caret );
3683             info->rcCaret.left   = reply->rect.left;
3684             info->rcCaret.top    = reply->rect.top;
3685             info->rcCaret.right  = reply->rect.right;
3686             info->rcCaret.bottom = reply->rect.bottom;
3687             if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
3688             if (reply->move_size) info->flags |= GUI_INMOVESIZE;
3689             if (reply->caret) info->flags |= GUI_CARETBLINKING;
3690         }
3691     }
3692     SERVER_END_REQ;
3693     return ret;
3694 }
3695
3696
3697 /******************************************************************
3698  *              IsHungAppWindow (USER32.@)
3699  *
3700  */
3701 BOOL WINAPI IsHungAppWindow( HWND hWnd )
3702 {
3703     BOOL ret;
3704
3705     SERVER_START_REQ( is_window_hung )
3706     {
3707         req->win = wine_server_user_handle( hWnd );
3708         ret = !wine_server_call_err( req ) && reply->is_hung;
3709     }
3710     SERVER_END_REQ;
3711     return ret;
3712 }
3713
3714 /******************************************************************
3715  *      ChangeWindowMessageFilter (USER32.@)
3716  */
3717 BOOL WINAPI ChangeWindowMessageFilter( UINT message, DWORD flag )
3718 {
3719     FIXME( "%x %08x\n", message, flag );
3720     return TRUE;
3721 }