Run WH_KEYBOARD_LL and WH_MOUSE_LL hooks in the context of the thread
[wine/dcerpc.git] / dlls / user / message.c
bloba7ca4832d7abcf7fec769a3b375085ebb3c9ead9
1 /*
2 * Window messaging support
4 * Copyright 2001 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "winerror.h"
30 #include "winnls.h"
31 #include "dde.h"
32 #include "wine/unicode.h"
33 #include "wine/server.h"
34 #include "message.h"
35 #include "user.h"
36 #include "win.h"
37 #include "winproc.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msg);
42 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
43 #define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK
45 #define MAX_PACK_COUNT 4
47 /* description of the data fields that need to be packed along with a sent message */
48 struct packed_message
50 int count;
51 const void *data[MAX_PACK_COUNT];
52 size_t size[MAX_PACK_COUNT];
55 /* info about the message currently being received by the current thread */
56 struct received_message_info
58 enum message_type type;
59 MSG msg;
60 UINT flags; /* InSendMessageEx return flags */
63 /* structure to group all parameters for sent messages of the various kinds */
64 struct send_message_info
66 enum message_type type;
67 HWND hwnd;
68 UINT msg;
69 WPARAM wparam;
70 LPARAM lparam;
71 UINT flags; /* flags for SendMessageTimeout */
72 UINT timeout; /* timeout for SendMessageTimeout */
73 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
74 ULONG_PTR data; /* callback data */
78 /* flag for messages that contain pointers */
79 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
81 #define SET(msg) (1 << ((msg) & 31))
83 static const unsigned int message_pointer_flags[] =
85 /* 0x00 - 0x1f */
86 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
87 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
88 /* 0x20 - 0x3f */
89 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
90 SET(WM_COMPAREITEM),
91 /* 0x40 - 0x5f */
92 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) |
93 SET(WM_NOTIFY) | SET(WM_HELP),
94 /* 0x60 - 0x7f */
95 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
96 /* 0x80 - 0x9f */
97 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
98 /* 0xa0 - 0xbf */
99 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
100 /* 0xc0 - 0xdf */
101 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
102 /* 0xe0 - 0xff */
103 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO),
104 /* 0x100 - 0x11f */
106 /* 0x120 - 0x13f */
108 /* 0x140 - 0x15f */
109 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
110 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
111 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
112 /* 0x160 - 0x17f */
114 /* 0x180 - 0x19f */
115 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
116 SET(LB_DIR) | SET(LB_FINDSTRING) |
117 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
118 /* 0x1a0 - 0x1bf */
119 SET(LB_FINDSTRINGEXACT),
120 /* 0x1c0 - 0x1df */
122 /* 0x1e0 - 0x1ff */
124 /* 0x200 - 0x21f */
125 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
126 /* 0x220 - 0x23f */
127 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
128 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
129 /* 0x240 - 0x25f */
131 /* 0x260 - 0x27f */
133 /* 0x280 - 0x29f */
135 /* 0x2a0 - 0x2bf */
137 /* 0x2c0 - 0x2df */
139 /* 0x2e0 - 0x2ff */
141 /* 0x300 - 0x31f */
142 SET(WM_ASKCBFORMATNAME)
145 /* flags for messages that contain Unicode strings */
146 static const unsigned int message_unicode_flags[] =
148 /* 0x00 - 0x1f */
149 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) | SET(WM_GETTEXTLENGTH) |
150 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
151 /* 0x20 - 0x3f */
152 SET(WM_CHARTOITEM),
153 /* 0x40 - 0x5f */
155 /* 0x60 - 0x7f */
157 /* 0x80 - 0x9f */
158 SET(WM_NCCREATE),
159 /* 0xa0 - 0xbf */
161 /* 0xc0 - 0xdf */
162 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETPASSWORDCHAR),
163 /* 0xe0 - 0xff */
165 /* 0x100 - 0x11f */
166 SET(WM_CHAR) | SET(WM_DEADCHAR) | SET(WM_SYSCHAR) | SET(WM_SYSDEADCHAR),
167 /* 0x120 - 0x13f */
168 SET(WM_MENUCHAR),
169 /* 0x140 - 0x15f */
170 SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) | SET(CB_GETLBTEXTLEN) |
171 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) | SET(CB_FINDSTRINGEXACT),
172 /* 0x160 - 0x17f */
174 /* 0x180 - 0x19f */
175 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_GETTEXTLEN) |
176 SET(LB_SELECTSTRING) | SET(LB_DIR) | SET(LB_FINDSTRING) | SET(LB_ADDFILE),
177 /* 0x1a0 - 0x1bf */
178 SET(LB_FINDSTRINGEXACT),
179 /* 0x1c0 - 0x1df */
181 /* 0x1e0 - 0x1ff */
183 /* 0x200 - 0x21f */
185 /* 0x220 - 0x23f */
186 SET(WM_MDICREATE),
187 /* 0x240 - 0x25f */
189 /* 0x260 - 0x27f */
191 /* 0x280 - 0x29f */
192 SET(WM_IME_CHAR),
193 /* 0x2a0 - 0x2bf */
195 /* 0x2c0 - 0x2df */
197 /* 0x2e0 - 0x2ff */
199 /* 0x300 - 0x31f */
200 SET(WM_PAINTCLIPBOARD) | SET(WM_SIZECLIPBOARD) | SET(WM_ASKCBFORMATNAME)
203 /* check whether a given message type includes pointers */
204 inline static int is_pointer_message( UINT message )
206 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
207 return (message_pointer_flags[message / 32] & SET(message)) != 0;
210 /* check whether a given message type contains Unicode (or ASCII) chars */
211 inline static int is_unicode_message( UINT message )
213 if (message >= 8*sizeof(message_unicode_flags)) return FALSE;
214 return (message_unicode_flags[message / 32] & SET(message)) != 0;
217 #undef SET
219 /* add a data field to a packed message */
220 inline static void push_data( struct packed_message *data, const void *ptr, size_t size )
222 data->data[data->count] = ptr;
223 data->size[data->count] = size;
224 data->count++;
227 /* add a string to a packed message */
228 inline static void push_string( struct packed_message *data, LPCWSTR str )
230 push_data( data, str, (strlenW(str) + 1) * sizeof(WCHAR) );
233 /* retrieve a pointer to data from a packed message and increment the buffer pointer */
234 inline static void *get_data( void **buffer, size_t size )
236 void *ret = *buffer;
237 *buffer = (char *)*buffer + size;
238 return ret;
241 /* make sure that the buffer contains a valid null-terminated Unicode string */
242 inline static BOOL check_string( LPCWSTR str, size_t size )
244 for (size /= sizeof(WCHAR); size; size--, str++)
245 if (!*str) return TRUE;
246 return FALSE;
249 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
250 inline static void *get_buffer_space( void **buffer, size_t size )
252 void *ret;
253 if (!(ret = HeapReAlloc( GetProcessHeap(), 0, *buffer, size )))
254 HeapFree( GetProcessHeap(), 0, *buffer );
255 *buffer = ret;
256 return ret;
259 /* retrieve a string pointer from packed data */
260 inline static LPWSTR get_string( void **buffer )
262 return get_data( buffer, (strlenW( (LPWSTR)*buffer ) + 1) * sizeof(WCHAR) );
265 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
266 inline static BOOL combobox_has_strings( HWND hwnd )
268 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
269 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
272 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
273 inline static BOOL listbox_has_strings( HWND hwnd )
275 DWORD style = GetWindowLongA( hwnd, GWL_STYLE );
276 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
280 /***********************************************************************
281 * broadcast_message_callback
283 * Helper callback for broadcasting messages.
285 static BOOL CALLBACK broadcast_message_callback( HWND hwnd, LPARAM lparam )
287 struct send_message_info *info = (struct send_message_info *)lparam;
288 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & (WS_POPUP|WS_CAPTION))) return TRUE;
289 switch(info->type)
291 case MSG_UNICODE:
292 SendMessageTimeoutW( hwnd, info->msg, info->wparam, info->lparam,
293 info->flags, info->timeout, NULL );
294 break;
295 case MSG_ASCII:
296 SendMessageTimeoutA( hwnd, info->msg, info->wparam, info->lparam,
297 info->flags, info->timeout, NULL );
298 break;
299 case MSG_NOTIFY:
300 SendNotifyMessageW( hwnd, info->msg, info->wparam, info->lparam );
301 break;
302 case MSG_CALLBACK:
303 SendMessageCallbackW( hwnd, info->msg, info->wparam, info->lparam,
304 info->callback, info->data );
305 break;
306 case MSG_POSTED:
307 PostMessageW( hwnd, info->msg, info->wparam, info->lparam );
308 break;
309 default:
310 ERR( "bad type %d\n", info->type );
311 break;
313 return TRUE;
317 /***********************************************************************
318 * map_wparam_AtoW
320 * Convert the wparam of an ASCII message to Unicode.
322 static WPARAM map_wparam_AtoW( UINT message, WPARAM wparam )
324 switch(message)
326 case WM_CHARTOITEM:
327 case EM_SETPASSWORDCHAR:
328 case WM_CHAR:
329 case WM_DEADCHAR:
330 case WM_SYSCHAR:
331 case WM_SYSDEADCHAR:
332 case WM_MENUCHAR:
334 char ch = LOWORD(wparam);
335 WCHAR wch;
336 MultiByteToWideChar(CP_ACP, 0, &ch, 1, &wch, 1);
337 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
339 break;
340 case WM_IME_CHAR:
342 char ch[2];
343 WCHAR wch;
344 ch[0] = (wparam >> 8);
345 ch[1] = (wparam & 0xff);
346 MultiByteToWideChar(CP_ACP, 0, ch, 2, &wch, 1);
347 wparam = MAKEWPARAM( wch, HIWORD(wparam) );
349 break;
351 return wparam;
355 /***********************************************************************
356 * map_wparam_WtoA
358 * Convert the wparam of a Unicode message to ASCII.
360 static WPARAM map_wparam_WtoA( UINT message, WPARAM wparam )
362 switch(message)
364 case WM_CHARTOITEM:
365 case EM_SETPASSWORDCHAR:
366 case WM_CHAR:
367 case WM_DEADCHAR:
368 case WM_SYSCHAR:
369 case WM_SYSDEADCHAR:
370 case WM_MENUCHAR:
372 WCHAR wch = LOWORD(wparam);
373 BYTE ch;
374 WideCharToMultiByte( CP_ACP, 0, &wch, 1, &ch, 1, NULL, NULL );
375 wparam = MAKEWPARAM( ch, HIWORD(wparam) );
377 break;
378 case WM_IME_CHAR:
380 WCHAR wch = LOWORD(wparam);
381 BYTE ch[2];
383 ch[1] = 0;
384 WideCharToMultiByte( CP_ACP, 0, &wch, 1, ch, 2, NULL, NULL );
385 wparam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(wparam) );
387 break;
389 return wparam;
393 /***********************************************************************
394 * pack_message
396 * Pack a message for sending to another process.
397 * Return the size of the data we expect in the message reply.
398 * Set data->count to -1 if there is an error.
400 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
401 struct packed_message *data )
403 data->count = 0;
404 switch(message)
406 case WM_NCCREATE:
407 case WM_CREATE:
409 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
410 push_data( data, cs, sizeof(*cs) );
411 if (HIWORD(cs->lpszName)) push_string( data, cs->lpszName );
412 if (HIWORD(cs->lpszClass)) push_string( data, cs->lpszClass );
413 return sizeof(*cs);
415 case WM_GETTEXT:
416 case WM_ASKCBFORMATNAME:
417 return wparam * sizeof(WCHAR);
418 case WM_WININICHANGE:
419 if (lparam) push_string(data, (LPWSTR)lparam );
420 return 0;
421 case WM_SETTEXT:
422 case WM_DEVMODECHANGE:
423 case CB_DIR:
424 case LB_DIR:
425 case LB_ADDFILE:
426 case EM_REPLACESEL:
427 push_string( data, (LPWSTR)lparam );
428 return 0;
429 case WM_GETMINMAXINFO:
430 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
431 return sizeof(MINMAXINFO);
432 case WM_DRAWITEM:
433 push_data( data, (DRAWITEMSTRUCT *)lparam, sizeof(DRAWITEMSTRUCT) );
434 return 0;
435 case WM_MEASUREITEM:
436 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
437 return sizeof(MEASUREITEMSTRUCT);
438 case WM_DELETEITEM:
439 push_data( data, (DELETEITEMSTRUCT *)lparam, sizeof(DELETEITEMSTRUCT) );
440 return 0;
441 case WM_COMPAREITEM:
442 push_data( data, (COMPAREITEMSTRUCT *)lparam, sizeof(COMPAREITEMSTRUCT) );
443 return 0;
444 case WM_WINDOWPOSCHANGING:
445 case WM_WINDOWPOSCHANGED:
446 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
447 return sizeof(WINDOWPOS);
448 case WM_COPYDATA:
450 COPYDATASTRUCT *cp = (COPYDATASTRUCT *)lparam;
451 push_data( data, cp, sizeof(*cp) );
452 if (cp->lpData) push_data( data, cp->lpData, cp->cbData );
453 return 0;
455 case WM_NOTIFY:
456 /* WM_NOTIFY cannot be sent across processes (MSDN) */
457 data->count = -1;
458 return 0;
459 case WM_HELP:
460 push_data( data, (HELPINFO *)lparam, sizeof(HELPINFO) );
461 return 0;
462 case WM_STYLECHANGING:
463 case WM_STYLECHANGED:
464 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
465 return 0;
466 case WM_NCCALCSIZE:
467 if (!wparam)
469 push_data( data, (RECT *)lparam, sizeof(RECT) );
470 return sizeof(RECT);
472 else
474 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
475 push_data( data, nc, sizeof(*nc) );
476 push_data( data, nc->lppos, sizeof(*nc->lppos) );
477 return sizeof(*nc) + sizeof(*nc->lppos);
479 case WM_GETDLGCODE:
480 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
481 return sizeof(MSG);
482 case SBM_SETSCROLLINFO:
483 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
484 return 0;
485 case SBM_GETSCROLLINFO:
486 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
487 return sizeof(SCROLLINFO);
488 case EM_GETSEL:
489 case SBM_GETRANGE:
490 case CB_GETEDITSEL:
492 size_t size = 0;
493 if (wparam) size += sizeof(DWORD);
494 if (lparam) size += sizeof(DWORD);
495 return size;
497 case EM_GETRECT:
498 case LB_GETITEMRECT:
499 case CB_GETDROPPEDCONTROLRECT:
500 return sizeof(RECT);
501 case EM_SETRECT:
502 case EM_SETRECTNP:
503 push_data( data, (RECT *)lparam, sizeof(RECT) );
504 return 0;
505 case EM_GETLINE:
507 WORD *pw = (WORD *)lparam;
508 push_data( data, pw, sizeof(*pw) );
509 return *pw * sizeof(WCHAR);
511 case EM_SETTABSTOPS:
512 case LB_SETTABSTOPS:
513 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
514 return 0;
515 case CB_ADDSTRING:
516 case CB_INSERTSTRING:
517 case CB_FINDSTRING:
518 case CB_FINDSTRINGEXACT:
519 case CB_SELECTSTRING:
520 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
521 return 0;
522 case CB_GETLBTEXT:
523 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
524 return (SendMessageW( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
525 case LB_ADDSTRING:
526 case LB_INSERTSTRING:
527 case LB_FINDSTRING:
528 case LB_FINDSTRINGEXACT:
529 case LB_SELECTSTRING:
530 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
531 return 0;
532 case LB_GETTEXT:
533 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
534 return (SendMessageW( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
535 case LB_GETSELITEMS:
536 return wparam * sizeof(UINT);
537 case WM_NEXTMENU:
538 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
539 return sizeof(MDINEXTMENU);
540 case WM_SIZING:
541 case WM_MOVING:
542 push_data( data, (RECT *)lparam, sizeof(RECT) );
543 return sizeof(RECT);
544 case WM_MDICREATE:
546 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
547 push_data( data, cs, sizeof(*cs) );
548 if (HIWORD(cs->szTitle)) push_string( data, cs->szTitle );
549 if (HIWORD(cs->szClass)) push_string( data, cs->szClass );
550 return sizeof(*cs);
552 case WM_MDIGETACTIVE:
553 if (lparam) return sizeof(BOOL);
554 return 0;
555 case WM_WINE_SETWINDOWPOS:
556 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
557 return 0;
558 case WM_WINE_KEYBOARD_LL_HOOK:
559 push_data( data, (KBDLLHOOKSTRUCT *)lparam, sizeof(KBDLLHOOKSTRUCT) );
560 return 0;
561 case WM_WINE_MOUSE_LL_HOOK:
562 push_data( data, (MSLLHOOKSTRUCT *)lparam, sizeof(MSLLHOOKSTRUCT) );
563 return 0;
565 /* these contain an HFONT */
566 case WM_SETFONT:
567 case WM_GETFONT:
568 /* these contain an HDC */
569 case WM_PAINT:
570 case WM_ERASEBKGND:
571 case WM_ICONERASEBKGND:
572 case WM_NCPAINT:
573 case WM_CTLCOLORMSGBOX:
574 case WM_CTLCOLOREDIT:
575 case WM_CTLCOLORLISTBOX:
576 case WM_CTLCOLORBTN:
577 case WM_CTLCOLORDLG:
578 case WM_CTLCOLORSCROLLBAR:
579 case WM_CTLCOLORSTATIC:
580 case WM_PRINT:
581 case WM_PRINTCLIENT:
582 /* these contain an HGLOBAL */
583 case WM_PAINTCLIPBOARD:
584 case WM_SIZECLIPBOARD:
585 /* these contain pointers */
586 case WM_DROPOBJECT:
587 case WM_QUERYDROPOBJECT:
588 case WM_DRAGLOOP:
589 case WM_DRAGSELECT:
590 case WM_DRAGMOVE:
591 case WM_DEVICECHANGE:
592 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
593 data->count = -1;
594 return 0;
596 return 0;
600 /***********************************************************************
601 * unpack_message
603 * Unpack a message received from another process.
605 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
606 void **buffer, size_t size )
608 size_t minsize = 0;
610 switch(message)
612 case WM_NCCREATE:
613 case WM_CREATE:
615 CREATESTRUCTW *cs = *buffer;
616 WCHAR *str = (WCHAR *)(cs + 1);
617 if (size < sizeof(*cs)) return FALSE;
618 size -= sizeof(*cs);
619 if (HIWORD(cs->lpszName))
621 if (!check_string( str, size )) return FALSE;
622 cs->lpszName = str;
623 size -= (strlenW(str) + 1) * sizeof(WCHAR);
624 str += strlenW(str) + 1;
626 if (HIWORD(cs->lpszClass))
628 if (!check_string( str, size )) return FALSE;
629 cs->lpszClass = str;
631 break;
633 case WM_GETTEXT:
634 case WM_ASKCBFORMATNAME:
635 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)) )) return FALSE;
636 break;
637 case WM_WININICHANGE:
638 if (!*lparam) return TRUE;
639 /* fall through */
640 case WM_SETTEXT:
641 case WM_DEVMODECHANGE:
642 case CB_DIR:
643 case LB_DIR:
644 case LB_ADDFILE:
645 case EM_REPLACESEL:
646 if (!check_string( *buffer, size )) return FALSE;
647 break;
648 case WM_GETMINMAXINFO:
649 minsize = sizeof(MINMAXINFO);
650 break;
651 case WM_DRAWITEM:
652 minsize = sizeof(DRAWITEMSTRUCT);
653 break;
654 case WM_MEASUREITEM:
655 minsize = sizeof(MEASUREITEMSTRUCT);
656 break;
657 case WM_DELETEITEM:
658 minsize = sizeof(DELETEITEMSTRUCT);
659 break;
660 case WM_COMPAREITEM:
661 minsize = sizeof(COMPAREITEMSTRUCT);
662 break;
663 case WM_WINDOWPOSCHANGING:
664 case WM_WINDOWPOSCHANGED:
665 case WM_WINE_SETWINDOWPOS:
666 minsize = sizeof(WINDOWPOS);
667 break;
668 case WM_COPYDATA:
670 COPYDATASTRUCT *cp = *buffer;
671 if (size < sizeof(*cp)) return FALSE;
672 if (cp->lpData)
674 minsize = sizeof(*cp) + cp->cbData;
675 cp->lpData = cp + 1;
677 break;
679 case WM_NOTIFY:
680 /* WM_NOTIFY cannot be sent across processes (MSDN) */
681 return FALSE;
682 case WM_HELP:
683 minsize = sizeof(HELPINFO);
684 break;
685 case WM_STYLECHANGING:
686 case WM_STYLECHANGED:
687 minsize = sizeof(STYLESTRUCT);
688 break;
689 case WM_NCCALCSIZE:
690 if (!*wparam) minsize = sizeof(RECT);
691 else
693 NCCALCSIZE_PARAMS *nc = *buffer;
694 if (size < sizeof(*nc) + sizeof(*nc->lppos)) return FALSE;
695 nc->lppos = (WINDOWPOS *)(nc + 1);
697 break;
698 case WM_GETDLGCODE:
699 if (!*lparam) return TRUE;
700 minsize = sizeof(MSG);
701 break;
702 case SBM_SETSCROLLINFO:
703 minsize = sizeof(SCROLLINFO);
704 break;
705 case SBM_GETSCROLLINFO:
706 if (!get_buffer_space( buffer, sizeof(SCROLLINFO ))) return FALSE;
707 break;
708 case EM_GETSEL:
709 case SBM_GETRANGE:
710 case CB_GETEDITSEL:
711 if (*wparam || *lparam)
713 if (!get_buffer_space( buffer, 2*sizeof(DWORD) )) return FALSE;
714 if (*wparam) *wparam = (WPARAM)*buffer;
715 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
717 return TRUE;
718 case EM_GETRECT:
719 case LB_GETITEMRECT:
720 case CB_GETDROPPEDCONTROLRECT:
721 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
722 break;
723 case EM_SETRECT:
724 case EM_SETRECTNP:
725 minsize = sizeof(RECT);
726 break;
727 case EM_GETLINE:
729 WORD len;
730 if (size < sizeof(WORD)) return FALSE;
731 len = *(WORD *)*buffer;
732 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR) )) return FALSE;
733 *lparam = (LPARAM)*buffer + sizeof(WORD); /* don't erase WORD at start of buffer */
734 return TRUE;
736 case EM_SETTABSTOPS:
737 case LB_SETTABSTOPS:
738 if (!*wparam) return TRUE;
739 minsize = *wparam * sizeof(UINT);
740 break;
741 case CB_ADDSTRING:
742 case CB_INSERTSTRING:
743 case CB_FINDSTRING:
744 case CB_FINDSTRINGEXACT:
745 case CB_SELECTSTRING:
746 case LB_ADDSTRING:
747 case LB_INSERTSTRING:
748 case LB_FINDSTRING:
749 case LB_FINDSTRINGEXACT:
750 case LB_SELECTSTRING:
751 if (!*buffer) return TRUE;
752 if (!check_string( *buffer, size )) return FALSE;
753 break;
754 case CB_GETLBTEXT:
756 size = sizeof(ULONG_PTR);
757 if (combobox_has_strings( hwnd ))
758 size = (SendMessageW( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
759 if (!get_buffer_space( buffer, size )) return FALSE;
760 break;
762 case LB_GETTEXT:
764 size = sizeof(ULONG_PTR);
765 if (listbox_has_strings( hwnd ))
766 size = (SendMessageW( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
767 if (!get_buffer_space( buffer, size )) return FALSE;
768 break;
770 case LB_GETSELITEMS:
771 if (!get_buffer_space( buffer, *wparam * sizeof(UINT) )) return FALSE;
772 break;
773 case WM_NEXTMENU:
774 minsize = sizeof(MDINEXTMENU);
775 if (!get_buffer_space( buffer, sizeof(MDINEXTMENU) )) return FALSE;
776 break;
777 case WM_SIZING:
778 case WM_MOVING:
779 minsize = sizeof(RECT);
780 if (!get_buffer_space( buffer, sizeof(RECT) )) return FALSE;
781 break;
782 case WM_MDICREATE:
784 MDICREATESTRUCTW *cs = *buffer;
785 WCHAR *str = (WCHAR *)(cs + 1);
786 if (size < sizeof(*cs)) return FALSE;
787 size -= sizeof(*cs);
788 if (HIWORD(cs->szTitle))
790 if (!check_string( str, size )) return FALSE;
791 cs->szTitle = str;
792 size -= (strlenW(str) + 1) * sizeof(WCHAR);
793 str += strlenW(str) + 1;
795 if (HIWORD(cs->szClass))
797 if (!check_string( str, size )) return FALSE;
798 cs->szClass = str;
800 break;
802 case WM_MDIGETACTIVE:
803 if (!*lparam) return TRUE;
804 if (!get_buffer_space( buffer, sizeof(BOOL) )) return FALSE;
805 break;
806 case WM_WINE_KEYBOARD_LL_HOOK:
807 minsize = sizeof(KBDLLHOOKSTRUCT);
808 break;
809 case WM_WINE_MOUSE_LL_HOOK:
810 minsize = sizeof(MSLLHOOKSTRUCT);
811 break;
813 /* these contain an HFONT */
814 case WM_SETFONT:
815 case WM_GETFONT:
816 /* these contain an HDC */
817 case WM_PAINT:
818 case WM_ERASEBKGND:
819 case WM_ICONERASEBKGND:
820 case WM_NCPAINT:
821 case WM_CTLCOLORMSGBOX:
822 case WM_CTLCOLOREDIT:
823 case WM_CTLCOLORLISTBOX:
824 case WM_CTLCOLORBTN:
825 case WM_CTLCOLORDLG:
826 case WM_CTLCOLORSCROLLBAR:
827 case WM_CTLCOLORSTATIC:
828 case WM_PRINT:
829 case WM_PRINTCLIENT:
830 /* these contain an HGLOBAL */
831 case WM_PAINTCLIPBOARD:
832 case WM_SIZECLIPBOARD:
833 /* these contain pointers */
834 case WM_DROPOBJECT:
835 case WM_QUERYDROPOBJECT:
836 case WM_DRAGLOOP:
837 case WM_DRAGSELECT:
838 case WM_DRAGMOVE:
839 case WM_DEVICECHANGE:
840 FIXME( "msg %x (%s) not supported yet\n", message, SPY_GetMsgName(message, hwnd) );
841 return FALSE;
843 default:
844 return TRUE; /* message doesn't need any unpacking */
847 /* default exit for most messages: check minsize and store buffer in lparam */
848 if (size < minsize) return FALSE;
849 *lparam = (LPARAM)*buffer;
850 return TRUE;
854 /***********************************************************************
855 * pack_reply
857 * Pack a reply to a message for sending to another process.
859 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
860 LRESULT res, struct packed_message *data )
862 data->count = 0;
863 switch(message)
865 case WM_NCCREATE:
866 case WM_CREATE:
867 push_data( data, (CREATESTRUCTW *)lparam, sizeof(CREATESTRUCTW) );
868 break;
869 case WM_GETTEXT:
870 case CB_GETLBTEXT:
871 case LB_GETTEXT:
872 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
873 break;
874 case WM_GETMINMAXINFO:
875 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
876 break;
877 case WM_MEASUREITEM:
878 push_data( data, (MEASUREITEMSTRUCT *)lparam, sizeof(MEASUREITEMSTRUCT) );
879 break;
880 case WM_WINDOWPOSCHANGING:
881 case WM_WINDOWPOSCHANGED:
882 push_data( data, (WINDOWPOS *)lparam, sizeof(WINDOWPOS) );
883 break;
884 case WM_GETDLGCODE:
885 if (lparam) push_data( data, (MSG *)lparam, sizeof(MSG) );
886 break;
887 case SBM_GETSCROLLINFO:
888 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
889 break;
890 case EM_GETRECT:
891 case LB_GETITEMRECT:
892 case CB_GETDROPPEDCONTROLRECT:
893 case WM_SIZING:
894 case WM_MOVING:
895 push_data( data, (RECT *)lparam, sizeof(RECT) );
896 break;
897 case EM_GETLINE:
899 WORD *ptr = (WORD *)lparam;
900 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
901 break;
903 case LB_GETSELITEMS:
904 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
905 break;
906 case WM_MDIGETACTIVE:
907 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
908 break;
909 case WM_NCCALCSIZE:
910 if (!wparam)
911 push_data( data, (RECT *)lparam, sizeof(RECT) );
912 else
914 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
915 push_data( data, nc, sizeof(*nc) );
916 push_data( data, nc->lppos, sizeof(*nc->lppos) );
918 break;
919 case EM_GETSEL:
920 case SBM_GETRANGE:
921 case CB_GETEDITSEL:
922 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
923 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
924 break;
925 case WM_NEXTMENU:
926 push_data( data, (MDINEXTMENU *)lparam, sizeof(MDINEXTMENU) );
927 break;
928 case WM_MDICREATE:
929 push_data( data, (MDICREATESTRUCTW *)lparam, sizeof(MDICREATESTRUCTW) );
930 break;
931 case WM_ASKCBFORMATNAME:
932 push_data( data, (WCHAR *)lparam, (strlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
933 break;
938 /***********************************************************************
939 * unpack_reply
941 * Unpack a message reply received from another process.
943 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
944 void *buffer, size_t size )
946 switch(message)
948 case WM_NCCREATE:
949 case WM_CREATE:
951 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
952 LPCWSTR name = cs->lpszName, class = cs->lpszClass;
953 memcpy( cs, buffer, min( sizeof(*cs), size ));
954 cs->lpszName = name; /* restore the original pointers */
955 cs->lpszClass = class;
956 break;
958 case WM_GETTEXT:
959 case WM_ASKCBFORMATNAME:
960 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
961 break;
962 case WM_GETMINMAXINFO:
963 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
964 break;
965 case WM_MEASUREITEM:
966 memcpy( (MEASUREITEMSTRUCT *)lparam, buffer, min( sizeof(MEASUREITEMSTRUCT), size ));
967 break;
968 case WM_WINDOWPOSCHANGING:
969 case WM_WINDOWPOSCHANGED:
970 memcpy( (WINDOWPOS *)lparam, buffer, min( sizeof(WINDOWPOS), size ));
971 break;
972 case WM_GETDLGCODE:
973 if (lparam) memcpy( (MSG *)lparam, buffer, min( sizeof(MSG), size ));
974 break;
975 case SBM_GETSCROLLINFO:
976 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
977 break;
978 case EM_GETRECT:
979 case CB_GETDROPPEDCONTROLRECT:
980 case LB_GETITEMRECT:
981 case WM_SIZING:
982 case WM_MOVING:
983 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
984 break;
985 case EM_GETLINE:
986 size = min( size, (size_t)*(WORD *)lparam );
987 memcpy( (WCHAR *)lparam, buffer, size );
988 break;
989 case LB_GETSELITEMS:
990 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
991 break;
992 case LB_GETTEXT:
993 case CB_GETLBTEXT:
994 memcpy( (WCHAR *)lparam, buffer, size );
995 break;
996 case WM_NEXTMENU:
997 memcpy( (MDINEXTMENU *)lparam, buffer, min( sizeof(MDINEXTMENU), size ));
998 break;
999 case WM_MDIGETACTIVE:
1000 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
1001 break;
1002 case WM_NCCALCSIZE:
1003 if (!wparam)
1004 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1005 else
1007 NCCALCSIZE_PARAMS *nc = (NCCALCSIZE_PARAMS *)lparam;
1008 WINDOWPOS *wp = nc->lppos;
1009 memcpy( nc, buffer, min( sizeof(*nc), size ));
1010 if (size > sizeof(*nc))
1012 size -= sizeof(*nc);
1013 memcpy( wp, (NCCALCSIZE_PARAMS*)buffer + 1, min( sizeof(*wp), size ));
1015 nc->lppos = wp; /* restore the original pointer */
1017 break;
1018 case EM_GETSEL:
1019 case SBM_GETRANGE:
1020 case CB_GETEDITSEL:
1021 if (wparam)
1023 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1024 if (size <= sizeof(DWORD)) break;
1025 size -= sizeof(DWORD);
1026 buffer = (DWORD *)buffer + 1;
1028 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1029 break;
1030 case WM_MDICREATE:
1032 MDICREATESTRUCTW *cs = (MDICREATESTRUCTW *)lparam;
1033 LPCWSTR title = cs->szTitle, class = cs->szClass;
1034 memcpy( cs, buffer, min( sizeof(*cs), size ));
1035 cs->szTitle = title; /* restore the original pointers */
1036 cs->szClass = class;
1037 break;
1039 default:
1040 ERR( "should not happen: unexpected message %x\n", message );
1041 break;
1046 /***********************************************************************
1047 * reply_message
1049 * Send a reply to a sent message.
1051 static void reply_message( struct received_message_info *info, LRESULT result, BOOL remove )
1053 struct packed_message data;
1054 int i, replied = info->flags & ISMEX_REPLIED;
1056 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1057 if (!remove && replied) return; /* replied already */
1059 data.count = 0;
1060 info->flags |= ISMEX_REPLIED;
1062 if (info->type == MSG_OTHER_PROCESS && !replied)
1064 pack_reply( info->msg.hwnd, info->msg.message, info->msg.wParam,
1065 info->msg.lParam, result, &data );
1068 SERVER_START_REQ( reply_message )
1070 req->type = info->type;
1071 req->result = result;
1072 req->remove = remove;
1073 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1074 wine_server_call( req );
1076 SERVER_END_REQ;
1080 /***********************************************************************
1081 * handle_internal_message
1083 * Handle an internal Wine message instead of calling the window proc.
1085 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1087 if (hwnd == GetDesktopWindow()) return 0;
1088 switch(msg)
1090 case WM_WINE_DESTROYWINDOW:
1091 return WIN_DestroyWindow( hwnd );
1092 case WM_WINE_SETWINDOWPOS:
1093 return USER_Driver.pSetWindowPos( (WINDOWPOS *)lparam );
1094 case WM_WINE_SHOWWINDOW:
1095 return ShowWindow( hwnd, wparam );
1096 case WM_WINE_SETPARENT:
1097 return (LRESULT)SetParent( hwnd, (HWND)wparam );
1098 case WM_WINE_SETWINDOWLONG:
1099 return (LRESULT)SetWindowLongW( hwnd, wparam, lparam );
1100 case WM_WINE_ENABLEWINDOW:
1101 return EnableWindow( hwnd, wparam );
1102 case WM_WINE_SETACTIVEWINDOW:
1103 return (LRESULT)SetActiveWindow( (HWND)wparam );
1104 case WM_WINE_KEYBOARD_LL_HOOK:
1105 return HOOK_CallHooks( WH_KEYBOARD_LL, HC_ACTION, wparam, lparam, TRUE );
1106 case WM_WINE_MOUSE_LL_HOOK:
1107 return HOOK_CallHooks( WH_MOUSE_LL, HC_ACTION, wparam, lparam, TRUE );
1108 default:
1109 FIXME( "unknown internal message %x\n", msg );
1110 return 0;
1114 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
1115 * to the memory handle, we keep track (in the server side) of all pairs of handle
1116 * used (the client passes its value and the content of the memory handle), and
1117 * the server stored both values (the client, and the local one, created after the
1118 * content). When a ACK message is generated, the list of pair is searched for a
1119 * matching pair, so that the client memory handle can be returned.
1121 struct DDE_pair {
1122 HGLOBAL client_hMem;
1123 HGLOBAL server_hMem;
1126 static struct DDE_pair* dde_pairs;
1127 static int dde_num_alloc;
1128 static int dde_num_used;
1129 static CRITICAL_SECTION dde_crst = CRITICAL_SECTION_INIT("Raw_DDE_CritSect");
1131 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
1133 int i;
1134 #define GROWBY 4
1136 EnterCriticalSection(&dde_crst);
1138 /* now remember the pair of hMem on both sides */
1139 if (dde_num_used == dde_num_alloc)
1141 struct DDE_pair* tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
1142 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
1143 if (!tmp)
1145 LeaveCriticalSection(&dde_crst);
1146 return FALSE;
1148 dde_pairs = tmp;
1149 /* zero out newly allocated part */
1150 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
1151 dde_num_alloc += GROWBY;
1153 #undef GROWBY
1154 for (i = 0; i < dde_num_alloc; i++)
1156 if (dde_pairs[i].server_hMem == 0)
1158 dde_pairs[i].client_hMem = chm;
1159 dde_pairs[i].server_hMem = shm;
1160 dde_num_used++;
1161 break;
1164 LeaveCriticalSection(&dde_crst);
1165 return TRUE;
1168 static HGLOBAL dde_get_pair(HGLOBAL shm)
1170 int i;
1171 HGLOBAL ret = 0;
1173 EnterCriticalSection(&dde_crst);
1174 for (i = 0; i < dde_num_alloc; i++)
1176 if (dde_pairs[i].server_hMem == shm)
1178 /* free this pair */
1179 dde_pairs[i].server_hMem = 0;
1180 dde_num_used--;
1181 ret = dde_pairs[i].client_hMem;
1182 break;
1185 LeaveCriticalSection(&dde_crst);
1186 return ret;
1189 /***********************************************************************
1190 * post_dde_message
1192 * Post a DDE message
1194 static BOOL post_dde_message( DWORD dest_tid, struct packed_message *data, const struct send_message_info *info )
1196 void* ptr = NULL;
1197 int size = 0;
1198 UINT uiLo, uiHi;
1199 LPARAM lp = 0;
1200 HGLOBAL hunlock = 0;
1201 int i;
1202 DWORD res;
1204 if (!UnpackDDElParam( info->msg, info->lparam, &uiLo, &uiHi ))
1205 return FALSE;
1207 lp = info->lparam;
1208 switch (info->msg)
1210 /* DDE messages which don't require packing are:
1211 * WM_DDE_INITIATE
1212 * WM_DDE_TERMINATE
1213 * WM_DDE_REQUEST
1214 * WM_DDE_UNADVISE
1216 case WM_DDE_ACK:
1217 if (HIWORD(uiHi))
1219 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
1220 HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
1221 if (h)
1223 /* send back the value of h on the other side */
1224 push_data( data, &h, sizeof(HGLOBAL) );
1225 lp = uiLo;
1226 TRACE( "send dde-ack %x %08x => %08lx\n", uiLo, uiHi, (DWORD)h );
1229 else
1231 /* uiHi should contain either an atom or 0 */
1232 TRACE( "send dde-ack %x atom=%x\n", uiLo, uiHi );
1233 lp = MAKELONG( uiLo, uiHi );
1235 break;
1236 case WM_DDE_ADVISE:
1237 case WM_DDE_DATA:
1238 case WM_DDE_POKE:
1239 size = 0;
1240 if (uiLo)
1242 size = GlobalSize( (HGLOBAL)uiLo ) ;
1243 if ((info->msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
1244 (info->msg == WM_DDE_DATA && size < sizeof(DDEDATA)) ||
1245 (info->msg == WM_DDE_POKE && size < sizeof(DDEPOKE))
1247 return FALSE;
1249 else if (info->msg != WM_DDE_DATA) return FALSE;
1251 lp = uiHi;
1252 if (uiLo)
1254 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
1256 DDEDATA *dde_data = (DDEDATA *)ptr;
1257 TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
1258 dde_data->unused, dde_data->fResponse, dde_data->fRelease,
1259 dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
1260 push_data( data, ptr, size );
1261 hunlock = (HGLOBAL)uiLo;
1264 TRACE( "send ddepack %u %x\n", size, uiHi );
1265 break;
1266 case WM_DDE_EXECUTE:
1267 if (info->lparam)
1269 if ((ptr = GlobalLock( (HGLOBAL)info->lparam) ))
1271 push_data(data, ptr, GlobalSize( (HGLOBAL)info->lparam ));
1272 /* so that the other side can send it back on ACK */
1273 lp = info->lparam;
1274 hunlock = (HGLOBAL)info->lparam;
1277 break;
1279 SERVER_START_REQ( send_message )
1281 req->id = dest_tid;
1282 req->type = info->type;
1283 req->flags = 0;
1284 req->win = info->hwnd;
1285 req->msg = info->msg;
1286 req->wparam = info->wparam;
1287 req->lparam = lp;
1288 req->time = GetCurrentTime();
1289 req->timeout = -1;
1290 for (i = 0; i < data->count; i++)
1291 wine_server_add_data( req, data->data[i], data->size[i] );
1292 if ((res = wine_server_call( req )))
1294 if (res == STATUS_INVALID_PARAMETER)
1295 /* FIXME: find a STATUS_ value for this one */
1296 SetLastError( ERROR_INVALID_THREAD_ID );
1297 else
1298 SetLastError( RtlNtStatusToDosError(res) );
1300 else
1301 FreeDDElParam(info->msg, info->lparam);
1303 SERVER_END_REQ;
1304 if (hunlock) GlobalUnlock(hunlock);
1306 return !res;
1309 /***********************************************************************
1310 * unpack_dde_message
1312 * Unpack a posted DDE message received from another process.
1314 static BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
1315 void **buffer, size_t size )
1317 UINT uiLo, uiHi;
1318 HGLOBAL hMem = 0;
1319 void* ptr;
1321 switch (message)
1323 case WM_DDE_ACK:
1324 if (size)
1326 /* hMem is being passed */
1327 if (size != sizeof(HGLOBAL)) return FALSE;
1328 if (!buffer || !*buffer) return FALSE;
1329 uiLo = *lparam;
1330 memcpy( &hMem, *buffer, size );
1331 uiHi = (UINT)hMem;
1332 TRACE("recv dde-ack %x mem=%x[%lx]\n", uiLo, uiHi, GlobalSize( hMem ));
1334 else
1336 uiLo = LOWORD( *lparam );
1337 uiHi = HIWORD( *lparam );
1338 TRACE("recv dde-ack %x atom=%x\n", uiLo, uiHi);
1340 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
1341 break;
1342 case WM_DDE_ADVISE:
1343 case WM_DDE_DATA:
1344 case WM_DDE_POKE:
1345 if ((!buffer || !*buffer) && message != WM_DDE_DATA) return FALSE;
1346 uiHi = *lparam;
1347 TRACE( "recv ddepack %u %x\n", size, uiHi );
1348 if (size)
1350 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1351 if (hMem && (ptr = GlobalLock( hMem )))
1353 memcpy( ptr, *buffer, size );
1354 GlobalUnlock( hMem );
1356 else return FALSE;
1358 uiLo = (UINT)hMem;
1360 *lparam = PackDDElParam( message, uiLo, uiHi );
1361 break;
1362 case WM_DDE_EXECUTE:
1363 if (size)
1365 if (!buffer || !*buffer) return FALSE;
1366 hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size );
1367 if (hMem && (ptr = GlobalLock( hMem )))
1369 memcpy( ptr, *buffer, size );
1370 GlobalUnlock( hMem );
1371 TRACE( "exec: pairing c=%08lx s=%08lx\n", *lparam, (DWORD)hMem );
1372 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
1374 GlobalFree( hMem );
1375 return FALSE;
1378 } else return FALSE;
1379 *lparam = (LPARAM)hMem;
1380 break;
1382 return TRUE;
1385 /***********************************************************************
1386 * call_window_proc
1388 * Call a window procedure and the corresponding hooks.
1390 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1391 BOOL unicode, BOOL same_thread )
1393 LRESULT result = 0;
1394 WNDPROC winproc;
1395 CWPSTRUCT cwp;
1396 CWPRETSTRUCT cwpret;
1397 MESSAGEQUEUE *queue = QUEUE_Current();
1399 if (queue->recursion_count > MAX_SENDMSG_RECURSION) return 0;
1400 queue->recursion_count++;
1402 if (msg & 0x80000000)
1404 result = handle_internal_message( hwnd, msg, wparam, lparam );
1405 goto done;
1408 /* first the WH_CALLWNDPROC hook */
1409 hwnd = WIN_GetFullHandle( hwnd );
1410 cwp.lParam = lparam;
1411 cwp.wParam = wparam;
1412 cwp.message = msg;
1413 cwp.hwnd = hwnd;
1414 HOOK_CallHooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, unicode );
1416 /* now call the window procedure */
1417 if (unicode)
1419 if (!(winproc = (WNDPROC)GetWindowLongW( hwnd, GWL_WNDPROC ))) goto done;
1420 result = CallWindowProcW( winproc, hwnd, msg, wparam, lparam );
1422 else
1424 if (!(winproc = (WNDPROC)GetWindowLongA( hwnd, GWL_WNDPROC ))) goto done;
1425 result = CallWindowProcA( winproc, hwnd, msg, wparam, lparam );
1428 /* and finally the WH_CALLWNDPROCRET hook */
1429 cwpret.lResult = result;
1430 cwpret.lParam = lparam;
1431 cwpret.wParam = wparam;
1432 cwpret.message = msg;
1433 cwpret.hwnd = hwnd;
1434 HOOK_CallHooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, unicode );
1435 done:
1436 queue->recursion_count--;
1437 return result;
1441 /***********************************************************************
1442 * process_hardware_message
1444 * Process a hardware message; return TRUE if message should be passed on to the app
1446 static BOOL process_hardware_message( MSG *msg, ULONG_PTR extra_info, HWND hwnd,
1447 UINT first, UINT last, BOOL remove )
1449 BOOL ret;
1451 if (!MSG_process_raw_hardware_message( msg, extra_info, hwnd, first, last, remove ))
1452 return FALSE;
1454 ret = MSG_process_cooked_hardware_message( msg, extra_info, remove );
1456 /* tell the server we have passed it to the app
1457 * (even though we may end up dropping it later on)
1459 SERVER_START_REQ( reply_message )
1461 req->type = MSG_HARDWARE;
1462 req->result = 0;
1463 req->remove = remove || !ret;
1464 wine_server_call( req );
1466 SERVER_END_REQ;
1467 return ret;
1471 /***********************************************************************
1472 * MSG_peek_message
1474 * Peek for a message matching the given parameters. Return FALSE if none available.
1475 * All pending sent messages are processed before returning.
1477 BOOL MSG_peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, int flags )
1479 LRESULT result;
1480 ULONG_PTR extra_info = 0;
1481 MESSAGEQUEUE *queue = QUEUE_Current();
1482 struct received_message_info info, *old_info;
1484 if (!first && !last) last = ~0;
1486 for (;;)
1488 NTSTATUS res;
1489 void *buffer = NULL;
1490 size_t size = 0, buffer_size = 0;
1492 do /* loop while buffer is too small */
1494 if (buffer_size && !(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1495 return FALSE;
1496 SERVER_START_REQ( get_message )
1498 req->flags = flags;
1499 req->get_win = hwnd;
1500 req->get_first = first;
1501 req->get_last = last;
1502 if (buffer_size) wine_server_set_reply( req, buffer, buffer_size );
1503 if (!(res = wine_server_call( req )))
1505 size = wine_server_reply_size( reply );
1506 info.type = reply->type;
1507 info.msg.hwnd = reply->win;
1508 info.msg.message = reply->msg;
1509 info.msg.wParam = reply->wparam;
1510 info.msg.lParam = reply->lparam;
1511 info.msg.time = reply->time;
1512 info.msg.pt.x = reply->x;
1513 info.msg.pt.y = reply->y;
1514 extra_info = reply->info;
1516 else
1518 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1519 buffer_size = reply->total;
1522 SERVER_END_REQ;
1523 } while (res == STATUS_BUFFER_OVERFLOW);
1525 if (res) return FALSE;
1527 TRACE( "got type %d msg %x (%s) hwnd %p wp %x lp %lx\n",
1528 info.type, info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1529 info.msg.hwnd, info.msg.wParam, info.msg.lParam );
1531 switch(info.type)
1533 case MSG_ASCII:
1534 case MSG_UNICODE:
1535 info.flags = ISMEX_SEND;
1536 break;
1537 case MSG_NOTIFY:
1538 info.flags = ISMEX_NOTIFY;
1539 break;
1540 case MSG_CALLBACK:
1541 info.flags = ISMEX_CALLBACK;
1542 break;
1543 case MSG_OTHER_PROCESS:
1544 info.flags = ISMEX_SEND;
1545 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1546 &info.msg.lParam, &buffer, size ))
1548 ERR( "invalid packed message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1549 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd), info.msg.hwnd,
1550 info.msg.wParam, info.msg.lParam, size );
1551 /* ignore it */
1552 reply_message( &info, 0, TRUE );
1553 goto next;
1555 break;
1556 case MSG_HARDWARE:
1557 if (!process_hardware_message( &info.msg, extra_info,
1558 hwnd, first, last, flags & GET_MSG_REMOVE ))
1560 TRACE("dropping msg %x\n", info.msg.message );
1561 goto next; /* ignore it */
1563 queue->GetMessagePosVal = MAKELONG( info.msg.pt.x, info.msg.pt.y );
1564 /* fall through */
1565 case MSG_POSTED:
1566 queue->GetMessageExtraInfoVal = extra_info;
1567 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
1569 if (!unpack_dde_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
1570 &info.msg.lParam, &buffer, size ))
1572 ERR( "invalid packed dde-message %x (%s) hwnd %p wp %x lp %lx size %d\n",
1573 info.msg.message, SPY_GetMsgName(info.msg.message, info.msg.hwnd),
1574 info.msg.hwnd, info.msg.wParam, info.msg.lParam, size );
1575 goto next; /* ignore it */
1578 *msg = info.msg;
1579 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1580 return TRUE;
1583 /* if we get here, we have a sent message; call the window procedure */
1584 old_info = queue->receive_info;
1585 queue->receive_info = &info;
1586 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
1587 info.msg.lParam, (info.type != MSG_ASCII), FALSE );
1588 reply_message( &info, result, TRUE );
1589 queue->receive_info = old_info;
1590 next:
1591 if (buffer) HeapFree( GetProcessHeap(), 0, buffer );
1596 /***********************************************************************
1597 * wait_message_reply
1599 * Wait until a sent message gets replied to.
1601 static void wait_message_reply( UINT flags )
1603 MESSAGEQUEUE *queue;
1605 if (!(queue = QUEUE_Current())) return;
1607 for (;;)
1609 unsigned int wake_bits = 0, changed_bits = 0;
1610 DWORD dwlc, res;
1612 SERVER_START_REQ( set_queue_mask )
1614 req->wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
1615 req->changed_mask = req->wake_mask;
1616 req->skip_wait = 1;
1617 if (!wine_server_call( req ))
1619 wake_bits = reply->wake_bits;
1620 changed_bits = reply->changed_bits;
1623 SERVER_END_REQ;
1625 if (wake_bits & QS_SMRESULT) return; /* got a result */
1626 if (wake_bits & QS_SENDMESSAGE)
1628 /* Process the sent message immediately */
1629 MSG msg;
1630 MSG_peek_message( &msg, 0, 0, 0, GET_MSG_REMOVE | GET_MSG_SENT_ONLY );
1631 continue;
1634 /* now wait for it */
1636 ReleaseThunkLock( &dwlc );
1638 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
1639 res = USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue,
1640 INFINITE, 0, 0 );
1641 else
1642 res = WaitForSingleObject( queue->server_queue, INFINITE );
1644 if (dwlc) RestoreThunkLock( dwlc );
1648 /***********************************************************************
1649 * put_message_in_queue
1651 * Put a sent message into the destination queue.
1652 * For inter-process message, reply_size is set to expected size of reply data.
1654 static BOOL put_message_in_queue( DWORD dest_tid, const struct send_message_info *info,
1655 size_t *reply_size )
1657 struct packed_message data;
1658 unsigned int res;
1659 int i, timeout = -1;
1661 if (info->type != MSG_NOTIFY &&
1662 info->type != MSG_CALLBACK &&
1663 info->type != MSG_POSTED &&
1664 info->timeout != INFINITE)
1665 timeout = info->timeout;
1667 data.count = 0;
1668 if (info->type == MSG_OTHER_PROCESS)
1670 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
1671 if (data.count == -1)
1673 WARN( "cannot pack message %x\n", info->msg );
1674 return FALSE;
1677 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
1679 return post_dde_message( dest_tid, &data, info );
1682 SERVER_START_REQ( send_message )
1684 req->id = dest_tid;
1685 req->type = info->type;
1686 req->flags = 0;
1687 req->win = info->hwnd;
1688 req->msg = info->msg;
1689 req->wparam = info->wparam;
1690 req->lparam = info->lparam;
1691 req->time = GetCurrentTime();
1692 req->timeout = timeout;
1693 if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
1694 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
1695 if ((res = wine_server_call( req )))
1697 if (res == STATUS_INVALID_PARAMETER)
1698 /* FIXME: find a STATUS_ value for this one */
1699 SetLastError( ERROR_INVALID_THREAD_ID );
1700 else
1701 SetLastError( RtlNtStatusToDosError(res) );
1704 SERVER_END_REQ;
1705 return !res;
1709 /***********************************************************************
1710 * retrieve_reply
1712 * Retrieve a message reply from the server.
1714 static LRESULT retrieve_reply( const struct send_message_info *info,
1715 size_t reply_size, LRESULT *result )
1717 NTSTATUS status;
1718 void *reply_data = NULL;
1720 if (reply_size)
1722 if (!(reply_data = HeapAlloc( GetProcessHeap(), 0, reply_size )))
1724 WARN( "no memory for reply %d bytes, will be truncated\n", reply_size );
1725 reply_size = 0;
1728 SERVER_START_REQ( get_message_reply )
1730 req->cancel = 1;
1731 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
1732 if (!(status = wine_server_call( req ))) *result = reply->result;
1733 reply_size = wine_server_reply_size( reply );
1735 SERVER_END_REQ;
1736 if (!status && reply_size)
1737 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
1739 if (reply_data) HeapFree( GetProcessHeap(), 0, reply_data );
1741 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx got reply %lx (err=%ld)\n",
1742 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam,
1743 info->lparam, *result, status );
1745 /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
1746 if (status) SetLastError( RtlNtStatusToDosError(status) );
1747 return !status;
1751 /***********************************************************************
1752 * send_inter_thread_message
1754 static LRESULT send_inter_thread_message( DWORD dest_tid, const struct send_message_info *info,
1755 LRESULT *res_ptr )
1757 LRESULT ret;
1758 int locks;
1759 size_t reply_size = 0;
1761 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
1762 info->hwnd, info->msg, SPY_GetMsgName(info->msg, info->hwnd), info->wparam, info->lparam );
1764 if (!put_message_in_queue( dest_tid, info, &reply_size )) return 0;
1766 /* there's no reply to wait for on notify/callback messages */
1767 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
1769 locks = WIN_SuspendWndsLock();
1771 wait_message_reply( info->flags );
1772 ret = retrieve_reply( info, reply_size, res_ptr );
1774 WIN_RestoreWndsLock( locks );
1775 return ret;
1779 /***********************************************************************
1780 * MSG_SendInternalMessageTimeout
1782 * Same as SendMessageTimeoutW but sends the message to a specific thread
1783 * without requiring a window handle. Only works for internal Wine messages.
1785 LRESULT MSG_SendInternalMessageTimeout( DWORD dest_pid, DWORD dest_tid,
1786 UINT msg, WPARAM wparam, LPARAM lparam,
1787 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1789 struct send_message_info info;
1790 LRESULT ret, result;
1792 assert( msg & 0x80000000 ); /* must be an internal Wine message */
1794 info.type = MSG_UNICODE;
1795 info.hwnd = 0;
1796 info.msg = msg;
1797 info.wparam = wparam;
1798 info.lparam = lparam;
1799 info.flags = flags;
1800 info.timeout = timeout;
1802 if (USER_IsExitingThread( dest_tid )) return 0;
1804 if (dest_tid == GetCurrentThreadId())
1806 result = handle_internal_message( 0, msg, wparam, lparam );
1807 ret = 1;
1809 else
1811 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1812 ret = send_inter_thread_message( dest_tid, &info, &result );
1814 if (ret && res_ptr) *res_ptr = result;
1815 return ret;
1819 /***********************************************************************
1820 * SendMessageTimeoutW (USER32.@)
1822 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1823 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1825 struct send_message_info info;
1826 DWORD dest_tid, dest_pid;
1827 LRESULT ret, result;
1829 info.type = MSG_UNICODE;
1830 info.hwnd = hwnd;
1831 info.msg = msg;
1832 info.wparam = wparam;
1833 info.lparam = lparam;
1834 info.flags = flags;
1835 info.timeout = timeout;
1837 if (is_broadcast(hwnd))
1839 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1840 if (res_ptr) *res_ptr = 1;
1841 return 1;
1844 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1846 if (USER_IsExitingThread( dest_tid )) return 0;
1848 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1850 if (dest_tid == GetCurrentThreadId())
1852 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1853 ret = 1;
1855 else
1857 if (dest_pid != GetCurrentProcessId()) info.type = MSG_OTHER_PROCESS;
1858 ret = send_inter_thread_message( dest_tid, &info, &result );
1861 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1862 if (ret && res_ptr) *res_ptr = result;
1863 return ret;
1867 /***********************************************************************
1868 * SendMessageTimeoutA (USER32.@)
1870 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
1871 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
1873 struct send_message_info info;
1874 DWORD dest_tid, dest_pid;
1875 LRESULT ret, result;
1877 info.type = MSG_ASCII;
1878 info.hwnd = hwnd;
1879 info.msg = msg;
1880 info.wparam = wparam;
1881 info.lparam = lparam;
1882 info.flags = flags;
1883 info.timeout = timeout;
1885 if (is_broadcast(hwnd))
1887 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1888 if (res_ptr) *res_ptr = 1;
1889 return 1;
1892 if (!(dest_tid = GetWindowThreadProcessId( hwnd, &dest_pid ))) return 0;
1894 if (USER_IsExitingThread( dest_tid )) return 0;
1896 SPY_EnterMessage( SPY_SENDMESSAGE, hwnd, msg, wparam, lparam );
1898 if (dest_tid == GetCurrentThreadId())
1900 result = call_window_proc( hwnd, msg, wparam, lparam, FALSE, TRUE );
1901 ret = 1;
1903 else if (dest_pid == GetCurrentProcessId())
1905 ret = send_inter_thread_message( dest_tid, &info, &result );
1907 else
1909 /* inter-process message: need to map to Unicode */
1910 info.type = MSG_OTHER_PROCESS;
1911 if (is_unicode_message( info.msg ))
1913 if (WINPROC_MapMsg32ATo32W( info.hwnd, info.msg, &info.wparam, &info.lparam ) == -1)
1914 return 0;
1915 ret = send_inter_thread_message( dest_tid, &info, &result );
1916 result = WINPROC_UnmapMsg32ATo32W( info.hwnd, info.msg, info.wparam,
1917 info.lparam, result );
1919 else ret = send_inter_thread_message( dest_tid, &info, &result );
1921 SPY_ExitMessage( SPY_RESULT_OK, hwnd, msg, result, wparam, lparam );
1922 if (ret && res_ptr) *res_ptr = result;
1923 return ret;
1927 /***********************************************************************
1928 * SendMessageW (USER32.@)
1930 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1932 LRESULT res = 0;
1933 SendMessageTimeoutW( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1934 return res;
1938 /***********************************************************************
1939 * SendMessageA (USER32.@)
1941 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1943 LRESULT res = 0;
1944 SendMessageTimeoutA( hwnd, msg, wparam, lparam, SMTO_NORMAL, INFINITE, &res );
1945 return res;
1949 /***********************************************************************
1950 * SendNotifyMessageA (USER32.@)
1952 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1954 return SendNotifyMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
1958 /***********************************************************************
1959 * SendNotifyMessageW (USER32.@)
1961 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1963 struct send_message_info info;
1964 DWORD dest_tid;
1965 LRESULT result;
1967 if (is_pointer_message(msg))
1969 SetLastError(ERROR_INVALID_PARAMETER);
1970 return FALSE;
1973 info.type = MSG_NOTIFY;
1974 info.hwnd = hwnd;
1975 info.msg = msg;
1976 info.wparam = wparam;
1977 info.lparam = lparam;
1979 if (is_broadcast(hwnd))
1981 EnumWindows( broadcast_message_callback, (LPARAM)&info );
1982 return TRUE;
1985 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
1987 if (USER_IsExitingThread( dest_tid )) return TRUE;
1989 if (dest_tid == GetCurrentThreadId())
1991 call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
1992 return TRUE;
1994 return send_inter_thread_message( dest_tid, &info, &result );
1998 /***********************************************************************
1999 * SendMessageCallbackA (USER32.@)
2001 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2002 SENDASYNCPROC callback, ULONG_PTR data )
2004 return SendMessageCallbackW( hwnd, msg, map_wparam_AtoW( msg, wparam ),
2005 lparam, callback, data );
2009 /***********************************************************************
2010 * SendMessageCallbackW (USER32.@)
2012 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2013 SENDASYNCPROC callback, ULONG_PTR data )
2015 struct send_message_info info;
2016 LRESULT result;
2017 DWORD dest_tid;
2019 if (is_pointer_message(msg))
2021 SetLastError(ERROR_INVALID_PARAMETER);
2022 return FALSE;
2025 info.type = MSG_CALLBACK;
2026 info.hwnd = hwnd;
2027 info.msg = msg;
2028 info.wparam = wparam;
2029 info.lparam = lparam;
2030 info.callback = callback;
2031 info.data = data;
2033 if (is_broadcast(hwnd))
2035 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2036 return TRUE;
2039 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2041 if (USER_IsExitingThread( dest_tid )) return TRUE;
2043 if (dest_tid == GetCurrentThreadId())
2045 result = call_window_proc( hwnd, msg, wparam, lparam, TRUE, TRUE );
2046 callback( hwnd, msg, data, result );
2047 return TRUE;
2049 FIXME( "callback will not be called\n" );
2050 return send_inter_thread_message( dest_tid, &info, &result );
2054 /***********************************************************************
2055 * ReplyMessage (USER32.@)
2057 BOOL WINAPI ReplyMessage( LRESULT result )
2059 MESSAGEQUEUE *queue = QUEUE_Current();
2060 struct received_message_info *info = queue->receive_info;
2062 if (!info) return FALSE;
2063 reply_message( info, result, FALSE );
2064 return TRUE;
2068 /***********************************************************************
2069 * InSendMessage (USER32.@)
2071 BOOL WINAPI InSendMessage(void)
2073 return (InSendMessageEx(NULL) & (ISMEX_SEND|ISMEX_REPLIED)) == ISMEX_SEND;
2077 /***********************************************************************
2078 * InSendMessageEx (USER32.@)
2080 DWORD WINAPI InSendMessageEx( LPVOID reserved )
2082 MESSAGEQUEUE *queue = QUEUE_Current();
2083 struct received_message_info *info = queue->receive_info;
2085 if (info) return info->flags;
2086 return ISMEX_NOSEND;
2090 /***********************************************************************
2091 * PostMessageA (USER32.@)
2093 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2095 return PostMessageW( hwnd, msg, map_wparam_AtoW( msg, wparam ), lparam );
2099 /***********************************************************************
2100 * PostMessageW (USER32.@)
2102 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2104 struct send_message_info info;
2105 DWORD dest_tid;
2107 if (is_pointer_message( msg ))
2109 SetLastError( ERROR_INVALID_PARAMETER );
2110 return FALSE;
2113 TRACE( "hwnd %p msg %x (%s) wp %x lp %lx\n",
2114 hwnd, msg, SPY_GetMsgName(msg, hwnd), wparam, lparam );
2116 info.type = MSG_POSTED;
2117 info.hwnd = hwnd;
2118 info.msg = msg;
2119 info.wparam = wparam;
2120 info.lparam = lparam;
2122 if (is_broadcast(hwnd))
2124 EnumWindows( broadcast_message_callback, (LPARAM)&info );
2125 return TRUE;
2128 if (!hwnd) return PostThreadMessageW( GetCurrentThreadId(), msg, wparam, lparam );
2130 if (!(dest_tid = GetWindowThreadProcessId( hwnd, NULL ))) return FALSE;
2132 if (USER_IsExitingThread( dest_tid )) return TRUE;
2134 return put_message_in_queue( dest_tid, &info, NULL );
2138 /**********************************************************************
2139 * PostThreadMessageA (USER32.@)
2141 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2143 return PostThreadMessageW( thread, msg, map_wparam_AtoW( msg, wparam ), lparam );
2147 /**********************************************************************
2148 * PostThreadMessageW (USER32.@)
2150 BOOL WINAPI PostThreadMessageW( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
2152 struct send_message_info info;
2154 if (is_pointer_message( msg ))
2156 SetLastError( ERROR_INVALID_PARAMETER );
2157 return FALSE;
2159 if (USER_IsExitingThread( thread )) return TRUE;
2161 info.type = MSG_POSTED;
2162 info.hwnd = 0;
2163 info.msg = msg;
2164 info.wparam = wparam;
2165 info.lparam = lparam;
2166 return put_message_in_queue( thread, &info, NULL );
2170 /***********************************************************************
2171 * PostQuitMessage (USER32.@)
2173 void WINAPI PostQuitMessage( INT exitCode )
2175 PostThreadMessageW( GetCurrentThreadId(), WM_QUIT, exitCode, 0 );
2179 /***********************************************************************
2180 * PeekMessageW (USER32.@)
2182 BOOL WINAPI PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
2184 MESSAGEQUEUE *queue;
2185 MSG msg;
2186 int locks;
2188 /* check for graphics events */
2189 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2190 USER_Driver.pMsgWaitForMultipleObjectsEx( 0, NULL, 0, 0, 0 );
2192 hwnd = WIN_GetFullHandle( hwnd );
2193 locks = WIN_SuspendWndsLock();
2195 if (!MSG_peek_message( &msg, hwnd, first, last,
2196 (flags & PM_REMOVE) ? GET_MSG_REMOVE : 0 ))
2198 if (!(flags & PM_NOYIELD))
2200 DWORD count;
2201 ReleaseThunkLock(&count);
2202 if (count) RestoreThunkLock(count);
2204 WIN_RestoreWndsLock( locks );
2205 return FALSE;
2208 WIN_RestoreWndsLock( locks );
2210 /* need to fill the window handle for WM_PAINT message */
2211 if (msg.message == WM_PAINT)
2213 if (IsIconic( msg.hwnd ) && GetClassLongA( msg.hwnd, GCL_HICON ))
2215 msg.message = WM_PAINTICON;
2216 msg.wParam = 1;
2218 /* clear internal paint flag */
2219 RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
2222 if ((queue = QUEUE_Current()))
2224 queue->GetMessageTimeVal = msg.time;
2225 msg.pt.x = LOWORD( queue->GetMessagePosVal );
2226 msg.pt.y = HIWORD( queue->GetMessagePosVal );
2229 HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
2231 /* copy back our internal safe copy of message data to msg_out.
2232 * msg_out is a variable from the *program*, so it can't be used
2233 * internally as it can get "corrupted" by our use of SendMessage()
2234 * (back to the program) inside the message handling itself. */
2235 *msg_out = msg;
2236 return TRUE;
2240 /***********************************************************************
2241 * PeekMessageA (USER32.@)
2243 BOOL WINAPI PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
2245 BOOL ret = PeekMessageW( msg, hwnd, first, last, flags );
2246 if (ret) msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2247 return ret;
2251 /***********************************************************************
2252 * GetMessageW (USER32.@)
2254 BOOL WINAPI GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
2256 MESSAGEQUEUE *queue = QUEUE_Current();
2257 int mask, locks;
2259 mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
2260 if (first || last)
2262 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
2263 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
2264 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
2265 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
2266 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
2267 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
2269 else mask |= QS_MOUSE | QS_KEY | QS_TIMER | QS_PAINT;
2271 locks = WIN_SuspendWndsLock();
2273 while (!PeekMessageW( msg, hwnd, first, last, PM_REMOVE ))
2275 /* wait until one of the bits is set */
2276 unsigned int wake_bits = 0, changed_bits = 0;
2277 DWORD dwlc;
2279 SERVER_START_REQ( set_queue_mask )
2281 req->wake_mask = QS_SENDMESSAGE;
2282 req->changed_mask = mask;
2283 req->skip_wait = 1;
2284 if (!wine_server_call( req ))
2286 wake_bits = reply->wake_bits;
2287 changed_bits = reply->changed_bits;
2290 SERVER_END_REQ;
2292 if (changed_bits & mask) continue;
2293 if (wake_bits & QS_SENDMESSAGE) continue;
2295 TRACE( "(%04x) mask=%08x, bits=%08x, changed=%08x, waiting\n",
2296 queue->self, mask, wake_bits, changed_bits );
2298 ReleaseThunkLock( &dwlc );
2299 if (USER_Driver.pMsgWaitForMultipleObjectsEx)
2300 USER_Driver.pMsgWaitForMultipleObjectsEx( 1, &queue->server_queue, INFINITE, 0, 0 );
2301 else
2302 WaitForSingleObject( queue->server_queue, INFINITE );
2303 if (dwlc) RestoreThunkLock( dwlc );
2306 WIN_RestoreWndsLock( locks );
2308 return (msg->message != WM_QUIT);
2312 /***********************************************************************
2313 * GetMessageA (USER32.@)
2315 BOOL WINAPI GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
2317 GetMessageW( msg, hwnd, first, last );
2318 msg->wParam = map_wparam_WtoA( msg->message, msg->wParam );
2319 return (msg->message != WM_QUIT);
2323 /***********************************************************************
2324 * IsDialogMessage (USER32.@)
2325 * IsDialogMessageA (USER32.@)
2327 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
2329 MSG msg = *pmsg;
2330 msg.wParam = map_wparam_AtoW( msg.message, msg.wParam );
2331 return IsDialogMessageW( hwndDlg, &msg );
2335 /***********************************************************************
2336 * SetMessageQueue (USER32.@)
2338 BOOL WINAPI SetMessageQueue( INT size )
2340 /* now obsolete the message queue will be expanded dynamically as necessary */
2341 return TRUE;
2345 /**********************************************************************
2346 * AttachThreadInput (USER32.@)
2348 * Attaches the input processing mechanism of one thread to that of
2349 * another thread.
2351 BOOL WINAPI AttachThreadInput( DWORD from, DWORD to, BOOL attach )
2353 BOOL ret;
2355 SERVER_START_REQ( attach_thread_input )
2357 req->tid_from = from;
2358 req->tid_to = to;
2359 req->attach = attach;
2360 ret = !wine_server_call_err( req );
2362 SERVER_END_REQ;
2363 return ret;
2367 /**********************************************************************
2368 * GetGUIThreadInfo (USER32.@)
2370 BOOL WINAPI GetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
2372 BOOL ret;
2374 SERVER_START_REQ( get_thread_input )
2376 req->tid = id;
2377 if ((ret = !wine_server_call_err( req )))
2379 info->flags = 0;
2380 info->hwndActive = reply->active;
2381 info->hwndFocus = reply->focus;
2382 info->hwndCapture = reply->capture;
2383 info->hwndMenuOwner = reply->menu_owner;
2384 info->hwndMoveSize = reply->move_size;
2385 info->hwndCaret = reply->caret;
2386 info->rcCaret.left = reply->rect.left;
2387 info->rcCaret.top = reply->rect.top;
2388 info->rcCaret.right = reply->rect.right;
2389 info->rcCaret.bottom = reply->rect.bottom;
2390 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
2391 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
2392 if (reply->caret) info->flags |= GUI_CARETBLINKING;
2395 SERVER_END_REQ;
2396 return ret;
2400 /**********************************************************************
2401 * GetKeyState (USER32.@)
2403 * An application calls the GetKeyState function in response to a
2404 * keyboard-input message. This function retrieves the state of the key
2405 * at the time the input message was generated.
2407 SHORT WINAPI GetKeyState(INT vkey)
2409 SHORT retval = 0;
2411 SERVER_START_REQ( get_key_state )
2413 req->tid = GetCurrentThreadId();
2414 req->key = vkey;
2415 if (!wine_server_call( req )) retval = (signed char)reply->state;
2417 SERVER_END_REQ;
2418 TRACE("key (0x%x) -> %x\n", vkey, retval);
2419 return retval;
2423 /**********************************************************************
2424 * GetKeyboardState (USER32.@)
2426 BOOL WINAPI GetKeyboardState( LPBYTE state )
2428 BOOL ret;
2430 TRACE("(%p)\n", state);
2432 memset( state, 0, 256 );
2433 SERVER_START_REQ( get_key_state )
2435 req->tid = GetCurrentThreadId();
2436 req->key = -1;
2437 wine_server_set_reply( req, state, 256 );
2438 ret = !wine_server_call_err( req );
2440 SERVER_END_REQ;
2441 return ret;
2445 /**********************************************************************
2446 * SetKeyboardState (USER32.@)
2448 BOOL WINAPI SetKeyboardState( LPBYTE state )
2450 BOOL ret;
2452 TRACE("(%p)\n", state);
2454 SERVER_START_REQ( set_key_state )
2456 req->tid = GetCurrentThreadId();
2457 wine_server_add_data( req, state, 256 );
2458 ret = !wine_server_call_err( req );
2460 SERVER_END_REQ;
2461 return ret;