2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32k subsystem
5 * FILE: subsystems/win32/win32k/ntuser/desktop.c
6 * PROGRAMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
9 /* INCLUDES ******************************************************************/
12 DBG_DEFAULT_CHANNEL(UserDesktop
);
15 UserInitializeDesktop(PDESKTOP pdesk
, PUNICODE_STRING DesktopName
, PWINSTATION_OBJECT pwinsta
);
18 IntMapDesktopView(IN PDESKTOP pdesk
);
21 IntUnmapDesktopView(IN PDESKTOP pdesk
);
24 IntFreeDesktopHeap(IN PDESKTOP pdesk
);
26 /* GLOBALS *******************************************************************/
28 /* Currently active desktop */
29 PDESKTOP gpdeskInputDesktop
= NULL
;
30 HDC ScreenDeviceContext
= NULL
;
31 PTHREADINFO gptiDesktopThread
= NULL
;
32 HCURSOR gDesktopCursor
= NULL
;
34 /* OBJECT CALLBACKS **********************************************************/
38 IntDesktopObjectParse(IN PVOID ParseObject
,
40 IN OUT PACCESS_STATE AccessState
,
41 IN KPROCESSOR_MODE AccessMode
,
43 IN OUT PUNICODE_STRING CompleteName
,
44 IN OUT PUNICODE_STRING RemainingName
,
45 IN OUT PVOID Context OPTIONAL
,
46 IN PSECURITY_QUALITY_OF_SERVICE SecurityQos OPTIONAL
,
51 OBJECT_ATTRIBUTES ObjectAttributes
;
52 PLIST_ENTRY NextEntry
, ListHead
;
53 PWINSTATION_OBJECT WinStaObject
= (PWINSTATION_OBJECT
)ParseObject
;
54 UNICODE_STRING DesktopName
;
55 PBOOLEAN pContext
= (PBOOLEAN
) Context
;
60 /* Set the list pointers and loop the window station */
61 ListHead
= &WinStaObject
->DesktopListHead
;
62 NextEntry
= ListHead
->Flink
;
63 while (NextEntry
!= ListHead
)
65 /* Get the current desktop */
66 Desktop
= CONTAINING_RECORD(NextEntry
, DESKTOP
, ListEntry
);
68 /* Get the desktop name */
69 ASSERT(Desktop
->pDeskInfo
!= NULL
);
70 RtlInitUnicodeString(&DesktopName
, Desktop
->pDeskInfo
->szDesktopName
);
72 /* Compare the name */
73 if (RtlEqualUnicodeString(RemainingName
,
75 (Attributes
& OBJ_CASE_INSENSITIVE
) != 0))
77 /* We found a match. Did this come from a create? */
80 /* Unless OPEN_IF was given, fail with an error */
81 if (!(Attributes
& OBJ_OPENIF
))
84 return STATUS_OBJECT_NAME_COLLISION
;
88 /* Otherwise, return with a warning only */
89 Status
= STATUS_OBJECT_NAME_EXISTS
;
94 /* This was a real open, so this is OK */
95 Status
= STATUS_SUCCESS
;
98 /* Reference the desktop and return it */
99 ObReferenceObject(Desktop
);
104 /* Go to the next desktop */
105 NextEntry
= NextEntry
->Flink
;
108 /* If we got here but this isn't a create, then fail */
109 if (!Context
) return STATUS_OBJECT_NAME_NOT_FOUND
;
111 /* Create the desktop object */
112 InitializeObjectAttributes(&ObjectAttributes
, RemainingName
, 0, NULL
, NULL
);
113 Status
= ObCreateObject(KernelMode
,
122 if (!NT_SUCCESS(Status
)) return Status
;
124 /* Initialize the desktop */
125 Status
= UserInitializeDesktop(Desktop
, RemainingName
, WinStaObject
);
126 if (!NT_SUCCESS(Status
))
128 ObDereferenceObject(Desktop
);
132 /* Set the desktop object and return success */
135 return STATUS_SUCCESS
;
140 IntDesktopObjectDelete(
141 _In_ PVOID Parameters
)
143 PWIN32_DELETEMETHOD_PARAMETERS DeleteParameters
= Parameters
;
144 PDESKTOP pdesk
= (PDESKTOP
)DeleteParameters
->Object
;
146 TRACE("Deleting desktop object 0x%p\n", pdesk
);
148 ASSERT(pdesk
->pDeskInfo
->spwnd
->spwndChild
== NULL
);
150 if (pdesk
->pDeskInfo
->spwnd
)
151 co_UserDestroyWindow(pdesk
->pDeskInfo
->spwnd
);
153 if (pdesk
->spwndMessage
)
154 co_UserDestroyWindow(pdesk
->spwndMessage
);
156 /* Remove the desktop from the window station's list of associcated desktops */
157 RemoveEntryList(&pdesk
->ListEntry
);
160 IntFreeDesktopHeap(pdesk
);
161 return STATUS_SUCCESS
;
167 _In_ PVOID Parameters
)
169 PWIN32_OKAYTOCLOSEMETHOD_PARAMETERS OkToCloseParameters
= Parameters
;
170 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
174 /* This happens when we leak desktop handles */
175 return STATUS_SUCCESS
;
178 /* Do not allow the current desktop or the initial desktop to be closed */
179 if( OkToCloseParameters
->Handle
== pti
->ppi
->hdeskStartup
||
180 OkToCloseParameters
->Handle
== pti
->hdesk
)
182 return STATUS_ACCESS_DENIED
;
185 return STATUS_SUCCESS
;
190 IntDesktopObjectOpen(
191 _In_ PVOID Parameters
)
193 PWIN32_OPENMETHOD_PARAMETERS OpenParameters
= Parameters
;
194 PPROCESSINFO ppi
= PsGetProcessWin32Process(OpenParameters
->Process
);
196 return STATUS_SUCCESS
;
198 return IntMapDesktopView((PDESKTOP
)OpenParameters
->Object
);
203 IntDesktopObjectClose(
204 _In_ PVOID Parameters
)
206 PWIN32_CLOSEMETHOD_PARAMETERS CloseParameters
= Parameters
;
207 PPROCESSINFO ppi
= PsGetProcessWin32Process(CloseParameters
->Process
);
210 /* This happens when the process leaks desktop handles.
211 * At this point the PPROCESSINFO is already destroyed */
212 return STATUS_SUCCESS
;
215 return IntUnmapDesktopView((PDESKTOP
)CloseParameters
->Object
);
219 /* PRIVATE FUNCTIONS **********************************************************/
224 InitDesktopImpl(VOID
)
226 GENERIC_MAPPING IntDesktopMapping
= { DESKTOP_READ
,
231 /* Set Desktop Object Attributes */
232 ExDesktopObjectType
->TypeInfo
.DefaultNonPagedPoolCharge
= sizeof(DESKTOP
);
233 ExDesktopObjectType
->TypeInfo
.GenericMapping
= IntDesktopMapping
;
234 ExDesktopObjectType
->TypeInfo
.ValidAccessMask
= DESKTOP_ALL_ACCESS
;
235 return STATUS_SUCCESS
;
238 static int GetSystemVersionString(LPWSTR buffer
)
240 RTL_OSVERSIONINFOEXW versionInfo
;
243 versionInfo
.dwOSVersionInfoSize
= sizeof(RTL_OSVERSIONINFOEXW
);
245 if (!NT_SUCCESS(RtlGetVersion((PRTL_OSVERSIONINFOW
)&versionInfo
)))
248 if (versionInfo
.dwMajorVersion
<= 4)
249 len
= swprintf(buffer
,
250 L
"ReactOS Version %lu.%lu %s Build %lu",
251 versionInfo
.dwMajorVersion
, versionInfo
.dwMinorVersion
,
252 versionInfo
.szCSDVersion
, versionInfo
.dwBuildNumber
&0xFFFF);
254 len
= swprintf(buffer
,
255 L
"ReactOS %s (Build %lu)",
256 versionInfo
.szCSDVersion
, versionInfo
.dwBuildNumber
&0xFFFF);
263 IntParseDesktopPath(PEPROCESS Process
,
264 PUNICODE_STRING DesktopPath
,
268 OBJECT_ATTRIBUTES ObjectAttributes
;
269 UNICODE_STRING ObjectName
;
271 WCHAR wstrWinstaFullName
[MAX_PATH
], *pwstrWinsta
= NULL
, *pwstrDesktop
= NULL
;
280 if(DesktopPath
->Buffer
!= NULL
&& DesktopPath
->Length
> sizeof(WCHAR
))
283 * Parse the desktop path string which can be in the form "WinSta\Desktop"
284 * or just "Desktop". In latter case WinSta0 will be used.
287 pwstrDesktop
= wcschr(DesktopPath
->Buffer
, L
'\\');
288 if(pwstrDesktop
!= NULL
)
292 pwstrWinsta
= DesktopPath
->Buffer
;
296 pwstrDesktop
= DesktopPath
->Buffer
;
300 TRACE("IntParseDesktopPath pwstrWinsta:%S pwstrDesktop:%S\n", pwstrWinsta
, pwstrDesktop
);
304 /* Search the process handle table for (inherited) window station
305 handles, use a more appropriate one than WinSta0 if possible. */
306 if (!ObFindHandleForObject(Process
,
308 ExWindowStationObjectType
,
313 /* We had no luck searching for opened handles, use WinSta0 now */
315 pwstrWinsta
= L
"WinSta0";
319 /* Search the process handle table for (inherited) desktop
320 handles, use a more appropriate one than Default if possible. */
321 if (!ObFindHandleForObject(Process
,
328 /* We had no luck searching for opened handles, use Desktop now */
330 pwstrDesktop
= L
"Default";
335 swprintf(wstrWinstaFullName
, L
"%wZ\\%ws", &gustrWindowStationsDir
, pwstrWinsta
);
336 RtlInitUnicodeString( &ObjectName
, wstrWinstaFullName
);
338 TRACE("parsed initial winsta: %wZ\n", &ObjectName
);
340 /* Open the window station */
341 InitializeObjectAttributes(&ObjectAttributes
,
343 OBJ_CASE_INSENSITIVE
,
347 Status
= ObOpenObjectByName(&ObjectAttributes
,
348 ExWindowStationObjectType
,
355 if(!NT_SUCCESS(Status
))
357 SetLastNtError(Status
);
358 ERR("Failed to reference window station %wZ PID: --!\n", &ObjectName
);
363 if(*hDesktop
== NULL
)
365 RtlInitUnicodeString(&ObjectName
, pwstrDesktop
);
367 TRACE("parsed initial desktop: %wZ\n", &ObjectName
);
369 /* Open the desktop object */
370 InitializeObjectAttributes(&ObjectAttributes
,
372 OBJ_CASE_INSENSITIVE
,
376 Status
= ObOpenObjectByName(&ObjectAttributes
,
384 if(!NT_SUCCESS(Status
))
389 SetLastNtError(Status
);
390 ERR("Failed to reference desktop %wZ PID: --!\n", &ObjectName
);
394 return STATUS_SUCCESS
;
398 * IntValidateDesktopHandle
400 * Validates the desktop handle.
403 * If the function succeeds, the handle remains referenced. If the
404 * fucntion fails, last error is set.
408 IntValidateDesktopHandle(
410 KPROCESSOR_MODE AccessMode
,
411 ACCESS_MASK DesiredAccess
,
416 Status
= ObReferenceObjectByHandle(
424 TRACE("IntValidateDesktopHandle: handle:0x%p obj:0x%p access:0x%x Status:0x%lx\n",
425 Desktop
, *Object
, DesiredAccess
, Status
);
427 if (!NT_SUCCESS(Status
))
428 SetLastNtError(Status
);
434 IntGetActiveDesktop(VOID
)
436 return gpdeskInputDesktop
;
440 * Returns or creates a handle to the desktop object
443 IntGetDesktopObjectHandle(PDESKTOP DesktopObject
)
448 ASSERT(DesktopObject
);
450 if (!ObFindHandleForObject(PsGetCurrentProcess(),
456 Status
= ObOpenObjectByPointer(DesktopObject
,
463 if(!NT_SUCCESS(Status
))
465 /* Unable to create a handle */
466 ERR("Unable to create a desktop handle\n");
472 ERR("Got handle: %p\n", Ret
);
478 PUSER_MESSAGE_QUEUE FASTCALL
479 IntGetFocusMessageQueue(VOID
)
481 PDESKTOP pdo
= IntGetActiveDesktop();
484 TRACE("No active desktop\n");
487 return (PUSER_MESSAGE_QUEUE
)pdo
->ActiveMessageQueue
;
491 IntSetFocusMessageQueue(PUSER_MESSAGE_QUEUE NewQueue
)
493 PUSER_MESSAGE_QUEUE Old
;
494 PDESKTOP pdo
= IntGetActiveDesktop();
497 TRACE("No active desktop\n");
502 if(NewQueue
->Desktop
!= NULL
)
504 TRACE("Message Queue already attached to another desktop!\n");
507 IntReferenceMessageQueue(NewQueue
);
508 (void)InterlockedExchangePointer((PVOID
*)&NewQueue
->Desktop
, pdo
);
510 Old
= (PUSER_MESSAGE_QUEUE
)InterlockedExchangePointer((PVOID
*)&pdo
->ActiveMessageQueue
, NewQueue
);
513 (void)InterlockedExchangePointer((PVOID
*)&Old
->Desktop
, 0);
514 IntDereferenceMessageQueue(Old
);
515 gpqForegroundPrev
= Old
;
517 // Only one Q can have active foreground even when there are more than one desktop.
520 gpqForeground
= pdo
->ActiveMessageQueue
;
524 gpqForeground
= NULL
;
525 ERR("ptiLastInput is CLEARED!!\n");
526 ptiLastInput
= NULL
; // ReactOS hacks,,,, should check for process death.
531 IntGetThreadDesktopWindow(PTHREADINFO pti
)
533 if (!pti
) pti
= PsGetCurrentThreadWin32Thread();
534 if (pti
->pDeskInfo
) return pti
->pDeskInfo
->spwnd
;
538 PWND FASTCALL
co_GetDesktopWindow(PWND pWnd
)
540 if (pWnd
->head
.rpdesk
&&
541 pWnd
->head
.rpdesk
->pDeskInfo
)
542 return pWnd
->head
.rpdesk
->pDeskInfo
->spwnd
;
546 HWND FASTCALL
IntGetDesktopWindow(VOID
)
548 PDESKTOP pdo
= IntGetActiveDesktop();
551 TRACE("No active desktop\n");
554 return pdo
->DesktopWindow
;
557 PWND FASTCALL
UserGetDesktopWindow(VOID
)
559 PDESKTOP pdo
= IntGetActiveDesktop();
563 TRACE("No active desktop\n");
566 // return pdo->pDeskInfo->spwnd;
567 return UserGetWindowObject(pdo
->DesktopWindow
);
570 HWND FASTCALL
IntGetMessageWindow(VOID
)
572 PDESKTOP pdo
= IntGetActiveDesktop();
576 TRACE("No active desktop\n");
579 return pdo
->spwndMessage
->head
.h
;
582 PWND FASTCALL
UserGetMessageWindow(VOID
)
584 PDESKTOP pdo
= IntGetActiveDesktop();
588 TRACE("No active desktop\n");
591 return pdo
->spwndMessage
;
594 HWND FASTCALL
IntGetCurrentThreadDesktopWindow(VOID
)
596 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
597 PDESKTOP pdo
= pti
->rpdesk
;
600 ERR("Thread doesn't have a desktop\n");
603 return pdo
->DesktopWindow
;
606 /* PUBLIC FUNCTIONS ***********************************************************/
609 DesktopWindowProc(PWND Wnd
, UINT Msg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*lResult
)
613 //ERR("DesktopWindowProc\n");
622 Wnd
->fnid
= FNID_DESKTOP
;
624 *lResult
= (LRESULT
)TRUE
;
628 Value
= HandleToULong(PsGetCurrentProcessId());
630 co_UserSetWindowLong(UserHMGetHandle(Wnd
), DT_GWL_PROCESSID
, Value
, FALSE
);
631 Value
= HandleToULong(PsGetCurrentThreadId());
633 co_UserSetWindowLong(UserHMGetHandle(Wnd
), DT_GWL_THREADID
, Value
, FALSE
);
637 case WM_DISPLAYCHANGE
:
638 co_WinPosSetWindowPos(Wnd
, 0, 0, 0, LOWORD(lParam
), HIWORD(lParam
), SWP_NOZORDER
| SWP_NOACTIVATE
);
642 IntPaintDesktop((HDC
)wParam
);
648 if (IntBeginPaint(Wnd
, &Ps
))
650 IntEndPaint(Wnd
, &Ps
);
654 case WM_SYSCOLORCHANGE
:
655 co_UserRedrawWindow(Wnd
, NULL
, NULL
, RDW_INVALIDATE
|RDW_ERASE
|RDW_ALLCHILDREN
);
659 PCURICON_OBJECT pcurOld
, pcurNew
;
660 pcurNew
= UserGetCurIconObject(gDesktopCursor
);
665 #ifdef NEW_CURSORICON
666 pcurNew
->CURSORF_flags
|= CURSORF_CURRENT
;
668 pcurOld
= UserSetCursor(pcurNew
, FALSE
);
671 #ifdef NEW_CURSORICON
672 pcurOld
->CURSORF_flags
&= ~CURSORF_CURRENT
;
674 UserDereferenceObject(pcurOld
);
679 case WM_WINDOWPOSCHANGING
:
681 PWINDOWPOS pWindowPos
= (PWINDOWPOS
)lParam
;
682 if((pWindowPos
->flags
& SWP_SHOWWINDOW
) != 0)
684 HDESK hdesk
= IntGetDesktopObjectHandle(gpdeskInputDesktop
);
685 IntSetThreadDesktop(hdesk
, FALSE
);
690 return TRUE
; /* We are done. Do not do any callbacks to user mode */
694 UserMessageWindowProc(PWND pwnd
, UINT Msg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*lResult
)
701 pwnd
->fnid
|= FNID_MESSAGEWND
;
702 *lResult
= (LRESULT
)TRUE
;
705 pwnd
->fnid
|= FNID_DESTROY
;
709 return TRUE
; /* We are done. Do not do any callbacks to user mode */
712 VOID NTAPI
DesktopThreadMain()
717 gptiDesktopThread
= PsGetCurrentThreadWin32Thread();
719 UserEnterExclusive();
721 /* Register system classes. This thread does not belong to any desktop so the
722 classes will be allocated from the shared heap */
723 UserRegisterSystemClasses();
727 Ret
= co_IntGetPeekMessage(&Msg
, 0, 0, 0, PM_REMOVE
, TRUE
);
730 IntDispatchMessage(&Msg
);
738 UserGetDesktopDC(ULONG DcType
, BOOL EmptyDC
, BOOL ValidatehWnd
)
740 PWND DesktopObject
= 0;
743 if (DcType
== DC_TYPE_DIRECT
)
745 DesktopObject
= UserGetDesktopWindow();
746 DesktopHDC
= (HDC
)UserGetWindowDC(DesktopObject
);
750 PMONITOR pMonitor
= UserGetPrimaryMonitor();
751 DesktopHDC
= IntGdiCreateDisplayDC(pMonitor
->hDev
, DcType
, EmptyDC
);
763 Window
= UserGetDesktopWindow();
764 Rgn
= IntSysCreateRectpRgnIndirect(&Window
->rcWindow
);
766 IntInvalidateWindows( Window
,
778 co_IntShowDesktop(PDESKTOP Desktop
, ULONG Width
, ULONG Height
, BOOL bRedraw
)
780 PWND pwnd
= Desktop
->pDeskInfo
->spwnd
;
781 UINT flags
= SWP_NOACTIVATE
|SWP_NOZORDER
|SWP_SHOWWINDOW
;
785 flags
|= SWP_NOREDRAW
;
787 co_WinPosSetWindowPos(pwnd
, NULL
, 0, 0, Width
, Height
, flags
);
790 co_UserRedrawWindow( pwnd
, NULL
, 0, RDW_UPDATENOW
| RDW_ALLCHILDREN
| RDW_INVALIDATE
);
792 return STATUS_SUCCESS
;
796 IntHideDesktop(PDESKTOP Desktop
)
800 DesktopWnd
= IntGetWindowObject(Desktop
->DesktopWindow
);
803 return ERROR_INVALID_WINDOW_HANDLE
;
805 DesktopWnd
->style
&= ~WS_VISIBLE
;
807 return STATUS_SUCCESS
;
812 UserBuildShellHookHwndList(PDESKTOP Desktop
)
815 PLIST_ENTRY ListEntry
;
816 PSHELL_HOOK_WINDOW Current
;
819 /* FIXME: If we save nb elements in desktop, we dont have to loop to find nb entries */
820 ListEntry
= Desktop
->ShellHookWindows
.Flink
;
821 while (ListEntry
!= &Desktop
->ShellHookWindows
)
823 ListEntry
= ListEntry
->Flink
;
827 if (!entries
) return NULL
;
829 list
= ExAllocatePoolWithTag(PagedPool
, sizeof(HWND
) * (entries
+ 1), USERTAG_WINDOWLIST
); /* alloc one extra for nullterm */
834 ListEntry
= Desktop
->ShellHookWindows
.Flink
;
835 while (ListEntry
!= &Desktop
->ShellHookWindows
)
837 Current
= CONTAINING_RECORD(ListEntry
, SHELL_HOOK_WINDOW
, ListEntry
);
838 ListEntry
= ListEntry
->Flink
;
839 *cursor
++ = Current
->hWnd
;
842 *cursor
= NULL
; /* Nullterm list */
849 * Send the Message to the windows registered for ShellHook
850 * notifications. The lParam contents depend on the Message. See
851 * MSDN for more details (RegisterShellHookWindow)
853 VOID
co_IntShellHookNotify(WPARAM Message
, WPARAM wParam
, LPARAM lParam
)
855 PDESKTOP Desktop
= IntGetActiveDesktop();
858 if (!gpsi
->uiShellMsg
)
860 gpsi
->uiShellMsg
= IntAddAtom(L
"SHELLHOOK");
862 TRACE("MsgType = %x\n", gpsi
->uiShellMsg
);
863 if (!gpsi
->uiShellMsg
)
864 ERR("LastError: %x\n", EngGetLastError());
869 TRACE("IntShellHookNotify: No desktop!\n");
873 // Allow other devices have a shot at foreground.
874 if (Message
== HSHELL_APPCOMMAND
) ptiLastInput
= NULL
;
876 // FIXME: System Tray Support.
878 HwndList
= UserBuildShellHookHwndList(Desktop
);
881 HWND
* cursor
= HwndList
;
883 for (; *cursor
; cursor
++)
885 TRACE("Sending notify\n");
886 UserPostMessage(*cursor
,
889 (Message
== HSHELL_LANGUAGE
? lParam
: (LPARAM
)wParam
) );
890 /* co_IntPostOrSendMessage(*cursor,
893 (Message == HSHELL_LANGUAGE ? lParam : (LPARAM)wParam) );*/
896 ExFreePoolWithTag(HwndList
, USERTAG_WINDOWLIST
);
899 if (ISITHOOKED(WH_SHELL
))
901 co_HOOK_CallHooks(WH_SHELL
, Message
, wParam
, lParam
);
906 * Add the window to the ShellHookWindows list. The windows
907 * on that list get notifications that are important to shell
910 * TODO: Validate the window? I'm not sure if sending these messages to
911 * an unsuspecting application that is not your own is a nice thing to do.
913 BOOL
IntRegisterShellHookWindow(HWND hWnd
)
915 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
916 PDESKTOP Desktop
= pti
->rpdesk
;
917 PSHELL_HOOK_WINDOW Entry
;
919 TRACE("IntRegisterShellHookWindow\n");
921 /* First deregister the window, so we can be sure it's never twice in the
924 IntDeRegisterShellHookWindow(hWnd
);
926 Entry
= ExAllocatePoolWithTag(PagedPool
,
927 sizeof(SHELL_HOOK_WINDOW
),
935 InsertTailList(&Desktop
->ShellHookWindows
, &Entry
->ListEntry
);
941 * Remove the window from the ShellHookWindows list. The windows
942 * on that list get notifications that are important to shell
945 BOOL
IntDeRegisterShellHookWindow(HWND hWnd
)
947 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
948 PDESKTOP Desktop
= pti
->rpdesk
;
949 PLIST_ENTRY ListEntry
;
950 PSHELL_HOOK_WINDOW Current
;
952 ListEntry
= Desktop
->ShellHookWindows
.Flink
;
953 while (ListEntry
!= &Desktop
->ShellHookWindows
)
955 Current
= CONTAINING_RECORD(ListEntry
, SHELL_HOOK_WINDOW
, ListEntry
);
956 ListEntry
= ListEntry
->Flink
;
957 if (Current
->hWnd
== hWnd
)
959 RemoveEntryList(&Current
->ListEntry
);
960 ExFreePoolWithTag(Current
, TAG_WINSTA
);
969 IntFreeDesktopHeap(IN OUT PDESKTOP Desktop
)
971 /* FIXME: Disable until unmapping works in mm */
973 if (Desktop
->pheapDesktop
!= NULL
)
975 MmUnmapViewInSessionSpace(Desktop
->pheapDesktop
);
976 Desktop
->pheapDesktop
= NULL
;
979 if (Desktop
->hsectionDesktop
!= NULL
)
981 ObDereferenceObject(Desktop
->hsectionDesktop
);
982 Desktop
->hsectionDesktop
= NULL
;
988 IntPaintDesktop(HDC hDC
)
991 HBRUSH DesktopBrush
, PreviousBrush
;
993 BOOL doPatBlt
= TRUE
;
995 static WCHAR s_wszSafeMode
[] = L
"Safe Mode";
1001 GdiGetClipBox(hDC
, &Rect
);
1003 hWndDesktop
= IntGetDesktopWindow(); // rpdesk->DesktopWindow;
1005 WndDesktop
= UserGetWindowObject(hWndDesktop
); // rpdesk->pDeskInfo->spwnd;
1011 if (!UserGetSystemMetrics(SM_CLEANBOOT
))
1013 DesktopBrush
= (HBRUSH
)WndDesktop
->pcls
->hbrBackground
;
1016 * Paint desktop background
1018 if (gspv
.hbmWallpaper
!= NULL
)
1024 sz
.cx
= WndDesktop
->rcWindow
.right
- WndDesktop
->rcWindow
.left
;
1025 sz
.cy
= WndDesktop
->rcWindow
.bottom
- WndDesktop
->rcWindow
.top
;
1027 if (gspv
.WallpaperMode
== wmStretch
||
1028 gspv
.WallpaperMode
== wmTile
)
1035 /* Find the upper left corner, can be negative if the bitmap is bigger then the screen */
1036 x
= (sz
.cx
/ 2) - (gspv
.cxWallpaper
/ 2);
1037 y
= (sz
.cy
/ 2) - (gspv
.cyWallpaper
/ 2);
1040 hWallpaperDC
= NtGdiCreateCompatibleDC(hDC
);
1041 if(hWallpaperDC
!= NULL
)
1045 /* Fill in the area that the bitmap is not going to cover */
1048 /* FIXME: Clip out the bitmap
1049 can be replaced with "NtGdiPatBlt(hDC, x, y, WinSta->cxWallpaper, WinSta->cyWallpaper, PATCOPY | DSTINVERT);"
1050 once we support DSTINVERT */
1051 PreviousBrush
= NtGdiSelectBrush(hDC
, DesktopBrush
);
1052 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
1053 NtGdiSelectBrush(hDC
, PreviousBrush
);
1056 /* Do not fill the background after it is painted no matter the size of the picture */
1059 hOldBitmap
= NtGdiSelectBitmap(hWallpaperDC
, gspv
.hbmWallpaper
);
1061 if (gspv
.WallpaperMode
== wmStretch
)
1063 if(Rect
.right
&& Rect
.bottom
)
1064 NtGdiStretchBlt(hDC
,
1078 else if (gspv
.WallpaperMode
== wmTile
)
1080 /* Paint the bitmap across the screen then down */
1081 for(y
= 0; y
< Rect
.bottom
; y
+= gspv
.cyWallpaper
)
1083 for(x
= 0; x
< Rect
.right
; x
+= gspv
.cxWallpaper
)
1113 NtGdiSelectBitmap(hWallpaperDC
, hOldBitmap
);
1114 NtGdiDeleteObjectApp(hWallpaperDC
);
1120 /* Black desktop background in Safe Mode */
1121 DesktopBrush
= StockObjects
[BLACK_BRUSH
];
1123 /* Back ground is set to none, clear the screen */
1126 PreviousBrush
= NtGdiSelectBrush(hDC
, DesktopBrush
);
1127 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
1128 NtGdiSelectBrush(hDC
, PreviousBrush
);
1132 * Display system version on the desktop background
1135 if (g_PaintDesktopVersion
|| UserGetSystemMetrics(SM_CLEANBOOT
))
1137 static WCHAR s_wszVersion
[256] = {0};
1142 len
= wcslen(s_wszVersion
);
1146 len
= GetSystemVersionString(s_wszVersion
);
1151 if (!UserSystemParametersInfo(SPI_GETWORKAREA
, 0, &rect
, 0))
1153 rect
.right
= UserGetSystemMetrics(SM_CXSCREEN
);
1154 rect
.bottom
= UserGetSystemMetrics(SM_CYSCREEN
);
1157 color_old
= IntGdiSetTextColor(hDC
, RGB(255,255,255));
1158 align_old
= IntGdiSetTextAlign(hDC
, TA_RIGHT
);
1159 mode_old
= IntGdiSetBkMode(hDC
, TRANSPARENT
);
1161 if(!UserGetSystemMetrics(SM_CLEANBOOT
))
1163 GreExtTextOutW(hDC
, rect
.right
- 16, rect
.bottom
- 48, 0, NULL
, s_wszVersion
, len
, NULL
, 0);
1169 /* Version information text in top center */
1170 IntGdiSetTextAlign(hDC
, TA_CENTER
| TA_TOP
);
1171 GreExtTextOutW(hDC
, (rect
.right
+ rect
.left
)/2, rect
.top
+ 3, 0, NULL
, s_wszVersion
, len
, NULL
, 0);
1173 /* Safe Mode text in corners */
1174 len
= wcslen(s_wszSafeMode
);
1175 IntGdiSetTextAlign(hDC
, TA_LEFT
| TA_TOP
);
1176 GreExtTextOutW(hDC
, rect
.left
, rect
.top
+ 3, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1177 IntGdiSetTextAlign(hDC
, TA_RIGHT
| TA_TOP
);
1178 GreExtTextOutW(hDC
, rect
.right
, rect
.top
+ 3, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1179 IntGdiSetTextAlign(hDC
, TA_LEFT
| TA_BASELINE
);
1180 GreExtTextOutW(hDC
, rect
.left
, rect
.bottom
- 5, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1181 IntGdiSetTextAlign(hDC
, TA_RIGHT
| TA_BASELINE
);
1182 GreExtTextOutW(hDC
, rect
.right
, rect
.bottom
- 5, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1185 IntGdiSetBkMode(hDC
, mode_old
);
1186 IntGdiSetTextAlign(hDC
, align_old
);
1187 IntGdiSetTextColor(hDC
, color_old
);
1194 UserInitializeDesktop(PDESKTOP pdesk
, PUNICODE_STRING DesktopName
, PWINSTATION_OBJECT pwinsta
)
1196 PVOID DesktopHeapSystemBase
= NULL
;
1197 ULONG_PTR HeapSize
= 400 * 1024;
1198 SIZE_T DesktopInfoSize
;
1201 TRACE("UserInitializeDesktop desktop 0x%p with name %wZ\n", pdesk
, DesktopName
);
1203 RtlZeroMemory(pdesk
, sizeof(DESKTOP
));
1205 /* Link the desktop with the parent window station */
1206 pdesk
->rpwinstaParent
= pwinsta
;
1207 InsertTailList(&pwinsta
->DesktopListHead
, &pdesk
->ListEntry
);
1209 /* Create the desktop heap */
1210 pdesk
->hsectionDesktop
= NULL
;
1211 pdesk
->pheapDesktop
= UserCreateHeap(&pdesk
->hsectionDesktop
,
1212 &DesktopHeapSystemBase
,
1214 if (pdesk
->pheapDesktop
== NULL
)
1216 ERR("Failed to create desktop heap!\n");
1217 return STATUS_NO_MEMORY
;
1220 /* Create DESKTOPINFO */
1221 DesktopInfoSize
= sizeof(DESKTOPINFO
) + DesktopName
->Length
+ sizeof(WCHAR
);
1222 pdesk
->pDeskInfo
= RtlAllocateHeap(pdesk
->pheapDesktop
,
1223 HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
,
1225 if (pdesk
->pDeskInfo
== NULL
)
1227 ERR("Failed to create the DESKTOP structure!\n");
1228 return STATUS_NO_MEMORY
;
1231 /* Initialize the DESKTOPINFO */
1232 pdesk
->pDeskInfo
->pvDesktopBase
= DesktopHeapSystemBase
;
1233 pdesk
->pDeskInfo
->pvDesktopLimit
= (PVOID
)((ULONG_PTR
)DesktopHeapSystemBase
+ HeapSize
);
1234 RtlCopyMemory(pdesk
->pDeskInfo
->szDesktopName
,
1235 DesktopName
->Buffer
,
1236 DesktopName
->Length
+ sizeof(WCHAR
));
1237 for (i
= 0; i
< NB_HOOKS
; i
++)
1239 InitializeListHead(&pdesk
->pDeskInfo
->aphkStart
[i
]);
1242 InitializeListHead(&pdesk
->ShellHookWindows
);
1243 InitializeListHead(&pdesk
->PtiList
);
1245 return STATUS_SUCCESS
;
1248 /* SYSCALLS *******************************************************************/
1251 * NtUserCreateDesktop
1253 * Creates a new desktop.
1257 * Object Attributes.
1260 * Name of the device.
1266 * Interaction flags.
1269 * Requested type of access.
1273 * If the function succeeds, the return value is a handle to the newly
1274 * created desktop. If the specified desktop already exists, the function
1275 * succeeds and returns a handle to the existing desktop. When you are
1276 * finished using the handle, call the CloseDesktop function to close it.
1277 * If the function fails, the return value is NULL.
1284 NtUserCreateDesktop(
1285 POBJECT_ATTRIBUTES ObjectAttributes
,
1286 PUNICODE_STRING lpszDesktopDevice
,
1289 ACCESS_MASK dwDesiredAccess
)
1291 PDESKTOP pdesk
= NULL
;
1292 NTSTATUS Status
= STATUS_SUCCESS
;
1295 UNICODE_STRING ClassName
;
1296 LARGE_STRING WindowName
;
1297 BOOL NoHooks
= FALSE
;
1300 PTHREADINFO ptiCurrent
;
1303 DECLARE_RETURN(HDESK
);
1305 TRACE("Enter NtUserCreateDesktop\n");
1306 UserEnterExclusive();
1308 ptiCurrent
= PsGetCurrentThreadWin32Thread();
1310 ASSERT(gptiDesktopThread
);
1312 /* Turn off hooks when calling any CreateWindowEx from inside win32k. */
1313 NoHooks
= (ptiCurrent
->TIF_flags
& TIF_DISABLEHOOKS
);
1314 ptiCurrent
->TIF_flags
|= TIF_DISABLEHOOKS
;
1315 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
1318 * Try to open already existing desktop
1320 Status
= ObOpenObjectByName(
1322 ExDesktopObjectType
,
1328 if (!NT_SUCCESS(Status
))
1330 ERR("ObOpenObjectByName failed to open/create desktop\n");
1331 SetLastNtError(Status
);
1335 /* In case the object was not created (eg if it existed), return now */
1336 if (Context
== FALSE
)
1338 TRACE("NtUserCreateDesktop opened desktop %wZ\n", ObjectAttributes
->ObjectName
);
1342 /* Reference the desktop */
1343 Status
= ObReferenceObjectByHandle(hdesk
,
1345 ExDesktopObjectType
,
1349 if (!NT_SUCCESS(Status
))
1351 ERR("Failed to reference desktop object\n");
1352 SetLastNtError(Status
);
1356 /* Get the desktop window class. The thread desktop does not belong to any desktop
1357 * so the classes created there (including the desktop class) are allocated in the shared heap
1358 * It would cause problems if we used a class that belongs to the caller
1360 ClassName
.Buffer
= WC_DESKTOP
;
1361 ClassName
.Length
= 0;
1362 pcls
= IntGetAndReferenceClass(&ClassName
, 0, TRUE
);
1369 RtlZeroMemory(&WindowName
, sizeof(WindowName
));
1370 RtlZeroMemory(&Cs
, sizeof(Cs
));
1371 Cs
.x
= UserGetSystemMetrics(SM_XVIRTUALSCREEN
),
1372 Cs
.y
= UserGetSystemMetrics(SM_YVIRTUALSCREEN
),
1373 Cs
.cx
= UserGetSystemMetrics(SM_CXVIRTUALSCREEN
),
1374 Cs
.cy
= UserGetSystemMetrics(SM_CYVIRTUALSCREEN
),
1375 Cs
.style
= WS_POPUP
|WS_CLIPCHILDREN
;
1376 Cs
.hInstance
= hModClient
; // hModuleWin; // Server side winproc!
1377 Cs
.lpszName
= (LPCWSTR
) &WindowName
;
1378 Cs
.lpszClass
= (LPCWSTR
) &ClassName
;
1380 /* Use IntCreateWindow instead of co_UserCreateWindowEx cause the later expects a thread with a desktop */
1381 pWnd
= IntCreateWindow(&Cs
, &WindowName
, pcls
, NULL
, NULL
, NULL
, pdesk
);
1384 ERR("Failed to create desktop window for the new desktop\n");
1388 pdesk
->dwSessionId
= PsGetCurrentProcessSessionId();
1389 pdesk
->DesktopWindow
= pWnd
->head
.h
;
1390 pdesk
->pDeskInfo
->spwnd
= pWnd
;
1391 pWnd
->fnid
= FNID_DESKTOP
;
1393 ClassName
.Buffer
= MAKEINTATOM(gpsi
->atomSysClass
[ICLS_HWNDMESSAGE
]);
1394 ClassName
.Length
= 0;
1395 pcls
= IntGetAndReferenceClass(&ClassName
, 0, TRUE
);
1402 RtlZeroMemory(&WindowName
, sizeof(WindowName
));
1403 RtlZeroMemory(&Cs
, sizeof(Cs
));
1404 Cs
.cx
= Cs
.cy
= 100;
1405 Cs
.style
= WS_POPUP
|WS_CLIPCHILDREN
;
1406 Cs
.hInstance
= hModClient
; // hModuleWin; // Server side winproc!
1407 Cs
.lpszName
= (LPCWSTR
) &WindowName
;
1408 Cs
.lpszClass
= (LPCWSTR
) &ClassName
;
1409 pWnd
= IntCreateWindow(&Cs
, &WindowName
, pcls
, NULL
, NULL
, NULL
, pdesk
);
1412 ERR("Failed to create message window for the new desktop\n");
1416 pdesk
->spwndMessage
= pWnd
;
1417 pWnd
->fnid
= FNID_MESSAGEWND
;
1420 if !(WinStaObject->Flags & WSF_NOIO) is (not set) for desktop input output mode (see wiki)
1421 Create Tooltip. Saved in DesktopObject->spwndTooltip.
1422 Tooltip dwExStyle: WS_EX_TOOLWINDOW|WS_EX_TOPMOST
1423 hWndParent are spwndMessage. Use hModuleWin for server side winproc!
1424 The rest is same as message window.
1425 http://msdn.microsoft.com/en-us/library/bb760250(VS.85).aspx
1432 ObDereferenceObject(pdesk
);
1434 if (_ret_
== NULL
&& hdesk
!= NULL
)
1436 ObCloseHandle(hdesk
, UserMode
);
1440 ptiCurrent
->TIF_flags
&= ~TIF_DISABLEHOOKS
;
1441 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
1443 TRACE("Leave NtUserCreateDesktop, ret=%p\n",_ret_
);
1451 * Opens an existing desktop.
1455 * Name of the existing desktop.
1458 * Interaction flags.
1461 * Requested type of access.
1464 * Handle to the desktop or zero on failure.
1472 POBJECT_ATTRIBUTES ObjectAttributes
,
1474 ACCESS_MASK dwDesiredAccess
)
1479 Status
= ObOpenObjectByName(
1481 ExDesktopObjectType
,
1488 if (!NT_SUCCESS(Status
))
1490 ERR("Failed to open desktop\n");
1491 SetLastNtError(Status
);
1495 TRACE("Opened desktop %S with handle 0x%p\n", ObjectAttributes
->ObjectName
->Buffer
, Desktop
);
1501 * NtUserOpenInputDesktop
1503 * Opens the input (interactive) desktop.
1507 * Interaction flags.
1510 * Inheritance option.
1513 * Requested type of access.
1516 * Handle to the input desktop or zero on failure.
1523 NtUserOpenInputDesktop(
1526 ACCESS_MASK dwDesiredAccess
)
1530 ULONG HandleAttributes
= 0;
1532 UserEnterExclusive();
1533 TRACE("Enter NtUserOpenInputDesktop gpdeskInputDesktop 0x%p\n",gpdeskInputDesktop
);
1535 if(fInherit
) HandleAttributes
= OBJ_INHERIT
;
1537 /* Create a new handle to the object */
1538 Status
= ObOpenObjectByPointer(
1543 ExDesktopObjectType
,
1547 if (!NT_SUCCESS(Status
))
1549 ERR("Failed to open input desktop object\n");
1550 SetLastNtError(Status
);
1553 TRACE("NtUserOpenInputDesktop returning 0x%p\n",hdesk
);
1559 * NtUserCloseDesktop
1561 * Closes a desktop handle.
1565 * Handle to the desktop.
1571 * The desktop handle can be created with NtUserCreateDesktop or
1572 * NtUserOpenDesktop. This function will fail if any thread in the calling
1573 * process is using the specified desktop handle or if the handle refers
1574 * to the initial desktop of the calling process.
1581 NtUserCloseDesktop(HDESK hDesktop
)
1585 DECLARE_RETURN(BOOL
);
1587 TRACE("NtUserCloseDesktop called (0x%p)\n", hDesktop
);
1588 UserEnterExclusive();
1590 if( hDesktop
== gptiCurrent
->hdesk
|| hDesktop
== gptiCurrent
->ppi
->hdeskStartup
)
1592 ERR("Attempted to close thread desktop\n");
1593 EngSetLastError(ERROR_BUSY
);
1597 Status
= IntValidateDesktopHandle( hDesktop
, UserMode
, 0, &pdesk
);
1598 if (!NT_SUCCESS(Status
))
1600 ERR("Validation of desktop handle (0x%p) failed\n", hDesktop
);
1604 ObDereferenceObject(pdesk
);
1606 Status
= ZwClose(hDesktop
);
1607 if (!NT_SUCCESS(Status
))
1609 ERR("Failed to close desktop handle 0x%p\n", hDesktop
);
1610 SetLastNtError(Status
);
1617 TRACE("Leave NtUserCloseDesktop, ret=%i\n",_ret_
);
1623 * NtUserPaintDesktop
1625 * The NtUserPaintDesktop function fills the clipping region in the
1626 * specified device context with the desktop pattern or wallpaper. The
1627 * function is provided primarily for shell desktops.
1631 * Handle to the device context.
1638 NtUserPaintDesktop(HDC hDC
)
1641 UserEnterExclusive();
1642 TRACE("Enter NtUserPaintDesktop\n");
1643 Ret
= IntPaintDesktop(hDC
);
1644 TRACE("Leave NtUserPaintDesktop, ret=%i\n",Ret
);
1650 * NtUserSwitchDesktop
1652 * Sets the current input (interactive) desktop.
1656 * Handle to desktop.
1666 NtUserSwitchDesktop(HDESK hdesk
)
1670 BOOL bRedrawDesktop
;
1671 DECLARE_RETURN(BOOL
);
1673 UserEnterExclusive();
1674 TRACE("Enter NtUserSwitchDesktop(0x%p)\n", hdesk
);
1676 Status
= IntValidateDesktopHandle( hdesk
, UserMode
, 0, &pdesk
);
1677 if (!NT_SUCCESS(Status
))
1679 ERR("Validation of desktop handle (0x%p) failed\n", hdesk
);
1683 if (PsGetCurrentProcessSessionId() != pdesk
->rpwinstaParent
->dwSessionId
)
1685 ERR("NtUserSwitchDesktop called for a desktop of a different session\n");
1689 if(pdesk
== gpdeskInputDesktop
)
1691 WARN("NtUserSwitchDesktop called for active desktop\n");
1696 * Don't allow applications switch the desktop if it's locked, unless the caller
1697 * is the logon application itself
1699 if((pdesk
->rpwinstaParent
->Flags
& WSS_LOCKED
) &&
1700 LogonProcess
!= PsGetCurrentProcessWin32Process())
1702 ObDereferenceObject(pdesk
);
1703 ERR("Switching desktop 0x%p denied because the window station is locked!\n", hdesk
);
1707 if(pdesk
->rpwinstaParent
!= InputWindowStation
)
1709 ObDereferenceObject(pdesk
);
1710 ERR("Switching desktop 0x%p denied because desktop doesn't belong to the interactive winsta!\n", hdesk
);
1714 /* FIXME: Fail if the process is associated with a secured
1715 desktop such as Winlogon or Screen-Saver */
1716 /* FIXME: Connect to input device */
1718 TRACE("Switching from desktop 0x%p to 0x%p\n", gpdeskInputDesktop
, pdesk
);
1720 bRedrawDesktop
= FALSE
;
1722 /* The first time SwitchDesktop is called, gpdeskInputDesktop is NULL */
1723 if(gpdeskInputDesktop
!= NULL
)
1725 if((gpdeskInputDesktop
->pDeskInfo
->spwnd
->style
& WS_VISIBLE
) == WS_VISIBLE
)
1726 bRedrawDesktop
= TRUE
;
1728 /* Hide the previous desktop window */
1729 IntHideDesktop(gpdeskInputDesktop
);
1732 /* Set the active desktop in the desktop's window station. */
1733 InputWindowStation
->ActiveDesktop
= pdesk
;
1735 /* Set the global state. */
1736 gpdeskInputDesktop
= pdesk
;
1738 /* Show the new desktop window */
1739 co_IntShowDesktop(pdesk
, UserGetSystemMetrics(SM_CXSCREEN
), UserGetSystemMetrics(SM_CYSCREEN
), bRedrawDesktop
);
1741 TRACE("SwitchDesktop gpdeskInputDesktop 0x%p\n",gpdeskInputDesktop
);
1742 ObDereferenceObject(pdesk
);
1747 TRACE("Leave NtUserSwitchDesktop, ret=%i\n",_ret_
);
1753 * NtUserGetThreadDesktop
1760 NtUserGetThreadDesktop(DWORD dwThreadId
, DWORD Unknown1
)
1764 PDESKTOP DesktopObject
;
1765 HDESK Ret
, hThreadDesktop
;
1766 OBJECT_HANDLE_INFORMATION HandleInformation
;
1767 DECLARE_RETURN(HDESK
);
1769 UserEnterExclusive();
1770 TRACE("Enter NtUserGetThreadDesktop\n");
1774 EngSetLastError(ERROR_INVALID_PARAMETER
);
1778 Status
= PsLookupThreadByThreadId((HANDLE
)(DWORD_PTR
)dwThreadId
, &Thread
);
1779 if(!NT_SUCCESS(Status
))
1781 EngSetLastError(ERROR_INVALID_PARAMETER
);
1785 if(Thread
->ThreadsProcess
== PsGetCurrentProcess())
1787 /* Just return the handle, we queried the desktop handle of a thread running
1788 in the same context */
1789 Ret
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->hdesk
;
1790 ObDereferenceObject(Thread
);
1794 /* Get the desktop handle and the desktop of the thread */
1795 if(!(hThreadDesktop
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->hdesk
) ||
1796 !(DesktopObject
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->rpdesk
))
1798 ObDereferenceObject(Thread
);
1799 ERR("Desktop information of thread 0x%x broken!?\n", dwThreadId
);
1803 /* We could just use DesktopObject instead of looking up the handle, but latter
1804 may be a bit safer (e.g. when the desktop is being destroyed */
1805 /* Switch into the context of the thread we're trying to get the desktop from,
1806 so we can use the handle */
1807 KeAttachProcess(&Thread
->ThreadsProcess
->Pcb
);
1808 Status
= ObReferenceObjectByHandle(hThreadDesktop
,
1810 ExDesktopObjectType
,
1812 (PVOID
*)&DesktopObject
,
1813 &HandleInformation
);
1816 /* The handle couldn't be found, there's nothing to get... */
1817 if(!NT_SUCCESS(Status
))
1819 ObDereferenceObject(Thread
);
1823 /* Lookup our handle table if we can find a handle to the desktop object,
1824 if not, create one */
1825 Ret
= IntGetDesktopObjectHandle(DesktopObject
);
1827 /* All done, we got a valid handle to the desktop */
1828 ObDereferenceObject(DesktopObject
);
1829 ObDereferenceObject(Thread
);
1833 TRACE("Leave NtUserGetThreadDesktop, ret=%p\n",_ret_
);
1839 IntUnmapDesktopView(IN PDESKTOP pdesk
)
1842 PW32HEAP_USER_MAPPING HeapMapping
, *PrevLink
;
1843 NTSTATUS Status
= STATUS_SUCCESS
;
1845 TRACE("IntUnmapDesktopView called for desktop object %p\n", pdesk
);
1847 ppi
= PsGetCurrentProcessWin32Process();
1848 PrevLink
= &ppi
->HeapMappings
.Next
;
1850 /* Unmap if we're the last thread using the desktop */
1851 HeapMapping
= *PrevLink
;
1852 while (HeapMapping
!= NULL
)
1854 if (HeapMapping
->KernelMapping
== (PVOID
)pdesk
->pheapDesktop
)
1856 if (--HeapMapping
->Count
== 0)
1858 *PrevLink
= HeapMapping
->Next
;
1860 TRACE("ppi 0x%p unmapped heap of desktop 0x%p\n", ppi
, pdesk
);
1861 Status
= MmUnmapViewOfSection(PsGetCurrentProcess(),
1862 HeapMapping
->UserMapping
);
1864 ObDereferenceObject(pdesk
);
1866 UserHeapFree(HeapMapping
);
1871 PrevLink
= &HeapMapping
->Next
;
1872 HeapMapping
= HeapMapping
->Next
;
1879 IntMapDesktopView(IN PDESKTOP pdesk
)
1882 PW32HEAP_USER_MAPPING HeapMapping
, *PrevLink
;
1883 PVOID UserBase
= NULL
;
1884 SIZE_T ViewSize
= 0;
1885 LARGE_INTEGER Offset
;
1888 TRACE("IntMapDesktopView called for desktop object 0x%p\n", pdesk
);
1890 ppi
= PsGetCurrentProcessWin32Process();
1891 PrevLink
= &ppi
->HeapMappings
.Next
;
1893 /* Find out if another thread already mapped the desktop heap */
1894 HeapMapping
= *PrevLink
;
1895 while (HeapMapping
!= NULL
)
1897 if (HeapMapping
->KernelMapping
== (PVOID
)pdesk
->pheapDesktop
)
1899 HeapMapping
->Count
++;
1900 return STATUS_SUCCESS
;
1903 PrevLink
= &HeapMapping
->Next
;
1904 HeapMapping
= HeapMapping
->Next
;
1907 /* We're the first, map the heap */
1908 Offset
.QuadPart
= 0;
1909 Status
= MmMapViewOfSection(pdesk
->hsectionDesktop
,
1910 PsGetCurrentProcess(),
1918 PAGE_EXECUTE_READ
); /* Would prefer PAGE_READONLY, but thanks to RTL heaps... */
1919 if (!NT_SUCCESS(Status
))
1921 ERR("Failed to map desktop\n");
1925 TRACE("ppi 0x%p mapped heap of desktop 0x%p\n", ppi
, pdesk
);
1927 /* Add the mapping */
1928 HeapMapping
= UserHeapAlloc(sizeof(W32HEAP_USER_MAPPING
));
1929 if (HeapMapping
== NULL
)
1931 MmUnmapViewOfSection(PsGetCurrentProcess(), UserBase
);
1932 ERR("UserHeapAlloc() failed!\n");
1933 return STATUS_NO_MEMORY
;
1936 HeapMapping
->Next
= NULL
;
1937 HeapMapping
->KernelMapping
= (PVOID
)pdesk
->pheapDesktop
;
1938 HeapMapping
->UserMapping
= UserBase
;
1939 HeapMapping
->Limit
= ViewSize
;
1940 HeapMapping
->Count
= 1;
1941 *PrevLink
= HeapMapping
;
1943 ObReferenceObject(pdesk
);
1945 return STATUS_SUCCESS
;
1949 IntSetThreadDesktop(IN HDESK hDesktop
,
1950 IN BOOL FreeOnFailure
)
1952 PDESKTOP pdesk
= NULL
, pdeskOld
;
1956 PCLIENTTHREADINFO pctiOld
, pctiNew
= NULL
;
1959 ASSERT(NtCurrentTeb());
1961 TRACE("IntSetThreadDesktop hDesktop:0x%p, FOF:%i\n",hDesktop
, FreeOnFailure
);
1963 pti
= PsGetCurrentThreadWin32Thread();
1964 pci
= pti
->pClientInfo
;
1966 /* If the caller gave us a desktop, ensure it is valid */
1967 if(hDesktop
!= NULL
)
1969 /* Validate the new desktop. */
1970 Status
= IntValidateDesktopHandle( hDesktop
, UserMode
, 0, &pdesk
);
1971 if (!NT_SUCCESS(Status
))
1973 ERR("Validation of desktop handle (0x%p) failed\n", hDesktop
);
1977 if (pti
->rpdesk
== pdesk
)
1980 ObDereferenceObject(pdesk
);
1985 /* Make sure that we don't own any window in the current desktop */
1986 if (!IsListEmpty(&pti
->WindowListHead
))
1989 ObDereferenceObject(pdesk
);
1990 ERR("Attempted to change thread desktop although the thread has windows!\n");
1991 EngSetLastError(ERROR_BUSY
);
1995 /* Desktop is being re-set so clear out foreground. */
1996 if (pti
->rpdesk
!= pdesk
&& pti
->MessageQueue
== gpqForeground
)
1998 // Like above, there shouldn't be any windows, hooks or anything active on this threads desktop!
1999 IntSetFocusMessageQueue(NULL
);
2002 /* Before doing the switch, map the new desktop heap and allocate the new pcti */
2005 Status
= IntMapDesktopView(pdesk
);
2006 if (!NT_SUCCESS(Status
))
2008 ERR("Failed to map desktop heap!\n");
2009 ObDereferenceObject(pdesk
);
2010 SetLastNtError(Status
);
2014 pctiNew
= DesktopHeapAlloc( pdesk
, sizeof(CLIENTTHREADINFO
));
2017 ERR("Failed to allocate new pcti\n");
2018 IntUnmapDesktopView(pdesk
);
2019 ObDereferenceObject(pdesk
);
2020 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2025 /* free all classes or move them to the shared heap */
2026 if(pti
->rpdesk
!= NULL
)
2028 if(!IntCheckProcessDesktopClasses(pti
->rpdesk
, FreeOnFailure
))
2030 ERR("Failed to move process classes to shared heap!\n");
2033 DesktopHeapFree(pdesk
, pctiNew
);
2034 IntUnmapDesktopView(pdesk
);
2035 ObDereferenceObject(pdesk
);
2041 pdeskOld
= pti
->rpdesk
;
2042 hdeskOld
= pti
->hdesk
;
2043 if (pti
->pcti
!= &pti
->cti
)
2044 pctiOld
= pti
->pcti
;
2051 pti
->rpdesk
= pdesk
;
2052 pti
->hdesk
= hDesktop
;
2053 pti
->pDeskInfo
= pti
->rpdesk
->pDeskInfo
;
2054 pti
->pcti
= pctiNew
;
2056 pci
->ulClientDelta
= DesktopHeapGetUserDelta();
2057 pci
->pDeskInfo
= (PVOID
)((ULONG_PTR
)pti
->pDeskInfo
- pci
->ulClientDelta
);
2058 pci
->pClientThreadInfo
= (PVOID
)((ULONG_PTR
)pti
->pcti
- pci
->ulClientDelta
);
2060 /* initialize the new pcti */
2063 RtlCopyMemory(pctiNew
, pctiOld
, sizeof(CLIENTTHREADINFO
));
2067 RtlZeroMemory(pctiNew
, sizeof(CLIENTTHREADINFO
));
2068 pci
->fsHooks
= pti
->fsHooks
;
2069 pci
->dwTIFlags
= pti
->TIF_flags
;
2076 pti
->pDeskInfo
= NULL
;
2077 pti
->pcti
= &pti
->cti
; // Always point inside so there will be no crash when posting or sending msg's!
2078 pci
->ulClientDelta
= 0;
2079 pci
->pDeskInfo
= NULL
;
2080 pci
->pClientThreadInfo
= NULL
;
2083 /* clean up the old desktop */
2084 if(pdeskOld
!= NULL
)
2086 RemoveEntryList(&pti
->PtiLink
);
2087 if (pctiOld
) DesktopHeapFree(pdeskOld
, pctiOld
);
2088 IntUnmapDesktopView(pdeskOld
);
2089 ObDereferenceObject(pdeskOld
);
2095 InsertTailList(&pdesk
->PtiList
, &pti
->PtiLink
);
2098 TRACE("IntSetThreadDesktop: pti 0x%p ppi 0x%p switched from object 0x%p to 0x%p\n", pti
, pti
->ppi
, pdeskOld
, pdesk
);
2104 * NtUserSetThreadDesktop
2111 NtUserSetThreadDesktop(HDESK hDesktop
)
2115 UserEnterExclusive();
2117 // FIXME: IntSetThreadDesktop validates the desktop handle, it should happen
2118 // here too and set the NT error level. Q. Is it necessary to have the validation
2119 // in IntSetThreadDesktop? Is it needed there too?
2120 if (hDesktop
|| (!hDesktop
&& PsGetCurrentProcess() == gpepCSRSS
))
2121 ret
= IntSetThreadDesktop(hDesktop
, FALSE
);