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
;
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 PUNICODE_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
);
69 DesktopName
= GET_DESKTOP_NAME(Desktop
);
72 /* Compare the name */
73 if (RtlEqualUnicodeString(RemainingName
,
75 (Attributes
& OBJ_CASE_INSENSITIVE
)))
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
);
105 /* Go to the next desktop */
106 NextEntry
= NextEntry
->Flink
;
109 /* If we got here but this isn't a create, then fail */
110 if (!Context
) return STATUS_OBJECT_NAME_NOT_FOUND
;
112 /* Create the desktop object */
113 InitializeObjectAttributes(&ObjectAttributes
, RemainingName
, 0, NULL
, NULL
);
114 Status
= ObCreateObject(KernelMode
,
123 if (!NT_SUCCESS(Status
)) return Status
;
125 /* Initialize the desktop */
126 Status
= UserInitializeDesktop(Desktop
, RemainingName
, WinStaObject
);
127 if (!NT_SUCCESS(Status
))
129 ObDereferenceObject(Desktop
);
133 /* Set the desktop object and return success */
136 return STATUS_SUCCESS
;
140 IntDesktopObjectDelete(PWIN32_DELETEMETHOD_PARAMETERS Parameters
)
142 PDESKTOP pdesk
= (PDESKTOP
)Parameters
->Object
;
144 TRACE("Deleting desktop object 0x%p\n", pdesk
);
146 ASSERT(pdesk
->pDeskInfo
->spwnd
->spwndChild
== NULL
);
148 if (pdesk
->pDeskInfo
->spwnd
)
149 co_UserDestroyWindow(pdesk
->pDeskInfo
->spwnd
);
151 if (pdesk
->spwndMessage
)
152 co_UserDestroyWindow(pdesk
->spwndMessage
);
154 /* Remove the desktop from the window station's list of associcated desktops */
155 RemoveEntryList(&pdesk
->ListEntry
);
158 IntFreeDesktopHeap(pdesk
);
162 IntDesktopOkToClose(PWIN32_OKAYTOCLOSEMETHOD_PARAMETERS Parameters
)
164 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
168 /* This happens when we leak desktop handles */
169 return STATUS_SUCCESS
;
172 /* Do not allow the current desktop or the initial desktop to be closed */
173 if( Parameters
->Handle
== pti
->ppi
->hdeskStartup
||
174 Parameters
->Handle
== pti
->hdesk
)
176 return STATUS_ACCESS_DENIED
;
179 return STATUS_SUCCESS
;
182 NTSTATUS NTAPI
IntDesktopObjectOpen(PWIN32_OPENMETHOD_PARAMETERS Parameters
)
184 PPROCESSINFO ppi
= PsGetProcessWin32Process(Parameters
->Process
);
186 return STATUS_SUCCESS
;
188 return IntMapDesktopView((PDESKTOP
)Parameters
->Object
);
191 NTSTATUS NTAPI
IntDesktopObjectClose(PWIN32_CLOSEMETHOD_PARAMETERS Parameters
)
193 PPROCESSINFO ppi
= PsGetProcessWin32Process(Parameters
->Process
);
196 /* This happens when the process leaks desktop handles.
197 * At this point the PPROCESSINFO is already destroyed */
198 return STATUS_SUCCESS
;
201 return IntUnmapDesktopView((PDESKTOP
)Parameters
->Object
);
205 /* PRIVATE FUNCTIONS **********************************************************/
210 InitDesktopImpl(VOID
)
212 GENERIC_MAPPING IntDesktopMapping
= { DESKTOP_READ
,
217 /* Set Desktop Object Attributes */
218 ExDesktopObjectType
->TypeInfo
.DefaultNonPagedPoolCharge
= sizeof(DESKTOP
);
219 ExDesktopObjectType
->TypeInfo
.GenericMapping
= IntDesktopMapping
;
220 ExDesktopObjectType
->TypeInfo
.ValidAccessMask
= DESKTOP_ALL_ACCESS
;
221 return STATUS_SUCCESS
;
224 static int GetSystemVersionString(LPWSTR buffer
)
226 RTL_OSVERSIONINFOEXW versionInfo
;
229 versionInfo
.dwOSVersionInfoSize
= sizeof(RTL_OSVERSIONINFOEXW
);
231 if (!NT_SUCCESS(RtlGetVersion((PRTL_OSVERSIONINFOW
)&versionInfo
)))
234 if (versionInfo
.dwMajorVersion
<= 4)
235 len
= swprintf(buffer
,
236 L
"ReactOS Version %d.%d %s Build %d",
237 versionInfo
.dwMajorVersion
, versionInfo
.dwMinorVersion
,
238 versionInfo
.szCSDVersion
, versionInfo
.dwBuildNumber
&0xFFFF);
240 len
= swprintf(buffer
,
241 L
"ReactOS %s (Build %d)",
242 versionInfo
.szCSDVersion
, versionInfo
.dwBuildNumber
&0xFFFF);
249 IntParseDesktopPath(PEPROCESS Process
,
250 PUNICODE_STRING DesktopPath
,
254 OBJECT_ATTRIBUTES ObjectAttributes
;
255 UNICODE_STRING ObjectName
;
257 WCHAR wstrWinstaFullName
[MAX_PATH
], *pwstrWinsta
= NULL
, *pwstrDesktop
= NULL
;
266 if(DesktopPath
->Buffer
!= NULL
&& DesktopPath
->Length
> sizeof(WCHAR
))
269 * Parse the desktop path string which can be in the form "WinSta\Desktop"
270 * or just "Desktop". In latter case WinSta0 will be used.
273 pwstrDesktop
= wcschr(DesktopPath
->Buffer
, L
'\\');
274 if(pwstrDesktop
!= NULL
)
278 pwstrWinsta
= DesktopPath
->Buffer
;
282 pwstrDesktop
= DesktopPath
->Buffer
;
286 TRACE("IntParseDesktopPath pwstrWinsta:%S pwstrDesktop:%S\n", pwstrWinsta
, pwstrDesktop
);
290 /* Search the process handle table for (inherited) window station
291 handles, use a more appropriate one than WinSta0 if possible. */
292 if (!ObFindHandleForObject(Process
,
294 ExWindowStationObjectType
,
299 /* We had no luck searching for opened handles, use WinSta0 now */
301 pwstrWinsta
= L
"WinSta0";
305 /* Search the process handle table for (inherited) desktop
306 handles, use a more appropriate one than Default if possible. */
307 if (!ObFindHandleForObject(Process
,
314 /* We had no luck searching for opened handles, use Desktop now */
316 pwstrDesktop
= L
"Default";
321 swprintf(wstrWinstaFullName
, L
"%wZ\\%ws", &gustrWindowStationsDir
, pwstrWinsta
);
322 RtlInitUnicodeString( &ObjectName
, wstrWinstaFullName
);
324 TRACE("parsed initial winsta: %wZ\n", &ObjectName
);
326 /* Open the window station */
327 InitializeObjectAttributes(&ObjectAttributes
,
329 OBJ_CASE_INSENSITIVE
,
333 Status
= ObOpenObjectByName(&ObjectAttributes
,
334 ExWindowStationObjectType
,
341 if(!NT_SUCCESS(Status
))
343 SetLastNtError(Status
);
344 ERR("Failed to reference window station %wZ PID: --!\n", &ObjectName
);
349 if(*hDesktop
== NULL
)
351 RtlInitUnicodeString(&ObjectName
, pwstrDesktop
);
353 TRACE("parsed initial desktop: %wZ\n", &ObjectName
);
355 /* Open the desktop object */
356 InitializeObjectAttributes(&ObjectAttributes
,
358 OBJ_CASE_INSENSITIVE
,
362 Status
= ObOpenObjectByName(&ObjectAttributes
,
370 if(!NT_SUCCESS(Status
))
375 SetLastNtError(Status
);
376 ERR("Failed to reference desktop %wZ PID: --!\n", &ObjectName
);
380 return STATUS_SUCCESS
;
384 * IntValidateDesktopHandle
386 * Validates the desktop handle.
389 * If the function succeeds, the handle remains referenced. If the
390 * fucntion fails, last error is set.
394 IntValidateDesktopHandle(
396 KPROCESSOR_MODE AccessMode
,
397 ACCESS_MASK DesiredAccess
,
402 Status
= ObReferenceObjectByHandle(
410 TRACE("IntValidateDesktopHandle: handle:0x%p obj:0x%p access:0x%x Status:0x%lx\n",
411 Desktop
, *Object
, DesiredAccess
, Status
);
413 if (!NT_SUCCESS(Status
))
414 SetLastNtError(Status
);
420 IntGetActiveDesktop(VOID
)
422 return gpdeskInputDesktop
;
426 * Returns or creates a handle to the desktop object
429 IntGetDesktopObjectHandle(PDESKTOP DesktopObject
)
434 ASSERT(DesktopObject
);
436 if (!ObFindHandleForObject(PsGetCurrentProcess(),
442 Status
= ObOpenObjectByPointer(DesktopObject
,
449 if(!NT_SUCCESS(Status
))
451 /* Unable to create a handle */
452 ERR("Unable to create a desktop handle\n");
458 ERR("Got handle: %p\n", Ret
);
464 PUSER_MESSAGE_QUEUE FASTCALL
465 IntGetFocusMessageQueue(VOID
)
467 PDESKTOP pdo
= IntGetActiveDesktop();
470 TRACE("No active desktop\n");
473 return (PUSER_MESSAGE_QUEUE
)pdo
->ActiveMessageQueue
;
477 IntSetFocusMessageQueue(PUSER_MESSAGE_QUEUE NewQueue
)
479 PUSER_MESSAGE_QUEUE Old
;
480 PDESKTOP pdo
= IntGetActiveDesktop();
483 TRACE("No active desktop\n");
488 if(NewQueue
->Desktop
!= NULL
)
490 TRACE("Message Queue already attached to another desktop!\n");
493 IntReferenceMessageQueue(NewQueue
);
494 (void)InterlockedExchangePointer((PVOID
*)&NewQueue
->Desktop
, pdo
);
496 Old
= (PUSER_MESSAGE_QUEUE
)InterlockedExchangePointer((PVOID
*)&pdo
->ActiveMessageQueue
, NewQueue
);
499 (void)InterlockedExchangePointer((PVOID
*)&Old
->Desktop
, 0);
500 IntDereferenceMessageQueue(Old
);
501 gpqForegroundPrev
= Old
;
503 // Only one Q can have active foreground even when there are more than one desktop.
504 if (NewQueue
) gpqForeground
= pdo
->ActiveMessageQueue
;
505 else gpqForeground
= NULL
;
509 IntGetThreadDesktopWindow(PTHREADINFO pti
)
511 if (!pti
) pti
= PsGetCurrentThreadWin32Thread();
512 if (pti
->pDeskInfo
) return pti
->pDeskInfo
->spwnd
;
516 PWND FASTCALL
co_GetDesktopWindow(PWND pWnd
)
518 if (pWnd
->head
.rpdesk
&&
519 pWnd
->head
.rpdesk
->pDeskInfo
)
520 return pWnd
->head
.rpdesk
->pDeskInfo
->spwnd
;
524 HWND FASTCALL
IntGetDesktopWindow(VOID
)
526 PDESKTOP pdo
= IntGetActiveDesktop();
529 TRACE("No active desktop\n");
532 return pdo
->DesktopWindow
;
535 PWND FASTCALL
UserGetDesktopWindow(VOID
)
537 PDESKTOP pdo
= IntGetActiveDesktop();
541 TRACE("No active desktop\n");
544 // return pdo->pDeskInfo->spwnd;
545 return UserGetWindowObject(pdo
->DesktopWindow
);
548 HWND FASTCALL
IntGetMessageWindow(VOID
)
550 PDESKTOP pdo
= IntGetActiveDesktop();
554 TRACE("No active desktop\n");
557 return pdo
->spwndMessage
->head
.h
;
560 PWND FASTCALL
UserGetMessageWindow(VOID
)
562 PDESKTOP pdo
= IntGetActiveDesktop();
566 TRACE("No active desktop\n");
569 return pdo
->spwndMessage
;
572 HWND FASTCALL
IntGetCurrentThreadDesktopWindow(VOID
)
574 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
575 PDESKTOP pdo
= pti
->rpdesk
;
578 ERR("Thread doesn't have a desktop\n");
581 return pdo
->DesktopWindow
;
584 /* PUBLIC FUNCTIONS ***********************************************************/
587 DesktopWindowProc(PWND Wnd
, UINT Msg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*lResult
)
591 //ERR("DesktopWindowProc\n");
600 Wnd
->fnid
= FNID_DESKTOP
;
602 *lResult
= (LRESULT
)TRUE
;
606 Value
= HandleToULong(PsGetCurrentProcessId());
608 co_UserSetWindowLong(UserHMGetHandle(Wnd
), DT_GWL_PROCESSID
, Value
, FALSE
);
609 Value
= HandleToULong(PsGetCurrentThreadId());
611 co_UserSetWindowLong(UserHMGetHandle(Wnd
), DT_GWL_THREADID
, Value
, FALSE
);
615 case WM_DISPLAYCHANGE
:
616 co_WinPosSetWindowPos(Wnd
, 0, 0, 0, LOWORD(lParam
), HIWORD(lParam
), SWP_NOZORDER
| SWP_NOACTIVATE
);
620 IntPaintDesktop((HDC
)wParam
);
626 if (IntBeginPaint(Wnd
, &Ps
))
628 IntEndPaint(Wnd
, &Ps
);
632 case WM_SYSCOLORCHANGE
:
633 co_UserRedrawWindow(Wnd
, NULL
, NULL
, RDW_INVALIDATE
|RDW_ERASE
|RDW_ALLCHILDREN
);
637 PCURICON_OBJECT pcurOld
, pcurNew
;
638 pcurNew
= UserGetCurIconObject(gDesktopCursor
);
643 pcurOld
= UserSetCursor(pcurNew
, FALSE
);
646 UserDereferenceObject(pcurOld
);
650 return TRUE
; /* We are done. Do not do any callbacks to user mode */
654 UserMessageWindowProc(PWND pwnd
, UINT Msg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*lResult
)
661 pwnd
->fnid
|= FNID_MESSAGEWND
;
662 *lResult
= (LRESULT
)TRUE
;
665 pwnd
->fnid
|= FNID_DESTROY
;
669 return TRUE
; /* We are done. Do not do any callbacks to user mode */
672 VOID NTAPI
DesktopThreadMain()
677 gptiDesktopThread
= PsGetCurrentThreadWin32Thread();
679 UserEnterExclusive();
681 /* Register system classes. This thread does not belong to any desktop so the
682 classes will be allocated from the shared heap */
683 UserRegisterSystemClasses();
687 Ret
= co_IntGetPeekMessage(&Msg
, 0, 0, 0, PM_REMOVE
, TRUE
);
690 IntDispatchMessage(&Msg
);
698 UserGetDesktopDC(ULONG DcType
, BOOL EmptyDC
, BOOL ValidatehWnd
)
700 PWND DesktopObject
= 0;
703 if (DcType
== DC_TYPE_DIRECT
)
705 DesktopObject
= UserGetDesktopWindow();
706 DesktopHDC
= (HDC
)UserGetWindowDC(DesktopObject
);
710 PMONITOR pMonitor
= UserGetPrimaryMonitor();
711 DesktopHDC
= IntGdiCreateDisplayDC(pMonitor
->hDev
, DcType
, EmptyDC
);
723 Window
= UserGetDesktopWindow();
724 hRgn
= IntSysCreateRectRgnIndirect(&Window
->rcWindow
);
726 IntInvalidateWindows( Window
,
733 GreDeleteObject(hRgn
);
738 co_IntShowDesktop(PDESKTOP Desktop
, ULONG Width
, ULONG Height
)
742 Window
= IntGetWindowObject(Desktop
->DesktopWindow
);
746 ERR("No Desktop window.\n");
747 return STATUS_UNSUCCESSFUL
;
749 co_WinPosSetWindowPos(Window
, NULL
, 0, 0, Width
, Height
, SWP_NOACTIVATE
|SWP_NOZORDER
|SWP_SHOWWINDOW
);
751 co_UserRedrawWindow( Window
, NULL
, 0, RDW_UPDATENOW
| RDW_ALLCHILDREN
);
752 return STATUS_SUCCESS
;
756 IntHideDesktop(PDESKTOP Desktop
)
760 DesktopWnd
= IntGetWindowObject(Desktop
->DesktopWindow
);
763 return ERROR_INVALID_WINDOW_HANDLE
;
765 DesktopWnd
->style
&= ~WS_VISIBLE
;
767 return STATUS_SUCCESS
;
772 UserBuildShellHookHwndList(PDESKTOP Desktop
)
775 PSHELL_HOOK_WINDOW Current
;
778 /* FIXME: If we save nb elements in desktop, we dont have to loop to find nb entries */
779 LIST_FOR_EACH(Current
, &Desktop
->ShellHookWindows
, SHELL_HOOK_WINDOW
, ListEntry
)
782 if (!entries
) return NULL
;
784 list
= ExAllocatePoolWithTag(PagedPool
, sizeof(HWND
) * (entries
+ 1), USERTAG_WINDOWLIST
); /* alloc one extra for nullterm */
789 LIST_FOR_EACH(Current
, &Desktop
->ShellHookWindows
, SHELL_HOOK_WINDOW
, ListEntry
)
790 *cursor
++ = Current
->hWnd
;
792 *cursor
= NULL
; /* Nullterm list */
799 * Send the Message to the windows registered for ShellHook
800 * notifications. The lParam contents depend on the Message. See
801 * MSDN for more details (RegisterShellHookWindow)
803 VOID
co_IntShellHookNotify(WPARAM Message
, WPARAM wParam
, LPARAM lParam
)
805 PDESKTOP Desktop
= IntGetActiveDesktop();
808 if (!gpsi
->uiShellMsg
)
810 gpsi
->uiShellMsg
= IntAddAtom(L
"SHELLHOOK");
812 TRACE("MsgType = %x\n", gpsi
->uiShellMsg
);
813 if (!gpsi
->uiShellMsg
)
814 ERR("LastError: %x\n", EngGetLastError());
819 TRACE("IntShellHookNotify: No desktop!\n");
823 // FIXME: System Tray Support.
825 HwndList
= UserBuildShellHookHwndList(Desktop
);
828 HWND
* cursor
= HwndList
;
830 for (; *cursor
; cursor
++)
832 TRACE("Sending notify\n");
833 co_IntPostOrSendMessage(*cursor
,
836 (Message
== HSHELL_LANGUAGE
? lParam
: (LPARAM
)wParam
) );
839 ExFreePoolWithTag(HwndList
, USERTAG_WINDOWLIST
);
842 if (ISITHOOKED(WH_SHELL
))
844 co_HOOK_CallHooks(WH_SHELL
, Message
, wParam
, lParam
);
849 * Add the window to the ShellHookWindows list. The windows
850 * on that list get notifications that are important to shell
853 * TODO: Validate the window? I'm not sure if sending these messages to
854 * an unsuspecting application that is not your own is a nice thing to do.
856 BOOL
IntRegisterShellHookWindow(HWND hWnd
)
858 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
859 PDESKTOP Desktop
= pti
->rpdesk
;
860 PSHELL_HOOK_WINDOW Entry
;
862 TRACE("IntRegisterShellHookWindow\n");
864 /* First deregister the window, so we can be sure it's never twice in the
867 IntDeRegisterShellHookWindow(hWnd
);
869 Entry
= ExAllocatePoolWithTag(PagedPool
,
870 sizeof(SHELL_HOOK_WINDOW
),
878 InsertTailList(&Desktop
->ShellHookWindows
, &Entry
->ListEntry
);
884 * Remove the window from the ShellHookWindows list. The windows
885 * on that list get notifications that are important to shell
888 BOOL
IntDeRegisterShellHookWindow(HWND hWnd
)
890 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
891 PDESKTOP Desktop
= pti
->rpdesk
;
892 PSHELL_HOOK_WINDOW Current
;
894 LIST_FOR_EACH(Current
, &Desktop
->ShellHookWindows
, SHELL_HOOK_WINDOW
, ListEntry
)
896 if (Current
->hWnd
== hWnd
)
898 RemoveEntryList(&Current
->ListEntry
);
899 ExFreePoolWithTag(Current
, TAG_WINSTA
);
908 IntFreeDesktopHeap(IN OUT PDESKTOP Desktop
)
910 /* FIXME: Disable until unmapping works in mm */
912 if (Desktop
->pheapDesktop
!= NULL
)
914 MmUnmapViewInSessionSpace(Desktop
->pheapDesktop
);
915 Desktop
->pheapDesktop
= NULL
;
918 if (Desktop
->hsectionDesktop
!= NULL
)
920 ObDereferenceObject(Desktop
->hsectionDesktop
);
921 Desktop
->hsectionDesktop
= NULL
;
927 IntPaintDesktop(HDC hDC
)
930 HBRUSH DesktopBrush
, PreviousBrush
;
932 BOOL doPatBlt
= TRUE
;
934 static WCHAR s_wszSafeMode
[] = L
"Safe Mode";
940 GdiGetClipBox(hDC
, &Rect
);
942 hWndDesktop
= IntGetDesktopWindow(); // rpdesk->DesktopWindow;
944 WndDesktop
= UserGetWindowObject(hWndDesktop
); // rpdesk->pDeskInfo->spwnd;
950 if (!UserGetSystemMetrics(SM_CLEANBOOT
))
952 DesktopBrush
= (HBRUSH
)WndDesktop
->pcls
->hbrBackground
;
955 * Paint desktop background
957 if (gspv
.hbmWallpaper
!= NULL
)
963 sz
.cx
= WndDesktop
->rcWindow
.right
- WndDesktop
->rcWindow
.left
;
964 sz
.cy
= WndDesktop
->rcWindow
.bottom
- WndDesktop
->rcWindow
.top
;
966 if (gspv
.WallpaperMode
== wmStretch
||
967 gspv
.WallpaperMode
== wmTile
)
974 /* Find the upper left corner, can be negtive if the bitmap is bigger then the screen */
975 x
= (sz
.cx
/ 2) - (gspv
.cxWallpaper
/ 2);
976 y
= (sz
.cy
/ 2) - (gspv
.cyWallpaper
/ 2);
979 hWallpaperDC
= NtGdiCreateCompatibleDC(hDC
);
980 if(hWallpaperDC
!= NULL
)
984 /* Fill in the area that the bitmap is not going to cover */
987 /* FIXME: Clip out the bitmap
988 can be replaced with "NtGdiPatBlt(hDC, x, y, WinSta->cxWallpaper, WinSta->cyWallpaper, PATCOPY | DSTINVERT);"
989 once we support DSTINVERT */
990 PreviousBrush
= NtGdiSelectBrush(hDC
, DesktopBrush
);
991 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
992 NtGdiSelectBrush(hDC
, PreviousBrush
);
995 /*Do not fill the background after it is painted no matter the size of the picture */
998 hOldBitmap
= NtGdiSelectBitmap(hWallpaperDC
, gspv
.hbmWallpaper
);
1000 if (gspv
.WallpaperMode
== wmStretch
)
1002 if(Rect
.right
&& Rect
.bottom
)
1003 NtGdiStretchBlt(hDC
,
1017 else if (gspv
.WallpaperMode
== wmTile
)
1019 /* Paint the bitmap across the screen then down */
1020 for(y
= 0; y
< Rect
.bottom
; y
+= gspv
.cyWallpaper
)
1022 for(x
= 0; x
< Rect
.right
; x
+= gspv
.cxWallpaper
)
1052 NtGdiSelectBitmap(hWallpaperDC
, hOldBitmap
);
1053 NtGdiDeleteObjectApp(hWallpaperDC
);
1059 /* Black desktop background in Safe Mode */
1060 DesktopBrush
= StockObjects
[BLACK_BRUSH
];
1062 /* Back ground is set to none, clear the screen */
1065 PreviousBrush
= NtGdiSelectBrush(hDC
, DesktopBrush
);
1066 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
1067 NtGdiSelectBrush(hDC
, PreviousBrush
);
1071 * Display system version on the desktop background
1074 if (g_PaintDesktopVersion
||UserGetSystemMetrics(SM_CLEANBOOT
))
1076 static WCHAR s_wszVersion
[256] = {0};
1081 len
= wcslen(s_wszVersion
);
1085 len
= GetSystemVersionString(s_wszVersion
);
1090 if (!UserSystemParametersInfo(SPI_GETWORKAREA
, 0, &rect
, 0))
1092 rect
.right
= UserGetSystemMetrics(SM_CXSCREEN
);
1093 rect
.bottom
= UserGetSystemMetrics(SM_CYSCREEN
);
1096 color_old
= IntGdiSetTextColor(hDC
, RGB(255,255,255));
1097 align_old
= IntGdiSetTextAlign(hDC
, TA_RIGHT
);
1098 mode_old
= IntGdiSetBkMode(hDC
, TRANSPARENT
);
1100 if(!UserGetSystemMetrics(SM_CLEANBOOT
))
1102 GreExtTextOutW(hDC
, rect
.right
-16, rect
.bottom
-48, 0, NULL
, s_wszVersion
, len
, NULL
, 0);
1107 /* Version information text in top center */
1108 IntGdiSetTextAlign(hDC
, TA_CENTER
|TA_TOP
);
1109 GreExtTextOutW(hDC
, (rect
.right
+rect
.left
)/2, rect
.top
, 0, NULL
, s_wszVersion
, len
, NULL
, 0);
1110 /* Safe Mode text in corners */
1111 len
= wcslen(s_wszSafeMode
);
1112 IntGdiSetTextAlign(hDC
, TA_RIGHT
|TA_TOP
);
1113 GreExtTextOutW(hDC
, rect
.right
, rect
.top
, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1114 IntGdiSetTextAlign(hDC
, TA_RIGHT
|TA_BASELINE
);
1115 GreExtTextOutW(hDC
, rect
.right
, rect
.bottom
, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1116 IntGdiSetTextAlign(hDC
, TA_LEFT
|TA_TOP
);
1117 GreExtTextOutW(hDC
, rect
.left
, rect
.top
, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1118 IntGdiSetTextAlign(hDC
, TA_LEFT
|TA_BASELINE
);
1119 GreExtTextOutW(hDC
, rect
.left
, rect
.bottom
, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1122 IntGdiSetBkMode(hDC
, mode_old
);
1123 IntGdiSetTextAlign(hDC
, align_old
);
1124 IntGdiSetTextColor(hDC
, color_old
);
1131 UserInitializeDesktop(PDESKTOP pdesk
, PUNICODE_STRING DesktopName
, PWINSTATION_OBJECT pwinsta
)
1133 PVOID DesktopHeapSystemBase
= NULL
;
1134 ULONG_PTR HeapSize
= 400 * 1024;
1135 SIZE_T DesktopInfoSize
;
1138 TRACE("UserInitializeDesktop desktop 0x%p with name %wZ\n", pdesk
, DesktopName
);
1140 RtlZeroMemory(pdesk
, sizeof(DESKTOP
));
1142 /* Link the desktop with the parent window station */
1143 pdesk
->rpwinstaParent
= pwinsta
;
1144 InsertTailList(&pwinsta
->DesktopListHead
, &pdesk
->ListEntry
);
1146 /* Create the desktop heap */
1147 pdesk
->hsectionDesktop
= NULL
;
1148 pdesk
->pheapDesktop
= UserCreateHeap(&pdesk
->hsectionDesktop
,
1149 &DesktopHeapSystemBase
,
1151 if (pdesk
->pheapDesktop
== NULL
)
1153 ERR("Failed to create desktop heap!\n");
1154 return STATUS_NO_MEMORY
;
1157 /* Create DESKTOPINFO */
1158 DesktopInfoSize
= sizeof(DESKTOPINFO
) + DesktopName
->Length
+ sizeof(WCHAR
);
1159 pdesk
->pDeskInfo
= RtlAllocateHeap(pdesk
->pheapDesktop
,
1160 HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
,
1162 if (pdesk
->pDeskInfo
== NULL
)
1164 ERR("Failed to create the DESKTOP structure!\n");
1165 return STATUS_NO_MEMORY
;
1168 /* Initialize the DESKTOPINFO */
1169 pdesk
->pDeskInfo
->pvDesktopBase
= DesktopHeapSystemBase
;
1170 pdesk
->pDeskInfo
->pvDesktopLimit
= (PVOID
)((ULONG_PTR
)DesktopHeapSystemBase
+ HeapSize
);
1171 RtlCopyMemory(pdesk
->pDeskInfo
->szDesktopName
,
1172 DesktopName
->Buffer
,
1173 DesktopName
->Length
+ sizeof(WCHAR
));
1174 for (i
= 0; i
< NB_HOOKS
; i
++)
1176 InitializeListHead(&pdesk
->pDeskInfo
->aphkStart
[i
]);
1179 InitializeListHead(&pdesk
->ShellHookWindows
);
1180 InitializeListHead(&pdesk
->PtiList
);
1182 return STATUS_SUCCESS
;
1185 /* SYSCALLS *******************************************************************/
1188 * NtUserCreateDesktop
1190 * Creates a new desktop.
1194 * Object Attributes.
1197 * Name of the device.
1203 * Interaction flags.
1206 * Requested type of access.
1210 * If the function succeeds, the return value is a handle to the newly
1211 * created desktop. If the specified desktop already exists, the function
1212 * succeeds and returns a handle to the existing desktop. When you are
1213 * finished using the handle, call the CloseDesktop function to close it.
1214 * If the function fails, the return value is NULL.
1221 NtUserCreateDesktop(
1222 POBJECT_ATTRIBUTES ObjectAttributes
,
1223 PUNICODE_STRING lpszDesktopDevice
,
1226 ACCESS_MASK dwDesiredAccess
)
1228 PDESKTOP pdesk
= NULL
;
1229 NTSTATUS Status
= STATUS_SUCCESS
;
1232 UNICODE_STRING ClassName
;
1233 LARGE_STRING WindowName
;
1234 BOOL NoHooks
= FALSE
;
1237 PTHREADINFO ptiCurrent
;
1240 DECLARE_RETURN(HDESK
);
1242 TRACE("Enter NtUserCreateDesktop\n");
1243 UserEnterExclusive();
1245 ptiCurrent
= PsGetCurrentThreadWin32Thread();
1247 ASSERT(gptiDesktopThread
);
1249 /* Turn off hooks when calling any CreateWindowEx from inside win32k. */
1250 NoHooks
= (ptiCurrent
->TIF_flags
& TIF_DISABLEHOOKS
);
1251 ptiCurrent
->TIF_flags
|= TIF_DISABLEHOOKS
;
1252 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
1255 * Try to open already existing desktop
1257 Status
= ObOpenObjectByName(
1259 ExDesktopObjectType
,
1265 if (!NT_SUCCESS(Status
))
1267 ERR("ObOpenObjectByName failed to open/create desktop\n");
1268 SetLastNtError(Status
);
1272 /* In case the object was not created (eg if it existed), return now */
1273 if (Context
== FALSE
)
1275 TRACE("NtUserCreateDesktop opened desktop %wZ\n", ObjectAttributes
->ObjectName
);
1279 /* Reference the desktop */
1280 Status
= ObReferenceObjectByHandle(hdesk
,
1282 ExDesktopObjectType
,
1286 if (!NT_SUCCESS(Status
))
1288 ERR("Failed to reference desktop object\n");
1289 SetLastNtError(Status
);
1293 if (!ptiCurrent
->rpdesk
) IntSetThreadDesktop(hdesk
,FALSE
);
1295 /* Get the desktop window class. The thread desktop does not belong to any desktop
1296 * so the classes created there (including the desktop class) are allocated in the shared heap
1297 * It would cause problems if we used a class that belongs to the caller
1299 ClassName
.Buffer
= WC_DESKTOP
;
1300 ClassName
.Length
= 0;
1301 pcls
= IntGetAndReferenceClass(&ClassName
, 0, TRUE
);
1308 RtlZeroMemory(&WindowName
, sizeof(WindowName
));
1309 RtlZeroMemory(&Cs
, sizeof(Cs
));
1310 Cs
.x
= UserGetSystemMetrics(SM_XVIRTUALSCREEN
),
1311 Cs
.y
= UserGetSystemMetrics(SM_YVIRTUALSCREEN
),
1312 Cs
.cx
= UserGetSystemMetrics(SM_CXVIRTUALSCREEN
),
1313 Cs
.cy
= UserGetSystemMetrics(SM_CYVIRTUALSCREEN
),
1314 Cs
.style
= WS_POPUP
|WS_CLIPCHILDREN
;
1315 Cs
.hInstance
= hModClient
; // hModuleWin; // Server side winproc!
1316 Cs
.lpszName
= (LPCWSTR
) &WindowName
;
1317 Cs
.lpszClass
= (LPCWSTR
) &ClassName
;
1319 /* Use IntCreateWindow instead of co_UserCreateWindowEx cause the later expects a thread with a desktop */
1320 pWnd
= IntCreateWindow(&Cs
, &WindowName
, pcls
, NULL
, NULL
, NULL
, pdesk
);
1323 ERR("Failed to create desktop window for the new desktop\n");
1327 pdesk
->DesktopWindow
= pWnd
->head
.h
;
1328 pdesk
->pDeskInfo
->spwnd
= pWnd
;
1329 pWnd
->fnid
= FNID_DESKTOP
;
1331 ClassName
.Buffer
= MAKEINTATOM(gpsi
->atomSysClass
[ICLS_HWNDMESSAGE
]);
1332 ClassName
.Length
= 0;
1333 pcls
= IntGetAndReferenceClass(&ClassName
, 0, TRUE
);
1340 RtlZeroMemory(&WindowName
, sizeof(WindowName
));
1341 RtlZeroMemory(&Cs
, sizeof(Cs
));
1342 Cs
.cx
= Cs
.cy
= 100;
1343 Cs
.style
= WS_POPUP
|WS_CLIPCHILDREN
;
1344 Cs
.hInstance
= hModClient
; // hModuleWin; // Server side winproc!
1345 Cs
.lpszName
= (LPCWSTR
) &WindowName
;
1346 Cs
.lpszClass
= (LPCWSTR
) &ClassName
;
1347 pWnd
= IntCreateWindow(&Cs
, &WindowName
, pcls
, NULL
, NULL
, NULL
, pdesk
);
1350 ERR("Failed to create message window for the new desktop\n");
1354 pdesk
->spwndMessage
= pWnd
;
1355 pWnd
->fnid
= FNID_MESSAGEWND
;
1358 if !(WinStaObject->Flags & WSF_NOIO) is (not set) for desktop input output mode (see wiki)
1359 Create Tooltip. Saved in DesktopObject->spwndTooltip.
1360 Tooltip dwExStyle: WS_EX_TOOLWINDOW|WS_EX_TOPMOST
1361 hWndParent are spwndMessage. Use hModuleWin for server side winproc!
1362 The rest is same as message window.
1363 http://msdn.microsoft.com/en-us/library/bb760250(VS.85).aspx
1370 ObDereferenceObject(pdesk
);
1372 if (_ret_
== NULL
&& hdesk
!= NULL
)
1374 ObCloseHandle(hdesk
, UserMode
);
1378 ptiCurrent
->TIF_flags
&= ~TIF_DISABLEHOOKS
;
1379 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
1381 TRACE("Leave NtUserCreateDesktop, ret=%p\n",_ret_
);
1389 * Opens an existing desktop.
1393 * Name of the existing desktop.
1396 * Interaction flags.
1399 * Requested type of access.
1402 * Handle to the desktop or zero on failure.
1410 POBJECT_ATTRIBUTES ObjectAttributes
,
1412 ACCESS_MASK dwDesiredAccess
)
1417 Status
= ObOpenObjectByName(
1419 ExDesktopObjectType
,
1426 if (!NT_SUCCESS(Status
))
1428 ERR("Failed to open desktop\n");
1429 SetLastNtError(Status
);
1433 TRACE("Opened desktop %S with handle 0x%p\n", ObjectAttributes
->ObjectName
->Buffer
, Desktop
);
1439 * NtUserOpenInputDesktop
1441 * Opens the input (interactive) desktop.
1445 * Interaction flags.
1448 * Inheritance option.
1451 * Requested type of access.
1454 * Handle to the input desktop or zero on failure.
1461 NtUserOpenInputDesktop(
1464 ACCESS_MASK dwDesiredAccess
)
1468 ULONG HandleAttributes
= 0;
1470 UserEnterExclusive();
1471 TRACE("Enter NtUserOpenInputDesktop gpdeskInputDesktop 0x%p\n",gpdeskInputDesktop
);
1473 if(fInherit
) HandleAttributes
= OBJ_INHERIT
;
1475 /* Create a new handle to the object */
1476 Status
= ObOpenObjectByPointer(
1481 ExDesktopObjectType
,
1485 if (!NT_SUCCESS(Status
))
1487 ERR("Failed to open input desktop object\n");
1488 SetLastNtError(Status
);
1491 TRACE("NtUserOpenInputDesktop returning 0x%p\n",hdesk
);
1497 * NtUserCloseDesktop
1499 * Closes a desktop handle.
1503 * Handle to the desktop.
1509 * The desktop handle can be created with NtUserCreateDesktop or
1510 * NtUserOpenDesktop. This function will fail if any thread in the calling
1511 * process is using the specified desktop handle or if the handle refers
1512 * to the initial desktop of the calling process.
1519 NtUserCloseDesktop(HDESK hDesktop
)
1523 DECLARE_RETURN(BOOL
);
1525 TRACE("NtUserCloseDesktop called (0x%p)\n", hDesktop
);
1526 UserEnterExclusive();
1528 if( hDesktop
== gptiCurrent
->hdesk
|| hDesktop
== gptiCurrent
->ppi
->hdeskStartup
)
1530 ERR("Attempted to close thread desktop\n");
1531 EngSetLastError(ERROR_BUSY
);
1535 Status
= IntValidateDesktopHandle( hDesktop
, UserMode
, 0, &pdesk
);
1536 if (!NT_SUCCESS(Status
))
1538 ERR("Validation of desktop handle (0x%p) failed\n", hDesktop
);
1542 ObDereferenceObject(pdesk
);
1544 Status
= ZwClose(hDesktop
);
1545 if (!NT_SUCCESS(Status
))
1547 ERR("Failed to close desktop handle 0x%p\n", hDesktop
);
1548 SetLastNtError(Status
);
1555 TRACE("Leave NtUserCloseDesktop, ret=%i\n",_ret_
);
1561 * NtUserPaintDesktop
1563 * The NtUserPaintDesktop function fills the clipping region in the
1564 * specified device context with the desktop pattern or wallpaper. The
1565 * function is provided primarily for shell desktops.
1569 * Handle to the device context.
1576 NtUserPaintDesktop(HDC hDC
)
1579 UserEnterExclusive();
1580 TRACE("Enter NtUserPaintDesktop\n");
1581 Ret
= IntPaintDesktop(hDC
);
1582 TRACE("Leave NtUserPaintDesktop, ret=%i\n",Ret
);
1588 * NtUserSwitchDesktop
1590 * Sets the current input (interactive) desktop.
1594 * Handle to desktop.
1604 NtUserSwitchDesktop(HDESK hdesk
)
1608 DECLARE_RETURN(BOOL
);
1610 UserEnterExclusive();
1611 TRACE("Enter NtUserSwitchDesktop(0x%p)\n", hdesk
);
1613 Status
= IntValidateDesktopHandle( hdesk
, UserMode
, 0, &pdesk
);
1614 if (!NT_SUCCESS(Status
))
1616 ERR("Validation of desktop handle (0x%p) failed\n", hdesk
);
1621 * Don't allow applications switch the desktop if it's locked, unless the caller
1622 * is the logon application itself
1624 if((pdesk
->rpwinstaParent
->Flags
& WSS_LOCKED
) &&
1625 LogonProcess
!= PsGetCurrentProcessWin32Process())
1627 ObDereferenceObject(pdesk
);
1628 ERR("Switching desktop 0x%p denied because the window station is locked!\n", hdesk
);
1632 if(pdesk
->rpwinstaParent
!= InputWindowStation
)
1634 ObDereferenceObject(pdesk
);
1635 ERR("Switching desktop 0x%p denied because desktop doesn't belong to the interactive winsta!\n", hdesk
);
1639 /* FIXME: Fail if the process is associated with a secured
1640 desktop such as Winlogon or Screen-Saver */
1641 /* FIXME: Connect to input device */
1643 /* Set the active desktop in the desktop's window station. */
1644 InputWindowStation
->ActiveDesktop
= pdesk
;
1646 /* Set the global state. */
1647 gpdeskInputDesktop
= pdesk
;
1648 TRACE("SwitchDesktop gpdeskInputDesktop 0x%p\n",gpdeskInputDesktop
);
1649 ObDereferenceObject(pdesk
);
1654 TRACE("Leave NtUserSwitchDesktop, ret=%i\n",_ret_
);
1660 * NtUserGetThreadDesktop
1667 NtUserGetThreadDesktop(DWORD dwThreadId
, DWORD Unknown1
)
1671 PDESKTOP DesktopObject
;
1672 HDESK Ret
, hThreadDesktop
;
1673 OBJECT_HANDLE_INFORMATION HandleInformation
;
1674 DECLARE_RETURN(HDESK
);
1676 UserEnterExclusive();
1677 TRACE("Enter NtUserGetThreadDesktop\n");
1681 EngSetLastError(ERROR_INVALID_PARAMETER
);
1685 Status
= PsLookupThreadByThreadId((HANDLE
)(DWORD_PTR
)dwThreadId
, &Thread
);
1686 if(!NT_SUCCESS(Status
))
1688 EngSetLastError(ERROR_INVALID_PARAMETER
);
1692 if(Thread
->ThreadsProcess
== PsGetCurrentProcess())
1694 /* Just return the handle, we queried the desktop handle of a thread running
1695 in the same context */
1696 Ret
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->hdesk
;
1697 ObDereferenceObject(Thread
);
1701 /* Get the desktop handle and the desktop of the thread */
1702 if(!(hThreadDesktop
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->hdesk
) ||
1703 !(DesktopObject
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->rpdesk
))
1705 ObDereferenceObject(Thread
);
1706 ERR("Desktop information of thread 0x%x broken!?\n", dwThreadId
);
1710 /* We could just use DesktopObject instead of looking up the handle, but latter
1711 may be a bit safer (e.g. when the desktop is being destroyed */
1712 /* Switch into the context of the thread we're trying to get the desktop from,
1713 so we can use the handle */
1714 KeAttachProcess(&Thread
->ThreadsProcess
->Pcb
);
1715 Status
= ObReferenceObjectByHandle(hThreadDesktop
,
1717 ExDesktopObjectType
,
1719 (PVOID
*)&DesktopObject
,
1720 &HandleInformation
);
1723 /* The handle couldn't be found, there's nothing to get... */
1724 if(!NT_SUCCESS(Status
))
1726 ObDereferenceObject(Thread
);
1730 /* Lookup our handle table if we can find a handle to the desktop object,
1731 if not, create one */
1732 Ret
= IntGetDesktopObjectHandle(DesktopObject
);
1734 /* All done, we got a valid handle to the desktop */
1735 ObDereferenceObject(DesktopObject
);
1736 ObDereferenceObject(Thread
);
1740 TRACE("Leave NtUserGetThreadDesktop, ret=%p\n",_ret_
);
1746 IntUnmapDesktopView(IN PDESKTOP pdesk
)
1749 PW32HEAP_USER_MAPPING HeapMapping
, *PrevLink
;
1750 NTSTATUS Status
= STATUS_SUCCESS
;
1752 TRACE("IntUnmapDesktopView called for desktop object %p\n", pdesk
);
1754 ppi
= PsGetCurrentProcessWin32Process();
1755 PrevLink
= &ppi
->HeapMappings
.Next
;
1757 /* Unmap if we're the last thread using the desktop */
1758 HeapMapping
= *PrevLink
;
1759 while (HeapMapping
!= NULL
)
1761 if (HeapMapping
->KernelMapping
== (PVOID
)pdesk
->pheapDesktop
)
1763 if (--HeapMapping
->Count
== 0)
1765 *PrevLink
= HeapMapping
->Next
;
1767 TRACE("ppi 0x%p unmapped heap of desktop 0x%p\n", ppi
, pdesk
);
1768 Status
= MmUnmapViewOfSection(PsGetCurrentProcess(),
1769 HeapMapping
->UserMapping
);
1771 ObDereferenceObject(pdesk
);
1773 UserHeapFree(HeapMapping
);
1778 PrevLink
= &HeapMapping
->Next
;
1779 HeapMapping
= HeapMapping
->Next
;
1786 IntMapDesktopView(IN PDESKTOP pdesk
)
1789 PW32HEAP_USER_MAPPING HeapMapping
, *PrevLink
;
1790 PVOID UserBase
= NULL
;
1791 SIZE_T ViewSize
= 0;
1792 LARGE_INTEGER Offset
;
1795 TRACE("IntMapDesktopView called for desktop object 0x%p\n", pdesk
);
1797 ppi
= PsGetCurrentProcessWin32Process();
1798 PrevLink
= &ppi
->HeapMappings
.Next
;
1800 /* Find out if another thread already mapped the desktop heap */
1801 HeapMapping
= *PrevLink
;
1802 while (HeapMapping
!= NULL
)
1804 if (HeapMapping
->KernelMapping
== (PVOID
)pdesk
->pheapDesktop
)
1806 HeapMapping
->Count
++;
1807 return STATUS_SUCCESS
;
1810 PrevLink
= &HeapMapping
->Next
;
1811 HeapMapping
= HeapMapping
->Next
;
1814 /* We're the first, map the heap */
1815 Offset
.QuadPart
= 0;
1816 Status
= MmMapViewOfSection(pdesk
->hsectionDesktop
,
1817 PsGetCurrentProcess(),
1825 PAGE_EXECUTE_READ
); /* Would prefer PAGE_READONLY, but thanks to RTL heaps... */
1826 if (!NT_SUCCESS(Status
))
1828 ERR("Failed to map desktop\n");
1832 TRACE("ppi 0x%p mapped heap of desktop 0x%p\n", ppi
, pdesk
);
1834 /* Add the mapping */
1835 HeapMapping
= UserHeapAlloc(sizeof(W32HEAP_USER_MAPPING
));
1836 if (HeapMapping
== NULL
)
1838 MmUnmapViewOfSection(PsGetCurrentProcess(), UserBase
);
1839 ERR("UserHeapAlloc() failed!\n");
1840 return STATUS_NO_MEMORY
;
1843 HeapMapping
->Next
= NULL
;
1844 HeapMapping
->KernelMapping
= (PVOID
)pdesk
->pheapDesktop
;
1845 HeapMapping
->UserMapping
= UserBase
;
1846 HeapMapping
->Limit
= ViewSize
;
1847 HeapMapping
->Count
= 1;
1848 *PrevLink
= HeapMapping
;
1850 ObReferenceObject(pdesk
);
1852 return STATUS_SUCCESS
;
1856 IntSetThreadDesktop(IN HDESK hDesktop
,
1857 IN BOOL FreeOnFailure
)
1859 PDESKTOP pdesk
= NULL
, pdeskOld
;
1863 PCLIENTTHREADINFO pctiOld
, pctiNew
= NULL
;
1866 ASSERT(NtCurrentTeb());
1868 TRACE("IntSetThreadDesktop hDesktop:0x%p, FOF:%i\n",hDesktop
, FreeOnFailure
);
1870 pti
= PsGetCurrentThreadWin32Thread();
1871 pci
= pti
->pClientInfo
;
1873 /* If the caller gave us a desktop, ensure it is valid */
1874 if(hDesktop
!= NULL
)
1876 /* Validate the new desktop. */
1877 Status
= IntValidateDesktopHandle( hDesktop
, UserMode
, 0, &pdesk
);
1878 if (!NT_SUCCESS(Status
))
1880 ERR("Validation of desktop handle (0x%p) failed\n", hDesktop
);
1884 if (pti
->rpdesk
== pdesk
)
1887 ObDereferenceObject(pdesk
);
1892 /* Make sure that we don't own any window in the current desktop */
1893 if (!IsListEmpty(&pti
->WindowListHead
))
1896 ObDereferenceObject(pdesk
);
1897 ERR("Attempted to change thread desktop although the thread has windows!\n");
1898 EngSetLastError(ERROR_BUSY
);
1902 /* Desktop is being re-set so clear out foreground. */
1903 if (pti
->rpdesk
!= pdesk
&& pti
->MessageQueue
== gpqForeground
)
1905 // Like above, there shouldn't be any windows, hooks or anything active on this threads desktop!
1906 IntSetFocusMessageQueue(NULL
);
1909 /* Before doing the switch, map the new desktop heap and allocate the new pcti */
1912 Status
= IntMapDesktopView(pdesk
);
1913 if (!NT_SUCCESS(Status
))
1915 ERR("Failed to map desktop heap!\n");
1916 ObDereferenceObject(pdesk
);
1917 SetLastNtError(Status
);
1921 pctiNew
= DesktopHeapAlloc( pdesk
, sizeof(CLIENTTHREADINFO
));
1924 ERR("Failed to allocate new pcti\n");
1925 IntUnmapDesktopView(pdesk
);
1926 ObDereferenceObject(pdesk
);
1927 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1932 /* free all classes or move them to the shared heap */
1933 if(pti
->rpdesk
!= NULL
)
1935 if(!IntCheckProcessDesktopClasses(pti
->rpdesk
, FreeOnFailure
))
1937 ERR("Failed to move process classes to shared heap!\n");
1940 DesktopHeapFree(pdesk
, pctiNew
);
1941 IntUnmapDesktopView(pdesk
);
1942 ObDereferenceObject(pdesk
);
1948 pdeskOld
= pti
->rpdesk
;
1949 hdeskOld
= pti
->hdesk
;
1950 if (pti
->pcti
!= &pti
->cti
)
1951 pctiOld
= pti
->pcti
;
1958 pti
->rpdesk
= pdesk
;
1959 pti
->hdesk
= hDesktop
;
1960 pti
->pDeskInfo
= pti
->rpdesk
->pDeskInfo
;
1961 pti
->pcti
= pctiNew
;
1963 pci
->ulClientDelta
= DesktopHeapGetUserDelta();
1964 pci
->pDeskInfo
= (PVOID
)((ULONG_PTR
)pti
->pDeskInfo
- pci
->ulClientDelta
);
1965 pci
->pClientThreadInfo
= (PVOID
)((ULONG_PTR
)pti
->pcti
- pci
->ulClientDelta
);
1967 /* initialize the new pcti */
1970 RtlCopyMemory(pctiNew
, pctiOld
, sizeof(CLIENTTHREADINFO
));
1974 RtlZeroMemory(pctiNew
, sizeof(CLIENTTHREADINFO
));
1975 pci
->fsHooks
= pti
->fsHooks
;
1976 pci
->dwTIFlags
= pti
->TIF_flags
;
1983 pti
->pDeskInfo
= NULL
;
1984 pti
->pcti
= &pti
->cti
; // Always point inside so there will be no crash when posting or sending msg's!
1985 pci
->ulClientDelta
= 0;
1986 pci
->pDeskInfo
= NULL
;
1987 pci
->pClientThreadInfo
= NULL
;
1990 /* clean up the old desktop */
1991 if(pdeskOld
!= NULL
)
1993 RemoveEntryList(&pti
->PtiLink
);
1994 if (pctiOld
) DesktopHeapFree(pdeskOld
, pctiOld
);
1995 IntUnmapDesktopView(pdeskOld
);
1996 ObDereferenceObject(pdeskOld
);
2002 InsertTailList(&pdesk
->PtiList
, &pti
->PtiLink
);
2005 TRACE("IntSetThreadDesktop: pti 0x%p ppi 0x%p switched from object 0x%p to 0x%p\n", pti
, pti
->ppi
, pdeskOld
, pdesk
);
2011 * NtUserSetThreadDesktop
2018 NtUserSetThreadDesktop(HDESK hDesktop
)
2022 UserEnterExclusive();
2024 // FIXME: IntSetThreadDesktop validates the desktop handle, it should happen
2025 // here too and set the NT error level. Q. Is it necessary to have the validation
2026 // in IntSetThreadDesktop? Is it needed there too?
2027 if (hDesktop
|| (!hDesktop
&& PsGetCurrentProcess() == gpepCSRSS
))
2028 ret
= IntSetThreadDesktop(hDesktop
, FALSE
);