[WIN32K] Tag more pool allocations
[reactos.git] / reactos / subsystems / win32 / win32k / ntuser / winsta.c
index d0ca93e..c4714c7 100644 (file)
@@ -12,9 +12,9 @@
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  *  COPYRIGHT:        See COPYING in the top level directory
  *  PROJECT:          ReactOS kernel
@@ -33,7 +33,7 @@
 
 /* INCLUDES ******************************************************************/
 
-#include <w32k.h>
+#include <win32k.h>
 
 #define NDEBUG
 #include <debug.h>
@@ -55,6 +55,7 @@ static GENERIC_MAPPING IntWindowStationMapping =
       WINSTA_READATTRIBUTES    | WINSTA_READSCREEN        | WINSTA_WRITEATTRIBUTES
    };
 
+
 NTSTATUS FASTCALL
 InitWindowStationImpl(VOID)
 {
@@ -92,35 +93,21 @@ CleanupWindowStationImpl(VOID)
    return STATUS_SUCCESS;
 }
 
-/* OBJECT CALLBACKS  **********************************************************/
-
-NTSTATUS
-STDCALL
-IntWinStaObjectOpen(PWIN32_OPENMETHOD_PARAMETERS Parameters)
+BOOL FASTCALL
+IntSetupClipboard(PWINSTATION_OBJECT WinStaObj)
 {
-    PWINSTATION_OBJECT WinSta = (PWINSTATION_OBJECT)Parameters->Object;
-    OB_OPEN_REASON Reason = Parameters->OpenReason;
-    NTSTATUS Status;
-
-   if (Reason == ObCreateHandle)
-   {
-      DPRINT("Creating window station (0x%X)\n", WinSta);
-
-      KeInitializeSpinLock(&WinSta->Lock);
-
-      InitializeListHead(&WinSta->DesktopListHead);
-
-      WinSta->AtomTable = NULL;
-      Status = RtlCreateAtomTable(37, &WinSta->AtomTable);
-      WinSta->SystemMenuTemplate = (HANDLE)0;
-
-      DPRINT("Window station successfully created.\n");
-   }
-
-   return STATUS_SUCCESS;
+    WinStaObj->Clipboard = ExAllocatePoolWithTag(PagedPool, sizeof(CLIPBOARDSYSTEM), TAG_WINSTA);
+    if (WinStaObj->Clipboard)
+    {
+        RtlZeroMemory(WinStaObj->Clipboard, sizeof(CLIPBOARDSYSTEM));
+        return TRUE;
+    }
+    return FALSE;
 }
 
-VOID STDCALL
+/* OBJECT CALLBACKS  **********************************************************/
+
+VOID APIENTRY
 IntWinStaObjectDelete(PWIN32_DELETEMETHOD_PARAMETERS Parameters)
 {
    PWINSTATION_OBJECT WinSta = (PWINSTATION_OBJECT)Parameters->Object;
@@ -133,7 +120,7 @@ IntWinStaObjectDelete(PWIN32_DELETEMETHOD_PARAMETERS Parameters)
 }
 
 NTSTATUS
-STDCALL
+APIENTRY
 IntWinStaObjectParse(PWIN32_PARSEMETHOD_PARAMETERS Parameters)
 {
     PUNICODE_STRING RemainingName = Parameters->RemainingName;
@@ -152,8 +139,8 @@ IntWinStaObjectParse(PWIN32_PARSEMETHOD_PARAMETERS Parameters)
         }
 
         /* Reference the window station and return */
-        //ObReferenceObject(Object);
-        //*NextObject = Object; => ROS Parse routines don't need to do this
+        ObReferenceObject(Parameters->ParseObject);
+        *Parameters->Object = Parameters->ParseObject;
         return STATUS_SUCCESS;
     }
 
@@ -175,10 +162,8 @@ IntWinStaObjectParse(PWIN32_PARSEMETHOD_PARAMETERS Parameters)
 
     /*
      * Check if we are parsing a desktop.
-     * FIXME: ROS Sends the wrong Object Type. The parsed object's type
-     * should be sent, not the parsed parent's.
      */
-    if (Parameters->ObjectType == ExWindowStationObjectType)
+    if (Parameters->ObjectType == ExDesktopObjectType)
     {
         /* Then call the desktop parse routine */
         return IntDesktopObjectParse(Parameters->ParseObject,
@@ -305,37 +290,49 @@ IntGetWindowStationObject(PWINSTATION_OBJECT Object)
    return NT_SUCCESS(Status);
 }
 
+
 BOOL FASTCALL
 co_IntInitializeDesktopGraphics(VOID)
 {
-   UNICODE_STRING DriverName;
+   TEXTMETRICW tmw;
+   UNICODE_STRING DriverName = RTL_CONSTANT_STRING(L"DISPLAY");
    if (! IntCreatePrimarySurface())
    {
       return FALSE;
    }
-   RtlInitUnicodeString(&DriverName, L"DISPLAY");
    ScreenDeviceContext = IntGdiCreateDC(&DriverName, NULL, NULL, NULL, FALSE);
    if (NULL == ScreenDeviceContext)
    {
       IntDestroyPrimarySurface();
       return FALSE;
    }
-   DC_SetOwnership(ScreenDeviceContext, NULL);
-
-   UserAcquireOrReleaseInputOwnership(FALSE);
+   IntGdiSetDCOwnerEx(ScreenDeviceContext, GDI_OBJ_HMGR_PUBLIC, FALSE);
 
    /* Setup the cursor */
    co_IntLoadDefaultCursors();
 
+   hSystemBM = NtGdiCreateCompatibleDC(ScreenDeviceContext);
+
+   NtGdiSelectFont( hSystemBM, NtGdiGetStockObject(SYSTEM_FONT));
+   IntGdiSetDCOwnerEx( hSystemBM, GDI_OBJ_HMGR_PUBLIC, FALSE);
+
+   // FIXME! Move these to a update routine.
+   gpsi->Planes        = NtGdiGetDeviceCaps(ScreenDeviceContext, PLANES);
+   gpsi->BitsPixel     = NtGdiGetDeviceCaps(ScreenDeviceContext, BITSPIXEL);
+   gpsi->BitCount      = gpsi->Planes * gpsi->BitsPixel;
+   gpsi->dmLogPixels   = NtGdiGetDeviceCaps(ScreenDeviceContext, LOGPIXELSY);
+   // Font is realized and this dc was previously set to internal DC_ATTR.
+   gpsi->cxSysFontChar = IntGetCharDimensions(hSystemBM, &tmw, (DWORD*)&gpsi->cySysFontChar);
+   gpsi->tmSysFont     = tmw;
+
    return TRUE;
 }
 
 VOID FASTCALL
 IntEndDesktopGraphics(VOID)
 {
-   UserAcquireOrReleaseInputOwnership(TRUE);
    if (NULL != ScreenDeviceContext)
-   {
+   {  // No need to allocate a new dcattr.
       DC_SetOwnership(ScreenDeviceContext, PsGetCurrentProcess());
       NtGdiDeleteObjectApp(ScreenDeviceContext);
       ScreenDeviceContext = NULL;
@@ -388,16 +385,16 @@ IntGetScreenDC(VOID)
  *    @implemented
  */
 
-HWINSTA STDCALL
+HWINSTA APIENTRY
 NtUserCreateWindowStation(
    PUNICODE_STRING lpszWindowStationName,
    ACCESS_MASK dwDesiredAccess,
    LPSECURITY_ATTRIBUTES lpSecurity,
    DWORD Unknown3,
    DWORD Unknown4,
-   DWORD Unknown5)
+   DWORD Unknown5,
+   DWORD Unknown6)
 {
-   PSYSTEM_CURSORINFO CurInfo;
    UNICODE_STRING WindowStationName;
    UNICODE_STRING FullWindowStationName;
    PWINSTATION_OBJECT WindowStationObject;
@@ -444,8 +441,8 @@ NtUserCreateWindowStation(
    Status = ObOpenObjectByName(
                &ObjectAttributes,
                ExWindowStationObjectType,
-               NULL,
                KernelMode,
+               NULL,
                dwDesiredAccess,
                NULL,
                (PVOID*)&WindowStation);
@@ -485,6 +482,17 @@ NtUserCreateWindowStation(
       return 0;
    }
 
+   /* Zero out the buffer */
+   RtlZeroMemory(WindowStationObject, sizeof(WINSTATION_OBJECT));
+
+   KeInitializeSpinLock(&WindowStationObject->Lock);
+
+   InitializeListHead(&WindowStationObject->DesktopListHead);
+
+   WindowStationObject->AtomTable = NULL;
+   Status = RtlCreateAtomTable(37, &WindowStationObject->AtomTable);
+   WindowStationObject->SystemMenuTemplate = (HANDLE)0;
+
    WindowStationObject->Name = WindowStationName;
 
    Status = ObInsertObject(
@@ -509,38 +517,23 @@ NtUserCreateWindowStation(
     * Initialize the new window station object
     */
 
-   if(!(CurInfo = ExAllocatePool(PagedPool, sizeof(SYSTEM_CURSORINFO))))
-   {
-      ExFreePool(FullWindowStationName.Buffer);
-      /* FIXME - Delete window station object */
-      ObDereferenceObject(WindowStationObject);
-      SetLastNtError(STATUS_INSUFFICIENT_RESOURCES);
-      return 0;
-   }
-
-   CurInfo->Enabled = FALSE;
-   CurInfo->ButtonsDown = 0;
-   CurInfo->CursorClipInfo.IsClipped = FALSE;
-   CurInfo->LastBtnDown = 0;
-   CurInfo->CurrentCursorObject = NULL;
-   CurInfo->ShowingCursor = 0;
+   WindowStationObject->ScreenSaverRunning = FALSE;
 
-   /* FIXME: Obtain the following information from the registry */
-   CurInfo->SwapButtons = FALSE;
-   CurInfo->DblClickSpeed = 500;
-   CurInfo->DblClickWidth = 4;
-   CurInfo->DblClickHeight = 4;
+   WindowStationObject->FlatMenu = FALSE;
 
-   WindowStationObject->SystemCursor = CurInfo;
+   if (!IntSetupClipboard(WindowStationObject))
+   {
+       DPRINT1("WindowStation: Error Setting up the clipboard!!!\n");
+   }
 
-   if (!IntSetupCurIconHandles(WindowStationObject))
+   if (InputWindowStation == NULL)
    {
-      DPRINT1("Setting up the Cursor/Icon Handle table failed!\n");
-      /* FIXME: Complain more loudly? */
-      ExFreePool(FullWindowStationName.Buffer);
+      InputWindowStation = WindowStationObject;
+
+      InitCursorImpl();
    }
 
-   DPRINT("Window station successfully created (%wZ)\n", FullWindowStationName);
+   DPRINT("Window station successfully created (%wZ)\n", &FullWindowStationName);
    ExFreePool(FullWindowStationName.Buffer);
    return WindowStation;
 }
@@ -569,7 +562,7 @@ NtUserCreateWindowStation(
  *    @implemented
  */
 
-HWINSTA STDCALL
+HWINSTA APIENTRY
 NtUserOpenWindowStation(
    PUNICODE_STRING lpszWindowStationName,
    ACCESS_MASK dwDesiredAccess)
@@ -596,15 +589,15 @@ NtUserOpenWindowStation(
    InitializeObjectAttributes(
       &ObjectAttributes,
       &WindowStationName,
-      0,
+      OBJ_CASE_INSENSITIVE,
       NULL,
       NULL);
 
    Status = ObOpenObjectByName(
                &ObjectAttributes,
                ExWindowStationObjectType,
+               KernelMode,
                NULL,
-               UserMode,
                dwDesiredAccess,
                NULL,
                (PVOID*)&WindowStation);
@@ -644,7 +637,7 @@ NtUserOpenWindowStation(
  */
 
 BOOL
-STDCALL
+APIENTRY
 NtUserCloseWindowStation(
    HWINSTA hWinSta)
 {
@@ -653,6 +646,11 @@ NtUserCloseWindowStation(
 
    DPRINT("About to close window station handle (0x%X)\n", hWinSta);
 
+       if (hWinSta == UserGetProcessWindowStation())
+       {
+               return FALSE;
+       }
+
    Status = IntValidateWindowStationHandle(
                hWinSta,
                KernelMode,
@@ -665,12 +663,6 @@ NtUserCloseWindowStation(
       return FALSE;
    }
 
-#if 0
-   /* FIXME - free the cursor information when actually deleting the object!! */
-   ASSERT(Object->SystemCursor);
-   ExFreePool(Object->SystemCursor);
-#endif
-
    ObDereferenceObject(Object);
 
    DPRINT("Closing window station handle (0x%X)\n", hWinSta);
@@ -725,7 +717,7 @@ NtUserCloseWindowStation(
  *    @unimplemented
  */
 
-BOOL STDCALL
+BOOL APIENTRY
 NtUserGetObjectInformation(
    HANDLE hObject,
    DWORD nIndex,
@@ -734,7 +726,7 @@ NtUserGetObjectInformation(
    PDWORD nLengthNeeded)
 {
    PWINSTATION_OBJECT WinStaObject = NULL;
-   PDESKTOP_OBJECT DesktopObject = NULL;
+   PDESKTOP DesktopObject = NULL;
    NTSTATUS Status;
    PVOID pvData = NULL;
    DWORD nDataSize = 0;
@@ -877,7 +869,7 @@ NtUserGetObjectInformation(
  */
 
 BOOL
-STDCALL
+APIENTRY
 NtUserSetObjectInformation(
    HANDLE hObject,
    DWORD nIndex,
@@ -897,6 +889,7 @@ HWINSTA FASTCALL
 UserGetProcessWindowStation(VOID)
 {
    NTSTATUS Status;
+   PTHREADINFO pti;
    HWINSTA WinSta;
 
    if(PsGetCurrentProcess() != CsrProcess)
@@ -906,7 +899,8 @@ UserGetProcessWindowStation(VOID)
    else
    {
       DPRINT1("Should use ObFindHandleForObject\n");
-      Status = ObOpenObjectByPointer(PsGetWin32Thread()->Desktop->WindowStation,
+      pti = PsGetCurrentThreadWin32Thread();
+      Status = ObOpenObjectByPointer(pti->rpdesk->rpwinstaParent,
                                      0,
                                      NULL,
                                      WINSTA_ALL_ACCESS,
@@ -939,7 +933,7 @@ UserGetProcessWindowStation(VOID)
  *    @implemented
  */
 
-HWINSTA STDCALL
+HWINSTA APIENTRY
 NtUserGetProcessWindowStation(VOID)
 {
    return UserGetProcessWindowStation();
@@ -949,19 +943,22 @@ PWINSTATION_OBJECT FASTCALL
 IntGetWinStaObj(VOID)
 {
    PWINSTATION_OBJECT WinStaObj;
+   PTHREADINFO Win32Thread;
+   PEPROCESS CurrentProcess;
 
    /*
     * just a temporary hack, this will be gone soon
     */
 
-   if(PsGetWin32Thread() != NULL && PsGetWin32Thread()->Desktop != NULL)
+   Win32Thread = PsGetCurrentThreadWin32Thread();
+   if(Win32Thread != NULL && Win32Thread->rpdesk != NULL)
    {
-      WinStaObj = PsGetWin32Thread()->Desktop->WindowStation;
+      WinStaObj = Win32Thread->rpdesk->rpwinstaParent;
       ObReferenceObjectByPointer(WinStaObj, KernelMode, ExWindowStationObjectType, 0);
    }
-   else if(PsGetCurrentProcess() != CsrProcess)
+   else if((CurrentProcess = PsGetCurrentProcess()) != CsrProcess)
    {
-      NTSTATUS Status = IntValidateWindowStationHandle(PsGetCurrentProcess()->Win32WindowStation,
+      NTSTATUS Status = IntValidateWindowStationHandle(CurrentProcess->Win32WindowStation,
                         KernelMode,
                         0,
                         &WinStaObj);
@@ -995,7 +992,7 @@ IntGetWinStaObj(VOID)
  *    @implemented
  */
 
-BOOL STDCALL
+BOOL APIENTRY
 NtUserSetProcessWindowStation(HWINSTA hWindowStation)
 {
    HANDLE hOld;
@@ -1048,7 +1045,7 @@ NtUserSetProcessWindowStation(HWINSTA hWindowStation)
  *    @implemented
  */
 
-BOOL STDCALL
+BOOL APIENTRY
 NtUserLockWindowStation(HWINSTA hWindowStation)
 {
    PWINSTATION_OBJECT Object;
@@ -1057,7 +1054,7 @@ NtUserLockWindowStation(HWINSTA hWindowStation)
    DPRINT("About to set process window station with handle (0x%X)\n",
           hWindowStation);
 
-   if(PsGetWin32Process() != LogonProcess)
+   if(PsGetCurrentProcessWin32Process() != LogonProcess)
    {
       DPRINT1("Unauthorized process attempted to lock the window station!\n");
       SetLastWin32Error(ERROR_ACCESS_DENIED);
@@ -1092,7 +1089,7 @@ NtUserLockWindowStation(HWINSTA hWindowStation)
  *    @implemented
  */
 
-BOOL STDCALL
+BOOL APIENTRY
 NtUserUnlockWindowStation(HWINSTA hWindowStation)
 {
    PWINSTATION_OBJECT Object;
@@ -1102,7 +1099,7 @@ NtUserUnlockWindowStation(HWINSTA hWindowStation)
    DPRINT("About to set process window station with handle (0x%X)\n",
           hWindowStation);
 
-   if(PsGetWin32Process() != LogonProcess)
+   if(PsGetCurrentProcessWin32Process() != LogonProcess)
    {
       DPRINT1("Unauthorized process attempted to unlock the window station!\n");
       SetLastWin32Error(ERROR_ACCESS_DENIED);
@@ -1136,7 +1133,7 @@ NtUserUnlockWindowStation(HWINSTA hWindowStation)
  *    @unimplemented
  */
 
-DWORD STDCALL
+DWORD APIENTRY
 NtUserSetWindowStationUser(
    DWORD Unknown0,
    DWORD Unknown1,
@@ -1233,7 +1230,7 @@ BuildWindowStationNameList(
                      FALSE, &Context, NULL))
          {
             /* Something went wrong, maybe someone added a directory entry? Just give up. */
-            ExFreePool(Buffer);
+            ExFreePoolWithTag(Buffer, TAG_WINSTA);
             ObDereferenceObject(DirectoryHandle);
             return NT_SUCCESS(Status) ? STATUS_INTERNAL_ERROR : Status;
          }
@@ -1247,10 +1244,10 @@ BuildWindowStationNameList(
     */
    ReturnLength = sizeof(DWORD);
    EntryCount = 0;
-   for (DirEntry = (POBJECT_DIRECTORY_INFORMATION) Buffer; 0 != DirEntry->ObjectName.Length;
+   for (DirEntry = (POBJECT_DIRECTORY_INFORMATION) Buffer; 0 != DirEntry->Name.Length;
          DirEntry++)
    {
-      ReturnLength += DirEntry->ObjectName.Length + sizeof(WCHAR);
+      ReturnLength += DirEntry->Name.Length + sizeof(WCHAR);
       EntryCount++;
    }
    DPRINT("Required size: %d Entry count: %d\n", ReturnLength, EntryCount);
@@ -1261,7 +1258,7 @@ BuildWindowStationNameList(
       {
          if (Buffer != InitialBuffer)
          {
-            ExFreePool(Buffer);
+            ExFreePoolWithTag(Buffer, TAG_WINSTA);
          }
          return STATUS_BUFFER_TOO_SMALL;
       }
@@ -1274,7 +1271,7 @@ BuildWindowStationNameList(
    {
       if (Buffer != InitialBuffer)
       {
-         ExFreePool(Buffer);
+         ExFreePoolWithTag(Buffer, TAG_WINSTA);
       }
       return STATUS_BUFFER_TOO_SMALL;
    }
@@ -1294,10 +1291,10 @@ BuildWindowStationNameList(
    lpBuffer = (PVOID) ((PCHAR) lpBuffer + sizeof(DWORD));
 
    NullWchar = L'\0';
-   for (DirEntry = (POBJECT_DIRECTORY_INFORMATION) Buffer; 0 != DirEntry->ObjectName.Length;
+   for (DirEntry = (POBJECT_DIRECTORY_INFORMATION) Buffer; 0 != DirEntry->Name.Length;
          DirEntry++)
    {
-      Status = MmCopyToCaller(lpBuffer, DirEntry->ObjectName.Buffer, DirEntry->ObjectName.Length);
+      Status = MmCopyToCaller(lpBuffer, DirEntry->Name.Buffer, DirEntry->Name.Length);
       if (! NT_SUCCESS(Status))
       {
          if (Buffer != InitialBuffer)
@@ -1306,7 +1303,7 @@ BuildWindowStationNameList(
          }
          return Status;
       }
-      lpBuffer = (PVOID) ((PCHAR) lpBuffer + DirEntry->ObjectName.Length);
+      lpBuffer = (PVOID) ((PCHAR) lpBuffer + DirEntry->Name.Length);
       Status = MmCopyToCaller(lpBuffer, &NullWchar, sizeof(WCHAR));
       if (! NT_SUCCESS(Status))
       {
@@ -1341,7 +1338,7 @@ BuildDesktopNameList(
    PWINSTATION_OBJECT WindowStation;
    KIRQL OldLevel;
    PLIST_ENTRY DesktopEntry;
-   PDESKTOP_OBJECT DesktopObject;
+   PDESKTOP DesktopObject;
    DWORD EntryCount;
    ULONG ReturnLength;
    WCHAR NullWchar;
@@ -1366,7 +1363,7 @@ BuildDesktopNameList(
          DesktopEntry != &WindowStation->DesktopListHead;
          DesktopEntry = DesktopEntry->Flink)
    {
-      DesktopObject = CONTAINING_RECORD(DesktopEntry, DESKTOP_OBJECT, ListEntry);
+      DesktopObject = CONTAINING_RECORD(DesktopEntry, DESKTOP, ListEntry);
       ReturnLength += ((PUNICODE_STRING)GET_DESKTOP_NAME(DesktopObject))->Length + sizeof(WCHAR);
       EntryCount++;
    }
@@ -1409,7 +1406,7 @@ BuildDesktopNameList(
          DesktopEntry != &WindowStation->DesktopListHead;
          DesktopEntry = DesktopEntry->Flink)
    {
-      DesktopObject = CONTAINING_RECORD(DesktopEntry, DESKTOP_OBJECT, ListEntry);
+      DesktopObject = CONTAINING_RECORD(DesktopEntry, DESKTOP, ListEntry);
       Status = MmCopyToCaller(lpBuffer, ((PUNICODE_STRING)GET_DESKTOP_NAME(DesktopObject))->Buffer, ((PUNICODE_STRING)GET_DESKTOP_NAME(DesktopObject))->Length);
       if (! NT_SUCCESS(Status))
       {
@@ -1463,7 +1460,7 @@ BuildDesktopNameList(
  *    @implemented
  */
 
-NTSTATUS STDCALL
+NTSTATUS APIENTRY
 NtUserBuildNameList(
    HWINSTA hWindowStation,
    ULONG dwSize,