2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
24 * FILE: subsys/win32k/ntuser/desktop.c
25 * PROGRAMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
27 * 06-06-2001 CSH Created
30 /* INCLUDES ******************************************************************/
34 /* not yet defined in w32api... */
36 ObFindHandleForObject(IN PEPROCESS Process
,
38 IN POBJECT_TYPE ObjectType
,
39 IN POBJECT_HANDLE_INFORMATION HandleInformation
,
42 #define ObFindHandleForObject(Process, Object, ObjectType, HandleInformation, Handle) \
46 /* GLOBALS *******************************************************************/
48 /* Currently active desktop */
49 PDESKTOP_OBJECT InputDesktop
= NULL
;
50 HDESK InputDesktopHandle
= NULL
;
51 HDC ScreenDeviceContext
= NULL
;
53 BOOL g_PaintDesktopVersion
= FALSE
;
55 /* INITALIZATION FUNCTIONS ****************************************************/
60 return STATUS_SUCCESS
;
64 CleanupDesktopImpl(VOID
)
66 return STATUS_SUCCESS
;
69 /* OBJECT CALLBACKS **********************************************************/
72 IntDesktopObjectCreate(PVOID ObjectBody
,
75 struct _OBJECT_ATTRIBUTES
* ObjectAttributes
)
77 PDESKTOP_OBJECT Desktop
= (PDESKTOP_OBJECT
)ObjectBody
;
78 UNICODE_STRING UnicodeString
;
80 DPRINT("Creating desktop (0x%X) Name (%S)\n", Desktop
, RemainingPath
);
81 if (RemainingPath
== NULL
)
83 return STATUS_SUCCESS
;
86 if (wcschr((RemainingPath
+ 1), '\\') != NULL
)
88 return STATUS_UNSUCCESSFUL
;
91 RtlInitUnicodeString(&UnicodeString
, (RemainingPath
+ 1));
95 KeInitializeSpinLock(&Desktop
->Lock
);
96 InitializeListHead(&Desktop
->ShellHookWindows
);
98 Desktop
->WindowStation
= (PWINSTATION_OBJECT
)Parent
;
100 /* Put the desktop on the window station's list of associcated desktops */
101 ExInterlockedInsertTailList(
102 &Desktop
->WindowStation
->DesktopListHead
,
104 &Desktop
->WindowStation
->Lock
);
106 return RtlCreateUnicodeString(&Desktop
->Name
, UnicodeString
.Buffer
);
110 IntDesktopObjectDelete(PVOID DeletedObject
)
112 PDESKTOP_OBJECT Desktop
= (PDESKTOP_OBJECT
)DeletedObject
;
115 DPRINT("Deleting desktop (0x%X)\n", Desktop
);
117 /* Remove the desktop from the window station's list of associcated desktops */
118 KeAcquireSpinLock(&Desktop
->WindowStation
->Lock
, &OldIrql
);
119 RemoveEntryList(&Desktop
->ListEntry
);
120 KeReleaseSpinLock(&Desktop
->WindowStation
->Lock
, OldIrql
);
122 RtlFreeUnicodeString(&Desktop
->Name
);
125 /* PRIVATE FUNCTIONS **********************************************************/
128 IntParseDesktopPath(PEPROCESS Process
,
129 PUNICODE_STRING DesktopPath
,
133 OBJECT_ATTRIBUTES ObjectAttributes
;
134 UNICODE_STRING WinSta
, Desktop
, FullName
;
135 BOOL DesktopPresent
= FALSE
;
136 BOOL WinStaPresent
= FALSE
;
148 RtlInitUnicodeString(&WinSta
, NULL
);
149 RtlInitUnicodeString(&Desktop
, NULL
);
151 if(DesktopPath
!= NULL
&& DesktopPath
->Buffer
!= NULL
&& DesktopPath
->Length
> sizeof(WCHAR
))
153 PWCHAR c
= DesktopPath
->Buffer
;
155 USHORT l
= DesktopPath
->Length
;
158 * Parse the desktop path string which can be in the form "WinSta\Desktop"
159 * or just "Desktop". In latter case WinSta0 will be used.
166 wl
= (ULONG_PTR
)c
- (ULONG_PTR
)DesktopPath
->Buffer
;
176 WinSta
.MaximumLength
= wl
+ sizeof(WCHAR
);
177 WinSta
.Buffer
= DesktopPath
->Buffer
;
179 WinStaPresent
= TRUE
;
183 Desktop
.Length
= DesktopPath
->Length
- wl
;
186 Desktop
.Length
-= sizeof(WCHAR
);
188 if(Desktop
.Length
> 0)
190 Desktop
.MaximumLength
= Desktop
.Length
+ sizeof(WCHAR
);
191 Desktop
.Buffer
= ((wl
> 0) ? c
: DesktopPath
->Buffer
);
192 DesktopPresent
= TRUE
;
198 /* search the process handle table for (inherited) window station
199 handles, use a more appropriate one than WinSta0 if possible. */
200 Status
= ObFindHandleForObject(Process
,
202 ExWindowStationObjectType
,
205 if(!NT_SUCCESS(Status
))
207 /* we had no luck searching for opened handles, use WinSta0 now */
208 RtlInitUnicodeString(&WinSta
, L
"WinSta0");
212 if(!DesktopPresent
&& hDesktop
!= NULL
)
214 /* search the process handle table for (inherited) desktop
215 handles, use a more appropriate one than Default if possible. */
216 Status
= ObFindHandleForObject(Process
,
221 if(!NT_SUCCESS(Status
))
223 /* we had no luck searching for opened handles, use Desktop now */
224 RtlInitUnicodeString(&Desktop
, L
"Default");
230 if(!IntGetFullWindowStationName(&FullName
, &WinSta
, NULL
))
232 return STATUS_INSUFFICIENT_RESOURCES
;
235 /* open the window station */
236 InitializeObjectAttributes(&ObjectAttributes
,
238 OBJ_CASE_INSENSITIVE
,
242 Status
= ObOpenObjectByName(&ObjectAttributes
,
243 ExWindowStationObjectType
,
250 RtlFreeUnicodeString(&FullName
);
252 if(!NT_SUCCESS(Status
))
254 SetLastNtError(Status
);
255 DPRINT("Failed to reference window station %wZ PID: %d!\n", &WinSta
, PsGetCurrentProcessId());
260 if(hDesktop
!= NULL
&& *hDesktop
== NULL
)
262 if(!IntGetFullWindowStationName(&FullName
, &WinSta
, &Desktop
))
266 return STATUS_INSUFFICIENT_RESOURCES
;
269 /* open the desktop object */
270 InitializeObjectAttributes(&ObjectAttributes
,
272 OBJ_CASE_INSENSITIVE
,
276 Status
= ObOpenObjectByName(&ObjectAttributes
,
284 RtlFreeUnicodeString(&FullName
);
286 if(!NT_SUCCESS(Status
))
291 SetLastNtError(Status
);
292 DPRINT("Failed to reference desktop %wZ PID: %d!\n", &Desktop
, PsGetCurrentProcessId());
297 return STATUS_SUCCESS
;
301 * IntValidateDesktopHandle
303 * Validates the desktop handle.
306 * If the function succeeds, the handle remains referenced. If the
307 * fucntion fails, last error is set.
311 IntValidateDesktopHandle(
313 KPROCESSOR_MODE AccessMode
,
314 ACCESS_MASK DesiredAccess
,
315 PDESKTOP_OBJECT
*Object
)
319 Status
= ObReferenceObjectByHandle(
327 if (!NT_SUCCESS(Status
))
328 SetLastNtError(Status
);
334 IntGetDesktopWorkArea(PDESKTOP_OBJECT Desktop
, PRECT Rect
)
340 Ret
= &Desktop
->WorkArea
;
341 if((Ret
->right
== -1) && ScreenDeviceContext
)
344 BITMAPOBJ
*BitmapObj
;
345 dc
= DC_LockDc(ScreenDeviceContext
);
346 /* FIXME - Handle dc == NULL!!!! */
347 BitmapObj
= BITMAPOBJ_LockBitmap(dc
->w
.hBitmap
);
350 Ret
->right
= BitmapObj
->SurfObj
.sizlBitmap
.cx
;
351 Ret
->bottom
= BitmapObj
->SurfObj
.sizlBitmap
.cy
;
352 BITMAPOBJ_UnlockBitmap(BitmapObj
);
363 PDESKTOP_OBJECT FASTCALL
364 IntGetActiveDesktop(VOID
)
370 * returns or creates a handle to the desktop object
373 IntGetDesktopObjectHandle(PDESKTOP_OBJECT DesktopObject
)
378 ASSERT(DesktopObject
);
380 Status
= ObFindHandleForObject(PsGetCurrentProcess(),
386 if(!NT_SUCCESS(Status
))
388 Status
= ObOpenObjectByPointer(DesktopObject
,
395 if(!NT_SUCCESS(Status
))
397 /* unable to create a handle */
398 DPRINT1("Unable to create a desktop handle\n");
406 PUSER_MESSAGE_QUEUE FASTCALL
407 IntGetFocusMessageQueue(VOID
)
409 PDESKTOP_OBJECT pdo
= IntGetActiveDesktop();
412 DPRINT("No active desktop\n");
415 return (PUSER_MESSAGE_QUEUE
)pdo
->ActiveMessageQueue
;
419 IntSetFocusMessageQueue(PUSER_MESSAGE_QUEUE NewQueue
)
421 PUSER_MESSAGE_QUEUE Old
;
422 PDESKTOP_OBJECT pdo
= IntGetActiveDesktop();
425 DPRINT("No active desktop\n");
430 if(NewQueue
->Desktop
!= NULL
)
432 DPRINT("Message Queue already attached to another desktop!\n");
435 IntReferenceMessageQueue(NewQueue
);
436 InterlockedExchange((LONG
*)&NewQueue
->Desktop
, (LONG
)pdo
);
438 Old
= (PUSER_MESSAGE_QUEUE
)InterlockedExchange((LONG
*)&pdo
->ActiveMessageQueue
, (LONG
)NewQueue
);
441 InterlockedExchange((LONG
*)&Old
->Desktop
, 0);
442 IntDereferenceMessageQueue(Old
);
446 HWND FASTCALL
IntGetDesktopWindow(VOID
)
448 PDESKTOP_OBJECT pdo
= IntGetActiveDesktop();
451 DPRINT("No active desktop\n");
454 return pdo
->DesktopWindow
;
457 HWND FASTCALL
IntGetCurrentThreadDesktopWindow(VOID
)
459 PDESKTOP_OBJECT pdo
= PsGetWin32Thread()->Desktop
;
462 DPRINT1("Thread doesn't have a desktop\n");
465 return pdo
->DesktopWindow
;
468 BOOL FASTCALL
IntDesktopUpdatePerUserSettings(BOOL bEnable
)
472 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
475 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
477 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
478 QueryTable
[0].Name
= L
"PaintDesktopVersion";
479 QueryTable
[0].EntryContext
= &g_PaintDesktopVersion
;
481 /* Query the "PaintDesktopVersion" flag in the "Control Panel\Desktop" key */
482 Status
= RtlQueryRegistryValues(RTL_REGISTRY_USER
,
483 L
"Control Panel\\Desktop",
484 QueryTable
, NULL
, NULL
);
485 if (!NT_SUCCESS(Status
))
487 DPRINT1("RtlQueryRegistryValues failed for PaintDesktopVersion (%x)\n",
489 g_PaintDesktopVersion
= FALSE
;
493 DPRINT("PaintDesktopVersion = %d\n", g_PaintDesktopVersion
);
499 g_PaintDesktopVersion
= FALSE
;
504 /* PUBLIC FUNCTIONS ***********************************************************/
507 IntShowDesktop(PDESKTOP_OBJECT Desktop
, ULONG Width
, ULONG Height
)
509 CSR_API_MESSAGE Request
;
511 Request
.Type
= MAKE_CSR_API(SHOW_DESKTOP
, CSR_GUI
);
512 Request
.Data
.ShowDesktopRequest
.DesktopWindow
= Desktop
->DesktopWindow
;
513 Request
.Data
.ShowDesktopRequest
.Width
= Width
;
514 Request
.Data
.ShowDesktopRequest
.Height
= Height
;
516 return CsrNotify(&Request
);
520 IntHideDesktop(PDESKTOP_OBJECT Desktop
)
523 CSRSS_API_REQUEST Request
;
524 CSRSS_API_REPLY Reply
;
526 Request
.Type
= CSRSS_HIDE_DESKTOP
;
527 Request
.Data
.HideDesktopRequest
.DesktopWindow
= Desktop
->DesktopWindow
;
529 return NotifyCsrss(&Request
, &Reply
);
531 PWINDOW_OBJECT DesktopWindow
;
533 DesktopWindow
= IntGetWindowObject(Desktop
->DesktopWindow
);
536 return ERROR_INVALID_WINDOW_HANDLE
;
538 DesktopWindow
->Style
&= ~WS_VISIBLE
;
540 return STATUS_SUCCESS
;
545 * Send the Message to the windows registered for ShellHook
546 * notifications. The lParam contents depend on the Message. See
547 * MSDN for more details (RegisterShellHookWindow)
549 VOID
IntShellHookNotify(WPARAM Message
, LPARAM lParam
)
551 PDESKTOP_OBJECT Desktop
= IntGetActiveDesktop();
552 PLIST_ENTRY Entry
, Entry2
;
553 PSHELL_HOOK_WINDOW Current
;
556 static UINT MsgType
= 0;
560 /* Too bad, this doesn't work.*/
563 RtlInitUnicodeString(&Str
, L
"SHELLHOOK");
564 MsgType
= NtUserRegisterWindowMessage(&Str
);
566 MsgType
= IntAddAtom(L
"SHELLHOOK");
568 DPRINT("MsgType = %x\n", MsgType
);
570 DPRINT1("LastError: %x\n", GetLastNtError());
574 DPRINT1("IntShellHookNotify: No desktop!\n");
578 /* We have to do some tricks because the list could change
579 * between calls, and we can't keep the lock during the call
582 KeAcquireSpinLock(&Desktop
->Lock
, &OldLevel
);
583 Entry
= Desktop
->ShellHookWindows
.Flink
;
584 while (Entry
!= &Desktop
->ShellHookWindows
) {
585 Current
= CONTAINING_RECORD(Entry
, SHELL_HOOK_WINDOW
, ListEntry
);
586 KeReleaseSpinLock(&Desktop
->Lock
, OldLevel
);
588 DPRINT("Sending notify\n");
589 IntPostOrSendMessage(Current
->hWnd
,
594 /* Loop again to find the window we were sending to. If it doesn't
595 * exist anymore, we just stop. This could leave an infinite loop
596 * if a window is removed and readded to the list. That's quite
600 KeAcquireSpinLock(&Desktop
->Lock
, &OldLevel
);
601 Entry2
= Desktop
->ShellHookWindows
.Flink
;
602 while (Entry2
!= Entry
&&
603 Entry2
!= &Desktop
->ShellHookWindows
) {
604 Entry2
= Entry2
->Flink
;
608 Entry
= Entry
->Flink
;
612 KeReleaseSpinLock(&Desktop
->Lock
, OldLevel
);
616 * Add the window to the ShellHookWindows list. The windows
617 * on that list get notifications that are important to shell
620 * TODO: Validate the window? I'm not sure if sending these messages to
621 * an unsuspecting application that is not your own is a nice thing to do.
623 BOOL
IntRegisterShellHookWindow(HWND hWnd
)
625 PDESKTOP_OBJECT Desktop
= PsGetWin32Thread()->Desktop
;
626 PSHELL_HOOK_WINDOW Entry
;
629 DPRINT("IntRegisterShellHookWindow\n");
631 /* First deregister the window, so we can be sure it's never twice in the
634 IntDeRegisterShellHookWindow(hWnd
);
636 Entry
= ExAllocatePoolWithTag(NonPagedPool
,
637 sizeof(SHELL_HOOK_WINDOW
),
639 /* We have to walk this structure with while holding a spinlock, so we
640 * need NonPagedPool */
647 KeAcquireSpinLock(&Desktop
->Lock
, &OldLevel
);
648 InsertTailList(&Desktop
->ShellHookWindows
, &Entry
->ListEntry
);
649 KeReleaseSpinLock(&Desktop
->Lock
, OldLevel
);
655 * Remove the window from the ShellHookWindows list. The windows
656 * on that list get notifications that are important to shell
659 BOOL
IntDeRegisterShellHookWindow(HWND hWnd
)
661 PDESKTOP_OBJECT Desktop
= PsGetWin32Thread()->Desktop
;
663 PSHELL_HOOK_WINDOW Current
;
666 KeAcquireSpinLock(&Desktop
->Lock
, &OldLevel
);
668 Entry
= Desktop
->ShellHookWindows
.Flink
;
669 while (Entry
!= &Desktop
->ShellHookWindows
) {
670 Current
= CONTAINING_RECORD(Entry
, SHELL_HOOK_WINDOW
, ListEntry
);
671 if (Current
->hWnd
== hWnd
) {
672 RemoveEntryList(Entry
);
673 KeReleaseSpinLock(&Desktop
->Lock
, OldLevel
);
677 Entry
= Entry
->Flink
;
680 KeReleaseSpinLock(&Desktop
->Lock
, OldLevel
);
686 * NtUserCreateDesktop
688 * Creates a new desktop.
692 * Name of the new desktop.
698 * Requested type of access.
701 * Security descriptor.
704 * Handle to window station on which to create the desktop.
707 * If the function succeeds, the return value is a handle to the newly
708 * created desktop. If the specified desktop already exists, the function
709 * succeeds and returns a handle to the existing desktop. When you are
710 * finished using the handle, call the CloseDesktop function to close it.
711 * If the function fails, the return value is NULL.
719 PUNICODE_STRING lpszDesktopName
,
721 ACCESS_MASK dwDesiredAccess
,
722 LPSECURITY_ATTRIBUTES lpSecurity
,
723 HWINSTA hWindowStation
)
725 OBJECT_ATTRIBUTES ObjectAttributes
;
726 PWINSTATION_OBJECT WinStaObject
;
727 PDESKTOP_OBJECT DesktopObject
;
728 UNICODE_STRING DesktopName
;
731 CSR_API_MESSAGE Request
;
733 DPRINT("CreateDesktop: %wZ\n", lpszDesktopName
);
735 Status
= IntValidateWindowStationHandle(
738 0, /* FIXME - WINSTA_CREATEDESKTOP */
741 if (! NT_SUCCESS(Status
))
743 DPRINT1("Failed validation of window station handle (0x%X), cannot create desktop %wZ\n",
744 hWindowStation
, lpszDesktopName
);
745 SetLastNtError(Status
);
749 if (! IntGetFullWindowStationName(&DesktopName
, &WinStaObject
->Name
,
752 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
753 ObDereferenceObject(WinStaObject
);
757 ObDereferenceObject(WinStaObject
);
760 * Try to open already existing desktop
763 DPRINT("Trying to open desktop (%wZ)\n", &DesktopName
);
765 /* Initialize ObjectAttributes for the desktop object */
766 InitializeObjectAttributes(
773 Status
= ObOpenObjectByName(
782 if (NT_SUCCESS(Status
))
784 DPRINT("Successfully opened desktop (%wZ)\n", &DesktopName
);
785 ExFreePool(DesktopName
.Buffer
);
790 * No existing desktop found, try to create new one
793 Status
= ObCreateObject(
799 sizeof(DESKTOP_OBJECT
),
802 (PVOID
*)&DesktopObject
);
804 if (! NT_SUCCESS(Status
))
806 DPRINT1("Failed creating desktop (%wZ)\n", &DesktopName
);
807 ExFreePool(DesktopName
.Buffer
);
808 SetLastNtError(STATUS_UNSUCCESSFUL
);
813 DesktopObject
->WorkArea
.left
= 0;
814 DesktopObject
->WorkArea
.top
= 0;
815 DesktopObject
->WorkArea
.right
= -1;
816 DesktopObject
->WorkArea
.bottom
= -1;
817 IntGetDesktopWorkArea(DesktopObject
, NULL
);
819 /* Initialize some local (to win32k) desktop state. */
820 DesktopObject
->ActiveMessageQueue
= NULL
;
822 Status
= ObInsertObject(
823 (PVOID
)DesktopObject
,
825 STANDARD_RIGHTS_REQUIRED
,
830 ObDereferenceObject(DesktopObject
);
831 ExFreePool(DesktopName
.Buffer
);
833 if (! NT_SUCCESS(Status
))
835 DPRINT1("Failed to create desktop handle\n");
836 SetLastNtError(Status
);
841 * Create a handle for CSRSS and notify CSRSS
843 Request
.Type
= MAKE_CSR_API(CREATE_DESKTOP
, CSR_GUI
);
844 Status
= CsrInsertObject(Desktop
,
846 (HANDLE
*)&Request
.Data
.CreateDesktopRequest
.DesktopHandle
);
847 if (! NT_SUCCESS(Status
))
849 DPRINT1("Failed to create desktop handle for CSRSS\n");
851 SetLastNtError(Status
);
855 Status
= CsrNotify(&Request
);
856 if (! NT_SUCCESS(Status
))
858 CsrCloseHandle(Request
.Data
.CreateDesktopRequest
.DesktopHandle
);
859 DPRINT1("Failed to notify CSRSS about new desktop\n");
861 SetLastNtError(Status
);
871 * Opens an existing desktop.
875 * Name of the existing desktop.
881 * Requested type of access.
884 * Handle to the desktop or zero on failure.
892 PUNICODE_STRING lpszDesktopName
,
894 ACCESS_MASK dwDesiredAccess
)
896 OBJECT_ATTRIBUTES ObjectAttributes
;
897 PWINSTATION_OBJECT WinStaObject
;
898 UNICODE_STRING DesktopName
;
903 * Validate the window station handle and compose the fully
904 * qualified desktop name
907 Status
= IntValidateWindowStationHandle(
908 PsGetCurrentProcess()->Win32WindowStation
,
913 if (!NT_SUCCESS(Status
))
915 DPRINT1("Failed validation of window station handle (0x%X)\n",
916 PsGetCurrentProcess()->Win32WindowStation
);
917 SetLastNtError(Status
);
921 if (!IntGetFullWindowStationName(&DesktopName
, &WinStaObject
->Name
,
924 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
925 ObDereferenceObject(WinStaObject
);
929 ObDereferenceObject(WinStaObject
);
931 DPRINT1("Trying to open desktop (%wZ)\n", &DesktopName
);
933 /* Initialize ObjectAttributes for the desktop object */
934 InitializeObjectAttributes(
941 Status
= ObOpenObjectByName(
950 if (!NT_SUCCESS(Status
))
952 SetLastNtError(Status
);
953 ExFreePool(DesktopName
.Buffer
);
957 DPRINT("Successfully opened desktop (%wZ)\n", &DesktopName
);
958 ExFreePool(DesktopName
.Buffer
);
964 * NtUserOpenInputDesktop
966 * Opens the input (interactive) desktop.
973 * Inheritance option.
976 * Requested type of access.
979 * Handle to the input desktop or zero on failure.
986 NtUserOpenInputDesktop(
989 ACCESS_MASK dwDesiredAccess
)
991 PDESKTOP_OBJECT Object
;
995 DPRINT("About to open input desktop\n");
997 /* Get a pointer to the desktop object */
999 Status
= IntValidateDesktopHandle(
1005 if (!NT_SUCCESS(Status
))
1007 DPRINT("Validation of input desktop handle (0x%X) failed\n", InputDesktop
);
1011 /* Create a new handle to the object */
1013 Status
= ObOpenObjectByPointer(
1018 ExDesktopObjectType
,
1022 ObDereferenceObject(Object
);
1024 if (NT_SUCCESS(Status
))
1026 DPRINT("Successfully opened input desktop\n");
1027 return (HDESK
)Desktop
;
1030 SetLastNtError(Status
);
1035 * NtUserCloseDesktop
1037 * Closes a desktop handle.
1041 * Handle to the desktop.
1047 * The desktop handle can be created with NtUserCreateDesktop or
1048 * NtUserOpenDesktop. This function will fail if any thread in the calling
1049 * process is using the specified desktop handle or if the handle refers
1050 * to the initial desktop of the calling process.
1057 NtUserCloseDesktop(HDESK hDesktop
)
1059 PDESKTOP_OBJECT Object
;
1062 DPRINT("About to close desktop handle (0x%X)\n", hDesktop
);
1064 Status
= IntValidateDesktopHandle(
1070 if (!NT_SUCCESS(Status
))
1072 DPRINT("Validation of desktop handle (0x%X) failed\n", hDesktop
);
1076 ObDereferenceObject(Object
);
1078 DPRINT("Closing desktop handle (0x%X)\n", hDesktop
);
1080 Status
= ZwClose(hDesktop
);
1081 if (!NT_SUCCESS(Status
))
1083 SetLastNtError(Status
);
1091 static int GetSystemVersionString(LPWSTR buffer
)
1093 RTL_OSVERSIONINFOEXW versionInfo
;
1096 versionInfo
.dwOSVersionInfoSize
= sizeof(RTL_OSVERSIONINFOEXW
);
1098 if (!NT_SUCCESS(RtlGetVersion((PRTL_OSVERSIONINFOW
)&versionInfo
)))
1101 if (versionInfo
.dwMajorVersion
<= 4)
1102 len
= swprintf(buffer
,
1103 L
"ReactOS Version %d.%d %s Build %d",
1104 versionInfo
.dwMajorVersion
, versionInfo
.dwMinorVersion
,
1105 versionInfo
.szCSDVersion
, versionInfo
.dwBuildNumber
&0xFFFF);
1107 len
= swprintf(buffer
,
1108 L
"ReactOS %s (Build %d)",
1109 versionInfo
.szCSDVersion
, versionInfo
.dwBuildNumber
&0xFFFF);
1115 * NtUserPaintDesktop
1117 * The NtUserPaintDesktop function fills the clipping region in the
1118 * specified device context with the desktop pattern or wallpaper. The
1119 * function is provided primarily for shell desktops.
1123 * Handle to the device context.
1130 NtUserPaintDesktop(HDC hDC
)
1133 HBRUSH DesktopBrush
, PreviousBrush
;
1135 BOOL doPatBlt
= TRUE
;
1138 PWINSTATION_OBJECT WinSta
= PsGetWin32Thread()->Desktop
->WindowStation
;
1140 IntGdiGetClipBox(hDC
, &Rect
);
1142 hWndDesktop
= IntGetDesktopWindow();
1143 DesktopBrush
= (HBRUSH
)NtUserGetClassLong(hWndDesktop
, GCL_HBRBACKGROUND
, FALSE
);
1146 * Paint desktop background
1149 if(WinSta
->hbmWallpaper
!= NULL
)
1151 PWINDOW_OBJECT DeskWin
;
1153 if((DeskWin
= IntGetWindowObject(hWndDesktop
)))
1159 sz
.cx
= DeskWin
->WindowRect
.right
- DeskWin
->WindowRect
.left
;
1160 sz
.cy
= DeskWin
->WindowRect
.bottom
- DeskWin
->WindowRect
.top
;
1161 IntReleaseWindowObject(DeskWin
);
1163 x
= (sz
.cx
/ 2) - (WinSta
->cxWallpaper
/ 2);
1164 y
= (sz
.cy
/ 2) - (WinSta
->cyWallpaper
/ 2);
1166 hWallpaperDC
= NtGdiCreateCompatableDC(hDC
);
1167 if(hWallpaperDC
!= NULL
)
1173 /* FIXME - clip out the bitmap */
1174 PreviousBrush
= NtGdiSelectObject(hDC
, DesktopBrush
);
1175 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
1176 NtGdiSelectObject(hDC
, PreviousBrush
);
1181 hOldBitmap
= NtGdiSelectObject(hWallpaperDC
, WinSta
->hbmWallpaper
);
1182 NtGdiBitBlt(hDC
, x
, y
, WinSta
->cxWallpaper
, WinSta
->cyWallpaper
, hWallpaperDC
, 0, 0, SRCCOPY
);
1183 NtGdiSelectObject(hWallpaperDC
, hOldBitmap
);
1185 NtGdiDeleteDC(hWallpaperDC
);
1191 PreviousBrush
= NtGdiSelectObject(hDC
, DesktopBrush
);
1192 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
1193 NtGdiSelectObject(hDC
, PreviousBrush
);
1197 * Display system version on the desktop background
1200 if (g_PaintDesktopVersion
) {
1201 static WCHAR s_wszVersion
[256] = {0};
1205 len
= wcslen(s_wszVersion
);
1207 len
= GetSystemVersionString(s_wszVersion
);
1210 if (!NtUserSystemParametersInfo(SPI_GETWORKAREA
, 0, &rect
, 0)) {
1211 rect
.right
= NtUserGetSystemMetrics(SM_CXSCREEN
);
1212 rect
.bottom
= NtUserGetSystemMetrics(SM_CYSCREEN
);
1215 COLORREF color_old
= NtGdiSetTextColor(hDC
, RGB(255,255,255));
1216 UINT align_old
= NtGdiSetTextAlign(hDC
, TA_RIGHT
);
1217 int mode_old
= NtGdiSetBkMode(hDC
, TRANSPARENT
);
1219 NtGdiTextOut(hDC
, rect
.right
-16, rect
.bottom
-48, s_wszVersion
, len
);
1221 NtGdiSetBkMode(hDC
, mode_old
);
1222 NtGdiSetTextAlign(hDC
, align_old
);
1223 NtGdiSetTextColor(hDC
, color_old
);
1232 * NtUserSwitchDesktop
1234 * Sets the current input (interactive) desktop.
1238 * Handle to desktop.
1248 NtUserSwitchDesktop(HDESK hDesktop
)
1250 PDESKTOP_OBJECT DesktopObject
;
1253 DPRINT("About to switch desktop (0x%X)\n", hDesktop
);
1255 Status
= IntValidateDesktopHandle(
1261 if (!NT_SUCCESS(Status
))
1263 DPRINT("Validation of desktop handle (0x%X) failed\n", hDesktop
);
1268 * Don't allow applications switch the desktop if it's locked, unless the caller
1269 * is the logon application itself
1271 if((DesktopObject
->WindowStation
->Flags
& WSS_LOCKED
) &&
1272 LogonProcess
!= NULL
&& LogonProcess
!= PsGetWin32Process())
1274 ObDereferenceObject(DesktopObject
);
1275 DPRINT1("Switching desktop 0x%x denied because the work station is locked!\n", hDesktop
);
1279 /* FIXME: Fail if the desktop belong to an invisible window station */
1280 /* FIXME: Fail if the process is associated with a secured
1281 desktop such as Winlogon or Screen-Saver */
1282 /* FIXME: Connect to input device */
1284 /* Set the active desktop in the desktop's window station. */
1285 DesktopObject
->WindowStation
->ActiveDesktop
= DesktopObject
;
1287 /* Set the global state. */
1288 InputDesktop
= DesktopObject
;
1289 InputDesktopHandle
= hDesktop
;
1290 InputWindowStation
= DesktopObject
->WindowStation
;
1292 ObDereferenceObject(DesktopObject
);
1298 * NtUserResolveDesktopForWOW
1305 NtUserResolveDesktopForWOW(DWORD Unknown0
)
1312 * NtUserGetThreadDesktop
1319 NtUserGetThreadDesktop(DWORD dwThreadId
, DWORD Unknown1
)
1323 PDESKTOP_OBJECT DesktopObject
;
1324 HDESK Ret
, hThreadDesktop
;
1325 OBJECT_HANDLE_INFORMATION HandleInformation
;
1329 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1333 Status
= PsLookupThreadByThreadId((HANDLE
)dwThreadId
, &Thread
);
1334 if(!NT_SUCCESS(Status
))
1336 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1340 if(Thread
->ThreadsProcess
== PsGetCurrentProcess())
1342 /* just return the handle, we queried the desktop handle of a thread running
1343 in the same context */
1344 Ret
= Thread
->Tcb
.Win32Thread
->hDesktop
;
1345 ObDereferenceObject(Thread
);
1349 /* get the desktop handle and the desktop of the thread */
1350 if(!(hThreadDesktop
= Thread
->Tcb
.Win32Thread
->hDesktop
) ||
1351 !(DesktopObject
= Thread
->Tcb
.Win32Thread
->Desktop
))
1353 ObDereferenceObject(Thread
);
1354 DPRINT1("Desktop information of thread 0x%x broken!?\n", dwThreadId
);
1358 /* we could just use DesktopObject instead of looking up the handle, but latter
1359 may be a bit safer (e.g. when the desktop is being destroyed */
1360 /* switch into the context of the thread we're trying to get the desktop from,
1361 so we can use the handle */
1362 KeAttachProcess(EPROCESS_TO_KPROCESS(Thread
->ThreadsProcess
));
1363 Status
= ObReferenceObjectByHandle(hThreadDesktop
,
1365 ExDesktopObjectType
,
1367 (PVOID
*)&DesktopObject
,
1368 &HandleInformation
);
1371 /* the handle couldn't be found, there's nothing to get... */
1372 if(!NT_SUCCESS(Status
))
1374 ObDereferenceObject(Thread
);
1378 /* lookup our handle table if we can find a handle to the desktop object,
1379 if not, create one */
1380 Ret
= IntGetDesktopObjectHandle(DesktopObject
);
1382 /* all done, we got a valid handle to the desktop */
1383 ObDereferenceObject(DesktopObject
);
1384 ObDereferenceObject(Thread
);
1389 * NtUserSetThreadDesktop
1396 NtUserSetThreadDesktop(HDESK hDesktop
)
1398 PW32THREAD W32Thread
;
1399 PDESKTOP_OBJECT DesktopObject
;
1402 /* Validate the new desktop. */
1403 Status
= IntValidateDesktopHandle(
1409 if (!NT_SUCCESS(Status
))
1411 DPRINT("Validation of desktop handle (0x%X) failed\n", hDesktop
);
1415 W32Thread
= PsGetWin32Thread();
1417 /* FIXME: Should check here to see if the thread has any windows. */
1419 if (W32Thread
->Desktop
!= NULL
)
1421 ObDereferenceObject(W32Thread
->Desktop
);
1424 W32Thread
->Desktop
= DesktopObject
;
1425 W32Thread
->hDesktop
= hDesktop
;