2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* $Id: message.c,v 1.74 2004/09/28 15:02:30 weiden Exp $
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
24 * FILE: subsys/win32k/ntuser/message.c
25 * PROGRAMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
27 * 06-06-2001 CSH Created
30 /* INCLUDES ******************************************************************/
42 } DOSENDMESSAGE
, *PDOSENDMESSAGE
;
44 /* FUNCTIONS *****************************************************************/
47 IntInitMessageImpl(VOID
)
49 return STATUS_SUCCESS
;
53 IntCleanupMessageImpl(VOID
)
55 return STATUS_SUCCESS
;
58 #define MMS_SIZE_WPARAM -1
59 #define MMS_SIZE_WPARAMWCHAR -2
60 #define MMS_SIZE_LPARAMSZ -3
61 #define MMS_SIZE_SPECIAL -4
62 #define MMS_FLAG_READ 0x01
63 #define MMS_FLAG_WRITE 0x02
64 #define MMS_FLAG_READWRITE (MMS_FLAG_READ | MMS_FLAG_WRITE)
65 typedef struct tagMSGMEMORY
70 } MSGMEMORY
, *PMSGMEMORY
;
72 static MSGMEMORY MsgMemory
[] =
74 { WM_CREATE
, MMS_SIZE_SPECIAL
, MMS_FLAG_READWRITE
},
75 { WM_DDE_ACK
, sizeof(KMDDELPARAM
), MMS_FLAG_READ
},
76 { WM_DDE_EXECUTE
, MMS_SIZE_WPARAM
, MMS_FLAG_READ
},
77 { WM_GETMINMAXINFO
, sizeof(MINMAXINFO
), MMS_FLAG_READWRITE
},
78 { WM_GETTEXT
, MMS_SIZE_WPARAMWCHAR
, MMS_FLAG_WRITE
},
79 { WM_NCCALCSIZE
, MMS_SIZE_SPECIAL
, MMS_FLAG_READWRITE
},
80 { WM_NCCREATE
, MMS_SIZE_SPECIAL
, MMS_FLAG_READWRITE
},
81 { WM_SETTEXT
, MMS_SIZE_LPARAMSZ
, MMS_FLAG_READ
},
82 { WM_STYLECHANGED
, sizeof(STYLESTRUCT
), MMS_FLAG_READ
},
83 { WM_STYLECHANGING
, sizeof(STYLESTRUCT
), MMS_FLAG_READWRITE
},
84 { WM_WINDOWPOSCHANGED
, sizeof(WINDOWPOS
), MMS_FLAG_READ
},
85 { WM_WINDOWPOSCHANGING
, sizeof(WINDOWPOS
), MMS_FLAG_READWRITE
},
88 static PMSGMEMORY FASTCALL
89 FindMsgMemory(UINT Msg
)
91 PMSGMEMORY MsgMemoryEntry
;
93 /* See if this message type is present in the table */
94 for (MsgMemoryEntry
= MsgMemory
;
95 MsgMemoryEntry
< MsgMemory
+ sizeof(MsgMemory
) / sizeof(MSGMEMORY
);
98 if (Msg
== MsgMemoryEntry
->Message
)
100 return MsgMemoryEntry
;
108 MsgMemorySize(PMSGMEMORY MsgMemoryEntry
, WPARAM wParam
, LPARAM lParam
)
111 PUNICODE_STRING WindowName
;
112 PUNICODE_STRING ClassName
;
115 if (MMS_SIZE_WPARAM
== MsgMemoryEntry
->Size
)
117 return (UINT
) wParam
;
119 else if (MMS_SIZE_WPARAMWCHAR
== MsgMemoryEntry
->Size
)
121 return (UINT
) (wParam
* sizeof(WCHAR
));
123 else if (MMS_SIZE_LPARAMSZ
== MsgMemoryEntry
->Size
)
125 return (UINT
) ((wcslen((PWSTR
) lParam
) + 1) * sizeof(WCHAR
));
127 else if (MMS_SIZE_SPECIAL
== MsgMemoryEntry
->Size
)
129 switch(MsgMemoryEntry
->Message
)
133 Cs
= (CREATESTRUCTW
*) lParam
;
134 WindowName
= (PUNICODE_STRING
) Cs
->lpszName
;
135 ClassName
= (PUNICODE_STRING
) Cs
->lpszClass
;
136 Size
= sizeof(CREATESTRUCTW
) + WindowName
->Length
+ sizeof(WCHAR
);
137 if (IS_ATOM(ClassName
->Buffer
))
139 Size
+= sizeof(WCHAR
) + sizeof(ATOM
);
143 Size
+= sizeof(WCHAR
) + ClassName
->Length
+ sizeof(WCHAR
);
149 return wParam
? sizeof(NCCALCSIZE_PARAMS
) + sizeof(WINDOWPOS
) : sizeof(RECT
);
160 return MsgMemoryEntry
->Size
;
164 static FASTCALL NTSTATUS
165 PackParam(LPARAM
*lParamPacked
, UINT Msg
, WPARAM wParam
, LPARAM lParam
)
167 NCCALCSIZE_PARAMS
*UnpackedNcCalcsize
;
168 NCCALCSIZE_PARAMS
*PackedNcCalcsize
;
169 CREATESTRUCTW
*UnpackedCs
;
170 CREATESTRUCTW
*PackedCs
;
171 PUNICODE_STRING WindowName
;
172 PUNICODE_STRING ClassName
;
176 *lParamPacked
= lParam
;
177 if (WM_NCCALCSIZE
== Msg
&& wParam
)
179 UnpackedNcCalcsize
= (NCCALCSIZE_PARAMS
*) lParam
;
180 if (UnpackedNcCalcsize
->lppos
!= (PWINDOWPOS
) (UnpackedNcCalcsize
+ 1))
182 PackedNcCalcsize
= ExAllocatePoolWithTag(PagedPool
,
183 sizeof(NCCALCSIZE_PARAMS
) + sizeof(WINDOWPOS
),
185 if (NULL
== PackedNcCalcsize
)
187 DPRINT1("Not enough memory to pack lParam\n");
188 return STATUS_NO_MEMORY
;
190 RtlCopyMemory(PackedNcCalcsize
, UnpackedNcCalcsize
, sizeof(NCCALCSIZE_PARAMS
));
191 PackedNcCalcsize
->lppos
= (PWINDOWPOS
) (PackedNcCalcsize
+ 1);
192 RtlCopyMemory(PackedNcCalcsize
->lppos
, UnpackedNcCalcsize
->lppos
, sizeof(WINDOWPOS
));
193 *lParamPacked
= (LPARAM
) PackedNcCalcsize
;
196 else if (WM_CREATE
== Msg
|| WM_NCCREATE
== Msg
)
198 UnpackedCs
= (CREATESTRUCTW
*) lParam
;
199 WindowName
= (PUNICODE_STRING
) UnpackedCs
->lpszName
;
200 ClassName
= (PUNICODE_STRING
) UnpackedCs
->lpszClass
;
201 Size
= sizeof(CREATESTRUCTW
) + WindowName
->Length
+ sizeof(WCHAR
);
202 if (IS_ATOM(ClassName
->Buffer
))
204 Size
+= sizeof(WCHAR
) + sizeof(ATOM
);
208 Size
+= sizeof(WCHAR
) + ClassName
->Length
+ sizeof(WCHAR
);
210 PackedCs
= ExAllocatePoolWithTag(PagedPool
, Size
, TAG_MSG
);
211 if (NULL
== PackedCs
)
213 DPRINT1("Not enough memory to pack lParam\n");
214 return STATUS_NO_MEMORY
;
216 RtlCopyMemory(PackedCs
, UnpackedCs
, sizeof(CREATESTRUCTW
));
217 CsData
= (PCHAR
) (PackedCs
+ 1);
218 PackedCs
->lpszName
= (LPCWSTR
) (CsData
- (PCHAR
) PackedCs
);
219 RtlCopyMemory(CsData
, WindowName
->Buffer
, WindowName
->Length
);
220 CsData
+= WindowName
->Length
;
221 *((WCHAR
*) CsData
) = L
'\0';
222 CsData
+= sizeof(WCHAR
);
223 PackedCs
->lpszClass
= (LPCWSTR
) (CsData
- (PCHAR
) PackedCs
);
224 if (IS_ATOM(ClassName
->Buffer
))
226 *((WCHAR
*) CsData
) = L
'A';
227 CsData
+= sizeof(WCHAR
);
228 *((ATOM
*) CsData
) = (ATOM
)(DWORD_PTR
) ClassName
->Buffer
;
229 CsData
+= sizeof(ATOM
);
233 *((WCHAR
*) CsData
) = L
'S';
234 CsData
+= sizeof(WCHAR
);
235 RtlCopyMemory(CsData
, ClassName
->Buffer
, ClassName
->Length
);
236 CsData
+= ClassName
->Length
;
237 *((WCHAR
*) CsData
) = L
'\0';
238 CsData
+= sizeof(WCHAR
);
240 ASSERT(CsData
== (PCHAR
) PackedCs
+ Size
);
241 *lParamPacked
= (LPARAM
) PackedCs
;
244 return STATUS_SUCCESS
;
247 static FASTCALL NTSTATUS
248 UnpackParam(LPARAM lParamPacked
, UINT Msg
, WPARAM wParam
, LPARAM lParam
)
250 NCCALCSIZE_PARAMS
*UnpackedParams
;
251 NCCALCSIZE_PARAMS
*PackedParams
;
252 PWINDOWPOS UnpackedWindowPos
;
254 if (lParamPacked
== lParam
)
256 return STATUS_SUCCESS
;
259 if (WM_NCCALCSIZE
== Msg
&& wParam
)
261 PackedParams
= (NCCALCSIZE_PARAMS
*) lParamPacked
;
262 UnpackedParams
= (NCCALCSIZE_PARAMS
*) lParam
;
263 UnpackedWindowPos
= UnpackedParams
->lppos
;
264 RtlCopyMemory(UnpackedParams
, PackedParams
, sizeof(NCCALCSIZE_PARAMS
));
265 UnpackedParams
->lppos
= UnpackedWindowPos
;
266 RtlCopyMemory(UnpackedWindowPos
, PackedParams
+ 1, sizeof(WINDOWPOS
));
267 ExFreePool((PVOID
) lParamPacked
);
269 return STATUS_SUCCESS
;
271 else if (WM_CREATE
== Msg
|| WM_NCCREATE
== Msg
)
273 ExFreePool((PVOID
) lParamPacked
);
275 return STATUS_SUCCESS
;
280 return STATUS_INVALID_PARAMETER
;
285 NtUserDispatchMessage(PNTUSERDISPATCHMESSAGEINFO UnsafeMsgInfo
)
288 NTUSERDISPATCHMESSAGEINFO MsgInfo
;
289 PWINDOW_OBJECT WindowObject
;
290 LRESULT Result
= TRUE
;
292 Status
= MmCopyFromCaller(&MsgInfo
, UnsafeMsgInfo
, sizeof(NTUSERDISPATCHMESSAGEINFO
));
293 if (! NT_SUCCESS(Status
))
295 SetLastNtError(Status
);
299 /* Process timer messages. */
300 if (WM_TIMER
== MsgInfo
.Msg
.message
&& 0 != MsgInfo
.Msg
.lParam
)
302 LARGE_INTEGER LargeTickCount
;
303 /* FIXME: Call hooks. */
305 /* FIXME: Check for continuing validity of timer. */
307 MsgInfo
.HandledByKernel
= FALSE
;
308 KeQueryTickCount(&LargeTickCount
);
309 MsgInfo
.Proc
= (WNDPROC
) MsgInfo
.Msg
.lParam
;
310 MsgInfo
.Msg
.lParam
= (LPARAM
)LargeTickCount
.u
.LowPart
;
312 else if (NULL
== MsgInfo
.Msg
.hwnd
)
314 MsgInfo
.HandledByKernel
= TRUE
;
319 /* Get the window object. */
320 WindowObject
= IntGetWindowObject(MsgInfo
.Msg
.hwnd
);
321 if (NULL
== WindowObject
)
323 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
324 MsgInfo
.HandledByKernel
= TRUE
;
329 if (WindowObject
->OwnerThread
!= PsGetCurrentThread())
331 IntReleaseWindowObject(WindowObject
);
332 DPRINT1("Window doesn't belong to the calling thread!\n");
333 MsgInfo
.HandledByKernel
= TRUE
;
338 /* FIXME: Call hook procedures. */
340 MsgInfo
.HandledByKernel
= FALSE
;
342 if (0xFFFF0000 != ((DWORD
) WindowObject
->WndProcW
& 0xFFFF0000))
344 if (0xFFFF0000 != ((DWORD
) WindowObject
->WndProcA
& 0xFFFF0000))
346 /* Both Unicode and Ansi winprocs are real, use whatever
348 MsgInfo
.Proc
= (MsgInfo
.Ansi
? WindowObject
->WndProcA
349 : WindowObject
->WndProcW
);
353 /* Real Unicode winproc */
354 MsgInfo
.Ansi
= FALSE
;
355 MsgInfo
.Proc
= WindowObject
->WndProcW
;
360 /* Must have real Ansi winproc */
362 MsgInfo
.Proc
= WindowObject
->WndProcA
;
365 IntReleaseWindowObject(WindowObject
);
368 Status
= MmCopyToCaller(UnsafeMsgInfo
, &MsgInfo
, sizeof(NTUSERDISPATCHMESSAGEINFO
));
369 if (! NT_SUCCESS(Status
))
371 SetLastNtError(Status
);
380 NtUserTranslateMessage(LPMSG lpMsg
,
386 Status
= MmCopyFromCaller(&SafeMsg
, lpMsg
, sizeof(MSG
));
387 if(!NT_SUCCESS(Status
))
389 SetLastNtError(Status
);
393 return IntTranslateKbdMessage(&SafeMsg
, dwhkl
);
398 IntSendHitTestMessages(PUSER_MESSAGE_QUEUE ThreadQueue
, LPMSG Msg
)
400 if(!Msg
->hwnd
|| ThreadQueue
->CaptureWindow
)
409 IntSendMessage(Msg
->hwnd
, WM_SETCURSOR
, (WPARAM
)Msg
->hwnd
, MAKELPARAM(HTCLIENT
, Msg
->message
));
414 IntSendMessage(Msg
->hwnd
, WM_SETCURSOR
, (WPARAM
)Msg
->hwnd
, MAKELPARAM(Msg
->wParam
, Msg
->message
));
421 case WM_LBUTTONDBLCLK
:
422 case WM_MBUTTONDBLCLK
:
423 case WM_RBUTTONDBLCLK
:
424 case WM_XBUTTONDBLCLK
:
427 PSYSTEM_CURSORINFO CurInfo
;
429 if(!IntGetWindowStationObject(InputWindowStation
))
433 CurInfo
= IntGetSysCursorInfo(InputWindowStation
);
434 wParam
= (WPARAM
)(CurInfo
->ButtonsDown
);
435 ObDereferenceObject(InputWindowStation
);
437 IntSendMessage(Msg
->hwnd
, WM_MOUSEMOVE
, wParam
, Msg
->lParam
);
438 IntSendMessage(Msg
->hwnd
, WM_SETCURSOR
, (WPARAM
)Msg
->hwnd
, MAKELPARAM(HTCLIENT
, Msg
->message
));
441 case WM_NCLBUTTONDOWN
:
442 case WM_NCMBUTTONDOWN
:
443 case WM_NCRBUTTONDOWN
:
444 case WM_NCXBUTTONDOWN
:
445 case WM_NCLBUTTONDBLCLK
:
446 case WM_NCMBUTTONDBLCLK
:
447 case WM_NCRBUTTONDBLCLK
:
448 case WM_NCXBUTTONDBLCLK
:
450 IntSendMessage(Msg
->hwnd
, WM_NCMOUSEMOVE
, (WPARAM
)Msg
->wParam
, Msg
->lParam
);
451 IntSendMessage(Msg
->hwnd
, WM_SETCURSOR
, (WPARAM
)Msg
->hwnd
, MAKELPARAM(Msg
->wParam
, Msg
->message
));
458 IntActivateWindowMouse(PUSER_MESSAGE_QUEUE ThreadQueue
, LPMSG Msg
, PWINDOW_OBJECT MsgWindow
,
463 if(*HitTest
== (USHORT
)HTTRANSPARENT
)
465 /* eat the message, search again! */
469 Result
= IntSendMessage(MsgWindow
->Self
, WM_MOUSEACTIVATE
, (WPARAM
)NtUserGetParent(MsgWindow
->Self
), (LPARAM
)MAKELONG(*HitTest
, Msg
->message
));
472 case MA_NOACTIVATEANDEAT
:
476 case MA_ACTIVATEANDEAT
:
477 IntMouseActivateWindow(MsgWindow
);
481 IntMouseActivateWindow(MsgWindow
);
489 IntTranslateMouseMessage(PUSER_MESSAGE_QUEUE ThreadQueue
, LPMSG Msg
, USHORT
*HitTest
, BOOL Remove
)
491 PWINDOW_OBJECT Window
;
493 if(!(Window
= IntGetWindowObject(Msg
->hwnd
)))
495 /* let's just eat the message?! */
499 if(ThreadQueue
== Window
->MessageQueue
&&
500 ThreadQueue
->CaptureWindow
!= Window
->Self
)
502 /* only send WM_NCHITTEST messages if we're not capturing the window! */
503 *HitTest
= IntSendMessage(Window
->Self
, WM_NCHITTEST
, 0,
504 MAKELONG(Msg
->pt
.x
, Msg
->pt
.y
));
506 if(*HitTest
== (USHORT
)HTTRANSPARENT
)
508 PWINDOW_OBJECT DesktopWindow
;
509 HWND hDesktop
= IntGetDesktopWindow();
511 if((DesktopWindow
= IntGetWindowObject(hDesktop
)))
515 WinPosWindowFromPoint(DesktopWindow
, Window
->MessageQueue
, &Msg
->pt
, &Wnd
);
520 /* post the message to the other window */
521 Msg
->hwnd
= Wnd
->Self
;
522 MsqPostMessage(Wnd
->MessageQueue
, Msg
, FALSE
);
524 /* eat the message */
525 IntReleaseWindowObject(Wnd
);
526 IntReleaseWindowObject(Window
);
527 IntReleaseWindowObject(DesktopWindow
);
530 IntReleaseWindowObject(Wnd
);
533 IntReleaseWindowObject(DesktopWindow
);
542 if(IS_BTN_MESSAGE(Msg
->message
, DOWN
))
544 /* generate double click messages, if necessary */
545 if ((((*HitTest
) != HTCLIENT
) ||
546 (IntGetClassLong(Window
, GCL_STYLE
, FALSE
) & CS_DBLCLKS
)) &&
547 MsqIsDblClk(Msg
, Remove
))
549 Msg
->message
+= WM_LBUTTONDBLCLK
- WM_LBUTTONDOWN
;
553 if(Msg
->message
!= WM_MOUSEWHEEL
)
556 if ((*HitTest
) != HTCLIENT
)
558 Msg
->message
+= WM_NCMOUSEMOVE
- WM_MOUSEMOVE
;
559 if((Msg
->message
== WM_NCRBUTTONUP
) &&
560 (((*HitTest
) == HTCAPTION
) || ((*HitTest
) == HTSYSMENU
)))
562 Msg
->message
= WM_CONTEXTMENU
;
563 Msg
->wParam
= (WPARAM
)Window
->Self
;
567 Msg
->wParam
= *HitTest
;
569 Msg
->lParam
= MAKELONG(Msg
->pt
.x
, Msg
->pt
.y
);
571 else if(ThreadQueue
->MoveSize
== NULL
&&
572 ThreadQueue
->MenuOwner
== NULL
)
574 /* NOTE: Msg->pt should remain in screen coordinates. -- FiN */
575 Msg
->lParam
= MAKELONG(
576 Msg
->pt
.x
- (WORD
)Window
->ClientRect
.left
,
577 Msg
->pt
.y
- (WORD
)Window
->ClientRect
.top
);
581 IntReleaseWindowObject(Window
);
587 * Internal version of PeekMessage() doing all the work
590 IntPeekMessage(PUSER_MESSAGE Msg
,
596 LARGE_INTEGER LargeTickCount
;
597 PUSER_MESSAGE_QUEUE ThreadQueue
;
598 PUSER_MESSAGE Message
;
599 BOOL Present
, RemoveMessages
;
601 /* The queues and order in which they are checked are documented in the MSDN
602 article on GetMessage() */
604 ThreadQueue
= (PUSER_MESSAGE_QUEUE
)PsGetWin32Thread()->MessageQueue
;
606 /* Inspect RemoveMsg flags */
607 /* FIXME: The only flag we process is PM_REMOVE - processing of others must still be implemented */
608 RemoveMessages
= RemoveMsg
& PM_REMOVE
;
614 KeQueryTickCount(&LargeTickCount
);
615 ThreadQueue
->LastMsgRead
= LargeTickCount
.u
.LowPart
;
617 /* Dispatch sent messages here. */
618 while (MsqDispatchOneSentMessage(ThreadQueue
));
620 /* Now look for a quit message. */
622 if (ThreadQueue
->QuitPosted
)
624 /* According to the PSDK, WM_QUIT messages are always returned, regardless
625 of the filter specified */
626 Msg
->Msg
.hwnd
= NULL
;
627 Msg
->Msg
.message
= WM_QUIT
;
628 Msg
->Msg
.wParam
= ThreadQueue
->QuitExitCode
;
630 Msg
->FreeLParam
= FALSE
;
633 ThreadQueue
->QuitPosted
= FALSE
;
638 /* Now check for normal messages. */
639 Present
= MsqFindMessage(ThreadQueue
,
648 RtlCopyMemory(Msg
, Message
, sizeof(USER_MESSAGE
));
651 MsqDestroyMessage(Message
);
656 /* Check for hardware events. */
657 Present
= MsqFindMessage(ThreadQueue
,
666 RtlCopyMemory(Msg
, Message
, sizeof(USER_MESSAGE
));
669 MsqDestroyMessage(Message
);
674 /* Check for sent messages again. */
675 while (MsqDispatchOneSentMessage(ThreadQueue
));
677 /* Check for paint messages. */
678 if (IntGetPaintMessage(Wnd
, MsgFilterMin
, MsgFilterMax
, PsGetWin32Thread(), &Msg
->Msg
, RemoveMessages
))
680 Msg
->FreeLParam
= FALSE
;
684 /* FIXME - get WM_(SYS)TIMER messages */
692 PWINDOW_OBJECT MsgWindow
= NULL
;;
694 if(Msg
->Msg
.hwnd
&& (MsgWindow
= IntGetWindowObject(Msg
->Msg
.hwnd
)) &&
695 Msg
->Msg
.message
>= WM_MOUSEFIRST
&& Msg
->Msg
.message
<= WM_MOUSELAST
)
699 if(IntTranslateMouseMessage(ThreadQueue
, &Msg
->Msg
, &HitTest
, TRUE
))
700 /* FIXME - check message filter again, if the message doesn't match anymore,
703 IntReleaseWindowObject(MsgWindow
);
704 /* eat the message, search again */
707 if(ThreadQueue
->CaptureWindow
== NULL
)
709 IntSendHitTestMessages(ThreadQueue
, &Msg
->Msg
);
710 if((Msg
->Msg
.message
!= WM_MOUSEMOVE
&& Msg
->Msg
.message
!= WM_NCMOUSEMOVE
) &&
711 IS_BTN_MESSAGE(Msg
->Msg
.message
, DOWN
) &&
712 IntActivateWindowMouse(ThreadQueue
, &Msg
->Msg
, MsgWindow
, &HitTest
))
714 IntReleaseWindowObject(MsgWindow
);
715 /* eat the message, search again */
722 IntSendHitTestMessages(ThreadQueue
, &Msg
->Msg
);
727 IntReleaseWindowObject(MsgWindow
);
734 if((Msg
->Msg
.hwnd
&& Msg
->Msg
.message
>= WM_MOUSEFIRST
&& Msg
->Msg
.message
<= WM_MOUSELAST
) &&
735 IntTranslateMouseMessage(ThreadQueue
, &Msg
->Msg
, &HitTest
, FALSE
))
736 /* FIXME - check message filter again, if the message doesn't match anymore,
739 /* eat the message, search again */
750 NtUserPeekMessage(PNTUSERGETMESSAGEINFO UnsafeInfo
,
758 NTUSERGETMESSAGEINFO Info
;
759 PWINDOW_OBJECT Window
;
760 PMSGMEMORY MsgMemoryEntry
;
768 Window
= IntGetWindowObject(Wnd
);
775 IntReleaseWindowObject(Window
);
779 if (MsgFilterMax
< MsgFilterMin
)
785 Present
= IntPeekMessage(&Msg
, Wnd
, MsgFilterMin
, MsgFilterMax
, RemoveMsg
);
789 /* See if this message type is present in the table */
790 MsgMemoryEntry
= FindMsgMemory(Info
.Msg
.message
);
791 if (NULL
== MsgMemoryEntry
)
793 /* Not present, no copying needed */
798 /* Determine required size */
799 Size
= MsgMemorySize(MsgMemoryEntry
, Info
.Msg
.wParam
,
801 /* Allocate required amount of user-mode memory */
802 Info
.LParamSize
= Size
;
804 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(), &UserMem
, 0,
805 &Info
.LParamSize
, MEM_COMMIT
, PAGE_READWRITE
);
806 if (! NT_SUCCESS(Status
))
808 SetLastNtError(Status
);
811 /* Transfer lParam data to user-mode mem */
812 Status
= MmCopyToCaller(UserMem
, (PVOID
) Info
.Msg
.lParam
, Size
);
813 if (! NT_SUCCESS(Status
))
815 ZwFreeVirtualMemory(NtCurrentProcess(), (PVOID
*) &UserMem
,
816 &Info
.LParamSize
, MEM_DECOMMIT
);
817 SetLastNtError(Status
);
820 Info
.Msg
.lParam
= (LPARAM
) UserMem
;
822 if (Msg
.FreeLParam
&& 0 != Msg
.Msg
.lParam
)
824 ExFreePool((void *) Msg
.Msg
.lParam
);
826 Status
= MmCopyToCaller(UnsafeInfo
, &Info
, sizeof(NTUSERGETMESSAGEINFO
));
827 if (! NT_SUCCESS(Status
))
829 SetLastNtError(Status
);
838 IntWaitMessage(HWND Wnd
,
842 PUSER_MESSAGE_QUEUE ThreadQueue
;
846 ThreadQueue
= (PUSER_MESSAGE_QUEUE
)PsGetWin32Thread()->MessageQueue
;
850 if (IntPeekMessage(&Msg
, Wnd
, MsgFilterMin
, MsgFilterMax
, PM_NOREMOVE
))
855 /* Nothing found. Wait for new messages. */
856 Status
= MsqWaitForNewMessages(ThreadQueue
);
858 while (STATUS_WAIT_0
<= Status
&& Status
<= STATUS_WAIT_63
);
860 SetLastNtError(Status
);
866 NtUserGetMessage(PNTUSERGETMESSAGEINFO UnsafeInfo
,
871 * FUNCTION: Get a message from the calling thread's message queue.
873 * UnsafeMsg - Pointer to the structure which receives the returned message.
874 * Wnd - Window whose messages are to be retrieved.
875 * MsgFilterMin - Integer value of the lowest message value to be
877 * MsgFilterMax - Integer value of the highest message value to be
882 NTUSERGETMESSAGEINFO Info
;
884 PWINDOW_OBJECT Window
;
885 PMSGMEMORY MsgMemoryEntry
;
893 Window
= IntGetWindowObject(Wnd
);
897 IntReleaseWindowObject(Window
);
899 if (MsgFilterMax
< MsgFilterMin
)
907 GotMessage
= IntPeekMessage(&Msg
, Wnd
, MsgFilterMin
, MsgFilterMax
, PM_REMOVE
);
911 /* See if this message type is present in the table */
912 MsgMemoryEntry
= FindMsgMemory(Info
.Msg
.message
);
913 if (NULL
== MsgMemoryEntry
)
915 /* Not present, no copying needed */
920 /* Determine required size */
921 Size
= MsgMemorySize(MsgMemoryEntry
, Info
.Msg
.wParam
,
923 /* Allocate required amount of user-mode memory */
924 Info
.LParamSize
= Size
;
926 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(), &UserMem
, 0,
927 &Info
.LParamSize
, MEM_COMMIT
, PAGE_READWRITE
);
929 if (! NT_SUCCESS(Status
))
931 SetLastNtError(Status
);
934 /* Transfer lParam data to user-mode mem */
935 Status
= MmCopyToCaller(UserMem
, (PVOID
) Info
.Msg
.lParam
, Size
);
936 if (! NT_SUCCESS(Status
))
938 ZwFreeVirtualMemory(NtCurrentProcess(), (PVOID
*) &UserMem
,
939 &Info
.LParamSize
, MEM_DECOMMIT
);
940 SetLastNtError(Status
);
943 Info
.Msg
.lParam
= (LPARAM
) UserMem
;
945 if (Msg
.FreeLParam
&& 0 != Msg
.Msg
.lParam
)
947 ExFreePool((void *) Msg
.Msg
.lParam
);
949 Status
= MmCopyToCaller(UnsafeInfo
, &Info
, sizeof(NTUSERGETMESSAGEINFO
));
950 if (! NT_SUCCESS(Status
))
952 SetLastNtError(Status
);
956 else if (! IntWaitMessage(Wnd
, MsgFilterMin
, MsgFilterMax
))
961 while (! GotMessage
);
963 return WM_QUIT
!= Info
.Msg
.message
;
982 static NTSTATUS FASTCALL
983 CopyMsgToKernelMem(MSG
*KernelModeMsg
, MSG
*UserModeMsg
, PMSGMEMORY MsgMemoryEntry
)
990 *KernelModeMsg
= *UserModeMsg
;
992 /* See if this message type is present in the table */
993 if (NULL
== MsgMemoryEntry
)
995 /* Not present, no copying needed */
996 return STATUS_SUCCESS
;
999 /* Determine required size */
1000 Size
= MsgMemorySize(MsgMemoryEntry
, UserModeMsg
->wParam
, UserModeMsg
->lParam
);
1004 /* Allocate kernel mem */
1005 KernelMem
= ExAllocatePoolWithTag(PagedPool
, Size
, TAG_MSG
);
1006 if (NULL
== KernelMem
)
1008 DPRINT1("Not enough memory to copy message to kernel mem\n");
1009 return STATUS_NO_MEMORY
;
1011 KernelModeMsg
->lParam
= (LPARAM
) KernelMem
;
1013 /* Copy data if required */
1014 if (0 != (MsgMemoryEntry
->Flags
& MMS_FLAG_READ
))
1016 Status
= MmCopyFromCaller(KernelMem
, (PVOID
) UserModeMsg
->lParam
, Size
);
1017 if (! NT_SUCCESS(Status
))
1019 DPRINT1("Failed to copy message to kernel: invalid usermode buffer\n");
1020 ExFreePool(KernelMem
);
1026 /* Make sure we don't pass any secrets to usermode */
1027 RtlZeroMemory(KernelMem
, Size
);
1032 KernelModeMsg
->lParam
= 0;
1035 return STATUS_SUCCESS
;
1038 static NTSTATUS FASTCALL
1039 CopyMsgToUserMem(MSG
*UserModeMsg
, MSG
*KernelModeMsg
)
1042 PMSGMEMORY MsgMemoryEntry
;
1045 /* See if this message type is present in the table */
1046 MsgMemoryEntry
= FindMsgMemory(UserModeMsg
->message
);
1047 if (NULL
== MsgMemoryEntry
)
1049 /* Not present, no copying needed */
1050 return STATUS_SUCCESS
;
1053 /* Determine required size */
1054 Size
= MsgMemorySize(MsgMemoryEntry
, UserModeMsg
->wParam
, UserModeMsg
->lParam
);
1058 /* Copy data if required */
1059 if (0 != (MsgMemoryEntry
->Flags
& MMS_FLAG_WRITE
))
1061 Status
= MmCopyToCaller((PVOID
) UserModeMsg
->lParam
, (PVOID
) KernelModeMsg
->lParam
, Size
);
1062 if (! NT_SUCCESS(Status
))
1064 DPRINT1("Failed to copy message from kernel: invalid usermode buffer\n");
1065 ExFreePool((PVOID
) KernelModeMsg
->lParam
);
1070 ExFreePool((PVOID
) KernelModeMsg
->lParam
);
1073 return STATUS_SUCCESS
;
1077 NtUserPostMessage(HWND Wnd
,
1082 PWINDOW_OBJECT Window
;
1083 MSG UserModeMsg
, KernelModeMsg
;
1084 LARGE_INTEGER LargeTickCount
;
1086 PMSGMEMORY MsgMemoryEntry
;
1090 MsqPostQuitMessage(PsGetWin32Thread()->MessageQueue
, wParam
);
1092 else if (Wnd
== HWND_BROADCAST
)
1095 PWINDOW_OBJECT DesktopWindow
;
1098 DesktopWindow
= IntGetWindowObject(IntGetDesktopWindow());
1099 List
= IntWinListChildren(DesktopWindow
);
1100 IntReleaseWindowObject(DesktopWindow
);
1103 for (i
= 0; List
[i
]; i
++)
1104 NtUserPostMessage(List
[i
], Msg
, wParam
, lParam
);
1110 PSYSTEM_CURSORINFO CurInfo
;
1111 Window
= IntGetWindowObject(Wnd
);
1114 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
1118 UserModeMsg
.hwnd
= Wnd
;
1119 UserModeMsg
.message
= Msg
;
1120 UserModeMsg
.wParam
= wParam
;
1121 UserModeMsg
.lParam
= lParam
;
1122 MsgMemoryEntry
= FindMsgMemory(UserModeMsg
.message
);
1123 Status
= CopyMsgToKernelMem(&KernelModeMsg
, &UserModeMsg
, MsgMemoryEntry
);
1124 if (! NT_SUCCESS(Status
))
1126 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1129 CurInfo
= IntGetSysCursorInfo(PsGetWin32Process()->WindowStation
);
1130 KernelModeMsg
.pt
.x
= CurInfo
->x
;
1131 KernelModeMsg
.pt
.y
= CurInfo
->y
;
1132 KeQueryTickCount(&LargeTickCount
);
1133 KernelModeMsg
.time
= LargeTickCount
.u
.LowPart
;
1134 MsqPostMessage(Window
->MessageQueue
, &KernelModeMsg
,
1135 NULL
!= MsgMemoryEntry
&& 0 != KernelModeMsg
.lParam
);
1136 IntReleaseWindowObject(Window
);
1143 NtUserPostThreadMessage(DWORD idThread
,
1148 MSG UserModeMsg
, KernelModeMsg
;
1152 PMSGMEMORY MsgMemoryEntry
;
1154 Status
= PsLookupThreadByThreadId((void *)idThread
,&peThread
);
1156 if( Status
== STATUS_SUCCESS
) {
1157 pThread
= peThread
->Tcb
.Win32Thread
;
1158 if( !pThread
|| !pThread
->MessageQueue
)
1160 ObDereferenceObject( peThread
);
1164 UserModeMsg
.hwnd
= NULL
;
1165 UserModeMsg
.message
= Msg
;
1166 UserModeMsg
.wParam
= wParam
;
1167 UserModeMsg
.lParam
= lParam
;
1168 MsgMemoryEntry
= FindMsgMemory(UserModeMsg
.message
);
1169 Status
= CopyMsgToKernelMem(&KernelModeMsg
, &UserModeMsg
, MsgMemoryEntry
);
1170 if (! NT_SUCCESS(Status
))
1172 ObDereferenceObject( peThread
);
1173 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1176 MsqPostMessage(pThread
->MessageQueue
, &KernelModeMsg
,
1177 NULL
!= MsgMemoryEntry
&& 0 != KernelModeMsg
.lParam
);
1178 ObDereferenceObject( peThread
);
1181 SetLastNtError( Status
);
1187 NtUserQuerySendMessage(DWORD Unknown0
)
1195 IntSendMessage(HWND hWnd
,
1200 ULONG_PTR Result
= 0;
1201 if(IntSendMessageTimeout(hWnd
, Msg
, wParam
, lParam
, SMTO_NORMAL
, 0, &Result
))
1203 return (LRESULT
)Result
;
1208 static LRESULT FASTCALL
1209 IntSendMessageTimeoutSingle(HWND hWnd
,
1219 PWINDOW_OBJECT Window
;
1220 PMSGMEMORY MsgMemoryEntry
;
1221 INT lParamBufferSize
;
1222 LPARAM lParamPacked
;
1223 PW32THREAD Win32Thread
;
1225 /* FIXME: Call hooks. */
1226 Window
= IntGetWindowObject(hWnd
);
1229 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
1233 Win32Thread
= PsGetWin32Thread();
1235 if (NULL
!= Win32Thread
&&
1236 Window
->MessageQueue
== Win32Thread
->MessageQueue
)
1238 if (Win32Thread
->IsExiting
)
1240 /* Never send messages to exiting threads */
1241 IntReleaseWindowObject(Window
);
1245 /* See if this message type is present in the table */
1246 MsgMemoryEntry
= FindMsgMemory(Msg
);
1247 if (NULL
== MsgMemoryEntry
)
1249 lParamBufferSize
= -1;
1253 lParamBufferSize
= MsgMemorySize(MsgMemoryEntry
, wParam
, lParam
);
1256 if (! NT_SUCCESS(PackParam(&lParamPacked
, Msg
, wParam
, lParam
)))
1258 IntReleaseWindowObject(Window
);
1259 DPRINT1("Failed to pack message parameters\n");
1262 if (0xFFFF0000 != ((DWORD
) Window
->WndProcW
& 0xFFFF0000))
1264 Result
= (ULONG_PTR
)IntCallWindowProc(Window
->WndProcW
, FALSE
, hWnd
, Msg
, wParam
,
1265 lParamPacked
,lParamBufferSize
);
1269 Result
= (ULONG_PTR
)IntCallWindowProc(Window
->WndProcA
, TRUE
, hWnd
, Msg
, wParam
,
1270 lParamPacked
,lParamBufferSize
);
1278 if (! NT_SUCCESS(UnpackParam(lParamPacked
, Msg
, wParam
, lParam
)))
1280 IntReleaseWindowObject(Window
);
1281 DPRINT1("Failed to unpack message parameters\n");
1285 IntReleaseWindowObject(Window
);
1289 if(uFlags
& SMTO_ABORTIFHUNG
&& MsqIsHung(Window
->MessageQueue
))
1291 IntReleaseWindowObject(Window
);
1292 /* FIXME - Set a LastError? */
1296 Status
= MsqSendMessage(Window
->MessageQueue
, hWnd
, Msg
, wParam
, lParam
,
1297 uTimeout
, (uFlags
& SMTO_BLOCK
), uResult
);
1298 IntReleaseWindowObject(Window
);
1299 if (STATUS_TIMEOUT
== Status
)
1301 /* MSDN says GetLastError() should return 0 after timeout */
1302 SetLastWin32Error(0);
1305 else if (! NT_SUCCESS(Status
))
1307 SetLastNtError(Status
);
1315 IntSendMessageTimeout(HWND hWnd
,
1323 PWINDOW_OBJECT DesktopWindow
;
1327 if (HWND_BROADCAST
!= hWnd
)
1329 return IntSendMessageTimeoutSingle(hWnd
, Msg
, wParam
, lParam
, uFlags
, uTimeout
, uResult
);
1332 DesktopWindow
= IntGetWindowObject(IntGetDesktopWindow());
1333 if (NULL
== DesktopWindow
)
1335 SetLastWin32Error(ERROR_INTERNAL_ERROR
);
1338 Children
= IntWinListChildren(DesktopWindow
);
1339 IntReleaseWindowObject(DesktopWindow
);
1340 if (NULL
== Children
)
1345 for (Child
= Children
; NULL
!= *Child
; Child
++)
1347 IntSendMessageTimeoutSingle(*Child
, Msg
, wParam
, lParam
, uFlags
, uTimeout
, uResult
);
1350 ExFreePool(Children
);
1352 return (LRESULT
) TRUE
;
1356 /* This function posts a message if the destination's message queue belongs to
1357 another thread, otherwise it sends the message. It does not support broadcast
1360 IntPostOrSendMessage(HWND hWnd
,
1366 PWINDOW_OBJECT Window
;
1368 if(hWnd
== HWND_BROADCAST
)
1373 Window
= IntGetWindowObject(hWnd
);
1376 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
1380 if(Window
->MessageQueue
!= PsGetWin32Thread()->MessageQueue
)
1382 Result
= NtUserPostMessage(hWnd
, Msg
, wParam
, lParam
);
1386 if(!IntSendMessageTimeoutSingle(hWnd
, Msg
, wParam
, lParam
, SMTO_NORMAL
, 0, &Result
))
1392 IntReleaseWindowObject(Window
);
1394 return (LRESULT
)Result
;
1398 IntDoSendMessage(HWND Wnd
,
1403 PNTUSERSENDMESSAGEINFO UnsafeInfo
)
1405 LRESULT Result
= TRUE
;
1407 PWINDOW_OBJECT Window
;
1408 NTUSERSENDMESSAGEINFO Info
;
1411 PMSGMEMORY MsgMemoryEntry
;
1413 RtlZeroMemory(&Info
, sizeof(NTUSERSENDMESSAGEINFO
));
1415 /* FIXME: Call hooks. */
1416 if (HWND_BROADCAST
!= Wnd
)
1418 Window
= IntGetWindowObject(Wnd
);
1421 /* Tell usermode to not touch this one */
1422 Info
.HandledByKernel
= TRUE
;
1423 MmCopyToCaller(UnsafeInfo
, &Info
, sizeof(NTUSERSENDMESSAGEINFO
));
1424 SetLastWin32Error(ERROR_INVALID_WINDOW_HANDLE
);
1429 /* FIXME: Check for an exiting window. */
1431 /* See if the current thread can handle the message */
1432 if (HWND_BROADCAST
!= Wnd
&& NULL
!= PsGetWin32Thread() &&
1433 Window
->MessageQueue
== PsGetWin32Thread()->MessageQueue
)
1435 /* Gather the information usermode needs to call the window proc directly */
1436 Info
.HandledByKernel
= FALSE
;
1437 if (0xFFFF0000 != ((DWORD
) Window
->WndProcW
& 0xFFFF0000))
1439 if (0xFFFF0000 != ((DWORD
) Window
->WndProcA
& 0xFFFF0000))
1441 /* Both Unicode and Ansi winprocs are real, see what usermode prefers */
1442 Status
= MmCopyFromCaller(&(Info
.Ansi
), &(UnsafeInfo
->Ansi
),
1444 if (! NT_SUCCESS(Status
))
1446 Info
.Ansi
= ! Window
->Unicode
;
1448 Info
.Proc
= (Info
.Ansi
? Window
->WndProcA
: Window
->WndProcW
);
1452 /* Real Unicode winproc */
1454 Info
.Proc
= Window
->WndProcW
;
1459 /* Must have real Ansi winproc */
1461 Info
.Proc
= Window
->WndProcA
;
1463 IntReleaseWindowObject(Window
);
1467 /* Must be handled by other thread */
1468 if (HWND_BROADCAST
!= Wnd
)
1470 IntReleaseWindowObject(Window
);
1472 Info
.HandledByKernel
= TRUE
;
1473 UserModeMsg
.hwnd
= Wnd
;
1474 UserModeMsg
.message
= Msg
;
1475 UserModeMsg
.wParam
= wParam
;
1476 UserModeMsg
.lParam
= lParam
;
1477 MsgMemoryEntry
= FindMsgMemory(UserModeMsg
.message
);
1478 Status
= CopyMsgToKernelMem(&KernelModeMsg
, &UserModeMsg
, MsgMemoryEntry
);
1479 if (! NT_SUCCESS(Status
))
1481 MmCopyToCaller(UnsafeInfo
, &Info
, sizeof(NTUSERSENDMESSAGEINFO
));
1482 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1483 return (dsm
? 0 : -1);
1487 Result
= IntSendMessage(KernelModeMsg
.hwnd
, KernelModeMsg
.message
,
1488 KernelModeMsg
.wParam
, KernelModeMsg
.lParam
);
1492 Result
= IntSendMessageTimeout(KernelModeMsg
.hwnd
, KernelModeMsg
.message
,
1493 KernelModeMsg
.wParam
, KernelModeMsg
.lParam
,
1494 dsm
->uFlags
, dsm
->uTimeout
, &dsm
->Result
);
1496 Status
= CopyMsgToUserMem(&UserModeMsg
, &KernelModeMsg
);
1497 if (! NT_SUCCESS(Status
))
1499 MmCopyToCaller(UnsafeInfo
, &Info
, sizeof(NTUSERSENDMESSAGEINFO
));
1500 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1501 return(dsm
? 0 : -1);
1505 Status
= MmCopyToCaller(UnsafeInfo
, &Info
, sizeof(NTUSERSENDMESSAGEINFO
));
1506 if (! NT_SUCCESS(Status
))
1508 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1511 return (LRESULT
)Result
;
1515 NtUserSendMessageTimeout(HWND hWnd
,
1522 PNTUSERSENDMESSAGEINFO UnsafeInfo
)
1527 dsm
.uFlags
= uFlags
;
1528 dsm
.uTimeout
= uTimeout
;
1529 Result
= IntDoSendMessage(hWnd
, Msg
, wParam
, lParam
, &dsm
, UnsafeInfo
);
1530 if(uResult
!= NULL
&& Result
!= 0)
1534 Status
= MmCopyToCaller(uResult
, &dsm
.Result
, sizeof(ULONG_PTR
));
1535 if(!NT_SUCCESS(Status
))
1537 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1545 NtUserSendMessage(HWND Wnd
,
1549 PNTUSERSENDMESSAGEINFO UnsafeInfo
)
1551 return IntDoSendMessage(Wnd
, Msg
, wParam
, lParam
, NULL
, UnsafeInfo
);
1555 NtUserSendMessageCallback(HWND hWnd
,
1559 SENDASYNCPROC lpCallBack
,
1568 NtUserSendNotifyMessage(HWND hWnd
,
1579 NtUserWaitMessage(VOID
)
1582 return IntWaitMessage(NULL
, 0, 0);
1586 NtUserGetQueueStatus(BOOL ClearChanges
)
1588 PUSER_MESSAGE_QUEUE Queue
;
1591 Queue
= PsGetWin32Thread()->MessageQueue
;
1593 IntLockMessageQueue(Queue
);
1595 Result
= MAKELONG(Queue
->ChangedBits
, Queue
->WakeBits
);
1598 Queue
->ChangedBits
= 0;
1601 IntUnLockMessageQueue(Queue
);
1607 IntInitMessagePumpHook()
1609 PsGetCurrentThread()->Tcb
.Win32Thread
->MessagePumpHookValue
++;
1614 IntUninitMessagePumpHook()
1616 if (PsGetCurrentThread()->Tcb
.Win32Thread
->MessagePumpHookValue
<= 0)
1620 PsGetCurrentThread()->Tcb
.Win32Thread
->MessagePumpHookValue
--;