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
);
68 /// @todo Don't mess around with the object headers!
70 _PRAGMA_WARNING_SUPPRESS(__WARNING_DEREF_NULL_PTR
)
71 DesktopName
= GET_DESKTOP_NAME(Desktop
);
74 /* Compare the name */
75 if (RtlEqualUnicodeString(RemainingName
,
77 (Attributes
& OBJ_CASE_INSENSITIVE
)))
79 /* We found a match. Did this come from a create? */
82 /* Unless OPEN_IF was given, fail with an error */
83 if (!(Attributes
& OBJ_OPENIF
))
86 return STATUS_OBJECT_NAME_COLLISION
;
90 /* Otherwise, return with a warning only */
91 Status
= STATUS_OBJECT_NAME_EXISTS
;
96 /* This was a real open, so this is OK */
97 Status
= STATUS_SUCCESS
;
100 /* Reference the desktop and return it */
101 ObReferenceObject(Desktop
);
107 /* Go to the next desktop */
108 NextEntry
= NextEntry
->Flink
;
111 /* If we got here but this isn't a create, then fail */
112 if (!Context
) return STATUS_OBJECT_NAME_NOT_FOUND
;
114 /* Create the desktop object */
115 InitializeObjectAttributes(&ObjectAttributes
, RemainingName
, 0, NULL
, NULL
);
116 Status
= ObCreateObject(KernelMode
,
125 if (!NT_SUCCESS(Status
)) return Status
;
127 /* Initialize the desktop */
128 Status
= UserInitializeDesktop(Desktop
, RemainingName
, WinStaObject
);
129 if (!NT_SUCCESS(Status
))
131 ObDereferenceObject(Desktop
);
135 /* Set the desktop object and return success */
138 return STATUS_SUCCESS
;
142 IntDesktopObjectDelete(PWIN32_DELETEMETHOD_PARAMETERS Parameters
)
144 PDESKTOP pdesk
= (PDESKTOP
)Parameters
->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
);
164 IntDesktopOkToClose(PWIN32_OKAYTOCLOSEMETHOD_PARAMETERS Parameters
)
166 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
170 /* This happens when we leak desktop handles */
171 return STATUS_SUCCESS
;
174 /* Do not allow the current desktop or the initial desktop to be closed */
175 if( Parameters
->Handle
== pti
->ppi
->hdeskStartup
||
176 Parameters
->Handle
== pti
->hdesk
)
178 return STATUS_ACCESS_DENIED
;
181 return STATUS_SUCCESS
;
184 NTSTATUS NTAPI
IntDesktopObjectOpen(PWIN32_OPENMETHOD_PARAMETERS Parameters
)
186 PPROCESSINFO ppi
= PsGetProcessWin32Process(Parameters
->Process
);
188 return STATUS_SUCCESS
;
190 return IntMapDesktopView((PDESKTOP
)Parameters
->Object
);
193 NTSTATUS NTAPI
IntDesktopObjectClose(PWIN32_CLOSEMETHOD_PARAMETERS Parameters
)
195 PPROCESSINFO ppi
= PsGetProcessWin32Process(Parameters
->Process
);
198 /* This happens when the process leaks desktop handles.
199 * At this point the PPROCESSINFO is already destroyed */
200 return STATUS_SUCCESS
;
203 return IntUnmapDesktopView((PDESKTOP
)Parameters
->Object
);
207 /* PRIVATE FUNCTIONS **********************************************************/
212 InitDesktopImpl(VOID
)
214 GENERIC_MAPPING IntDesktopMapping
= { DESKTOP_READ
,
219 /* Set Desktop Object Attributes */
220 ExDesktopObjectType
->TypeInfo
.DefaultNonPagedPoolCharge
= sizeof(DESKTOP
);
221 ExDesktopObjectType
->TypeInfo
.GenericMapping
= IntDesktopMapping
;
222 ExDesktopObjectType
->TypeInfo
.ValidAccessMask
= DESKTOP_ALL_ACCESS
;
223 return STATUS_SUCCESS
;
226 static int GetSystemVersionString(LPWSTR buffer
)
228 RTL_OSVERSIONINFOEXW versionInfo
;
231 versionInfo
.dwOSVersionInfoSize
= sizeof(RTL_OSVERSIONINFOEXW
);
233 if (!NT_SUCCESS(RtlGetVersion((PRTL_OSVERSIONINFOW
)&versionInfo
)))
236 if (versionInfo
.dwMajorVersion
<= 4)
237 len
= swprintf(buffer
,
238 L
"ReactOS Version %d.%d %s Build %d",
239 versionInfo
.dwMajorVersion
, versionInfo
.dwMinorVersion
,
240 versionInfo
.szCSDVersion
, versionInfo
.dwBuildNumber
&0xFFFF);
242 len
= swprintf(buffer
,
243 L
"ReactOS %s (Build %d)",
244 versionInfo
.szCSDVersion
, versionInfo
.dwBuildNumber
&0xFFFF);
251 IntParseDesktopPath(PEPROCESS Process
,
252 PUNICODE_STRING DesktopPath
,
256 OBJECT_ATTRIBUTES ObjectAttributes
;
257 UNICODE_STRING ObjectName
;
259 WCHAR wstrWinstaFullName
[MAX_PATH
], *pwstrWinsta
= NULL
, *pwstrDesktop
= NULL
;
268 if(DesktopPath
->Buffer
!= NULL
&& DesktopPath
->Length
> sizeof(WCHAR
))
271 * Parse the desktop path string which can be in the form "WinSta\Desktop"
272 * or just "Desktop". In latter case WinSta0 will be used.
275 pwstrDesktop
= wcschr(DesktopPath
->Buffer
, L
'\\');
276 if(pwstrDesktop
!= NULL
)
280 pwstrWinsta
= DesktopPath
->Buffer
;
284 pwstrDesktop
= DesktopPath
->Buffer
;
288 TRACE("IntParseDesktopPath pwstrWinsta:%S pwstrDesktop:%S\n", pwstrWinsta
, pwstrDesktop
);
292 /* Search the process handle table for (inherited) window station
293 handles, use a more appropriate one than WinSta0 if possible. */
294 if (!ObFindHandleForObject(Process
,
296 ExWindowStationObjectType
,
301 /* We had no luck searching for opened handles, use WinSta0 now */
303 pwstrWinsta
= L
"WinSta0";
307 /* Search the process handle table for (inherited) desktop
308 handles, use a more appropriate one than Default if possible. */
309 if (!ObFindHandleForObject(Process
,
316 /* We had no luck searching for opened handles, use Desktop now */
318 pwstrDesktop
= L
"Default";
323 swprintf(wstrWinstaFullName
, L
"%wZ\\%ws", &gustrWindowStationsDir
, pwstrWinsta
);
324 RtlInitUnicodeString( &ObjectName
, wstrWinstaFullName
);
326 TRACE("parsed initial winsta: %wZ\n", &ObjectName
);
328 /* Open the window station */
329 InitializeObjectAttributes(&ObjectAttributes
,
331 OBJ_CASE_INSENSITIVE
,
335 Status
= ObOpenObjectByName(&ObjectAttributes
,
336 ExWindowStationObjectType
,
343 if(!NT_SUCCESS(Status
))
345 SetLastNtError(Status
);
346 ERR("Failed to reference window station %wZ PID: --!\n", &ObjectName
);
351 if(*hDesktop
== NULL
)
353 RtlInitUnicodeString(&ObjectName
, pwstrDesktop
);
355 TRACE("parsed initial desktop: %wZ\n", &ObjectName
);
357 /* Open the desktop object */
358 InitializeObjectAttributes(&ObjectAttributes
,
360 OBJ_CASE_INSENSITIVE
,
364 Status
= ObOpenObjectByName(&ObjectAttributes
,
372 if(!NT_SUCCESS(Status
))
377 SetLastNtError(Status
);
378 ERR("Failed to reference desktop %wZ PID: --!\n", &ObjectName
);
382 return STATUS_SUCCESS
;
386 * IntValidateDesktopHandle
388 * Validates the desktop handle.
391 * If the function succeeds, the handle remains referenced. If the
392 * fucntion fails, last error is set.
396 IntValidateDesktopHandle(
398 KPROCESSOR_MODE AccessMode
,
399 ACCESS_MASK DesiredAccess
,
404 Status
= ObReferenceObjectByHandle(
412 TRACE("IntValidateDesktopHandle: handle:0x%p obj:0x%p access:0x%x Status:0x%lx\n",
413 Desktop
, *Object
, DesiredAccess
, Status
);
415 if (!NT_SUCCESS(Status
))
416 SetLastNtError(Status
);
422 IntGetActiveDesktop(VOID
)
424 return gpdeskInputDesktop
;
428 * Returns or creates a handle to the desktop object
431 IntGetDesktopObjectHandle(PDESKTOP DesktopObject
)
436 ASSERT(DesktopObject
);
438 if (!ObFindHandleForObject(PsGetCurrentProcess(),
444 Status
= ObOpenObjectByPointer(DesktopObject
,
451 if(!NT_SUCCESS(Status
))
453 /* Unable to create a handle */
454 ERR("Unable to create a desktop handle\n");
460 ERR("Got handle: %p\n", Ret
);
466 PUSER_MESSAGE_QUEUE FASTCALL
467 IntGetFocusMessageQueue(VOID
)
469 PDESKTOP pdo
= IntGetActiveDesktop();
472 TRACE("No active desktop\n");
475 return (PUSER_MESSAGE_QUEUE
)pdo
->ActiveMessageQueue
;
479 IntSetFocusMessageQueue(PUSER_MESSAGE_QUEUE NewQueue
)
481 PUSER_MESSAGE_QUEUE Old
;
482 PDESKTOP pdo
= IntGetActiveDesktop();
485 TRACE("No active desktop\n");
490 if(NewQueue
->Desktop
!= NULL
)
492 TRACE("Message Queue already attached to another desktop!\n");
495 IntReferenceMessageQueue(NewQueue
);
496 (void)InterlockedExchangePointer((PVOID
*)&NewQueue
->Desktop
, pdo
);
498 Old
= (PUSER_MESSAGE_QUEUE
)InterlockedExchangePointer((PVOID
*)&pdo
->ActiveMessageQueue
, NewQueue
);
501 (void)InterlockedExchangePointer((PVOID
*)&Old
->Desktop
, 0);
502 IntDereferenceMessageQueue(Old
);
503 gpqForegroundPrev
= Old
;
505 // Only one Q can have active foreground even when there are more than one desktop.
506 if (NewQueue
) gpqForeground
= pdo
->ActiveMessageQueue
;
507 else gpqForeground
= NULL
;
511 IntGetThreadDesktopWindow(PTHREADINFO pti
)
513 if (!pti
) pti
= PsGetCurrentThreadWin32Thread();
514 if (pti
->pDeskInfo
) return pti
->pDeskInfo
->spwnd
;
518 PWND FASTCALL
co_GetDesktopWindow(PWND pWnd
)
520 if (pWnd
->head
.rpdesk
&&
521 pWnd
->head
.rpdesk
->pDeskInfo
)
522 return pWnd
->head
.rpdesk
->pDeskInfo
->spwnd
;
526 HWND FASTCALL
IntGetDesktopWindow(VOID
)
528 PDESKTOP pdo
= IntGetActiveDesktop();
531 TRACE("No active desktop\n");
534 return pdo
->DesktopWindow
;
537 PWND FASTCALL
UserGetDesktopWindow(VOID
)
539 PDESKTOP pdo
= IntGetActiveDesktop();
543 TRACE("No active desktop\n");
546 // return pdo->pDeskInfo->spwnd;
547 return UserGetWindowObject(pdo
->DesktopWindow
);
550 HWND FASTCALL
IntGetMessageWindow(VOID
)
552 PDESKTOP pdo
= IntGetActiveDesktop();
556 TRACE("No active desktop\n");
559 return pdo
->spwndMessage
->head
.h
;
562 PWND FASTCALL
UserGetMessageWindow(VOID
)
564 PDESKTOP pdo
= IntGetActiveDesktop();
568 TRACE("No active desktop\n");
571 return pdo
->spwndMessage
;
574 HWND FASTCALL
IntGetCurrentThreadDesktopWindow(VOID
)
576 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
577 PDESKTOP pdo
= pti
->rpdesk
;
580 ERR("Thread doesn't have a desktop\n");
583 return pdo
->DesktopWindow
;
586 /* PUBLIC FUNCTIONS ***********************************************************/
589 DesktopWindowProc(PWND Wnd
, UINT Msg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*lResult
)
593 //ERR("DesktopWindowProc\n");
602 Wnd
->fnid
= FNID_DESKTOP
;
604 *lResult
= (LRESULT
)TRUE
;
608 Value
= HandleToULong(PsGetCurrentProcessId());
610 co_UserSetWindowLong(UserHMGetHandle(Wnd
), DT_GWL_PROCESSID
, Value
, FALSE
);
611 Value
= HandleToULong(PsGetCurrentThreadId());
613 co_UserSetWindowLong(UserHMGetHandle(Wnd
), DT_GWL_THREADID
, Value
, FALSE
);
617 case WM_DISPLAYCHANGE
:
618 co_WinPosSetWindowPos(Wnd
, 0, 0, 0, LOWORD(lParam
), HIWORD(lParam
), SWP_NOZORDER
| SWP_NOACTIVATE
);
622 IntPaintDesktop((HDC
)wParam
);
628 if (IntBeginPaint(Wnd
, &Ps
))
630 IntEndPaint(Wnd
, &Ps
);
634 case WM_SYSCOLORCHANGE
:
635 co_UserRedrawWindow(Wnd
, NULL
, NULL
, RDW_INVALIDATE
|RDW_ERASE
|RDW_ALLCHILDREN
);
639 PCURICON_OBJECT pcurOld
, pcurNew
;
640 pcurNew
= UserGetCurIconObject(gDesktopCursor
);
645 pcurOld
= UserSetCursor(pcurNew
, FALSE
);
648 UserDereferenceObject(pcurOld
);
652 return TRUE
; /* We are done. Do not do any callbacks to user mode */
656 UserMessageWindowProc(PWND pwnd
, UINT Msg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*lResult
)
663 pwnd
->fnid
|= FNID_MESSAGEWND
;
664 *lResult
= (LRESULT
)TRUE
;
667 pwnd
->fnid
|= FNID_DESTROY
;
671 return TRUE
; /* We are done. Do not do any callbacks to user mode */
674 VOID NTAPI
DesktopThreadMain()
679 gptiDesktopThread
= PsGetCurrentThreadWin32Thread();
681 UserEnterExclusive();
683 /* Register system classes. This thread does not belong to any desktop so the
684 classes will be allocated from the shared heap */
685 UserRegisterSystemClasses();
689 Ret
= co_IntGetPeekMessage(&Msg
, 0, 0, 0, PM_REMOVE
, TRUE
);
692 IntDispatchMessage(&Msg
);
700 UserGetDesktopDC(ULONG DcType
, BOOL EmptyDC
, BOOL ValidatehWnd
)
702 PWND DesktopObject
= 0;
705 if (DcType
== DC_TYPE_DIRECT
)
707 DesktopObject
= UserGetDesktopWindow();
708 DesktopHDC
= (HDC
)UserGetWindowDC(DesktopObject
);
712 PMONITOR pMonitor
= UserGetPrimaryMonitor();
713 DesktopHDC
= IntGdiCreateDisplayDC(pMonitor
->hDev
, DcType
, EmptyDC
);
725 Window
= UserGetDesktopWindow();
726 hRgn
= IntSysCreateRectRgnIndirect(&Window
->rcWindow
);
728 IntInvalidateWindows( Window
,
735 GreDeleteObject(hRgn
);
740 co_IntShowDesktop(PDESKTOP Desktop
, ULONG Width
, ULONG Height
)
744 Window
= IntGetWindowObject(Desktop
->DesktopWindow
);
748 ERR("No Desktop window.\n");
749 return STATUS_UNSUCCESSFUL
;
751 co_WinPosSetWindowPos(Window
, NULL
, 0, 0, Width
, Height
, SWP_NOACTIVATE
|SWP_NOZORDER
|SWP_SHOWWINDOW
);
753 co_UserRedrawWindow( Window
, NULL
, 0, RDW_UPDATENOW
| RDW_ALLCHILDREN
);
754 return STATUS_SUCCESS
;
758 IntHideDesktop(PDESKTOP Desktop
)
762 DesktopWnd
= IntGetWindowObject(Desktop
->DesktopWindow
);
765 return ERROR_INVALID_WINDOW_HANDLE
;
767 DesktopWnd
->style
&= ~WS_VISIBLE
;
769 return STATUS_SUCCESS
;
774 UserBuildShellHookHwndList(PDESKTOP Desktop
)
777 PSHELL_HOOK_WINDOW Current
;
780 /* FIXME: If we save nb elements in desktop, we dont have to loop to find nb entries */
781 LIST_FOR_EACH(Current
, &Desktop
->ShellHookWindows
, SHELL_HOOK_WINDOW
, ListEntry
)
784 if (!entries
) return NULL
;
786 list
= ExAllocatePoolWithTag(PagedPool
, sizeof(HWND
) * (entries
+ 1), USERTAG_WINDOWLIST
); /* alloc one extra for nullterm */
791 LIST_FOR_EACH(Current
, &Desktop
->ShellHookWindows
, SHELL_HOOK_WINDOW
, ListEntry
)
792 *cursor
++ = Current
->hWnd
;
794 *cursor
= NULL
; /* Nullterm list */
801 * Send the Message to the windows registered for ShellHook
802 * notifications. The lParam contents depend on the Message. See
803 * MSDN for more details (RegisterShellHookWindow)
805 VOID
co_IntShellHookNotify(WPARAM Message
, WPARAM wParam
, LPARAM lParam
)
807 PDESKTOP Desktop
= IntGetActiveDesktop();
810 if (!gpsi
->uiShellMsg
)
812 gpsi
->uiShellMsg
= IntAddAtom(L
"SHELLHOOK");
814 TRACE("MsgType = %x\n", gpsi
->uiShellMsg
);
815 if (!gpsi
->uiShellMsg
)
816 ERR("LastError: %x\n", EngGetLastError());
821 TRACE("IntShellHookNotify: No desktop!\n");
825 // FIXME: System Tray Support.
827 HwndList
= UserBuildShellHookHwndList(Desktop
);
830 HWND
* cursor
= HwndList
;
832 for (; *cursor
; cursor
++)
834 TRACE("Sending notify\n");
835 co_IntPostOrSendMessage(*cursor
,
838 (Message
== HSHELL_LANGUAGE
? lParam
: (LPARAM
)wParam
) );
841 ExFreePoolWithTag(HwndList
, USERTAG_WINDOWLIST
);
844 if (ISITHOOKED(WH_SHELL
))
846 co_HOOK_CallHooks(WH_SHELL
, Message
, wParam
, lParam
);
851 * Add the window to the ShellHookWindows list. The windows
852 * on that list get notifications that are important to shell
855 * TODO: Validate the window? I'm not sure if sending these messages to
856 * an unsuspecting application that is not your own is a nice thing to do.
858 BOOL
IntRegisterShellHookWindow(HWND hWnd
)
860 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
861 PDESKTOP Desktop
= pti
->rpdesk
;
862 PSHELL_HOOK_WINDOW Entry
;
864 TRACE("IntRegisterShellHookWindow\n");
866 /* First deregister the window, so we can be sure it's never twice in the
869 IntDeRegisterShellHookWindow(hWnd
);
871 Entry
= ExAllocatePoolWithTag(PagedPool
,
872 sizeof(SHELL_HOOK_WINDOW
),
880 InsertTailList(&Desktop
->ShellHookWindows
, &Entry
->ListEntry
);
886 * Remove the window from the ShellHookWindows list. The windows
887 * on that list get notifications that are important to shell
890 BOOL
IntDeRegisterShellHookWindow(HWND hWnd
)
892 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
893 PDESKTOP Desktop
= pti
->rpdesk
;
894 PSHELL_HOOK_WINDOW Current
;
896 LIST_FOR_EACH(Current
, &Desktop
->ShellHookWindows
, SHELL_HOOK_WINDOW
, ListEntry
)
898 if (Current
->hWnd
== hWnd
)
900 RemoveEntryList(&Current
->ListEntry
);
901 ExFreePoolWithTag(Current
, TAG_WINSTA
);
910 IntFreeDesktopHeap(IN OUT PDESKTOP Desktop
)
912 /* FIXME: Disable until unmapping works in mm */
914 if (Desktop
->pheapDesktop
!= NULL
)
916 MmUnmapViewInSessionSpace(Desktop
->pheapDesktop
);
917 Desktop
->pheapDesktop
= NULL
;
920 if (Desktop
->hsectionDesktop
!= NULL
)
922 ObDereferenceObject(Desktop
->hsectionDesktop
);
923 Desktop
->hsectionDesktop
= NULL
;
929 IntPaintDesktop(HDC hDC
)
932 HBRUSH DesktopBrush
, PreviousBrush
;
934 BOOL doPatBlt
= TRUE
;
936 static WCHAR s_wszSafeMode
[] = L
"Safe Mode";
942 GdiGetClipBox(hDC
, &Rect
);
944 hWndDesktop
= IntGetDesktopWindow(); // rpdesk->DesktopWindow;
946 WndDesktop
= UserGetWindowObject(hWndDesktop
); // rpdesk->pDeskInfo->spwnd;
952 if (!UserGetSystemMetrics(SM_CLEANBOOT
))
954 DesktopBrush
= (HBRUSH
)WndDesktop
->pcls
->hbrBackground
;
957 * Paint desktop background
959 if (gspv
.hbmWallpaper
!= NULL
)
965 sz
.cx
= WndDesktop
->rcWindow
.right
- WndDesktop
->rcWindow
.left
;
966 sz
.cy
= WndDesktop
->rcWindow
.bottom
- WndDesktop
->rcWindow
.top
;
968 if (gspv
.WallpaperMode
== wmStretch
||
969 gspv
.WallpaperMode
== wmTile
)
976 /* Find the upper left corner, can be negtive if the bitmap is bigger then the screen */
977 x
= (sz
.cx
/ 2) - (gspv
.cxWallpaper
/ 2);
978 y
= (sz
.cy
/ 2) - (gspv
.cyWallpaper
/ 2);
981 hWallpaperDC
= NtGdiCreateCompatibleDC(hDC
);
982 if(hWallpaperDC
!= NULL
)
986 /* Fill in the area that the bitmap is not going to cover */
989 /* FIXME: Clip out the bitmap
990 can be replaced with "NtGdiPatBlt(hDC, x, y, WinSta->cxWallpaper, WinSta->cyWallpaper, PATCOPY | DSTINVERT);"
991 once we support DSTINVERT */
992 PreviousBrush
= NtGdiSelectBrush(hDC
, DesktopBrush
);
993 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
994 NtGdiSelectBrush(hDC
, PreviousBrush
);
997 /*Do not fill the background after it is painted no matter the size of the picture */
1000 hOldBitmap
= NtGdiSelectBitmap(hWallpaperDC
, gspv
.hbmWallpaper
);
1002 if (gspv
.WallpaperMode
== wmStretch
)
1004 if(Rect
.right
&& Rect
.bottom
)
1005 NtGdiStretchBlt(hDC
,
1019 else if (gspv
.WallpaperMode
== wmTile
)
1021 /* Paint the bitmap across the screen then down */
1022 for(y
= 0; y
< Rect
.bottom
; y
+= gspv
.cyWallpaper
)
1024 for(x
= 0; x
< Rect
.right
; x
+= gspv
.cxWallpaper
)
1054 NtGdiSelectBitmap(hWallpaperDC
, hOldBitmap
);
1055 NtGdiDeleteObjectApp(hWallpaperDC
);
1061 /* Black desktop background in Safe Mode */
1062 DesktopBrush
= StockObjects
[BLACK_BRUSH
];
1064 /* Back ground is set to none, clear the screen */
1067 PreviousBrush
= NtGdiSelectBrush(hDC
, DesktopBrush
);
1068 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
1069 NtGdiSelectBrush(hDC
, PreviousBrush
);
1073 * Display system version on the desktop background
1076 if (g_PaintDesktopVersion
||UserGetSystemMetrics(SM_CLEANBOOT
))
1078 static WCHAR s_wszVersion
[256] = {0};
1083 len
= wcslen(s_wszVersion
);
1087 len
= GetSystemVersionString(s_wszVersion
);
1092 if (!UserSystemParametersInfo(SPI_GETWORKAREA
, 0, &rect
, 0))
1094 rect
.right
= UserGetSystemMetrics(SM_CXSCREEN
);
1095 rect
.bottom
= UserGetSystemMetrics(SM_CYSCREEN
);
1098 color_old
= IntGdiSetTextColor(hDC
, RGB(255,255,255));
1099 align_old
= IntGdiSetTextAlign(hDC
, TA_RIGHT
);
1100 mode_old
= IntGdiSetBkMode(hDC
, TRANSPARENT
);
1102 if(!UserGetSystemMetrics(SM_CLEANBOOT
))
1104 GreExtTextOutW(hDC
, rect
.right
-16, rect
.bottom
-48, 0, NULL
, s_wszVersion
, len
, NULL
, 0);
1109 /* Version information text in top center */
1110 IntGdiSetTextAlign(hDC
, TA_CENTER
|TA_TOP
);
1111 GreExtTextOutW(hDC
, (rect
.right
+rect
.left
)/2, rect
.top
, 0, NULL
, s_wszVersion
, len
, NULL
, 0);
1112 /* Safe Mode text in corners */
1113 len
= wcslen(s_wszSafeMode
);
1114 IntGdiSetTextAlign(hDC
, TA_RIGHT
|TA_TOP
);
1115 GreExtTextOutW(hDC
, rect
.right
, rect
.top
, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1116 IntGdiSetTextAlign(hDC
, TA_RIGHT
|TA_BASELINE
);
1117 GreExtTextOutW(hDC
, rect
.right
, rect
.bottom
, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1118 IntGdiSetTextAlign(hDC
, TA_LEFT
|TA_TOP
);
1119 GreExtTextOutW(hDC
, rect
.left
, rect
.top
, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1120 IntGdiSetTextAlign(hDC
, TA_LEFT
|TA_BASELINE
);
1121 GreExtTextOutW(hDC
, rect
.left
, rect
.bottom
, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1124 IntGdiSetBkMode(hDC
, mode_old
);
1125 IntGdiSetTextAlign(hDC
, align_old
);
1126 IntGdiSetTextColor(hDC
, color_old
);
1133 UserInitializeDesktop(PDESKTOP pdesk
, PUNICODE_STRING DesktopName
, PWINSTATION_OBJECT pwinsta
)
1135 PVOID DesktopHeapSystemBase
= NULL
;
1136 ULONG_PTR HeapSize
= 400 * 1024;
1137 SIZE_T DesktopInfoSize
;
1140 TRACE("UserInitializeDesktop desktop 0x%p with name %wZ\n", pdesk
, DesktopName
);
1142 RtlZeroMemory(pdesk
, sizeof(DESKTOP
));
1144 /* Link the desktop with the parent window station */
1145 pdesk
->rpwinstaParent
= pwinsta
;
1146 InsertTailList(&pwinsta
->DesktopListHead
, &pdesk
->ListEntry
);
1148 /* Create the desktop heap */
1149 pdesk
->hsectionDesktop
= NULL
;
1150 pdesk
->pheapDesktop
= UserCreateHeap(&pdesk
->hsectionDesktop
,
1151 &DesktopHeapSystemBase
,
1153 if (pdesk
->pheapDesktop
== NULL
)
1155 ERR("Failed to create desktop heap!\n");
1156 return STATUS_NO_MEMORY
;
1159 /* Create DESKTOPINFO */
1160 DesktopInfoSize
= sizeof(DESKTOPINFO
) + DesktopName
->Length
+ sizeof(WCHAR
);
1161 pdesk
->pDeskInfo
= RtlAllocateHeap(pdesk
->pheapDesktop
,
1162 HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
,
1164 if (pdesk
->pDeskInfo
== NULL
)
1166 ERR("Failed to create the DESKTOP structure!\n");
1167 return STATUS_NO_MEMORY
;
1170 /* Initialize the DESKTOPINFO */
1171 pdesk
->pDeskInfo
->pvDesktopBase
= DesktopHeapSystemBase
;
1172 pdesk
->pDeskInfo
->pvDesktopLimit
= (PVOID
)((ULONG_PTR
)DesktopHeapSystemBase
+ HeapSize
);
1173 RtlCopyMemory(pdesk
->pDeskInfo
->szDesktopName
,
1174 DesktopName
->Buffer
,
1175 DesktopName
->Length
+ sizeof(WCHAR
));
1176 for (i
= 0; i
< NB_HOOKS
; i
++)
1178 InitializeListHead(&pdesk
->pDeskInfo
->aphkStart
[i
]);
1181 InitializeListHead(&pdesk
->ShellHookWindows
);
1182 InitializeListHead(&pdesk
->PtiList
);
1184 return STATUS_SUCCESS
;
1187 /* SYSCALLS *******************************************************************/
1190 * NtUserCreateDesktop
1192 * Creates a new desktop.
1196 * Object Attributes.
1199 * Name of the device.
1205 * Interaction flags.
1208 * Requested type of access.
1212 * If the function succeeds, the return value is a handle to the newly
1213 * created desktop. If the specified desktop already exists, the function
1214 * succeeds and returns a handle to the existing desktop. When you are
1215 * finished using the handle, call the CloseDesktop function to close it.
1216 * If the function fails, the return value is NULL.
1223 NtUserCreateDesktop(
1224 POBJECT_ATTRIBUTES ObjectAttributes
,
1225 PUNICODE_STRING lpszDesktopDevice
,
1228 ACCESS_MASK dwDesiredAccess
)
1230 PDESKTOP pdesk
= NULL
;
1231 NTSTATUS Status
= STATUS_SUCCESS
;
1234 UNICODE_STRING ClassName
;
1235 LARGE_STRING WindowName
;
1236 BOOL NoHooks
= FALSE
;
1239 PTHREADINFO ptiCurrent
;
1242 DECLARE_RETURN(HDESK
);
1244 TRACE("Enter NtUserCreateDesktop\n");
1245 UserEnterExclusive();
1247 ptiCurrent
= PsGetCurrentThreadWin32Thread();
1249 ASSERT(gptiDesktopThread
);
1251 /* Turn off hooks when calling any CreateWindowEx from inside win32k. */
1252 NoHooks
= (ptiCurrent
->TIF_flags
& TIF_DISABLEHOOKS
);
1253 ptiCurrent
->TIF_flags
|= TIF_DISABLEHOOKS
;
1254 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
1257 * Try to open already existing desktop
1259 Status
= ObOpenObjectByName(
1261 ExDesktopObjectType
,
1267 if (!NT_SUCCESS(Status
))
1269 ERR("ObOpenObjectByName failed to open/create desktop\n");
1270 SetLastNtError(Status
);
1274 /* In case the object was not created (eg if it existed), return now */
1275 if (Context
== FALSE
)
1277 TRACE("NtUserCreateDesktop opened desktop %wZ\n", ObjectAttributes
->ObjectName
);
1281 /* Reference the desktop */
1282 Status
= ObReferenceObjectByHandle(hdesk
,
1284 ExDesktopObjectType
,
1288 if (!NT_SUCCESS(Status
))
1290 ERR("Failed to reference desktop object\n");
1291 SetLastNtError(Status
);
1295 if (!ptiCurrent
->rpdesk
) IntSetThreadDesktop(hdesk
,FALSE
);
1297 /* Get the desktop window class. The thread desktop does not belong to any desktop
1298 * so the classes created there (including the desktop class) are allocated in the shared heap
1299 * It would cause problems if we used a class that belongs to the caller
1301 ClassName
.Buffer
= WC_DESKTOP
;
1302 ClassName
.Length
= 0;
1303 pcls
= IntGetAndReferenceClass(&ClassName
, 0, TRUE
);
1310 RtlZeroMemory(&WindowName
, sizeof(WindowName
));
1311 RtlZeroMemory(&Cs
, sizeof(Cs
));
1312 Cs
.x
= UserGetSystemMetrics(SM_XVIRTUALSCREEN
),
1313 Cs
.y
= UserGetSystemMetrics(SM_YVIRTUALSCREEN
),
1314 Cs
.cx
= UserGetSystemMetrics(SM_CXVIRTUALSCREEN
),
1315 Cs
.cy
= UserGetSystemMetrics(SM_CYVIRTUALSCREEN
),
1316 Cs
.style
= WS_POPUP
|WS_CLIPCHILDREN
;
1317 Cs
.hInstance
= hModClient
; // hModuleWin; // Server side winproc!
1318 Cs
.lpszName
= (LPCWSTR
) &WindowName
;
1319 Cs
.lpszClass
= (LPCWSTR
) &ClassName
;
1321 /* Use IntCreateWindow instead of co_UserCreateWindowEx cause the later expects a thread with a desktop */
1322 pWnd
= IntCreateWindow(&Cs
, &WindowName
, pcls
, NULL
, NULL
, NULL
, pdesk
);
1325 ERR("Failed to create desktop window for the new desktop\n");
1329 pdesk
->DesktopWindow
= pWnd
->head
.h
;
1330 pdesk
->pDeskInfo
->spwnd
= pWnd
;
1331 pWnd
->fnid
= FNID_DESKTOP
;
1333 ClassName
.Buffer
= MAKEINTATOM(gpsi
->atomSysClass
[ICLS_HWNDMESSAGE
]);
1334 ClassName
.Length
= 0;
1335 pcls
= IntGetAndReferenceClass(&ClassName
, 0, TRUE
);
1342 RtlZeroMemory(&WindowName
, sizeof(WindowName
));
1343 RtlZeroMemory(&Cs
, sizeof(Cs
));
1344 Cs
.cx
= Cs
.cy
= 100;
1345 Cs
.style
= WS_POPUP
|WS_CLIPCHILDREN
;
1346 Cs
.hInstance
= hModClient
; // hModuleWin; // Server side winproc!
1347 Cs
.lpszName
= (LPCWSTR
) &WindowName
;
1348 Cs
.lpszClass
= (LPCWSTR
) &ClassName
;
1349 pWnd
= IntCreateWindow(&Cs
, &WindowName
, pcls
, NULL
, NULL
, NULL
, pdesk
);
1352 ERR("Failed to create message window for the new desktop\n");
1356 pdesk
->spwndMessage
= pWnd
;
1357 pWnd
->fnid
= FNID_MESSAGEWND
;
1360 if !(WinStaObject->Flags & WSF_NOIO) is (not set) for desktop input output mode (see wiki)
1361 Create Tooltip. Saved in DesktopObject->spwndTooltip.
1362 Tooltip dwExStyle: WS_EX_TOOLWINDOW|WS_EX_TOPMOST
1363 hWndParent are spwndMessage. Use hModuleWin for server side winproc!
1364 The rest is same as message window.
1365 http://msdn.microsoft.com/en-us/library/bb760250(VS.85).aspx
1372 ObDereferenceObject(pdesk
);
1374 if (_ret_
== NULL
&& hdesk
!= NULL
)
1376 ObCloseHandle(hdesk
, UserMode
);
1380 ptiCurrent
->TIF_flags
&= ~TIF_DISABLEHOOKS
;
1381 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
1383 TRACE("Leave NtUserCreateDesktop, ret=%p\n",_ret_
);
1391 * Opens an existing desktop.
1395 * Name of the existing desktop.
1398 * Interaction flags.
1401 * Requested type of access.
1404 * Handle to the desktop or zero on failure.
1412 POBJECT_ATTRIBUTES ObjectAttributes
,
1414 ACCESS_MASK dwDesiredAccess
)
1419 Status
= ObOpenObjectByName(
1421 ExDesktopObjectType
,
1428 if (!NT_SUCCESS(Status
))
1430 ERR("Failed to open desktop\n");
1431 SetLastNtError(Status
);
1435 TRACE("Opened desktop %S with handle 0x%p\n", ObjectAttributes
->ObjectName
->Buffer
, Desktop
);
1441 * NtUserOpenInputDesktop
1443 * Opens the input (interactive) desktop.
1447 * Interaction flags.
1450 * Inheritance option.
1453 * Requested type of access.
1456 * Handle to the input desktop or zero on failure.
1463 NtUserOpenInputDesktop(
1466 ACCESS_MASK dwDesiredAccess
)
1470 ULONG HandleAttributes
= 0;
1472 UserEnterExclusive();
1473 TRACE("Enter NtUserOpenInputDesktop gpdeskInputDesktop 0x%p\n",gpdeskInputDesktop
);
1475 if(fInherit
) HandleAttributes
= OBJ_INHERIT
;
1477 /* Create a new handle to the object */
1478 Status
= ObOpenObjectByPointer(
1483 ExDesktopObjectType
,
1487 if (!NT_SUCCESS(Status
))
1489 ERR("Failed to open input desktop object\n");
1490 SetLastNtError(Status
);
1493 TRACE("NtUserOpenInputDesktop returning 0x%p\n",hdesk
);
1499 * NtUserCloseDesktop
1501 * Closes a desktop handle.
1505 * Handle to the desktop.
1511 * The desktop handle can be created with NtUserCreateDesktop or
1512 * NtUserOpenDesktop. This function will fail if any thread in the calling
1513 * process is using the specified desktop handle or if the handle refers
1514 * to the initial desktop of the calling process.
1521 NtUserCloseDesktop(HDESK hDesktop
)
1525 DECLARE_RETURN(BOOL
);
1527 TRACE("NtUserCloseDesktop called (0x%p)\n", hDesktop
);
1528 UserEnterExclusive();
1530 if( hDesktop
== gptiCurrent
->hdesk
|| hDesktop
== gptiCurrent
->ppi
->hdeskStartup
)
1532 ERR("Attempted to close thread desktop\n");
1533 EngSetLastError(ERROR_BUSY
);
1537 Status
= IntValidateDesktopHandle( hDesktop
, UserMode
, 0, &pdesk
);
1538 if (!NT_SUCCESS(Status
))
1540 ERR("Validation of desktop handle (0x%p) failed\n", hDesktop
);
1544 ObDereferenceObject(pdesk
);
1546 Status
= ZwClose(hDesktop
);
1547 if (!NT_SUCCESS(Status
))
1549 ERR("Failed to close desktop handle 0x%p\n", hDesktop
);
1550 SetLastNtError(Status
);
1557 TRACE("Leave NtUserCloseDesktop, ret=%i\n",_ret_
);
1563 * NtUserPaintDesktop
1565 * The NtUserPaintDesktop function fills the clipping region in the
1566 * specified device context with the desktop pattern or wallpaper. The
1567 * function is provided primarily for shell desktops.
1571 * Handle to the device context.
1578 NtUserPaintDesktop(HDC hDC
)
1581 UserEnterExclusive();
1582 TRACE("Enter NtUserPaintDesktop\n");
1583 Ret
= IntPaintDesktop(hDC
);
1584 TRACE("Leave NtUserPaintDesktop, ret=%i\n",Ret
);
1590 * NtUserSwitchDesktop
1592 * Sets the current input (interactive) desktop.
1596 * Handle to desktop.
1606 NtUserSwitchDesktop(HDESK hdesk
)
1610 DECLARE_RETURN(BOOL
);
1612 UserEnterExclusive();
1613 TRACE("Enter NtUserSwitchDesktop(0x%p)\n", hdesk
);
1615 Status
= IntValidateDesktopHandle( hdesk
, UserMode
, 0, &pdesk
);
1616 if (!NT_SUCCESS(Status
))
1618 ERR("Validation of desktop handle (0x%p) failed\n", hdesk
);
1623 * Don't allow applications switch the desktop if it's locked, unless the caller
1624 * is the logon application itself
1626 if((pdesk
->rpwinstaParent
->Flags
& WSS_LOCKED
) &&
1627 LogonProcess
!= PsGetCurrentProcessWin32Process())
1629 ObDereferenceObject(pdesk
);
1630 ERR("Switching desktop 0x%p denied because the window station is locked!\n", hdesk
);
1634 if(pdesk
->rpwinstaParent
!= InputWindowStation
)
1636 ObDereferenceObject(pdesk
);
1637 ERR("Switching desktop 0x%p denied because desktop doesn't belong to the interactive winsta!\n", hdesk
);
1641 /* FIXME: Fail if the process is associated with a secured
1642 desktop such as Winlogon or Screen-Saver */
1643 /* FIXME: Connect to input device */
1645 /* Set the active desktop in the desktop's window station. */
1646 InputWindowStation
->ActiveDesktop
= pdesk
;
1648 /* Set the global state. */
1649 gpdeskInputDesktop
= pdesk
;
1650 TRACE("SwitchDesktop gpdeskInputDesktop 0x%p\n",gpdeskInputDesktop
);
1651 ObDereferenceObject(pdesk
);
1656 TRACE("Leave NtUserSwitchDesktop, ret=%i\n",_ret_
);
1662 * NtUserGetThreadDesktop
1669 NtUserGetThreadDesktop(DWORD dwThreadId
, DWORD Unknown1
)
1673 PDESKTOP DesktopObject
;
1674 HDESK Ret
, hThreadDesktop
;
1675 OBJECT_HANDLE_INFORMATION HandleInformation
;
1676 DECLARE_RETURN(HDESK
);
1678 UserEnterExclusive();
1679 TRACE("Enter NtUserGetThreadDesktop\n");
1683 EngSetLastError(ERROR_INVALID_PARAMETER
);
1687 Status
= PsLookupThreadByThreadId((HANDLE
)(DWORD_PTR
)dwThreadId
, &Thread
);
1688 if(!NT_SUCCESS(Status
))
1690 EngSetLastError(ERROR_INVALID_PARAMETER
);
1694 if(Thread
->ThreadsProcess
== PsGetCurrentProcess())
1696 /* Just return the handle, we queried the desktop handle of a thread running
1697 in the same context */
1698 Ret
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->hdesk
;
1699 ObDereferenceObject(Thread
);
1703 /* Get the desktop handle and the desktop of the thread */
1704 if(!(hThreadDesktop
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->hdesk
) ||
1705 !(DesktopObject
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->rpdesk
))
1707 ObDereferenceObject(Thread
);
1708 ERR("Desktop information of thread 0x%x broken!?\n", dwThreadId
);
1712 /* We could just use DesktopObject instead of looking up the handle, but latter
1713 may be a bit safer (e.g. when the desktop is being destroyed */
1714 /* Switch into the context of the thread we're trying to get the desktop from,
1715 so we can use the handle */
1716 KeAttachProcess(&Thread
->ThreadsProcess
->Pcb
);
1717 Status
= ObReferenceObjectByHandle(hThreadDesktop
,
1719 ExDesktopObjectType
,
1721 (PVOID
*)&DesktopObject
,
1722 &HandleInformation
);
1725 /* The handle couldn't be found, there's nothing to get... */
1726 if(!NT_SUCCESS(Status
))
1728 ObDereferenceObject(Thread
);
1732 /* Lookup our handle table if we can find a handle to the desktop object,
1733 if not, create one */
1734 Ret
= IntGetDesktopObjectHandle(DesktopObject
);
1736 /* All done, we got a valid handle to the desktop */
1737 ObDereferenceObject(DesktopObject
);
1738 ObDereferenceObject(Thread
);
1742 TRACE("Leave NtUserGetThreadDesktop, ret=%p\n",_ret_
);
1748 IntUnmapDesktopView(IN PDESKTOP pdesk
)
1751 PW32HEAP_USER_MAPPING HeapMapping
, *PrevLink
;
1752 NTSTATUS Status
= STATUS_SUCCESS
;
1754 TRACE("IntUnmapDesktopView called for desktop object %p\n", pdesk
);
1756 ppi
= PsGetCurrentProcessWin32Process();
1757 PrevLink
= &ppi
->HeapMappings
.Next
;
1759 /* Unmap if we're the last thread using the desktop */
1760 HeapMapping
= *PrevLink
;
1761 while (HeapMapping
!= NULL
)
1763 if (HeapMapping
->KernelMapping
== (PVOID
)pdesk
->pheapDesktop
)
1765 if (--HeapMapping
->Count
== 0)
1767 *PrevLink
= HeapMapping
->Next
;
1769 TRACE("ppi 0x%p unmapped heap of desktop 0x%p\n", ppi
, pdesk
);
1770 Status
= MmUnmapViewOfSection(PsGetCurrentProcess(),
1771 HeapMapping
->UserMapping
);
1773 ObDereferenceObject(pdesk
);
1775 UserHeapFree(HeapMapping
);
1780 PrevLink
= &HeapMapping
->Next
;
1781 HeapMapping
= HeapMapping
->Next
;
1788 IntMapDesktopView(IN PDESKTOP pdesk
)
1791 PW32HEAP_USER_MAPPING HeapMapping
, *PrevLink
;
1792 PVOID UserBase
= NULL
;
1793 SIZE_T ViewSize
= 0;
1794 LARGE_INTEGER Offset
;
1797 TRACE("IntMapDesktopView called for desktop object 0x%p\n", pdesk
);
1799 ppi
= PsGetCurrentProcessWin32Process();
1800 PrevLink
= &ppi
->HeapMappings
.Next
;
1802 /* Find out if another thread already mapped the desktop heap */
1803 HeapMapping
= *PrevLink
;
1804 while (HeapMapping
!= NULL
)
1806 if (HeapMapping
->KernelMapping
== (PVOID
)pdesk
->pheapDesktop
)
1808 HeapMapping
->Count
++;
1809 return STATUS_SUCCESS
;
1812 PrevLink
= &HeapMapping
->Next
;
1813 HeapMapping
= HeapMapping
->Next
;
1816 /* We're the first, map the heap */
1817 Offset
.QuadPart
= 0;
1818 Status
= MmMapViewOfSection(pdesk
->hsectionDesktop
,
1819 PsGetCurrentProcess(),
1827 PAGE_EXECUTE_READ
); /* Would prefer PAGE_READONLY, but thanks to RTL heaps... */
1828 if (!NT_SUCCESS(Status
))
1830 ERR("Failed to map desktop\n");
1834 TRACE("ppi 0x%p mapped heap of desktop 0x%p\n", ppi
, pdesk
);
1836 /* Add the mapping */
1837 HeapMapping
= UserHeapAlloc(sizeof(W32HEAP_USER_MAPPING
));
1838 if (HeapMapping
== NULL
)
1840 MmUnmapViewOfSection(PsGetCurrentProcess(), UserBase
);
1841 ERR("UserHeapAlloc() failed!\n");
1842 return STATUS_NO_MEMORY
;
1845 HeapMapping
->Next
= NULL
;
1846 HeapMapping
->KernelMapping
= (PVOID
)pdesk
->pheapDesktop
;
1847 HeapMapping
->UserMapping
= UserBase
;
1848 HeapMapping
->Limit
= ViewSize
;
1849 HeapMapping
->Count
= 1;
1850 *PrevLink
= HeapMapping
;
1852 ObReferenceObject(pdesk
);
1854 return STATUS_SUCCESS
;
1858 IntSetThreadDesktop(IN HDESK hDesktop
,
1859 IN BOOL FreeOnFailure
)
1861 PDESKTOP pdesk
= NULL
, pdeskOld
;
1865 PCLIENTTHREADINFO pctiOld
, pctiNew
= NULL
;
1868 ASSERT(NtCurrentTeb());
1870 TRACE("IntSetThreadDesktop hDesktop:0x%p, FOF:%i\n",hDesktop
, FreeOnFailure
);
1872 pti
= PsGetCurrentThreadWin32Thread();
1873 pci
= pti
->pClientInfo
;
1875 /* If the caller gave us a desktop, ensure it is valid */
1876 if(hDesktop
!= NULL
)
1878 /* Validate the new desktop. */
1879 Status
= IntValidateDesktopHandle( hDesktop
, UserMode
, 0, &pdesk
);
1880 if (!NT_SUCCESS(Status
))
1882 ERR("Validation of desktop handle (0x%p) failed\n", hDesktop
);
1886 if (pti
->rpdesk
== pdesk
)
1889 ObDereferenceObject(pdesk
);
1894 /* Make sure that we don't own any window in the current desktop */
1895 if (!IsListEmpty(&pti
->WindowListHead
))
1898 ObDereferenceObject(pdesk
);
1899 ERR("Attempted to change thread desktop although the thread has windows!\n");
1900 EngSetLastError(ERROR_BUSY
);
1904 /* Desktop is being re-set so clear out foreground. */
1905 if (pti
->rpdesk
!= pdesk
&& pti
->MessageQueue
== gpqForeground
)
1907 // Like above, there shouldn't be any windows, hooks or anything active on this threads desktop!
1908 IntSetFocusMessageQueue(NULL
);
1911 /* Before doing the switch, map the new desktop heap and allocate the new pcti */
1914 Status
= IntMapDesktopView(pdesk
);
1915 if (!NT_SUCCESS(Status
))
1917 ERR("Failed to map desktop heap!\n");
1918 ObDereferenceObject(pdesk
);
1919 SetLastNtError(Status
);
1923 pctiNew
= DesktopHeapAlloc( pdesk
, sizeof(CLIENTTHREADINFO
));
1926 ERR("Failed to allocate new pcti\n");
1927 IntUnmapDesktopView(pdesk
);
1928 ObDereferenceObject(pdesk
);
1929 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1934 /* free all classes or move them to the shared heap */
1935 if(pti
->rpdesk
!= NULL
)
1937 if(!IntCheckProcessDesktopClasses(pti
->rpdesk
, FreeOnFailure
))
1939 ERR("Failed to move process classes to shared heap!\n");
1942 DesktopHeapFree(pdesk
, pctiNew
);
1943 IntUnmapDesktopView(pdesk
);
1944 ObDereferenceObject(pdesk
);
1950 pdeskOld
= pti
->rpdesk
;
1951 hdeskOld
= pti
->hdesk
;
1952 if (pti
->pcti
!= &pti
->cti
)
1953 pctiOld
= pti
->pcti
;
1960 pti
->rpdesk
= pdesk
;
1961 pti
->hdesk
= hDesktop
;
1962 pti
->pDeskInfo
= pti
->rpdesk
->pDeskInfo
;
1963 pti
->pcti
= pctiNew
;
1965 pci
->ulClientDelta
= DesktopHeapGetUserDelta();
1966 pci
->pDeskInfo
= (PVOID
)((ULONG_PTR
)pti
->pDeskInfo
- pci
->ulClientDelta
);
1967 pci
->pClientThreadInfo
= (PVOID
)((ULONG_PTR
)pti
->pcti
- pci
->ulClientDelta
);
1969 /* initialize the new pcti */
1972 RtlCopyMemory(pctiNew
, pctiOld
, sizeof(CLIENTTHREADINFO
));
1976 RtlZeroMemory(pctiNew
, sizeof(CLIENTTHREADINFO
));
1977 pci
->fsHooks
= pti
->fsHooks
;
1978 pci
->dwTIFlags
= pti
->TIF_flags
;
1985 pti
->pDeskInfo
= NULL
;
1986 pti
->pcti
= &pti
->cti
; // Always point inside so there will be no crash when posting or sending msg's!
1987 pci
->ulClientDelta
= 0;
1988 pci
->pDeskInfo
= NULL
;
1989 pci
->pClientThreadInfo
= NULL
;
1992 /* clean up the old desktop */
1993 if(pdeskOld
!= NULL
)
1995 RemoveEntryList(&pti
->PtiLink
);
1996 if (pctiOld
) DesktopHeapFree(pdeskOld
, pctiOld
);
1997 IntUnmapDesktopView(pdeskOld
);
1998 ObDereferenceObject(pdeskOld
);
2004 InsertTailList(&pdesk
->PtiList
, &pti
->PtiLink
);
2007 TRACE("IntSetThreadDesktop: pti 0x%p ppi 0x%p switched from object 0x%p to 0x%p\n", pti
, pti
->ppi
, pdeskOld
, pdesk
);
2013 * NtUserSetThreadDesktop
2020 NtUserSetThreadDesktop(HDESK hDesktop
)
2024 UserEnterExclusive();
2026 // FIXME: IntSetThreadDesktop validates the desktop handle, it should happen
2027 // here too and set the NT error level. Q. Is it necessary to have the validation
2028 // in IntSetThreadDesktop? Is it needed there too?
2029 if (hDesktop
|| (!hDesktop
&& CsrProcess
== PsGetCurrentProcess()))
2030 ret
= IntSetThreadDesktop(hDesktop
, FALSE
);