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 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
;
143 IntDesktopObjectDelete(
144 _In_ PVOID Parameters
)
146 PWIN32_DELETEMETHOD_PARAMETERS DeleteParameters
= Parameters
;
147 PDESKTOP pdesk
= (PDESKTOP
)DeleteParameters
->Object
;
149 TRACE("Deleting desktop object 0x%p\n", pdesk
);
151 ASSERT(pdesk
->pDeskInfo
->spwnd
->spwndChild
== NULL
);
153 if (pdesk
->pDeskInfo
->spwnd
)
154 co_UserDestroyWindow(pdesk
->pDeskInfo
->spwnd
);
156 if (pdesk
->spwndMessage
)
157 co_UserDestroyWindow(pdesk
->spwndMessage
);
159 /* Remove the desktop from the window station's list of associcated desktops */
160 RemoveEntryList(&pdesk
->ListEntry
);
163 IntFreeDesktopHeap(pdesk
);
164 return STATUS_SUCCESS
;
170 _In_ PVOID Parameters
)
172 PWIN32_OKAYTOCLOSEMETHOD_PARAMETERS OkToCloseParameters
= Parameters
;
173 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
177 /* This happens when we leak desktop handles */
178 return STATUS_SUCCESS
;
181 /* Do not allow the current desktop or the initial desktop to be closed */
182 if( OkToCloseParameters
->Handle
== pti
->ppi
->hdeskStartup
||
183 OkToCloseParameters
->Handle
== pti
->hdesk
)
185 return STATUS_ACCESS_DENIED
;
188 return STATUS_SUCCESS
;
193 IntDesktopObjectOpen(
194 _In_ PVOID Parameters
)
196 PWIN32_OPENMETHOD_PARAMETERS OpenParameters
= Parameters
;
197 PPROCESSINFO ppi
= PsGetProcessWin32Process(OpenParameters
->Process
);
199 return STATUS_SUCCESS
;
201 return IntMapDesktopView((PDESKTOP
)OpenParameters
->Object
);
206 IntDesktopObjectClose(
207 _In_ PVOID Parameters
)
209 PWIN32_CLOSEMETHOD_PARAMETERS CloseParameters
= Parameters
;
210 PPROCESSINFO ppi
= PsGetProcessWin32Process(CloseParameters
->Process
);
213 /* This happens when the process leaks desktop handles.
214 * At this point the PPROCESSINFO is already destroyed */
215 return STATUS_SUCCESS
;
218 return IntUnmapDesktopView((PDESKTOP
)CloseParameters
->Object
);
222 /* PRIVATE FUNCTIONS **********************************************************/
227 InitDesktopImpl(VOID
)
229 GENERIC_MAPPING IntDesktopMapping
= { DESKTOP_READ
,
234 /* Set Desktop Object Attributes */
235 ExDesktopObjectType
->TypeInfo
.DefaultNonPagedPoolCharge
= sizeof(DESKTOP
);
236 ExDesktopObjectType
->TypeInfo
.GenericMapping
= IntDesktopMapping
;
237 ExDesktopObjectType
->TypeInfo
.ValidAccessMask
= DESKTOP_ALL_ACCESS
;
238 return STATUS_SUCCESS
;
241 static int GetSystemVersionString(LPWSTR buffer
)
243 RTL_OSVERSIONINFOEXW versionInfo
;
246 versionInfo
.dwOSVersionInfoSize
= sizeof(RTL_OSVERSIONINFOEXW
);
248 if (!NT_SUCCESS(RtlGetVersion((PRTL_OSVERSIONINFOW
)&versionInfo
)))
251 if (versionInfo
.dwMajorVersion
<= 4)
252 len
= swprintf(buffer
,
253 L
"ReactOS Version %lu.%lu %s Build %lu",
254 versionInfo
.dwMajorVersion
, versionInfo
.dwMinorVersion
,
255 versionInfo
.szCSDVersion
, versionInfo
.dwBuildNumber
&0xFFFF);
257 len
= swprintf(buffer
,
258 L
"ReactOS %s (Build %lu)",
259 versionInfo
.szCSDVersion
, versionInfo
.dwBuildNumber
&0xFFFF);
266 IntParseDesktopPath(PEPROCESS Process
,
267 PUNICODE_STRING DesktopPath
,
271 OBJECT_ATTRIBUTES ObjectAttributes
;
272 UNICODE_STRING ObjectName
;
274 WCHAR wstrWinstaFullName
[MAX_PATH
], *pwstrWinsta
= NULL
, *pwstrDesktop
= NULL
;
283 if(DesktopPath
->Buffer
!= NULL
&& DesktopPath
->Length
> sizeof(WCHAR
))
286 * Parse the desktop path string which can be in the form "WinSta\Desktop"
287 * or just "Desktop". In latter case WinSta0 will be used.
290 pwstrDesktop
= wcschr(DesktopPath
->Buffer
, L
'\\');
291 if(pwstrDesktop
!= NULL
)
295 pwstrWinsta
= DesktopPath
->Buffer
;
299 pwstrDesktop
= DesktopPath
->Buffer
;
303 TRACE("IntParseDesktopPath pwstrWinsta:%S pwstrDesktop:%S\n", pwstrWinsta
, pwstrDesktop
);
307 /* Search the process handle table for (inherited) window station
308 handles, use a more appropriate one than WinSta0 if possible. */
309 if (!ObFindHandleForObject(Process
,
311 ExWindowStationObjectType
,
316 /* We had no luck searching for opened handles, use WinSta0 now */
318 pwstrWinsta
= L
"WinSta0";
322 /* Search the process handle table for (inherited) desktop
323 handles, use a more appropriate one than Default if possible. */
324 if (!ObFindHandleForObject(Process
,
331 /* We had no luck searching for opened handles, use Desktop now */
333 pwstrDesktop
= L
"Default";
338 swprintf(wstrWinstaFullName
, L
"%wZ\\%ws", &gustrWindowStationsDir
, pwstrWinsta
);
339 RtlInitUnicodeString( &ObjectName
, wstrWinstaFullName
);
341 TRACE("parsed initial winsta: %wZ\n", &ObjectName
);
343 /* Open the window station */
344 InitializeObjectAttributes(&ObjectAttributes
,
346 OBJ_CASE_INSENSITIVE
,
350 Status
= ObOpenObjectByName(&ObjectAttributes
,
351 ExWindowStationObjectType
,
358 if(!NT_SUCCESS(Status
))
360 SetLastNtError(Status
);
361 ERR("Failed to reference window station %wZ PID: --!\n", &ObjectName
);
366 if(*hDesktop
== NULL
)
368 RtlInitUnicodeString(&ObjectName
, pwstrDesktop
);
370 TRACE("parsed initial desktop: %wZ\n", &ObjectName
);
372 /* Open the desktop object */
373 InitializeObjectAttributes(&ObjectAttributes
,
375 OBJ_CASE_INSENSITIVE
,
379 Status
= ObOpenObjectByName(&ObjectAttributes
,
387 if(!NT_SUCCESS(Status
))
392 SetLastNtError(Status
);
393 ERR("Failed to reference desktop %wZ PID: --!\n", &ObjectName
);
397 return STATUS_SUCCESS
;
401 * IntValidateDesktopHandle
403 * Validates the desktop handle.
406 * If the function succeeds, the handle remains referenced. If the
407 * fucntion fails, last error is set.
411 IntValidateDesktopHandle(
413 KPROCESSOR_MODE AccessMode
,
414 ACCESS_MASK DesiredAccess
,
419 Status
= ObReferenceObjectByHandle(
427 TRACE("IntValidateDesktopHandle: handle:0x%p obj:0x%p access:0x%x Status:0x%lx\n",
428 Desktop
, *Object
, DesiredAccess
, Status
);
430 if (!NT_SUCCESS(Status
))
431 SetLastNtError(Status
);
437 IntGetActiveDesktop(VOID
)
439 return gpdeskInputDesktop
;
443 * Returns or creates a handle to the desktop object
446 IntGetDesktopObjectHandle(PDESKTOP DesktopObject
)
451 ASSERT(DesktopObject
);
453 if (!ObFindHandleForObject(PsGetCurrentProcess(),
459 Status
= ObOpenObjectByPointer(DesktopObject
,
466 if(!NT_SUCCESS(Status
))
468 /* Unable to create a handle */
469 ERR("Unable to create a desktop handle\n");
475 ERR("Got handle: %p\n", Ret
);
481 PUSER_MESSAGE_QUEUE FASTCALL
482 IntGetFocusMessageQueue(VOID
)
484 PDESKTOP pdo
= IntGetActiveDesktop();
487 TRACE("No active desktop\n");
490 return (PUSER_MESSAGE_QUEUE
)pdo
->ActiveMessageQueue
;
494 IntSetFocusMessageQueue(PUSER_MESSAGE_QUEUE NewQueue
)
496 PUSER_MESSAGE_QUEUE Old
;
497 PDESKTOP pdo
= IntGetActiveDesktop();
500 TRACE("No active desktop\n");
505 if(NewQueue
->Desktop
!= NULL
)
507 TRACE("Message Queue already attached to another desktop!\n");
510 IntReferenceMessageQueue(NewQueue
);
511 (void)InterlockedExchangePointer((PVOID
*)&NewQueue
->Desktop
, pdo
);
513 Old
= (PUSER_MESSAGE_QUEUE
)InterlockedExchangePointer((PVOID
*)&pdo
->ActiveMessageQueue
, NewQueue
);
516 (void)InterlockedExchangePointer((PVOID
*)&Old
->Desktop
, 0);
517 IntDereferenceMessageQueue(Old
);
518 gpqForegroundPrev
= Old
;
520 // Only one Q can have active foreground even when there are more than one desktop.
523 gpqForeground
= pdo
->ActiveMessageQueue
;
527 gpqForeground
= NULL
;
528 ERR("ptiLastInput is CLEARED!!\n");
529 ptiLastInput
= NULL
; // ReactOS hacks,,,, should check for process death.
534 IntGetThreadDesktopWindow(PTHREADINFO pti
)
536 if (!pti
) pti
= PsGetCurrentThreadWin32Thread();
537 if (pti
->pDeskInfo
) return pti
->pDeskInfo
->spwnd
;
541 PWND FASTCALL
co_GetDesktopWindow(PWND pWnd
)
543 if (pWnd
->head
.rpdesk
&&
544 pWnd
->head
.rpdesk
->pDeskInfo
)
545 return pWnd
->head
.rpdesk
->pDeskInfo
->spwnd
;
549 HWND FASTCALL
IntGetDesktopWindow(VOID
)
551 PDESKTOP pdo
= IntGetActiveDesktop();
554 TRACE("No active desktop\n");
557 return pdo
->DesktopWindow
;
560 PWND FASTCALL
UserGetDesktopWindow(VOID
)
562 PDESKTOP pdo
= IntGetActiveDesktop();
566 TRACE("No active desktop\n");
569 // return pdo->pDeskInfo->spwnd;
570 return UserGetWindowObject(pdo
->DesktopWindow
);
573 HWND FASTCALL
IntGetMessageWindow(VOID
)
575 PDESKTOP pdo
= IntGetActiveDesktop();
579 TRACE("No active desktop\n");
582 return pdo
->spwndMessage
->head
.h
;
585 PWND FASTCALL
UserGetMessageWindow(VOID
)
587 PDESKTOP pdo
= IntGetActiveDesktop();
591 TRACE("No active desktop\n");
594 return pdo
->spwndMessage
;
597 HWND FASTCALL
IntGetCurrentThreadDesktopWindow(VOID
)
599 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
600 PDESKTOP pdo
= pti
->rpdesk
;
603 ERR("Thread doesn't have a desktop\n");
606 return pdo
->DesktopWindow
;
609 /* PUBLIC FUNCTIONS ***********************************************************/
612 DesktopWindowProc(PWND Wnd
, UINT Msg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*lResult
)
616 //ERR("DesktopWindowProc\n");
625 Wnd
->fnid
= FNID_DESKTOP
;
627 *lResult
= (LRESULT
)TRUE
;
631 Value
= HandleToULong(PsGetCurrentProcessId());
633 co_UserSetWindowLong(UserHMGetHandle(Wnd
), DT_GWL_PROCESSID
, Value
, FALSE
);
634 Value
= HandleToULong(PsGetCurrentThreadId());
636 co_UserSetWindowLong(UserHMGetHandle(Wnd
), DT_GWL_THREADID
, Value
, FALSE
);
640 case WM_DISPLAYCHANGE
:
641 co_WinPosSetWindowPos(Wnd
, 0, 0, 0, LOWORD(lParam
), HIWORD(lParam
), SWP_NOZORDER
| SWP_NOACTIVATE
);
645 IntPaintDesktop((HDC
)wParam
);
651 if (IntBeginPaint(Wnd
, &Ps
))
653 IntEndPaint(Wnd
, &Ps
);
657 case WM_SYSCOLORCHANGE
:
658 co_UserRedrawWindow(Wnd
, NULL
, NULL
, RDW_INVALIDATE
|RDW_ERASE
|RDW_ALLCHILDREN
);
662 PCURICON_OBJECT pcurOld
, pcurNew
;
663 pcurNew
= UserGetCurIconObject(gDesktopCursor
);
668 pcurOld
= UserSetCursor(pcurNew
, FALSE
);
671 UserDereferenceObject(pcurOld
);
676 case WM_WINDOWPOSCHANGING
:
678 PWINDOWPOS pWindowPos
= (PWINDOWPOS
)lParam
;
679 if((pWindowPos
->flags
& SWP_SHOWWINDOW
) != 0)
681 HDESK hdesk
= IntGetDesktopObjectHandle(gpdeskInputDesktop
);
682 IntSetThreadDesktop(hdesk
, FALSE
);
687 return TRUE
; /* We are done. Do not do any callbacks to user mode */
691 UserMessageWindowProc(PWND pwnd
, UINT Msg
, WPARAM wParam
, LPARAM lParam
, LRESULT
*lResult
)
698 pwnd
->fnid
|= FNID_MESSAGEWND
;
699 *lResult
= (LRESULT
)TRUE
;
702 pwnd
->fnid
|= FNID_DESTROY
;
706 return TRUE
; /* We are done. Do not do any callbacks to user mode */
709 VOID NTAPI
DesktopThreadMain()
714 gptiDesktopThread
= PsGetCurrentThreadWin32Thread();
716 UserEnterExclusive();
718 /* Register system classes. This thread does not belong to any desktop so the
719 classes will be allocated from the shared heap */
720 UserRegisterSystemClasses();
724 Ret
= co_IntGetPeekMessage(&Msg
, 0, 0, 0, PM_REMOVE
, TRUE
);
727 IntDispatchMessage(&Msg
);
735 UserGetDesktopDC(ULONG DcType
, BOOL EmptyDC
, BOOL ValidatehWnd
)
737 PWND DesktopObject
= 0;
740 if (DcType
== DC_TYPE_DIRECT
)
742 DesktopObject
= UserGetDesktopWindow();
743 DesktopHDC
= (HDC
)UserGetWindowDC(DesktopObject
);
747 PMONITOR pMonitor
= UserGetPrimaryMonitor();
748 DesktopHDC
= IntGdiCreateDisplayDC(pMonitor
->hDev
, DcType
, EmptyDC
);
760 Window
= UserGetDesktopWindow();
761 hRgn
= IntSysCreateRectRgnIndirect(&Window
->rcWindow
);
763 IntInvalidateWindows( Window
,
770 GreDeleteObject(hRgn
);
775 co_IntShowDesktop(PDESKTOP Desktop
, ULONG Width
, ULONG Height
, BOOL bRedraw
)
777 PWND pwnd
= Desktop
->pDeskInfo
->spwnd
;
778 UINT flags
= SWP_NOACTIVATE
|SWP_NOZORDER
|SWP_SHOWWINDOW
;
782 flags
|= SWP_NOREDRAW
;
784 co_WinPosSetWindowPos(pwnd
, NULL
, 0, 0, Width
, Height
, flags
);
787 co_UserRedrawWindow( pwnd
, NULL
, 0, RDW_UPDATENOW
| RDW_ALLCHILDREN
| RDW_INVALIDATE
);
789 return STATUS_SUCCESS
;
793 IntHideDesktop(PDESKTOP Desktop
)
797 DesktopWnd
= IntGetWindowObject(Desktop
->DesktopWindow
);
800 return ERROR_INVALID_WINDOW_HANDLE
;
802 DesktopWnd
->style
&= ~WS_VISIBLE
;
804 return STATUS_SUCCESS
;
809 UserBuildShellHookHwndList(PDESKTOP Desktop
)
812 PSHELL_HOOK_WINDOW Current
;
815 /* FIXME: If we save nb elements in desktop, we dont have to loop to find nb entries */
816 LIST_FOR_EACH(Current
, &Desktop
->ShellHookWindows
, SHELL_HOOK_WINDOW
, ListEntry
)
819 if (!entries
) return NULL
;
821 list
= ExAllocatePoolWithTag(PagedPool
, sizeof(HWND
) * (entries
+ 1), USERTAG_WINDOWLIST
); /* alloc one extra for nullterm */
826 LIST_FOR_EACH(Current
, &Desktop
->ShellHookWindows
, SHELL_HOOK_WINDOW
, ListEntry
)
827 *cursor
++ = Current
->hWnd
;
829 *cursor
= NULL
; /* Nullterm list */
836 * Send the Message to the windows registered for ShellHook
837 * notifications. The lParam contents depend on the Message. See
838 * MSDN for more details (RegisterShellHookWindow)
840 VOID
co_IntShellHookNotify(WPARAM Message
, WPARAM wParam
, LPARAM lParam
)
842 PDESKTOP Desktop
= IntGetActiveDesktop();
845 if (!gpsi
->uiShellMsg
)
847 gpsi
->uiShellMsg
= IntAddAtom(L
"SHELLHOOK");
849 TRACE("MsgType = %x\n", gpsi
->uiShellMsg
);
850 if (!gpsi
->uiShellMsg
)
851 ERR("LastError: %x\n", EngGetLastError());
856 TRACE("IntShellHookNotify: No desktop!\n");
860 // Allow other devices have a shot at foreground.
861 if (Message
== HSHELL_APPCOMMAND
) ptiLastInput
= NULL
;
863 // FIXME: System Tray Support.
865 HwndList
= UserBuildShellHookHwndList(Desktop
);
868 HWND
* cursor
= HwndList
;
870 for (; *cursor
; cursor
++)
872 TRACE("Sending notify\n");
873 UserPostMessage(*cursor
,
876 (Message
== HSHELL_LANGUAGE
? lParam
: (LPARAM
)wParam
) );
877 /* co_IntPostOrSendMessage(*cursor,
880 (Message == HSHELL_LANGUAGE ? lParam : (LPARAM)wParam) );*/
883 ExFreePoolWithTag(HwndList
, USERTAG_WINDOWLIST
);
886 if (ISITHOOKED(WH_SHELL
))
888 co_HOOK_CallHooks(WH_SHELL
, Message
, wParam
, lParam
);
893 * Add the window to the ShellHookWindows list. The windows
894 * on that list get notifications that are important to shell
897 * TODO: Validate the window? I'm not sure if sending these messages to
898 * an unsuspecting application that is not your own is a nice thing to do.
900 BOOL
IntRegisterShellHookWindow(HWND hWnd
)
902 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
903 PDESKTOP Desktop
= pti
->rpdesk
;
904 PSHELL_HOOK_WINDOW Entry
;
906 TRACE("IntRegisterShellHookWindow\n");
908 /* First deregister the window, so we can be sure it's never twice in the
911 IntDeRegisterShellHookWindow(hWnd
);
913 Entry
= ExAllocatePoolWithTag(PagedPool
,
914 sizeof(SHELL_HOOK_WINDOW
),
922 InsertTailList(&Desktop
->ShellHookWindows
, &Entry
->ListEntry
);
928 * Remove the window from the ShellHookWindows list. The windows
929 * on that list get notifications that are important to shell
932 BOOL
IntDeRegisterShellHookWindow(HWND hWnd
)
934 PTHREADINFO pti
= PsGetCurrentThreadWin32Thread();
935 PDESKTOP Desktop
= pti
->rpdesk
;
936 PSHELL_HOOK_WINDOW Current
;
938 LIST_FOR_EACH(Current
, &Desktop
->ShellHookWindows
, SHELL_HOOK_WINDOW
, ListEntry
)
940 if (Current
->hWnd
== hWnd
)
942 RemoveEntryList(&Current
->ListEntry
);
943 ExFreePoolWithTag(Current
, TAG_WINSTA
);
952 IntFreeDesktopHeap(IN OUT PDESKTOP Desktop
)
954 /* FIXME: Disable until unmapping works in mm */
956 if (Desktop
->pheapDesktop
!= NULL
)
958 MmUnmapViewInSessionSpace(Desktop
->pheapDesktop
);
959 Desktop
->pheapDesktop
= NULL
;
962 if (Desktop
->hsectionDesktop
!= NULL
)
964 ObDereferenceObject(Desktop
->hsectionDesktop
);
965 Desktop
->hsectionDesktop
= NULL
;
971 IntPaintDesktop(HDC hDC
)
974 HBRUSH DesktopBrush
, PreviousBrush
;
976 BOOL doPatBlt
= TRUE
;
978 static WCHAR s_wszSafeMode
[] = L
"Safe Mode";
984 GdiGetClipBox(hDC
, &Rect
);
986 hWndDesktop
= IntGetDesktopWindow(); // rpdesk->DesktopWindow;
988 WndDesktop
= UserGetWindowObject(hWndDesktop
); // rpdesk->pDeskInfo->spwnd;
994 if (!UserGetSystemMetrics(SM_CLEANBOOT
))
996 DesktopBrush
= (HBRUSH
)WndDesktop
->pcls
->hbrBackground
;
999 * Paint desktop background
1001 if (gspv
.hbmWallpaper
!= NULL
)
1007 sz
.cx
= WndDesktop
->rcWindow
.right
- WndDesktop
->rcWindow
.left
;
1008 sz
.cy
= WndDesktop
->rcWindow
.bottom
- WndDesktop
->rcWindow
.top
;
1010 if (gspv
.WallpaperMode
== wmStretch
||
1011 gspv
.WallpaperMode
== wmTile
)
1018 /* Find the upper left corner, can be negative if the bitmap is bigger then the screen */
1019 x
= (sz
.cx
/ 2) - (gspv
.cxWallpaper
/ 2);
1020 y
= (sz
.cy
/ 2) - (gspv
.cyWallpaper
/ 2);
1023 hWallpaperDC
= NtGdiCreateCompatibleDC(hDC
);
1024 if(hWallpaperDC
!= NULL
)
1028 /* Fill in the area that the bitmap is not going to cover */
1031 /* FIXME: Clip out the bitmap
1032 can be replaced with "NtGdiPatBlt(hDC, x, y, WinSta->cxWallpaper, WinSta->cyWallpaper, PATCOPY | DSTINVERT);"
1033 once we support DSTINVERT */
1034 PreviousBrush
= NtGdiSelectBrush(hDC
, DesktopBrush
);
1035 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
1036 NtGdiSelectBrush(hDC
, PreviousBrush
);
1039 /* Do not fill the background after it is painted no matter the size of the picture */
1042 hOldBitmap
= NtGdiSelectBitmap(hWallpaperDC
, gspv
.hbmWallpaper
);
1044 if (gspv
.WallpaperMode
== wmStretch
)
1046 if(Rect
.right
&& Rect
.bottom
)
1047 NtGdiStretchBlt(hDC
,
1061 else if (gspv
.WallpaperMode
== wmTile
)
1063 /* Paint the bitmap across the screen then down */
1064 for(y
= 0; y
< Rect
.bottom
; y
+= gspv
.cyWallpaper
)
1066 for(x
= 0; x
< Rect
.right
; x
+= gspv
.cxWallpaper
)
1096 NtGdiSelectBitmap(hWallpaperDC
, hOldBitmap
);
1097 NtGdiDeleteObjectApp(hWallpaperDC
);
1103 /* Black desktop background in Safe Mode */
1104 DesktopBrush
= StockObjects
[BLACK_BRUSH
];
1106 /* Back ground is set to none, clear the screen */
1109 PreviousBrush
= NtGdiSelectBrush(hDC
, DesktopBrush
);
1110 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
1111 NtGdiSelectBrush(hDC
, PreviousBrush
);
1115 * Display system version on the desktop background
1118 if (g_PaintDesktopVersion
|| UserGetSystemMetrics(SM_CLEANBOOT
))
1120 static WCHAR s_wszVersion
[256] = {0};
1125 len
= wcslen(s_wszVersion
);
1129 len
= GetSystemVersionString(s_wszVersion
);
1134 if (!UserSystemParametersInfo(SPI_GETWORKAREA
, 0, &rect
, 0))
1136 rect
.right
= UserGetSystemMetrics(SM_CXSCREEN
);
1137 rect
.bottom
= UserGetSystemMetrics(SM_CYSCREEN
);
1140 color_old
= IntGdiSetTextColor(hDC
, RGB(255,255,255));
1141 align_old
= IntGdiSetTextAlign(hDC
, TA_RIGHT
);
1142 mode_old
= IntGdiSetBkMode(hDC
, TRANSPARENT
);
1144 if(!UserGetSystemMetrics(SM_CLEANBOOT
))
1146 GreExtTextOutW(hDC
, rect
.right
- 16, rect
.bottom
- 48, 0, NULL
, s_wszVersion
, len
, NULL
, 0);
1152 /* Version information text in top center */
1153 IntGdiSetTextAlign(hDC
, TA_CENTER
| TA_TOP
);
1154 GreExtTextOutW(hDC
, (rect
.right
+ rect
.left
)/2, rect
.top
+ 3, 0, NULL
, s_wszVersion
, len
, NULL
, 0);
1156 /* Safe Mode text in corners */
1157 len
= wcslen(s_wszSafeMode
);
1158 IntGdiSetTextAlign(hDC
, TA_LEFT
| TA_TOP
);
1159 GreExtTextOutW(hDC
, rect
.left
, rect
.top
+ 3, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1160 IntGdiSetTextAlign(hDC
, TA_RIGHT
| TA_TOP
);
1161 GreExtTextOutW(hDC
, rect
.right
, rect
.top
+ 3, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1162 IntGdiSetTextAlign(hDC
, TA_LEFT
| TA_BASELINE
);
1163 GreExtTextOutW(hDC
, rect
.left
, rect
.bottom
- 5, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1164 IntGdiSetTextAlign(hDC
, TA_RIGHT
| TA_BASELINE
);
1165 GreExtTextOutW(hDC
, rect
.right
, rect
.bottom
- 5, 0, NULL
, s_wszSafeMode
, len
, NULL
, 0);
1168 IntGdiSetBkMode(hDC
, mode_old
);
1169 IntGdiSetTextAlign(hDC
, align_old
);
1170 IntGdiSetTextColor(hDC
, color_old
);
1177 UserInitializeDesktop(PDESKTOP pdesk
, PUNICODE_STRING DesktopName
, PWINSTATION_OBJECT pwinsta
)
1179 PVOID DesktopHeapSystemBase
= NULL
;
1180 ULONG_PTR HeapSize
= 400 * 1024;
1181 SIZE_T DesktopInfoSize
;
1184 TRACE("UserInitializeDesktop desktop 0x%p with name %wZ\n", pdesk
, DesktopName
);
1186 RtlZeroMemory(pdesk
, sizeof(DESKTOP
));
1188 /* Link the desktop with the parent window station */
1189 pdesk
->rpwinstaParent
= pwinsta
;
1190 InsertTailList(&pwinsta
->DesktopListHead
, &pdesk
->ListEntry
);
1192 /* Create the desktop heap */
1193 pdesk
->hsectionDesktop
= NULL
;
1194 pdesk
->pheapDesktop
= UserCreateHeap(&pdesk
->hsectionDesktop
,
1195 &DesktopHeapSystemBase
,
1197 if (pdesk
->pheapDesktop
== NULL
)
1199 ERR("Failed to create desktop heap!\n");
1200 return STATUS_NO_MEMORY
;
1203 /* Create DESKTOPINFO */
1204 DesktopInfoSize
= sizeof(DESKTOPINFO
) + DesktopName
->Length
+ sizeof(WCHAR
);
1205 pdesk
->pDeskInfo
= RtlAllocateHeap(pdesk
->pheapDesktop
,
1206 HEAP_NO_SERIALIZE
| HEAP_ZERO_MEMORY
,
1208 if (pdesk
->pDeskInfo
== NULL
)
1210 ERR("Failed to create the DESKTOP structure!\n");
1211 return STATUS_NO_MEMORY
;
1214 /* Initialize the DESKTOPINFO */
1215 pdesk
->pDeskInfo
->pvDesktopBase
= DesktopHeapSystemBase
;
1216 pdesk
->pDeskInfo
->pvDesktopLimit
= (PVOID
)((ULONG_PTR
)DesktopHeapSystemBase
+ HeapSize
);
1217 RtlCopyMemory(pdesk
->pDeskInfo
->szDesktopName
,
1218 DesktopName
->Buffer
,
1219 DesktopName
->Length
+ sizeof(WCHAR
));
1220 for (i
= 0; i
< NB_HOOKS
; i
++)
1222 InitializeListHead(&pdesk
->pDeskInfo
->aphkStart
[i
]);
1225 InitializeListHead(&pdesk
->ShellHookWindows
);
1226 InitializeListHead(&pdesk
->PtiList
);
1228 return STATUS_SUCCESS
;
1231 /* SYSCALLS *******************************************************************/
1234 * NtUserCreateDesktop
1236 * Creates a new desktop.
1240 * Object Attributes.
1243 * Name of the device.
1249 * Interaction flags.
1252 * Requested type of access.
1256 * If the function succeeds, the return value is a handle to the newly
1257 * created desktop. If the specified desktop already exists, the function
1258 * succeeds and returns a handle to the existing desktop. When you are
1259 * finished using the handle, call the CloseDesktop function to close it.
1260 * If the function fails, the return value is NULL.
1267 NtUserCreateDesktop(
1268 POBJECT_ATTRIBUTES ObjectAttributes
,
1269 PUNICODE_STRING lpszDesktopDevice
,
1272 ACCESS_MASK dwDesiredAccess
)
1274 PDESKTOP pdesk
= NULL
;
1275 NTSTATUS Status
= STATUS_SUCCESS
;
1278 UNICODE_STRING ClassName
;
1279 LARGE_STRING WindowName
;
1280 BOOL NoHooks
= FALSE
;
1283 PTHREADINFO ptiCurrent
;
1286 DECLARE_RETURN(HDESK
);
1288 TRACE("Enter NtUserCreateDesktop\n");
1289 UserEnterExclusive();
1291 ptiCurrent
= PsGetCurrentThreadWin32Thread();
1293 ASSERT(gptiDesktopThread
);
1295 /* Turn off hooks when calling any CreateWindowEx from inside win32k. */
1296 NoHooks
= (ptiCurrent
->TIF_flags
& TIF_DISABLEHOOKS
);
1297 ptiCurrent
->TIF_flags
|= TIF_DISABLEHOOKS
;
1298 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
1301 * Try to open already existing desktop
1303 Status
= ObOpenObjectByName(
1305 ExDesktopObjectType
,
1311 if (!NT_SUCCESS(Status
))
1313 ERR("ObOpenObjectByName failed to open/create desktop\n");
1314 SetLastNtError(Status
);
1318 /* In case the object was not created (eg if it existed), return now */
1319 if (Context
== FALSE
)
1321 TRACE("NtUserCreateDesktop opened desktop %wZ\n", ObjectAttributes
->ObjectName
);
1325 /* Reference the desktop */
1326 Status
= ObReferenceObjectByHandle(hdesk
,
1328 ExDesktopObjectType
,
1332 if (!NT_SUCCESS(Status
))
1334 ERR("Failed to reference desktop object\n");
1335 SetLastNtError(Status
);
1339 /* Get the desktop window class. The thread desktop does not belong to any desktop
1340 * so the classes created there (including the desktop class) are allocated in the shared heap
1341 * It would cause problems if we used a class that belongs to the caller
1343 ClassName
.Buffer
= WC_DESKTOP
;
1344 ClassName
.Length
= 0;
1345 pcls
= IntGetAndReferenceClass(&ClassName
, 0, TRUE
);
1352 RtlZeroMemory(&WindowName
, sizeof(WindowName
));
1353 RtlZeroMemory(&Cs
, sizeof(Cs
));
1354 Cs
.x
= UserGetSystemMetrics(SM_XVIRTUALSCREEN
),
1355 Cs
.y
= UserGetSystemMetrics(SM_YVIRTUALSCREEN
),
1356 Cs
.cx
= UserGetSystemMetrics(SM_CXVIRTUALSCREEN
),
1357 Cs
.cy
= UserGetSystemMetrics(SM_CYVIRTUALSCREEN
),
1358 Cs
.style
= WS_POPUP
|WS_CLIPCHILDREN
;
1359 Cs
.hInstance
= hModClient
; // hModuleWin; // Server side winproc!
1360 Cs
.lpszName
= (LPCWSTR
) &WindowName
;
1361 Cs
.lpszClass
= (LPCWSTR
) &ClassName
;
1363 /* Use IntCreateWindow instead of co_UserCreateWindowEx cause the later expects a thread with a desktop */
1364 pWnd
= IntCreateWindow(&Cs
, &WindowName
, pcls
, NULL
, NULL
, NULL
, pdesk
);
1367 ERR("Failed to create desktop window for the new desktop\n");
1371 pdesk
->dwSessionId
= PsGetCurrentProcessSessionId();
1372 pdesk
->DesktopWindow
= pWnd
->head
.h
;
1373 pdesk
->pDeskInfo
->spwnd
= pWnd
;
1374 pWnd
->fnid
= FNID_DESKTOP
;
1376 ClassName
.Buffer
= MAKEINTATOM(gpsi
->atomSysClass
[ICLS_HWNDMESSAGE
]);
1377 ClassName
.Length
= 0;
1378 pcls
= IntGetAndReferenceClass(&ClassName
, 0, TRUE
);
1385 RtlZeroMemory(&WindowName
, sizeof(WindowName
));
1386 RtlZeroMemory(&Cs
, sizeof(Cs
));
1387 Cs
.cx
= Cs
.cy
= 100;
1388 Cs
.style
= WS_POPUP
|WS_CLIPCHILDREN
;
1389 Cs
.hInstance
= hModClient
; // hModuleWin; // Server side winproc!
1390 Cs
.lpszName
= (LPCWSTR
) &WindowName
;
1391 Cs
.lpszClass
= (LPCWSTR
) &ClassName
;
1392 pWnd
= IntCreateWindow(&Cs
, &WindowName
, pcls
, NULL
, NULL
, NULL
, pdesk
);
1395 ERR("Failed to create message window for the new desktop\n");
1399 pdesk
->spwndMessage
= pWnd
;
1400 pWnd
->fnid
= FNID_MESSAGEWND
;
1403 if !(WinStaObject->Flags & WSF_NOIO) is (not set) for desktop input output mode (see wiki)
1404 Create Tooltip. Saved in DesktopObject->spwndTooltip.
1405 Tooltip dwExStyle: WS_EX_TOOLWINDOW|WS_EX_TOPMOST
1406 hWndParent are spwndMessage. Use hModuleWin for server side winproc!
1407 The rest is same as message window.
1408 http://msdn.microsoft.com/en-us/library/bb760250(VS.85).aspx
1415 ObDereferenceObject(pdesk
);
1417 if (_ret_
== NULL
&& hdesk
!= NULL
)
1419 ObCloseHandle(hdesk
, UserMode
);
1423 ptiCurrent
->TIF_flags
&= ~TIF_DISABLEHOOKS
;
1424 ptiCurrent
->pClientInfo
->dwTIFlags
= ptiCurrent
->TIF_flags
;
1426 TRACE("Leave NtUserCreateDesktop, ret=%p\n",_ret_
);
1434 * Opens an existing desktop.
1438 * Name of the existing desktop.
1441 * Interaction flags.
1444 * Requested type of access.
1447 * Handle to the desktop or zero on failure.
1455 POBJECT_ATTRIBUTES ObjectAttributes
,
1457 ACCESS_MASK dwDesiredAccess
)
1462 Status
= ObOpenObjectByName(
1464 ExDesktopObjectType
,
1471 if (!NT_SUCCESS(Status
))
1473 ERR("Failed to open desktop\n");
1474 SetLastNtError(Status
);
1478 TRACE("Opened desktop %S with handle 0x%p\n", ObjectAttributes
->ObjectName
->Buffer
, Desktop
);
1484 * NtUserOpenInputDesktop
1486 * Opens the input (interactive) desktop.
1490 * Interaction flags.
1493 * Inheritance option.
1496 * Requested type of access.
1499 * Handle to the input desktop or zero on failure.
1506 NtUserOpenInputDesktop(
1509 ACCESS_MASK dwDesiredAccess
)
1513 ULONG HandleAttributes
= 0;
1515 UserEnterExclusive();
1516 TRACE("Enter NtUserOpenInputDesktop gpdeskInputDesktop 0x%p\n",gpdeskInputDesktop
);
1518 if(fInherit
) HandleAttributes
= OBJ_INHERIT
;
1520 /* Create a new handle to the object */
1521 Status
= ObOpenObjectByPointer(
1526 ExDesktopObjectType
,
1530 if (!NT_SUCCESS(Status
))
1532 ERR("Failed to open input desktop object\n");
1533 SetLastNtError(Status
);
1536 TRACE("NtUserOpenInputDesktop returning 0x%p\n",hdesk
);
1542 * NtUserCloseDesktop
1544 * Closes a desktop handle.
1548 * Handle to the desktop.
1554 * The desktop handle can be created with NtUserCreateDesktop or
1555 * NtUserOpenDesktop. This function will fail if any thread in the calling
1556 * process is using the specified desktop handle or if the handle refers
1557 * to the initial desktop of the calling process.
1564 NtUserCloseDesktop(HDESK hDesktop
)
1568 DECLARE_RETURN(BOOL
);
1570 TRACE("NtUserCloseDesktop called (0x%p)\n", hDesktop
);
1571 UserEnterExclusive();
1573 if( hDesktop
== gptiCurrent
->hdesk
|| hDesktop
== gptiCurrent
->ppi
->hdeskStartup
)
1575 ERR("Attempted to close thread desktop\n");
1576 EngSetLastError(ERROR_BUSY
);
1580 Status
= IntValidateDesktopHandle( hDesktop
, UserMode
, 0, &pdesk
);
1581 if (!NT_SUCCESS(Status
))
1583 ERR("Validation of desktop handle (0x%p) failed\n", hDesktop
);
1587 ObDereferenceObject(pdesk
);
1589 Status
= ZwClose(hDesktop
);
1590 if (!NT_SUCCESS(Status
))
1592 ERR("Failed to close desktop handle 0x%p\n", hDesktop
);
1593 SetLastNtError(Status
);
1600 TRACE("Leave NtUserCloseDesktop, ret=%i\n",_ret_
);
1606 * NtUserPaintDesktop
1608 * The NtUserPaintDesktop function fills the clipping region in the
1609 * specified device context with the desktop pattern or wallpaper. The
1610 * function is provided primarily for shell desktops.
1614 * Handle to the device context.
1621 NtUserPaintDesktop(HDC hDC
)
1624 UserEnterExclusive();
1625 TRACE("Enter NtUserPaintDesktop\n");
1626 Ret
= IntPaintDesktop(hDC
);
1627 TRACE("Leave NtUserPaintDesktop, ret=%i\n",Ret
);
1633 * NtUserSwitchDesktop
1635 * Sets the current input (interactive) desktop.
1639 * Handle to desktop.
1649 NtUserSwitchDesktop(HDESK hdesk
)
1653 BOOL bRedrawDesktop
;
1654 DECLARE_RETURN(BOOL
);
1656 UserEnterExclusive();
1657 TRACE("Enter NtUserSwitchDesktop(0x%p)\n", hdesk
);
1659 Status
= IntValidateDesktopHandle( hdesk
, UserMode
, 0, &pdesk
);
1660 if (!NT_SUCCESS(Status
))
1662 ERR("Validation of desktop handle (0x%p) failed\n", hdesk
);
1666 if (PsGetCurrentProcessSessionId() != pdesk
->rpwinstaParent
->dwSessionId
)
1668 ERR("NtUserSwitchDesktop called for a desktop of a different session\n");
1672 if(pdesk
== gpdeskInputDesktop
)
1674 WARN("NtUserSwitchDesktop called for active desktop\n");
1679 * Don't allow applications switch the desktop if it's locked, unless the caller
1680 * is the logon application itself
1682 if((pdesk
->rpwinstaParent
->Flags
& WSS_LOCKED
) &&
1683 LogonProcess
!= PsGetCurrentProcessWin32Process())
1685 ObDereferenceObject(pdesk
);
1686 ERR("Switching desktop 0x%p denied because the window station is locked!\n", hdesk
);
1690 if(pdesk
->rpwinstaParent
!= InputWindowStation
)
1692 ObDereferenceObject(pdesk
);
1693 ERR("Switching desktop 0x%p denied because desktop doesn't belong to the interactive winsta!\n", hdesk
);
1697 /* FIXME: Fail if the process is associated with a secured
1698 desktop such as Winlogon or Screen-Saver */
1699 /* FIXME: Connect to input device */
1701 TRACE("Switching from desktop 0x%p to 0x%p\n", gpdeskInputDesktop
, pdesk
);
1703 bRedrawDesktop
= FALSE
;
1705 /* The first time SwitchDesktop is called, gpdeskInputDesktop is NULL */
1706 if(gpdeskInputDesktop
!= NULL
)
1708 if((gpdeskInputDesktop
->pDeskInfo
->spwnd
->style
& WS_VISIBLE
) == WS_VISIBLE
)
1709 bRedrawDesktop
= TRUE
;
1711 /* Hide the previous desktop window */
1712 IntHideDesktop(gpdeskInputDesktop
);
1715 /* Set the active desktop in the desktop's window station. */
1716 InputWindowStation
->ActiveDesktop
= pdesk
;
1718 /* Set the global state. */
1719 gpdeskInputDesktop
= pdesk
;
1721 /* Show the new desktop window */
1722 co_IntShowDesktop(pdesk
, UserGetSystemMetrics(SM_CXSCREEN
), UserGetSystemMetrics(SM_CYSCREEN
), bRedrawDesktop
);
1724 TRACE("SwitchDesktop gpdeskInputDesktop 0x%p\n",gpdeskInputDesktop
);
1725 ObDereferenceObject(pdesk
);
1730 TRACE("Leave NtUserSwitchDesktop, ret=%i\n",_ret_
);
1736 * NtUserGetThreadDesktop
1743 NtUserGetThreadDesktop(DWORD dwThreadId
, DWORD Unknown1
)
1747 PDESKTOP DesktopObject
;
1748 HDESK Ret
, hThreadDesktop
;
1749 OBJECT_HANDLE_INFORMATION HandleInformation
;
1750 DECLARE_RETURN(HDESK
);
1752 UserEnterExclusive();
1753 TRACE("Enter NtUserGetThreadDesktop\n");
1757 EngSetLastError(ERROR_INVALID_PARAMETER
);
1761 Status
= PsLookupThreadByThreadId((HANDLE
)(DWORD_PTR
)dwThreadId
, &Thread
);
1762 if(!NT_SUCCESS(Status
))
1764 EngSetLastError(ERROR_INVALID_PARAMETER
);
1768 if(Thread
->ThreadsProcess
== PsGetCurrentProcess())
1770 /* Just return the handle, we queried the desktop handle of a thread running
1771 in the same context */
1772 Ret
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->hdesk
;
1773 ObDereferenceObject(Thread
);
1777 /* Get the desktop handle and the desktop of the thread */
1778 if(!(hThreadDesktop
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->hdesk
) ||
1779 !(DesktopObject
= ((PTHREADINFO
)Thread
->Tcb
.Win32Thread
)->rpdesk
))
1781 ObDereferenceObject(Thread
);
1782 ERR("Desktop information of thread 0x%x broken!?\n", dwThreadId
);
1786 /* We could just use DesktopObject instead of looking up the handle, but latter
1787 may be a bit safer (e.g. when the desktop is being destroyed */
1788 /* Switch into the context of the thread we're trying to get the desktop from,
1789 so we can use the handle */
1790 KeAttachProcess(&Thread
->ThreadsProcess
->Pcb
);
1791 Status
= ObReferenceObjectByHandle(hThreadDesktop
,
1793 ExDesktopObjectType
,
1795 (PVOID
*)&DesktopObject
,
1796 &HandleInformation
);
1799 /* The handle couldn't be found, there's nothing to get... */
1800 if(!NT_SUCCESS(Status
))
1802 ObDereferenceObject(Thread
);
1806 /* Lookup our handle table if we can find a handle to the desktop object,
1807 if not, create one */
1808 Ret
= IntGetDesktopObjectHandle(DesktopObject
);
1810 /* All done, we got a valid handle to the desktop */
1811 ObDereferenceObject(DesktopObject
);
1812 ObDereferenceObject(Thread
);
1816 TRACE("Leave NtUserGetThreadDesktop, ret=%p\n",_ret_
);
1822 IntUnmapDesktopView(IN PDESKTOP pdesk
)
1825 PW32HEAP_USER_MAPPING HeapMapping
, *PrevLink
;
1826 NTSTATUS Status
= STATUS_SUCCESS
;
1828 TRACE("IntUnmapDesktopView called for desktop object %p\n", pdesk
);
1830 ppi
= PsGetCurrentProcessWin32Process();
1831 PrevLink
= &ppi
->HeapMappings
.Next
;
1833 /* Unmap if we're the last thread using the desktop */
1834 HeapMapping
= *PrevLink
;
1835 while (HeapMapping
!= NULL
)
1837 if (HeapMapping
->KernelMapping
== (PVOID
)pdesk
->pheapDesktop
)
1839 if (--HeapMapping
->Count
== 0)
1841 *PrevLink
= HeapMapping
->Next
;
1843 TRACE("ppi 0x%p unmapped heap of desktop 0x%p\n", ppi
, pdesk
);
1844 Status
= MmUnmapViewOfSection(PsGetCurrentProcess(),
1845 HeapMapping
->UserMapping
);
1847 ObDereferenceObject(pdesk
);
1849 UserHeapFree(HeapMapping
);
1854 PrevLink
= &HeapMapping
->Next
;
1855 HeapMapping
= HeapMapping
->Next
;
1862 IntMapDesktopView(IN PDESKTOP pdesk
)
1865 PW32HEAP_USER_MAPPING HeapMapping
, *PrevLink
;
1866 PVOID UserBase
= NULL
;
1867 SIZE_T ViewSize
= 0;
1868 LARGE_INTEGER Offset
;
1871 TRACE("IntMapDesktopView called for desktop object 0x%p\n", pdesk
);
1873 ppi
= PsGetCurrentProcessWin32Process();
1874 PrevLink
= &ppi
->HeapMappings
.Next
;
1876 /* Find out if another thread already mapped the desktop heap */
1877 HeapMapping
= *PrevLink
;
1878 while (HeapMapping
!= NULL
)
1880 if (HeapMapping
->KernelMapping
== (PVOID
)pdesk
->pheapDesktop
)
1882 HeapMapping
->Count
++;
1883 return STATUS_SUCCESS
;
1886 PrevLink
= &HeapMapping
->Next
;
1887 HeapMapping
= HeapMapping
->Next
;
1890 /* We're the first, map the heap */
1891 Offset
.QuadPart
= 0;
1892 Status
= MmMapViewOfSection(pdesk
->hsectionDesktop
,
1893 PsGetCurrentProcess(),
1901 PAGE_EXECUTE_READ
); /* Would prefer PAGE_READONLY, but thanks to RTL heaps... */
1902 if (!NT_SUCCESS(Status
))
1904 ERR("Failed to map desktop\n");
1908 TRACE("ppi 0x%p mapped heap of desktop 0x%p\n", ppi
, pdesk
);
1910 /* Add the mapping */
1911 HeapMapping
= UserHeapAlloc(sizeof(W32HEAP_USER_MAPPING
));
1912 if (HeapMapping
== NULL
)
1914 MmUnmapViewOfSection(PsGetCurrentProcess(), UserBase
);
1915 ERR("UserHeapAlloc() failed!\n");
1916 return STATUS_NO_MEMORY
;
1919 HeapMapping
->Next
= NULL
;
1920 HeapMapping
->KernelMapping
= (PVOID
)pdesk
->pheapDesktop
;
1921 HeapMapping
->UserMapping
= UserBase
;
1922 HeapMapping
->Limit
= ViewSize
;
1923 HeapMapping
->Count
= 1;
1924 *PrevLink
= HeapMapping
;
1926 ObReferenceObject(pdesk
);
1928 return STATUS_SUCCESS
;
1932 IntSetThreadDesktop(IN HDESK hDesktop
,
1933 IN BOOL FreeOnFailure
)
1935 PDESKTOP pdesk
= NULL
, pdeskOld
;
1939 PCLIENTTHREADINFO pctiOld
, pctiNew
= NULL
;
1942 ASSERT(NtCurrentTeb());
1944 TRACE("IntSetThreadDesktop hDesktop:0x%p, FOF:%i\n",hDesktop
, FreeOnFailure
);
1946 pti
= PsGetCurrentThreadWin32Thread();
1947 pci
= pti
->pClientInfo
;
1949 /* If the caller gave us a desktop, ensure it is valid */
1950 if(hDesktop
!= NULL
)
1952 /* Validate the new desktop. */
1953 Status
= IntValidateDesktopHandle( hDesktop
, UserMode
, 0, &pdesk
);
1954 if (!NT_SUCCESS(Status
))
1956 ERR("Validation of desktop handle (0x%p) failed\n", hDesktop
);
1960 if (pti
->rpdesk
== pdesk
)
1963 ObDereferenceObject(pdesk
);
1968 /* Make sure that we don't own any window in the current desktop */
1969 if (!IsListEmpty(&pti
->WindowListHead
))
1972 ObDereferenceObject(pdesk
);
1973 ERR("Attempted to change thread desktop although the thread has windows!\n");
1974 EngSetLastError(ERROR_BUSY
);
1978 /* Desktop is being re-set so clear out foreground. */
1979 if (pti
->rpdesk
!= pdesk
&& pti
->MessageQueue
== gpqForeground
)
1981 // Like above, there shouldn't be any windows, hooks or anything active on this threads desktop!
1982 IntSetFocusMessageQueue(NULL
);
1985 /* Before doing the switch, map the new desktop heap and allocate the new pcti */
1988 Status
= IntMapDesktopView(pdesk
);
1989 if (!NT_SUCCESS(Status
))
1991 ERR("Failed to map desktop heap!\n");
1992 ObDereferenceObject(pdesk
);
1993 SetLastNtError(Status
);
1997 pctiNew
= DesktopHeapAlloc( pdesk
, sizeof(CLIENTTHREADINFO
));
2000 ERR("Failed to allocate new pcti\n");
2001 IntUnmapDesktopView(pdesk
);
2002 ObDereferenceObject(pdesk
);
2003 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY
);
2008 /* free all classes or move them to the shared heap */
2009 if(pti
->rpdesk
!= NULL
)
2011 if(!IntCheckProcessDesktopClasses(pti
->rpdesk
, FreeOnFailure
))
2013 ERR("Failed to move process classes to shared heap!\n");
2016 DesktopHeapFree(pdesk
, pctiNew
);
2017 IntUnmapDesktopView(pdesk
);
2018 ObDereferenceObject(pdesk
);
2024 pdeskOld
= pti
->rpdesk
;
2025 hdeskOld
= pti
->hdesk
;
2026 if (pti
->pcti
!= &pti
->cti
)
2027 pctiOld
= pti
->pcti
;
2034 pti
->rpdesk
= pdesk
;
2035 pti
->hdesk
= hDesktop
;
2036 pti
->pDeskInfo
= pti
->rpdesk
->pDeskInfo
;
2037 pti
->pcti
= pctiNew
;
2039 pci
->ulClientDelta
= DesktopHeapGetUserDelta();
2040 pci
->pDeskInfo
= (PVOID
)((ULONG_PTR
)pti
->pDeskInfo
- pci
->ulClientDelta
);
2041 pci
->pClientThreadInfo
= (PVOID
)((ULONG_PTR
)pti
->pcti
- pci
->ulClientDelta
);
2043 /* initialize the new pcti */
2046 RtlCopyMemory(pctiNew
, pctiOld
, sizeof(CLIENTTHREADINFO
));
2050 RtlZeroMemory(pctiNew
, sizeof(CLIENTTHREADINFO
));
2051 pci
->fsHooks
= pti
->fsHooks
;
2052 pci
->dwTIFlags
= pti
->TIF_flags
;
2059 pti
->pDeskInfo
= NULL
;
2060 pti
->pcti
= &pti
->cti
; // Always point inside so there will be no crash when posting or sending msg's!
2061 pci
->ulClientDelta
= 0;
2062 pci
->pDeskInfo
= NULL
;
2063 pci
->pClientThreadInfo
= NULL
;
2066 /* clean up the old desktop */
2067 if(pdeskOld
!= NULL
)
2069 RemoveEntryList(&pti
->PtiLink
);
2070 if (pctiOld
) DesktopHeapFree(pdeskOld
, pctiOld
);
2071 IntUnmapDesktopView(pdeskOld
);
2072 ObDereferenceObject(pdeskOld
);
2078 InsertTailList(&pdesk
->PtiList
, &pti
->PtiLink
);
2081 TRACE("IntSetThreadDesktop: pti 0x%p ppi 0x%p switched from object 0x%p to 0x%p\n", pti
, pti
->ppi
, pdeskOld
, pdesk
);
2087 * NtUserSetThreadDesktop
2094 NtUserSetThreadDesktop(HDESK hDesktop
)
2098 UserEnterExclusive();
2100 // FIXME: IntSetThreadDesktop validates the desktop handle, it should happen
2101 // here too and set the NT error level. Q. Is it necessary to have the validation
2102 // in IntSetThreadDesktop? Is it needed there too?
2103 if (hDesktop
|| (!hDesktop
&& PsGetCurrentProcess() == gpepCSRSS
))
2104 ret
= IntSetThreadDesktop(hDesktop
, FALSE
);