Added mappings for a few messages.
[wine] / dlls / user / message.c
1 /*
2  * Window messaging support
3  *
4  * Copyright 2001 Alexandre Julliard
5  */
6
7 #include "winbase.h"
8 #include "wingdi.h"
9 #include "winuser.h"
10 #include "winerror.h"
11 #include "winnls.h"
12 #include "dde.h"
13 #include "wine/unicode.h"
14 #include "wine/server.h"
15 #include "queue.h"
16 #include "input.h"
17 #include "message.h"
18 #include "hook.h"
19 #include "spy.h"
20 #include "user.h"
21 #include "win.h"
22 #include "debugtools.h"
23
24 DEFAULT_DEBUG_CHANNEL(msg);
25
26 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
27 #define WM_NCMOUSELAST  WM_NCMBUTTONDBLCLK
28
29 #define MAX_PACK_COUNT 4
30
31 /* description of the data fields that need to be packed along with a sent message */
32 struct packed_message
33 {
34     int         count;
35     const void *data[MAX_PACK_COUNT];
36     size_t      size[MAX_PACK_COUNT];
37 };
38
39 /* info about the message currently being received by the current thread */
40 struct received_message_info
41 {
42     enum message_type type;
43     MSG               msg;
44     UINT              flags;  /* InSendMessageEx return flags */
45 };
46
47 /* structure to group all parameters for sent messages of the various kinds */
48 struct send_message_info
49 {
50     enum message_type type;
51     HWND              hwnd;
52     UINT              msg;
53     WPARAM            wparam;
54     LPARAM            lparam;
55     UINT              flags;      /* flags for SendMessageTimeout */
56     UINT              timeout;    /* timeout for SendMessageTimeout */
57     SENDASYNCPROC     callback;   /* callback function for SendMessageCallback */
58     ULONG_PTR         data;       /* callback data */
59 };
60
61
62 /* flag for messages that contain pointers */
63 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
64
65 #define SET(msg) (1 << ((msg) & 31))
66
67 static const unsigned int message_pointer_flags[] =
68 {
69     /* 0x00 - 0x1f */
70     SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
71     SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
72     /* 0x20 - 0x3f */
73     SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
74     SET(WM_COMPAREITEM),
75     /* 0x40 - 0x5f */
76     SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
77     SET(WM_NOTIFY) | SET(WM_HELP),
78     /* 0x60 - 0x7f */
79     SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
80     /* 0x80 - 0x9f */
81     SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
82     /* 0xa0 - 0xbf */
83     SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
84     /* 0xc0 - 0xdf */
85     SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
86     /* 0xe0 - 0xff */
87     SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO),
88     /* 0x100 - 0x11f */
89     0,
90     /* 0x120 - 0x13f */
91     0,
92     /* 0x140 - 0x15f */
93     SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
94     SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
95     SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
96     /* 0x160 - 0x17f */
97     0,
98     /* 0x180 - 0x19f */
99     SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
100     SET(LB_DIR) | SET(LB_FINDSTRING) |
101     SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
102     /* 0x1a0 - 0x1bf */
103     SET(LB_FINDSTRINGEXACT),
104     /* 0x1c0 - 0x1df */
105     0,
106     /* 0x1e0 - 0x1ff */
107     0,
108     /* 0x200 - 0x21f */
109     SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
110     /* 0x220 - 0x23f */
111     SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
112     SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
113     /* 0x240 - 0x25f */
114     0,
115     /* 0x260 - 0x27f */
116     0,
117     /* 0x280 - 0x29f */
118     0,
119     /* 0x2a0 - 0x2bf */
120     0,
121     /* 0x2c0 - 0x2df */
122     0,
123     /* 0x2e0 - 0x2ff */
124     0,
125     /* 0x300 - 0x31f */
126     SET(WM_ASKCBFORMATNAME)
127 };
128
129 /* flags for messages that contain Unicode strings */
130 static const unsigned int message_unicode_flags[] =
131 {
132     /* 0x00 - 0x1f */
133     SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
134     SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
135     /* 0x20 - 0x3f */
136     SET(WM_CHARTOITEM),
137     /* 0x40 - 0x5f */
138     0,
139     /* 0x60 - 0x7f */
140     0,
141     /* 0x80 - 0x9f */
142     SET(WM_NCCREATE),
143     /* 0xa0 - 0xbf */
144     0,
145     /* 0xc0 - 0xdf */
146     SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
147     /* 0xe0 - 0xff */
148     0,
149     /* 0x100 - 0x11f */
150     SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
151     /* 0x120 - 0x13f */
152     SET(WM_MENUCHAR),
153     /* 0x140 - 0x15f */
154     SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
155     SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
156     /* 0x160 - 0x17f */
157     0,
158     /* 0x180 - 0x19f */
159     SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
160     SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
161     /* 0x1a0 - 0x1bf */
162     SET(LB_FINDSTRINGEXACT),
163     /* 0x1c0 - 0x1df */
164     0,
165     /* 0x1e0 - 0x1ff */
166     0,
167     /* 0x200 - 0x21f */
168     0,
169     /* 0x220 - 0x23f */
170     SET(WM_MDICREATE),
171     /* 0x240 - 0x25f */
172     0,
173     /* 0x260 - 0x27f */
174     0,
175     /* 0x280 - 0x29f */
176     0,
177     /* 0x2a0 - 0x2bf */
178     0,
179     /* 0x2c0 - 0x2df */
180     0,
181     /* 0x2e0 - 0x2ff */
182     0,
183     /* 0x300 - 0x31f */
184     SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
185 };
186
187 /* check whether a given message type includes pointers */
188 inline static int is_pointer_message( UINT message )
189 {
190     if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
191     return (message_pointer_flags[message / 32] & SET(message)) != 0;
192 }
193
194 /* check whether a given message type contains Unicode (or ASCII) chars */
195 inline static int is_unicode_message( UINT message )
196 {
197     if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
198     return (message_unicode_flags[message / 32] & SET(message)) != 0;
199 }
200
201 #undef SET
202
203 /* compute the total size of the packed data */
204 inline static size_t get_data_total_size( const struct packed_message *data )
205 {
206     int i;
207     size_t total = 0;
208     for (i = 0; i < data->count; i++) total += data->size[i];
209     return total;
210 }
211
212 /* copy all the data of a packed message into a single dest buffer */
213 inline static void copy_all_data( void *dest, const struct packed_message *data )
214 {
215     int i;
216     for (i = 0; i < data->count; i++)
217     {
218         memcpy( dest, data->data[i], data->size[i] );
219         dest = (char *)dest + data->size[i];
220     }
221 }
222
223 /* add a data field to a packed message */
224 inline static void push_data( struct packed_message *data, const void *ptr, size_t size )
225 {
226     data->data[data->count] = ptr;
227     data->size[data->count] = size;
228     data->count++;
229 }
230
231 /* add a string to a packed message */
232 inline static void push_string( struct packed_message *data, LPCWSTR str )
233 {
234     push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
235 }
236
237 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
238 inline static void *get_data( void **buffer, size_t size )
239 {
240     void *ret = *buffer;
241     *buffer = (char *)*buffer + size;
242     return ret;
243 }
244
245 /* make sure that the buffer contains a valid null-terminated Unicode string */
246 inline static BOOL check_string( LPCWSTR str, size_t size )
247 {
248     for (size /= sizeof(WCHAR); size; size--, str++)
249         if (!*str) return TRUE;
250     return FALSE;
251 }
252
253 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
254 inline static void *get_buffer_space( void **buffer, size_t size )
255 {
256     void *ret;
257     if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
258         HeapFree( GetProcessHeap(), 0, *buffer );
259     *buffer = ret;
260     return ret;
261 }
262
263 /* retrieve a string pointer from packed data */
264 inline static LPWSTR get_string( void **buffer )
265 {
266     return get_data( buffer, (strlenW( (LPWSTR)*buffer ) + 1) * sizeof(WCHAR) );
267 }
268
269 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
270 inline static BOOL combobox_has_strings( HWND hwnd )
271 {
272     DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
273     return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
274 }
275
276 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
277 inline static BOOL listbox_has_strings( HWND hwnd )
278 {
279     DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
280     return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
281 }
282
283 /* check if hwnd is a broadcast magic handle */
284 inline static BOOL is_broadcast( HWND hwnd )
285 {
286     return (hwnd == HWND_BROADCAST || hwnd == HWND_TOPMOST);
287 }
288
289
290 /***********************************************************************
291  *              broadcast_message_callback
292  *
293  * Helper callback for broadcasting messages.
294  */
295 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
296 {
297     struct send_message_info *info = (struct send_message_info *)lparam;
298     if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
299     switch(info->type)
300     {
301     case MSG_UNICODE:
302         SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
303                              info->flags, info->timeout, NULL );
304         break;
305     case MSG_ASCII:
306         SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
307                              info->flags, info->timeout, NULL );
308         break;
309     case MSG_NOTIFY:
310         SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
311         break;
312     case MSG_CALLBACK:
313         SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
314                               info->callback, info->data );
315         break;
316     case MSG_POSTED:
317         PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
318         break;
319     default:
320         ERR( "bad type %d\n", info->type );
321         break;
322     }
323     return TRUE;
324 }
325
326
327 /***********************************************************************
328  *              map_wparam_AtoW
329  *
330  * Convert the wparam of an ASCII message to Unicode.
331  */
332 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
333 {
334     if (message == WM_CHARTOITEM ||
335         message == EM_SETPASSWORDCHAR ||
336         message == WM_CHAR ||
337         message == WM_DEADCHAR ||
338         message == WM_SYSCHAR ||
339         message == WM_SYSDEADCHAR ||
340         message == WM_MENUCHAR)
341     {
342         char ch = LOWORD(wparam);
343         WCHAR wch;
344         MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
345         wparam = MAKEWPARAM( wch, HIWORD(wparam) );
346     }
347     return wparam;
348 }
349
350
351 /***********************************************************************
352  *              map_wparam_WtoA
353  *
354  * Convert the wparam of a Unicode message to ASCII.
355  */
356 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
357 {
358     if (message == WM_CHARTOITEM ||
359         message == EM_SETPASSWORDCHAR ||
360         message == WM_CHAR ||
361         message == WM_DEADCHAR ||
362         message == WM_SYSCHAR ||
363         message == WM_SYSDEADCHAR ||
364         message == WM_MENUCHAR)
365     {
366         WCHAR wch = LOWORD(wparam);
367         char ch;
368         WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
369         wparam = MAKEWPARAM( ch, HIWORD(wparam) );
370     }
371     return wparam;
372 }
373
374
375 /***********************************************************************
376  *              pack_message
377  *
378  * Pack a message for sending to another process.
379  * Return the size of the data we expect in the message reply.
380  * Set data->count to -1 if there is an error.
381  */
382 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
383                             struct packed_message *data )
384 {
385     data->count = 0;
386     switch(message)
387     {
388     case WM_NCCREATE:
389     case WM_CREATE:
390     {
391         CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
392         push_data( data, cs, sizeof(*cs) );
393         if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
394         if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
395         return sizeof(*cs);
396     }
397     case WM_GETTEXT:
398     case WM_ASKCBFORMATNAME:
399         return wparam * sizeof(WCHAR);
400     case WM_SETTEXT:
401     case WM_WININICHANGE:
402     case WM_DEVMODECHANGE:
403     case CB_DIR:
404     case CB_FINDSTRING:
405     case CB_FINDSTRINGEXACT:
406     case CB_SELECTSTRING:
407     case LB_DIR:
408     case LB_ADDFILE:
409     case LB_FINDSTRING:
410     case LB_FINDSTRINGEXACT:
411     case LB_SELECTSTRING:
412     case EM_REPLACESEL:
413         push_string( data, (LPWSTR)lparam );
414         return 0;
415     case WM_GETMINMAXINFO:
416         push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
417         return sizeof(MINMAXINFO);
418     case WM_DRAWITEM:
419         push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
420         return 0;
421     case WM_MEASUREITEM:
422         push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
423         return sizeof(MEASUREITEMSTRUCT);
424     case WM_DELETEITEM:
425         push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
426         return 0;
427     case WM_COMPAREITEM:
428         push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
429         return 0;
430     case WM_WINDOWPOSCHANGING:
431     case WM_WINDOWPOSCHANGED:
432         push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
433         return sizeof(WINDOWPOS);
434     case WM_COPYDATA:
435     {
436         COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
437         push_data( data, cp, sizeof(*cp) );
438         if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
439         return 0;
440     }
441     case WM_NOTIFY:
442         /* WM_NOTIFY cannot be sent across processes (MSDN) */
443         data->count = -1;
444         return 0;
445     case WM_HELP:
446         push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
447         return 0;
448     case WM_STYLECHANGING:
449     case WM_STYLECHANGED:
450         push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
451         return 0;
452     case WM_NCCALCSIZE:
453         if (!wparam)
454         {
455             push_data( data, (RECT *)lparam, sizeof(RECT) );
456             return sizeof(RECT);
457         }
458         else
459         {
460             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
461             push_data( data, nc, sizeof(*nc) );
462             push_data( data, nc->lppos, sizeof(*nc->lppos) );
463             return sizeof(*nc) + sizeof(*nc->lppos);
464         }
465     case WM_GETDLGCODE:
466         if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
467         return sizeof(MSG);
468     case SBM_SETSCROLLINFO:
469         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
470         return 0;
471     case SBM_GETSCROLLINFO:
472         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
473         return sizeof(SCROLLINFO);
474     case EM_GETSEL:
475     case SBM_GETRANGE:
476     case CB_GETEDITSEL:
477     {
478         size_t size = 0;
479         if (wparam) size += sizeof(DWORD);
480         if (lparam) size += sizeof(DWORD);
481         return size;
482     }
483     case EM_GETRECT:
484     case LB_GETITEMRECT:
485     case CB_GETDROPPEDCONTROLRECT:
486         return sizeof(RECT);
487     case EM_SETRECT:
488     case EM_SETRECTNP:
489         push_data( data, (RECT *)lparam, sizeof(RECT) );
490         return 0;
491     case EM_GETLINE:
492     {
493         WORD *pw = (WORD *)lparam;
494         push_data( data, pw, sizeof(*pw) );
495         return *pw * sizeof(WCHAR);
496     }
497     case EM_SETTABSTOPS:
498     case LB_SETTABSTOPS:
499         if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
500         return 0;
501     case CB_ADDSTRING:
502     case CB_INSERTSTRING:
503         if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
504         return 0;
505     case CB_GETLBTEXT:
506         if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
507         return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
508     case LB_ADDSTRING:
509     case LB_INSERTSTRING:
510         if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
511         return 0;
512     case LB_GETTEXT:
513         if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
514         return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
515     case LB_GETSELITEMS:
516         return wparam * sizeof(UINT);
517     case WM_NEXTMENU:
518         push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
519         return sizeof(MDINEXTMENU);
520     case WM_SIZING:
521     case WM_MOVING:
522         push_data( data, (RECT *)lparam, sizeof(RECT) );
523         return sizeof(RECT);
524     case WM_MDICREATE:
525     {
526         MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
527         push_data( data, cs, sizeof(*cs) );
528         if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
529         if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
530         return sizeof(*cs);
531     }
532     case WM_MDIGETACTIVE:
533         if (lparam) return sizeof(BOOL);
534         return 0;
535
536     /* these contain an HFONT */
537     case WM_SETFONT:
538     case WM_GETFONT:
539     /* these contain an HDC */
540     case WM_PAINT:
541     case WM_ERASEBKGND:
542     case WM_ICONERASEBKGND:
543     case WM_NCPAINT:
544     case WM_CTLCOLORMSGBOX:
545     case WM_CTLCOLOREDIT:
546     case WM_CTLCOLORLISTBOX:
547     case WM_CTLCOLORBTN:
548     case WM_CTLCOLORDLG:
549     case WM_CTLCOLORSCROLLBAR:
550     case WM_CTLCOLORSTATIC:
551     case WM_PRINT:
552     case WM_PRINTCLIENT:
553     /* these contain an HGLOBAL */
554     case WM_PAINTCLIPBOARD:
555     case WM_SIZECLIPBOARD:
556     case WM_DDE_INITIATE:
557     case WM_DDE_ADVISE:
558     case WM_DDE_UNADVISE:
559     case WM_DDE_DATA:
560     case WM_DDE_REQUEST:
561     case WM_DDE_POKE:
562     case WM_DDE_EXECUTE:
563     /* these contain pointers */
564     case WM_DROPOBJECT:
565     case WM_QUERYDROPOBJECT:
566     case WM_DRAGLOOP:
567     case WM_DRAGSELECT:
568     case WM_DRAGMOVE:
569     case WM_DEVICECHANGE:
570         FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message) );
571         data->count = -1;
572         return 0;
573     }
574     return 0;
575 }
576
577
578 /***********************************************************************
579  *              unpack_message
580  *
581  * Unpack a message received from another process.
582  */
583 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
584                             void **buffer, size_t size )
585 {
586     size_t minsize = 0;
587
588     switch(message)
589     {
590     case WM_NCCREATE:
591     case WM_CREATE:
592     {
593         CREATESTRUCTW *cs = *buffer;
594         WCHAR *str = (WCHAR *)(cs + 1);
595         if (size < sizeof(*cs)) return FALSE;
596         size -= sizeof(*cs);
597         if (HIWORD(cs->lpszName))
598         {
599             if (!check_string( str, size )) return FALSE;
600             cs->lpszName = str;
601             size -= (strlenW(str) + 1) * sizeof(WCHAR);
602             str += strlenW(str) + 1;
603         }
604         if (HIWORD(cs->lpszClass))
605         {
606             if (!check_string( str, size )) return FALSE;
607             cs->lpszClass = str;
608         }
609         break;
610     }
611     case WM_GETTEXT:
612     case WM_ASKCBFORMATNAME:
613         if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
614         break;
615     case WM_SETTEXT:
616     case WM_WININICHANGE:
617     case WM_DEVMODECHANGE:
618     case CB_DIR:
619     case CB_FINDSTRING:
620     case CB_FINDSTRINGEXACT:
621     case CB_SELECTSTRING:
622     case LB_DIR:
623     case LB_ADDFILE:
624     case LB_FINDSTRING:
625     case LB_FINDSTRINGEXACT:
626     case LB_SELECTSTRING:
627     case EM_REPLACESEL:
628         if (!check_string( *buffer, size )) return FALSE;
629         break;
630     case WM_GETMINMAXINFO:
631         minsize = sizeof(MINMAXINFO);
632         break;
633     case WM_DRAWITEM:
634         minsize = sizeof(DRAWITEMSTRUCT);
635         break;
636     case WM_MEASUREITEM:
637         minsize = sizeof(MEASUREITEMSTRUCT);
638         break;
639     case WM_DELETEITEM:
640         minsize = sizeof(DELETEITEMSTRUCT);
641         break;
642     case WM_COMPAREITEM:
643         minsize = sizeof(COMPAREITEMSTRUCT);
644         break;
645     case WM_WINDOWPOSCHANGING:
646     case WM_WINDOWPOSCHANGED:
647         minsize = sizeof(WINDOWPOS);
648         break;
649     case WM_COPYDATA:
650     {
651         COPYDATASTRUCT *cp = *buffer;
652         if (size < sizeof(*cp)) return FALSE;
653         if (cp->lpData)
654         {
655             minsize = sizeof(*cp) + cp->cbData;
656             cp->lpData = cp + 1;
657         }
658         break;
659     }
660     case WM_NOTIFY:
661         /* WM_NOTIFY cannot be sent across processes (MSDN) */
662         return FALSE;
663     case WM_HELP:
664         minsize = sizeof(HELPINFO);
665         break;
666     case WM_STYLECHANGING:
667     case WM_STYLECHANGED:
668         minsize = sizeof(STYLESTRUCT);
669         break;
670     case WM_NCCALCSIZE:
671         if (!*wparam) minsize = sizeof(RECT);
672         else
673         {
674             NCCALCSIZE_PARAMS *nc = *buffer;
675             if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
676             nc->lppos = (WINDOWPOS *)(nc + 1);
677         }
678         break;
679     case WM_GETDLGCODE:
680         if (!*lparam) return TRUE;
681         minsize = sizeof(MSG);
682         break;
683     case SBM_SETSCROLLINFO:
684         minsize = sizeof(SCROLLINFO);
685         break;
686     case SBM_GETSCROLLINFO:
687         if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
688         break;
689     case EM_GETSEL:
690     case SBM_GETRANGE:
691     case CB_GETEDITSEL:
692         if (*wparam || *lparam)
693         {
694             if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
695             if (*wparam) *wparam = (WPARAM)*buffer;
696             if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
697         }
698         return TRUE;
699     case EM_GETRECT:
700     case LB_GETITEMRECT:
701     case CB_GETDROPPEDCONTROLRECT:
702         if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
703         break;
704     case EM_SETRECT:
705     case EM_SETRECTNP:
706         minsize = sizeof(RECT);
707         break;
708     case EM_GETLINE:
709     {
710         WORD len;
711         if (size < sizeof(WORD)) return FALSE;
712         len = *(WORD *)*buffer;
713         if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
714         *lparam = (LPARAM)*buffer + sizeof(WORD);  /* don't erase WORD at start of buffer */
715         return TRUE;
716     }
717     case EM_SETTABSTOPS:
718     case LB_SETTABSTOPS:
719         if (!*wparam) return TRUE;
720         minsize = *wparam * sizeof(UINT);
721         break;
722     case CB_ADDSTRING:
723     case CB_INSERTSTRING:
724     case LB_ADDSTRING:
725     case LB_INSERTSTRING:
726         if (!*buffer) return TRUE;
727         if (!check_string( *buffer, size )) return FALSE;
728         break;
729     case CB_GETLBTEXT:
730     {
731         size = sizeof(ULONG_PTR);
732         if (combobox_has_strings( hwnd ))
733             size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
734         if (!get_buffer_space( buffer, size )) return FALSE;
735         break;
736     }
737     case LB_GETTEXT:
738     {
739         size = sizeof(ULONG_PTR);
740         if (listbox_has_strings( hwnd ))
741             size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
742         if (!get_buffer_space( buffer, size )) return FALSE;
743         break;
744     }
745     case LB_GETSELITEMS:
746         if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
747         break;
748     case WM_NEXTMENU:
749         minsize = sizeof(MDINEXTMENU);
750         if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
751         break;
752     case WM_SIZING:
753     case WM_MOVING:
754         minsize = sizeof(RECT);
755         if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
756         break;
757     case WM_MDICREATE:
758     {
759         MDICREATESTRUCTW *cs = *buffer;
760         WCHAR *str = (WCHAR *)(cs + 1);
761         if (size < sizeof(*cs)) return FALSE;
762         size -= sizeof(*cs);
763         if (HIWORD(cs->szTitle))
764         {
765             if (!check_string( str, size )) return FALSE;
766             cs->szTitle = str;
767             size -= (strlenW(str) + 1) * sizeof(WCHAR);
768             str += strlenW(str) + 1;
769         }
770         if (HIWORD(cs->szClass))
771         {
772             if (!check_string( str, size )) return FALSE;
773             cs->szClass = str;
774         }
775         break;
776     }
777     case WM_MDIGETACTIVE:
778         if (!*lparam) return TRUE;
779         if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
780         break;
781
782     /* these contain an HFONT */
783     case WM_SETFONT:
784     case WM_GETFONT:
785     /* these contain an HDC */
786     case WM_PAINT:
787     case WM_ERASEBKGND:
788     case WM_ICONERASEBKGND:
789     case WM_NCPAINT:
790     case WM_CTLCOLORMSGBOX:
791     case WM_CTLCOLOREDIT:
792     case WM_CTLCOLORLISTBOX:
793     case WM_CTLCOLORBTN:
794     case WM_CTLCOLORDLG:
795     case WM_CTLCOLORSCROLLBAR:
796     case WM_CTLCOLORSTATIC:
797     case WM_PRINT:
798     case WM_PRINTCLIENT:
799     /* these contain an HGLOBAL */
800     case WM_PAINTCLIPBOARD:
801     case WM_SIZECLIPBOARD:
802     case WM_DDE_INITIATE:
803     case WM_DDE_ADVISE:
804     case WM_DDE_UNADVISE:
805     case WM_DDE_DATA:
806     case WM_DDE_REQUEST:
807     case WM_DDE_POKE:
808     case WM_DDE_EXECUTE:
809     /* these contain pointers */
810     case WM_DROPOBJECT:
811     case WM_QUERYDROPOBJECT:
812     case WM_DRAGLOOP:
813     case WM_DRAGSELECT:
814     case WM_DRAGMOVE:
815     case WM_DEVICECHANGE:
816         FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message) );
817         return FALSE;
818
819     default:
820         return TRUE; /* message doesn't need any unpacking */
821     }
822
823     /* default exit for most messages: check minsize and store buffer in lparam */
824     if (size < minsize) return FALSE;
825     *lparam = (LPARAM)*buffer;
826     return TRUE;
827 }
828
829
830 /***********************************************************************
831  *              pack_reply
832  *
833  * Pack a reply to a message for sending to another process.
834  */
835 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
836                         LRESULT res, struct packed_message *data )
837 {
838     data->count = 0;
839     switch(message)
840     {
841     case WM_NCCREATE:
842     case WM_CREATE:
843         push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
844         break;
845     case WM_GETTEXT:
846     case CB_GETLBTEXT:
847     case LB_GETTEXT:
848         push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
849         break;
850     case WM_GETMINMAXINFO:
851         push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
852         break;
853     case WM_MEASUREITEM:
854         push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
855         break;
856     case WM_WINDOWPOSCHANGING:
857     case WM_WINDOWPOSCHANGED:
858         push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
859         break;
860     case WM_GETDLGCODE:
861         if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
862         break;
863     case SBM_GETSCROLLINFO:
864         push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
865         break;
866     case EM_GETRECT:
867     case LB_GETITEMRECT:
868     case CB_GETDROPPEDCONTROLRECT:
869     case WM_SIZING:
870     case WM_MOVING:
871         push_data( data, (RECT *)lparam, sizeof(RECT) );
872         break;
873     case EM_GETLINE:
874     {
875         WORD *ptr = (WORD *)lparam;
876         push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
877         break;
878     }
879     case LB_GETSELITEMS:
880         push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
881         break;
882     case WM_MDIGETACTIVE:
883         if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
884         break;
885     case WM_NCCALCSIZE:
886         if (!wparam)
887             push_data( data, (RECT *)lparam, sizeof(RECT) );
888         else
889         {
890             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
891             push_data( data, nc, sizeof(*nc) );
892             push_data( data, nc->lppos, sizeof(*nc->lppos) );
893         }
894         break;
895     case EM_GETSEL:
896     case SBM_GETRANGE:
897     case CB_GETEDITSEL:
898         if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
899         if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
900         break;
901     case WM_NEXTMENU:
902         push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
903         break;
904     case WM_MDICREATE:
905         push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
906         break;
907     case WM_ASKCBFORMATNAME:
908         push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
909         break;
910     }
911 }
912
913
914 /***********************************************************************
915  *              unpack_reply
916  *
917  * Unpack a message reply received from another process.
918  */
919 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
920                           void *buffer, size_t size )
921 {
922     switch(message)
923     {
924     case WM_NCCREATE:
925     case WM_CREATE:
926     {
927         CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
928         LPCWSTR name = cs->lpszName, class = cs->lpszClass;
929         memcpy( cs, buffer, min( sizeof(*cs), size ));
930         cs->lpszName = name;  /* restore the original pointers */
931         cs->lpszClass = class;
932         break;
933     }
934     case WM_GETTEXT:
935     case WM_ASKCBFORMATNAME:
936         memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
937         break;
938     case WM_GETMINMAXINFO:
939         memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
940         break;
941     case WM_MEASUREITEM:
942         memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
943         break;
944     case WM_WINDOWPOSCHANGING:
945     case WM_WINDOWPOSCHANGED:
946         memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
947         break;
948     case WM_GETDLGCODE:
949         if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
950         break;
951     case SBM_GETSCROLLINFO:
952         memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
953         break;
954     case EM_GETRECT:
955     case CB_GETDROPPEDCONTROLRECT:
956     case LB_GETITEMRECT:
957     case WM_SIZING:
958     case WM_MOVING:
959         memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
960         break;
961     case EM_GETLINE:
962         size = min( size, *(WORD *)lparam );
963         memcpy( (WCHAR *)lparam, buffer, size );
964         break;
965     case LB_GETSELITEMS:
966         memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
967         break;
968     case LB_GETTEXT:
969     case CB_GETLBTEXT:
970         memcpy( (WCHAR *)lparam, buffer, size );
971         break;
972     case WM_NEXTMENU:
973         memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
974         break;
975     case WM_MDIGETACTIVE:
976         if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
977         break;
978     case WM_NCCALCSIZE:
979         if (!wparam)
980             memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
981         else
982         {
983             NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
984             WINDOWPOS *wp = nc->lppos;
985             memcpy( nc, buffer, min( sizeof(*nc), size ));
986             if (size > sizeof(*nc))
987             {
988                 size -= sizeof(*nc);
989                 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
990             }
991             nc->lppos = wp;  /* restore the original pointer */
992         }
993         break;
994     case EM_GETSEL:
995     case SBM_GETRANGE:
996     case CB_GETEDITSEL:
997         if (wparam)
998         {
999             memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1000             if (size <= sizeof(DWORD)) break;
1001             size -= sizeof(DWORD);
1002             buffer = (DWORD *)buffer + 1;
1003         }
1004         if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1005         break;
1006     case WM_MDICREATE:
1007     {
1008         MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
1009         LPCWSTR title = cs->szTitle, class = cs->szClass;
1010         memcpy( cs, buffer, min( sizeof(*cs), size ));
1011         cs->szTitle = title;  /* restore the original pointers */
1012         cs->szClass = class;
1013         break;
1014     }
1015     default:
1016         ERR( "should not happen: unexpected message %x\n", message );
1017         break;
1018     }
1019 }
1020
1021
1022 /***********************************************************************
1023  *           reply_message
1024  *
1025  * Send a reply to a sent message.
1026  */
1027 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1028 {
1029     struct packed_message data;
1030     int replied = info->flags & ISMEX_REPLIED;
1031
1032     if (info->flags & ISMEX_NOTIFY) return;  /* notify messages don't get replies */
1033     if (!remove && replied) return;  /* replied already */
1034
1035     data.count = 0;
1036     info->flags |= ISMEX_REPLIED;
1037
1038     if (info->type == MSG_OTHER_PROCESS && !replied)
1039     {
1040         pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1041                     info->msg.lParam, result, &data );
1042         if (data.count)
1043         {
1044             size_t total = get_data_total_size( &data );
1045
1046             if (total > REQUEST_MAX_VAR_SIZE)
1047             {
1048                 FIXME( "inter-process msg data size %d not supported yet, expect trouble\n",
1049                        total );
1050                 total = REQUEST_MAX_VAR_SIZE;
1051             }
1052
1053             SERVER_START_VAR_REQ( reply_message, total )
1054             {
1055                 req->result = result;
1056                 req->remove = remove;
1057                 copy_all_data( server_data_ptr(req), &data );
1058                 SERVER_CALL();
1059             }
1060             SERVER_END_VAR_REQ;
1061             return;
1062         }
1063     }
1064
1065     SERVER_START_REQ( reply_message )
1066     {
1067         req->result = result;
1068         req->remove = remove;
1069         SERVER_CALL();
1070     }
1071     SERVER_END_REQ;
1072 }
1073
1074
1075 /***********************************************************************
1076  *           call_window_proc
1077  *
1078  * Call a window procedure and the corresponding hooks.
1079  */
1080 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam, BOOL unicode )
1081 {
1082     LRESULT result;
1083     WNDPROC winproc;
1084
1085     /* FIXME: should check for exiting queue */
1086
1087     /* first the WH_CALLWNDPROC hook */
1088     if (HOOK_IsHooked( WH_CALLWNDPROC ))
1089     {
1090         CWPSTRUCT cwp;
1091         cwp.lParam  = lparam;
1092         cwp.wParam  = wparam;
1093         cwp.message = msg;
1094         cwp.hwnd    = hwnd;
1095         if (unicode) HOOK_CallHooksW( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1096         else HOOK_CallHooksA( WH_CALLWNDPROC, HC_ACTION, 1, (LPARAM)&cwp );
1097         lparam = cwp.lParam;
1098         wparam = cwp.wParam;
1099         msg    = cwp.message;
1100         hwnd   = cwp.hwnd;
1101     }
1102
1103     /* now call the window procedure */
1104     if (unicode)
1105     {
1106         if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) return 0;
1107         result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1108     }
1109     else
1110     {
1111         if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) return 0;
1112         result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1113     }
1114
1115     /* and finally the WH_CALLWNDPROCRET hook */
1116     if (HOOK_IsHooked( WH_CALLWNDPROCRET ))
1117     {
1118         CWPRETSTRUCT cwp;
1119         cwp.lResult = result;
1120         cwp.lParam  = lparam;
1121         cwp.wParam  = wparam;
1122         cwp.message = msg;
1123         cwp.hwnd    = hwnd;
1124         if (unicode) HOOK_CallHooksW( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
1125         else HOOK_CallHooksA( WH_CALLWNDPROCRET, HC_ACTION, 1, (LPARAM)&cwp );
1126     }
1127     return result;
1128 }
1129
1130
1131 /***********************************************************************
1132  *           MSG_peek_message
1133  *
1134  * Peek for a message matching the given parameters. Return FALSE if none available.
1135  * All pending sent messages are processed before returning.
1136  */
1137 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1138 {
1139     BOOL ret;
1140     LRESULT result;
1141     ULONG_PTR extra_info = 0;
1142     MESSAGEQUEUE *queue = QUEUE_Current();
1143     struct received_message_info info, *old_info;
1144
1145     if (!first && !last) last = ~0;
1146
1147     for (;;)
1148     {
1149         void *buffer = NULL;
1150         size_t size = 0;
1151
1152         SERVER_START_VAR_REQ( get_message, REQUEST_MAX_VAR_SIZE )
1153         {
1154             req->flags     = flags;
1155             req->get_win   = hwnd;
1156             req->get_first = first;
1157             req->get_last  = last;
1158             if ((ret = !SERVER_CALL()))
1159             {
1160                 info.type        = req->type;
1161                 info.msg.hwnd    = req->win;
1162                 info.msg.message = req->msg;
1163                 info.msg.wParam  = req->wparam;
1164                 info.msg.lParam  = req->lparam;
1165                 info.msg.time    = req->time;
1166                 info.msg.pt.x    = req->x;
1167                 info.msg.pt.y    = req->y;
1168                 extra_info       = req->info;
1169
1170                 if ((size = server_data_size(req)))
1171                 {
1172                     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size )))
1173                     {
1174                         ERR("out of memory for message data\n");
1175                         ret = FALSE;
1176                     }
1177                     else memcpy( buffer, server_data_ptr(req), size );
1178                 }
1179             }
1180         }
1181         SERVER_END_VAR_REQ;
1182
1183         if (!ret) return FALSE;  /* no message available */
1184
1185         TRACE( "got type %d msg %x hwnd %x wp %x lp %lx\n",
1186                info.type, info.msg.message, info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1187
1188         switch(info.type)
1189         {
1190         case MSG_ASCII:
1191         case MSG_UNICODE:
1192             info.flags = ISMEX_SEND;
1193             break;
1194         case MSG_NOTIFY:
1195             info.flags = ISMEX_NOTIFY;
1196             break;
1197         case MSG_CALLBACK:
1198             info.flags = ISMEX_CALLBACK;
1199             break;
1200         case MSG_OTHER_PROCESS:
1201             info.flags = ISMEX_SEND;
1202             if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1203                                  &info.msg.lParam, &buffer, size ))
1204             {
1205                 ERR( "invalid packed message %x (%s) hwnd %x wp %x lp %lx size %d\n",
1206                      info.msg.message, SPY_GetMsgName(info.msg.message), info.msg.hwnd,
1207                      info.msg.wParam, info.msg.lParam, size );
1208                 /* ignore it */
1209                 reply_message( &info, 0, TRUE );
1210                 continue;
1211             }
1212             break;
1213         case MSG_POSTED:
1214             goto got_one;
1215         case MSG_HARDWARE_RAW:
1216             if (!MSG_process_raw_hardware_message( &info.msg, extra_info,
1217                                                    hwnd, first, last, flags & GET_MSG_REMOVE ))
1218                 continue;
1219             /* fall through */
1220         case MSG_HARDWARE_COOKED:
1221             if (!MSG_process_cooked_hardware_message( &info.msg, flags & GET_MSG_REMOVE ))
1222             {
1223                 flags |= GET_MSG_REMOVE_LAST;
1224                 continue;
1225             }
1226             queue->GetMessageExtraInfoVal = extra_info;
1227             goto got_one;
1228         }
1229
1230         /* if we get here, we have a sent message; call the window procedure */
1231         old_info = queue->receive_info;
1232         queue->receive_info = &info;
1233         result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1234                                    info.msg.lParam, (info.type != MSG_ASCII) );
1235         reply_message( &info, result, TRUE );
1236         queue->receive_info = old_info;
1237
1238         if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1239     }
1240
1241  got_one:
1242     *msg = info.msg;
1243     return TRUE;
1244 }
1245
1246
1247 /***********************************************************************
1248  *           wait_message_reply
1249  *
1250  * Wait until a sent message gets replied to.
1251  */
1252 static void wait_message_reply( UINT flags )
1253 {
1254     MESSAGEQUEUE *queue;
1255
1256     if (!(queue = QUEUE_Current())) return;
1257
1258     for (;;)
1259     {
1260         unsigned int wake_bits = 0, changed_bits = 0;
1261         DWORD dwlc, res;
1262
1263         SERVER_START_REQ( set_queue_mask )
1264         {
1265             req->wake_mask    = (flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE;
1266             req->changed_mask = QS_SMRESULT | req->wake_mask;
1267             req->skip_wait    = 1;
1268             if (!SERVER_CALL())
1269             {
1270                 wake_bits    = req->wake_bits;
1271                 changed_bits = req->changed_bits;
1272             }
1273         }
1274         SERVER_END_REQ;
1275
1276         if (changed_bits & QS_SMRESULT) return;  /* got a result */
1277         if (wake_bits & QS_SENDMESSAGE)
1278         {
1279             /* Process the sent message immediately */
1280             MSG msg;
1281             MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1282             continue;
1283         }
1284
1285         /* now wait for it */
1286
1287         ReleaseThunkLock( &dwlc );
1288
1289         if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1290             res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1291                                                             INFINITE, 0, 0 );
1292         else
1293             res = WaitForSingleObject( queue->server_queue, INFINITE );
1294
1295         if (dwlc) RestoreThunkLock( dwlc );
1296     }
1297 }
1298
1299 /***********************************************************************
1300  *              put_message_in_queue
1301  *
1302  * Put a sent message into the destination queue.
1303  * For inter-process message, reply_size is set to expected size of reply data.
1304  */
1305 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1306                                   size_t *reply_size )
1307 {
1308     unsigned int res;
1309     int timeout = -1;
1310
1311     /* FIXME: should check for exiting queue */
1312
1313     if (info->type != MSG_NOTIFY &&
1314         info->type != MSG_CALLBACK &&
1315         info->type != MSG_POSTED &&
1316         info->timeout != INFINITE)
1317         timeout = info->timeout;
1318
1319     if (info->type == MSG_OTHER_PROCESS)
1320     {
1321         struct packed_message data;
1322         *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1323
1324         if (data.count == -1)
1325         {
1326             WARN( "cannot pack message %x\n", info->msg );
1327             return FALSE;
1328         }
1329
1330         if (data.size[0]) /* need to send extra data along with the message */
1331         {
1332             size_t total = get_data_total_size( &data );
1333
1334             if (total > REQUEST_MAX_VAR_SIZE)
1335             {
1336                 FIXME( "inter-process msg data size %d not supported yet, expect trouble\n",
1337                        total );
1338                 total = REQUEST_MAX_VAR_SIZE;
1339             }
1340
1341             SERVER_START_VAR_REQ( send_message, total )
1342             {
1343                 req->id      = (void *)dest_tid;
1344                 req->type    = MSG_OTHER_PROCESS;
1345                 req->win     = info->hwnd;
1346                 req->msg     = info->msg;
1347                 req->wparam  = info->wparam;
1348                 req->lparam  = info->lparam;
1349                 req->time    = GetCurrentTime();
1350                 req->timeout = timeout;
1351                 copy_all_data( server_data_ptr(req), &data );
1352                 res = SERVER_CALL();
1353             }
1354             SERVER_END_VAR_REQ;
1355             goto done;
1356         }
1357     }
1358
1359     /* no extra data, or not inter-process message */
1360     SERVER_START_REQ( send_message )
1361     {
1362         req->id      = (void *)dest_tid;
1363         req->type    = info->type;
1364         req->win     = info->hwnd;
1365         req->msg     = info->msg;
1366         req->wparam  = info->wparam;
1367         req->lparam  = info->lparam;
1368         req->time    = GetCurrentTime();
1369         req->timeout = timeout;
1370         res = SERVER_CALL();
1371     }
1372     SERVER_END_REQ;
1373
1374  done:
1375     if (res)
1376     {
1377         if (res == STATUS_INVALID_PARAMETER)
1378             /* FIXME: find a STATUS_ value for this one */
1379             SetLastError( ERROR_INVALID_THREAD_ID );
1380         else
1381             SetLastError( RtlNtStatusToDosError(res) );
1382     }
1383     return !res;
1384 }
1385
1386
1387 /***********************************************************************
1388  *              retrieve_reply
1389  *
1390  * Retrieve a message reply from the server.
1391  */
1392 static LRESULT retrieve_reply( const struct send_message_info *info,
1393                                size_t reply_size, LRESULT *result )
1394 {
1395     NTSTATUS status;
1396
1397     if (reply_size)
1398     {
1399         if (reply_size > REQUEST_MAX_VAR_SIZE)
1400         {
1401             WARN( "reply_size %d too large, reply may be truncated\n", reply_size );
1402             reply_size = REQUEST_MAX_VAR_SIZE;
1403         }
1404         SERVER_START_VAR_REQ( get_message_reply, reply_size )
1405         {
1406             req->cancel = 1;
1407             if (!(status = SERVER_CALL()))
1408             {
1409                 *result = req->result;
1410                 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam,
1411                               server_data_ptr(req), server_data_size(req) );
1412             }
1413         }
1414         SERVER_END_VAR_REQ;
1415     }
1416     else
1417     {
1418         SERVER_START_REQ( get_message_reply )
1419         {
1420             req->cancel = 1;
1421             if (!(status = SERVER_CALL())) *result = req->result;
1422         }
1423         SERVER_END_REQ;
1424     }
1425
1426     TRACE( "hwnd %x msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1427            info->hwnd, info->msg, SPY_GetMsgName(info->msg), info->wparam,
1428            info->lparam, *result, status );
1429
1430     if (!status) return 1;
1431     if (status == STATUS_TIMEOUT) SetLastError(0);  /* timeout */
1432     else SetLastError( RtlNtStatusToDosError(status) );
1433     return 0;
1434 }
1435
1436
1437 /***********************************************************************
1438  *              send_inter_thread_message
1439  */
1440 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1441                                           LRESULT *res_ptr )
1442 {
1443     LRESULT ret;
1444     int locks;
1445     size_t reply_size = 0;
1446
1447     TRACE( "hwnd %x msg %x (%s) wp %x lp %lx\n",
1448            info->hwnd, info->msg, SPY_GetMsgName(info->msg), info->wparam, info->lparam );
1449
1450     if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1451
1452     /* there's no reply to wait for on notify/callback messages */
1453     if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1454
1455     locks = WIN_SuspendWndsLock();
1456
1457     wait_message_reply( info->flags );
1458     ret = retrieve_reply( info, reply_size, res_ptr );
1459
1460     WIN_RestoreWndsLock( locks );
1461     return ret;
1462 }
1463
1464
1465 /***********************************************************************
1466  *              SendMessageTimeoutW  (USER32.@)
1467  */
1468 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1469                                     UINT flags, UINT timeout, LPDWORD res_ptr )
1470 {
1471     struct send_message_info info;
1472     DWORD dest_tid, dest_pid;
1473     LRESULT ret, result;
1474
1475     info.type    = MSG_UNICODE;
1476     info.hwnd    = hwnd;
1477     info.msg     = msg;
1478     info.wparam  = wparam;
1479     info.lparam  = lparam;
1480     info.flags   = flags;
1481     info.timeout = timeout;
1482
1483     if (is_broadcast(hwnd))
1484     {
1485         EnumWindows( broadcast_message_callback, (LPARAM)&info );
1486         if (res_ptr) *res_ptr = 1;
1487         return 1;
1488     }
1489
1490     SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1491
1492     dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid );
1493
1494     if (dest_tid == GetCurrentThreadId())
1495     {
1496         result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1497         ret = 1;
1498     }
1499     else
1500     {
1501         if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1502         ret = send_inter_thread_message( dest_tid, &info, &result );
1503     }
1504
1505     SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1506     if (ret && res_ptr) *res_ptr = result;
1507     return ret;
1508 }
1509
1510
1511 /***********************************************************************
1512  *              SendMessageTimeoutA  (USER32.@)
1513  */
1514 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1515                                     UINT flags, UINT timeout, LPDWORD res_ptr )
1516 {
1517     struct send_message_info info;
1518     DWORD dest_tid, dest_pid;
1519     LRESULT ret, result;
1520
1521     info.type    = MSG_ASCII;
1522     info.hwnd    = hwnd;
1523     info.msg     = msg;
1524     info.wparam  = wparam;
1525     info.lparam  = lparam;
1526     info.flags   = flags;
1527     info.timeout = timeout;
1528
1529     if (is_broadcast(hwnd))
1530     {
1531         EnumWindows( broadcast_message_callback, (LPARAM)&info );
1532         if (res_ptr) *res_ptr = 1;
1533         return 1;
1534     }
1535
1536     SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1537
1538     dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid );
1539
1540     if (dest_tid == GetCurrentThreadId())
1541     {
1542         result = call_window_proc( hwnd, msg, wparam, lparam, FALSE );
1543         ret = 1;
1544     }
1545     else if (dest_pid == GetCurrentProcessId())
1546     {
1547         ret = send_inter_thread_message( dest_tid, &info, &result );
1548     }
1549     else
1550     {
1551         /* inter-process message: need to map to Unicode */
1552         info.type = MSG_OTHER_PROCESS;
1553         if (is_unicode_message( info.msg ))
1554         {
1555             if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1556                 return 0;
1557             ret = send_inter_thread_message( dest_tid, &info, &result );
1558             result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1559                                                info.lparam, result );
1560         }
1561         else ret = send_inter_thread_message( dest_tid, &info, &result );
1562     }
1563     SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1564     if (ret && res_ptr) *res_ptr = result;
1565     return ret;
1566 }
1567
1568
1569 /***********************************************************************
1570  *              SendMessageW  (USER32.@)
1571  */
1572 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1573 {
1574     LRESULT res = 0;
1575     SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1576     return res;
1577 }
1578
1579
1580 /***********************************************************************
1581  *              SendMessageA  (USER32.@)
1582  */
1583 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1584 {
1585     LRESULT res = 0;
1586     SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1587     return res;
1588 }
1589
1590
1591 /***********************************************************************
1592  *              SendNotifyMessageA  (USER32.@)
1593  */
1594 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1595 {
1596     return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1597 }
1598
1599
1600 /***********************************************************************
1601  *              SendNotifyMessageW  (USER32.@)
1602  */
1603 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1604 {
1605     struct send_message_info info;
1606     DWORD dest_tid;
1607     LRESULT result;
1608
1609     if (is_pointer_message(msg))
1610     {
1611         SetLastError(ERROR_INVALID_PARAMETER);
1612         return FALSE;
1613     }
1614
1615     info.type    = MSG_NOTIFY;
1616     info.hwnd    = hwnd;
1617     info.msg     = msg;
1618     info.wparam  = wparam;
1619     info.lparam  = lparam;
1620
1621     if (is_broadcast(hwnd))
1622     {
1623         EnumWindows( broadcast_message_callback, (LPARAM)&info );
1624         return TRUE;
1625     }
1626
1627     dest_tid = GetWindowThreadProcessId( hwnd, NULL );
1628
1629     if (dest_tid == GetCurrentThreadId())
1630     {
1631         call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1632         return TRUE;
1633     }
1634     return send_inter_thread_message( dest_tid, &info, &result );
1635 }
1636
1637
1638 /***********************************************************************
1639  *              SendMessageCallbackA  (USER32.@)
1640  */
1641 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1642                                   SENDASYNCPROC callback, ULONG_PTR data )
1643 {
1644     return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
1645                                  lparam, callback, data );
1646 }
1647
1648
1649 /***********************************************************************
1650  *              SendMessageCallbackW  (USER32.@)
1651  */
1652 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1653                                   SENDASYNCPROC callback, ULONG_PTR data )
1654 {
1655     struct send_message_info info;
1656     LRESULT result;
1657     DWORD dest_tid;
1658
1659     if (is_pointer_message(msg))
1660     {
1661         SetLastError(ERROR_INVALID_PARAMETER);
1662         return FALSE;
1663     }
1664
1665     info.type     = MSG_CALLBACK;
1666     info.hwnd     = hwnd;
1667     info.msg      = msg;
1668     info.wparam   = wparam;
1669     info.lparam   = lparam;
1670     info.callback = callback;
1671     info.data     = data;
1672
1673     if (is_broadcast(hwnd))
1674     {
1675         EnumWindows( broadcast_message_callback, (LPARAM)&info );
1676         return TRUE;
1677     }
1678
1679     dest_tid = GetWindowThreadProcessId( hwnd, NULL );
1680
1681     if (dest_tid == GetCurrentThreadId())
1682     {
1683         result = call_window_proc( hwnd, msg, wparam, lparam, TRUE );
1684         callback( hwnd, msg, data, result );
1685         return TRUE;
1686     }
1687     FIXME( "callback will not be called\n" );
1688     return send_inter_thread_message( dest_tid, &info, &result );
1689 }
1690
1691
1692 /***********************************************************************
1693  *              ReplyMessage  (USER32.@)
1694  */
1695 BOOL WINAPI ReplyMessage( LRESULT result )
1696 {
1697     MESSAGEQUEUE *queue = QUEUE_Current();
1698     struct received_message_info *info = queue->receive_info;
1699
1700     if (!info) return FALSE;
1701     reply_message( info, result, FALSE );
1702     return TRUE;
1703 }
1704
1705
1706 /***********************************************************************
1707  *              InSendMessage  (USER32.@)
1708  */
1709 BOOL WINAPI InSendMessage(void)
1710 {
1711     return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
1712 }
1713
1714
1715 /***********************************************************************
1716  *              InSendMessageEx  (USER32.@)
1717  */
1718 DWORD WINAPI InSendMessageEx( LPVOID reserved )
1719 {
1720     MESSAGEQUEUE *queue = QUEUE_Current();
1721     struct received_message_info *info = queue->receive_info;
1722
1723     if (info) return info->flags;
1724     return ISMEX_NOSEND;
1725 }
1726
1727
1728 /***********************************************************************
1729  *              PostMessageA  (USER32.@)
1730  */
1731 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1732 {
1733     return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1734 }
1735
1736
1737 /***********************************************************************
1738  *              PostMessageW  (USER32.@)
1739  */
1740 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1741 {
1742     struct send_message_info info;
1743
1744     if (is_pointer_message( msg ))
1745     {
1746         SetLastError( ERROR_INVALID_PARAMETER );
1747         return FALSE;
1748     }
1749
1750     info.type   = MSG_POSTED;
1751     info.hwnd   = hwnd;
1752     info.msg    = msg;
1753     info.wparam = wparam;
1754     info.lparam = lparam;
1755
1756     if (is_broadcast(hwnd))
1757     {
1758         EnumWindows( broadcast_message_callback, (LPARAM)&info );
1759         return TRUE;
1760     }
1761     return put_message_in_queue( GetWindowThreadProcessId( hwnd, NULL ), &info, NULL );
1762 }
1763
1764
1765 /**********************************************************************
1766  *              PostThreadMessageA  (USER32.@)
1767  */
1768 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
1769 {
1770     return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
1771 }
1772
1773
1774 /**********************************************************************
1775  *              PostThreadMessageW  (USER32.@)
1776  */
1777 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
1778 {
1779     struct send_message_info info;
1780
1781     if (is_pointer_message( msg ))
1782     {
1783         SetLastError( ERROR_INVALID_PARAMETER );
1784         return FALSE;
1785     }
1786
1787     info.type   = MSG_POSTED;
1788     info.hwnd   = 0;
1789     info.msg    = msg;
1790     info.wparam = wparam;
1791     info.lparam = lparam;
1792     return put_message_in_queue( thread, &info, NULL );
1793 }
1794
1795
1796 /***********************************************************************
1797  *              PostQuitMessage  (USER32.@)
1798  */
1799 void WINAPI PostQuitMessage( INT exitCode )
1800 {
1801     PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
1802 }
1803
1804
1805 /***********************************************************************
1806  *              PeekMessageW  (USER32.@)
1807  */
1808 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
1809 {
1810     MESSAGEQUEUE *queue;
1811     MSG msg;
1812     int locks;
1813
1814     /* check for graphics events */
1815     if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1816         USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
1817
1818     locks = WIN_SuspendWndsLock();
1819
1820     if (!MSG_peek_message( &msg, hwnd, first, last,
1821                            (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
1822     {
1823         /* FIXME: should be done before checking for hw events */
1824         MSG_JournalPlayBackMsg();
1825
1826         if (!(flags & PM_NOYIELD))
1827         {
1828             DWORD count;
1829             ReleaseThunkLock(&count);
1830             if (count) RestoreThunkLock(count);
1831         }
1832         WIN_RestoreWndsLock( locks );
1833         return FALSE;
1834     }
1835
1836     WIN_RestoreWndsLock( locks );
1837
1838     /* need to fill the window handle for WM_PAINT message */
1839     if (msg.message == WM_PAINT)
1840     {
1841         if (!(msg.hwnd = WIN_FindWinToRepaint( hwnd ))) return FALSE;
1842
1843         if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
1844         {
1845             msg.message = WM_PAINTICON;
1846             msg.wParam = 1;
1847         }
1848
1849         /* check hwnd filter */
1850         if (hwnd && msg.hwnd != hwnd && !IsChild( hwnd, msg.hwnd )) return FALSE;
1851
1852         /* clear internal paint flag */
1853         RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
1854     }
1855
1856     if ((queue = QUEUE_Current()))
1857     {
1858         queue->GetMessageTimeVal = msg.time;
1859         queue->GetMessagePosVal  = MAKELONG( msg.pt.x, msg.pt.y );
1860     }
1861
1862       /* We got a message */
1863     if (flags & PM_REMOVE)
1864     {
1865         if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1866         {
1867             BYTE *p = &QueueKeyStateTable[msg.wParam & 0xff];
1868
1869             if (!(*p & 0x80)) *p ^= 0x01;
1870             *p |= 0x80;
1871         }
1872         else if (msg.message == WM_KEYUP || msg.message == WM_SYSKEYUP)
1873             QueueKeyStateTable[msg.wParam & 0xff] &= ~0x80;
1874     }
1875
1876     HOOK_CallHooksW( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg );
1877
1878     /* copy back our internal safe copy of message data to msg_out.
1879      * msg_out is a variable from the *program*, so it can't be used
1880      * internally as it can get "corrupted" by our use of SendMessage()
1881      * (back to the program) inside the message handling itself. */
1882     *msg_out = msg;
1883     return TRUE;
1884 }
1885
1886
1887 /***********************************************************************
1888  *              PeekMessageA  (USER32.@)
1889  */
1890 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
1891 {
1892     BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
1893     if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
1894     return ret;
1895 }
1896
1897
1898 /***********************************************************************
1899  *              GetMessageW  (USER32.@)
1900  */
1901 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
1902 {
1903     MESSAGEQUEUE *queue = QUEUE_Current();
1904     int mask, locks;
1905
1906     mask = QS_POSTMESSAGE | QS_SENDMESSAGE;  /* Always selected */
1907     if (first || last)
1908     {
1909         if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
1910         if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
1911              ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
1912         if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
1913         if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
1914         if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
1915     }
1916     else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
1917
1918     locks = WIN_SuspendWndsLock();
1919
1920     while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
1921     {
1922         /* wait until one of the bits is set */
1923         unsigned int wake_bits = 0, changed_bits = 0;
1924         DWORD dwlc;
1925
1926         SERVER_START_REQ( set_queue_mask )
1927         {
1928             req->wake_mask    = QS_SENDMESSAGE;
1929             req->changed_mask = mask;
1930             req->skip_wait    = 1;
1931             if (!SERVER_CALL())
1932             {
1933                 wake_bits    = req->wake_bits;
1934                 changed_bits = req->changed_bits;
1935             }
1936         }
1937         SERVER_END_REQ;
1938
1939         if (changed_bits & mask) continue;
1940         if (wake_bits & QS_SENDMESSAGE) continue;
1941
1942         TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
1943                queue->self, mask, wake_bits, changed_bits );
1944
1945         ReleaseThunkLock( &dwlc );
1946         if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1947             USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
1948         else
1949             WaitForSingleObject( queue->server_queue, INFINITE );
1950         if (dwlc) RestoreThunkLock( dwlc );
1951     }
1952
1953     WIN_RestoreWndsLock( locks );
1954
1955     return (msg->message != WM_QUIT);
1956 }
1957
1958
1959 /***********************************************************************
1960  *              GetMessageA  (USER32.@)
1961  */
1962 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
1963 {
1964     GetMessageW( msg, hwnd, first, last );
1965     msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
1966     return (msg->message != WM_QUIT);
1967 }
1968
1969
1970 /***********************************************************************
1971  *              SetMessageQueue (USER32.@)
1972  */
1973 BOOL WINAPI SetMessageQueue( INT size )
1974 {
1975     /* now obsolete the message queue will be expanded dynamically as necessary */
1976     return TRUE;
1977 }