[CMAKE]
[reactos.git] / subsystems / win32 / win32k / main / dllmain.c
index 3d5e0aa..5ed49f1 100644 (file)
@@ -1,23 +1,9 @@
 /*
- *  ReactOS W32 Subsystem
- *  Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 ReactOS Team
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  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.,
- *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-/*
- *  Entry Point for win32k.sys
+ * COPYRIGHT:        See COPYING in the top level directory
+ * PROJECT:          ReactOS kernel
+ * PURPOSE:          Driver entry and initialization of win32k
+ * FILE:             subsystems/win32/win32k/main/main.c
+ * PROGRAMER:
  */
 
 #include <win32k.h>
@@ -30,17 +16,12 @@ HANDLE hModuleWin;
 
 PGDI_HANDLE_TABLE INTERNAL_CALL GDIOBJ_iAllocHandleTable(OUT PSECTION_OBJECT *SectionObject);
 BOOL INTERNAL_CALL GDI_CleanupForProcess (struct _EPROCESS *Process);
-/* FIXME */
-PGDI_HANDLE_TABLE GdiHandleTable = NULL;
-PSECTION_OBJECT GdiTableSection = NULL;
 
 HANDLE GlobalUserHeap = NULL;
 PSECTION_OBJECT GlobalUserHeapSection = NULL;
 
 PSERVERINFO gpsi = NULL; // Global User Server Information.
 
-HSEMAPHORE hsemDriverMgmt = NULL;
-
 SHORT gusLanguageID;
 
 extern ULONG_PTR Win32kSSDT[];
@@ -77,92 +58,101 @@ Win32kProcessCallback(struct _EPROCESS *Process,
         /* FIXME - unlock the process */
     }
 
-  if (Create)
+    if (Create)
     {
-      SIZE_T ViewSize = 0;
-      LARGE_INTEGER Offset;
-      PVOID UserBase = NULL;
-      NTSTATUS Status;
-      extern PSECTION_OBJECT GlobalUserHeapSection;
-      DPRINT("Creating W32 process PID:%d at IRQ level: %lu\n", Process->UniqueProcessId, KeGetCurrentIrql());
-
-      /* map the global heap into the process */
-      Offset.QuadPart = 0;
-      Status = MmMapViewOfSection(GlobalUserHeapSection,
-                                  PsGetCurrentProcess(),
-                                  &UserBase,
-                                  0,
-                                  0,
-                                  &Offset,
-                                  &ViewSize,
-                                  ViewUnmap,
-                                  SEC_NO_CHANGE,
-                                  PAGE_EXECUTE_READ); /* would prefer PAGE_READONLY, but thanks to RTL heaps... */
-      if (!NT_SUCCESS(Status))
-      {
-          DPRINT1("Failed to map the global heap! 0x%x\n", Status);
-          RETURN(Status);
-      }
-      Win32Process->HeapMappings.Next = NULL;
-      Win32Process->HeapMappings.KernelMapping = (PVOID)GlobalUserHeap;
-      Win32Process->HeapMappings.UserMapping = UserBase;
-      Win32Process->HeapMappings.Count = 1;
-
-      InitializeListHead(&Win32Process->ClassList);
-
-      InitializeListHead(&Win32Process->MenuListHead);
-
-      InitializeListHead(&Win32Process->GDIBrushAttrFreeList);
-      InitializeListHead(&Win32Process->GDIDcAttrFreeList);
-
-      InitializeListHead(&Win32Process->PrivateFontListHead);
-      ExInitializeFastMutex(&Win32Process->PrivateFontListLock);
-
-      InitializeListHead(&Win32Process->DriverObjListHead);
-      ExInitializeFastMutex(&Win32Process->DriverObjListLock);
-
-      Win32Process->KeyboardLayout = W32kGetDefaultKeyLayout();
-
-      if(Process->Peb != NULL)
-      {
-        /* map the gdi handle table to user land */
-        Process->Peb->GdiSharedHandleTable = GDI_MapHandleTable(GdiTableSection, Process);
-        Process->Peb->GdiDCAttributeList = GDI_BATCH_LIMIT;
-      }
-
-      Win32Process->peProcess = Process;
-      /* setup process flags */
-      Win32Process->W32PF_flags = 0;
+        SIZE_T ViewSize = 0;
+        LARGE_INTEGER Offset;
+        PVOID UserBase = NULL;
+        NTSTATUS Status;
+        extern PSECTION_OBJECT GlobalUserHeapSection;
+        DPRINT("Creating W32 process PID:%d at IRQ level: %lu\n", Process->UniqueProcessId, KeGetCurrentIrql());
+
+        /* map the global heap into the process */
+        Offset.QuadPart = 0;
+        Status = MmMapViewOfSection(GlobalUserHeapSection,
+                                    PsGetCurrentProcess(),
+                                    &UserBase,
+                                    0,
+                                    0,
+                                    &Offset,
+                                    &ViewSize,
+                                    ViewUnmap,
+                                    SEC_NO_CHANGE,
+                                    PAGE_EXECUTE_READ); /* would prefer PAGE_READONLY, but thanks to RTL heaps... */
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT1("Failed to map the global heap! 0x%x\n", Status);
+            RETURN(Status);
+        }
+        Win32Process->HeapMappings.Next = NULL;
+        Win32Process->HeapMappings.KernelMapping = (PVOID)GlobalUserHeap;
+        Win32Process->HeapMappings.UserMapping = UserBase;
+        Win32Process->HeapMappings.Count = 1;
+
+        InitializeListHead(&Win32Process->ClassList);
+
+        InitializeListHead(&Win32Process->MenuListHead);
+
+        InitializeListHead(&Win32Process->GDIBrushAttrFreeList);
+        InitializeListHead(&Win32Process->GDIDcAttrFreeList);
+
+        InitializeListHead(&Win32Process->PrivateFontListHead);
+        ExInitializeFastMutex(&Win32Process->PrivateFontListLock);
+
+        InitializeListHead(&Win32Process->DriverObjListHead);
+        ExInitializeFastMutex(&Win32Process->DriverObjListLock);
+
+        Win32Process->KeyboardLayout = W32kGetDefaultKeyLayout();
+        EngCreateEvent((PEVENT *)&Win32Process->InputIdleEvent);
+        KeInitializeEvent(Win32Process->InputIdleEvent, NotificationEvent, FALSE);
+
+        if(Process->Peb != NULL)
+        {
+            /* map the gdi handle table to user land */
+            Process->Peb->GdiSharedHandleTable = GDI_MapHandleTable(GdiTableSection, Process);
+            Process->Peb->GdiDCAttributeList = GDI_BATCH_LIMIT;
+        }
+
+        Win32Process->peProcess = Process;
+        /* setup process flags */
+        Win32Process->W32PF_flags = 0;
     }
-  else
+    else
     {
-      DPRINT("Destroying W32 process PID:%d at IRQ level: %lu\n", Process->UniqueProcessId, KeGetCurrentIrql());
-      IntCleanupMenus(Process, Win32Process);
-      IntCleanupCurIcons(Process, Win32Process);
-      CleanupMonitorImpl();
+        DPRINT("Destroying W32 process PID:%d at IRQ level: %lu\n", Process->UniqueProcessId, KeGetCurrentIrql());
+        Win32Process->W32PF_flags |= W32PF_TERMINATED;
+        if (Win32Process->InputIdleEvent)
+        {
+           EngFreeMem((PVOID)Win32Process->InputIdleEvent);
+           Win32Process->InputIdleEvent = NULL;
+        }
+
+        IntCleanupMenus(Process, Win32Process);
+        IntCleanupCurIcons(Process, Win32Process);
+        CleanupMonitorImpl();
 
-      /* no process windows should exist at this point, or the function will assert! */
-      DestroyProcessClasses(Win32Process);
+        /* no process windows should exist at this point, or the function will assert! */
+        DestroyProcessClasses(Win32Process);
 
-      GDI_CleanupForProcess(Process);
+        GDI_CleanupForProcess(Process);
 
-      co_IntGraphicsCheck(FALSE);
+        co_IntGraphicsCheck(FALSE);
 
-      /*
-       * Deregister logon application automatically
-       */
-      if(LogonProcess == Win32Process)
-      {
-        LogonProcess = NULL;
-      }
+        /*
+         * Deregister logon application automatically
+         */
+        if(LogonProcess == Win32Process)
+        {
+            LogonProcess = NULL;
+        }
     }
 
-  RETURN( STATUS_SUCCESS);
+    RETURN( STATUS_SUCCESS);
 
 CLEANUP:
-  UserLeave();
-  DPRINT("Leave Win32kProcessCallback, ret=%i\n",_ret_);
-  END_CLEANUP;
+    UserLeave();
+    DPRINT("Leave Win32kProcessCallback, ret=%i\n",_ret_);
+    END_CLEANUP;
 }
 
 
@@ -173,6 +163,7 @@ Win32kThreadCallback(struct _ETHREAD *Thread,
 {
     struct _EPROCESS *Process;
     PTHREADINFO Win32Thread;
+    int i;
     DECLARE_RETURN(NTSTATUS);
 
     DPRINT("Enter Win32kThreadCallback\n");
@@ -198,203 +189,240 @@ Win32kThreadCallback(struct _ETHREAD *Thread,
         PsSetThreadWin32Thread(Thread, Win32Thread);
         /* FIXME - unlock the process */
     }
-  if (Type == PsW32ThreadCalloutInitialize)
+    if (Type == PsW32ThreadCalloutInitialize)
     {
-      HWINSTA hWinSta = NULL;
-      PTEB pTeb;
-      HDESK hDesk = NULL;
-      NTSTATUS Status;
-      PUNICODE_STRING DesktopPath;
-      PRTL_USER_PROCESS_PARAMETERS ProcessParams = (Process->Peb ? Process->Peb->ProcessParameters : NULL);
-
-      DPRINT("Creating W32 thread TID:%d at IRQ level: %lu\n", Thread->Cid.UniqueThread, KeGetCurrentIrql());
-
-      InitializeListHead(&Win32Thread->WindowListHead);
-      InitializeListHead(&Win32Thread->W32CallbackListHead);
-      InitializeListHead(&Win32Thread->PtiLink);
-
-      /*
-       * inherit the thread desktop and process window station (if not yet inherited) from the process startup
-       * info structure. See documentation of CreateProcess()
-       */
-      DesktopPath = (ProcessParams ? ((ProcessParams->DesktopInfo.Length > 0) ? &ProcessParams->DesktopInfo : NULL) : NULL);
-      Status = IntParseDesktopPath(Process,
-                                   DesktopPath,
-                                   &hWinSta,
-                                   &hDesk);
-      if(NT_SUCCESS(Status))
-      {
-        if(hWinSta != NULL)
+        HWINSTA hWinSta = NULL;
+        PTEB pTeb;
+        HDESK hDesk = NULL;
+        NTSTATUS Status;
+        PUNICODE_STRING DesktopPath;
+        PRTL_USER_PROCESS_PARAMETERS ProcessParams = (Process->Peb ? Process->Peb->ProcessParameters : NULL);
+
+        DPRINT("Creating W32 thread TID:%d at IRQ level: %lu\n", Thread->Cid.UniqueThread, KeGetCurrentIrql());
+
+        InitializeListHead(&Win32Thread->WindowListHead);
+        InitializeListHead(&Win32Thread->W32CallbackListHead);
+        InitializeListHead(&Win32Thread->PtiLink);
+        for (i = 0; i < NB_HOOKS; i++)
         {
-          if(Process != CsrProcess)
-          {
-            HWINSTA hProcessWinSta = (HWINSTA)InterlockedCompareExchangePointer((PVOID)&Process->Win32WindowStation, (PVOID)hWinSta, NULL);
-            if(hProcessWinSta != NULL)
-            {
-              /* our process is already assigned to a different window station, we don't need the handle anymore */
-              NtClose(hWinSta);
-            }
-          }
-          else
-          {
-            NtClose(hWinSta);
-          }
+            InitializeListHead(&Win32Thread->aphkStart[i]);
         }
 
-        if (hDesk != NULL)
+        /*
+         * inherit the thread desktop and process window station (if not yet inherited) from the process startup
+         * info structure. See documentation of CreateProcess()
+         */
+        DesktopPath = (ProcessParams ? ((ProcessParams->DesktopInfo.Length > 0) ? &ProcessParams->DesktopInfo : NULL) : NULL);
+        Status = IntParseDesktopPath(Process,
+                                     DesktopPath,
+                                     &hWinSta,
+                                     &hDesk);
+        if(NT_SUCCESS(Status))
         {
-          PDESKTOP DesktopObject;
-          Win32Thread->rpdesk = NULL;
-          Status = ObReferenceObjectByHandle(hDesk,
-                                             0,
-                                             ExDesktopObjectType,
-                                             KernelMode,
-                                             (PVOID*)&DesktopObject,
-                                             NULL);
-          NtClose(hDesk);
-          if(NT_SUCCESS(Status))
-          {
-            if (!IntSetThreadDesktop(DesktopObject,
-                                     FALSE))
+            if(hWinSta != NULL)
             {
-              DPRINT1("Unable to set thread desktop\n");
+                if(Process != CsrProcess)
+                {
+                    HWINSTA hProcessWinSta = (HWINSTA)InterlockedCompareExchangePointer((PVOID)&Process->Win32WindowStation, (PVOID)hWinSta, NULL);
+                    if(hProcessWinSta != NULL)
+                    {
+                        /* our process is already assigned to a different window station, we don't need the handle anymore */
+                        NtClose(hWinSta);
+                    }
+                }
+                else
+                {
+                    NtClose(hWinSta);
+                }
             }
-          }
-          else
-          {
-            DPRINT1("Unable to reference thread desktop handle 0x%x\n", hDesk);
-          }
+
+            if (hDesk != NULL)
+            {
+                PDESKTOP DesktopObject;
+                Win32Thread->rpdesk = NULL;
+                Status = ObReferenceObjectByHandle(hDesk,
+                                                   0,
+                                                   ExDesktopObjectType,
+                                                   KernelMode,
+                                                   (PVOID*)&DesktopObject,
+                                                   NULL);
+                NtClose(hDesk);
+                if(NT_SUCCESS(Status))
+                {
+                    if (!IntSetThreadDesktop(DesktopObject,
+                                             FALSE))
+                    {
+                        DPRINT1("Unable to set thread desktop\n");
+                    }
+                }
+                else
+                {
+                    DPRINT1("Unable to reference thread desktop handle 0x%x\n", hDesk);
+                }
+            }
+        }
+        else
+        {
+           DPRINT1("No Desktop handle for this Thread!\n");
         }
-      }
-      Win32Thread->TIF_flags &= ~TIF_INCLEANUP;
-      co_IntDestroyCaret(Win32Thread);
-      Win32Thread->ppi = PsGetCurrentProcessWin32Process();
-      pTeb = NtCurrentTeb();
-      if (pTeb)
-      {
-          Win32Thread->pClientInfo = (PCLIENTINFO)pTeb->Win32ClientInfo;
-          Win32Thread->pClientInfo->pClientThreadInfo = NULL;
-      }
-      Win32Thread->MessageQueue = MsqCreateMessageQueue(Thread);
-      Win32Thread->KeyboardLayout = W32kGetDefaultKeyLayout();
-      Win32Thread->pEThread = Thread;
+        Win32Thread->TIF_flags &= ~TIF_INCLEANUP;
+        co_IntDestroyCaret(Win32Thread);
+        Win32Thread->ppi = PsGetCurrentProcessWin32Process();
+        if (Win32Thread->rpdesk && !Win32Thread->pDeskInfo)
+        {
+           Win32Thread->pDeskInfo = Win32Thread->rpdesk->pDeskInfo;
+        }
+        Win32Thread->MessageQueue = MsqCreateMessageQueue(Thread);
+        Win32Thread->KeyboardLayout = W32kGetDefaultKeyLayout();
+        pTeb = NtCurrentTeb();
+        if (pTeb)
+        { /* Attempt to startup client support which should have been initialized in IntSetThreadDesktop. */
+           PCLIENTINFO pci = (PCLIENTINFO)pTeb->Win32ClientInfo;
+           Win32Thread->pClientInfo = pci;
+           pci->pClientThreadInfo = NULL;
+           pci->ppi = Win32Thread->ppi;
+           pci->fsHooks = Win32Thread->fsHooks;
+           if (Win32Thread->KeyboardLayout) pci->hKL = Win32Thread->KeyboardLayout->hkl;
+           pci->dwTIFlags = Win32Thread->TIF_flags;
+           /* CI may not have been initialized. */
+           if (!pci->pDeskInfo && Win32Thread->pDeskInfo) 
+           {
+              if (!pci->ulClientDelta) pci->ulClientDelta = DesktopHeapGetUserDelta();
+
+              pci->pDeskInfo = (PVOID)((ULONG_PTR)Win32Thread->pDeskInfo - pci->ulClientDelta);
+           }
+        }
+        else
+        {
+           DPRINT1("No TEB for this Thread!\n");
+        }
+        Win32Thread->pEThread = Thread;
     }
-  else
+    else
     {
-      PSINGLE_LIST_ENTRY e;
-
-      DPRINT("Destroying W32 thread TID:%d at IRQ level: %lu\n", Thread->Cid.UniqueThread, KeGetCurrentIrql());
-
-      Win32Thread->TIF_flags |= TIF_INCLEANUP;
-      DceFreeThreadDCE(Win32Thread);
-      HOOK_DestroyThreadHooks(Thread);
-      /* Cleanup timers */
-      DestroyTimersForThread(Win32Thread);
-      KeSetEvent(Win32Thread->MessageQueue->NewMessages, IO_NO_INCREMENT, FALSE);
-      UnregisterThreadHotKeys(Thread);
-      /* what if this co_ func crash in umode? what will clean us up then? */
-      co_DestroyThreadWindows(Thread);
-      IntBlockInput(Win32Thread, FALSE);
-      MsqDestroyMessageQueue(Win32Thread->MessageQueue);
-      IntCleanupThreadCallbacks(Win32Thread);
-
-      /* cleanup user object references stack */
-      e = PopEntryList(&Win32Thread->ReferencesList);
-      while (e)
-      {
-         PUSER_REFERENCE_ENTRY ref = CONTAINING_RECORD(e, USER_REFERENCE_ENTRY, Entry);
-         DPRINT("thread clean: remove reference obj 0x%x\n",ref->obj);
-         UserDereferenceObject(ref->obj);
-
-         e = PopEntryList(&Win32Thread->ReferencesList);
-      }
-
-      IntSetThreadDesktop(NULL,
-                          TRUE);
-
-      PsSetThreadWin32Thread(Thread, NULL);
+        PSINGLE_LIST_ENTRY e;
+
+        DPRINT("Destroying W32 thread TID:%d at IRQ level: %lu\n", Thread->Cid.UniqueThread, KeGetCurrentIrql());
+
+        Win32Thread->TIF_flags |= TIF_INCLEANUP;
+        DceFreeThreadDCE(Win32Thread);
+        HOOK_DestroyThreadHooks(Thread);
+        EVENT_DestroyThreadEvents(Thread);
+        /* Cleanup timers */
+        DestroyTimersForThread(Win32Thread);
+        KeSetEvent(Win32Thread->MessageQueue->NewMessages, IO_NO_INCREMENT, FALSE);
+        UnregisterThreadHotKeys(Thread);
+        /* what if this co_ func crash in umode? what will clean us up then? */
+        co_DestroyThreadWindows(Thread);
+        IntBlockInput(Win32Thread, FALSE);
+        MsqDestroyMessageQueue(Win32Thread->MessageQueue);
+        IntCleanupThreadCallbacks(Win32Thread);
+
+        /* cleanup user object references stack */
+        e = PopEntryList(&Win32Thread->ReferencesList);
+        while (e)
+        {
+            PUSER_REFERENCE_ENTRY ref = CONTAINING_RECORD(e, USER_REFERENCE_ENTRY, Entry);
+            DPRINT("thread clean: remove reference obj 0x%x\n",ref->obj);
+            UserDereferenceObject(ref->obj);
+
+            e = PopEntryList(&Win32Thread->ReferencesList);
+        }
+
+        IntSetThreadDesktop(NULL,
+                            TRUE);
+
+        PsSetThreadWin32Thread(Thread, NULL);
     }
 
-  RETURN( STATUS_SUCCESS);
+    RETURN( STATUS_SUCCESS);
 
 CLEANUP:
-  UserLeave();
-  DPRINT("Leave Win32kThreadCallback, ret=%i\n",_ret_);
-  END_CLEANUP;
+    UserLeave();
+    DPRINT("Leave Win32kThreadCallback, ret=%i\n",_ret_);
+    END_CLEANUP;
 }
 
-/* Only used in ntuser/input.c KeyboardThreadMain(). If it's
-   not called there anymore, please delete */
 NTSTATUS
 Win32kInitWin32Thread(PETHREAD Thread)
 {
-  PEPROCESS Process;
+    PEPROCESS Process;
 
-  Process = Thread->ThreadsProcess;
+    Process = Thread->ThreadsProcess;
 
-  if (Process->Win32Process == NULL)
+    if (Process->Win32Process == NULL)
     {
-      /* FIXME - lock the process */
-      Process->Win32Process = ExAllocatePoolWithTag(NonPagedPool, sizeof(PROCESSINFO), USERTAG_PROCESSINFO);
+        /* FIXME - lock the process */
+        Process->Win32Process = ExAllocatePoolWithTag(NonPagedPool, sizeof(PROCESSINFO), USERTAG_PROCESSINFO);
 
-      if (Process->Win32Process == NULL)
-       return STATUS_NO_MEMORY;
+        if (Process->Win32Process == NULL)
+            return STATUS_NO_MEMORY;
 
-      RtlZeroMemory(Process->Win32Process, sizeof(PROCESSINFO));
-      /* FIXME - unlock the process */
+        RtlZeroMemory(Process->Win32Process, sizeof(PROCESSINFO));
+        /* FIXME - unlock the process */
 
-      Win32kProcessCallback(Process, TRUE);
+        Win32kProcessCallback(Process, TRUE);
     }
 
-  if (Thread->Tcb.Win32Thread == NULL)
+    if (Thread->Tcb.Win32Thread == NULL)
     {
-      Thread->Tcb.Win32Thread = ExAllocatePoolWithTag(NonPagedPool, sizeof(THREADINFO), USERTAG_THREADINFO);
-      if (Thread->Tcb.Win32Thread == NULL)
-       return STATUS_NO_MEMORY;
+        Thread->Tcb.Win32Thread = ExAllocatePoolWithTag(NonPagedPool, sizeof(THREADINFO), USERTAG_THREADINFO);
+        if (Thread->Tcb.Win32Thread == NULL)
+            return STATUS_NO_MEMORY;
 
-      RtlZeroMemory(Thread->Tcb.Win32Thread, sizeof(THREADINFO));
+        RtlZeroMemory(Thread->Tcb.Win32Thread, sizeof(THREADINFO));
 
-      Win32kThreadCallback(Thread, PsW32ThreadCalloutInitialize);
+        Win32kThreadCallback(Thread, PsW32ThreadCalloutInitialize);
     }
 
-  return(STATUS_SUCCESS);
+    return(STATUS_SUCCESS);
 }
 
 C_ASSERT(sizeof(SERVERINFO) <= PAGE_SIZE);
 
+// Return on failure
+#define NT_ROF(x) \
+    Status = (x); \
+    if (!NT_SUCCESS(Status)) \
+    { \
+        DPRINT1("Failed '%s' (0x%lx)\n", #x, Status); \
+        return Status; \
+    }
+
 /*
  * This definition doesn't work
  */
-NTSTATUS APIENTRY
-DriverEntry (
-  IN   PDRIVER_OBJECT  DriverObject,
-  IN   PUNICODE_STRING RegistryPath)
+INIT_FUNCTION
+NTSTATUS
+APIENTRY
+DriverEntry(
+    IN PDRIVER_OBJECT  DriverObject,
+    IN PUNICODE_STRING RegistryPath)
 {
-  NTSTATUS Status;
-  BOOLEAN Result;
-  WIN32_CALLOUTS_FPNS CalloutData = {0};
-  PVOID GlobalUserHeapBase = NULL;
-
-  /*
-   * Register user mode call interface
-   * (system service table index = 1)
-   */
-  Result = KeAddSystemServiceTable (Win32kSSDT,
-                                   NULL,
-                                   Win32kNumberOfSysCalls,
-                                   Win32kSSPT,
-                                   1);
-  if (Result == FALSE)
-    {
-      DPRINT1("Adding system services failed!\n");
-      return STATUS_UNSUCCESSFUL;
-    }
+    NTSTATUS Status;
+    BOOLEAN Result;
+    WIN32_CALLOUTS_FPNS CalloutData = {0};
+    PVOID GlobalUserHeapBase = NULL;
 
-  hModuleWin = MmPageEntireDriver(DriverEntry);
-  DPRINT("Win32k hInstance 0x%x!\n",hModuleWin);
     /*
-     * Register Object Manager Callbacks
+     * Register user mode call interface
+     * (system service table index = 1)
      */
+    Result = KeAddSystemServiceTable(Win32kSSDT,
+                                     NULL,
+                                     Win32kNumberOfSysCalls,
+                                     Win32kSSPT,
+                                     1);
+    if (Result == FALSE)
+    {
+        DPRINT1("Adding system services failed!\n");
+        return STATUS_UNSUCCESSFUL;
+    }
+
+    hModuleWin = MmPageEntireDriver(DriverEntry);
+    DPRINT("Win32k hInstance 0x%x!\n",hModuleWin);
+
+    /* Register Object Manager Callbacks */
     CalloutData.WindowStationParseProcedure = IntWinStaObjectParse;
     CalloutData.WindowStationDeleteProcedure = IntWinStaObjectDelete;
     CalloutData.DesktopDeleteProcedure = IntDesktopObjectDelete;
@@ -402,11 +430,10 @@ DriverEntry (
     CalloutData.ThreadCallout = Win32kThreadCallback;
     CalloutData.BatchFlushRoutine = NtGdiFlushUserBatch;
 
-    /*
-     * Register our per-process and per-thread structures.
-     */
+    /* Register our per-process and per-thread structures. */
     PsEstablishWin32Callouts((PWIN32_CALLOUTS_FPNS)&CalloutData);
 
+    /* Create the global USER heap */
     GlobalUserHeap = UserCreateHeap(&GlobalUserHeapSection,
                                     &GlobalUserHeapBase,
                                     1 * 1024 * 1024); /* FIXME - 1 MB for now... */
@@ -416,150 +443,54 @@ DriverEntry (
         return STATUS_UNSUCCESSFUL;
     }
 
-   if (!gpsi)
-   {
-      gpsi = UserHeapAlloc(sizeof(SERVERINFO));
-      if (gpsi)
-      {
-         RtlZeroMemory(gpsi, sizeof(SERVERINFO));
-         DPRINT("Global Server Data -> %x\n", gpsi);
-      }
-      else
-      {
-          ASSERT(FALSE);
-      }
-   }
-
-  if(!hsemDriverMgmt) hsemDriverMgmt = EngCreateSemaphore();
-
-  GdiHandleTable = GDIOBJ_iAllocHandleTable(&GdiTableSection);
-  if (GdiHandleTable == NULL)
-  {
-      DPRINT1("Failed to initialize the GDI handle table.\n");
-      return STATUS_UNSUCCESSFUL;
-  }
-
-  /* Initialize default palettes */
-  PALETTE_Init();
-
-  /* Create stock objects, ie. precreated objects commonly
-     used by win32 applications */
-  CreateStockObjects();
-  CreateSysColorObjects();
-
-  InitXlateImpl();
-  InitPDEVImpl();
-  InitLDEVImpl();
-  InitDeviceImpl();
-
-  Status = InitDcImpl();
-  if (!NT_SUCCESS(Status))
-  {
-    DPRINT1("Failed to initialize Device context implementation!\n");
-    return STATUS_UNSUCCESSFUL;
-  }
-
-  Status = InitUserImpl();
-  if (!NT_SUCCESS(Status))
-  {
-    DPRINT1("Failed to initialize user implementation!\n");
-    return STATUS_UNSUCCESSFUL;
-  }
-
-  Status = InitHotkeyImpl();
-  if (!NT_SUCCESS(Status))
-  {
-    DPRINT1("Failed to initialize hotkey implementation!\n");
-    return STATUS_UNSUCCESSFUL;
-  }
-
-  Status = InitWindowStationImpl();
-  if (!NT_SUCCESS(Status))
-  {
-    DPRINT1("Failed to initialize window station implementation!\n");
-    return STATUS_UNSUCCESSFUL;
-  }
-
-  Status = InitDesktopImpl();
-  if (!NT_SUCCESS(Status))
-  {
-    DPRINT1("Failed to initialize desktop implementation!\n");
-    return STATUS_UNSUCCESSFUL;
-  }
-
-  Status = InitWindowImpl();
-  if (!NT_SUCCESS(Status))
-  {
-    DPRINT1("Failed to initialize window implementation!\n");
-    return STATUS_UNSUCCESSFUL;
-  }
-
-  Status = InitMenuImpl();
-  if (!NT_SUCCESS(Status))
-  {
-    DPRINT1("Failed to initialize menu implementation!\n");
-    return STATUS_UNSUCCESSFUL;
-  }
-
-  Status = InitInputImpl();
-  if (!NT_SUCCESS(Status))
-    {
-      DPRINT1("Failed to initialize input implementation.\n");
-      return(Status);
-    }
-
-  Status = InitKeyboardImpl();
-  if (!NT_SUCCESS(Status))
-    {
-      DPRINT1("Failed to initialize keyboard implementation.\n");
-      return(Status);
-    }
-
-  Status = InitMonitorImpl();
-  if (!NT_SUCCESS(Status))
-    {
-      DbgPrint("Failed to initialize monitor implementation!\n");
-      return STATUS_UNSUCCESSFUL;
-    }
-
-  Status = MsqInitializeImpl();
-  if (!NT_SUCCESS(Status))
-    {
-      DPRINT1("Failed to initialize message queue implementation.\n");
-      return(Status);
-    }
-
-  Status = InitTimerImpl();
-  if (!NT_SUCCESS(Status))
-    {
-      DPRINT1("Failed to initialize timer implementation.\n");
-      return(Status);
-    }
-
-  Status = InitAcceleratorImpl();
-  if (!NT_SUCCESS(Status))
+    /* Allocate global server info structure */
+    gpsi = UserHeapAlloc(sizeof(SERVERINFO));
+    if (!gpsi)
     {
-      DPRINT1("Failed to initialize accelerator implementation.\n");
-      return(Status);
-    }
-
-  Status = InitGuiCheckImpl();
-  if (!NT_SUCCESS(Status))
-    {
-      DPRINT1("Failed to initialize GUI check implementation.\n");
-      return(Status);
+        DPRINT1("Failed allocate server info structure!\n");
+        return STATUS_UNSUCCESSFUL;
     }
 
-  /* Initialize FreeType library */
-  if (! InitFontSupport())
+    RtlZeroMemory(gpsi, sizeof(SERVERINFO));
+    DPRINT("Global Server Data -> %x\n", gpsi);
+
+    NT_ROF(InitGdiHandleTable());
+    NT_ROF(InitPaletteImpl());
+
+    /* Create stock objects, ie. precreated objects commonly
+       used by win32 applications */
+    CreateStockObjects();
+    CreateSysColorObjects();
+
+    NT_ROF(InitXlateImpl());
+    NT_ROF(InitPDEVImpl());
+    NT_ROF(InitLDEVImpl());
+    NT_ROF(InitDeviceImpl());
+    NT_ROF(InitDcImpl());
+    NT_ROF(InitUserImpl());
+    NT_ROF(InitHotkeyImpl());
+    NT_ROF(InitWindowStationImpl());
+    NT_ROF(InitDesktopImpl());
+    NT_ROF(InitWindowImpl());
+    NT_ROF(InitMenuImpl());
+    NT_ROF(InitInputImpl());
+    NT_ROF(InitKeyboardImpl());
+    NT_ROF(InitMonitorImpl());
+    NT_ROF(MsqInitializeImpl());
+    NT_ROF(InitTimerImpl());
+    NT_ROF(InitAcceleratorImpl());
+    NT_ROF(InitGuiCheckImpl());
+
+    /* Initialize FreeType library */
+    if (!InitFontSupport())
     {
-      DPRINT1("Unable to initialize font support\n");
-      return STATUS_UNSUCCESSFUL;
+        DPRINT1("Unable to initialize font support\n");
+        return Status;
     }
 
-  gusLanguageID = IntGdiGetLanguageID();
+    gusLanguageID = IntGdiGetLanguageID();
 
-  return STATUS_SUCCESS;
+    return STATUS_SUCCESS;
 }
 
 /* EOF */