cmd: DIR command outputs free space for the path.
[wine.git] / dlls / win32u / message.c
blobd15f9af3f9f965523d2eb27b4a4e9bf983fc0824
1 /*
2 * Window messaging support
4 * Copyright 2001 Alexandre Julliard
5 * Copyright 2008 Maarten Lankhorst
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #if 0
24 #pragma makedep unix
25 #endif
27 #include <assert.h>
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "win32u_private.h"
31 #include "ntuser_private.h"
32 #include "winnls.h"
33 #include "hidusage.h"
34 #include "dbt.h"
35 #include "dde.h"
36 #include "immdev.h"
37 #include "wine/server.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msg);
41 WINE_DECLARE_DEBUG_CHANNEL(key);
42 WINE_DECLARE_DEBUG_CHANNEL(relay);
44 #define MAX_WINPROC_RECURSION 64
46 #define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
47 #define WM_NCMOUSELAST (WM_NCMOUSEFIRST+(WM_MOUSELAST-WM_MOUSEFIRST))
49 #define MAX_PACK_COUNT 4
51 /* info about the message currently being received by the current thread */
52 struct received_message_info
54 UINT type;
55 MSG msg;
56 UINT flags; /* InSendMessageEx return flags */
57 struct received_message_info *prev;
60 struct packed_hook_extra_info
62 user_handle_t handle;
63 DWORD __pad;
64 ULONGLONG lparam;
67 /* the various structures that can be sent in messages, in platform-independent layout */
68 struct packed_CREATESTRUCTW
70 ULONGLONG lpCreateParams;
71 ULONGLONG hInstance;
72 user_handle_t hMenu;
73 DWORD __pad1;
74 user_handle_t hwndParent;
75 DWORD __pad2;
76 INT cy;
77 INT cx;
78 INT y;
79 INT x;
80 LONG style;
81 ULONGLONG lpszName;
82 ULONGLONG lpszClass;
83 DWORD dwExStyle;
84 DWORD __pad3;
87 struct packed_DRAWITEMSTRUCT
89 UINT CtlType;
90 UINT CtlID;
91 UINT itemID;
92 UINT itemAction;
93 UINT itemState;
94 user_handle_t hwndItem;
95 DWORD __pad1;
96 user_handle_t hDC;
97 DWORD __pad2;
98 RECT rcItem;
99 ULONGLONG itemData;
102 struct packed_MEASUREITEMSTRUCT
104 UINT CtlType;
105 UINT CtlID;
106 UINT itemID;
107 UINT itemWidth;
108 UINT itemHeight;
109 ULONGLONG itemData;
112 struct packed_DELETEITEMSTRUCT
114 UINT CtlType;
115 UINT CtlID;
116 UINT itemID;
117 user_handle_t hwndItem;
118 DWORD __pad;
119 ULONGLONG itemData;
122 struct packed_COMPAREITEMSTRUCT
124 UINT CtlType;
125 UINT CtlID;
126 user_handle_t hwndItem;
127 DWORD __pad1;
128 UINT itemID1;
129 ULONGLONG itemData1;
130 UINT itemID2;
131 ULONGLONG itemData2;
132 DWORD dwLocaleId;
133 DWORD __pad2;
136 struct packed_WINDOWPOS
138 UINT hwnd;
139 DWORD __pad1;
140 user_handle_t hwndInsertAfter;
141 DWORD __pad2;
142 INT x;
143 INT y;
144 INT cx;
145 INT cy;
146 UINT flags;
147 DWORD __pad3;
150 struct packed_COPYDATASTRUCT
152 ULONGLONG dwData;
153 DWORD cbData;
154 ULONGLONG lpData;
157 struct packed_HELPINFO
159 UINT cbSize;
160 INT iContextType;
161 INT iCtrlId;
162 user_handle_t hItemHandle;
163 DWORD __pad;
164 ULONGLONG dwContextId;
165 POINT MousePos;
168 struct packed_NCCALCSIZE_PARAMS
170 RECT rgrc[3];
171 ULONGLONG __pad1;
172 user_handle_t hwnd;
173 DWORD __pad2;
174 user_handle_t hwndInsertAfter;
175 DWORD __pad3;
176 INT x;
177 INT y;
178 INT cx;
179 INT cy;
180 UINT flags;
181 DWORD __pad4;
184 struct packed_MSG
186 user_handle_t hwnd;
187 DWORD __pad1;
188 UINT message;
189 ULONGLONG wParam;
190 ULONGLONG lParam;
191 DWORD time;
192 POINT pt;
193 DWORD __pad2;
196 struct packed_MDINEXTMENU
198 user_handle_t hmenuIn;
199 DWORD __pad1;
200 user_handle_t hmenuNext;
201 DWORD __pad2;
202 user_handle_t hwndNext;
203 DWORD __pad3;
206 struct packed_MDICREATESTRUCTW
208 ULONGLONG szClass;
209 ULONGLONG szTitle;
210 ULONGLONG hOwner;
211 INT x;
212 INT y;
213 INT cx;
214 INT cy;
215 DWORD style;
216 ULONGLONG lParam;
219 struct packed_COMBOBOXINFO
221 DWORD cbSize;
222 RECT rcItem;
223 RECT rcButton;
224 DWORD stateButton;
225 ULONGLONG hwndCombo;
226 ULONGLONG hwndItem;
227 ULONGLONG hwndList;
230 /* the structures are unpacked on top of the packed ones, so make sure they fit */
231 C_ASSERT( sizeof(struct packed_CREATESTRUCTW) >= sizeof(CREATESTRUCTW) );
232 C_ASSERT( sizeof(struct packed_DRAWITEMSTRUCT) >= sizeof(DRAWITEMSTRUCT) );
233 C_ASSERT( sizeof(struct packed_MEASUREITEMSTRUCT) >= sizeof(MEASUREITEMSTRUCT) );
234 C_ASSERT( sizeof(struct packed_DELETEITEMSTRUCT) >= sizeof(DELETEITEMSTRUCT) );
235 C_ASSERT( sizeof(struct packed_COMPAREITEMSTRUCT) >= sizeof(COMPAREITEMSTRUCT) );
236 C_ASSERT( sizeof(struct packed_WINDOWPOS) >= sizeof(WINDOWPOS) );
237 C_ASSERT( sizeof(struct packed_COPYDATASTRUCT) >= sizeof(COPYDATASTRUCT) );
238 C_ASSERT( sizeof(struct packed_HELPINFO) >= sizeof(HELPINFO) );
239 C_ASSERT( sizeof(struct packed_NCCALCSIZE_PARAMS) >= sizeof(NCCALCSIZE_PARAMS) + sizeof(WINDOWPOS) );
240 C_ASSERT( sizeof(struct packed_MSG) >= sizeof(MSG) );
241 C_ASSERT( sizeof(struct packed_MDINEXTMENU) >= sizeof(MDINEXTMENU) );
242 C_ASSERT( sizeof(struct packed_MDICREATESTRUCTW) >= sizeof(MDICREATESTRUCTW) );
243 C_ASSERT( sizeof(struct packed_COMBOBOXINFO) >= sizeof(COMBOBOXINFO) );
244 C_ASSERT( sizeof(struct packed_hook_extra_info) >= sizeof(struct hook_extra_info) );
246 union packed_structs
248 struct packed_CREATESTRUCTW cs;
249 struct packed_DRAWITEMSTRUCT dis;
250 struct packed_MEASUREITEMSTRUCT mis;
251 struct packed_DELETEITEMSTRUCT dls;
252 struct packed_COMPAREITEMSTRUCT cis;
253 struct packed_WINDOWPOS wp;
254 struct packed_COPYDATASTRUCT cds;
255 struct packed_HELPINFO hi;
256 struct packed_NCCALCSIZE_PARAMS ncp;
257 struct packed_MSG msg;
258 struct packed_MDINEXTMENU mnm;
259 struct packed_MDICREATESTRUCTW mcs;
260 struct packed_COMBOBOXINFO cbi;
261 struct packed_hook_extra_info hook;
264 /* description of the data fields that need to be packed along with a sent message */
265 struct packed_message
267 union packed_structs ps;
268 int count;
269 const void *data[MAX_PACK_COUNT];
270 size_t size[MAX_PACK_COUNT];
273 /* structure to group all parameters for sent messages of the various kinds */
274 struct send_message_info
276 enum message_type type;
277 DWORD dest_tid;
278 HWND hwnd;
279 UINT msg;
280 WPARAM wparam;
281 LPARAM lparam;
282 UINT flags; /* flags for SendMessageTimeout */
283 UINT timeout; /* timeout for SendMessageTimeout */
284 SENDASYNCPROC callback; /* callback function for SendMessageCallback */
285 ULONG_PTR data; /* callback data */
286 enum wm_char_mapping wm_char;
287 struct win_proc_params *params;
290 static const INPUT_MESSAGE_SOURCE msg_source_unavailable = { IMDT_UNAVAILABLE, IMO_UNAVAILABLE };
292 /* flag for messages that contain pointers */
293 /* 32 messages per entry, messages 0..31 map to bits 0..31 */
295 #define SET(msg) (1 << ((msg) & 31))
297 static const unsigned int message_pointer_flags[] =
299 /* 0x00 - 0x1f */
300 SET(WM_CREATE) | SET(WM_SETTEXT) | SET(WM_GETTEXT) |
301 SET(WM_WININICHANGE) | SET(WM_DEVMODECHANGE),
302 /* 0x20 - 0x3f */
303 SET(WM_GETMINMAXINFO) | SET(WM_DRAWITEM) | SET(WM_MEASUREITEM) | SET(WM_DELETEITEM) |
304 SET(WM_COMPAREITEM),
305 /* 0x40 - 0x5f */
306 SET(WM_WINDOWPOSCHANGING) | SET(WM_WINDOWPOSCHANGED) | SET(WM_COPYDATA) | SET(WM_HELP),
307 /* 0x60 - 0x7f */
308 SET(WM_STYLECHANGING) | SET(WM_STYLECHANGED),
309 /* 0x80 - 0x9f */
310 SET(WM_NCCREATE) | SET(WM_NCCALCSIZE) | SET(WM_GETDLGCODE),
311 /* 0xa0 - 0xbf */
312 SET(EM_GETSEL) | SET(EM_GETRECT) | SET(EM_SETRECT) | SET(EM_SETRECTNP),
313 /* 0xc0 - 0xdf */
314 SET(EM_REPLACESEL) | SET(EM_GETLINE) | SET(EM_SETTABSTOPS),
315 /* 0xe0 - 0xff */
316 SET(SBM_GETRANGE) | SET(SBM_SETSCROLLINFO) | SET(SBM_GETSCROLLINFO) | SET(SBM_GETSCROLLBARINFO),
317 /* 0x100 - 0x11f */
319 /* 0x120 - 0x13f */
321 /* 0x140 - 0x15f */
322 SET(CB_GETEDITSEL) | SET(CB_ADDSTRING) | SET(CB_DIR) | SET(CB_GETLBTEXT) |
323 SET(CB_INSERTSTRING) | SET(CB_FINDSTRING) | SET(CB_SELECTSTRING) |
324 SET(CB_GETDROPPEDCONTROLRECT) | SET(CB_FINDSTRINGEXACT),
325 /* 0x160 - 0x17f */
326 SET(CB_GETCOMBOBOXINFO),
327 /* 0x180 - 0x19f */
328 SET(LB_ADDSTRING) | SET(LB_INSERTSTRING) | SET(LB_GETTEXT) | SET(LB_SELECTSTRING) |
329 SET(LB_DIR) | SET(LB_FINDSTRING) |
330 SET(LB_GETSELITEMS) | SET(LB_SETTABSTOPS) | SET(LB_ADDFILE) | SET(LB_GETITEMRECT),
331 /* 0x1a0 - 0x1bf */
332 SET(LB_FINDSTRINGEXACT),
333 /* 0x1c0 - 0x1df */
335 /* 0x1e0 - 0x1ff */
337 /* 0x200 - 0x21f */
338 SET(WM_NEXTMENU) | SET(WM_SIZING) | SET(WM_MOVING) | SET(WM_DEVICECHANGE),
339 /* 0x220 - 0x23f */
340 SET(WM_MDICREATE) | SET(WM_MDIGETACTIVE) | SET(WM_DROPOBJECT) |
341 SET(WM_QUERYDROPOBJECT) | SET(WM_DRAGLOOP) | SET(WM_DRAGSELECT) | SET(WM_DRAGMOVE),
342 /* 0x240 - 0x25f */
344 /* 0x260 - 0x27f */
346 /* 0x280 - 0x29f */
348 /* 0x2a0 - 0x2bf */
350 /* 0x2c0 - 0x2df */
352 /* 0x2e0 - 0x2ff */
354 /* 0x300 - 0x31f */
355 SET(WM_ASKCBFORMATNAME)
358 /* check whether a given message type includes pointers */
359 static inline BOOL is_pointer_message( UINT message, WPARAM wparam )
361 if (message >= 8*sizeof(message_pointer_flags)) return FALSE;
362 if (message == WM_DEVICECHANGE && !(wparam & 0x8000)) return FALSE;
363 return (message_pointer_flags[message / 32] & SET(message)) != 0;
366 #undef SET
368 static BOOL init_win_proc_params( struct win_proc_params *params, HWND hwnd, UINT msg,
369 WPARAM wparam, LPARAM lparam, BOOL ansi )
371 if (!params->func) return FALSE;
373 user_check_not_lock();
375 params->hwnd = get_full_window_handle( hwnd );
376 params->msg = msg;
377 params->wparam = wparam;
378 params->lparam = lparam;
379 params->ansi = params->ansi_dst = ansi;
380 params->mapping = WMCHAR_MAP_CALLWINDOWPROC;
381 params->dpi_awareness = get_window_dpi_awareness_context( params->hwnd );
382 get_winproc_params( params, TRUE );
383 return TRUE;
386 static BOOL init_window_call_params( struct win_proc_params *params, HWND hwnd, UINT msg, WPARAM wParam,
387 LPARAM lParam, BOOL ansi, enum wm_char_mapping mapping )
389 BOOL is_dialog;
390 WND *win;
392 user_check_not_lock();
394 if (!(win = get_win_ptr( hwnd ))) return FALSE;
395 if (win == WND_OTHER_PROCESS || win == WND_DESKTOP) return FALSE;
396 if (win->tid != GetCurrentThreadId())
398 release_win_ptr( win );
399 return FALSE;
401 params->func = win->winproc;
402 params->ansi_dst = !(win->flags & WIN_ISUNICODE);
403 is_dialog = win->dlgInfo != NULL;
404 release_win_ptr( win );
406 params->hwnd = get_full_window_handle( hwnd );
407 params->msg = msg;
408 params->wparam = wParam;
409 params->lparam = lParam;
410 params->ansi = ansi;
411 params->mapping = mapping;
412 params->dpi_awareness = get_window_dpi_awareness_context( params->hwnd );
413 get_winproc_params( params, !is_dialog );
414 return TRUE;
417 static LRESULT dispatch_win_proc_params( struct win_proc_params *params, size_t size,
418 void **client_ret, size_t *client_ret_size )
420 struct ntuser_thread_info *thread_info = NtUserGetThreadInfo();
421 LRESULT result = 0;
422 void *ret_ptr;
423 ULONG ret_len;
425 if (thread_info->recursion_count > MAX_WINPROC_RECURSION) return 0;
426 thread_info->recursion_count++;
427 KeUserModeCallback( NtUserCallWinProc, params, size, &ret_ptr, &ret_len );
428 thread_info->recursion_count--;
430 if (ret_len >= sizeof(result))
432 result = *(LRESULT *)ret_ptr;
433 if (client_ret)
435 *client_ret = (LRESULT *)ret_ptr + 1;
436 *client_ret_size = ret_len - sizeof(result);
440 return result;
443 /* add a data field to a packed message */
444 static inline void push_data( struct packed_message *data, const void *ptr, size_t size )
446 data->data[data->count] = ptr;
447 data->size[data->count] = size;
448 data->count++;
451 /* pack a pointer into a 32/64 portable format */
452 static inline ULONGLONG pack_ptr( const void *ptr )
454 return (ULONG_PTR)ptr;
457 /* unpack a potentially 64-bit pointer, returning 0 when truncated */
458 static inline void *unpack_ptr( ULONGLONG ptr64 )
460 if ((ULONG_PTR)ptr64 != ptr64) return 0;
461 return (void *)(ULONG_PTR)ptr64;
464 /* add a string to a packed message */
465 static inline void push_string( struct packed_message *data, LPCWSTR str )
467 push_data( data, str, (lstrlenW(str) + 1) * sizeof(WCHAR) );
470 /* make sure that there is space for 'size' bytes in buffer, growing it if needed */
471 static inline void *get_buffer_space( void **buffer, size_t size, size_t prev_size )
473 if (prev_size < size) *buffer = malloc( size );
474 return *buffer;
477 /* check whether a combobox expects strings or ids in CB_ADDSTRING/CB_INSERTSTRING */
478 static inline BOOL combobox_has_strings( HWND hwnd )
480 DWORD style = get_window_long( hwnd, GWL_STYLE );
481 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
484 /* check whether a listbox expects strings or ids in LB_ADDSTRING/LB_INSERTSTRING */
485 static inline BOOL listbox_has_strings( HWND hwnd )
487 DWORD style = get_window_long( hwnd, GWL_STYLE );
488 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
491 /* check whether message is in the range of keyboard messages */
492 static inline BOOL is_keyboard_message( UINT message )
494 return (message >= WM_KEYFIRST && message <= WM_KEYLAST);
497 /* check whether message is in the range of mouse messages */
498 static inline BOOL is_mouse_message( UINT message )
500 return ((message >= WM_NCMOUSEFIRST && message <= WM_NCMOUSELAST) ||
501 (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST));
504 /* check whether message matches the specified hwnd filter */
505 static inline BOOL check_hwnd_filter( const MSG *msg, HWND hwnd_filter )
507 if (!hwnd_filter || hwnd_filter == get_desktop_window()) return TRUE;
508 return (msg->hwnd == hwnd_filter || is_child( hwnd_filter, msg->hwnd ));
511 /***********************************************************************
512 * unpack_message
514 * Unpack a message received from another process.
516 static BOOL unpack_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
517 void **buffer, size_t size )
519 size_t minsize = 0;
520 union packed_structs *ps = *buffer;
522 switch(message)
524 case WM_NCCREATE:
525 case WM_CREATE:
527 CREATESTRUCTW cs;
528 WCHAR *str = (WCHAR *)(&ps->cs + 1);
529 if (size < sizeof(ps->cs)) return FALSE;
530 size -= sizeof(ps->cs);
531 cs.lpCreateParams = unpack_ptr( ps->cs.lpCreateParams );
532 cs.hInstance = unpack_ptr( ps->cs.hInstance );
533 cs.hMenu = wine_server_ptr_handle( ps->cs.hMenu );
534 cs.hwndParent = wine_server_ptr_handle( ps->cs.hwndParent );
535 cs.cy = ps->cs.cy;
536 cs.cx = ps->cs.cx;
537 cs.y = ps->cs.y;
538 cs.x = ps->cs.x;
539 cs.style = ps->cs.style;
540 cs.dwExStyle = ps->cs.dwExStyle;
541 cs.lpszName = unpack_ptr( ps->cs.lpszName );
542 cs.lpszClass = unpack_ptr( ps->cs.lpszClass );
543 if (ps->cs.lpszName >> 16)
545 cs.lpszName = str;
546 size -= (lstrlenW(str) + 1) * sizeof(WCHAR);
547 str += lstrlenW(str) + 1;
549 if (ps->cs.lpszClass >> 16)
551 cs.lpszClass = str;
553 memcpy( *buffer, &cs, sizeof(cs) );
554 break;
556 case WM_NCCALCSIZE:
557 if (!*wparam) minsize = sizeof(RECT);
558 else
560 NCCALCSIZE_PARAMS ncp;
561 WINDOWPOS wp;
562 if (size < sizeof(ps->ncp)) return FALSE;
563 ncp.rgrc[0] = ps->ncp.rgrc[0];
564 ncp.rgrc[1] = ps->ncp.rgrc[1];
565 ncp.rgrc[2] = ps->ncp.rgrc[2];
566 wp.hwnd = wine_server_ptr_handle( ps->ncp.hwnd );
567 wp.hwndInsertAfter = wine_server_ptr_handle( ps->ncp.hwndInsertAfter );
568 wp.x = ps->ncp.x;
569 wp.y = ps->ncp.y;
570 wp.cx = ps->ncp.cx;
571 wp.cy = ps->ncp.cy;
572 wp.flags = ps->ncp.flags;
573 ncp.lppos = (WINDOWPOS *)((NCCALCSIZE_PARAMS *)&ps->ncp + 1);
574 memcpy( &ps->ncp, &ncp, sizeof(ncp) );
575 *ncp.lppos = wp;
577 break;
578 case WM_GETTEXT:
579 case WM_ASKCBFORMATNAME:
580 if (!get_buffer_space( buffer, (*wparam * sizeof(WCHAR)), size )) return FALSE;
581 break;
582 case WM_WININICHANGE:
583 if (!*lparam) return TRUE;
584 /* fall through */
585 case WM_SETTEXT:
586 case WM_DEVMODECHANGE:
587 case CB_DIR:
588 case LB_DIR:
589 case LB_ADDFILE:
590 case EM_REPLACESEL:
591 break;
592 case WM_GETMINMAXINFO:
593 minsize = sizeof(MINMAXINFO);
594 break;
595 case WM_DRAWITEM:
597 DRAWITEMSTRUCT dis;
598 if (size < sizeof(ps->dis)) return FALSE;
599 dis.CtlType = ps->dis.CtlType;
600 dis.CtlID = ps->dis.CtlID;
601 dis.itemID = ps->dis.itemID;
602 dis.itemAction = ps->dis.itemAction;
603 dis.itemState = ps->dis.itemState;
604 dis.hwndItem = wine_server_ptr_handle( ps->dis.hwndItem );
605 dis.hDC = wine_server_ptr_handle( ps->dis.hDC );
606 dis.rcItem = ps->dis.rcItem;
607 dis.itemData = (ULONG_PTR)unpack_ptr( ps->dis.itemData );
608 memcpy( *buffer, &dis, sizeof(dis) );
609 break;
611 case WM_MEASUREITEM:
613 MEASUREITEMSTRUCT mis;
614 if (size < sizeof(ps->mis)) return FALSE;
615 mis.CtlType = ps->mis.CtlType;
616 mis.CtlID = ps->mis.CtlID;
617 mis.itemID = ps->mis.itemID;
618 mis.itemWidth = ps->mis.itemWidth;
619 mis.itemHeight = ps->mis.itemHeight;
620 mis.itemData = (ULONG_PTR)unpack_ptr( ps->mis.itemData );
621 memcpy( *buffer, &mis, sizeof(mis) );
622 break;
624 case WM_DELETEITEM:
626 DELETEITEMSTRUCT dls;
627 if (size < sizeof(ps->dls)) return FALSE;
628 dls.CtlType = ps->dls.CtlType;
629 dls.CtlID = ps->dls.CtlID;
630 dls.itemID = ps->dls.itemID;
631 dls.hwndItem = wine_server_ptr_handle( ps->dls.hwndItem );
632 dls.itemData = (ULONG_PTR)unpack_ptr( ps->dls.itemData );
633 memcpy( *buffer, &dls, sizeof(dls) );
634 break;
636 case WM_COMPAREITEM:
638 COMPAREITEMSTRUCT cis;
639 if (size < sizeof(ps->cis)) return FALSE;
640 cis.CtlType = ps->cis.CtlType;
641 cis.CtlID = ps->cis.CtlID;
642 cis.hwndItem = wine_server_ptr_handle( ps->cis.hwndItem );
643 cis.itemID1 = ps->cis.itemID1;
644 cis.itemData1 = (ULONG_PTR)unpack_ptr( ps->cis.itemData1 );
645 cis.itemID2 = ps->cis.itemID2;
646 cis.itemData2 = (ULONG_PTR)unpack_ptr( ps->cis.itemData2 );
647 cis.dwLocaleId = ps->cis.dwLocaleId;
648 memcpy( *buffer, &cis, sizeof(cis) );
649 break;
651 case WM_WINDOWPOSCHANGING:
652 case WM_WINDOWPOSCHANGED:
654 WINDOWPOS wp;
655 if (size < sizeof(ps->wp)) return FALSE;
656 wp.hwnd = wine_server_ptr_handle( ps->wp.hwnd );
657 wp.hwndInsertAfter = wine_server_ptr_handle( ps->wp.hwndInsertAfter );
658 wp.x = ps->wp.x;
659 wp.y = ps->wp.y;
660 wp.cx = ps->wp.cx;
661 wp.cy = ps->wp.cy;
662 wp.flags = ps->wp.flags;
663 memcpy( *buffer, &wp, sizeof(wp) );
664 break;
666 case WM_COPYDATA:
668 COPYDATASTRUCT cds;
669 if (size < sizeof(ps->cds)) return FALSE;
670 cds.dwData = (ULONG_PTR)unpack_ptr( ps->cds.dwData );
671 if (ps->cds.lpData)
673 cds.cbData = ps->cds.cbData;
674 cds.lpData = &ps->cds + 1;
675 minsize = sizeof(ps->cds) + cds.cbData;
677 else
679 cds.cbData = 0;
680 cds.lpData = 0;
682 memcpy( &ps->cds, &cds, sizeof(cds) );
683 break;
685 case WM_HELP:
687 HELPINFO hi;
688 if (size < sizeof(ps->hi)) return FALSE;
689 hi.cbSize = sizeof(hi);
690 hi.iContextType = ps->hi.iContextType;
691 hi.iCtrlId = ps->hi.iCtrlId;
692 hi.hItemHandle = wine_server_ptr_handle( ps->hi.hItemHandle );
693 hi.dwContextId = (ULONG_PTR)unpack_ptr( ps->hi.dwContextId );
694 hi.MousePos = ps->hi.MousePos;
695 memcpy( &ps->hi, &hi, sizeof(hi) );
696 break;
698 case WM_STYLECHANGING:
699 case WM_STYLECHANGED:
700 minsize = sizeof(STYLESTRUCT);
701 break;
702 case WM_GETDLGCODE:
703 if (*lparam)
705 MSG msg;
706 if (size < sizeof(ps->msg)) return FALSE;
707 msg.hwnd = wine_server_ptr_handle( ps->msg.hwnd );
708 msg.message = ps->msg.message;
709 msg.wParam = (ULONG_PTR)unpack_ptr( ps->msg.wParam );
710 msg.lParam = (ULONG_PTR)unpack_ptr( ps->msg.lParam );
711 msg.time = ps->msg.time;
712 msg.pt = ps->msg.pt;
713 memcpy( &ps->msg, &msg, sizeof(msg) );
714 break;
716 return TRUE;
717 case SBM_SETSCROLLINFO:
718 minsize = sizeof(SCROLLINFO);
719 break;
720 case SBM_GETSCROLLINFO:
721 if (!get_buffer_space( buffer, sizeof(SCROLLINFO), size )) return FALSE;
722 break;
723 case SBM_GETSCROLLBARINFO:
724 if (!get_buffer_space( buffer, sizeof(SCROLLBARINFO), size )) return FALSE;
725 break;
726 case EM_GETSEL:
727 case SBM_GETRANGE:
728 case CB_GETEDITSEL:
729 if (*wparam || *lparam)
731 if (!get_buffer_space( buffer, 2 * sizeof(DWORD), size )) return FALSE;
732 if (*wparam) *wparam = (WPARAM)*buffer;
733 if (*lparam) *lparam = (LPARAM)((DWORD *)*buffer + 1);
735 return TRUE;
736 case EM_GETRECT:
737 case LB_GETITEMRECT:
738 case CB_GETDROPPEDCONTROLRECT:
739 if (!get_buffer_space( buffer, sizeof(RECT), size )) return FALSE;
740 break;
741 case EM_SETRECT:
742 case EM_SETRECTNP:
743 minsize = sizeof(RECT);
744 break;
745 case EM_GETLINE:
747 WORD *len_ptr, len;
748 if (size < sizeof(WORD)) return FALSE;
749 len = *(WORD *)*buffer;
750 if (!get_buffer_space( buffer, (len + 1) * sizeof(WCHAR), size )) return FALSE;
751 len_ptr = *buffer;
752 len_ptr[0] = len_ptr[1] = len;
753 *lparam = (LPARAM)(len_ptr + 1);
754 return TRUE;
756 case EM_SETTABSTOPS:
757 case LB_SETTABSTOPS:
758 if (!*wparam) return TRUE;
759 minsize = *wparam * sizeof(UINT);
760 break;
761 case CB_ADDSTRING:
762 case CB_INSERTSTRING:
763 case CB_FINDSTRING:
764 case CB_FINDSTRINGEXACT:
765 case CB_SELECTSTRING:
766 case LB_ADDSTRING:
767 case LB_INSERTSTRING:
768 case LB_FINDSTRING:
769 case LB_FINDSTRINGEXACT:
770 case LB_SELECTSTRING:
771 if (!*buffer) return TRUE;
772 break;
773 case CB_GETLBTEXT:
775 size_t prev_size = size;
776 if (combobox_has_strings( hwnd ))
777 size = (send_message( hwnd, CB_GETLBTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
778 else
779 size = sizeof(ULONG_PTR);
780 if (!get_buffer_space( buffer, size, prev_size )) return FALSE;
781 break;
783 case LB_GETTEXT:
785 size_t prev_size = size;
786 if (listbox_has_strings( hwnd ))
787 size = (send_message( hwnd, LB_GETTEXTLEN, *wparam, 0 ) + 1) * sizeof(WCHAR);
788 else
789 size = sizeof(ULONG_PTR);
790 if (!get_buffer_space( buffer, size, prev_size )) return FALSE;
791 break;
793 case LB_GETSELITEMS:
794 if (!get_buffer_space( buffer, *wparam * sizeof(UINT), size )) return FALSE;
795 break;
796 case WM_NEXTMENU:
798 MDINEXTMENU mnm;
799 if (size < sizeof(ps->mnm)) return FALSE;
800 mnm.hmenuIn = wine_server_ptr_handle( ps->mnm.hmenuIn );
801 mnm.hmenuNext = wine_server_ptr_handle( ps->mnm.hmenuNext );
802 mnm.hwndNext = wine_server_ptr_handle( ps->mnm.hwndNext );
803 memcpy( *buffer, &mnm, sizeof(mnm) );
804 break;
806 case WM_SIZING:
807 case WM_MOVING:
808 minsize = sizeof(RECT);
809 if (!get_buffer_space( buffer, sizeof(RECT), size )) return FALSE;
810 break;
811 case WM_MDICREATE:
813 MDICREATESTRUCTW mcs;
814 WCHAR *str = (WCHAR *)(&ps->mcs + 1);
815 if (size < sizeof(ps->mcs)) return FALSE;
816 size -= sizeof(ps->mcs);
818 mcs.szClass = unpack_ptr( ps->mcs.szClass );
819 mcs.szTitle = unpack_ptr( ps->mcs.szTitle );
820 mcs.hOwner = unpack_ptr( ps->mcs.hOwner );
821 mcs.x = ps->mcs.x;
822 mcs.y = ps->mcs.y;
823 mcs.cx = ps->mcs.cx;
824 mcs.cy = ps->mcs.cy;
825 mcs.style = ps->mcs.style;
826 mcs.lParam = (LPARAM)unpack_ptr( ps->mcs.lParam );
827 if (ps->mcs.szClass >> 16)
829 mcs.szClass = str;
830 size -= (lstrlenW(str) + 1) * sizeof(WCHAR);
831 str += lstrlenW(str) + 1;
833 if (ps->mcs.szTitle >> 16)
835 mcs.szTitle = str;
837 memcpy( *buffer, &mcs, sizeof(mcs) );
838 break;
840 case WM_WINE_SETWINDOWPOS:
842 WINDOWPOS wp;
843 if (size < sizeof(ps->wp)) return FALSE;
844 wp.hwnd = wine_server_ptr_handle( ps->wp.hwnd );
845 wp.hwndInsertAfter = wine_server_ptr_handle( ps->wp.hwndInsertAfter );
846 wp.x = ps->wp.x;
847 wp.y = ps->wp.y;
848 wp.cx = ps->wp.cx;
849 wp.cy = ps->wp.cy;
850 wp.flags = ps->wp.flags;
851 memcpy( ps, &wp, sizeof(wp) );
852 break;
854 case WM_WINE_KEYBOARD_LL_HOOK:
855 case WM_WINE_MOUSE_LL_HOOK:
857 struct hook_extra_info h_extra;
858 minsize = sizeof(ps->hook) +
859 (message == WM_WINE_KEYBOARD_LL_HOOK ? sizeof(KBDLLHOOKSTRUCT)
860 : sizeof(MSLLHOOKSTRUCT));
861 if (size < minsize) return FALSE;
862 h_extra.handle = wine_server_ptr_handle( ps->hook.handle );
863 h_extra.lparam = (LPARAM)(&ps->hook + 1);
864 memcpy( &ps->hook, &h_extra, sizeof(h_extra) );
865 break;
867 case CB_GETCOMBOBOXINFO:
869 COMBOBOXINFO cbi = { sizeof(COMBOBOXINFO) };
870 memcpy( ps, &cbi, sizeof(cbi) );
871 break;
873 case WM_MDIGETACTIVE:
874 if (!*lparam) return TRUE;
875 if (!get_buffer_space( buffer, sizeof(BOOL), size )) return FALSE;
876 break;
877 case WM_DEVICECHANGE:
878 if (!(*wparam & 0x8000)) return TRUE;
879 minsize = sizeof(DEV_BROADCAST_HDR);
880 break;
881 case WM_NOTIFY:
882 /* WM_NOTIFY cannot be sent across processes (MSDN) */
883 return FALSE;
884 case WM_NCPAINT:
885 if (*wparam <= 1) return TRUE;
886 FIXME( "WM_NCPAINT hdc unpacking not supported\n" );
887 return FALSE;
888 case WM_PAINT:
889 if (!*wparam) return TRUE;
890 /* fall through */
892 /* these contain an HFONT */
893 case WM_SETFONT:
894 case WM_GETFONT:
895 /* these contain an HDC */
896 case WM_ERASEBKGND:
897 case WM_ICONERASEBKGND:
898 case WM_CTLCOLORMSGBOX:
899 case WM_CTLCOLOREDIT:
900 case WM_CTLCOLORLISTBOX:
901 case WM_CTLCOLORBTN:
902 case WM_CTLCOLORDLG:
903 case WM_CTLCOLORSCROLLBAR:
904 case WM_CTLCOLORSTATIC:
905 case WM_PRINT:
906 case WM_PRINTCLIENT:
907 /* these contain an HGLOBAL */
908 case WM_PAINTCLIPBOARD:
909 case WM_SIZECLIPBOARD:
910 /* these contain HICON */
911 case WM_GETICON:
912 case WM_SETICON:
913 case WM_QUERYDRAGICON:
914 case WM_QUERYPARKICON:
915 /* these contain pointers */
916 case WM_DROPOBJECT:
917 case WM_QUERYDROPOBJECT:
918 case WM_DRAGLOOP:
919 case WM_DRAGSELECT:
920 case WM_DRAGMOVE:
921 FIXME( "msg %x (%s) not supported yet\n", message, debugstr_msg_name( message, hwnd ));
922 return FALSE;
924 default:
925 return TRUE; /* message doesn't need any unpacking */
928 /* default exit for most messages: check minsize and store buffer in lparam */
929 if (size < minsize) return FALSE;
930 *lparam = (LPARAM)*buffer;
931 return TRUE;
934 /***********************************************************************
935 * pack_message
937 * Pack a message for sending to another process.
938 * Return the size of the data we expect in the message reply.
939 * Set data->count to -1 if there is an error.
941 static size_t pack_message( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
942 struct packed_message *data )
944 data->count = 0;
945 switch(message)
947 case WM_NCCREATE:
948 case WM_CREATE:
950 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
951 data->ps.cs.lpCreateParams = pack_ptr( cs->lpCreateParams );
952 data->ps.cs.hInstance = pack_ptr( cs->hInstance );
953 data->ps.cs.hMenu = wine_server_user_handle( cs->hMenu );
954 data->ps.cs.hwndParent = wine_server_user_handle( cs->hwndParent );
955 data->ps.cs.cy = cs->cy;
956 data->ps.cs.cx = cs->cx;
957 data->ps.cs.y = cs->y;
958 data->ps.cs.x = cs->x;
959 data->ps.cs.style = cs->style;
960 data->ps.cs.dwExStyle = cs->dwExStyle;
961 data->ps.cs.lpszName = pack_ptr( cs->lpszName );
962 data->ps.cs.lpszClass = pack_ptr( cs->lpszClass );
963 push_data( data, &data->ps.cs, sizeof(data->ps.cs) );
964 if (!IS_INTRESOURCE(cs->lpszName)) push_string( data, cs->lpszName );
965 if (!IS_INTRESOURCE(cs->lpszClass)) push_string( data, cs->lpszClass );
966 return sizeof(data->ps.cs);
968 case WM_GETTEXT:
969 case WM_ASKCBFORMATNAME:
970 return wparam * sizeof(WCHAR);
971 case WM_WININICHANGE:
972 if (lparam) push_string(data, (LPWSTR)lparam );
973 return 0;
974 case WM_SETTEXT:
975 case WM_DEVMODECHANGE:
976 case CB_DIR:
977 case LB_DIR:
978 case LB_ADDFILE:
979 case EM_REPLACESEL:
980 push_string( data, (LPWSTR)lparam );
981 return 0;
982 case WM_GETMINMAXINFO:
983 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
984 return sizeof(MINMAXINFO);
985 case WM_DRAWITEM:
987 DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)lparam;
988 data->ps.dis.CtlType = dis->CtlType;
989 data->ps.dis.CtlID = dis->CtlID;
990 data->ps.dis.itemID = dis->itemID;
991 data->ps.dis.itemAction = dis->itemAction;
992 data->ps.dis.itemState = dis->itemState;
993 data->ps.dis.hwndItem = wine_server_user_handle( dis->hwndItem );
994 data->ps.dis.hDC = wine_server_user_handle( dis->hDC ); /* FIXME */
995 data->ps.dis.rcItem = dis->rcItem;
996 data->ps.dis.itemData = dis->itemData;
997 push_data( data, &data->ps.dis, sizeof(data->ps.dis) );
998 return 0;
1000 case WM_MEASUREITEM:
1002 MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lparam;
1003 data->ps.mis.CtlType = mis->CtlType;
1004 data->ps.mis.CtlID = mis->CtlID;
1005 data->ps.mis.itemID = mis->itemID;
1006 data->ps.mis.itemWidth = mis->itemWidth;
1007 data->ps.mis.itemHeight = mis->itemHeight;
1008 data->ps.mis.itemData = mis->itemData;
1009 push_data( data, &data->ps.mis, sizeof(data->ps.mis) );
1010 return sizeof(data->ps.mis);
1012 case WM_DELETEITEM:
1014 DELETEITEMSTRUCT *dls = (DELETEITEMSTRUCT *)lparam;
1015 data->ps.dls.CtlType = dls->CtlType;
1016 data->ps.dls.CtlID = dls->CtlID;
1017 data->ps.dls.itemID = dls->itemID;
1018 data->ps.dls.hwndItem = wine_server_user_handle( dls->hwndItem );
1019 data->ps.dls.itemData = dls->itemData;
1020 push_data( data, &data->ps.dls, sizeof(data->ps.dls) );
1021 return 0;
1023 case WM_COMPAREITEM:
1025 COMPAREITEMSTRUCT *cis = (COMPAREITEMSTRUCT *)lparam;
1026 data->ps.cis.CtlType = cis->CtlType;
1027 data->ps.cis.CtlID = cis->CtlID;
1028 data->ps.cis.hwndItem = wine_server_user_handle( cis->hwndItem );
1029 data->ps.cis.itemID1 = cis->itemID1;
1030 data->ps.cis.itemData1 = cis->itemData1;
1031 data->ps.cis.itemID2 = cis->itemID2;
1032 data->ps.cis.itemData2 = cis->itemData2;
1033 data->ps.cis.dwLocaleId = cis->dwLocaleId;
1034 push_data( data, &data->ps.cis, sizeof(data->ps.cis) );
1035 return 0;
1037 case WM_WINE_SETWINDOWPOS:
1038 case WM_WINDOWPOSCHANGING:
1039 case WM_WINDOWPOSCHANGED:
1041 WINDOWPOS *wp = (WINDOWPOS *)lparam;
1042 data->ps.wp.hwnd = wine_server_user_handle( wp->hwnd );
1043 data->ps.wp.hwndInsertAfter = wine_server_user_handle( wp->hwndInsertAfter );
1044 data->ps.wp.x = wp->x;
1045 data->ps.wp.y = wp->y;
1046 data->ps.wp.cx = wp->cx;
1047 data->ps.wp.cy = wp->cy;
1048 data->ps.wp.flags = wp->flags;
1049 push_data( data, &data->ps.wp, sizeof(data->ps.wp) );
1050 return sizeof(data->ps.wp);
1052 case WM_COPYDATA:
1054 COPYDATASTRUCT *cds = (COPYDATASTRUCT *)lparam;
1055 data->ps.cds.cbData = cds->cbData;
1056 data->ps.cds.dwData = cds->dwData;
1057 data->ps.cds.lpData = pack_ptr( cds->lpData );
1058 push_data( data, &data->ps.cds, sizeof(data->ps.cds) );
1059 if (cds->lpData) push_data( data, cds->lpData, cds->cbData );
1060 return 0;
1062 case WM_NOTIFY:
1063 /* WM_NOTIFY cannot be sent across processes (MSDN) */
1064 data->count = -1;
1065 return 0;
1066 case WM_HELP:
1068 HELPINFO *hi = (HELPINFO *)lparam;
1069 data->ps.hi.iContextType = hi->iContextType;
1070 data->ps.hi.iCtrlId = hi->iCtrlId;
1071 data->ps.hi.hItemHandle = wine_server_user_handle( hi->hItemHandle );
1072 data->ps.hi.dwContextId = hi->dwContextId;
1073 data->ps.hi.MousePos = hi->MousePos;
1074 push_data( data, &data->ps.hi, sizeof(data->ps.hi) );
1075 return 0;
1077 case WM_STYLECHANGING:
1078 case WM_STYLECHANGED:
1079 push_data( data, (STYLESTRUCT *)lparam, sizeof(STYLESTRUCT) );
1080 return 0;
1081 case WM_NCCALCSIZE:
1082 if (!wparam)
1084 push_data( data, (RECT *)lparam, sizeof(RECT) );
1085 return sizeof(RECT);
1087 else
1089 NCCALCSIZE_PARAMS *ncp = (NCCALCSIZE_PARAMS *)lparam;
1090 data->ps.ncp.rgrc[0] = ncp->rgrc[0];
1091 data->ps.ncp.rgrc[1] = ncp->rgrc[1];
1092 data->ps.ncp.rgrc[2] = ncp->rgrc[2];
1093 data->ps.ncp.hwnd = wine_server_user_handle( ncp->lppos->hwnd );
1094 data->ps.ncp.hwndInsertAfter = wine_server_user_handle( ncp->lppos->hwndInsertAfter );
1095 data->ps.ncp.x = ncp->lppos->x;
1096 data->ps.ncp.y = ncp->lppos->y;
1097 data->ps.ncp.cx = ncp->lppos->cx;
1098 data->ps.ncp.cy = ncp->lppos->cy;
1099 data->ps.ncp.flags = ncp->lppos->flags;
1100 push_data( data, &data->ps.ncp, sizeof(data->ps.ncp) );
1101 return sizeof(data->ps.ncp);
1103 case WM_GETDLGCODE:
1104 if (lparam)
1106 MSG *msg = (MSG *)lparam;
1107 data->ps.msg.hwnd = wine_server_user_handle( msg->hwnd );
1108 data->ps.msg.message = msg->message;
1109 data->ps.msg.wParam = msg->wParam;
1110 data->ps.msg.lParam = msg->lParam;
1111 data->ps.msg.time = msg->time;
1112 data->ps.msg.pt = msg->pt;
1113 push_data( data, &data->ps.msg, sizeof(data->ps.msg) );
1114 return sizeof(data->ps.msg);
1116 return 0;
1117 case SBM_SETSCROLLINFO:
1118 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
1119 return 0;
1120 case SBM_GETSCROLLINFO:
1121 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
1122 return sizeof(SCROLLINFO);
1123 case SBM_GETSCROLLBARINFO:
1125 const SCROLLBARINFO *info = (const SCROLLBARINFO *)lparam;
1126 size_t size = min( info->cbSize, sizeof(SCROLLBARINFO) );
1127 push_data( data, info, size );
1128 return size;
1130 case EM_GETSEL:
1131 case SBM_GETRANGE:
1132 case CB_GETEDITSEL:
1134 size_t size = 0;
1135 if (wparam) size += sizeof(DWORD);
1136 if (lparam) size += sizeof(DWORD);
1137 return size;
1139 case EM_GETRECT:
1140 case LB_GETITEMRECT:
1141 case CB_GETDROPPEDCONTROLRECT:
1142 return sizeof(RECT);
1143 case EM_SETRECT:
1144 case EM_SETRECTNP:
1145 push_data( data, (RECT *)lparam, sizeof(RECT) );
1146 return 0;
1147 case EM_GETLINE:
1149 WORD *pw = (WORD *)lparam;
1150 push_data( data, pw, sizeof(*pw) );
1151 return *pw * sizeof(WCHAR);
1153 case EM_SETTABSTOPS:
1154 case LB_SETTABSTOPS:
1155 if (wparam) push_data( data, (UINT *)lparam, sizeof(UINT) * wparam );
1156 return 0;
1157 case CB_ADDSTRING:
1158 case CB_INSERTSTRING:
1159 case CB_FINDSTRING:
1160 case CB_FINDSTRINGEXACT:
1161 case CB_SELECTSTRING:
1162 if (combobox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
1163 return 0;
1164 case CB_GETLBTEXT:
1165 if (!combobox_has_strings( hwnd )) return sizeof(ULONG_PTR);
1166 return (send_message( hwnd, CB_GETLBTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
1167 case CB_GETCOMBOBOXINFO:
1168 return sizeof(data->ps.cbi);
1169 case LB_ADDSTRING:
1170 case LB_INSERTSTRING:
1171 case LB_FINDSTRING:
1172 case LB_FINDSTRINGEXACT:
1173 case LB_SELECTSTRING:
1174 if (listbox_has_strings( hwnd )) push_string( data, (LPWSTR)lparam );
1175 return 0;
1176 case LB_GETTEXT:
1177 if (!listbox_has_strings( hwnd )) return sizeof(ULONG_PTR);
1178 return (send_message( hwnd, LB_GETTEXTLEN, wparam, 0 ) + 1) * sizeof(WCHAR);
1179 case LB_GETSELITEMS:
1180 return wparam * sizeof(UINT);
1181 case WM_NEXTMENU:
1183 MDINEXTMENU *mnm = (MDINEXTMENU *)lparam;
1184 data->ps.mnm.hmenuIn = wine_server_user_handle( mnm->hmenuIn );
1185 data->ps.mnm.hmenuNext = wine_server_user_handle( mnm->hmenuNext );
1186 data->ps.mnm.hwndNext = wine_server_user_handle( mnm->hwndNext );
1187 push_data( data, &data->ps.mnm, sizeof(data->ps.mnm) );
1188 return sizeof(data->ps.mnm);
1190 case WM_SIZING:
1191 case WM_MOVING:
1192 push_data( data, (RECT *)lparam, sizeof(RECT) );
1193 return sizeof(RECT);
1194 case WM_MDICREATE:
1196 MDICREATESTRUCTW *mcs = (MDICREATESTRUCTW *)lparam;
1197 data->ps.mcs.szClass = pack_ptr( mcs->szClass );
1198 data->ps.mcs.szTitle = pack_ptr( mcs->szTitle );
1199 data->ps.mcs.hOwner = pack_ptr( mcs->hOwner );
1200 data->ps.mcs.x = mcs->x;
1201 data->ps.mcs.y = mcs->y;
1202 data->ps.mcs.cx = mcs->cx;
1203 data->ps.mcs.cy = mcs->cy;
1204 data->ps.mcs.style = mcs->style;
1205 data->ps.mcs.lParam = mcs->lParam;
1206 push_data( data, &data->ps.mcs, sizeof(data->ps.mcs) );
1207 if (!IS_INTRESOURCE(mcs->szClass)) push_string( data, mcs->szClass );
1208 if (!IS_INTRESOURCE(mcs->szTitle)) push_string( data, mcs->szTitle );
1209 return sizeof(data->ps.mcs);
1211 case WM_MDIGETACTIVE:
1212 if (lparam) return sizeof(BOOL);
1213 return 0;
1214 case WM_DEVICECHANGE:
1216 DEV_BROADCAST_HDR *header = (DEV_BROADCAST_HDR *)lparam;
1217 if ((wparam & 0x8000) && header) push_data( data, header, header->dbch_size );
1218 return 0;
1220 case WM_WINE_KEYBOARD_LL_HOOK:
1222 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
1223 data->ps.hook.handle = wine_server_user_handle( h_extra->handle );
1224 push_data( data, &data->ps.hook, sizeof(data->ps.hook) );
1225 push_data( data, (LPVOID)h_extra->lparam, sizeof(KBDLLHOOKSTRUCT) );
1226 return 0;
1228 case WM_WINE_MOUSE_LL_HOOK:
1230 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
1231 data->ps.hook.handle = wine_server_user_handle( h_extra->handle );
1232 push_data( data, &data->ps.hook, sizeof(data->ps.hook) );
1233 push_data( data, (LPVOID)h_extra->lparam, sizeof(MSLLHOOKSTRUCT) );
1234 return 0;
1236 case WM_NCPAINT:
1237 if (wparam <= 1) return 0;
1238 FIXME( "WM_NCPAINT hdc packing not supported yet\n" );
1239 data->count = -1;
1240 return 0;
1241 case WM_PAINT:
1242 if (!wparam) return 0;
1243 /* fall through */
1245 /* these contain an HFONT */
1246 case WM_SETFONT:
1247 case WM_GETFONT:
1248 /* these contain an HDC */
1249 case WM_ERASEBKGND:
1250 case WM_ICONERASEBKGND:
1251 case WM_CTLCOLORMSGBOX:
1252 case WM_CTLCOLOREDIT:
1253 case WM_CTLCOLORLISTBOX:
1254 case WM_CTLCOLORBTN:
1255 case WM_CTLCOLORDLG:
1256 case WM_CTLCOLORSCROLLBAR:
1257 case WM_CTLCOLORSTATIC:
1258 case WM_PRINT:
1259 case WM_PRINTCLIENT:
1260 /* these contain an HGLOBAL */
1261 case WM_PAINTCLIPBOARD:
1262 case WM_SIZECLIPBOARD:
1263 /* these contain HICON */
1264 case WM_GETICON:
1265 case WM_SETICON:
1266 case WM_QUERYDRAGICON:
1267 case WM_QUERYPARKICON:
1268 /* these contain pointers */
1269 case WM_DROPOBJECT:
1270 case WM_QUERYDROPOBJECT:
1271 case WM_DRAGLOOP:
1272 case WM_DRAGSELECT:
1273 case WM_DRAGMOVE:
1274 FIXME( "msg %x (%s) not supported yet\n", message, debugstr_msg_name(message, hwnd) );
1275 data->count = -1;
1276 return 0;
1278 return 0;
1281 /***********************************************************************
1282 * pack_reply
1284 * Pack a reply to a message for sending to another process.
1286 static void pack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1287 LRESULT res, struct packed_message *data )
1289 data->count = 0;
1290 switch(message)
1292 case WM_NCCREATE:
1293 case WM_CREATE:
1295 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
1296 data->ps.cs.lpCreateParams = (ULONG_PTR)cs->lpCreateParams;
1297 data->ps.cs.hInstance = (ULONG_PTR)cs->hInstance;
1298 data->ps.cs.hMenu = wine_server_user_handle( cs->hMenu );
1299 data->ps.cs.hwndParent = wine_server_user_handle( cs->hwndParent );
1300 data->ps.cs.cy = cs->cy;
1301 data->ps.cs.cx = cs->cx;
1302 data->ps.cs.y = cs->y;
1303 data->ps.cs.x = cs->x;
1304 data->ps.cs.style = cs->style;
1305 data->ps.cs.dwExStyle = cs->dwExStyle;
1306 data->ps.cs.lpszName = (ULONG_PTR)cs->lpszName;
1307 data->ps.cs.lpszClass = (ULONG_PTR)cs->lpszClass;
1308 push_data( data, &data->ps.cs, sizeof(data->ps.cs) );
1309 break;
1311 case WM_GETTEXT:
1312 case CB_GETLBTEXT:
1313 case LB_GETTEXT:
1314 push_data( data, (WCHAR *)lparam, (res + 1) * sizeof(WCHAR) );
1315 break;
1316 case CB_GETCOMBOBOXINFO:
1318 COMBOBOXINFO *cbi = (COMBOBOXINFO *)lparam;
1319 data->ps.cbi.rcItem = cbi->rcItem;
1320 data->ps.cbi.rcButton = cbi->rcButton;
1321 data->ps.cbi.stateButton = cbi->stateButton;
1322 data->ps.cbi.hwndCombo = wine_server_user_handle( cbi->hwndCombo );
1323 data->ps.cbi.hwndItem = wine_server_user_handle( cbi->hwndItem );
1324 data->ps.cbi.hwndList = wine_server_user_handle( cbi->hwndList );
1325 push_data( data, &data->ps.cbi, sizeof(data->ps.cbi) );
1326 break;
1328 case WM_GETMINMAXINFO:
1329 push_data( data, (MINMAXINFO *)lparam, sizeof(MINMAXINFO) );
1330 break;
1331 case WM_MEASUREITEM:
1333 MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lparam;
1334 data->ps.mis.CtlType = mis->CtlType;
1335 data->ps.mis.CtlID = mis->CtlID;
1336 data->ps.mis.itemID = mis->itemID;
1337 data->ps.mis.itemWidth = mis->itemWidth;
1338 data->ps.mis.itemHeight = mis->itemHeight;
1339 data->ps.mis.itemData = mis->itemData;
1340 push_data( data, &data->ps.mis, sizeof(data->ps.mis) );
1341 break;
1343 case WM_WINDOWPOSCHANGING:
1344 case WM_WINDOWPOSCHANGED:
1346 WINDOWPOS *wp = (WINDOWPOS *)lparam;
1347 data->ps.wp.hwnd = wine_server_user_handle( wp->hwnd );
1348 data->ps.wp.hwndInsertAfter = wine_server_user_handle( wp->hwndInsertAfter );
1349 data->ps.wp.x = wp->x;
1350 data->ps.wp.y = wp->y;
1351 data->ps.wp.cx = wp->cx;
1352 data->ps.wp.cy = wp->cy;
1353 data->ps.wp.flags = wp->flags;
1354 push_data( data, &data->ps.wp, sizeof(data->ps.wp) );
1355 break;
1357 case SBM_GETSCROLLINFO:
1358 push_data( data, (SCROLLINFO *)lparam, sizeof(SCROLLINFO) );
1359 break;
1360 case EM_GETRECT:
1361 case LB_GETITEMRECT:
1362 case CB_GETDROPPEDCONTROLRECT:
1363 case WM_SIZING:
1364 case WM_MOVING:
1365 push_data( data, (RECT *)lparam, sizeof(RECT) );
1366 break;
1367 case EM_GETLINE:
1369 WORD *ptr = (WORD *)lparam;
1370 push_data( data, ptr, ptr[-1] * sizeof(WCHAR) );
1371 break;
1373 case LB_GETSELITEMS:
1374 push_data( data, (UINT *)lparam, wparam * sizeof(UINT) );
1375 break;
1376 case WM_MDIGETACTIVE:
1377 if (lparam) push_data( data, (BOOL *)lparam, sizeof(BOOL) );
1378 break;
1379 case WM_NCCALCSIZE:
1380 if (!wparam)
1381 push_data( data, (RECT *)lparam, sizeof(RECT) );
1382 else
1384 NCCALCSIZE_PARAMS *ncp = (NCCALCSIZE_PARAMS *)lparam;
1385 data->ps.ncp.rgrc[0] = ncp->rgrc[0];
1386 data->ps.ncp.rgrc[1] = ncp->rgrc[1];
1387 data->ps.ncp.rgrc[2] = ncp->rgrc[2];
1388 data->ps.ncp.hwnd = wine_server_user_handle( ncp->lppos->hwnd );
1389 data->ps.ncp.hwndInsertAfter = wine_server_user_handle( ncp->lppos->hwndInsertAfter );
1390 data->ps.ncp.x = ncp->lppos->x;
1391 data->ps.ncp.y = ncp->lppos->y;
1392 data->ps.ncp.cx = ncp->lppos->cx;
1393 data->ps.ncp.cy = ncp->lppos->cy;
1394 data->ps.ncp.flags = ncp->lppos->flags;
1395 push_data( data, &data->ps.ncp, sizeof(data->ps.ncp) );
1397 break;
1398 case EM_GETSEL:
1399 case SBM_GETRANGE:
1400 case CB_GETEDITSEL:
1401 if (wparam) push_data( data, (DWORD *)wparam, sizeof(DWORD) );
1402 if (lparam) push_data( data, (DWORD *)lparam, sizeof(DWORD) );
1403 break;
1404 case WM_NEXTMENU:
1406 MDINEXTMENU *mnm = (MDINEXTMENU *)lparam;
1407 data->ps.mnm.hmenuIn = wine_server_user_handle( mnm->hmenuIn );
1408 data->ps.mnm.hmenuNext = wine_server_user_handle( mnm->hmenuNext );
1409 data->ps.mnm.hwndNext = wine_server_user_handle( mnm->hwndNext );
1410 push_data( data, &data->ps.mnm, sizeof(data->ps.mnm) );
1411 break;
1413 case WM_MDICREATE:
1415 MDICREATESTRUCTW *mcs = (MDICREATESTRUCTW *)lparam;
1416 data->ps.mcs.szClass = pack_ptr( mcs->szClass );
1417 data->ps.mcs.szTitle = pack_ptr( mcs->szTitle );
1418 data->ps.mcs.hOwner = pack_ptr( mcs->hOwner );
1419 data->ps.mcs.x = mcs->x;
1420 data->ps.mcs.y = mcs->y;
1421 data->ps.mcs.cx = mcs->cx;
1422 data->ps.mcs.cy = mcs->cy;
1423 data->ps.mcs.style = mcs->style;
1424 data->ps.mcs.lParam = mcs->lParam;
1425 push_data( data, &data->ps.mcs, sizeof(data->ps.mcs) );
1426 break;
1428 case WM_ASKCBFORMATNAME:
1429 push_data( data, (WCHAR *)lparam, (lstrlenW((WCHAR *)lparam) + 1) * sizeof(WCHAR) );
1430 break;
1434 /***********************************************************************
1435 * unpack_reply
1437 * Unpack a message reply received from another process.
1439 static void unpack_reply( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1440 void *buffer, size_t size )
1442 union packed_structs *ps = buffer;
1444 switch(message)
1446 case WM_NCCREATE:
1447 case WM_CREATE:
1448 if (size >= sizeof(ps->cs))
1450 CREATESTRUCTW *cs = (CREATESTRUCTW *)lparam;
1451 cs->lpCreateParams = unpack_ptr( ps->cs.lpCreateParams );
1452 cs->hInstance = unpack_ptr( ps->cs.hInstance );
1453 cs->hMenu = wine_server_ptr_handle( ps->cs.hMenu );
1454 cs->hwndParent = wine_server_ptr_handle( ps->cs.hwndParent );
1455 cs->cy = ps->cs.cy;
1456 cs->cx = ps->cs.cx;
1457 cs->y = ps->cs.y;
1458 cs->x = ps->cs.x;
1459 cs->style = ps->cs.style;
1460 cs->dwExStyle = ps->cs.dwExStyle;
1461 /* don't allow changing name and class pointers */
1463 break;
1464 case WM_GETTEXT:
1465 case WM_ASKCBFORMATNAME:
1466 memcpy( (WCHAR *)lparam, buffer, min( wparam*sizeof(WCHAR), size ));
1467 break;
1468 case WM_GETMINMAXINFO:
1469 memcpy( (MINMAXINFO *)lparam, buffer, min( sizeof(MINMAXINFO), size ));
1470 break;
1471 case WM_MEASUREITEM:
1472 if (size >= sizeof(ps->mis))
1474 MEASUREITEMSTRUCT *mis = (MEASUREITEMSTRUCT *)lparam;
1475 mis->CtlType = ps->mis.CtlType;
1476 mis->CtlID = ps->mis.CtlID;
1477 mis->itemID = ps->mis.itemID;
1478 mis->itemWidth = ps->mis.itemWidth;
1479 mis->itemHeight = ps->mis.itemHeight;
1480 mis->itemData = (ULONG_PTR)unpack_ptr( ps->mis.itemData );
1482 break;
1483 case WM_WINDOWPOSCHANGING:
1484 case WM_WINDOWPOSCHANGED:
1485 if (size >= sizeof(ps->wp))
1487 WINDOWPOS *wp = (WINDOWPOS *)lparam;
1488 wp->hwnd = wine_server_ptr_handle( ps->wp.hwnd );
1489 wp->hwndInsertAfter = wine_server_ptr_handle( ps->wp.hwndInsertAfter );
1490 wp->x = ps->wp.x;
1491 wp->y = ps->wp.y;
1492 wp->cx = ps->wp.cx;
1493 wp->cy = ps->wp.cy;
1494 wp->flags = ps->wp.flags;
1496 break;
1497 case SBM_GETSCROLLINFO:
1498 memcpy( (SCROLLINFO *)lparam, buffer, min( sizeof(SCROLLINFO), size ));
1499 break;
1500 case SBM_GETSCROLLBARINFO:
1501 memcpy( (SCROLLBARINFO *)lparam, buffer, min( sizeof(SCROLLBARINFO), size ));
1502 break;
1503 case EM_GETRECT:
1504 case CB_GETDROPPEDCONTROLRECT:
1505 case LB_GETITEMRECT:
1506 case WM_SIZING:
1507 case WM_MOVING:
1508 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1509 break;
1510 case EM_GETLINE:
1511 size = min( size, (size_t)*(WORD *)lparam );
1512 memcpy( (WCHAR *)lparam, buffer, size );
1513 break;
1514 case LB_GETSELITEMS:
1515 memcpy( (UINT *)lparam, buffer, min( wparam*sizeof(UINT), size ));
1516 break;
1517 case LB_GETTEXT:
1518 case CB_GETLBTEXT:
1519 memcpy( (WCHAR *)lparam, buffer, size );
1520 break;
1521 case CB_GETCOMBOBOXINFO:
1522 if (lparam && size >= sizeof(ps->cbi) &&
1523 ((COMBOBOXINFO*)lparam)->cbSize == sizeof(COMBOBOXINFO))
1525 COMBOBOXINFO *cbi = (COMBOBOXINFO *)lparam;
1526 cbi->rcItem = ps->cbi.rcItem;
1527 cbi->rcButton = ps->cbi.rcButton;
1528 cbi->stateButton = ps->cbi.stateButton;
1529 cbi->hwndCombo = wine_server_ptr_handle( ps->cbi.hwndCombo );
1530 cbi->hwndItem = wine_server_ptr_handle( ps->cbi.hwndItem );
1531 cbi->hwndList = wine_server_ptr_handle( ps->cbi.hwndList );
1533 break;
1534 case WM_NEXTMENU:
1535 if (size >= sizeof(ps->mnm))
1537 MDINEXTMENU *mnm = (MDINEXTMENU *)lparam;
1538 mnm->hmenuIn = wine_server_ptr_handle( ps->mnm.hmenuIn );
1539 mnm->hmenuNext = wine_server_ptr_handle( ps->mnm.hmenuNext );
1540 mnm->hwndNext = wine_server_ptr_handle( ps->mnm.hwndNext );
1542 break;
1543 case WM_MDIGETACTIVE:
1544 if (lparam) memcpy( (BOOL *)lparam, buffer, min( sizeof(BOOL), size ));
1545 break;
1546 case WM_NCCALCSIZE:
1547 if (!wparam)
1548 memcpy( (RECT *)lparam, buffer, min( sizeof(RECT), size ));
1549 else if (size >= sizeof(ps->ncp))
1551 NCCALCSIZE_PARAMS *ncp = (NCCALCSIZE_PARAMS *)lparam;
1552 ncp->rgrc[0] = ps->ncp.rgrc[0];
1553 ncp->rgrc[1] = ps->ncp.rgrc[1];
1554 ncp->rgrc[2] = ps->ncp.rgrc[2];
1555 ncp->lppos->hwnd = wine_server_ptr_handle( ps->ncp.hwnd );
1556 ncp->lppos->hwndInsertAfter = wine_server_ptr_handle( ps->ncp.hwndInsertAfter );
1557 ncp->lppos->x = ps->ncp.x;
1558 ncp->lppos->y = ps->ncp.y;
1559 ncp->lppos->cx = ps->ncp.cx;
1560 ncp->lppos->cy = ps->ncp.cy;
1561 ncp->lppos->flags = ps->ncp.flags;
1563 break;
1564 case EM_GETSEL:
1565 case SBM_GETRANGE:
1566 case CB_GETEDITSEL:
1567 if (wparam)
1569 memcpy( (DWORD *)wparam, buffer, min( sizeof(DWORD), size ));
1570 if (size <= sizeof(DWORD)) break;
1571 size -= sizeof(DWORD);
1572 buffer = (DWORD *)buffer + 1;
1574 if (lparam) memcpy( (DWORD *)lparam, buffer, min( sizeof(DWORD), size ));
1575 break;
1576 case WM_MDICREATE:
1577 if (size >= sizeof(ps->mcs))
1579 MDICREATESTRUCTW *mcs = (MDICREATESTRUCTW *)lparam;
1580 mcs->hOwner = unpack_ptr( ps->mcs.hOwner );
1581 mcs->x = ps->mcs.x;
1582 mcs->y = ps->mcs.y;
1583 mcs->cx = ps->mcs.cx;
1584 mcs->cy = ps->mcs.cy;
1585 mcs->style = ps->mcs.style;
1586 mcs->lParam = (LPARAM)unpack_ptr( ps->mcs.lParam );
1587 /* don't allow changing class and title pointers */
1589 break;
1590 default:
1591 ERR( "should not happen: unexpected message %x\n", message );
1592 break;
1596 static size_t char_size( BOOL ansi )
1598 return ansi ? sizeof(char) : sizeof(WCHAR);
1601 static size_t string_size( const void *str, BOOL ansi )
1603 return ansi ? strlen( str ) + 1 : (wcslen( str ) + 1) * sizeof(WCHAR);
1606 static size_t copy_string( void *ptr, const void *str, BOOL ansi )
1608 size_t size = string_size( str, ansi );
1609 memcpy( ptr, str, size );
1610 return size;
1613 /***********************************************************************
1614 * user_message_size
1616 * Calculate size of packed message buffer.
1618 size_t user_message_size( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam,
1619 BOOL other_process, BOOL ansi )
1621 const void *lparam_ptr = (const void *)lparam;
1622 size_t size = 0;
1624 switch (message)
1626 case WM_NCCREATE:
1627 case WM_CREATE:
1629 const CREATESTRUCTW *cs = lparam_ptr;
1630 size = sizeof(*cs);
1631 if (!IS_INTRESOURCE(cs->lpszName)) size += string_size( cs->lpszName, ansi );
1632 if (!IS_INTRESOURCE(cs->lpszClass)) size += string_size( cs->lpszClass, ansi );
1633 break;
1635 case WM_NCCALCSIZE:
1636 size = wparam ? sizeof(NCCALCSIZE_PARAMS) + sizeof(WINDOWPOS) : sizeof(RECT);
1637 break;
1638 case WM_GETTEXT:
1639 case WM_ASKCBFORMATNAME:
1640 size = wparam * char_size( ansi );
1641 break;
1642 case WM_WININICHANGE:
1643 case WM_SETTEXT:
1644 case WM_DEVMODECHANGE:
1645 case CB_DIR:
1646 case LB_DIR:
1647 case LB_ADDFILE:
1648 case EM_REPLACESEL:
1649 case CB_ADDSTRING:
1650 case CB_INSERTSTRING:
1651 case CB_FINDSTRING:
1652 case CB_FINDSTRINGEXACT:
1653 case CB_SELECTSTRING:
1654 case LB_ADDSTRING:
1655 case LB_INSERTSTRING:
1656 case LB_FINDSTRING:
1657 case LB_FINDSTRINGEXACT:
1658 case LB_SELECTSTRING:
1659 if (other_process && lparam) size = string_size( lparam_ptr, ansi );
1660 break;
1661 case WM_GETMINMAXINFO:
1662 size = sizeof(MINMAXINFO);
1663 break;
1664 case WM_DRAWITEM:
1665 size = sizeof(DRAWITEMSTRUCT);
1666 break;
1667 case WM_MEASUREITEM:
1668 size = sizeof(MEASUREITEMSTRUCT);
1669 break;
1670 case WM_DELETEITEM:
1671 size = sizeof(DELETEITEMSTRUCT);
1672 break;
1673 case WM_COMPAREITEM:
1674 size = sizeof(COMPAREITEMSTRUCT);
1675 break;
1676 case WM_WINDOWPOSCHANGING:
1677 case WM_WINDOWPOSCHANGED:
1678 size = sizeof(WINDOWPOS);
1679 break;
1680 case WM_COPYDATA:
1682 const COPYDATASTRUCT *cds = lparam_ptr;
1683 size = sizeof(*cds) + cds->cbData;
1684 break;
1686 case WM_HELP:
1687 size = sizeof(HELPINFO);
1688 break;
1689 case WM_STYLECHANGING:
1690 case WM_STYLECHANGED:
1691 size = sizeof(STYLESTRUCT);
1692 break;
1693 case WM_GETDLGCODE:
1694 size = sizeof(MSG);
1695 break;
1696 case SBM_SETSCROLLINFO:
1697 case SBM_GETSCROLLINFO:
1698 size = sizeof(SCROLLINFO);
1699 break;
1700 case SBM_GETSCROLLBARINFO:
1701 size = sizeof(SCROLLBARINFO);
1702 break;
1703 case EM_GETSEL:
1704 case SBM_GETRANGE:
1705 case CB_GETEDITSEL:
1706 size = 2 * sizeof(DWORD);
1707 break;
1708 case WM_SIZING:
1709 case WM_MOVING:
1710 case EM_GETRECT:
1711 case LB_GETITEMRECT:
1712 case CB_GETDROPPEDCONTROLRECT:
1713 case EM_SETRECT:
1714 case EM_SETRECTNP:
1715 size = sizeof(RECT);
1716 break;
1717 case EM_GETLINE:
1718 size = max( *(WORD *)lparam * char_size( ansi ), sizeof(WORD) );
1719 break;
1720 case EM_SETTABSTOPS:
1721 case LB_SETTABSTOPS:
1722 size = wparam * sizeof(UINT);
1723 break;
1724 case CB_GETLBTEXT:
1725 size = send_message_timeout( hwnd, CB_GETLBTEXTLEN, wparam, 0, SMTO_NORMAL, 0, ansi );
1726 size = (size + 1) * char_size( ansi );
1727 break;
1728 case LB_GETTEXT:
1729 size = send_message_timeout( hwnd, LB_GETTEXTLEN, wparam, 0, SMTO_NORMAL, 0, ansi );
1730 size = (size + 1) * char_size( ansi );
1731 break;
1732 case LB_GETSELITEMS:
1733 size = wparam * sizeof(UINT);
1734 break;
1735 case WM_NEXTMENU:
1736 size = sizeof(MDINEXTMENU);
1737 break;
1738 case WM_MDICREATE:
1740 const MDICREATESTRUCTW *mcs = lparam_ptr;
1741 size = sizeof(*mcs);
1742 if (!IS_INTRESOURCE(mcs->szClass)) size += string_size( mcs->szClass, ansi );
1743 if (!IS_INTRESOURCE(mcs->szTitle)) size += string_size( mcs->szTitle, ansi );
1744 break;
1746 case CB_GETCOMBOBOXINFO:
1747 size = sizeof(COMBOBOXINFO);
1748 break;
1749 case WM_MDIGETACTIVE:
1750 if (lparam) size = sizeof(BOOL);
1751 break;
1752 case WM_DEVICECHANGE:
1753 if ((wparam & 0x8000) && lparam)
1755 const DEV_BROADCAST_HDR *header = lparam_ptr;
1756 size = header->dbch_size;
1758 break;
1761 return size;
1764 /***********************************************************************
1765 * pack_user_message
1767 * Copy message to a buffer for passing to client.
1769 void pack_user_message( void *buffer, size_t size, UINT message,
1770 WPARAM wparam, LPARAM lparam, BOOL ansi )
1772 const void *lparam_ptr = (const void *)lparam;
1773 void const *inline_ptr = (void *)0xffffffff;
1775 if (!size) return;
1777 switch (message)
1779 case WM_NCCREATE:
1780 case WM_CREATE:
1782 CREATESTRUCTW *cs = buffer;
1783 char *ptr = (char *)(cs + 1);
1785 memcpy( cs, lparam_ptr, sizeof(*cs) );
1786 if (!IS_INTRESOURCE(cs->lpszName))
1788 ptr += copy_string( ptr, cs->lpszName, ansi );
1789 cs->lpszName = inline_ptr;
1791 if (!IS_INTRESOURCE(cs->lpszClass))
1793 copy_string( ptr, cs->lpszClass, ansi );
1794 cs->lpszClass = inline_ptr;
1796 return;
1798 case WM_NCCALCSIZE:
1799 if (wparam)
1801 const NCCALCSIZE_PARAMS *ncp = lparam_ptr;
1802 memcpy( (char *)buffer + sizeof(*ncp), ncp->lppos, sizeof(*ncp->lppos) );
1803 size = sizeof(*ncp);
1805 break;
1806 case WM_GETTEXT:
1807 case WM_ASKCBFORMATNAME:
1808 if (wparam) memset( buffer, 0, char_size( ansi ));
1809 return;
1810 case WM_COPYDATA:
1812 const COPYDATASTRUCT *cds = lparam_ptr;
1813 if (cds->lpData && cds->cbData)
1814 memcpy( (char *)buffer + sizeof(*cds), cds->lpData, cds->cbData );
1815 size = sizeof(*cds);
1816 break;
1818 case EM_GETSEL:
1819 case SBM_GETRANGE:
1820 case CB_GETEDITSEL:
1821 case EM_GETRECT:
1822 case CB_GETDROPPEDCONTROLRECT:
1823 return;
1824 case EM_GETLINE:
1825 size = sizeof(WORD);
1826 break;
1827 case WM_MDICREATE:
1829 MDICREATESTRUCTW *mcs = buffer;
1830 char *ptr = (char *)(mcs + 1);
1832 memcpy( buffer, lparam_ptr, sizeof(*mcs) );
1833 if (!IS_INTRESOURCE(mcs->szClass))
1835 ptr += copy_string( ptr, mcs->szClass, ansi );
1836 mcs->szClass = inline_ptr;
1838 if (!IS_INTRESOURCE(mcs->szTitle))
1840 copy_string( ptr, mcs->szTitle, ansi );
1841 mcs->szTitle = inline_ptr;
1843 return;
1845 case CB_GETCOMBOBOXINFO:
1846 if (sizeof(void *) == 4)
1848 COMBOBOXINFO *cbi = buffer;
1849 memcpy( cbi, lparam_ptr, sizeof(*cbi) );
1850 cbi->cbSize = sizeof(*cbi);
1851 return;
1853 break;
1856 if (size) memcpy( buffer, lparam_ptr, size );
1859 /***********************************************************************
1860 * copy_user_result
1862 * Copy a message result received from client.
1864 static void copy_user_result( void *buffer, size_t size, LRESULT result, UINT message,
1865 WPARAM wparam, LPARAM lparam, BOOL ansi )
1867 void *lparam_ptr = (void *)lparam;
1868 size_t copy_size = 0;
1870 if (!size) return;
1872 switch (message)
1874 case WM_NCCREATE:
1875 case WM_CREATE:
1876 if (size >= sizeof(CREATESTRUCTW))
1878 CREATESTRUCTW *dst = lparam_ptr;
1879 const CREATESTRUCTW *src = buffer;
1880 dst->lpCreateParams = src->lpCreateParams;
1881 dst->hInstance = src->hInstance;
1882 dst->hMenu = src->hMenu;
1883 dst->hwndParent = src->hwndParent;
1884 dst->cy = src->cy;
1885 dst->cx = src->cx;
1886 dst->y = src->y;
1887 dst->x = src->x;
1888 dst->style = src->style;
1889 dst->dwExStyle = src->dwExStyle;
1890 /* don't allow changing name and class pointers */
1892 return;
1893 case WM_NCCALCSIZE:
1894 if (wparam)
1896 NCCALCSIZE_PARAMS *dst = lparam_ptr;
1897 const NCCALCSIZE_PARAMS *src = buffer;
1898 const WINDOWPOS *winpos = (const WINDOWPOS *)(src + 1);
1899 dst->rgrc[0] = src->rgrc[0];
1900 dst->rgrc[1] = src->rgrc[1];
1901 dst->rgrc[2] = src->rgrc[2];
1902 *dst->lppos = *winpos;
1903 return;
1905 copy_size = sizeof(RECT);
1906 break;
1907 case WM_GETTEXT:
1908 if (!result) memset( buffer, 0, char_size( ansi ));
1909 copy_size = string_size( buffer, ansi );
1910 break;
1911 case CB_GETLBTEXT:
1912 case LB_GETTEXT:
1913 copy_size = size;
1914 break;
1915 case WM_ASKCBFORMATNAME:
1916 copy_size = string_size( buffer, ansi );
1917 break;
1918 case WM_GETMINMAXINFO:
1919 copy_size = sizeof(MINMAXINFO);
1920 break;
1921 case WM_MEASUREITEM:
1922 copy_size = sizeof(MEASUREITEMSTRUCT);
1923 break;
1924 case WM_WINDOWPOSCHANGING:
1925 copy_size = sizeof(WINDOWPOS);
1926 break;
1927 case WM_STYLECHANGING:
1928 copy_size = sizeof(STYLESTRUCT);
1929 break;
1930 case SBM_SETSCROLLINFO:
1931 case SBM_GETSCROLLINFO:
1932 copy_size = sizeof(SCROLLINFO);
1933 break;
1934 case SBM_GETSCROLLBARINFO:
1935 copy_size = sizeof(SCROLLBARINFO);
1936 break;
1937 case EM_GETSEL:
1938 case SBM_GETRANGE:
1939 case CB_GETEDITSEL:
1941 DWORD *ptr = buffer;
1942 if (wparam) *(DWORD *)wparam = ptr[0];
1943 if (lparam) *(DWORD *)lparam = ptr[1];
1944 break;
1946 case WM_SIZING:
1947 case WM_MOVING:
1948 case EM_GETRECT:
1949 case EM_SETRECT:
1950 case EM_SETRECTNP:
1951 case LB_GETITEMRECT:
1952 case CB_GETDROPPEDCONTROLRECT:
1953 copy_size = sizeof(RECT);
1954 break;
1955 case EM_GETLINE:
1956 copy_size = string_size( buffer, ansi );
1957 break;
1958 case LB_GETSELITEMS:
1959 copy_size = wparam * sizeof(UINT);
1960 break;
1961 case WM_NEXTMENU:
1962 copy_size = sizeof(MDINEXTMENU);
1963 break;
1964 case CB_GETCOMBOBOXINFO:
1965 if (sizeof(void *) == 4)
1967 COMBOBOXINFO *cbi = lparam_ptr;
1968 memcpy( cbi, buffer, size );
1969 cbi->cbSize = sizeof(*cbi);
1970 return;
1972 copy_size = sizeof(COMBOBOXINFO);
1973 break;
1974 case WM_MDIGETACTIVE:
1975 if (lparam) copy_size = sizeof(BOOL);
1976 break;
1977 default:
1978 return;
1981 if (copy_size > size) copy_size = size;
1982 if (copy_size) memcpy( lparam_ptr, buffer, copy_size );
1985 /***********************************************************************
1986 * reply_message
1988 * Send a reply to a sent message.
1990 static void reply_message( struct received_message_info *info, LRESULT result, MSG *msg )
1992 struct packed_message data;
1993 int i, replied = info->flags & ISMEX_REPLIED;
1994 BOOL remove = msg != NULL;
1996 if (info->flags & ISMEX_NOTIFY) return; /* notify messages don't get replies */
1997 if (!remove && replied) return; /* replied already */
1999 memset( &data, 0, sizeof(data) );
2000 info->flags |= ISMEX_REPLIED;
2001 if (info == get_user_thread_info()->receive_info)
2002 NtUserGetThreadInfo()->receive_flags = info->flags;
2004 if (info->type == MSG_OTHER_PROCESS && !replied)
2006 if (!msg) msg = &info->msg;
2007 pack_reply( msg->hwnd, msg->message, msg->wParam, msg->lParam, result, &data );
2010 SERVER_START_REQ( reply_message )
2012 req->result = result;
2013 req->remove = remove;
2014 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
2015 wine_server_call( req );
2017 SERVER_END_REQ;
2020 /***********************************************************************
2021 * reply_message_result
2023 * Send a reply to a sent message and update thread receive info.
2025 BOOL reply_message_result( LRESULT result )
2027 struct user_thread_info *thread_info = get_user_thread_info();
2028 struct received_message_info *info = thread_info->receive_info;
2030 if (!info) return FALSE;
2031 reply_message( info, result, NULL );
2032 return TRUE;
2035 /***********************************************************************
2036 * reply_winproc_result
2038 * Send a reply to a sent message and update thread receive info.
2040 static BOOL reply_winproc_result( LRESULT result, HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
2042 struct user_thread_info *thread_info = get_user_thread_info();
2043 struct received_message_info *info = thread_info->receive_info;
2044 MSG msg;
2046 if (!info) return FALSE;
2048 msg.hwnd = hwnd;
2049 msg.message = message;
2050 msg.wParam = wparam;
2051 msg.lParam = lparam;
2052 reply_message( info, result, &msg );
2054 thread_info->receive_info = info->prev;
2055 thread_info->client_info.receive_flags = info->prev ? info->prev->flags : ISMEX_NOSEND;
2056 return TRUE;
2059 /***********************************************************************
2060 * handle_internal_message
2062 * Handle an internal Wine message instead of calling the window proc.
2064 static LRESULT handle_internal_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2066 switch(msg)
2068 case WM_WINE_DESTROYWINDOW:
2069 return destroy_window( hwnd );
2070 case WM_WINE_SETWINDOWPOS:
2071 if (is_desktop_window( hwnd )) return 0;
2072 return set_window_pos( (WINDOWPOS *)lparam, 0, 0 );
2073 case WM_WINE_SHOWWINDOW:
2074 if (is_desktop_window( hwnd )) return 0;
2075 return NtUserShowWindow( hwnd, wparam );
2076 case WM_WINE_SETPARENT:
2077 if (is_desktop_window( hwnd )) return 0;
2078 return HandleToUlong( NtUserSetParent( hwnd, UlongToHandle(wparam) ));
2079 case WM_WINE_SETWINDOWLONG:
2080 return set_window_long( hwnd, (short)LOWORD(wparam), HIWORD(wparam), lparam, FALSE );
2081 case WM_WINE_SETSTYLE:
2082 if (is_desktop_window( hwnd )) return 0;
2083 return set_window_style( hwnd, wparam, lparam );
2084 case WM_WINE_SETACTIVEWINDOW:
2085 if (!wparam && NtUserGetForegroundWindow() == hwnd) return 0;
2086 return (LRESULT)NtUserSetActiveWindow( (HWND)wparam );
2087 case WM_WINE_KEYBOARD_LL_HOOK:
2088 case WM_WINE_MOUSE_LL_HOOK:
2090 struct hook_extra_info *h_extra = (struct hook_extra_info *)lparam;
2092 return call_current_hook( h_extra->handle, HC_ACTION, wparam, h_extra->lparam );
2094 case WM_WINE_CLIPCURSOR:
2095 /* non-hardware message, posted on display mode change to trigger fullscreen
2096 clipping or to the desktop window to forcefully release the cursor grabs */
2097 if (wparam & SET_CURSOR_FSCLIP) return clip_fullscreen_window( hwnd, FALSE );
2098 return process_wine_clipcursor( hwnd, wparam, lparam );
2099 case WM_WINE_SETCURSOR:
2100 FIXME( "Unexpected non-hardware WM_WINE_SETCURSOR message\n" );
2101 return FALSE;
2102 case WM_WINE_UPDATEWINDOWSTATE:
2103 update_window_state( hwnd );
2104 return 0;
2105 default:
2106 if (msg >= WM_WINE_FIRST_DRIVER_MSG && msg <= WM_WINE_LAST_DRIVER_MSG)
2107 return user_driver->pWindowMessage( hwnd, msg, wparam, lparam );
2108 FIXME( "unknown internal message %x\n", msg );
2109 return 0;
2113 /**********************************************************************
2114 * NtUserGetGUIThreadInfo (win32u.@)
2116 BOOL WINAPI NtUserGetGUIThreadInfo( DWORD id, GUITHREADINFO *info )
2118 BOOL ret;
2120 if (info->cbSize != sizeof(*info))
2122 RtlSetLastWin32Error( ERROR_INVALID_PARAMETER );
2123 return FALSE;
2126 SERVER_START_REQ( get_thread_input )
2128 req->tid = id;
2129 if ((ret = !wine_server_call_err( req )))
2131 info->flags = 0;
2132 info->hwndActive = wine_server_ptr_handle( reply->active );
2133 info->hwndFocus = wine_server_ptr_handle( reply->focus );
2134 info->hwndCapture = wine_server_ptr_handle( reply->capture );
2135 info->hwndMenuOwner = wine_server_ptr_handle( reply->menu_owner );
2136 info->hwndMoveSize = wine_server_ptr_handle( reply->move_size );
2137 info->hwndCaret = wine_server_ptr_handle( reply->caret );
2138 info->rcCaret.left = reply->rect.left;
2139 info->rcCaret.top = reply->rect.top;
2140 info->rcCaret.right = reply->rect.right;
2141 info->rcCaret.bottom = reply->rect.bottom;
2142 if (reply->menu_owner) info->flags |= GUI_INMENUMODE;
2143 if (reply->move_size) info->flags |= GUI_INMOVESIZE;
2144 if (reply->caret) info->flags |= GUI_CARETBLINKING;
2147 SERVER_END_REQ;
2148 return ret;
2151 /***********************************************************************
2152 * call_window_proc
2154 * Call a window procedure and the corresponding hooks.
2156 static LRESULT call_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
2157 enum message_type type, BOOL same_thread,
2158 enum wm_char_mapping mapping, BOOL ansi_dst )
2160 struct win_proc_params p, *params = &p;
2161 BOOL ansi = ansi_dst && type == MSG_ASCII;
2162 size_t packed_size = 0, offset = sizeof(*params);
2163 LRESULT result = 0;
2164 CWPSTRUCT cwp;
2165 CWPRETSTRUCT cwpret;
2166 void *ret_ptr;
2167 size_t ret_len = 0;
2169 if (msg & 0x80000000)
2170 return handle_internal_message( hwnd, msg, wparam, lparam );
2172 if (!is_current_thread_window( hwnd )) return 0;
2174 packed_size = user_message_size( hwnd, msg, wparam, lparam, type == MSG_OTHER_PROCESS, ansi );
2176 /* first the WH_CALLWNDPROC hook */
2177 cwp.lParam = lparam;
2178 cwp.wParam = wparam;
2179 cwp.message = msg;
2180 cwp.hwnd = hwnd = get_full_window_handle( hwnd );
2181 call_message_hooks( WH_CALLWNDPROC, HC_ACTION, same_thread, (LPARAM)&cwp, sizeof(cwp),
2182 packed_size, ansi );
2184 if (packed_size)
2186 offset = (offset + 15) & ~15;
2187 if (!(params = malloc( offset + packed_size ))) return 0;
2190 if (!init_window_call_params( params, hwnd, msg, wparam, lparam, ansi_dst, mapping ))
2192 if (params != &p) free( params );
2193 return 0;
2196 if (type == MSG_OTHER_PROCESS) params->ansi = FALSE;
2197 if (packed_size)
2198 pack_user_message( (char *)params + offset, packed_size, msg, wparam, lparam, ansi );
2200 result = dispatch_win_proc_params( params, offset + packed_size, &ret_ptr, &ret_len );
2201 if (params != &p) free( params );
2203 copy_user_result( ret_ptr, min( ret_len, packed_size ), result, msg, wparam, lparam, ansi );
2205 /* and finally the WH_CALLWNDPROCRET hook */
2206 cwpret.lResult = result;
2207 cwpret.lParam = lparam;
2208 cwpret.wParam = wparam;
2209 cwpret.message = msg;
2210 cwpret.hwnd = hwnd;
2211 call_message_hooks( WH_CALLWNDPROCRET, HC_ACTION, same_thread, (LPARAM)&cwpret, sizeof(cwpret),
2212 packed_size, ansi );
2213 return result;
2216 /***********************************************************************
2217 * call_sendmsg_callback
2219 * Call the callback function of SendMessageCallback.
2221 static inline void call_sendmsg_callback( SENDASYNCPROC callback, HWND hwnd, UINT msg,
2222 ULONG_PTR data, LRESULT result )
2224 struct send_async_params params;
2225 void *ret_ptr;
2226 ULONG ret_len;
2228 if (!callback) return;
2230 params.callback = callback;
2231 params.hwnd = hwnd;
2232 params.msg = msg;
2233 params.data = data;
2234 params.result = result;
2236 TRACE_(relay)( "\1Call message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
2237 callback, hwnd, debugstr_msg_name( msg, hwnd ), data, result );
2239 KeUserModeCallback( NtUserCallSendAsyncCallback, &params, sizeof(params), &ret_ptr, &ret_len );
2241 TRACE_(relay)( "\1Ret message callback %p (hwnd=%p,msg=%s,data=%08lx,result=%08lx)\n",
2242 callback, hwnd, debugstr_msg_name( msg, hwnd ), data, result );
2245 /***********************************************************************
2246 * accept_hardware_message
2248 * Tell the server we have passed the message to the app
2249 * (even though we may end up dropping it later on)
2251 static void accept_hardware_message( UINT hw_id )
2253 SERVER_START_REQ( accept_hardware_message )
2255 req->hw_id = hw_id;
2256 if (wine_server_call( req ))
2257 FIXME("Failed to reply to MSG_HARDWARE message. Message may not be removed from queue.\n");
2259 SERVER_END_REQ;
2262 /***********************************************************************
2263 * send_parent_notify
2265 * Send a WM_PARENTNOTIFY to all ancestors of the given window, unless
2266 * the window has the WS_EX_NOPARENTNOTIFY style.
2268 static void send_parent_notify( HWND hwnd, WORD event, WORD idChild, POINT pt )
2270 /* pt has to be in the client coordinates of the parent window */
2271 map_window_points( 0, hwnd, &pt, 1, get_thread_dpi() );
2272 for (;;)
2274 HWND parent;
2276 if (!(get_window_long( hwnd, GWL_STYLE ) & WS_CHILD)) break;
2277 if (get_window_long( hwnd, GWL_EXSTYLE ) & WS_EX_NOPARENTNOTIFY) break;
2278 if (!(parent = get_parent( hwnd ))) break;
2279 if (parent == get_desktop_window()) break;
2280 map_window_points( hwnd, parent, &pt, 1, get_thread_dpi() );
2281 hwnd = parent;
2282 send_message( hwnd, WM_PARENTNOTIFY,
2283 MAKEWPARAM( event, idChild ), MAKELPARAM( pt.x, pt.y ) );
2287 /***********************************************************************
2288 * process_keyboard_message
2290 * returns TRUE if the contents of 'msg' should be passed to the application
2292 static BOOL process_keyboard_message( MSG *msg, UINT hw_id, HWND hwnd_filter,
2293 UINT first, UINT last, BOOL remove )
2295 EVENTMSG event;
2297 if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN ||
2298 msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP)
2299 switch (msg->wParam)
2301 case VK_LSHIFT: case VK_RSHIFT:
2302 msg->wParam = VK_SHIFT;
2303 break;
2304 case VK_LCONTROL: case VK_RCONTROL:
2305 msg->wParam = VK_CONTROL;
2306 break;
2307 case VK_LMENU: case VK_RMENU:
2308 msg->wParam = VK_MENU;
2309 break;
2312 /* FIXME: is this really the right place for this hook? */
2313 event.message = msg->message;
2314 event.hwnd = msg->hwnd;
2315 event.time = msg->time;
2316 event.paramL = (msg->wParam & 0xFF) | (HIWORD(msg->lParam) << 8);
2317 event.paramH = msg->lParam & 0x7FFF;
2318 if (HIWORD(msg->lParam) & 0x0100) event.paramH |= 0x8000; /* special_key - bit */
2319 call_hooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, sizeof(event) );
2321 /* check message filters */
2322 if (msg->message < first || msg->message > last) return FALSE;
2323 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
2325 if (remove)
2327 if((msg->message == WM_KEYDOWN) &&
2328 (msg->hwnd != get_desktop_window()))
2330 /* Handle F1 key by sending out WM_HELP message */
2331 if (msg->wParam == VK_F1)
2333 NtUserPostMessage( msg->hwnd, WM_KEYF1, 0, 0 );
2335 else if(msg->wParam >= VK_BROWSER_BACK &&
2336 msg->wParam <= VK_LAUNCH_APP2)
2338 /* FIXME: Process keystate */
2339 send_message( msg->hwnd, WM_APPCOMMAND, (WPARAM)msg->hwnd,
2340 MAKELPARAM(0, (FAPPCOMMAND_KEY | (msg->wParam - VK_BROWSER_BACK + 1))) );
2343 else if (msg->message == WM_KEYUP)
2345 /* Handle VK_APPS key by posting a WM_CONTEXTMENU message */
2346 if (msg->wParam == VK_APPS && !is_menu_active())
2347 NtUserPostMessage( msg->hwnd, WM_CONTEXTMENU, (WPARAM)msg->hwnd, -1 );
2351 if (call_hooks( WH_KEYBOARD, remove ? HC_ACTION : HC_NOREMOVE,
2352 LOWORD(msg->wParam), msg->lParam, 0 ))
2354 /* skip this message */
2355 call_hooks( WH_CBT, HCBT_KEYSKIPPED, LOWORD(msg->wParam), msg->lParam, 0 );
2356 accept_hardware_message( hw_id );
2357 return FALSE;
2359 if (remove) accept_hardware_message( hw_id );
2360 msg->pt = point_phys_to_win_dpi( msg->hwnd, msg->pt );
2362 if (remove && msg->message == WM_KEYDOWN)
2363 if (ImmProcessKey( msg->hwnd, NtUserGetKeyboardLayout(0), msg->wParam, msg->lParam, 0 ))
2364 msg->wParam = VK_PROCESSKEY;
2366 return TRUE;
2369 /***********************************************************************
2370 * process_mouse_message
2372 * returns TRUE if the contents of 'msg' should be passed to the application
2374 static BOOL process_mouse_message( MSG *msg, UINT hw_id, ULONG_PTR extra_info, HWND hwnd_filter,
2375 UINT first, UINT last, BOOL remove )
2377 static MSG clk_msg;
2379 POINT pt;
2380 UINT message;
2381 INT hittest;
2382 EVENTMSG event;
2383 GUITHREADINFO info;
2384 MOUSEHOOKSTRUCTEX hook;
2385 BOOL eat_msg;
2386 WPARAM wparam;
2388 /* find the window to dispatch this mouse message to */
2390 info.cbSize = sizeof(info);
2391 NtUserGetGUIThreadInfo( GetCurrentThreadId(), &info );
2392 if (info.hwndCapture)
2394 hittest = HTCLIENT;
2395 msg->hwnd = info.hwndCapture;
2397 else
2399 HWND orig = msg->hwnd;
2401 msg->hwnd = window_from_point( msg->hwnd, msg->pt, &hittest );
2402 if (!msg->hwnd) /* As a heuristic, try the next window if it's the owner of orig */
2404 HWND next = get_window_relative( orig, GW_HWNDNEXT );
2406 if (next && get_window_relative( orig, GW_OWNER ) == next &&
2407 is_current_thread_window( next ))
2408 msg->hwnd = window_from_point( next, msg->pt, &hittest );
2412 if (!msg->hwnd || !is_current_thread_window( msg->hwnd ))
2414 accept_hardware_message( hw_id );
2415 return FALSE;
2418 msg->pt = point_phys_to_win_dpi( msg->hwnd, msg->pt );
2419 SetThreadDpiAwarenessContext( get_window_dpi_awareness_context( msg->hwnd ));
2421 /* FIXME: is this really the right place for this hook? */
2422 event.message = msg->message;
2423 event.time = msg->time;
2424 event.hwnd = msg->hwnd;
2425 event.paramL = msg->pt.x;
2426 event.paramH = msg->pt.y;
2427 call_hooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&event, sizeof(event) );
2429 if (!check_hwnd_filter( msg, hwnd_filter )) return FALSE;
2431 pt = msg->pt;
2432 message = msg->message;
2433 wparam = msg->wParam;
2434 /* Note: windows has no concept of a non-client wheel message */
2435 if (message != WM_MOUSEWHEEL)
2437 if (hittest != HTCLIENT)
2439 message += WM_NCMOUSEMOVE - WM_MOUSEMOVE;
2440 wparam = hittest;
2442 else
2444 /* coordinates don't get translated while tracking a menu */
2445 /* FIXME: should differentiate popups and top-level menus */
2446 if (!(info.flags & GUI_INMENUMODE))
2447 screen_to_client( msg->hwnd, &pt );
2450 msg->lParam = MAKELONG( pt.x, pt.y );
2452 /* translate double clicks */
2454 if (msg->message == WM_LBUTTONDOWN ||
2455 msg->message == WM_RBUTTONDOWN ||
2456 msg->message == WM_MBUTTONDOWN ||
2457 msg->message == WM_XBUTTONDOWN)
2459 BOOL update = remove;
2461 /* translate double clicks -
2462 * note that ...MOUSEMOVEs can slip in between
2463 * ...BUTTONDOWN and ...BUTTONDBLCLK messages */
2465 if ((info.flags & (GUI_INMENUMODE|GUI_INMOVESIZE)) ||
2466 hittest != HTCLIENT ||
2467 (get_class_long( msg->hwnd, GCL_STYLE, FALSE ) & CS_DBLCLKS))
2469 if ((msg->message == clk_msg.message) &&
2470 (msg->hwnd == clk_msg.hwnd) &&
2471 (msg->wParam == clk_msg.wParam) &&
2472 (msg->time - clk_msg.time < NtUserGetDoubleClickTime()) &&
2473 (abs(msg->pt.x - clk_msg.pt.x) < get_system_metrics( SM_CXDOUBLECLK ) / 2) &&
2474 (abs(msg->pt.y - clk_msg.pt.y) < get_system_metrics( SM_CYDOUBLECLK ) / 2))
2476 message += (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
2477 if (update)
2479 clk_msg.message = 0; /* clear the double click conditions */
2480 update = FALSE;
2484 if (message < first || message > last) return FALSE;
2485 /* update static double click conditions */
2486 if (update) clk_msg = *msg;
2488 else
2490 if (message < first || message > last) return FALSE;
2492 msg->wParam = wparam;
2494 /* message is accepted now (but may still get dropped) */
2496 hook.pt = msg->pt;
2497 hook.hwnd = msg->hwnd;
2498 hook.wHitTestCode = hittest;
2499 hook.dwExtraInfo = extra_info;
2500 hook.mouseData = msg->wParam;
2501 if (call_hooks( WH_MOUSE, remove ? HC_ACTION : HC_NOREMOVE, message, (LPARAM)&hook, sizeof(hook) ))
2503 hook.pt = msg->pt;
2504 hook.hwnd = msg->hwnd;
2505 hook.wHitTestCode = hittest;
2506 hook.dwExtraInfo = extra_info;
2507 hook.mouseData = msg->wParam;
2508 call_hooks( WH_CBT, HCBT_CLICKSKIPPED, message, (LPARAM)&hook, sizeof(hook) );
2509 accept_hardware_message( hw_id );
2510 return FALSE;
2513 if ((hittest == HTERROR) || (hittest == HTNOWHERE))
2515 send_message( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd, MAKELONG( hittest, msg->message ));
2516 accept_hardware_message( hw_id );
2517 return FALSE;
2520 if (remove) accept_hardware_message( hw_id );
2522 if (!remove || info.hwndCapture)
2524 msg->message = message;
2525 return TRUE;
2528 eat_msg = FALSE;
2530 if (msg->message == WM_LBUTTONDOWN ||
2531 msg->message == WM_RBUTTONDOWN ||
2532 msg->message == WM_MBUTTONDOWN ||
2533 msg->message == WM_XBUTTONDOWN)
2535 /* Send the WM_PARENTNOTIFY,
2536 * note that even for double/nonclient clicks
2537 * notification message is still WM_L/M/RBUTTONDOWN.
2539 send_parent_notify( msg->hwnd, msg->message, 0, msg->pt );
2541 /* Activate the window if needed */
2543 if (msg->hwnd != info.hwndActive)
2545 HWND hwndTop = NtUserGetAncestor( msg->hwnd, GA_ROOT );
2547 if ((get_window_long( hwndTop, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD)
2549 UINT ret = send_message( msg->hwnd, WM_MOUSEACTIVATE, (WPARAM)hwndTop,
2550 MAKELONG( hittest, msg->message ) );
2551 switch(ret)
2553 case MA_NOACTIVATEANDEAT:
2554 eat_msg = TRUE;
2555 /* fall through */
2556 case MA_NOACTIVATE:
2557 break;
2558 case MA_ACTIVATEANDEAT:
2559 eat_msg = TRUE;
2560 /* fall through */
2561 case MA_ACTIVATE:
2562 case 0:
2563 if (!set_foreground_window( hwndTop, TRUE )) eat_msg = TRUE;
2564 break;
2565 default:
2566 WARN( "unknown WM_MOUSEACTIVATE code %d\n", ret );
2567 break;
2573 /* send the WM_SETCURSOR message */
2575 /* Windows sends the normal mouse message as the message parameter
2576 in the WM_SETCURSOR message even if it's non-client mouse message */
2577 send_message( msg->hwnd, WM_SETCURSOR, (WPARAM)msg->hwnd, MAKELONG( hittest, msg->message ));
2579 msg->message = message;
2580 return !eat_msg;
2583 /***********************************************************************
2584 * process_hardware_message
2586 * Process a hardware message; return TRUE if message should be passed on to the app
2588 static BOOL process_hardware_message( MSG *msg, UINT hw_id, const struct hardware_msg_data *msg_data,
2589 HWND hwnd_filter, UINT first, UINT last, BOOL remove )
2591 struct ntuser_thread_info *thread_info = NtUserGetThreadInfo();
2592 DPI_AWARENESS_CONTEXT context;
2593 BOOL ret = FALSE;
2595 thread_info->msg_source.deviceType = msg_data->source.device;
2596 thread_info->msg_source.originId = msg_data->source.origin;
2598 /* hardware messages are always in physical coords */
2599 context = SetThreadDpiAwarenessContext( DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE );
2601 if (msg->message == WM_INPUT || msg->message == WM_INPUT_DEVICE_CHANGE)
2602 ret = process_rawinput_message( msg, hw_id, msg_data );
2603 else if (is_keyboard_message( msg->message ))
2604 ret = process_keyboard_message( msg, hw_id, hwnd_filter, first, last, remove );
2605 else if (is_mouse_message( msg->message ))
2606 ret = process_mouse_message( msg, hw_id, msg_data->info, hwnd_filter, first, last, remove );
2607 else if (msg->message == WM_WINE_CLIPCURSOR)
2608 process_wine_clipcursor( msg->hwnd, msg->wParam, msg->lParam );
2609 else if (msg->message == WM_WINE_SETCURSOR)
2610 process_wine_setcursor( msg->hwnd, (HWND)msg->wParam, (HCURSOR)msg->lParam );
2611 else
2612 ERR( "unknown message type %x\n", msg->message );
2613 SetThreadDpiAwarenessContext( context );
2614 return ret;
2617 /***********************************************************************
2618 * peek_message
2620 * Peek for a message matching the given parameters. Return 0 if none are
2621 * available; -1 on error.
2622 * All pending sent messages are processed before returning.
2624 static int peek_message( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags, UINT changed_mask )
2626 LRESULT result;
2627 struct user_thread_info *thread_info = get_user_thread_info();
2628 INPUT_MESSAGE_SOURCE prev_source = thread_info->client_info.msg_source;
2629 struct received_message_info info;
2630 unsigned int hw_id = 0; /* id of previous hardware message */
2631 void *buffer;
2632 size_t buffer_size = 1024;
2634 if (!(buffer = malloc( buffer_size ))) return -1;
2636 if (!first && !last) last = ~0;
2637 if (hwnd == HWND_BROADCAST) hwnd = HWND_TOPMOST;
2639 for (;;)
2641 NTSTATUS res;
2642 size_t size = 0;
2643 const message_data_t *msg_data = buffer;
2645 thread_info->client_info.msg_source = prev_source;
2647 SERVER_START_REQ( get_message )
2649 req->flags = flags;
2650 req->get_win = wine_server_user_handle( hwnd );
2651 req->get_first = first;
2652 req->get_last = last;
2653 req->hw_id = hw_id;
2654 req->wake_mask = changed_mask & (QS_SENDMESSAGE | QS_SMRESULT);
2655 req->changed_mask = changed_mask;
2656 wine_server_set_reply( req, buffer, buffer_size );
2657 if (!(res = wine_server_call( req )))
2659 size = wine_server_reply_size( reply );
2660 info.type = reply->type;
2661 info.msg.hwnd = wine_server_ptr_handle( reply->win );
2662 info.msg.message = reply->msg;
2663 info.msg.wParam = reply->wparam;
2664 info.msg.lParam = reply->lparam;
2665 info.msg.time = reply->time;
2666 info.msg.pt.x = reply->x;
2667 info.msg.pt.y = reply->y;
2668 hw_id = 0;
2669 thread_info->active_hooks = reply->active_hooks;
2671 else buffer_size = reply->total;
2673 SERVER_END_REQ;
2675 if (res)
2677 free( buffer );
2678 if (res == STATUS_PENDING)
2680 thread_info->wake_mask = changed_mask & (QS_SENDMESSAGE | QS_SMRESULT);
2681 thread_info->changed_mask = changed_mask;
2682 return 0;
2684 if (res != STATUS_BUFFER_OVERFLOW)
2686 RtlSetLastWin32Error( RtlNtStatusToDosError(res) );
2687 return -1;
2689 if (!(buffer = malloc( buffer_size ))) return -1;
2690 continue;
2693 TRACE( "got type %d msg %x (%s) hwnd %p wp %lx lp %lx\n",
2694 info.type, info.msg.message,
2695 (info.type == MSG_WINEVENT) ? "MSG_WINEVENT" : debugstr_msg_name(info.msg.message, info.msg.hwnd),
2696 info.msg.hwnd, (long)info.msg.wParam, info.msg.lParam );
2698 switch(info.type)
2700 case MSG_ASCII:
2701 case MSG_UNICODE:
2702 info.flags = ISMEX_SEND;
2703 break;
2704 case MSG_NOTIFY:
2705 info.flags = ISMEX_NOTIFY;
2706 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2707 &info.msg.lParam, &buffer, size ))
2708 continue;
2709 break;
2710 case MSG_CALLBACK:
2711 info.flags = ISMEX_CALLBACK;
2712 break;
2713 case MSG_CALLBACK_RESULT:
2714 if (size >= sizeof(msg_data->callback))
2715 call_sendmsg_callback( wine_server_get_ptr(msg_data->callback.callback),
2716 info.msg.hwnd, info.msg.message,
2717 msg_data->callback.data, msg_data->callback.result );
2718 continue;
2719 case MSG_WINEVENT:
2720 if (size >= sizeof(msg_data->winevent))
2722 struct win_event_hook_params params;
2723 void *ret_ptr;
2724 ULONG ret_len;
2726 params.proc = wine_server_get_ptr( msg_data->winevent.hook_proc );
2727 size -= sizeof(msg_data->winevent);
2728 if (size)
2730 size = min( size, sizeof(params.module) - sizeof(WCHAR) );
2731 memcpy( params.module, &msg_data->winevent + 1, size );
2733 params.module[size / sizeof(WCHAR)] = 0;
2734 size = FIELD_OFFSET( struct win_event_hook_params, module[size / sizeof(WCHAR) + 1] );
2736 params.handle = wine_server_ptr_handle( msg_data->winevent.hook );
2737 params.event = info.msg.message;
2738 params.hwnd = info.msg.hwnd;
2739 params.object_id = info.msg.wParam;
2740 params.child_id = info.msg.lParam;
2741 params.tid = msg_data->winevent.tid;
2742 params.time = info.msg.time;
2744 KeUserModeCallback( NtUserCallWinEventHook, &params, size, &ret_ptr, &ret_len );
2746 continue;
2747 case MSG_HOOK_LL:
2748 info.flags = ISMEX_SEND;
2749 result = 0;
2750 if (info.msg.message == WH_KEYBOARD_LL && size >= sizeof(msg_data->hardware))
2752 KBDLLHOOKSTRUCT hook;
2754 hook.vkCode = LOWORD( info.msg.lParam );
2755 hook.scanCode = HIWORD( info.msg.lParam );
2756 hook.flags = msg_data->hardware.flags;
2757 hook.time = info.msg.time;
2758 hook.dwExtraInfo = msg_data->hardware.info;
2759 TRACE( "calling keyboard LL hook vk %x scan %x flags %x time %u info %lx\n",
2760 (int)hook.vkCode, (int)hook.scanCode, (int)hook.flags,
2761 (int)hook.time, (long)hook.dwExtraInfo );
2762 result = call_hooks( WH_KEYBOARD_LL, HC_ACTION, info.msg.wParam,
2763 (LPARAM)&hook, sizeof(hook) );
2765 else if (info.msg.message == WH_MOUSE_LL && size >= sizeof(msg_data->hardware))
2767 MSLLHOOKSTRUCT hook;
2769 hook.pt = info.msg.pt;
2770 hook.mouseData = info.msg.lParam;
2771 hook.flags = msg_data->hardware.flags;
2772 hook.time = info.msg.time;
2773 hook.dwExtraInfo = msg_data->hardware.info;
2774 TRACE( "calling mouse LL hook pos %d,%d data %x flags %x time %u info %lx\n",
2775 (int)hook.pt.x, (int)hook.pt.y, (int)hook.mouseData, (int)hook.flags,
2776 (int)hook.time, (long)hook.dwExtraInfo );
2777 result = call_hooks( WH_MOUSE_LL, HC_ACTION, info.msg.wParam,
2778 (LPARAM)&hook, sizeof(hook) );
2780 reply_message( &info, result, &info.msg );
2781 continue;
2782 case MSG_OTHER_PROCESS:
2783 info.flags = ISMEX_SEND;
2784 if (!unpack_message( info.msg.hwnd, info.msg.message, &info.msg.wParam,
2785 &info.msg.lParam, &buffer, size ))
2787 /* ignore it */
2788 reply_message( &info, 0, &info.msg );
2789 continue;
2791 break;
2792 case MSG_HARDWARE:
2793 if (size >= sizeof(msg_data->hardware))
2795 hw_id = msg_data->hardware.hw_id;
2796 if (!process_hardware_message( &info.msg, hw_id, &msg_data->hardware,
2797 hwnd, first, last, flags & PM_REMOVE ))
2799 TRACE("dropping msg %x\n", info.msg.message );
2800 continue; /* ignore it */
2802 *msg = info.msg;
2803 thread_info->client_info.message_pos = MAKELONG( info.msg.pt.x, info.msg.pt.y );
2804 thread_info->client_info.message_time = info.msg.time;
2805 thread_info->client_info.message_extra = msg_data->hardware.info;
2806 free( buffer );
2807 call_hooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)msg, sizeof(*msg) );
2808 return 1;
2810 continue;
2811 case MSG_POSTED:
2812 if (info.msg.message & 0x80000000) /* internal message */
2814 if (flags & PM_REMOVE)
2816 handle_internal_message( info.msg.hwnd, info.msg.message,
2817 info.msg.wParam, info.msg.lParam );
2818 /* if this is a nested call return right away */
2819 if (first == info.msg.message && last == info.msg.message)
2821 free( buffer );
2822 return 0;
2825 else
2826 peek_message( msg, info.msg.hwnd, info.msg.message,
2827 info.msg.message, flags | PM_REMOVE, changed_mask );
2828 continue;
2830 if (info.msg.message >= WM_DDE_FIRST && info.msg.message <= WM_DDE_LAST)
2832 struct unpack_dde_message_result result;
2833 struct unpack_dde_message_params *params;
2834 void *ret_ptr;
2835 ULONG len;
2836 BOOL ret;
2838 len = FIELD_OFFSET( struct unpack_dde_message_params, data[size] );
2839 if (!(params = malloc( len )))
2840 continue;
2841 params->result = &result;
2842 params->hwnd = info.msg.hwnd;
2843 params->message = info.msg.message;
2844 params->wparam = info.msg.wParam;
2845 params->lparam = info.msg.lParam;
2846 if (size) memcpy( params->data, buffer, size );
2847 ret = KeUserModeCallback( NtUserUnpackDDEMessage, params, len, &ret_ptr, &len );
2848 if (len == sizeof(result)) result = *(struct unpack_dde_message_result *)ret_ptr;
2849 free( params );
2850 if (!ret) continue; /* ignore it */
2851 info.msg.wParam = result.wparam;
2852 info.msg.lParam = result.lparam;
2854 *msg = info.msg;
2855 msg->pt = point_phys_to_win_dpi( info.msg.hwnd, info.msg.pt );
2856 thread_info->client_info.message_pos = MAKELONG( msg->pt.x, msg->pt.y );
2857 thread_info->client_info.message_time = info.msg.time;
2858 thread_info->client_info.message_extra = 0;
2859 thread_info->client_info.msg_source = msg_source_unavailable;
2860 free( buffer );
2861 call_hooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)msg, sizeof(*msg) );
2862 return 1;
2865 /* if we get here, we have a sent message; call the window procedure */
2866 info.prev = thread_info->receive_info;
2867 thread_info->receive_info = &info;
2868 thread_info->client_info.msg_source = msg_source_unavailable;
2869 thread_info->client_info.receive_flags = info.flags;
2870 result = call_window_proc( info.msg.hwnd, info.msg.message, info.msg.wParam,
2871 info.msg.lParam, info.type, FALSE, WMCHAR_MAP_RECVMESSAGE,
2872 info.type == MSG_ASCII );
2873 if (thread_info->receive_info == &info)
2874 reply_winproc_result( result, info.msg.hwnd, info.msg.message,
2875 info.msg.wParam, info.msg.lParam );
2877 /* if some PM_QS* flags were specified, only handle sent messages from now on */
2878 if (HIWORD(flags) && !changed_mask) flags = PM_QS_SENDMESSAGE | LOWORD(flags);
2882 /***********************************************************************
2883 * process_sent_messages
2885 * Process all pending sent messages.
2887 static void process_sent_messages(void)
2889 MSG msg;
2890 peek_message( &msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE, 0 );
2893 /***********************************************************************
2894 * get_server_queue_handle
2896 * Get a handle to the server message queue for the current thread.
2898 static HANDLE get_server_queue_handle(void)
2900 struct user_thread_info *thread_info = get_user_thread_info();
2901 HANDLE ret;
2903 if (!(ret = thread_info->server_queue))
2905 SERVER_START_REQ( get_msg_queue )
2907 wine_server_call( req );
2908 ret = wine_server_ptr_handle( reply->handle );
2910 SERVER_END_REQ;
2911 thread_info->server_queue = ret;
2912 if (!ret) ERR( "Cannot get server thread queue\n" );
2914 return ret;
2917 /* check for driver events if we detect that the app is not properly consuming messages */
2918 static inline void check_for_driver_events( UINT msg )
2920 if (get_user_thread_info()->message_count > 200)
2922 flush_window_surfaces( FALSE );
2923 user_driver->pProcessEvents( QS_ALLINPUT );
2925 else if (msg == WM_TIMER || msg == WM_SYSTIMER)
2927 /* driver events should have priority over timers, so make sure we'll check for them soon */
2928 get_user_thread_info()->message_count += 100;
2930 else get_user_thread_info()->message_count++;
2933 /* helper for kernel32->ntdll timeout format conversion */
2934 static inline LARGE_INTEGER *get_nt_timeout( LARGE_INTEGER *time, DWORD timeout )
2936 if (timeout == INFINITE) return NULL;
2937 time->QuadPart = (ULONGLONG)timeout * -10000;
2938 return time;
2941 /* wait for message or signaled handle */
2942 static DWORD wait_message( DWORD count, const HANDLE *handles, DWORD timeout, DWORD mask, DWORD flags )
2944 LARGE_INTEGER time;
2945 DWORD ret, lock;
2946 void *ret_ptr;
2947 ULONG ret_len;
2949 if (enable_thunk_lock)
2950 lock = KeUserModeCallback( NtUserThunkLock, NULL, 0, &ret_ptr, &ret_len );
2952 if (user_driver->pProcessEvents( mask )) ret = count ? count - 1 : 0;
2953 else if (count)
2955 ret = NtWaitForMultipleObjects( count, handles, !(flags & MWMO_WAITALL),
2956 !!(flags & MWMO_ALERTABLE), get_nt_timeout( &time, timeout ));
2957 if (ret == count - 1) user_driver->pProcessEvents( mask );
2958 else if (HIWORD(ret)) /* is it an error code? */
2960 RtlSetLastWin32Error( RtlNtStatusToDosError(ret) );
2961 ret = WAIT_FAILED;
2964 else ret = WAIT_TIMEOUT;
2966 if (ret == WAIT_TIMEOUT && !count && !timeout) NtYieldExecution();
2967 if ((mask & QS_INPUT) == QS_INPUT) get_user_thread_info()->message_count = 0;
2969 if (enable_thunk_lock)
2970 KeUserModeCallback( NtUserThunkLock, &lock, sizeof(lock), &ret_ptr, &ret_len );
2972 return ret;
2975 /***********************************************************************
2976 * wait_objects
2978 * Wait for multiple objects including the server queue, with specific queue masks.
2980 static DWORD wait_objects( DWORD count, const HANDLE *handles, DWORD timeout,
2981 DWORD wake_mask, DWORD changed_mask, DWORD flags )
2983 struct user_thread_info *thread_info = get_user_thread_info();
2984 DWORD ret;
2986 assert( count ); /* we must have at least the server queue */
2988 flush_window_surfaces( TRUE );
2990 if (thread_info->wake_mask != wake_mask || thread_info->changed_mask != changed_mask)
2992 SERVER_START_REQ( set_queue_mask )
2994 req->wake_mask = wake_mask;
2995 req->changed_mask = changed_mask;
2996 req->skip_wait = 0;
2997 wine_server_call( req );
2999 SERVER_END_REQ;
3000 thread_info->wake_mask = wake_mask;
3001 thread_info->changed_mask = changed_mask;
3004 ret = wait_message( count, handles, timeout, changed_mask, flags );
3006 if (ret != WAIT_TIMEOUT) thread_info->wake_mask = thread_info->changed_mask = 0;
3007 return ret;
3010 static HANDLE normalize_std_handle( HANDLE handle )
3012 if (handle == (HANDLE)STD_INPUT_HANDLE)
3013 return NtCurrentTeb()->Peb->ProcessParameters->hStdInput;
3014 if (handle == (HANDLE)STD_OUTPUT_HANDLE)
3015 return NtCurrentTeb()->Peb->ProcessParameters->hStdOutput;
3016 if (handle == (HANDLE)STD_ERROR_HANDLE)
3017 return NtCurrentTeb()->Peb->ProcessParameters->hStdError;
3019 return handle;
3022 /***********************************************************************
3023 * NtUserMsgWaitForMultipleObjectsEx (win32u.@)
3025 DWORD WINAPI NtUserMsgWaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
3026 DWORD timeout, DWORD mask, DWORD flags )
3028 HANDLE wait_handles[MAXIMUM_WAIT_OBJECTS];
3029 DWORD i;
3031 if (count > MAXIMUM_WAIT_OBJECTS-1)
3033 RtlSetLastWin32Error( ERROR_INVALID_PARAMETER );
3034 return WAIT_FAILED;
3037 /* add the queue to the handle list */
3038 for (i = 0; i < count; i++) wait_handles[i] = normalize_std_handle( handles[i] );
3039 wait_handles[count] = get_server_queue_handle();
3041 return wait_objects( count+1, wait_handles, timeout,
3042 (flags & MWMO_INPUTAVAILABLE) ? mask : 0, mask, flags );
3045 /***********************************************************************
3046 * NtUserWaitForInputIdle (win32u.@)
3048 DWORD WINAPI NtUserWaitForInputIdle( HANDLE process, DWORD timeout, BOOL wow )
3050 DWORD start_time, elapsed, ret;
3051 HANDLE handles[2];
3053 handles[0] = process;
3054 SERVER_START_REQ( get_process_idle_event )
3056 req->handle = wine_server_obj_handle( process );
3057 wine_server_call_err( req );
3058 handles[1] = wine_server_ptr_handle( reply->event );
3060 SERVER_END_REQ;
3061 if (!handles[1]) return WAIT_FAILED; /* no event to wait on */
3063 start_time = NtGetTickCount();
3064 elapsed = 0;
3066 TRACE("waiting for %p\n", handles[1] );
3068 for (;;)
3070 ret = NtUserMsgWaitForMultipleObjectsEx( 2, handles, timeout - elapsed, QS_SENDMESSAGE, 0 );
3071 switch (ret)
3073 case WAIT_OBJECT_0:
3074 return 0;
3075 case WAIT_OBJECT_0+2:
3076 process_sent_messages();
3077 break;
3078 case WAIT_TIMEOUT:
3079 case WAIT_FAILED:
3080 TRACE("timeout or error\n");
3081 return ret;
3082 default:
3083 TRACE("finished\n");
3084 return 0;
3086 if (timeout != INFINITE)
3088 elapsed = NtGetTickCount() - start_time;
3089 if (elapsed > timeout)
3090 break;
3094 return WAIT_TIMEOUT;
3097 /***********************************************************************
3098 * NtUserWaitMessage (win32u.@)
3100 BOOL WINAPI NtUserWaitMessage(void)
3102 return NtUserMsgWaitForMultipleObjectsEx( 0, NULL, INFINITE, QS_ALLINPUT, 0 ) != WAIT_FAILED;
3105 /***********************************************************************
3106 * NtUserPeekMessage (win32u.@)
3108 BOOL WINAPI NtUserPeekMessage( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
3110 MSG msg;
3111 int ret;
3113 user_check_not_lock();
3114 check_for_driver_events( 0 );
3116 ret = peek_message( &msg, hwnd, first, last, flags, 0 );
3117 if (ret < 0) return FALSE;
3119 if (!ret)
3121 flush_window_surfaces( TRUE );
3122 ret = wait_message( 0, NULL, 0, QS_ALLINPUT, 0 );
3123 /* if we received driver events, check again for a pending message */
3124 if (ret == WAIT_TIMEOUT || peek_message( &msg, hwnd, first, last, flags, 0 ) <= 0) return FALSE;
3127 check_for_driver_events( msg.message );
3129 /* copy back our internal safe copy of message data to msg_out.
3130 * msg_out is a variable from the *program*, so it can't be used
3131 * internally as it can get "corrupted" by our use of SendMessage()
3132 * (back to the program) inside the message handling itself. */
3133 if (!msg_out)
3135 RtlSetLastWin32Error( ERROR_NOACCESS );
3136 return FALSE;
3138 *msg_out = msg;
3139 return TRUE;
3142 /***********************************************************************
3143 * NtUserGetMessage (win32u.@)
3145 BOOL WINAPI NtUserGetMessage( MSG *msg, HWND hwnd, UINT first, UINT last )
3147 HANDLE server_queue = get_server_queue_handle();
3148 unsigned int mask = QS_POSTMESSAGE | QS_SENDMESSAGE; /* Always selected */
3149 int ret;
3151 user_check_not_lock();
3152 check_for_driver_events( 0 );
3154 if (first || last)
3156 if ((first <= WM_KEYLAST) && (last >= WM_KEYFIRST)) mask |= QS_KEY;
3157 if ( ((first <= WM_MOUSELAST) && (last >= WM_MOUSEFIRST)) ||
3158 ((first <= WM_NCMOUSELAST) && (last >= WM_NCMOUSEFIRST)) ) mask |= QS_MOUSE;
3159 if ((first <= WM_TIMER) && (last >= WM_TIMER)) mask |= QS_TIMER;
3160 if ((first <= WM_SYSTIMER) && (last >= WM_SYSTIMER)) mask |= QS_TIMER;
3161 if ((first <= WM_PAINT) && (last >= WM_PAINT)) mask |= QS_PAINT;
3163 else mask = QS_ALLINPUT;
3165 while (!(ret = peek_message( msg, hwnd, first, last, PM_REMOVE | (mask << 16), mask )))
3167 wait_objects( 1, &server_queue, INFINITE, mask & (QS_SENDMESSAGE | QS_SMRESULT), mask, 0 );
3169 if (ret < 0) return -1;
3171 check_for_driver_events( msg->message );
3173 return msg->message != WM_QUIT;
3176 /***********************************************************************
3177 * put_message_in_queue
3179 * Put a sent message into the destination queue.
3180 * For inter-process message, reply_size is set to expected size of reply data.
3182 static BOOL put_message_in_queue( const struct send_message_info *info, size_t *reply_size )
3184 struct packed_message data;
3185 message_data_t msg_data;
3186 unsigned int res;
3187 int i;
3188 timeout_t timeout = TIMEOUT_INFINITE;
3190 /* Check for INFINITE timeout for compatibility with Win9x,
3191 * although Windows >= NT does not do so
3193 if (info->type != MSG_NOTIFY &&
3194 info->type != MSG_CALLBACK &&
3195 info->type != MSG_POSTED &&
3196 info->timeout &&
3197 info->timeout != INFINITE)
3199 /* timeout is signed despite the prototype */
3200 timeout = (timeout_t)max( 0, (int)info->timeout ) * -10000;
3203 memset( &data, 0, sizeof(data) );
3204 if (info->type == MSG_OTHER_PROCESS || info->type == MSG_NOTIFY)
3206 *reply_size = pack_message( info->hwnd, info->msg, info->wparam, info->lparam, &data );
3207 if (data.count == -1)
3209 WARN( "cannot pack message %x\n", info->msg );
3210 return FALSE;
3213 else if (info->type == MSG_CALLBACK)
3215 msg_data.callback.callback = wine_server_client_ptr( info->callback );
3216 msg_data.callback.data = info->data;
3217 msg_data.callback.result = 0;
3218 data.data[0] = &msg_data;
3219 data.size[0] = sizeof(msg_data.callback);
3220 data.count = 1;
3222 else if (info->type == MSG_POSTED && info->msg >= WM_DDE_FIRST && info->msg <= WM_DDE_LAST)
3224 struct post_dde_message_params params;
3225 void *ret_ptr;
3226 ULONG ret_len;
3228 params.hwnd = info->hwnd;
3229 params.msg = info->msg;
3230 params.wparam = info->wparam;
3231 params.lparam = info->lparam;
3232 params.dest_tid = info->dest_tid;
3233 params.type = info->type;
3234 return KeUserModeCallback( NtUserPostDDEMessage, &params, sizeof(params), &ret_ptr, &ret_len );
3237 SERVER_START_REQ( send_message )
3239 req->id = info->dest_tid;
3240 req->type = info->type;
3241 req->flags = 0;
3242 req->win = wine_server_user_handle( info->hwnd );
3243 req->msg = info->msg;
3244 req->wparam = info->wparam;
3245 req->lparam = info->lparam;
3246 req->timeout = timeout;
3248 if (info->flags & SMTO_ABORTIFHUNG) req->flags |= SEND_MSG_ABORT_IF_HUNG;
3249 for (i = 0; i < data.count; i++) wine_server_add_data( req, data.data[i], data.size[i] );
3250 if ((res = wine_server_call( req )))
3252 if (res == STATUS_INVALID_PARAMETER)
3253 /* FIXME: find a STATUS_ value for this one */
3254 RtlSetLastWin32Error( ERROR_INVALID_THREAD_ID );
3255 else
3256 RtlSetLastWin32Error( RtlNtStatusToDosError(res) );
3259 SERVER_END_REQ;
3260 return !res;
3263 /***********************************************************************
3264 * wait_message_reply
3266 * Wait until a sent message gets replied to.
3268 static void wait_message_reply( UINT flags )
3270 struct user_thread_info *thread_info = get_user_thread_info();
3271 HANDLE server_queue = get_server_queue_handle();
3272 unsigned int wake_mask = QS_SMRESULT | ((flags & SMTO_BLOCK) ? 0 : QS_SENDMESSAGE);
3274 for (;;)
3276 unsigned int wake_bits = 0;
3278 SERVER_START_REQ( set_queue_mask )
3280 req->wake_mask = wake_mask;
3281 req->changed_mask = wake_mask;
3282 req->skip_wait = 1;
3283 if (!wine_server_call( req )) wake_bits = reply->wake_bits & wake_mask;
3285 SERVER_END_REQ;
3287 thread_info->wake_mask = thread_info->changed_mask = 0;
3289 if (wake_bits & QS_SMRESULT) return; /* got a result */
3290 if (wake_bits & QS_SENDMESSAGE)
3292 /* Process the sent message immediately */
3293 process_sent_messages();
3294 continue;
3297 wait_message( 1, &server_queue, INFINITE, wake_mask, 0 );
3301 /***********************************************************************
3302 * retrieve_reply
3304 * Retrieve a message reply from the server.
3306 static LRESULT retrieve_reply( const struct send_message_info *info,
3307 size_t reply_size, LRESULT *result )
3309 unsigned int status;
3310 void *reply_data = NULL;
3312 if (reply_size)
3314 if (!(reply_data = malloc( reply_size )))
3316 WARN( "no memory for reply, will be truncated\n" );
3317 reply_size = 0;
3320 SERVER_START_REQ( get_message_reply )
3322 req->cancel = 1;
3323 if (reply_size) wine_server_set_reply( req, reply_data, reply_size );
3324 if (!(status = wine_server_call( req ))) *result = reply->result;
3325 reply_size = wine_server_reply_size( reply );
3327 SERVER_END_REQ;
3328 if (!status && reply_size)
3329 unpack_reply( info->hwnd, info->msg, info->wparam, info->lparam, reply_data, reply_size );
3331 free( reply_data );
3333 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx got reply %lx (err=%d)\n",
3334 info->hwnd, info->msg, debugstr_msg_name(info->msg, info->hwnd), (long)info->wparam,
3335 info->lparam, *result, status );
3337 /* MSDN states that last error is 0 on timeout, but at least NT4 returns ERROR_TIMEOUT */
3338 if (status) RtlSetLastWin32Error( RtlNtStatusToDosError(status) );
3339 return !status;
3342 /***********************************************************************
3343 * send_inter_thread_message
3345 static LRESULT send_inter_thread_message( const struct send_message_info *info, LRESULT *res_ptr )
3347 size_t reply_size = 0;
3349 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
3350 info->hwnd, info->msg, debugstr_msg_name(info->msg, info->hwnd),
3351 (long)info->wparam, info->lparam );
3353 user_check_not_lock();
3355 if (!put_message_in_queue( info, &reply_size )) return 0;
3357 /* there's no reply to wait for on notify/callback messages */
3358 if (info->type == MSG_NOTIFY || info->type == MSG_CALLBACK) return 1;
3360 wait_message_reply( info->flags );
3361 return retrieve_reply( info, reply_size, res_ptr );
3364 static LRESULT send_inter_thread_callback( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
3365 LRESULT *result, void *arg )
3367 struct send_message_info *info = arg;
3368 info->hwnd = hwnd;
3369 info->msg = msg;
3370 info->wparam = wp;
3371 info->lparam = lp;
3372 return send_inter_thread_message( info, result );
3375 /***********************************************************************
3376 * send_internal_message_timeout
3378 * Same as SendMessageTimeoutW but sends the message to a specific thread
3379 * without requiring a window handle. Only works for internal Wine messages.
3381 LRESULT send_internal_message_timeout( DWORD dest_pid, DWORD dest_tid,
3382 UINT msg, WPARAM wparam, LPARAM lparam,
3383 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
3385 LRESULT ret, result = 0;
3387 assert( msg & 0x80000000 ); /* must be an internal Wine message */
3389 if (is_exiting_thread( dest_tid )) return 0;
3391 if (dest_tid == GetCurrentThreadId())
3393 result = handle_internal_message( 0, msg, wparam, lparam );
3394 ret = 1;
3396 else
3398 struct send_message_info info;
3400 info.type = dest_pid == GetCurrentProcessId() ? MSG_UNICODE : MSG_OTHER_PROCESS;
3401 info.dest_tid = dest_tid;
3402 info.hwnd = 0;
3403 info.msg = msg;
3404 info.wparam = wparam;
3405 info.lparam = lparam;
3406 info.flags = flags;
3407 info.timeout = timeout;
3408 info.params = NULL;
3410 ret = send_inter_thread_message( &info, &result );
3412 if (ret && res_ptr) *res_ptr = result;
3413 return ret;
3416 /***********************************************************************
3417 * send_hardware_message
3419 NTSTATUS send_hardware_message( HWND hwnd, const INPUT *input, const RAWINPUT *rawinput, UINT flags )
3421 struct send_message_info info;
3422 int prev_x, prev_y, new_x, new_y;
3423 USAGE hid_usage_page, hid_usage;
3424 NTSTATUS ret;
3425 BOOL wait, affects_key_state = FALSE;
3427 info.type = MSG_HARDWARE;
3428 info.dest_tid = 0;
3429 info.hwnd = hwnd;
3430 info.flags = 0;
3431 info.timeout = 0;
3432 info.params = NULL;
3434 if (input->type == INPUT_MOUSE && (input->mi.dwFlags & (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_RIGHTDOWN)))
3435 clip_fullscreen_window( hwnd, FALSE );
3437 if (input->type == INPUT_HARDWARE && rawinput->header.dwType == RIM_TYPEHID)
3439 if (input->hi.uMsg == WM_INPUT_DEVICE_CHANGE)
3441 hid_usage_page = ((USAGE *)rawinput->data.hid.bRawData)[0];
3442 hid_usage = ((USAGE *)rawinput->data.hid.bRawData)[1];
3444 if (input->hi.uMsg == WM_INPUT &&
3445 !rawinput_device_get_usages( rawinput->header.hDevice, &hid_usage_page, &hid_usage ))
3447 WARN( "unable to get HID usages for device %p\n", rawinput->header.hDevice );
3448 return STATUS_INVALID_HANDLE;
3452 SERVER_START_REQ( send_hardware_message )
3454 req->win = wine_server_user_handle( hwnd );
3455 req->flags = flags;
3456 req->input.type = input->type;
3457 switch (input->type)
3459 case INPUT_MOUSE:
3460 req->input.mouse.x = input->mi.dx;
3461 req->input.mouse.y = input->mi.dy;
3462 req->input.mouse.data = input->mi.mouseData;
3463 req->input.mouse.flags = input->mi.dwFlags;
3464 req->input.mouse.time = input->mi.time;
3465 req->input.mouse.info = input->mi.dwExtraInfo;
3466 affects_key_state = !!(input->mi.dwFlags & (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP |
3467 MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP |
3468 MOUSEEVENTF_MIDDLEDOWN | MOUSEEVENTF_MIDDLEUP |
3469 MOUSEEVENTF_XDOWN | MOUSEEVENTF_XUP));
3470 break;
3471 case INPUT_KEYBOARD:
3472 req->input.kbd.vkey = input->ki.wVk;
3473 req->input.kbd.scan = input->ki.wScan;
3474 req->input.kbd.flags = input->ki.dwFlags;
3475 req->input.kbd.time = input->ki.time;
3476 req->input.kbd.info = input->ki.dwExtraInfo;
3477 affects_key_state = TRUE;
3478 break;
3479 case INPUT_HARDWARE:
3480 req->input.hw.msg = input->hi.uMsg;
3481 req->input.hw.lparam = MAKELONG( input->hi.wParamL, input->hi.wParamH );
3482 switch (input->hi.uMsg)
3484 case WM_INPUT:
3485 case WM_INPUT_DEVICE_CHANGE:
3486 req->input.hw.rawinput.type = rawinput->header.dwType;
3487 switch (rawinput->header.dwType)
3489 case RIM_TYPEHID:
3490 req->input.hw.rawinput.hid.device = HandleToUlong( rawinput->header.hDevice );
3491 req->input.hw.rawinput.hid.param = rawinput->header.wParam;
3492 req->input.hw.rawinput.hid.usage_page = hid_usage_page;
3493 req->input.hw.rawinput.hid.usage = hid_usage;
3494 req->input.hw.rawinput.hid.count = rawinput->data.hid.dwCount;
3495 req->input.hw.rawinput.hid.length = rawinput->data.hid.dwSizeHid;
3496 wine_server_add_data( req, rawinput->data.hid.bRawData,
3497 rawinput->data.hid.dwCount * rawinput->data.hid.dwSizeHid );
3498 break;
3499 default:
3500 assert( 0 );
3501 break;
3504 break;
3506 ret = wine_server_call( req );
3507 wait = reply->wait;
3508 prev_x = reply->prev_x;
3509 prev_y = reply->prev_y;
3510 new_x = reply->new_x;
3511 new_y = reply->new_y;
3513 SERVER_END_REQ;
3515 if (!ret)
3517 if (affects_key_state)
3518 InterlockedIncrement( &global_key_state_counter ); /* force refreshing the key state cache */
3519 if ((flags & SEND_HWMSG_INJECTED) && (prev_x != new_x || prev_y != new_y))
3520 user_driver->pSetCursorPos( new_x, new_y );
3523 if (wait)
3525 LRESULT ignored;
3526 wait_message_reply( 0 );
3527 retrieve_reply( &info, 0, &ignored );
3529 return ret;
3532 /**********************************************************************
3533 * NtUserDispatchMessage (win32u.@)
3535 LRESULT WINAPI NtUserDispatchMessage( const MSG *msg )
3537 struct win_proc_params params;
3538 LRESULT retval = 0;
3540 /* Process timer messages */
3541 if (msg->lParam && msg->message == WM_TIMER)
3543 params.func = (WNDPROC)msg->lParam;
3544 if (!init_win_proc_params( &params, msg->hwnd, msg->message,
3545 msg->wParam, NtGetTickCount(), FALSE ))
3546 return 0;
3547 return dispatch_win_proc_params( &params, sizeof(params), NULL, NULL );
3549 if (msg->message == WM_SYSTIMER)
3551 switch (msg->wParam)
3553 case SYSTEM_TIMER_CARET:
3554 toggle_caret( msg->hwnd );
3555 return 0;
3557 case SYSTEM_TIMER_TRACK_MOUSE:
3558 update_mouse_tracking_info( msg->hwnd );
3559 return 0;
3563 if (!msg->hwnd) return 0;
3565 spy_enter_message( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message, msg->wParam, msg->lParam );
3567 if (init_window_call_params( &params, msg->hwnd, msg->message, msg->wParam, msg->lParam,
3568 FALSE, WMCHAR_MAP_DISPATCHMESSAGE ))
3569 retval = dispatch_win_proc_params( &params, sizeof(params), NULL, NULL );
3570 else if (!is_window( msg->hwnd )) RtlSetLastWin32Error( ERROR_INVALID_WINDOW_HANDLE );
3571 else RtlSetLastWin32Error( ERROR_MESSAGE_SYNC_ONLY );
3573 spy_exit_message( SPY_RESULT_OK, msg->hwnd, msg->message, retval, msg->wParam, msg->lParam );
3575 if (msg->message == WM_PAINT)
3577 /* send a WM_NCPAINT and WM_ERASEBKGND if the non-client area is still invalid */
3578 HRGN hrgn = NtGdiCreateRectRgn( 0, 0, 0, 0 );
3579 NtUserGetUpdateRgn( msg->hwnd, hrgn, TRUE );
3580 NtGdiDeleteObjectApp( hrgn );
3582 return retval;
3585 static BOOL is_message_broadcastable( UINT msg )
3587 return msg < WM_USER || msg >= 0xc000;
3590 /***********************************************************************
3591 * broadcast_message
3593 static BOOL broadcast_message( struct send_message_info *info, DWORD_PTR *res_ptr )
3595 HWND *list;
3597 if (is_message_broadcastable( info->msg ) &&
3598 (list = list_window_children( 0, get_desktop_window(), NULL, 0 )))
3600 int i;
3602 for (i = 0; list[i]; i++)
3604 if (!is_window(list[i])) continue;
3605 if ((get_window_long( list[i], GWL_STYLE ) & (WS_POPUP|WS_CHILD)) == WS_CHILD)
3606 continue;
3608 switch(info->type)
3610 case MSG_UNICODE:
3611 case MSG_OTHER_PROCESS:
3612 send_message_timeout( list[i], info->msg, info->wparam, info->lparam,
3613 info->flags, info->timeout, FALSE );
3614 break;
3615 case MSG_ASCII:
3616 send_message_timeout( list[i], info->msg, info->wparam, info->lparam,
3617 info->flags, info->timeout, TRUE );
3618 break;
3619 case MSG_NOTIFY:
3620 NtUserMessageCall( list[i], info->msg, info->wparam, info->lparam,
3621 0, NtUserSendNotifyMessage, FALSE );
3622 break;
3623 case MSG_CALLBACK:
3625 struct send_message_callback_params params =
3626 { .callback = info->callback, .data = info->data };
3627 NtUserMessageCall( list[i], info->msg, info->wparam, info->lparam,
3628 &params, NtUserSendMessageCallback, FALSE );
3629 break;
3631 case MSG_POSTED:
3632 NtUserPostMessage( list[i], info->msg, info->wparam, info->lparam );
3633 break;
3634 default:
3635 ERR( "bad type %d\n", info->type );
3636 break;
3640 free( list );
3643 if (res_ptr) *res_ptr = 1;
3644 return TRUE;
3647 static inline void *get_buffer( void *static_buffer, size_t size, size_t need )
3649 if (size >= need) return static_buffer;
3650 return malloc( need );
3653 static inline void free_buffer( void *static_buffer, void *buffer )
3655 if (buffer != static_buffer) free( buffer );
3658 typedef LRESULT (*winproc_callback_t)( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
3659 LRESULT *result, void *arg );
3661 /**********************************************************************
3662 * test_lb_for_string
3664 * Return TRUE if the lparam is a string
3666 static inline BOOL test_lb_for_string( HWND hwnd, UINT msg )
3668 DWORD style = get_window_long( hwnd, GWL_STYLE );
3669 if (msg <= CB_MSGMAX)
3670 return (!(style & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE)) || (style & CBS_HASSTRINGS));
3671 else
3672 return (!(style & (LBS_OWNERDRAWFIXED | LBS_OWNERDRAWVARIABLE)) || (style & LBS_HASSTRINGS));
3676 static CPTABLEINFO *get_input_codepage( void )
3678 const NLS_LOCALE_DATA *locale;
3679 HKL hkl = NtUserGetKeyboardLayout( 0 );
3680 CPTABLEINFO *ret = NULL;
3682 locale = get_locale_data( LOWORD(hkl) );
3683 if (locale && locale->idefaultansicodepage != CP_UTF8)
3684 ret = get_cptable( locale->idefaultansicodepage );
3685 return ret ? ret : &ansi_cp;
3688 /***********************************************************************
3689 * map_wparam_AtoW
3691 * Convert the wparam of an ASCII message to Unicode.
3693 static BOOL map_wparam_AtoW( UINT message, WPARAM *wparam, enum wm_char_mapping mapping )
3695 char ch[2];
3696 WCHAR wch[2];
3698 wch[0] = wch[1] = 0;
3699 switch(message)
3701 case WM_CHARTOITEM:
3702 case EM_SETPASSWORDCHAR:
3703 case WM_DEADCHAR:
3704 case WM_SYSCHAR:
3705 case WM_SYSDEADCHAR:
3706 case WM_MENUCHAR:
3707 ch[0] = LOBYTE(*wparam);
3708 ch[1] = HIBYTE(*wparam);
3709 win32u_mbtowc( get_input_codepage(), wch, 2, ch, 2 );
3710 *wparam = MAKEWPARAM(wch[0], wch[1]);
3711 break;
3713 case WM_IME_CHAR:
3714 ch[0] = HIBYTE(*wparam);
3715 ch[1] = LOBYTE(*wparam);
3716 if (ch[0]) win32u_mbtowc( get_input_codepage(), wch, 2, ch, 2 );
3717 else win32u_mbtowc( get_input_codepage(), wch, 1, ch + 1, 1 );
3718 *wparam = MAKEWPARAM(wch[0], HIWORD(*wparam));
3719 break;
3721 return TRUE;
3724 /**********************************************************************
3725 * call_messageAtoW
3727 * Call a window procedure, translating args from Ansi to Unicode.
3729 static LRESULT call_messageAtoW( winproc_callback_t callback, HWND hwnd, UINT msg, WPARAM wparam,
3730 LPARAM lparam, LRESULT *result, void *arg, enum wm_char_mapping mapping )
3732 LRESULT ret = 0;
3734 TRACE( "(hwnd=%p,msg=%s,wp=%#lx,lp=%#lx)\n", hwnd, debugstr_msg_name( msg, hwnd ),
3735 (long)wparam, (long)lparam );
3737 switch(msg)
3739 case WM_MDICREATE:
3741 WCHAR *ptr, buffer[512];
3742 DWORD title_lenA = 0, title_lenW = 0, class_lenA = 0, class_lenW = 0;
3743 MDICREATESTRUCTA *csA = (MDICREATESTRUCTA *)lparam;
3744 MDICREATESTRUCTW csW;
3746 memcpy( &csW, csA, sizeof(csW) );
3748 if (!IS_INTRESOURCE(csA->szTitle))
3750 title_lenA = strlen(csA->szTitle) + 1;
3751 title_lenW = win32u_mbtowc_size( &ansi_cp, csA->szTitle, title_lenA );
3753 if (!IS_INTRESOURCE(csA->szClass))
3755 class_lenA = strlen(csA->szClass) + 1;
3756 class_lenW = win32u_mbtowc_size( &ansi_cp, csA->szClass, class_lenA );
3759 if (!(ptr = get_buffer( buffer, sizeof(buffer), (title_lenW + class_lenW) * sizeof(WCHAR) )))
3760 break;
3762 if (title_lenW)
3764 csW.szTitle = ptr;
3765 win32u_mbtowc( &ansi_cp, ptr, title_lenW, csA->szTitle, title_lenA );
3767 if (class_lenW)
3769 csW.szClass = ptr + title_lenW;
3770 win32u_mbtowc( &ansi_cp, ptr + title_lenW, class_lenW, csA->szClass, class_lenA );
3772 ret = callback( hwnd, msg, wparam, (LPARAM)&csW, result, arg );
3773 free_buffer( buffer, ptr );
3775 break;
3777 case WM_GETTEXT:
3778 case WM_ASKCBFORMATNAME:
3780 WCHAR *ptr, buffer[512];
3781 LPSTR str = (LPSTR)lparam;
3782 DWORD len = wparam * sizeof(WCHAR);
3784 if (!(ptr = get_buffer( buffer, sizeof(buffer), len ))) break;
3785 ret = callback( hwnd, msg, wparam, (LPARAM)ptr, result, arg );
3786 if (wparam)
3788 len = 0;
3789 len = *result ? win32u_wctomb( &ansi_cp, str, wparam - 1, ptr, *result ) : 0;
3790 str[len] = 0;
3791 *result = len;
3793 free_buffer( buffer, ptr );
3795 break;
3797 case LB_ADDSTRING:
3798 case LB_INSERTSTRING:
3799 case LB_FINDSTRING:
3800 case LB_FINDSTRINGEXACT:
3801 case LB_SELECTSTRING:
3802 case CB_ADDSTRING:
3803 case CB_INSERTSTRING:
3804 case CB_FINDSTRING:
3805 case CB_FINDSTRINGEXACT:
3806 case CB_SELECTSTRING:
3807 if (!lparam || !test_lb_for_string( hwnd, msg ))
3809 ret = callback( hwnd, msg, wparam, lparam, result, arg );
3810 break;
3812 /* fall through */
3813 case WM_SETTEXT:
3814 case WM_WININICHANGE:
3815 case WM_DEVMODECHANGE:
3816 case CB_DIR:
3817 case LB_DIR:
3818 case LB_ADDFILE:
3819 case EM_REPLACESEL:
3820 if (!lparam)
3822 ret = callback( hwnd, msg, wparam, lparam, result, arg );
3824 else
3826 WCHAR *ptr, buffer[512];
3827 LPCSTR strA = (LPCSTR)lparam;
3828 DWORD lenW, lenA = strlen(strA) + 1;
3830 lenW = win32u_mbtowc_size( &ansi_cp, strA, lenA );
3831 if ((ptr = get_buffer( buffer, sizeof(buffer), lenW * sizeof(WCHAR) )))
3833 win32u_mbtowc( &ansi_cp, ptr, lenW, strA, lenA );
3834 ret = callback( hwnd, msg, wparam, (LPARAM)ptr, result, arg );
3835 free_buffer( buffer, ptr );
3838 break;
3840 case EM_GETLINE:
3842 WCHAR *ptr, buffer[512];
3843 WORD len = *(WORD *)lparam;
3845 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * sizeof(WCHAR) ))) break;
3846 *((WORD *)ptr) = len; /* store the length */
3847 ret = callback( hwnd, msg, wparam, (LPARAM)ptr, result, arg );
3848 if (*result)
3850 DWORD reslen;
3851 reslen = win32u_wctomb( &ansi_cp, (char *)lparam, len, ptr, *result );
3852 if (reslen < len) ((LPSTR)lparam)[reslen] = 0;
3853 *result = reslen;
3855 free_buffer( buffer, ptr );
3857 break;
3859 case WM_GETDLGCODE:
3860 if (lparam)
3862 MSG newmsg = *(MSG *)lparam;
3863 if (map_wparam_AtoW( newmsg.message, &newmsg.wParam, WMCHAR_MAP_NOMAPPING ))
3864 ret = callback( hwnd, msg, wparam, (LPARAM)&newmsg, result, arg );
3866 else ret = callback( hwnd, msg, wparam, lparam, result, arg );
3867 break;
3869 case WM_CHARTOITEM:
3870 case WM_MENUCHAR:
3871 case WM_DEADCHAR:
3872 case WM_SYSCHAR:
3873 case WM_SYSDEADCHAR:
3874 case EM_SETPASSWORDCHAR:
3875 case WM_IME_CHAR:
3876 if (map_wparam_AtoW( msg, &wparam, mapping ))
3877 ret = callback( hwnd, msg, wparam, lparam, result, arg );
3878 break;
3880 case WM_GETTEXTLENGTH:
3881 case CB_GETLBTEXTLEN:
3882 case LB_GETTEXTLEN:
3883 ret = callback( hwnd, msg, wparam, lparam, result, arg );
3884 if (*result >= 0)
3886 WCHAR *ptr, buffer[512];
3887 LRESULT res;
3888 DWORD len = *result + 1;
3889 /* Determine respective GETTEXT message */
3890 UINT msg_get_text = msg == WM_GETTEXTLENGTH ? WM_GETTEXT :
3891 (msg == CB_GETLBTEXTLEN ? CB_GETLBTEXT : LB_GETTEXT);
3892 /* wparam differs between the messages */
3893 WPARAM wp = msg == WM_GETTEXTLENGTH ? len : wparam;
3895 if (!(ptr = get_buffer( buffer, sizeof(buffer), len * sizeof(WCHAR) ))) break;
3897 res = send_message( hwnd, msg_get_text, wp, (LPARAM)ptr );
3898 *result = win32u_wctomb_size( &ansi_cp, ptr, res );
3899 free_buffer( buffer, ptr );
3901 break;
3903 default:
3904 ret = callback( hwnd, msg, wparam, lparam, result, arg );
3905 break;
3907 return ret;
3910 /***********************************************************************
3911 * process_message
3913 * Backend implementation of the various SendMessage functions.
3915 static BOOL process_message( struct send_message_info *info, DWORD_PTR *res_ptr, BOOL ansi )
3917 struct ntuser_thread_info *thread_info = NtUserGetThreadInfo();
3918 INPUT_MESSAGE_SOURCE prev_source = thread_info->msg_source;
3919 DWORD dest_pid;
3920 BOOL ret;
3921 LRESULT result = 0;
3923 if (is_broadcast( info->hwnd )) return broadcast_message( info, res_ptr );
3925 if (!(info->dest_tid = get_window_thread( info->hwnd, &dest_pid ))) return FALSE;
3926 if (is_exiting_thread( info->dest_tid )) return FALSE;
3928 if (info->params && info->dest_tid == GetCurrentThreadId() &&
3929 !is_hooked( WH_CALLWNDPROC ) && !is_hooked( WH_CALLWNDPROCRET ) &&
3930 thread_info->recursion_count <= MAX_WINPROC_RECURSION)
3932 /* if we're called from client side and need just a simple winproc call,
3933 * just fill dispatch params and let user32 do the rest */
3934 return init_window_call_params( info->params, info->hwnd, info->msg, info->wparam, info->lparam,
3935 ansi, info->wm_char );
3938 thread_info->msg_source = msg_source_unavailable;
3939 spy_enter_message( SPY_SENDMESSAGE, info->hwnd, info->msg, info->wparam, info->lparam );
3941 if (info->dest_tid != GetCurrentThreadId())
3943 if (dest_pid != GetCurrentProcessId() && (info->type == MSG_ASCII || info->type == MSG_UNICODE))
3944 info->type = MSG_OTHER_PROCESS;
3946 /* MSG_ASCII can be sent unconverted except for WM_CHAR; everything else needs to be Unicode */
3947 if (ansi && (info->type != MSG_ASCII || info->msg == WM_CHAR))
3948 ret = call_messageAtoW( send_inter_thread_callback, info->hwnd, info->msg,
3949 info->wparam, info->lparam, &result, info, info->wm_char );
3950 else
3951 ret = send_inter_thread_message( info, &result );
3953 else
3955 result = call_window_proc( info->hwnd, info->msg, info->wparam, info->lparam,
3956 info->type, TRUE, info->wm_char, ansi );
3957 if (info->type == MSG_CALLBACK)
3958 call_sendmsg_callback( info->callback, info->hwnd, info->msg, info->data, result );
3959 ret = TRUE;
3962 spy_exit_message( SPY_RESULT_OK, info->hwnd, info->msg, result, info->wparam, info->lparam );
3963 thread_info->msg_source = prev_source;
3964 if (ret && res_ptr) *res_ptr = result;
3965 return ret;
3968 /***********************************************************************
3969 * NtUserSetTimer (win32u.@)
3971 UINT_PTR WINAPI NtUserSetTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc, ULONG tolerance )
3973 UINT_PTR ret;
3974 WNDPROC winproc = 0;
3976 if (proc) winproc = alloc_winproc( (WNDPROC)proc, TRUE );
3978 timeout = min( max( USER_TIMER_MINIMUM, timeout ), USER_TIMER_MAXIMUM );
3980 SERVER_START_REQ( set_win_timer )
3982 req->win = wine_server_user_handle( hwnd );
3983 req->msg = WM_TIMER;
3984 req->id = id;
3985 req->rate = timeout;
3986 req->lparam = (ULONG_PTR)winproc;
3987 if (!wine_server_call_err( req ))
3989 ret = reply->id;
3990 if (!ret) ret = TRUE;
3992 else ret = 0;
3994 SERVER_END_REQ;
3996 TRACE( "Added %p %lx %p timeout %d\n", hwnd, (long)id, winproc, timeout );
3997 return ret;
4000 /***********************************************************************
4001 * NtUserSetSystemTimer (win32u.@)
4003 UINT_PTR WINAPI NtUserSetSystemTimer( HWND hwnd, UINT_PTR id, UINT timeout )
4005 UINT_PTR ret;
4007 TRACE( "window %p, id %#lx, timeout %u\n", hwnd, (long)id, timeout );
4009 timeout = min( max( USER_TIMER_MINIMUM, timeout ), USER_TIMER_MAXIMUM );
4011 SERVER_START_REQ( set_win_timer )
4013 req->win = wine_server_user_handle( hwnd );
4014 req->msg = WM_SYSTIMER;
4015 req->id = id;
4016 req->rate = timeout;
4017 req->lparam = 0;
4018 if (!wine_server_call_err( req ))
4020 ret = reply->id;
4021 if (!ret) ret = TRUE;
4023 else ret = 0;
4025 SERVER_END_REQ;
4027 return ret;
4030 /***********************************************************************
4031 * NtUserKillTimer (win32u.@)
4033 BOOL WINAPI NtUserKillTimer( HWND hwnd, UINT_PTR id )
4035 BOOL ret;
4037 SERVER_START_REQ( kill_win_timer )
4039 req->win = wine_server_user_handle( hwnd );
4040 req->msg = WM_TIMER;
4041 req->id = id;
4042 ret = !wine_server_call_err( req );
4044 SERVER_END_REQ;
4045 return ret;
4048 /* see KillSystemTimer */
4049 BOOL kill_system_timer( HWND hwnd, UINT_PTR id )
4051 BOOL ret;
4053 SERVER_START_REQ( kill_win_timer )
4055 req->win = wine_server_user_handle( hwnd );
4056 req->msg = WM_SYSTIMER;
4057 req->id = id;
4058 ret = !wine_server_call_err( req );
4060 SERVER_END_REQ;
4061 return ret;
4064 static LRESULT send_window_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
4065 struct win_proc_params *client_params, BOOL ansi )
4067 struct send_message_info info;
4068 DWORD_PTR res = 0;
4070 info.type = ansi ? MSG_ASCII : MSG_UNICODE;
4071 info.hwnd = hwnd;
4072 info.msg = msg;
4073 info.wparam = wparam;
4074 info.lparam = lparam;
4075 info.flags = SMTO_NORMAL;
4076 info.timeout = 0;
4077 info.wm_char = WMCHAR_MAP_SENDMESSAGE;
4078 info.params = client_params;
4080 process_message( &info, &res, ansi );
4081 return res;
4084 /* see SendMessageTimeoutW */
4085 static LRESULT send_client_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
4086 UINT flags, UINT timeout, DWORD_PTR *res_ptr, BOOL ansi )
4088 struct send_message_info info;
4090 info.type = ansi ? MSG_ASCII : MSG_UNICODE;
4091 info.hwnd = hwnd;
4092 info.msg = msg;
4093 info.wparam = wparam;
4094 info.lparam = lparam;
4095 info.flags = flags;
4096 info.timeout = timeout;
4097 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
4098 info.params = NULL;
4100 return process_message( &info, res_ptr, ansi );
4103 LRESULT send_message_timeout( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
4104 UINT flags, UINT timeout, BOOL ansi )
4106 struct send_message_info info;
4107 DWORD_PTR res = 0;
4109 if (!is_pointer_message( msg, wparam ))
4111 send_client_message( hwnd, msg, wparam, lparam, flags, timeout, &res, ansi );
4112 return res;
4115 info.type = MSG_OTHER_PROCESS;
4116 info.hwnd = hwnd;
4117 info.msg = msg;
4118 info.wparam = wparam;
4119 info.lparam = lparam;
4120 info.flags = flags;
4121 info.timeout = timeout;
4122 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
4123 info.params = NULL;
4125 process_message( &info, &res, ansi );
4126 return res;
4129 /* see SendMessageW */
4130 LRESULT send_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
4132 return send_message_timeout( hwnd, msg, wparam, lparam, SMTO_NORMAL, 0, FALSE );
4135 /* see SendNotifyMessageW */
4136 BOOL send_notify_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam, BOOL ansi )
4138 struct send_message_info info;
4140 if (is_pointer_message( msg, wparam ))
4142 RtlSetLastWin32Error( ERROR_MESSAGE_SYNC_ONLY );
4143 return FALSE;
4146 info.type = MSG_NOTIFY;
4147 info.hwnd = hwnd;
4148 info.msg = msg;
4149 info.wparam = wparam;
4150 info.lparam = lparam;
4151 info.flags = 0;
4152 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
4153 info.params = NULL;
4155 return process_message( &info, NULL, ansi );
4158 /* see SendMessageCallbackW */
4159 static BOOL send_message_callback( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
4160 const struct send_message_callback_params *params, BOOL ansi )
4162 struct send_message_info info;
4164 if (is_pointer_message( msg, wparam ))
4166 RtlSetLastWin32Error( ERROR_MESSAGE_SYNC_ONLY );
4167 return FALSE;
4170 info.type = MSG_CALLBACK;
4171 info.hwnd = hwnd;
4172 info.msg = msg;
4173 info.wparam = wparam;
4174 info.lparam = lparam;
4175 info.callback = params->callback;
4176 info.data = params->data;
4177 info.flags = 0;
4178 info.wm_char = WMCHAR_MAP_SENDMESSAGETIMEOUT;
4179 info.params = NULL;
4181 return process_message( &info, NULL, ansi );
4184 /***********************************************************************
4185 * NtUserPostMessage (win32u.@)
4187 BOOL WINAPI NtUserPostMessage( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
4189 struct send_message_info info;
4191 if (is_pointer_message( msg, wparam ))
4193 RtlSetLastWin32Error( ERROR_MESSAGE_SYNC_ONLY );
4194 return FALSE;
4197 TRACE( "hwnd %p msg %x (%s) wp %lx lp %lx\n",
4198 hwnd, msg, debugstr_msg_name(msg, hwnd), (long)wparam, lparam );
4200 info.type = MSG_POSTED;
4201 info.hwnd = hwnd;
4202 info.msg = msg;
4203 info.wparam = wparam;
4204 info.lparam = lparam;
4205 info.flags = 0;
4206 info.params = NULL;
4208 if (is_broadcast(hwnd)) return broadcast_message( &info, NULL );
4210 if (!hwnd) return NtUserPostThreadMessage( GetCurrentThreadId(), msg, wparam, lparam );
4212 if (!(info.dest_tid = get_window_thread( hwnd, NULL ))) return FALSE;
4214 if (is_exiting_thread( info.dest_tid )) return TRUE;
4216 return put_message_in_queue( &info, NULL );
4219 /**********************************************************************
4220 * NtUserPostThreadMessage (win32u.@)
4222 BOOL WINAPI NtUserPostThreadMessage( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
4224 struct send_message_info info;
4226 if (is_pointer_message( msg, wparam ))
4228 RtlSetLastWin32Error( ERROR_MESSAGE_SYNC_ONLY );
4229 return FALSE;
4231 if (is_exiting_thread( thread )) return TRUE;
4233 info.type = MSG_POSTED;
4234 info.dest_tid = thread;
4235 info.hwnd = 0;
4236 info.msg = msg;
4237 info.wparam = wparam;
4238 info.lparam = lparam;
4239 info.flags = 0;
4240 info.params = NULL;
4241 return put_message_in_queue( &info, NULL );
4244 LRESULT WINAPI NtUserMessageCall( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
4245 void *result_info, DWORD type, BOOL ansi )
4247 switch (type)
4249 case NtUserScrollBarWndProc:
4250 return scroll_bar_window_proc( hwnd, msg, wparam, lparam, ansi );
4252 case NtUserPopupMenuWndProc:
4253 return popup_menu_window_proc( hwnd, msg, wparam, lparam );
4255 case NtUserDesktopWindowProc:
4256 return desktop_window_proc( hwnd, msg, wparam, lparam );
4258 case NtUserDefWindowProc:
4259 return default_window_proc( hwnd, msg, wparam, lparam, ansi );
4261 case NtUserCallWindowProc:
4262 return init_win_proc_params( (struct win_proc_params *)result_info, hwnd, msg,
4263 wparam, lparam, ansi );
4265 case NtUserSendMessage:
4266 return send_window_message( hwnd, msg, wparam, lparam, result_info, ansi );
4268 case NtUserSendMessageTimeout:
4270 struct send_message_timeout_params *params = (void *)result_info;
4271 DWORD_PTR res = 0;
4272 params->result = send_client_message( hwnd, msg, wparam, lparam, params->flags,
4273 params->timeout, &res, ansi );
4274 return res;
4277 case NtUserSendNotifyMessage:
4278 return send_notify_message( hwnd, msg, wparam, lparam, ansi );
4280 case NtUserSendMessageCallback:
4281 return send_message_callback( hwnd, msg, wparam, lparam, result_info, ansi );
4283 case NtUserClipboardWindowProc:
4284 return user_driver->pClipboardWindowProc( hwnd, msg, wparam, lparam );
4286 case NtUserGetDispatchParams:
4287 if (!hwnd) return FALSE;
4288 if (init_window_call_params( result_info, hwnd, msg, wparam, lparam,
4289 ansi, WMCHAR_MAP_DISPATCHMESSAGE ))
4290 return TRUE;
4291 if (!is_window( hwnd )) RtlSetLastWin32Error( ERROR_INVALID_WINDOW_HANDLE );
4292 else RtlSetLastWin32Error( ERROR_MESSAGE_SYNC_ONLY );
4293 return FALSE;
4295 case NtUserSpyEnter:
4296 spy_enter_message( ansi, hwnd, msg, wparam, lparam );
4297 return 0;
4299 case NtUserSpyGetMsgName:
4300 lstrcpynA( result_info, debugstr_msg_name( msg, hwnd ), wparam );
4301 return 0;
4303 case NtUserSpyExit:
4304 spy_exit_message( ansi, hwnd, msg, (LPARAM)result_info, wparam, lparam );
4305 return 0;
4307 case NtUserImeDriverCall:
4308 return ime_driver_call( hwnd, msg, wparam, lparam, result_info );
4310 default:
4311 FIXME( "%p %x %lx %lx %p %x %x\n", hwnd, msg, (long)wparam, lparam, result_info, (int)type, ansi );
4313 return 0;
4316 /***********************************************************************
4317 * NtUserTranslateMessage (win32u.@)
4319 BOOL WINAPI NtUserTranslateMessage( const MSG *msg, UINT flags )
4321 UINT message;
4322 WCHAR wp[8];
4323 BYTE state[256];
4324 INT len;
4326 if (flags) FIXME( "unsupported flags %x\n", flags );
4328 if (msg->message < WM_KEYFIRST || msg->message > WM_KEYLAST) return FALSE;
4329 if (msg->message != WM_KEYDOWN && msg->message != WM_SYSKEYDOWN) return TRUE;
4331 TRACE_(key)( "Translating key %s (%04x), scancode %04x\n",
4332 debugstr_vkey_name( msg->wParam ), LOWORD(msg->wParam), HIWORD(msg->lParam) );
4334 switch (msg->wParam)
4336 case VK_PACKET:
4337 message = (msg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
4338 TRACE_(key)( "PostMessageW(%p,%s,%04x,%08x)\n", msg->hwnd,
4339 debugstr_msg_name( message, msg->hwnd ),
4340 HIWORD(msg->lParam), LOWORD(msg->lParam) );
4341 NtUserPostMessage( msg->hwnd, message, HIWORD(msg->lParam), LOWORD(msg->lParam) );
4342 return TRUE;
4344 case VK_PROCESSKEY:
4345 return ImmTranslateMessage( msg->hwnd, msg->message, msg->wParam, msg->lParam );
4348 NtUserGetKeyboardState( state );
4349 len = NtUserToUnicodeEx( msg->wParam, HIWORD(msg->lParam), state, wp, ARRAY_SIZE(wp), 0,
4350 NtUserGetKeyboardLayout(0) );
4351 if (len == -1)
4353 message = msg->message == WM_KEYDOWN ? WM_DEADCHAR : WM_SYSDEADCHAR;
4354 TRACE_(key)( "-1 -> PostMessageW(%p,%s,%04x,%08lx)\n",
4355 msg->hwnd, debugstr_msg_name( message, msg->hwnd ), wp[0], msg->lParam );
4356 NtUserPostMessage( msg->hwnd, message, wp[0], msg->lParam );
4358 else if (len > 0)
4360 INT i;
4362 message = msg->message == WM_KEYDOWN ? WM_CHAR : WM_SYSCHAR;
4363 TRACE_(key)( "%d -> PostMessageW(%p,%s,<x>,%08lx) for <x> in %s\n", len, msg->hwnd,
4364 debugstr_msg_name(message, msg->hwnd), msg->lParam, debugstr_wn(wp, len) );
4365 for (i = 0; i < len; i++)
4366 NtUserPostMessage( msg->hwnd, message, wp[i], msg->lParam );
4368 return TRUE;