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.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
23 * PURPOSE: Window classes
24 * FILE: subsys/win32k/ntuser/wndproc.c
25 * PROGRAMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
26 * Thomas Weidenmueller (w3seek@users.sourceforge.net)
28 * 06-06-2001 CSH Created
29 * NOTES: Please use the Callback Memory Management functions for
30 * callbacks to make sure, the memory is freed on thread
34 /* INCLUDES ******************************************************************/
41 /* CALLBACK MEMORY MANAGEMENT ************************************************/
43 typedef struct _INT_CALLBACK_HEADER
45 /* list entry in the THREADINFO structure */
48 INT_CALLBACK_HEADER
, *PINT_CALLBACK_HEADER
;
51 IntCbAllocateMemory(ULONG Size
)
53 PINT_CALLBACK_HEADER Mem
;
54 PTHREADINFO W32Thread
;
56 if(!(Mem
= ExAllocatePoolWithTag(PagedPool
, Size
+ sizeof(INT_CALLBACK_HEADER
),
62 W32Thread
= PsGetCurrentThreadWin32Thread();
65 /* insert the callback memory into the thread's callback list */
67 InsertTailList(&W32Thread
->W32CallbackListHead
, &Mem
->ListEntry
);
73 IntCbFreeMemory(PVOID Data
)
75 PINT_CALLBACK_HEADER Mem
;
76 PTHREADINFO W32Thread
;
80 Mem
= ((PINT_CALLBACK_HEADER
)Data
- 1);
82 W32Thread
= PsGetCurrentThreadWin32Thread();
85 /* remove the memory block from the thread's callback list */
86 RemoveEntryList(&Mem
->ListEntry
);
89 ExFreePoolWithTag(Mem
, TAG_CALLBACK
);
93 IntCleanupThreadCallbacks(PTHREADINFO W32Thread
)
95 PLIST_ENTRY CurrentEntry
;
96 PINT_CALLBACK_HEADER Mem
;
98 while (!IsListEmpty(&W32Thread
->W32CallbackListHead
))
100 CurrentEntry
= RemoveHeadList(&W32Thread
->W32CallbackListHead
);
101 Mem
= CONTAINING_RECORD(CurrentEntry
, INT_CALLBACK_HEADER
,
111 // Pass the Current Window handle and pointer to the Client Callback.
112 // This will help user space programs speed up read access with the window object.
115 IntSetTebWndCallback (HWND
* hWnd
, PVOID
* pWnd
)
118 PWINDOW_OBJECT Window
= UserGetWindowObject(*hWnd
);
119 PCLIENTINFO ClientInfo
= GetWin32ClientInfo();
121 *hWnd
= ClientInfo
->CallbackWnd
.hWnd
;
122 *pWnd
= ClientInfo
->CallbackWnd
.pvWnd
;
124 ClientInfo
->CallbackWnd
.hWnd
= hWndS
;
125 ClientInfo
->CallbackWnd
.pvWnd
= DesktopHeapAddressToUser(Window
->Wnd
);
129 IntRestoreTebWndCallback (HWND hWnd
, PVOID pWnd
)
131 PCLIENTINFO ClientInfo
= GetWin32ClientInfo();
133 ClientInfo
->CallbackWnd
.hWnd
= hWnd
;
134 ClientInfo
->CallbackWnd
.pvWnd
= pWnd
;
137 /* FUNCTIONS *****************************************************************/
140 co_IntCallSentMessageCallback(SENDASYNCPROC CompletionCallback
,
143 ULONG_PTR CompletionCallbackContext
,
146 SENDASYNCPROC_CALLBACK_ARGUMENTS Arguments
;
147 PVOID ResultPointer
, pWnd
;
151 Arguments
.Callback
= CompletionCallback
;
152 Arguments
.Wnd
= hWnd
;
154 Arguments
.Context
= CompletionCallbackContext
;
155 Arguments
.Result
= Result
;
157 IntSetTebWndCallback (&hWnd
, &pWnd
);
161 Status
= KeUserModeCallback(USER32_CALLBACK_SENDASYNCPROC
,
163 sizeof(SENDASYNCPROC_CALLBACK_ARGUMENTS
),
169 IntRestoreTebWndCallback (hWnd
, pWnd
);
171 if (!NT_SUCCESS(Status
))
179 co_IntCallWindowProc(WNDPROC Proc
,
185 INT lParamBufferSize
)
187 WINDOWPROC_CALLBACK_ARGUMENTS StackArguments
;
188 PWINDOWPROC_CALLBACK_ARGUMENTS Arguments
;
190 PVOID ResultPointer
, pWnd
;
192 ULONG ArgumentLength
;
195 if (0 < lParamBufferSize
)
197 ArgumentLength
= sizeof(WINDOWPROC_CALLBACK_ARGUMENTS
) + lParamBufferSize
;
198 Arguments
= IntCbAllocateMemory(ArgumentLength
);
199 if (NULL
== Arguments
)
201 DPRINT1("Unable to allocate buffer for window proc callback\n");
204 RtlMoveMemory((PVOID
) ((char *) Arguments
+ sizeof(WINDOWPROC_CALLBACK_ARGUMENTS
)),
205 (PVOID
) lParam
, lParamBufferSize
);
209 Arguments
= &StackArguments
;
210 ArgumentLength
= sizeof(WINDOWPROC_CALLBACK_ARGUMENTS
);
212 Arguments
->Proc
= Proc
;
213 Arguments
->IsAnsiProc
= IsAnsiProc
;
214 Arguments
->Wnd
= Wnd
;
215 Arguments
->Msg
= Message
;
216 Arguments
->wParam
= wParam
;
217 Arguments
->lParam
= lParam
;
218 Arguments
->lParamBufferSize
= lParamBufferSize
;
219 ResultPointer
= NULL
;
220 ResultLength
= ArgumentLength
;
222 IntSetTebWndCallback (&Wnd
, &pWnd
);
226 Status
= KeUserModeCallback(USER32_CALLBACK_WINDOWPROC
,
234 /* Simulate old behaviour: copy into our local buffer */
235 RtlMoveMemory(Arguments
, ResultPointer
, ArgumentLength
);
237 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
239 Status
= _SEH2_GetExceptionCode();
245 IntRestoreTebWndCallback (Wnd
, pWnd
);
247 if (!NT_SUCCESS(Status
))
249 if (0 < lParamBufferSize
)
251 IntCbFreeMemory(Arguments
);
255 Result
= Arguments
->Result
;
257 if (0 < lParamBufferSize
)
259 RtlMoveMemory((PVOID
) lParam
,
260 (PVOID
) ((char *) Arguments
+ sizeof(WINDOWPROC_CALLBACK_ARGUMENTS
)),
262 IntCbFreeMemory(Arguments
);
269 co_IntLoadSysMenuTemplate()
276 ResultPointer
= NULL
;
277 ResultLength
= sizeof(LRESULT
);
281 Status
= KeUserModeCallback(USER32_CALLBACK_LOADSYSMENUTEMPLATE
,
287 /* Simulate old behaviour: copy into our local buffer */
288 Result
= *(LRESULT
*)ResultPointer
;
292 if (!NT_SUCCESS(Status
))
296 return (HMENU
)Result
;
300 co_IntLoadDefaultCursors(VOID
)
306 BOOL DefaultCursor
= TRUE
;
308 ResultPointer
= NULL
;
309 ResultLength
= sizeof(LRESULT
);
313 Status
= KeUserModeCallback(USER32_CALLBACK_LOADDEFAULTCURSORS
,
319 /* Simulate old behaviour: copy into our local buffer */
320 Result
= *(LRESULT
*)ResultPointer
;
324 if (!NT_SUCCESS(Status
))
332 co_IntCallHookProc(INT HookId
,
338 PUNICODE_STRING ModuleName
)
340 ULONG ArgumentLength
;
346 PHOOKPROC_CALLBACK_ARGUMENTS Common
;
347 CBT_CREATEWNDW
*CbtCreateWnd
=NULL
;
349 PHOOKPROC_CBT_CREATEWND_EXTRA_ARGUMENTS CbtCreatewndExtra
;
350 PUNICODE_STRING WindowName
= NULL
;
351 PUNICODE_STRING ClassName
= NULL
;
353 ArgumentLength
= sizeof(HOOKPROC_CALLBACK_ARGUMENTS
) - sizeof(WCHAR
)
354 + ModuleName
->Length
;
361 CbtCreateWnd
= (CBT_CREATEWNDW
*) lParam
;
362 ArgumentLength
+= sizeof(HOOKPROC_CBT_CREATEWND_EXTRA_ARGUMENTS
);
363 WindowName
= (PUNICODE_STRING
) (CbtCreateWnd
->lpcs
->lpszName
);
364 ArgumentLength
+= WindowName
->Length
+ sizeof(WCHAR
);
365 ClassName
= (PUNICODE_STRING
) (CbtCreateWnd
->lpcs
->lpszClass
);
366 if (! IS_ATOM(ClassName
->Buffer
))
368 ArgumentLength
+= ClassName
->Length
+ sizeof(WCHAR
);
372 DPRINT1("Trying to call unsupported CBT hook %d\n", Code
);
377 ArgumentLength
+= sizeof(KBDLLHOOKSTRUCT
);
380 ArgumentLength
+= sizeof(MSLLHOOKSTRUCT
);
383 ArgumentLength
+= sizeof(MOUSEHOOKSTRUCT
);
386 ArgumentLength
+= sizeof(CWPSTRUCT
);
388 case WH_CALLWNDPROCRET
:
389 ArgumentLength
+= sizeof(CWPRETSTRUCT
);
392 case WH_SYSMSGFILTER
:
394 ArgumentLength
+= sizeof(MSG
);
396 case WH_FOREGROUNDIDLE
:
401 DPRINT1("Trying to call unsupported window hook %d\n", HookId
);
405 Argument
= IntCbAllocateMemory(ArgumentLength
);
406 if (NULL
== Argument
)
408 DPRINT1("HookProc callback failed: out of memory\n");
411 Common
= (PHOOKPROC_CALLBACK_ARGUMENTS
) Argument
;
412 Common
->HookId
= HookId
;
414 Common
->wParam
= wParam
;
415 Common
->lParam
= lParam
;
418 Common
->ModuleNameLength
= ModuleName
->Length
;
419 memcpy(Common
->ModuleName
, ModuleName
->Buffer
, ModuleName
->Length
);
420 Extra
= (PCHAR
) Common
->ModuleName
+ Common
->ModuleNameLength
;
428 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
429 CbtCreatewndExtra
= (PHOOKPROC_CBT_CREATEWND_EXTRA_ARGUMENTS
) Extra
;
430 CbtCreatewndExtra
->Cs
= *(CbtCreateWnd
->lpcs
);
431 CbtCreatewndExtra
->WndInsertAfter
= CbtCreateWnd
->hwndInsertAfter
;
432 Extra
= (PCHAR
) (CbtCreatewndExtra
+ 1);
433 RtlCopyMemory(Extra
, WindowName
->Buffer
, WindowName
->Length
);
434 CbtCreatewndExtra
->Cs
.lpszName
= (LPCWSTR
) (Extra
- (PCHAR
) CbtCreatewndExtra
);
435 CbtCreatewndExtra
->Cs
.lpszClass
= ClassName
->Buffer
;
436 Extra
+= WindowName
->Length
;
437 *((WCHAR
*) Extra
) = L
'\0';
438 Extra
+= sizeof(WCHAR
);
439 if (! IS_ATOM(ClassName
->Buffer
))
441 RtlCopyMemory(Extra
, ClassName
->Buffer
, ClassName
->Length
);
442 CbtCreatewndExtra
->Cs
.lpszClass
=
443 (LPCWSTR
)(ULONG_PTR
) MAKELONG(Extra
- (PCHAR
) CbtCreatewndExtra
, 1);
444 Extra
+= ClassName
->Length
;
445 *((WCHAR
*) Extra
) = L
'\0';
451 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(KBDLLHOOKSTRUCT
));
452 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
455 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(MSLLHOOKSTRUCT
));
456 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
459 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(MOUSEHOOKSTRUCT
));
460 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
463 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(CWPSTRUCT
));
464 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
466 case WH_CALLWNDPROCRET
:
467 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(CWPRETSTRUCT
));
468 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
471 case WH_SYSMSGFILTER
:
473 RtlCopyMemory(Extra
, (PVOID
) lParam
, sizeof(MSG
));
474 Common
->lParam
= (LPARAM
) (Extra
- (PCHAR
) Common
);
475 // DPRINT1("KHOOK Memory: %x\n",Common);
477 case WH_FOREGROUNDIDLE
:
483 ResultPointer
= NULL
;
484 ResultLength
= sizeof(LRESULT
);
488 Status
= KeUserModeCallback(USER32_CALLBACK_HOOKPROC
,
498 ProbeForRead((PVOID
)*(LRESULT
*)ResultPointer
,
501 /* Simulate old behaviour: copy into our local buffer */
502 Result
= *(LRESULT
*)ResultPointer
;
504 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
510 if (!NT_SUCCESS(Status
))
515 if (Argument
) IntCbFreeMemory(Argument
);
522 co_IntCallEventProc(HWINEVENTHOOK hook
,
533 PEVENTPROC_CALLBACK_ARGUMENTS Common
;
534 ULONG ArgumentLength
, ResultLength
;
535 PVOID Argument
, ResultPointer
, pWnd
;
537 ArgumentLength
= sizeof(EVENTPROC_CALLBACK_ARGUMENTS
);
539 Argument
= IntCbAllocateMemory(ArgumentLength
);
540 if (NULL
== Argument
)
542 DPRINT1("EventProc callback failed: out of memory\n");
545 Common
= (PEVENTPROC_CALLBACK_ARGUMENTS
) Argument
;
547 Common
->event
= event
;
549 Common
->idObject
= idObject
;
550 Common
->idChild
= idChild
;
551 Common
->dwEventThread
= dwEventThread
;
552 Common
->dwmsEventTime
= dwmsEventTime
;
555 ResultPointer
= NULL
;
556 ResultLength
= sizeof(LRESULT
);
558 IntSetTebWndCallback (&hWnd
, &pWnd
);
562 Status
= KeUserModeCallback(USER32_CALLBACK_EVENTPROC
,
568 /* Simulate old behaviour: copy into our local buffer */
569 Result
= *(LRESULT
*)ResultPointer
;
573 IntRestoreTebWndCallback (hWnd
, pWnd
);
575 IntCbFreeMemory(Argument
);
577 if (!NT_SUCCESS(Status
))