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
)))
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 pcurOld
= UserSetCursor(pcurNew
, FALSE
);
668 UserDereferenceObject(pcurOld
);
673 case WM_WINDOWPOSCHANGING
:
675 PWINDOWPOS pWindowPos
= (PWINDOWPOS
)lParam
;
676 if((pWindowPos
->flags
& SWP_SHOWWINDOW
) != 0)
678 HDESK hdesk
= IntGetDesktopObjectHandle(gpdeskInputDesktop
);
679 IntSetThreadDesktop(hdesk
, FALSE
);
684 return TRUE
; /* We are done. Do not do any callbacks to user mode */
688 UserMessageWindowProc(PWND pwnd
, UINT Msg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*lResult
)
695 pwnd
->fnid
|= FNID_MESSAGEWND
;
696 *lResult
= (LRESULT
)TRUE
;
699 pwnd
->fnid
|= FNID_DESTROY
;
703 return TRUE
; /* We are done. Do not do any callbacks to user mode */
706 VOID NTAPI
DesktopThreadMain()
711 gptiDesktopThread
= PsGetCurrentThreadWin32Thread();
713 UserEnterExclusive();
715 /* Register system classes. This thread does not belong to any desktop so the
716 classes will be allocated from the shared heap */
717 UserRegisterSystemClasses();
721 Ret
= co_IntGetPeekMessage(&Msg
, 0, 0, 0, PM_REMOVE
, TRUE
);
724 IntDispatchMessage(&Msg
);
732 UserGetDesktopDC(ULONG DcType
, BOOL EmptyDC
, BOOL ValidatehWnd
)
734 PWND DesktopObject
= 0;
737 if (DcType
== DC_TYPE_DIRECT
)
739 DesktopObject
= UserGetDesktopWindow();
740 DesktopHDC
= (HDC
)UserGetWindowDC(DesktopObject
);
744 PMONITOR pMonitor
= UserGetPrimaryMonitor();
745 DesktopHDC
= IntGdiCreateDisplayDC(pMonitor
->hDev
, DcType
, EmptyDC
);
757 Window
= UserGetDesktopWindow();
758 hRgn
= IntSysCreateRectRgnIndirect(&Window
->rcWindow
);
760 IntInvalidateWindows( Window
,
767 GreDeleteObject(hRgn
);
772 co_IntShowDesktop(PDESKTOP Desktop
, ULONG Width
, ULONG Height
, BOOL bRedraw
)
774 PWND pwnd
= Desktop
->pDeskInfo
->spwnd
;
775 UINT flags
= SWP_NOACTIVATE
|SWP_NOZORDER
|SWP_SHOWWINDOW
;
779 flags
|= SWP_NOREDRAW
;
781 co_WinPosSetWindowPos(pwnd
, NULL
, 0, 0, Width
, Height
, flags
);
784 co_UserRedrawWindow( pwnd
, NULL
, 0, RDW_UPDATENOW
| RDW_ALLCHILDREN
| RDW_INVALIDATE
);
786 return STATUS_SUCCESS
;
790 IntHideDesktop(PDESKTOP Desktop
)
794 DesktopWnd
= IntGetWindowObject(Desktop
->DesktopWindow
);
797 return ERROR_INVALID_WINDOW_HANDLE
;
799 DesktopWnd
->style
&= ~WS_VISIBLE
;
801 return STATUS_SUCCESS
;
806 UserBuildShellHookHwndList(PDESKTOP Desktop
)
809 PSHELL_HOOK_WINDOW Current
;
812 /* FIXME: If we save nb elements in desktop, we dont have to loop to find nb entries */
813 LIST_FOR_EACH(Current
, &Desktop
->ShellHookWindows
, SHELL_HOOK_WINDOW
, ListEntry
)
816 if (!entries
) return NULL
;
818 list
= ExAllocatePoolWithTag(PagedPool
, sizeof(HWND
) * (entries
+ 1), USERTAG_WINDOWLIST
); /* alloc one extra for nullterm */
823 LIST_FOR_EACH(Current
, &Desktop
->ShellHookWindows
, SHELL_HOOK_WINDOW
, ListEntry
)
824 *cursor
++ = Current
->hWnd
;
826 *cursor
= NULL
; /* Nullterm list */
833 * Send the Message to the windows registered for ShellHook
834 * notifications. The lParam contents depend on the Message. See
835 * MSDN for more details (RegisterShellHookWindow)
837 VOID
co_IntShellHookNotify(WPARAM Message
, WPARAM wParam
, LPARAM lParam
)
839 PDESKTOP Desktop
= IntGetActiveDesktop();
842 if (!gpsi
->uiShellMsg
)
844 gpsi
->uiShellMsg
= IntAddAtom(L
"SHELLHOOK");
846 TRACE("MsgType = %x\n", gpsi
->uiShellMsg
);
847 if (!gpsi
->uiShellMsg
)
848 ERR("LastError: %x\n", EngGetLastError());
853 TRACE("IntShellHookNotify: No desktop!\n");
857 // Allow other devices have a shot at foreground.
858 if (Message
== HSHELL_APPCOMMAND
) ptiLastInput
= NULL
;
860 // FIXME: System Tray Support.
862 HwndList
= UserBuildShellHookHwndList(Desktop
);
865 HWND
* cursor
= HwndList
;
867 for (; *cursor
; cursor
++)
869 TRACE("Sending notify\n");
870 UserPostMessage(*cursor
,
873 (Message
== HSHELL_LANGUAGE
? lParam
: (LPARAM
)wParam
) );
874 /* co_IntPostOrSendMessage(*cursor,
877 (Message == HSHELL_LANGUAGE ? lParam : (LPARAM)wParam) );*/
880 ExFreePoolWithTag(HwndList
, USERTAG_WINDOWLIST
);
883 if (ISITHOOKED(WH_SHELL
))
885 co_HOOK_CallHooks(WH_SHELL
, Message
, wParam
, lParam
);
890 * Add the window to the ShellHookWindows list. The windows
891 * on that list get notifications that are important to shell
894 * TODO: Validate the window? I'm not sure if sending these messages to
895 * an unsuspecting application that is not your own is a nice thing to do.
897 BOOL
IntRegisterShellHookWindow(HWND hWnd
)
899 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
900 PDESKTOP Desktop
= pti
->rpdesk
;
901 PSHELL_HOOK_WINDOW Entry
;
903 TRACE("IntRegisterShellHookWindow\n");
905 /* First deregister the window, so we can be sure it's never twice in the
908 IntDeRegisterShellHookWindow(hWnd
);
910 Entry
= ExAllocatePoolWithTag(PagedPool
,
911 sizeof(SHELL_HOOK_WINDOW
),
919 InsertTailList(&Desktop
->ShellHookWindows
, &Entry
->ListEntry
);
925 * Remove the window from the ShellHookWindows list. The windows
926 * on that list get notifications that are important to shell
929 BOOL
IntDeRegisterShellHookWindow(HWND hWnd
)
931 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
932 PDESKTOP Desktop
= pti
->rpdesk
;
933 PSHELL_HOOK_WINDOW Current
;
935 LIST_FOR_EACH(Current
, &Desktop
->ShellHookWindows
, SHELL_HOOK_WINDOW
, ListEntry
)
937 if (Current
->hWnd
== hWnd
)
939 RemoveEntryList(&Current
->ListEntry
);
940 ExFreePoolWithTag(Current
, TAG_WINSTA
);
949 IntFreeDesktopHeap(IN OUT PDESKTOP Desktop
)
951 /* FIXME: Disable until unmapping works in mm */
953 if (Desktop
->pheapDesktop
!= NULL
)
955 MmUnmapViewInSessionSpace(Desktop
->pheapDesktop
);
956 Desktop
->pheapDesktop
= NULL
;
959 if (Desktop
->hsectionDesktop
!= NULL
)
961 ObDereferenceObject(Desktop
->hsectionDesktop
);
962 Desktop
->hsectionDesktop
= NULL
;
968 IntPaintDesktop(HDC hDC
)
971 HBRUSH DesktopBrush
, PreviousBrush
;
973 BOOL doPatBlt
= TRUE
;
975 static WCHAR s_wszSafeMode
[] = L
"Safe Mode";
981 GdiGetClipBox(hDC
, &Rect
);
983 hWndDesktop
= IntGetDesktopWindow(); // rpdesk->DesktopWindow;
985 WndDesktop
= UserGetWindowObject(hWndDesktop
); // rpdesk->pDeskInfo->spwnd;
991 if (!UserGetSystemMetrics(SM_CLEANBOOT
))
993 DesktopBrush
= (HBRUSH
)WndDesktop
->pcls
->hbrBackground
;
996 * Paint desktop background
998 if (gspv
.hbmWallpaper
!= NULL
)
1004 sz
.cx
= WndDesktop
->rcWindow
.right
- WndDesktop
->rcWindow
.left
;
1005 sz
.cy
= WndDesktop
->rcWindow
.bottom
- WndDesktop
->rcWindow
.top
;
1007 if (gspv
.WallpaperMode
== wmStretch
||
1008 gspv
.WallpaperMode
== wmTile
)
1015 /* Find the upper left corner, can be negative if the bitmap is bigger then the screen */
1016 x
= (sz
.cx
/ 2) - (gspv
.cxWallpaper
/ 2);
1017 y
= (sz
.cy
/ 2) - (gspv
.cyWallpaper
/ 2);
1020 hWallpaperDC
= NtGdiCreateCompatibleDC(hDC
);
1021 if(hWallpaperDC
!= NULL
)
1025 /* Fill in the area that the bitmap is not going to cover */
1028 /* FIXME: Clip out the bitmap
1029 can be replaced with "NtGdiPatBlt(hDC, x, y, WinSta->cxWallpaper, WinSta->cyWallpaper, PATCOPY | DSTINVERT);"
1030 once we support DSTINVERT */
1031 PreviousBrush
= NtGdiSelectBrush(hDC
, DesktopBrush
);
1032 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
1033 NtGdiSelectBrush(hDC
, PreviousBrush
);
1036 /* Do not fill the background after it is painted no matter the size of the picture */
1039 hOldBitmap
= NtGdiSelectBitmap(hWallpaperDC
, gspv
.hbmWallpaper
);
1041 if (gspv
.WallpaperMode
== wmStretch
)
1043 if(Rect
.right
&& Rect
.bottom
)
1044 NtGdiStretchBlt(hDC
,
1058 else if (gspv
.WallpaperMode
== wmTile
)
1060 /* Paint the bitmap across the screen then down */
1061 for(y
= 0; y
< Rect
.bottom
; y
+= gspv
.cyWallpaper
)
1063 for(x
= 0; x
< Rect
.right
; x
+= gspv
.cxWallpaper
)
1093 NtGdiSelectBitmap(hWallpaperDC
, hOldBitmap
);
1094 NtGdiDeleteObjectApp(hWallpaperDC
);
1100 /* Black desktop background in Safe Mode */
1101 DesktopBrush
= StockObjects
[BLACK_BRUSH
];
1103 /* Back ground is set to none, clear the screen */
1106 PreviousBrush
= NtGdiSelectBrush(hDC
, DesktopBrush
);
1107 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
1108 NtGdiSelectBrush(hDC
, PreviousBrush
);
1112 * Display system version on the desktop background
1115 if (g_PaintDesktopVersion
|| UserGetSystemMetrics(SM_CLEANBOOT
))
1117 static WCHAR s_wszVersion
[256] = {0};
1122 len
= wcslen(s_wszVersion
);
1126 len
= GetSystemVersionString(s_wszVersion
);
1131 if (!UserSystemParametersInfo(SPI_GETWORKAREA
, 0, &rect
, 0))
1133 rect
.right
= UserGetSystemMetrics(SM_CXSCREEN
);
1134 rect
.bottom
= UserGetSystemMetrics(SM_CYSCREEN
);
1137 color_old
= IntGdiSetTextColor(hDC
, RGB(255,255,255));
1138 align_old
= IntGdiSetTextAlign(hDC
, TA_RIGHT
);
1139 mode_old
= IntGdiSetBkMode(hDC
, TRANSPARENT
);
1141 if(!UserGetSystemMetrics(SM_CLEANBOOT
))
1143 GreExtTextOutW(hDC
, rect
.right
- 16, rect
.bottom
- 48, 0, NULL
, s_wszVersion
, len
, NULL
, 0);
1149 /* Version information text in top center */
1150 IntGdiSetTextAlign(hDC
, TA_CENTER
| TA_TOP
);
1151 GreExtTextOutW(hDC
, (rect
.right
+ rect
.left
)/2, rect
.top
+ 3, 0, NULL
, s_wszVersion
, len
, NULL
, 0);
1153 /* Safe Mode text in corners */
1154 len
= wcslen(s_wszSafeMode
);
1155 IntGdiSetTextAlign(hDC
, TA_LEFT
| TA_TOP
);
1156 GreExtTextOutW(hDC
, rect
.left
, rect
.top
+ 3, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1157 IntGdiSetTextAlign(hDC
, TA_RIGHT
| TA_TOP
);
1158 GreExtTextOutW(hDC
, rect
.right
, rect
.top
+ 3, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1159 IntGdiSetTextAlign(hDC
, TA_LEFT
| TA_BASELINE
);
1160 GreExtTextOutW(hDC
, rect
.left
, rect
.bottom
- 5, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1161 IntGdiSetTextAlign(hDC
, TA_RIGHT
| TA_BASELINE
);
1162 GreExtTextOutW(hDC
, rect
.right
, rect
.bottom
- 5, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1165 IntGdiSetBkMode(hDC
, mode_old
);
1166 IntGdiSetTextAlign(hDC
, align_old
);
1167 IntGdiSetTextColor(hDC
, color_old
);
1174 UserInitializeDesktop(PDESKTOP pdesk
, PUNICODE_STRING DesktopName
, PWINSTATION_OBJECT pwinsta
)
1176 PVOID DesktopHeapSystemBase
= NULL
;
1177 ULONG_PTR HeapSize
= 400 * 1024;
1178 SIZE_T DesktopInfoSize
;
1181 TRACE("UserInitializeDesktop desktop 0x%p with name %wZ\n", pdesk
, DesktopName
);
1183 RtlZeroMemory(pdesk
, sizeof(DESKTOP
));
1185 /* Link the desktop with the parent window station */
1186 pdesk
->rpwinstaParent
= pwinsta
;
1187 InsertTailList(&pwinsta
->DesktopListHead
, &pdesk
->ListEntry
);
1189 /* Create the desktop heap */
1190 pdesk
->hsectionDesktop
= NULL
;
1191 pdesk
->pheapDesktop
= UserCreateHeap(&pdesk
->hsectionDesktop
,
1192 &DesktopHeapSystemBase
,
1194 if (pdesk
->pheapDesktop
== NULL
)
1196 ERR("Failed to create desktop heap!\n");
1197 return STATUS_NO_MEMORY
;
1200 /* Create DESKTOPINFO */
1201 DesktopInfoSize
= sizeof(DESKTOPINFO
) + DesktopName
->Length
+ sizeof(WCHAR
);
1202 pdesk
->pDeskInfo
= RtlAllocateHeap(pdesk
->pheapDesktop
,
1203 HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
,
1205 if (pdesk
->pDeskInfo
== NULL
)
1207 ERR("Failed to create the DESKTOP structure!\n");
1208 return STATUS_NO_MEMORY
;
1211 /* Initialize the DESKTOPINFO */
1212 pdesk
->pDeskInfo
->pvDesktopBase
= DesktopHeapSystemBase
;
1213 pdesk
->pDeskInfo
->pvDesktopLimit
= (PVOID
)((ULONG_PTR
)DesktopHeapSystemBase
+ HeapSize
);
1214 RtlCopyMemory(pdesk
->pDeskInfo
->szDesktopName
,
1215 DesktopName
->Buffer
,
1216 DesktopName
->Length
+ sizeof(WCHAR
));
1217 for (i
= 0; i
< NB_HOOKS
; i
++)
1219 InitializeListHead(&pdesk
->pDeskInfo
->aphkStart
[i
]);
1222 InitializeListHead(&pdesk
->ShellHookWindows
);
1223 InitializeListHead(&pdesk
->PtiList
);
1225 return STATUS_SUCCESS
;
1228 /* SYSCALLS *******************************************************************/
1231 * NtUserCreateDesktop
1233 * Creates a new desktop.
1237 * Object Attributes.
1240 * Name of the device.
1246 * Interaction flags.
1249 * Requested type of access.
1253 * If the function succeeds, the return value is a handle to the newly
1254 * created desktop. If the specified desktop already exists, the function
1255 * succeeds and returns a handle to the existing desktop. When you are
1256 * finished using the handle, call the CloseDesktop function to close it.
1257 * If the function fails, the return value is NULL.
1264 NtUserCreateDesktop(
1265 POBJECT_ATTRIBUTES ObjectAttributes
,
1266 PUNICODE_STRING lpszDesktopDevice
,
1269 ACCESS_MASK dwDesiredAccess
)
1271 PDESKTOP pdesk
= NULL
;
1272 NTSTATUS Status
= STATUS_SUCCESS
;
1275 UNICODE_STRING ClassName
;
1276 LARGE_STRING WindowName
;
1277 BOOL NoHooks
= FALSE
;
1280 PTHREADINFO ptiCurrent
;
1283 DECLARE_RETURN(HDESK
);
1285 TRACE("Enter NtUserCreateDesktop\n");
1286 UserEnterExclusive();
1288 ptiCurrent
= PsGetCurrentThreadWin32Thread();
1290 ASSERT(gptiDesktopThread
);
1292 /* Turn off hooks when calling any CreateWindowEx from inside win32k. */
1293 NoHooks
= (ptiCurrent
->TIF_flags
& TIF_DISABLEHOOKS
);
1294 ptiCurrent
->TIF_flags
|= TIF_DISABLEHOOKS
;
1295 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
1298 * Try to open already existing desktop
1300 Status
= ObOpenObjectByName(
1302 ExDesktopObjectType
,
1308 if (!NT_SUCCESS(Status
))
1310 ERR("ObOpenObjectByName failed to open/create desktop\n");
1311 SetLastNtError(Status
);
1315 /* In case the object was not created (eg if it existed), return now */
1316 if (Context
== FALSE
)
1318 TRACE("NtUserCreateDesktop opened desktop %wZ\n", ObjectAttributes
->ObjectName
);
1322 /* Reference the desktop */
1323 Status
= ObReferenceObjectByHandle(hdesk
,
1325 ExDesktopObjectType
,
1329 if (!NT_SUCCESS(Status
))
1331 ERR("Failed to reference desktop object\n");
1332 SetLastNtError(Status
);
1336 /* Get the desktop window class. The thread desktop does not belong to any desktop
1337 * so the classes created there (including the desktop class) are allocated in the shared heap
1338 * It would cause problems if we used a class that belongs to the caller
1340 ClassName
.Buffer
= WC_DESKTOP
;
1341 ClassName
.Length
= 0;
1342 pcls
= IntGetAndReferenceClass(&ClassName
, 0, TRUE
);
1349 RtlZeroMemory(&WindowName
, sizeof(WindowName
));
1350 RtlZeroMemory(&Cs
, sizeof(Cs
));
1351 Cs
.x
= UserGetSystemMetrics(SM_XVIRTUALSCREEN
),
1352 Cs
.y
= UserGetSystemMetrics(SM_YVIRTUALSCREEN
),
1353 Cs
.cx
= UserGetSystemMetrics(SM_CXVIRTUALSCREEN
),
1354 Cs
.cy
= UserGetSystemMetrics(SM_CYVIRTUALSCREEN
),
1355 Cs
.style
= WS_POPUP
|WS_CLIPCHILDREN
;
1356 Cs
.hInstance
= hModClient
; // hModuleWin; // Server side winproc!
1357 Cs
.lpszName
= (LPCWSTR
) &WindowName
;
1358 Cs
.lpszClass
= (LPCWSTR
) &ClassName
;
1360 /* Use IntCreateWindow instead of co_UserCreateWindowEx cause the later expects a thread with a desktop */
1361 pWnd
= IntCreateWindow(&Cs
, &WindowName
, pcls
, NULL
, NULL
, NULL
, pdesk
);
1364 ERR("Failed to create desktop window for the new desktop\n");
1368 pdesk
->dwSessionId
= PsGetCurrentProcessSessionId();
1369 pdesk
->DesktopWindow
= pWnd
->head
.h
;
1370 pdesk
->pDeskInfo
->spwnd
= pWnd
;
1371 pWnd
->fnid
= FNID_DESKTOP
;
1373 ClassName
.Buffer
= MAKEINTATOM(gpsi
->atomSysClass
[ICLS_HWNDMESSAGE
]);
1374 ClassName
.Length
= 0;
1375 pcls
= IntGetAndReferenceClass(&ClassName
, 0, TRUE
);
1382 RtlZeroMemory(&WindowName
, sizeof(WindowName
));
1383 RtlZeroMemory(&Cs
, sizeof(Cs
));
1384 Cs
.cx
= Cs
.cy
= 100;
1385 Cs
.style
= WS_POPUP
|WS_CLIPCHILDREN
;
1386 Cs
.hInstance
= hModClient
; // hModuleWin; // Server side winproc!
1387 Cs
.lpszName
= (LPCWSTR
) &WindowName
;
1388 Cs
.lpszClass
= (LPCWSTR
) &ClassName
;
1389 pWnd
= IntCreateWindow(&Cs
, &WindowName
, pcls
, NULL
, NULL
, NULL
, pdesk
);
1392 ERR("Failed to create message window for the new desktop\n");
1396 pdesk
->spwndMessage
= pWnd
;
1397 pWnd
->fnid
= FNID_MESSAGEWND
;
1400 if !(WinStaObject->Flags & WSF_NOIO) is (not set) for desktop input output mode (see wiki)
1401 Create Tooltip. Saved in DesktopObject->spwndTooltip.
1402 Tooltip dwExStyle: WS_EX_TOOLWINDOW|WS_EX_TOPMOST
1403 hWndParent are spwndMessage. Use hModuleWin for server side winproc!
1404 The rest is same as message window.
1405 http://msdn.microsoft.com/en-us/library/bb760250(VS.85).aspx
1412 ObDereferenceObject(pdesk
);
1414 if (_ret_
== NULL
&& hdesk
!= NULL
)
1416 ObCloseHandle(hdesk
, UserMode
);
1420 ptiCurrent
->TIF_flags
&= ~TIF_DISABLEHOOKS
;
1421 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
1423 TRACE("Leave NtUserCreateDesktop, ret=%p\n",_ret_
);
1431 * Opens an existing desktop.
1435 * Name of the existing desktop.
1438 * Interaction flags.
1441 * Requested type of access.
1444 * Handle to the desktop or zero on failure.
1452 POBJECT_ATTRIBUTES ObjectAttributes
,
1454 ACCESS_MASK dwDesiredAccess
)
1459 Status
= ObOpenObjectByName(
1461 ExDesktopObjectType
,
1468 if (!NT_SUCCESS(Status
))
1470 ERR("Failed to open desktop\n");
1471 SetLastNtError(Status
);
1475 TRACE("Opened desktop %S with handle 0x%p\n", ObjectAttributes
->ObjectName
->Buffer
, Desktop
);
1481 * NtUserOpenInputDesktop
1483 * Opens the input (interactive) desktop.
1487 * Interaction flags.
1490 * Inheritance option.
1493 * Requested type of access.
1496 * Handle to the input desktop or zero on failure.
1503 NtUserOpenInputDesktop(
1506 ACCESS_MASK dwDesiredAccess
)
1510 ULONG HandleAttributes
= 0;
1512 UserEnterExclusive();
1513 TRACE("Enter NtUserOpenInputDesktop gpdeskInputDesktop 0x%p\n",gpdeskInputDesktop
);
1515 if(fInherit
) HandleAttributes
= OBJ_INHERIT
;
1517 /* Create a new handle to the object */
1518 Status
= ObOpenObjectByPointer(
1523 ExDesktopObjectType
,
1527 if (!NT_SUCCESS(Status
))
1529 ERR("Failed to open input desktop object\n");
1530 SetLastNtError(Status
);
1533 TRACE("NtUserOpenInputDesktop returning 0x%p\n",hdesk
);
1539 * NtUserCloseDesktop
1541 * Closes a desktop handle.
1545 * Handle to the desktop.
1551 * The desktop handle can be created with NtUserCreateDesktop or
1552 * NtUserOpenDesktop. This function will fail if any thread in the calling
1553 * process is using the specified desktop handle or if the handle refers
1554 * to the initial desktop of the calling process.
1561 NtUserCloseDesktop(HDESK hDesktop
)
1565 DECLARE_RETURN(BOOL
);
1567 TRACE("NtUserCloseDesktop called (0x%p)\n", hDesktop
);
1568 UserEnterExclusive();
1570 if( hDesktop
== gptiCurrent
->hdesk
|| hDesktop
== gptiCurrent
->ppi
->hdeskStartup
)
1572 ERR("Attempted to close thread desktop\n");
1573 EngSetLastError(ERROR_BUSY
);
1577 Status
= IntValidateDesktopHandle( hDesktop
, UserMode
, 0, &pdesk
);
1578 if (!NT_SUCCESS(Status
))
1580 ERR("Validation of desktop handle (0x%p) failed\n", hDesktop
);
1584 ObDereferenceObject(pdesk
);
1586 Status
= ZwClose(hDesktop
);
1587 if (!NT_SUCCESS(Status
))
1589 ERR("Failed to close desktop handle 0x%p\n", hDesktop
);
1590 SetLastNtError(Status
);
1597 TRACE("Leave NtUserCloseDesktop, ret=%i\n",_ret_
);
1603 * NtUserPaintDesktop
1605 * The NtUserPaintDesktop function fills the clipping region in the
1606 * specified device context with the desktop pattern or wallpaper. The
1607 * function is provided primarily for shell desktops.
1611 * Handle to the device context.
1618 NtUserPaintDesktop(HDC hDC
)
1621 UserEnterExclusive();
1622 TRACE("Enter NtUserPaintDesktop\n");
1623 Ret
= IntPaintDesktop(hDC
);
1624 TRACE("Leave NtUserPaintDesktop, ret=%i\n",Ret
);
1630 * NtUserSwitchDesktop
1632 * Sets the current input (interactive) desktop.
1636 * Handle to desktop.
1646 NtUserSwitchDesktop(HDESK hdesk
)
1650 BOOL bRedrawDesktop
;
1651 DECLARE_RETURN(BOOL
);
1653 UserEnterExclusive();
1654 TRACE("Enter NtUserSwitchDesktop(0x%p)\n", hdesk
);
1656 Status
= IntValidateDesktopHandle( hdesk
, UserMode
, 0, &pdesk
);
1657 if (!NT_SUCCESS(Status
))
1659 ERR("Validation of desktop handle (0x%p) failed\n", hdesk
);
1663 if (PsGetCurrentProcessSessionId() != pdesk
->rpwinstaParent
->dwSessionId
)
1665 ERR("NtUserSwitchDesktop called for a desktop of a different session\n");
1669 if(pdesk
== gpdeskInputDesktop
)
1671 WARN("NtUserSwitchDesktop called for active desktop\n");
1676 * Don't allow applications switch the desktop if it's locked, unless the caller
1677 * is the logon application itself
1679 if((pdesk
->rpwinstaParent
->Flags
& WSS_LOCKED
) &&
1680 LogonProcess
!= PsGetCurrentProcessWin32Process())
1682 ObDereferenceObject(pdesk
);
1683 ERR("Switching desktop 0x%p denied because the window station is locked!\n", hdesk
);
1687 if(pdesk
->rpwinstaParent
!= InputWindowStation
)
1689 ObDereferenceObject(pdesk
);
1690 ERR("Switching desktop 0x%p denied because desktop doesn't belong to the interactive winsta!\n", hdesk
);
1694 /* FIXME: Fail if the process is associated with a secured
1695 desktop such as Winlogon or Screen-Saver */
1696 /* FIXME: Connect to input device */
1698 TRACE("Switching from desktop 0x%p to 0x%p\n", gpdeskInputDesktop
, pdesk
);
1700 bRedrawDesktop
= FALSE
;
1702 /* The first time SwitchDesktop is called, gpdeskInputDesktop is NULL */
1703 if(gpdeskInputDesktop
!= NULL
)
1705 if((gpdeskInputDesktop
->pDeskInfo
->spwnd
->style
& WS_VISIBLE
) == WS_VISIBLE
)
1706 bRedrawDesktop
= TRUE
;
1708 /* Hide the previous desktop window */
1709 IntHideDesktop(gpdeskInputDesktop
);
1712 /* Set the active desktop in the desktop's window station. */
1713 InputWindowStation
->ActiveDesktop
= pdesk
;
1715 /* Set the global state. */
1716 gpdeskInputDesktop
= pdesk
;
1718 /* Show the new desktop window */
1719 co_IntShowDesktop(pdesk
, UserGetSystemMetrics(SM_CXSCREEN
), UserGetSystemMetrics(SM_CYSCREEN
), bRedrawDesktop
);
1721 TRACE("SwitchDesktop gpdeskInputDesktop 0x%p\n",gpdeskInputDesktop
);
1722 ObDereferenceObject(pdesk
);
1727 TRACE("Leave NtUserSwitchDesktop, ret=%i\n",_ret_
);
1733 * NtUserGetThreadDesktop
1740 NtUserGetThreadDesktop(DWORD dwThreadId
, DWORD Unknown1
)
1744 PDESKTOP DesktopObject
;
1745 HDESK Ret
, hThreadDesktop
;
1746 OBJECT_HANDLE_INFORMATION HandleInformation
;
1747 DECLARE_RETURN(HDESK
);
1749 UserEnterExclusive();
1750 TRACE("Enter NtUserGetThreadDesktop\n");
1754 EngSetLastError(ERROR_INVALID_PARAMETER
);
1758 Status
= PsLookupThreadByThreadId((HANDLE
)(DWORD_PTR
)dwThreadId
, &Thread
);
1759 if(!NT_SUCCESS(Status
))
1761 EngSetLastError(ERROR_INVALID_PARAMETER
);
1765 if(Thread
->ThreadsProcess
== PsGetCurrentProcess())
1767 /* Just return the handle, we queried the desktop handle of a thread running
1768 in the same context */
1769 Ret
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->hdesk
;
1770 ObDereferenceObject(Thread
);
1774 /* Get the desktop handle and the desktop of the thread */
1775 if(!(hThreadDesktop
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->hdesk
) ||
1776 !(DesktopObject
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->rpdesk
))
1778 ObDereferenceObject(Thread
);
1779 ERR("Desktop information of thread 0x%x broken!?\n", dwThreadId
);
1783 /* We could just use DesktopObject instead of looking up the handle, but latter
1784 may be a bit safer (e.g. when the desktop is being destroyed */
1785 /* Switch into the context of the thread we're trying to get the desktop from,
1786 so we can use the handle */
1787 KeAttachProcess(&Thread
->ThreadsProcess
->Pcb
);
1788 Status
= ObReferenceObjectByHandle(hThreadDesktop
,
1790 ExDesktopObjectType
,
1792 (PVOID
*)&DesktopObject
,
1793 &HandleInformation
);
1796 /* The handle couldn't be found, there's nothing to get... */
1797 if(!NT_SUCCESS(Status
))
1799 ObDereferenceObject(Thread
);
1803 /* Lookup our handle table if we can find a handle to the desktop object,
1804 if not, create one */
1805 Ret
= IntGetDesktopObjectHandle(DesktopObject
);
1807 /* All done, we got a valid handle to the desktop */
1808 ObDereferenceObject(DesktopObject
);
1809 ObDereferenceObject(Thread
);
1813 TRACE("Leave NtUserGetThreadDesktop, ret=%p\n",_ret_
);
1819 IntUnmapDesktopView(IN PDESKTOP pdesk
)
1822 PW32HEAP_USER_MAPPING HeapMapping
, *PrevLink
;
1823 NTSTATUS Status
= STATUS_SUCCESS
;
1825 TRACE("IntUnmapDesktopView called for desktop object %p\n", pdesk
);
1827 ppi
= PsGetCurrentProcessWin32Process();
1828 PrevLink
= &ppi
->HeapMappings
.Next
;
1830 /* Unmap if we're the last thread using the desktop */
1831 HeapMapping
= *PrevLink
;
1832 while (HeapMapping
!= NULL
)
1834 if (HeapMapping
->KernelMapping
== (PVOID
)pdesk
->pheapDesktop
)
1836 if (--HeapMapping
->Count
== 0)
1838 *PrevLink
= HeapMapping
->Next
;
1840 TRACE("ppi 0x%p unmapped heap of desktop 0x%p\n", ppi
, pdesk
);
1841 Status
= MmUnmapViewOfSection(PsGetCurrentProcess(),
1842 HeapMapping
->UserMapping
);
1844 ObDereferenceObject(pdesk
);
1846 UserHeapFree(HeapMapping
);
1851 PrevLink
= &HeapMapping
->Next
;
1852 HeapMapping
= HeapMapping
->Next
;
1859 IntMapDesktopView(IN PDESKTOP pdesk
)
1862 PW32HEAP_USER_MAPPING HeapMapping
, *PrevLink
;
1863 PVOID UserBase
= NULL
;
1864 SIZE_T ViewSize
= 0;
1865 LARGE_INTEGER Offset
;
1868 TRACE("IntMapDesktopView called for desktop object 0x%p\n", pdesk
);
1870 ppi
= PsGetCurrentProcessWin32Process();
1871 PrevLink
= &ppi
->HeapMappings
.Next
;
1873 /* Find out if another thread already mapped the desktop heap */
1874 HeapMapping
= *PrevLink
;
1875 while (HeapMapping
!= NULL
)
1877 if (HeapMapping
->KernelMapping
== (PVOID
)pdesk
->pheapDesktop
)
1879 HeapMapping
->Count
++;
1880 return STATUS_SUCCESS
;
1883 PrevLink
= &HeapMapping
->Next
;
1884 HeapMapping
= HeapMapping
->Next
;
1887 /* We're the first, map the heap */
1888 Offset
.QuadPart
= 0;
1889 Status
= MmMapViewOfSection(pdesk
->hsectionDesktop
,
1890 PsGetCurrentProcess(),
1898 PAGE_EXECUTE_READ
); /* Would prefer PAGE_READONLY, but thanks to RTL heaps... */
1899 if (!NT_SUCCESS(Status
))
1901 ERR("Failed to map desktop\n");
1905 TRACE("ppi 0x%p mapped heap of desktop 0x%p\n", ppi
, pdesk
);
1907 /* Add the mapping */
1908 HeapMapping
= UserHeapAlloc(sizeof(W32HEAP_USER_MAPPING
));
1909 if (HeapMapping
== NULL
)
1911 MmUnmapViewOfSection(PsGetCurrentProcess(), UserBase
);
1912 ERR("UserHeapAlloc() failed!\n");
1913 return STATUS_NO_MEMORY
;
1916 HeapMapping
->Next
= NULL
;
1917 HeapMapping
->KernelMapping
= (PVOID
)pdesk
->pheapDesktop
;
1918 HeapMapping
->UserMapping
= UserBase
;
1919 HeapMapping
->Limit
= ViewSize
;
1920 HeapMapping
->Count
= 1;
1921 *PrevLink
= HeapMapping
;
1923 ObReferenceObject(pdesk
);
1925 return STATUS_SUCCESS
;
1929 IntSetThreadDesktop(IN HDESK hDesktop
,
1930 IN BOOL FreeOnFailure
)
1932 PDESKTOP pdesk
= NULL
, pdeskOld
;
1936 PCLIENTTHREADINFO pctiOld
, pctiNew
= NULL
;
1939 ASSERT(NtCurrentTeb());
1941 TRACE("IntSetThreadDesktop hDesktop:0x%p, FOF:%i\n",hDesktop
, FreeOnFailure
);
1943 pti
= PsGetCurrentThreadWin32Thread();
1944 pci
= pti
->pClientInfo
;
1946 /* If the caller gave us a desktop, ensure it is valid */
1947 if(hDesktop
!= NULL
)
1949 /* Validate the new desktop. */
1950 Status
= IntValidateDesktopHandle( hDesktop
, UserMode
, 0, &pdesk
);
1951 if (!NT_SUCCESS(Status
))
1953 ERR("Validation of desktop handle (0x%p) failed\n", hDesktop
);
1957 if (pti
->rpdesk
== pdesk
)
1960 ObDereferenceObject(pdesk
);
1965 /* Make sure that we don't own any window in the current desktop */
1966 if (!IsListEmpty(&pti
->WindowListHead
))
1969 ObDereferenceObject(pdesk
);
1970 ERR("Attempted to change thread desktop although the thread has windows!\n");
1971 EngSetLastError(ERROR_BUSY
);
1975 /* Desktop is being re-set so clear out foreground. */
1976 if (pti
->rpdesk
!= pdesk
&& pti
->MessageQueue
== gpqForeground
)
1978 // Like above, there shouldn't be any windows, hooks or anything active on this threads desktop!
1979 IntSetFocusMessageQueue(NULL
);
1982 /* Before doing the switch, map the new desktop heap and allocate the new pcti */
1985 Status
= IntMapDesktopView(pdesk
);
1986 if (!NT_SUCCESS(Status
))
1988 ERR("Failed to map desktop heap!\n");
1989 ObDereferenceObject(pdesk
);
1990 SetLastNtError(Status
);
1994 pctiNew
= DesktopHeapAlloc( pdesk
, sizeof(CLIENTTHREADINFO
));
1997 ERR("Failed to allocate new pcti\n");
1998 IntUnmapDesktopView(pdesk
);
1999 ObDereferenceObject(pdesk
);
2000 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2005 /* free all classes or move them to the shared heap */
2006 if(pti
->rpdesk
!= NULL
)
2008 if(!IntCheckProcessDesktopClasses(pti
->rpdesk
, FreeOnFailure
))
2010 ERR("Failed to move process classes to shared heap!\n");
2013 DesktopHeapFree(pdesk
, pctiNew
);
2014 IntUnmapDesktopView(pdesk
);
2015 ObDereferenceObject(pdesk
);
2021 pdeskOld
= pti
->rpdesk
;
2022 hdeskOld
= pti
->hdesk
;
2023 if (pti
->pcti
!= &pti
->cti
)
2024 pctiOld
= pti
->pcti
;
2031 pti
->rpdesk
= pdesk
;
2032 pti
->hdesk
= hDesktop
;
2033 pti
->pDeskInfo
= pti
->rpdesk
->pDeskInfo
;
2034 pti
->pcti
= pctiNew
;
2036 pci
->ulClientDelta
= DesktopHeapGetUserDelta();
2037 pci
->pDeskInfo
= (PVOID
)((ULONG_PTR
)pti
->pDeskInfo
- pci
->ulClientDelta
);
2038 pci
->pClientThreadInfo
= (PVOID
)((ULONG_PTR
)pti
->pcti
- pci
->ulClientDelta
);
2040 /* initialize the new pcti */
2043 RtlCopyMemory(pctiNew
, pctiOld
, sizeof(CLIENTTHREADINFO
));
2047 RtlZeroMemory(pctiNew
, sizeof(CLIENTTHREADINFO
));
2048 pci
->fsHooks
= pti
->fsHooks
;
2049 pci
->dwTIFlags
= pti
->TIF_flags
;
2056 pti
->pDeskInfo
= NULL
;
2057 pti
->pcti
= &pti
->cti
; // Always point inside so there will be no crash when posting or sending msg's!
2058 pci
->ulClientDelta
= 0;
2059 pci
->pDeskInfo
= NULL
;
2060 pci
->pClientThreadInfo
= NULL
;
2063 /* clean up the old desktop */
2064 if(pdeskOld
!= NULL
)
2066 RemoveEntryList(&pti
->PtiLink
);
2067 if (pctiOld
) DesktopHeapFree(pdeskOld
, pctiOld
);
2068 IntUnmapDesktopView(pdeskOld
);
2069 ObDereferenceObject(pdeskOld
);
2075 InsertTailList(&pdesk
->PtiList
, &pti
->PtiLink
);
2078 TRACE("IntSetThreadDesktop: pti 0x%p ppi 0x%p switched from object 0x%p to 0x%p\n", pti
, pti
->ppi
, pdeskOld
, pdesk
);
2084 * NtUserSetThreadDesktop
2091 NtUserSetThreadDesktop(HDESK hDesktop
)
2095 UserEnterExclusive();
2097 // FIXME: IntSetThreadDesktop validates the desktop handle, it should happen
2098 // here too and set the NT error level. Q. Is it necessary to have the validation
2099 // in IntSetThreadDesktop? Is it needed there too?
2100 if (hDesktop
|| (!hDesktop
&& PsGetCurrentProcess() == gpepCSRSS
))
2101 ret
= IntSetThreadDesktop(hDesktop
, FALSE
);