d3dcompiler: Allow D3DCompile2() to succeed with null output shader blob pointer.
[wine.git] / dlls / user32 / message.c
blob33e81c76e4b4566ca7dfcf14781583e94e3ae417
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
22 #include "ntstatus.h"
23 #define WIN32_NO_STATUS
24 #include "user_private.h"
25 #include "controls.h"
26 #include "dde.h"
27 #include "wine/server.h"
28 #include "wine/debug.h"
29 #include "wine/exception.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(msg);
34 /* pack a pointer into a 32/64 portable format */
35 static inline ULONGLONG pack_ptr( const void *ptr )
37 return (ULONG_PTR)ptr;
40 /* unpack a potentially 64-bit pointer, returning 0 when truncated */
41 static inline void *unpack_ptr( ULONGLONG ptr64 )
43 if ((ULONG_PTR)ptr64 != ptr64) return 0;
44 return (void *)(ULONG_PTR)ptr64;
47 static struct wm_char_mapping_data *get_wmchar_data(void)
49 return (struct wm_char_mapping_data *)(UINT_PTR)NtUserGetThreadInfo()->wmchar_data;
52 /* check for pending WM_CHAR message with DBCS trailing byte */
53 static inline BOOL get_pending_wmchar( MSG *msg, UINT first, UINT last, BOOL remove )
55 struct wm_char_mapping_data *data = get_wmchar_data();
57 if (!data || !data->get_msg.message) return FALSE;
58 if ((first || last) && (first > WM_CHAR || last < WM_CHAR)) return FALSE;
59 if (!msg) return FALSE;
60 *msg = data->get_msg;
61 if (remove) data->get_msg.message = 0;
62 return TRUE;
66 /***********************************************************************
67 * MessageWndProc
69 * Window procedure for "Message" windows (HWND_MESSAGE parent).
71 LRESULT WINAPI MessageWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
73 if (message == WM_NCCREATE) return TRUE;
74 return 0; /* all other messages are ignored */
78 DWORD get_input_codepage( void )
80 DWORD cp;
81 int ret;
82 HKL hkl = NtUserGetKeyboardLayout( 0 );
84 ret = GetLocaleInfoW( LOWORD(hkl), LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
85 (WCHAR *)&cp, sizeof(cp) / sizeof(WCHAR) );
86 if (!ret) cp = CP_ACP;
87 return cp;
90 /***********************************************************************
91 * map_wparam_AtoW
93 * Convert the wparam of an ASCII message to Unicode.
95 BOOL map_wparam_AtoW( UINT message, WPARAM *wparam, enum wm_char_mapping mapping )
97 char ch[2];
98 WCHAR wch[2];
99 DWORD cp;
101 wch[0] = wch[1] = 0;
102 switch(message)
104 case WM_CHAR:
105 /* WM_CHAR is magic: a DBCS char can be sent/posted as two consecutive WM_CHAR
106 * messages, in which case the first char is stored, and the conversion
107 * to Unicode only takes place once the second char is sent/posted.
109 if (mapping != WMCHAR_MAP_NOMAPPING)
111 struct wm_char_mapping_data *data = get_wmchar_data();
112 BYTE low = LOBYTE(*wparam);
113 cp = get_input_codepage();
115 if (HIBYTE(*wparam))
117 ch[0] = low;
118 ch[1] = HIBYTE(*wparam);
119 MultiByteToWideChar( cp, 0, ch, 2, wch, 2 );
120 TRACE( "map %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
121 if (data) data->lead_byte[mapping] = 0;
123 else if (data && data->lead_byte[mapping])
125 ch[0] = data->lead_byte[mapping];
126 ch[1] = low;
127 MultiByteToWideChar( cp, 0, ch, 2, wch, 2 );
128 TRACE( "map stored %02x,%02x -> %04x mapping %u\n", (BYTE)ch[0], (BYTE)ch[1], wch[0], mapping );
129 data->lead_byte[mapping] = 0;
131 else if (!IsDBCSLeadByte( low ))
133 ch[0] = low;
134 MultiByteToWideChar( cp, 0, ch, 1, wch, 2 );
135 TRACE( "map %02x -> %04x\n", (BYTE)ch[0], wch[0] );
136 if (data) data->lead_byte[mapping] = 0;
138 else /* store it and wait for trail byte */
140 if (!data)
142 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) )))
143 return FALSE;
144 NtUserGetThreadInfo()->wmchar_data = (UINT_PTR)data;
146 TRACE( "storing lead byte %02x mapping %u\n", low, mapping );
147 data->lead_byte[mapping] = low;
148 return FALSE;
150 *wparam = MAKEWPARAM(wch[0], wch[1]);
151 break;
153 /* else fall through */
154 case WM_CHARTOITEM:
155 case EM_SETPASSWORDCHAR:
156 case WM_DEADCHAR:
157 case WM_SYSCHAR:
158 case WM_SYSDEADCHAR:
159 case WM_MENUCHAR:
160 cp = get_input_codepage();
161 ch[0] = LOBYTE(*wparam);
162 ch[1] = HIBYTE(*wparam);
163 MultiByteToWideChar( cp, 0, ch, 2, wch, 2 );
164 *wparam = MAKEWPARAM(wch[0], wch[1]);
165 break;
166 case WM_IME_CHAR:
167 cp = get_input_codepage();
168 ch[0] = HIBYTE(*wparam);
169 ch[1] = LOBYTE(*wparam);
170 if (ch[0]) MultiByteToWideChar( cp, 0, ch, 2, wch, 2 );
171 else MultiByteToWideChar( cp, 0, ch + 1, 1, wch, 1 );
172 *wparam = MAKEWPARAM(wch[0], HIWORD(*wparam));
173 break;
175 return TRUE;
179 /***********************************************************************
180 * map_wparam_WtoA
182 * Convert the wparam of a Unicode message to ASCII.
184 static void map_wparam_WtoA( MSG *msg, BOOL remove )
186 BYTE ch[4];
187 WCHAR wch[2];
188 DWORD len;
189 DWORD cp;
191 switch(msg->message)
193 case WM_CHAR:
194 if (!HIWORD(msg->wParam))
196 cp = get_input_codepage();
197 wch[0] = LOWORD(msg->wParam);
198 ch[0] = ch[1] = 0;
199 len = WideCharToMultiByte( cp, 0, wch, 1, (LPSTR)ch, 2, NULL, NULL );
200 if (len == 2) /* DBCS char */
202 struct wm_char_mapping_data *data = get_wmchar_data();
203 if (!data)
205 if (!(data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data) ))) return;
206 NtUserGetThreadInfo()->wmchar_data = (UINT_PTR)data;
208 if (remove)
210 data->get_msg = *msg;
211 data->get_msg.wParam = ch[1];
213 msg->wParam = ch[0];
214 return;
217 /* else fall through */
218 case WM_CHARTOITEM:
219 case EM_SETPASSWORDCHAR:
220 case WM_DEADCHAR:
221 case WM_SYSCHAR:
222 case WM_SYSDEADCHAR:
223 case WM_MENUCHAR:
224 cp = get_input_codepage();
225 wch[0] = LOWORD(msg->wParam);
226 wch[1] = HIWORD(msg->wParam);
227 ch[0] = ch[1] = 0;
228 WideCharToMultiByte( cp, 0, wch, 2, (LPSTR)ch, 4, NULL, NULL );
229 msg->wParam = MAKEWPARAM( ch[0] | (ch[1] << 8), 0 );
230 break;
231 case WM_IME_CHAR:
232 cp = get_input_codepage();
233 wch[0] = LOWORD(msg->wParam);
234 ch[0] = ch[1] = 0;
235 len = WideCharToMultiByte( cp, 0, wch, 1, (LPSTR)ch, 2, NULL, NULL );
236 if (len == 2)
237 msg->wParam = MAKEWPARAM( (ch[0] << 8) | ch[1], HIWORD(msg->wParam) );
238 else
239 msg->wParam = MAKEWPARAM( ch[0], HIWORD(msg->wParam) );
240 break;
245 /* since the WM_DDE_ACK response to a WM_DDE_EXECUTE message should contain the handle
246 * to the memory handle, we keep track (in the server side) of all pairs of handle
247 * used (the client passes its value and the content of the memory handle), and
248 * the server stored both values (the client, and the local one, created after the
249 * content). When a ACK message is generated, the list of pair is searched for a
250 * matching pair, so that the client memory handle can be returned.
252 struct DDE_pair {
253 HGLOBAL client_hMem;
254 HGLOBAL server_hMem;
257 static struct DDE_pair* dde_pairs;
258 static int dde_num_alloc;
259 static int dde_num_used;
261 static CRITICAL_SECTION dde_crst;
262 static CRITICAL_SECTION_DEBUG critsect_debug =
264 0, 0, &dde_crst,
265 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
266 0, 0, { (DWORD_PTR)(__FILE__ ": dde_crst") }
268 static CRITICAL_SECTION dde_crst = { &critsect_debug, -1, 0, 0, 0, 0 };
270 static BOOL dde_add_pair(HGLOBAL chm, HGLOBAL shm)
272 int i;
273 #define GROWBY 4
275 EnterCriticalSection(&dde_crst);
277 /* now remember the pair of hMem on both sides */
278 if (dde_num_used == dde_num_alloc)
280 struct DDE_pair* tmp;
281 if (dde_pairs)
282 tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
283 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
284 else
285 tmp = HeapAlloc( GetProcessHeap(), 0,
286 (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
288 if (!tmp)
290 LeaveCriticalSection(&dde_crst);
291 return FALSE;
293 dde_pairs = tmp;
294 /* zero out newly allocated part */
295 memset(&dde_pairs[dde_num_alloc], 0, GROWBY * sizeof(struct DDE_pair));
296 dde_num_alloc += GROWBY;
298 #undef GROWBY
299 for (i = 0; i < dde_num_alloc; i++)
301 if (dde_pairs[i].server_hMem == 0)
303 dde_pairs[i].client_hMem = chm;
304 dde_pairs[i].server_hMem = shm;
305 dde_num_used++;
306 break;
309 LeaveCriticalSection(&dde_crst);
310 return TRUE;
313 static HGLOBAL dde_get_pair(HGLOBAL shm)
315 int i;
316 HGLOBAL ret = 0;
318 EnterCriticalSection(&dde_crst);
319 for (i = 0; i < dde_num_alloc; i++)
321 if (dde_pairs[i].server_hMem == shm)
323 /* free this pair */
324 dde_pairs[i].server_hMem = 0;
325 dde_num_used--;
326 ret = dde_pairs[i].client_hMem;
327 break;
330 LeaveCriticalSection(&dde_crst);
331 return ret;
334 /***********************************************************************
335 * post_dde_message
337 * Post a DDE message
339 BOOL post_dde_message( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam, DWORD dest_tid, DWORD type )
341 void* ptr = NULL;
342 int size = 0;
343 UINT_PTR uiLo, uiHi;
344 LPARAM lp;
345 HGLOBAL hunlock = 0;
346 DWORD res;
347 ULONGLONG hpack;
349 if (!UnpackDDElParam( msg, lparam, &uiLo, &uiHi ))
350 return FALSE;
352 lp = lparam;
353 switch (msg)
355 /* DDE messages which don't require packing are:
356 * WM_DDE_INITIATE
357 * WM_DDE_TERMINATE
358 * WM_DDE_REQUEST
359 * WM_DDE_UNADVISE
361 case WM_DDE_ACK:
362 if (HIWORD(uiHi))
364 /* uiHi should contain a hMem from WM_DDE_EXECUTE */
365 HGLOBAL h = dde_get_pair( (HANDLE)uiHi );
366 if (h)
368 hpack = pack_ptr( h );
369 /* send back the value of h on the other side */
370 ptr = &hpack;
371 size = sizeof(hpack);
372 lp = uiLo;
373 TRACE( "send dde-ack %Ix %08Ix => %p\n", uiLo, uiHi, h );
376 else
378 /* uiHi should contain either an atom or 0 */
379 TRACE( "send dde-ack %Ix atom=%Ix\n", uiLo, uiHi );
380 lp = MAKELONG( uiLo, uiHi );
382 break;
383 case WM_DDE_ADVISE:
384 case WM_DDE_DATA:
385 case WM_DDE_POKE:
386 if (uiLo)
388 size = GlobalSize( (HGLOBAL)uiLo ) ;
389 if ((msg == WM_DDE_ADVISE && size < sizeof(DDEADVISE)) ||
390 (msg == WM_DDE_DATA && size < FIELD_OFFSET(DDEDATA, Value)) ||
391 (msg == WM_DDE_POKE && size < FIELD_OFFSET(DDEPOKE, Value)))
392 return FALSE;
394 else if (msg != WM_DDE_DATA) return FALSE;
396 lp = uiHi;
397 if (uiLo)
399 if ((ptr = GlobalLock( (HGLOBAL)uiLo) ))
401 DDEDATA *dde_data = ptr;
402 TRACE("unused %d, fResponse %d, fRelease %d, fDeferUpd %d, fAckReq %d, cfFormat %d\n",
403 dde_data->unused, dde_data->fResponse, dde_data->fRelease,
404 dde_data->reserved, dde_data->fAckReq, dde_data->cfFormat);
405 hunlock = (HGLOBAL)uiLo;
408 TRACE( "send ddepack %u %Ix\n", size, uiHi );
409 break;
410 case WM_DDE_EXECUTE:
411 if (lparam)
413 if ((ptr = GlobalLock( (HGLOBAL)lparam) ))
415 size = GlobalSize( (HGLOBAL)lparam );
416 /* so that the other side can send it back on ACK */
417 lp = lparam;
418 hunlock = (HGLOBAL)lparam;
421 break;
423 SERVER_START_REQ( send_message )
425 req->id = dest_tid;
426 req->type = type;
427 req->flags = 0;
428 req->win = wine_server_user_handle( hwnd );
429 req->msg = msg;
430 req->wparam = wparam;
431 req->lparam = lp;
432 req->timeout = TIMEOUT_INFINITE;
433 if (size) wine_server_add_data( req, ptr, size );
434 if ((res = wine_server_call( req )))
436 if (res == STATUS_INVALID_PARAMETER)
437 /* FIXME: find a STATUS_ value for this one */
438 SetLastError( ERROR_INVALID_THREAD_ID );
439 else
440 SetLastError( RtlNtStatusToDosError(res) );
442 else
443 FreeDDElParam( msg, lparam );
445 SERVER_END_REQ;
446 if (hunlock) GlobalUnlock(hunlock);
448 return !res;
451 /***********************************************************************
452 * unpack_dde_message
454 * Unpack a posted DDE message received from another process.
456 BOOL unpack_dde_message( HWND hwnd, UINT message, WPARAM *wparam, LPARAM *lparam,
457 const void *buffer, size_t size )
459 UINT_PTR uiLo, uiHi;
460 HGLOBAL hMem = 0;
461 void* ptr;
463 switch (message)
465 case WM_DDE_ACK:
466 if (size)
468 ULONGLONG hpack;
469 /* hMem is being passed */
470 if (size != sizeof(hpack)) return FALSE;
471 uiLo = *lparam;
472 memcpy( &hpack, buffer, size );
473 hMem = unpack_ptr( hpack );
474 uiHi = (UINT_PTR)hMem;
475 TRACE("recv dde-ack %Ix mem=%Ix[%Ix]\n", uiLo, uiHi, GlobalSize( hMem ));
477 else
479 uiLo = LOWORD( *lparam );
480 uiHi = HIWORD( *lparam );
481 TRACE("recv dde-ack %Ix atom=%Ix\n", uiLo, uiHi);
483 *lparam = PackDDElParam( WM_DDE_ACK, uiLo, uiHi );
484 break;
485 case WM_DDE_ADVISE:
486 case WM_DDE_DATA:
487 case WM_DDE_POKE:
488 if (!size && message != WM_DDE_DATA) return FALSE;
489 uiHi = *lparam;
490 if (size)
492 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size )))
493 return FALSE;
494 if ((ptr = GlobalLock( hMem )))
496 memcpy( ptr, buffer, size );
497 GlobalUnlock( hMem );
499 else
501 GlobalFree( hMem );
502 return FALSE;
505 uiLo = (UINT_PTR)hMem;
507 *lparam = PackDDElParam( message, uiLo, uiHi );
508 break;
509 case WM_DDE_EXECUTE:
510 if (size)
512 if (!(hMem = GlobalAlloc( GMEM_MOVEABLE|GMEM_DDESHARE, size ))) return FALSE;
513 if ((ptr = GlobalLock( hMem )))
515 memcpy( ptr, buffer, size );
516 GlobalUnlock( hMem );
517 TRACE( "exec: pairing c=%08Ix s=%p\n", *lparam, hMem );
518 if (!dde_add_pair( (HGLOBAL)*lparam, hMem ))
520 GlobalFree( hMem );
521 return FALSE;
524 else
526 GlobalFree( hMem );
527 return FALSE;
529 } else return FALSE;
530 *lparam = (LPARAM)hMem;
531 break;
533 return TRUE;
536 /***********************************************************************
537 * SendMessageTimeoutW (USER32.@)
539 LRESULT WINAPI SendMessageTimeoutW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
540 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
542 struct send_message_timeout_params params = { .flags = flags, .timeout = timeout };
543 LRESULT res;
545 res = NtUserMessageCall( hwnd, msg, wparam, lparam, &params, NtUserSendMessageTimeout, FALSE );
546 if (res_ptr) *res_ptr = res;
547 return params.result;
550 /***********************************************************************
551 * SendMessageTimeoutA (USER32.@)
553 LRESULT WINAPI SendMessageTimeoutA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
554 UINT flags, UINT timeout, PDWORD_PTR res_ptr )
556 struct send_message_timeout_params params = { .flags = flags, .timeout = timeout };
557 LRESULT res = 0;
559 if (msg != WM_CHAR || WIN_IsCurrentThread( hwnd ))
561 res = NtUserMessageCall( hwnd, msg, wparam, lparam, &params,
562 NtUserSendMessageTimeout, TRUE );
564 else if (map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_SENDMESSAGE ))
566 res = NtUserMessageCall( hwnd, msg, wparam, lparam, &params,
567 NtUserSendMessageTimeout, FALSE );
570 if (res_ptr) *res_ptr = res;
571 return params.result;
575 static LRESULT dispatch_send_message( struct win_proc_params *params, WPARAM wparam, LPARAM lparam )
577 struct ntuser_thread_info *thread_info = NtUserGetThreadInfo();
578 INPUT_MESSAGE_SOURCE prev_source = thread_info->msg_source;
579 LRESULT retval = 0;
581 static const INPUT_MESSAGE_SOURCE msg_source_unavailable = { IMDT_UNAVAILABLE, IMO_UNAVAILABLE };
583 /* params may contain arguments modified by wow, use original parameters instead */
584 params->wparam = wparam;
585 params->lparam = lparam;
587 thread_info->recursion_count++;
589 params->result = &retval;
590 thread_info->msg_source = msg_source_unavailable;
591 SPY_EnterMessage( SPY_SENDMESSAGE, params->hwnd, params->msg, params->wparam, params->lparam );
593 dispatch_win_proc_params( params );
595 SPY_ExitMessage( SPY_RESULT_OK, params->hwnd, params->msg, retval, params->wparam, params->lparam );
596 thread_info->msg_source = prev_source;
597 thread_info->recursion_count--;
598 return retval;
602 /***********************************************************************
603 * SendMessageW (USER32.@)
605 LRESULT WINAPI SendMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
607 struct win_proc_params params;
608 LRESULT retval;
610 params.hwnd = 0;
611 retval = NtUserMessageCall( hwnd, msg, wparam, lparam, &params, NtUserSendMessage, FALSE );
612 if (params.hwnd) retval = dispatch_send_message( &params, wparam, lparam );
613 return retval;
617 /***********************************************************************
618 * SendMessageA (USER32.@)
620 LRESULT WINAPI SendMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
622 struct win_proc_params params;
623 LRESULT retval;
625 if (msg == WM_CHAR && !WIN_IsCurrentThread( hwnd ))
627 if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_SENDMESSAGE ))
628 return 0;
629 return NtUserMessageCall( hwnd, msg, wparam, lparam, NULL, NtUserSendMessage, FALSE );
632 params.hwnd = 0;
633 retval = NtUserMessageCall( hwnd, msg, wparam, lparam, &params, NtUserSendMessage, TRUE );
634 if (params.hwnd) retval = dispatch_send_message( &params, wparam, lparam );
635 return retval;
639 /***********************************************************************
640 * SendNotifyMessageA (USER32.@)
642 BOOL WINAPI SendNotifyMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
644 if (!WIN_IsCurrentThread( hwnd ) && !map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_SENDMESSAGE ))
645 return FALSE;
647 return NtUserMessageCall( hwnd, msg, wparam, lparam, 0, NtUserSendNotifyMessage, TRUE );
651 /***********************************************************************
652 * SendNotifyMessageW (USER32.@)
654 BOOL WINAPI SendNotifyMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
656 return NtUserMessageCall( hwnd, msg, wparam, lparam, 0, NtUserSendNotifyMessage, FALSE );
660 /***********************************************************************
661 * SendMessageCallbackA (USER32.@)
663 BOOL WINAPI SendMessageCallbackA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
664 SENDASYNCPROC callback, ULONG_PTR data )
666 struct send_message_callback_params params = { .callback = callback, .data = data };
668 if (!WIN_IsCurrentThread( hwnd ) && !map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_SENDMESSAGE ))
669 return FALSE;
671 return NtUserMessageCall( hwnd, msg, wparam, lparam, &params, NtUserSendMessageCallback, TRUE );
675 /***********************************************************************
676 * SendMessageCallbackW (USER32.@)
678 BOOL WINAPI SendMessageCallbackW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam,
679 SENDASYNCPROC callback, ULONG_PTR data )
681 struct send_message_callback_params params = { .callback = callback, .data = data };
682 return NtUserMessageCall( hwnd, msg, wparam, lparam, &params, NtUserSendMessageCallback, FALSE );
686 /***********************************************************************
687 * ReplyMessage (USER32.@)
689 BOOL WINAPI ReplyMessage( LRESULT result )
691 return NtUserReplyMessage( result );
695 /***********************************************************************
696 * InSendMessage (USER32.@)
698 BOOL WINAPI InSendMessage(void)
700 return (InSendMessageEx( NULL ) & (ISMEX_SEND | ISMEX_NOTIFY | ISMEX_CALLBACK)) != 0;
704 /***********************************************************************
705 * InSendMessageEx (USER32.@)
707 DWORD WINAPI InSendMessageEx( LPVOID reserved )
709 return NtUserGetThreadInfo()->receive_flags;
713 /***********************************************************************
714 * PostMessageA (USER32.@)
716 BOOL WINAPI PostMessageA( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
718 if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
719 return PostMessageW( hwnd, msg, wparam, lparam );
723 /***********************************************************************
724 * PostMessageW (USER32.@)
726 BOOL WINAPI PostMessageW( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
728 return NtUserPostMessage( hwnd, msg, wparam, lparam );
732 /**********************************************************************
733 * PostThreadMessageA (USER32.@)
735 BOOL WINAPI PostThreadMessageA( DWORD thread, UINT msg, WPARAM wparam, LPARAM lparam )
737 if (!map_wparam_AtoW( msg, &wparam, WMCHAR_MAP_POSTMESSAGE )) return TRUE;
738 return NtUserPostThreadMessage( thread, msg, wparam, lparam );
742 /***********************************************************************
743 * PostQuitMessage (USER32.@)
745 * Posts a quit message to the current thread's message queue.
747 * PARAMS
748 * exit_code [I] Exit code to return from message loop.
750 * RETURNS
751 * Nothing.
753 * NOTES
754 * This function is not the same as calling:
755 *|PostThreadMessage(GetCurrentThreadId(), WM_QUIT, exit_code, 0);
756 * It instead sets a flag in the message queue that signals it to generate
757 * a WM_QUIT message when there are no other pending sent or posted messages
758 * in the queue.
760 void WINAPI PostQuitMessage( INT exit_code )
762 SERVER_START_REQ( post_quit_message )
764 req->exit_code = exit_code;
765 wine_server_call( req );
767 SERVER_END_REQ;
770 /***********************************************************************
771 * PeekMessageW (USER32.@)
773 BOOL WINAPI DECLSPEC_HOTPATCH PeekMessageW( MSG *msg_out, HWND hwnd, UINT first, UINT last, UINT flags )
775 return NtUserPeekMessage( msg_out, hwnd, first, last, flags );
779 /***********************************************************************
780 * PeekMessageA (USER32.@)
782 BOOL WINAPI DECLSPEC_HOTPATCH PeekMessageA( MSG *msg, HWND hwnd, UINT first, UINT last, UINT flags )
784 if (get_pending_wmchar( msg, first, last, (flags & PM_REMOVE) )) return TRUE;
785 if (!PeekMessageW( msg, hwnd, first, last, flags )) return FALSE;
786 map_wparam_WtoA( msg, (flags & PM_REMOVE) );
787 return TRUE;
791 /***********************************************************************
792 * GetMessageW (USER32.@)
794 BOOL WINAPI DECLSPEC_HOTPATCH GetMessageW( MSG *msg, HWND hwnd, UINT first, UINT last )
796 return NtUserGetMessage( msg, hwnd, first, last );
800 /***********************************************************************
801 * GetMessageA (USER32.@)
803 BOOL WINAPI DECLSPEC_HOTPATCH GetMessageA( MSG *msg, HWND hwnd, UINT first, UINT last )
805 if (get_pending_wmchar( msg, first, last, TRUE )) return TRUE;
806 if (GetMessageW( msg, hwnd, first, last ) < 0) return -1;
807 map_wparam_WtoA( msg, TRUE );
808 return (msg->message != WM_QUIT);
812 /***********************************************************************
813 * IsDialogMessageA (USER32.@)
814 * IsDialogMessage (USER32.@)
816 BOOL WINAPI IsDialogMessageA( HWND hwndDlg, LPMSG pmsg )
818 MSG msg = *pmsg;
819 map_wparam_AtoW( msg.message, &msg.wParam, WMCHAR_MAP_NOMAPPING );
820 return IsDialogMessageW( hwndDlg, &msg );
824 /***********************************************************************
825 * TranslateMessage (USER32.@)
827 * Implementation of TranslateMessage.
829 * TranslateMessage translates virtual-key messages into character-messages,
830 * as follows :
831 * WM_KEYDOWN/WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message.
832 * ditto replacing WM_* with WM_SYS*
833 * This produces WM_CHAR messages only for keys mapped to ASCII characters
834 * by the keyboard driver.
836 * If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the
837 * return value is nonzero, regardless of the translation.
840 BOOL WINAPI TranslateMessage( const MSG *msg )
842 return NtUserTranslateMessage( msg, 0 );
846 static LRESULT dispatch_message( const MSG *msg, BOOL ansi )
848 struct win_proc_params params;
849 LRESULT retval = 0;
851 if (!NtUserMessageCall( msg->hwnd, msg->message, msg->wParam, msg->lParam,
852 &params, NtUserGetDispatchParams, ansi )) return 0;
853 params.result = &retval;
855 SPY_EnterMessage( SPY_DISPATCHMESSAGE, msg->hwnd, msg->message, msg->wParam, msg->lParam );
856 dispatch_win_proc_params( &params );
857 SPY_ExitMessage( SPY_RESULT_OK, msg->hwnd, msg->message, retval, msg->wParam, msg->lParam );
858 return retval;
862 /***********************************************************************
863 * DispatchMessageA (USER32.@)
865 * See DispatchMessageW.
867 LRESULT WINAPI DECLSPEC_HOTPATCH DispatchMessageA( const MSG* msg )
869 LRESULT retval;
871 /* Process timer messages */
872 if (msg->lParam && msg->message == WM_TIMER)
874 __TRY
876 retval = CallWindowProcA( (WNDPROC)msg->lParam, msg->hwnd,
877 msg->message, msg->wParam, GetTickCount() );
879 __EXCEPT_ALL
881 retval = 0;
883 __ENDTRY
884 return retval;
887 /* whenever possible, avoid using NtUserDispatchMessage to make the call unwindable */
888 if (msg->message != WM_SYSTIMER && msg->message != WM_PAINT)
889 return dispatch_message( msg, TRUE );
891 return NtUserDispatchMessage( msg );
895 /***********************************************************************
896 * DispatchMessageW (USER32.@) Process a message
898 * Process the message specified in the structure *_msg_.
900 * If the lpMsg parameter points to a WM_TIMER message and the
901 * parameter of the WM_TIMER message is not NULL, the lParam parameter
902 * points to the function that is called instead of the window
903 * procedure. The function stored in lParam (timer callback) is protected
904 * from causing page-faults.
906 * The message must be valid.
908 * RETURNS
910 * DispatchMessage() returns the result of the window procedure invoked.
912 * CONFORMANCE
914 * ECMA-234, Win32
917 LRESULT WINAPI DECLSPEC_HOTPATCH DispatchMessageW( const MSG* msg )
919 LRESULT retval;
921 /* Process timer messages */
922 if ((msg->message == WM_TIMER) || (msg->message == WM_SYSTIMER))
924 if (msg->lParam)
926 __TRY
928 retval = CallWindowProcW( (WNDPROC)msg->lParam, msg->hwnd,
929 msg->message, msg->wParam, GetTickCount() );
931 __EXCEPT_ALL
933 retval = 0;
935 __ENDTRY
936 return retval;
940 /* whenever possible, avoid using NtUserDispatchMessage to make the call unwindable */
941 if (msg->message != WM_SYSTIMER && msg->message != WM_PAINT)
942 return dispatch_message( msg, FALSE );
944 return NtUserDispatchMessage( msg );
948 /***********************************************************************
949 * GetMessagePos (USER.119)
950 * GetMessagePos (USER32.@)
952 * The GetMessagePos() function returns a long value representing a
953 * cursor position, in screen coordinates, when the last message
954 * retrieved by the GetMessage() function occurs. The x-coordinate is
955 * in the low-order word of the return value, the y-coordinate is in
956 * the high-order word. The application can use the MAKEPOINT()
957 * macro to obtain a POINT structure from the return value.
959 * For the current cursor position, use GetCursorPos().
961 * RETURNS
963 * Cursor position of last message on success, zero on failure.
965 * CONFORMANCE
967 * ECMA-234, Win32
970 DWORD WINAPI GetMessagePos(void)
972 return NtUserGetThreadInfo()->message_pos;
976 /***********************************************************************
977 * GetMessageTime (USER.120)
978 * GetMessageTime (USER32.@)
980 * GetMessageTime() returns the message time for the last message
981 * retrieved by the function. The time is measured in milliseconds with
982 * the same offset as GetTickCount().
984 * Since the tick count wraps, this is only useful for moderately short
985 * relative time comparisons.
987 * RETURNS
989 * Time of last message on success, zero on failure.
991 LONG WINAPI GetMessageTime(void)
993 return NtUserGetThreadInfo()->message_time;
997 /***********************************************************************
998 * GetMessageExtraInfo (USER.288)
999 * GetMessageExtraInfo (USER32.@)
1001 LPARAM WINAPI GetMessageExtraInfo(void)
1003 return NtUserGetThreadInfo()->message_extra;
1007 /***********************************************************************
1008 * SetMessageExtraInfo (USER32.@)
1010 LPARAM WINAPI SetMessageExtraInfo(LPARAM lParam)
1012 struct ntuser_thread_info *thread_info = NtUserGetThreadInfo();
1013 LONG old_value = thread_info->message_extra;
1014 thread_info->message_extra = lParam;
1015 return old_value;
1019 /***********************************************************************
1020 * GetCurrentInputMessageSource (USER32.@)
1022 BOOL WINAPI GetCurrentInputMessageSource( INPUT_MESSAGE_SOURCE *source )
1024 *source = NtUserGetThreadInfo()->msg_source;
1025 return TRUE;
1029 /***********************************************************************
1030 * MsgWaitForMultipleObjects (USER32.@)
1032 DWORD WINAPI MsgWaitForMultipleObjects( DWORD count, const HANDLE *handles,
1033 BOOL wait_all, DWORD timeout, DWORD mask )
1035 return NtUserMsgWaitForMultipleObjectsEx( count, handles, timeout, mask,
1036 wait_all ? MWMO_WAITALL : 0 );
1040 /***********************************************************************
1041 * WaitForInputIdle (USER32.@)
1043 DWORD WINAPI WaitForInputIdle( HANDLE process, DWORD timeout )
1045 return NtUserWaitForInputIdle( process, timeout, FALSE );
1049 /***********************************************************************
1050 * RegisterWindowMessageA (USER32.@)
1051 * RegisterWindowMessage (USER.118)
1053 UINT WINAPI RegisterWindowMessageA( LPCSTR str )
1055 UINT ret = GlobalAddAtomA(str);
1056 TRACE("%s, ret=%x\n", str, ret);
1057 return ret;
1061 /***********************************************************************
1062 * RegisterWindowMessageW (USER32.@)
1064 UINT WINAPI RegisterWindowMessageW( LPCWSTR str )
1066 UINT ret = GlobalAddAtomW(str);
1067 TRACE("%s ret=%x\n", debugstr_w(str), ret);
1068 return ret;
1071 typedef struct BroadcastParm
1073 DWORD flags;
1074 LPDWORD recipients;
1075 UINT msg;
1076 WPARAM wp;
1077 LPARAM lp;
1078 BOOL success;
1079 HWINSTA winsta;
1080 } BroadcastParm;
1082 static BOOL CALLBACK bcast_childwindow( HWND hw, LPARAM lp )
1084 BroadcastParm *parm = (BroadcastParm*)lp;
1085 DWORD_PTR retval = 0;
1086 LRESULT lresult;
1088 if (parm->flags & BSF_IGNORECURRENTTASK && WIN_IsCurrentProcess(hw))
1090 TRACE("Not telling myself %p\n", hw);
1091 return TRUE;
1094 /* I don't know 100% for sure if this is what Windows does, but it fits the tests */
1095 if (parm->flags & BSF_QUERY)
1097 TRACE("Telling window %p using SendMessageTimeout\n", hw);
1099 /* Not tested for conflicting flags */
1100 if (parm->flags & BSF_FORCEIFHUNG || parm->flags & BSF_NOHANG)
1101 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_ABORTIFHUNG, 2000, &retval );
1102 else if (parm->flags & BSF_NOTIMEOUTIFNOTHUNG)
1103 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NOTIMEOUTIFNOTHUNG, 2000, &retval );
1104 else
1105 lresult = SendMessageTimeoutW( hw, parm->msg, parm->wp, parm->lp, SMTO_NORMAL, 2000, &retval );
1107 if (!lresult && GetLastError() == ERROR_TIMEOUT)
1109 WARN("Timed out!\n");
1110 if (!(parm->flags & BSF_FORCEIFHUNG))
1111 goto fail;
1113 if (retval == BROADCAST_QUERY_DENY)
1114 goto fail;
1116 return TRUE;
1118 fail:
1119 parm->success = FALSE;
1120 return FALSE;
1122 else if (parm->flags & BSF_POSTMESSAGE)
1124 TRACE("Telling window %p using PostMessage\n", hw);
1125 PostMessageW( hw, parm->msg, parm->wp, parm->lp );
1127 else
1129 TRACE("Telling window %p using SendNotifyMessage\n", hw);
1130 SendNotifyMessageW( hw, parm->msg, parm->wp, parm->lp );
1133 return TRUE;
1136 static BOOL CALLBACK bcast_desktop( LPWSTR desktop, LPARAM lp )
1138 BOOL ret;
1139 HDESK hdesktop;
1140 BroadcastParm *parm = (BroadcastParm*)lp;
1142 TRACE("desktop: %s\n", debugstr_w( desktop ));
1144 hdesktop = open_winstation_desktop( parm->winsta, desktop, 0, FALSE, DESKTOP_ENUMERATE|DESKTOP_WRITEOBJECTS|STANDARD_RIGHTS_WRITE );
1145 if (!hdesktop)
1147 FIXME("Could not open desktop %s\n", debugstr_w(desktop));
1148 return TRUE;
1151 ret = EnumDesktopWindows( hdesktop, bcast_childwindow, lp );
1152 NtUserCloseDesktop( hdesktop );
1153 TRACE("-->%d\n", ret);
1154 return parm->success;
1157 static BOOL CALLBACK bcast_winsta( LPWSTR winsta, LPARAM lp )
1159 BOOL ret;
1160 HWINSTA hwinsta = OpenWindowStationW( winsta, FALSE, WINSTA_ENUMDESKTOPS );
1161 TRACE("hwinsta: %p/%s/%08lx\n", hwinsta, debugstr_w( winsta ), GetLastError());
1162 if (!hwinsta)
1163 return TRUE;
1164 ((BroadcastParm *)lp)->winsta = hwinsta;
1165 ret = EnumDesktopsW( hwinsta, bcast_desktop, lp );
1166 NtUserCloseWindowStation( hwinsta );
1167 TRACE("-->%d\n", ret);
1168 return ret;
1171 /***********************************************************************
1172 * BroadcastSystemMessageA (USER32.@)
1173 * BroadcastSystemMessage (USER32.@)
1175 LONG WINAPI BroadcastSystemMessageA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
1177 return BroadcastSystemMessageExA( flags, recipients, msg, wp, lp, NULL );
1181 /***********************************************************************
1182 * BroadcastSystemMessageW (USER32.@)
1184 LONG WINAPI BroadcastSystemMessageW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp )
1186 return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, NULL );
1189 /***********************************************************************
1190 * BroadcastSystemMessageExA (USER32.@)
1192 LONG WINAPI BroadcastSystemMessageExA( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
1194 map_wparam_AtoW( msg, &wp, WMCHAR_MAP_NOMAPPING );
1195 return BroadcastSystemMessageExW( flags, recipients, msg, wp, lp, pinfo );
1199 /***********************************************************************
1200 * BroadcastSystemMessageExW (USER32.@)
1202 LONG WINAPI BroadcastSystemMessageExW( DWORD flags, LPDWORD recipients, UINT msg, WPARAM wp, LPARAM lp, PBSMINFO pinfo )
1204 BroadcastParm parm;
1205 DWORD recips = BSM_ALLCOMPONENTS;
1206 BOOL ret = TRUE;
1207 static const DWORD all_flags = ( BSF_QUERY | BSF_IGNORECURRENTTASK | BSF_FLUSHDISK | BSF_NOHANG
1208 | BSF_POSTMESSAGE | BSF_FORCEIFHUNG | BSF_NOTIMEOUTIFNOTHUNG
1209 | BSF_ALLOWSFW | BSF_SENDNOTIFYMESSAGE | BSF_RETURNHDESK | BSF_LUID );
1211 TRACE("Flags: %08lx, recipients: %p(0x%lx), msg: %04x, wparam: %08Ix, lparam: %08Ix\n", flags, recipients,
1212 (recipients ? *recipients : recips), msg, wp, lp);
1214 if (flags & ~all_flags)
1216 SetLastError(ERROR_INVALID_PARAMETER);
1217 return 0;
1220 if (!recipients)
1221 recipients = &recips;
1223 if ( pinfo && flags & BSF_QUERY )
1224 FIXME("Not returning PBSMINFO information yet\n");
1226 parm.flags = flags;
1227 parm.recipients = recipients;
1228 parm.msg = msg;
1229 parm.wp = wp;
1230 parm.lp = lp;
1231 parm.success = TRUE;
1233 if (*recipients & BSM_ALLDESKTOPS || *recipients == BSM_ALLCOMPONENTS)
1234 ret = EnumWindowStationsW(bcast_winsta, (LONG_PTR)&parm);
1235 else if (*recipients & BSM_APPLICATIONS)
1237 EnumWindows(bcast_childwindow, (LONG_PTR)&parm);
1238 ret = parm.success;
1240 else
1241 FIXME("Recipients %08lx not supported!\n", *recipients);
1243 return ret;
1246 /***********************************************************************
1247 * SetMessageQueue (USER32.@)
1249 BOOL WINAPI SetMessageQueue( INT size )
1251 /* now obsolete the message queue will be expanded dynamically as necessary */
1252 return TRUE;
1256 /***********************************************************************
1257 * MessageBeep (USER32.@)
1259 BOOL WINAPI MessageBeep( UINT i )
1261 return NtUserMessageBeep( i );
1265 /******************************************************************
1266 * SetTimer (USER32.@)
1268 UINT_PTR WINAPI SetTimer( HWND hwnd, UINT_PTR id, UINT timeout, TIMERPROC proc )
1270 return NtUserSetTimer( hwnd, id, timeout, proc, TIMERV_DEFAULT_COALESCING );
1274 /******************************************************************
1275 * SetSystemTimer (USER32.@)
1277 UINT_PTR WINAPI SetSystemTimer( HWND hwnd, UINT_PTR id, UINT timeout, void *unknown )
1279 if (unknown) FIXME( "ignoring unknown parameter %p\n", unknown );
1281 return NtUserSetSystemTimer( hwnd, id, timeout );
1285 /***********************************************************************
1286 * KillSystemTimer (USER32.@)
1288 BOOL WINAPI KillSystemTimer( HWND hwnd, UINT_PTR id )
1290 return NtUserKillSystemTimer( hwnd, id );
1294 /**********************************************************************
1295 * IsGUIThread (USER32.@)
1297 BOOL WINAPI IsGUIThread( BOOL convert )
1299 FIXME( "%u: stub\n", convert );
1300 return TRUE;
1304 /******************************************************************
1305 * IsHungAppWindow (USER32.@)
1308 BOOL WINAPI IsHungAppWindow( HWND hWnd )
1310 BOOL ret;
1312 SERVER_START_REQ( is_window_hung )
1314 req->win = wine_server_user_handle( hWnd );
1315 ret = !wine_server_call_err( req ) && reply->is_hung;
1317 SERVER_END_REQ;
1318 return ret;
1321 /******************************************************************
1322 * ChangeWindowMessageFilter (USER32.@)
1324 BOOL WINAPI ChangeWindowMessageFilter( UINT message, DWORD flag )
1326 FIXME( "%x %08lx\n", message, flag );
1327 return TRUE;
1330 /******************************************************************
1331 * ChangeWindowMessageFilterEx (USER32.@)
1333 BOOL WINAPI ChangeWindowMessageFilterEx( HWND hwnd, UINT message, DWORD action, CHANGEFILTERSTRUCT *changefilter )
1335 FIXME( "%p %x %ld %p\n", hwnd, message, action, changefilter );
1336 return TRUE;