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(dc
->w
.hBitmap
);
354 DC_UnlockDc(ScreenDeviceContext
);
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 CSRSS_API_REQUEST Request
;
510 CSRSS_API_REPLY Reply
;
512 Request
.Type
= CSRSS_SHOW_DESKTOP
;
513 Request
.Data
.ShowDesktopRequest
.DesktopWindow
= Desktop
->DesktopWindow
;
514 Request
.Data
.ShowDesktopRequest
.Width
= Width
;
515 Request
.Data
.ShowDesktopRequest
.Height
= Height
;
517 return CsrNotify(&Request
, &Reply
);
521 IntHideDesktop(PDESKTOP_OBJECT Desktop
)
524 CSRSS_API_REQUEST Request
;
525 CSRSS_API_REPLY Reply
;
527 Request
.Type
= CSRSS_HIDE_DESKTOP
;
528 Request
.Data
.HideDesktopRequest
.DesktopWindow
= Desktop
->DesktopWindow
;
530 return NotifyCsrss(&Request
, &Reply
);
532 PWINDOW_OBJECT DesktopWindow
;
534 DesktopWindow
= IntGetWindowObject(Desktop
->DesktopWindow
);
537 return ERROR_INVALID_WINDOW_HANDLE
;
539 DesktopWindow
->Style
&= ~WS_VISIBLE
;
541 return STATUS_SUCCESS
;
546 * Send the Message to the windows registered for ShellHook
547 * notifications. The lParam contents depend on the Message. See
548 * MSDN for more details (RegisterShellHookWindow)
550 VOID
IntShellHookNotify(WPARAM Message
, LPARAM lParam
)
552 PDESKTOP_OBJECT Desktop
= IntGetActiveDesktop();
553 PLIST_ENTRY Entry
, Entry2
;
554 PSHELL_HOOK_WINDOW Current
;
557 static UINT MsgType
= 0;
561 /* Too bad, this doesn't work.*/
564 RtlInitUnicodeString(&Str
, L
"SHELLHOOK");
565 MsgType
= NtUserRegisterWindowMessage(&Str
);
567 MsgType
= IntAddAtom(L
"SHELLHOOK");
569 DPRINT("MsgType = %x\n", MsgType
);
571 DPRINT1("LastError: %x\n", GetLastNtError());
575 DPRINT1("IntShellHookNotify: No desktop!\n");
579 /* We have to do some tricks because the list could change
580 * between calls, and we can't keep the lock during the call
583 KeAcquireSpinLock(&Desktop
->Lock
, &OldLevel
);
584 Entry
= Desktop
->ShellHookWindows
.Flink
;
585 while (Entry
!= &Desktop
->ShellHookWindows
) {
586 Current
= CONTAINING_RECORD(Entry
, SHELL_HOOK_WINDOW
, ListEntry
);
587 KeReleaseSpinLock(&Desktop
->Lock
, OldLevel
);
589 DPRINT("Sending notify\n");
590 IntPostOrSendMessage(Current
->hWnd
,
595 /* Loop again to find the window we were sending to. If it doesn't
596 * exist anymore, we just stop. This could leave an infinite loop
597 * if a window is removed and readded to the list. That's quite
601 KeAcquireSpinLock(&Desktop
->Lock
, &OldLevel
);
602 Entry2
= Desktop
->ShellHookWindows
.Flink
;
603 while (Entry2
!= Entry
&&
604 Entry2
!= &Desktop
->ShellHookWindows
) {
605 Entry2
= Entry2
->Flink
;
609 Entry
= Entry
->Flink
;
613 KeReleaseSpinLock(&Desktop
->Lock
, OldLevel
);
617 * Add the window to the ShellHookWindows list. The windows
618 * on that list get notifications that are important to shell
621 * TODO: Validate the window? I'm not sure if sending these messages to
622 * an unsuspecting application that is not your own is a nice thing to do.
624 BOOL
IntRegisterShellHookWindow(HWND hWnd
)
626 PDESKTOP_OBJECT Desktop
= PsGetWin32Thread()->Desktop
;
627 PSHELL_HOOK_WINDOW Entry
;
630 DPRINT("IntRegisterShellHookWindow\n");
632 /* First deregister the window, so we can be sure it's never twice in the
635 IntDeRegisterShellHookWindow(hWnd
);
637 Entry
= ExAllocatePoolWithTag(NonPagedPool
,
638 sizeof(SHELL_HOOK_WINDOW
),
640 /* We have to walk this structure with while holding a spinlock, so we
641 * need NonPagedPool */
648 KeAcquireSpinLock(&Desktop
->Lock
, &OldLevel
);
649 InsertTailList(&Desktop
->ShellHookWindows
, &Entry
->ListEntry
);
650 KeReleaseSpinLock(&Desktop
->Lock
, OldLevel
);
656 * Remove the window from the ShellHookWindows list. The windows
657 * on that list get notifications that are important to shell
660 BOOL
IntDeRegisterShellHookWindow(HWND hWnd
)
662 PDESKTOP_OBJECT Desktop
= PsGetWin32Thread()->Desktop
;
664 PSHELL_HOOK_WINDOW Current
;
667 KeAcquireSpinLock(&Desktop
->Lock
, &OldLevel
);
669 Entry
= Desktop
->ShellHookWindows
.Flink
;
670 while (Entry
!= &Desktop
->ShellHookWindows
) {
671 Current
= CONTAINING_RECORD(Entry
, SHELL_HOOK_WINDOW
, ListEntry
);
672 if (Current
->hWnd
== hWnd
) {
673 RemoveEntryList(Entry
);
674 KeReleaseSpinLock(&Desktop
->Lock
, OldLevel
);
678 Entry
= Entry
->Flink
;
681 KeReleaseSpinLock(&Desktop
->Lock
, OldLevel
);
687 * NtUserCreateDesktop
689 * Creates a new desktop.
693 * Name of the new desktop.
699 * Requested type of access.
702 * Security descriptor.
705 * Handle to window station on which to create the desktop.
708 * If the function succeeds, the return value is a handle to the newly
709 * created desktop. If the specified desktop already exists, the function
710 * succeeds and returns a handle to the existing desktop. When you are
711 * finished using the handle, call the CloseDesktop function to close it.
712 * If the function fails, the return value is NULL.
720 PUNICODE_STRING lpszDesktopName
,
722 ACCESS_MASK dwDesiredAccess
,
723 LPSECURITY_ATTRIBUTES lpSecurity
,
724 HWINSTA hWindowStation
)
726 OBJECT_ATTRIBUTES ObjectAttributes
;
727 PWINSTATION_OBJECT WinStaObject
;
728 PDESKTOP_OBJECT DesktopObject
;
729 UNICODE_STRING DesktopName
;
732 CSRSS_API_REQUEST Request
;
733 CSRSS_API_REPLY Reply
;
735 DPRINT("CreateDesktop: %wZ\n", lpszDesktopName
);
737 Status
= IntValidateWindowStationHandle(
740 0, /* FIXME - WINSTA_CREATEDESKTOP */
743 if (! NT_SUCCESS(Status
))
745 DPRINT1("Failed validation of window station handle (0x%X), cannot create desktop %wZ\n",
746 hWindowStation
, lpszDesktopName
);
747 SetLastNtError(Status
);
751 if (! IntGetFullWindowStationName(&DesktopName
, &WinStaObject
->Name
,
754 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
755 ObDereferenceObject(WinStaObject
);
759 ObDereferenceObject(WinStaObject
);
762 * Try to open already existing desktop
765 DPRINT("Trying to open desktop (%wZ)\n", &DesktopName
);
767 /* Initialize ObjectAttributes for the desktop object */
768 InitializeObjectAttributes(
775 Status
= ObOpenObjectByName(
784 if (NT_SUCCESS(Status
))
786 DPRINT("Successfully opened desktop (%wZ)\n", &DesktopName
);
787 ExFreePool(DesktopName
.Buffer
);
792 * No existing desktop found, try to create new one
795 Status
= ObCreateObject(
801 sizeof(DESKTOP_OBJECT
),
804 (PVOID
*)&DesktopObject
);
806 if (! NT_SUCCESS(Status
))
808 DPRINT1("Failed creating desktop (%wZ)\n", &DesktopName
);
809 ExFreePool(DesktopName
.Buffer
);
810 SetLastNtError(STATUS_UNSUCCESSFUL
);
815 DesktopObject
->WorkArea
.left
= 0;
816 DesktopObject
->WorkArea
.top
= 0;
817 DesktopObject
->WorkArea
.right
= -1;
818 DesktopObject
->WorkArea
.bottom
= -1;
819 IntGetDesktopWorkArea(DesktopObject
, NULL
);
821 /* Initialize some local (to win32k) desktop state. */
822 DesktopObject
->ActiveMessageQueue
= NULL
;
824 Status
= ObInsertObject(
825 (PVOID
)DesktopObject
,
827 STANDARD_RIGHTS_REQUIRED
,
832 ObDereferenceObject(DesktopObject
);
833 ExFreePool(DesktopName
.Buffer
);
835 if (! NT_SUCCESS(Status
))
837 DPRINT1("Failed to create desktop handle\n");
838 SetLastNtError(Status
);
843 * Create a handle for CSRSS and notify CSRSS
845 Request
.Type
= CSRSS_CREATE_DESKTOP
;
846 Status
= CsrInsertObject(Desktop
,
848 (HANDLE
*)&Request
.Data
.CreateDesktopRequest
.DesktopHandle
);
849 if (! NT_SUCCESS(Status
))
851 DPRINT1("Failed to create desktop handle for CSRSS\n");
853 SetLastNtError(Status
);
857 Status
= CsrNotify(&Request
, &Reply
);
858 if (! NT_SUCCESS(Status
))
860 CsrCloseHandle(Request
.Data
.CreateDesktopRequest
.DesktopHandle
);
861 DPRINT1("Failed to notify CSRSS about new desktop\n");
863 SetLastNtError(Status
);
873 * Opens an existing desktop.
877 * Name of the existing desktop.
883 * Requested type of access.
886 * Handle to the desktop or zero on failure.
894 PUNICODE_STRING lpszDesktopName
,
896 ACCESS_MASK dwDesiredAccess
)
898 OBJECT_ATTRIBUTES ObjectAttributes
;
899 PWINSTATION_OBJECT WinStaObject
;
900 UNICODE_STRING DesktopName
;
905 * Validate the window station handle and compose the fully
906 * qualified desktop name
909 Status
= IntValidateWindowStationHandle(
910 PsGetCurrentProcess()->Win32WindowStation
,
915 if (!NT_SUCCESS(Status
))
917 DPRINT1("Failed validation of window station handle (0x%X)\n",
918 PsGetCurrentProcess()->Win32WindowStation
);
919 SetLastNtError(Status
);
923 if (!IntGetFullWindowStationName(&DesktopName
, &WinStaObject
->Name
,
926 SetLastNtError(STATUS_INSUFFICIENT_RESOURCES
);
927 ObDereferenceObject(WinStaObject
);
931 ObDereferenceObject(WinStaObject
);
933 DPRINT1("Trying to open desktop (%wZ)\n", &DesktopName
);
935 /* Initialize ObjectAttributes for the desktop object */
936 InitializeObjectAttributes(
943 Status
= ObOpenObjectByName(
952 if (!NT_SUCCESS(Status
))
954 SetLastNtError(Status
);
955 ExFreePool(DesktopName
.Buffer
);
959 DPRINT("Successfully opened desktop (%wZ)\n", &DesktopName
);
960 ExFreePool(DesktopName
.Buffer
);
966 * NtUserOpenInputDesktop
968 * Opens the input (interactive) desktop.
975 * Inheritance option.
978 * Requested type of access.
981 * Handle to the input desktop or zero on failure.
988 NtUserOpenInputDesktop(
991 ACCESS_MASK dwDesiredAccess
)
993 PDESKTOP_OBJECT Object
;
997 DPRINT("About to open input desktop\n");
999 /* Get a pointer to the desktop object */
1001 Status
= IntValidateDesktopHandle(
1007 if (!NT_SUCCESS(Status
))
1009 DPRINT("Validation of input desktop handle (0x%X) failed\n", InputDesktop
);
1013 /* Create a new handle to the object */
1015 Status
= ObOpenObjectByPointer(
1020 ExDesktopObjectType
,
1024 ObDereferenceObject(Object
);
1026 if (NT_SUCCESS(Status
))
1028 DPRINT("Successfully opened input desktop\n");
1029 return (HDESK
)Desktop
;
1032 SetLastNtError(Status
);
1037 * NtUserCloseDesktop
1039 * Closes a desktop handle.
1043 * Handle to the desktop.
1049 * The desktop handle can be created with NtUserCreateDesktop or
1050 * NtUserOpenDesktop. This function will fail if any thread in the calling
1051 * process is using the specified desktop handle or if the handle refers
1052 * to the initial desktop of the calling process.
1059 NtUserCloseDesktop(HDESK hDesktop
)
1061 PDESKTOP_OBJECT Object
;
1064 DPRINT("About to close desktop handle (0x%X)\n", hDesktop
);
1066 Status
= IntValidateDesktopHandle(
1072 if (!NT_SUCCESS(Status
))
1074 DPRINT("Validation of desktop handle (0x%X) failed\n", hDesktop
);
1078 ObDereferenceObject(Object
);
1080 DPRINT("Closing desktop handle (0x%X)\n", hDesktop
);
1082 Status
= ZwClose(hDesktop
);
1083 if (!NT_SUCCESS(Status
))
1085 SetLastNtError(Status
);
1093 static int GetSystemVersionString(LPWSTR buffer
)
1095 RTL_OSVERSIONINFOEXW versionInfo
;
1098 versionInfo
.dwOSVersionInfoSize
= sizeof(RTL_OSVERSIONINFOEXW
);
1100 if (!NT_SUCCESS(RtlGetVersion((PRTL_OSVERSIONINFOW
)&versionInfo
)))
1103 if (versionInfo
.dwMajorVersion
<= 4)
1104 len
= swprintf(buffer
,
1105 L
"ReactOS Version %d.%d %s Build %d",
1106 versionInfo
.dwMajorVersion
, versionInfo
.dwMinorVersion
,
1107 versionInfo
.szCSDVersion
, versionInfo
.dwBuildNumber
&0xFFFF);
1109 len
= swprintf(buffer
,
1110 L
"ReactOS %s (Build %d)",
1111 versionInfo
.szCSDVersion
, versionInfo
.dwBuildNumber
&0xFFFF);
1117 * NtUserPaintDesktop
1119 * The NtUserPaintDesktop function fills the clipping region in the
1120 * specified device context with the desktop pattern or wallpaper. The
1121 * function is provided primarily for shell desktops.
1125 * Handle to the device context.
1132 NtUserPaintDesktop(HDC hDC
)
1135 HBRUSH DesktopBrush
, PreviousBrush
;
1137 BOOL doPatBlt
= TRUE
;
1140 PWINSTATION_OBJECT WinSta
= PsGetWin32Thread()->Desktop
->WindowStation
;
1142 IntGdiGetClipBox(hDC
, &Rect
);
1144 hWndDesktop
= IntGetDesktopWindow();
1145 DesktopBrush
= (HBRUSH
)NtUserGetClassLong(hWndDesktop
, GCL_HBRBACKGROUND
, FALSE
);
1148 * Paint desktop background
1151 if(WinSta
->hbmWallpaper
!= NULL
)
1153 PWINDOW_OBJECT DeskWin
;
1155 if((DeskWin
= IntGetWindowObject(hWndDesktop
)))
1161 sz
.cx
= DeskWin
->WindowRect
.right
- DeskWin
->WindowRect
.left
;
1162 sz
.cy
= DeskWin
->WindowRect
.bottom
- DeskWin
->WindowRect
.top
;
1163 IntReleaseWindowObject(DeskWin
);
1165 x
= (sz
.cx
/ 2) - (WinSta
->cxWallpaper
/ 2);
1166 y
= (sz
.cy
/ 2) - (WinSta
->cyWallpaper
/ 2);
1168 hWallpaperDC
= NtGdiCreateCompatableDC(hDC
);
1169 if(hWallpaperDC
!= NULL
)
1175 /* FIXME - clip out the bitmap */
1176 PreviousBrush
= NtGdiSelectObject(hDC
, DesktopBrush
);
1177 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
1178 NtGdiSelectObject(hDC
, PreviousBrush
);
1183 hOldBitmap
= NtGdiSelectObject(hWallpaperDC
, WinSta
->hbmWallpaper
);
1184 NtGdiBitBlt(hDC
, x
, y
, WinSta
->cxWallpaper
, WinSta
->cyWallpaper
, hWallpaperDC
, 0, 0, SRCCOPY
);
1185 NtGdiSelectObject(hWallpaperDC
, hOldBitmap
);
1187 NtGdiDeleteDC(hWallpaperDC
);
1193 PreviousBrush
= NtGdiSelectObject(hDC
, DesktopBrush
);
1194 NtGdiPatBlt(hDC
, Rect
.left
, Rect
.top
, Rect
.right
, Rect
.bottom
, PATCOPY
);
1195 NtGdiSelectObject(hDC
, PreviousBrush
);
1199 * Display system version on the desktop background
1202 if (g_PaintDesktopVersion
) {
1203 static WCHAR s_wszVersion
[256] = {0};
1207 len
= wcslen(s_wszVersion
);
1209 len
= GetSystemVersionString(s_wszVersion
);
1212 if (!NtUserSystemParametersInfo(SPI_GETWORKAREA
, 0, &rect
, 0)) {
1213 rect
.right
= NtUserGetSystemMetrics(SM_CXSCREEN
);
1214 rect
.bottom
= NtUserGetSystemMetrics(SM_CYSCREEN
);
1217 COLORREF color_old
= NtGdiSetTextColor(hDC
, RGB(255,255,255));
1218 UINT align_old
= NtGdiSetTextAlign(hDC
, TA_RIGHT
);
1219 int mode_old
= NtGdiSetBkMode(hDC
, TRANSPARENT
);
1221 NtGdiTextOut(hDC
, rect
.right
-16, rect
.bottom
-48, s_wszVersion
, len
);
1223 NtGdiSetBkMode(hDC
, mode_old
);
1224 NtGdiSetTextAlign(hDC
, align_old
);
1225 NtGdiSetTextColor(hDC
, color_old
);
1234 * NtUserSwitchDesktop
1236 * Sets the current input (interactive) desktop.
1240 * Handle to desktop.
1250 NtUserSwitchDesktop(HDESK hDesktop
)
1252 PDESKTOP_OBJECT DesktopObject
;
1255 DPRINT("About to switch desktop (0x%X)\n", hDesktop
);
1257 Status
= IntValidateDesktopHandle(
1263 if (!NT_SUCCESS(Status
))
1265 DPRINT("Validation of desktop handle (0x%X) failed\n", hDesktop
);
1270 * Don't allow applications switch the desktop if it's locked, unless the caller
1271 * is the logon application itself
1273 if((DesktopObject
->WindowStation
->Flags
& WSS_LOCKED
) &&
1274 LogonProcess
!= NULL
&& LogonProcess
!= PsGetWin32Process())
1276 ObDereferenceObject(DesktopObject
);
1277 DPRINT1("Switching desktop 0x%x denied because the work station is locked!\n", hDesktop
);
1281 /* FIXME: Fail if the desktop belong to an invisible window station */
1282 /* FIXME: Fail if the process is associated with a secured
1283 desktop such as Winlogon or Screen-Saver */
1284 /* FIXME: Connect to input device */
1286 /* Set the active desktop in the desktop's window station. */
1287 DesktopObject
->WindowStation
->ActiveDesktop
= DesktopObject
;
1289 /* Set the global state. */
1290 InputDesktop
= DesktopObject
;
1291 InputDesktopHandle
= hDesktop
;
1292 InputWindowStation
= DesktopObject
->WindowStation
;
1294 ObDereferenceObject(DesktopObject
);
1300 * NtUserResolveDesktopForWOW
1307 NtUserResolveDesktopForWOW(DWORD Unknown0
)
1314 * NtUserGetThreadDesktop
1321 NtUserGetThreadDesktop(DWORD dwThreadId
, DWORD Unknown1
)
1325 PDESKTOP_OBJECT DesktopObject
;
1326 HDESK Ret
, hThreadDesktop
;
1327 OBJECT_HANDLE_INFORMATION HandleInformation
;
1331 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1335 Status
= PsLookupThreadByThreadId((HANDLE
)dwThreadId
, &Thread
);
1336 if(!NT_SUCCESS(Status
))
1338 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1342 if(Thread
->ThreadsProcess
== PsGetCurrentProcess())
1344 /* just return the handle, we queried the desktop handle of a thread running
1345 in the same context */
1346 Ret
= Thread
->Tcb
.Win32Thread
->hDesktop
;
1347 ObDereferenceObject(Thread
);
1351 /* get the desktop handle and the desktop of the thread */
1352 if(!(hThreadDesktop
= Thread
->Tcb
.Win32Thread
->hDesktop
) ||
1353 !(DesktopObject
= Thread
->Tcb
.Win32Thread
->Desktop
))
1355 ObDereferenceObject(Thread
);
1356 DPRINT1("Desktop information of thread 0x%x broken!?\n", dwThreadId
);
1360 /* we could just use DesktopObject instead of looking up the handle, but latter
1361 may be a bit safer (e.g. when the desktop is being destroyed */
1362 /* switch into the context of the thread we're trying to get the desktop from,
1363 so we can use the handle */
1364 KeAttachProcess(Thread
->ThreadsProcess
);
1365 Status
= ObReferenceObjectByHandle(hThreadDesktop
,
1367 ExDesktopObjectType
,
1369 (PVOID
*)&DesktopObject
,
1370 &HandleInformation
);
1373 /* the handle couldn't be found, there's nothing to get... */
1374 if(!NT_SUCCESS(Status
))
1376 ObDereferenceObject(Thread
);
1380 /* lookup our handle table if we can find a handle to the desktop object,
1381 if not, create one */
1382 Ret
= IntGetDesktopObjectHandle(DesktopObject
);
1384 /* all done, we got a valid handle to the desktop */
1385 ObDereferenceObject(DesktopObject
);
1386 ObDereferenceObject(Thread
);
1391 * NtUserSetThreadDesktop
1398 NtUserSetThreadDesktop(HDESK hDesktop
)
1400 PW32THREAD W32Thread
;
1401 PDESKTOP_OBJECT DesktopObject
;
1404 /* Validate the new desktop. */
1405 Status
= IntValidateDesktopHandle(
1411 if (!NT_SUCCESS(Status
))
1413 DPRINT("Validation of desktop handle (0x%X) failed\n", hDesktop
);
1417 W32Thread
= PsGetWin32Thread();
1419 /* FIXME: Should check here to see if the thread has any windows. */
1421 if (W32Thread
->Desktop
!= NULL
)
1423 ObDereferenceObject(W32Thread
->Desktop
);
1426 W32Thread
->Desktop
= DesktopObject
;
1427 W32Thread
->hDesktop
= hDesktop
;