[CMAKE]
[reactos.git] / subsystems / win32 / win32k / eng / pdevobj.c
index 9ca4311..9d1ee12 100644 (file)
 #define NDEBUG
 #include <debug.h>
 
-static PPDEVOBJ gppdevList = NULL;
 PPDEVOBJ gppdevPrimary = NULL;
+
+static PPDEVOBJ gppdevList = NULL;
 static HSEMAPHORE ghsemPDEV;
 
-BOOL
+INIT_FUNCTION
+NTSTATUS
 NTAPI
 InitPDEVImpl()
 {
     ghsemPDEV = EngCreateSemaphore();
-    return TRUE;
+    if (!ghsemPDEV) return STATUS_INSUFFICIENT_RESOURCES;
+    return STATUS_SUCCESS;
 }
 
 
@@ -114,7 +117,7 @@ PDEVOBJ_bEnablePDEV(
 {
     PFN_DrvEnablePDEV pfnEnablePDEV;
 
-    DPRINT1("PDEVOBJ_bEnablePDEV()\n");
+    DPRINT("PDEVOBJ_bEnablePDEV()\n");
 
     /* Get the DrvEnablePDEV function */
     pfnEnablePDEV = ppdev->pldev->pfn.EnablePDEV;
@@ -140,10 +143,9 @@ PDEVOBJ_bEnablePDEV(
         ppdev->gdiinfo.ulLogPixelsY = 96;
 
     /* Setup Palette */
-    GDIOBJ_SetOwnership(ppdev->devinfo.hpalDefault, NULL);
     ppdev->ppalSurf = PALETTE_ShareLockPalette(ppdev->devinfo.hpalDefault);
 
-    DPRINT1("PDEVOBJ_bEnablePDEV - dhpdev = %p\n", ppdev->dhpdev);
+    DPRINT("PDEVOBJ_bEnablePDEV - dhpdev = %p\n", ppdev->dhpdev);
 
     return TRUE;
 }
@@ -168,7 +170,7 @@ PDEVOBJ_pSurface(
     if (ppdev->pSurface)
     {
         /* Increment reference count */
-        GDIOBJ_IncrementShareCount(&ppdev->pSurface->BaseObject);
+        GDIOBJ_vReferenceObjectByPointer(&ppdev->pSurface->BaseObject);
     }
     else
     {
@@ -192,6 +194,7 @@ PDEVOBJ_pdmMatchDevMode(
     PGRAPHICS_DEVICE pGraphicsDevice;
     PDEVMODEW pdmCurrent;
     INT i;
+    DWORD dwFields;
 
     pGraphicsDevice = ppdev->pGraphicsDevice;
 
@@ -199,22 +202,31 @@ PDEVOBJ_pdmMatchDevMode(
     {
         pdmCurrent = pGraphicsDevice->pDevModeList[i].pdm;
 
-        /* Compare DEVMODE fields */
-        if (pdmCurrent->dmBitsPerPel == pdm->dmBitsPerPel &&
-            pdmCurrent->dmPelsWidth == pdm->dmPelsWidth &&
-            pdmCurrent->dmPelsHeight == pdm->dmPelsHeight &&
-            pdmCurrent->dmDisplayFrequency == pdm->dmDisplayFrequency)
-        {
-            /* Match! Return the DEVMODE */
-            return pdmCurrent;
-        }
+        /* Compare asked DEVMODE fields
+         * Only compare those that are valid in both DEVMODE structs */
+        dwFields = pdmCurrent->dmFields & pdm->dmFields ;
+        /* For now, we only need those */
+        if ((dwFields & DM_BITSPERPEL) &&
+                (pdmCurrent->dmBitsPerPel != pdm->dmBitsPerPel))
+            continue;
+        if ((dwFields & DM_PELSWIDTH) &&
+                (pdmCurrent->dmPelsWidth != pdm->dmPelsWidth))
+            continue;
+        if ((dwFields & DM_PELSHEIGHT) &&
+                (pdmCurrent->dmPelsHeight != pdm->dmPelsHeight))
+            continue;
+        if ((dwFields & DM_DISPLAYFREQUENCY) &&
+                (pdmCurrent->dmDisplayFrequency != pdm->dmDisplayFrequency))
+            continue;
+
+        /* Match! Return the DEVMODE */
+        return pdmCurrent;
     }
 
     /* Nothing found */
     return NULL;
 }
 
-
 static
 PPDEVOBJ
 EngpCreatePDEV(
@@ -225,12 +237,19 @@ EngpCreatePDEV(
     PPDEVOBJ ppdev;
 
     /* Try to find the GRAPHICS_DEVICE */
-    pGraphicsDevice = EngpFindGraphicsDevice(pustrDeviceName, 0, 0);
-    if (!pGraphicsDevice)
+    if (pustrDeviceName)
     {
-        DPRINT1("No GRAPHICS_DEVICE found for %ls!\n",
-                pustrDeviceName ? pustrDeviceName->Buffer : 0);
-        return NULL;
+        pGraphicsDevice = EngpFindGraphicsDevice(pustrDeviceName, 0, 0);
+        if (!pGraphicsDevice)
+        {
+            DPRINT1("No GRAPHICS_DEVICE found for %ls!\n",
+                    pustrDeviceName ? pustrDeviceName->Buffer : 0);
+            return NULL;
+        }
+    }
+    else
+    {
+        pGraphicsDevice = gpPrimaryGraphicsDevice;
     }
 
     /* Allocate a new PDEVOBJ */
@@ -246,11 +265,11 @@ EngpCreatePDEV(
     {
         /* ... use the device's default one */
         pdm = pGraphicsDevice->pDevModeList[pGraphicsDevice->iDefaultMode].pdm;
-        DPRINT1("Using iDefaultMode = %ld\n", pGraphicsDevice->iDefaultMode);
+        DPRINT("Using iDefaultMode = %ld\n", pGraphicsDevice->iDefaultMode);
     }
 
     /* Try to get a diplay driver */
-    ppdev->pldev = EngLoadDriver(pdm->dmDeviceName, LDEV_DEVICE_DISPLAY);
+    ppdev->pldev = EngLoadImageEx(pdm->dmDeviceName, LDEV_DEVICE_DISPLAY);
     if (!ppdev->pldev)
     {
         DPRINT1("Could not load display driver '%ls'\n",
@@ -302,6 +321,7 @@ PDEVOBJ_vSwitchPdev(
     PPDEVOBJ ppdev2)
 {
     PDEVOBJ pdevTmp;
+    DWORD tmpStateFlags;
 
     /* Exchange data */
     pdevTmp = *ppdev;
@@ -334,7 +354,12 @@ PDEVOBJ_vSwitchPdev(
 
     /* Exchange DEVMODE */
     ppdev->pdmwDev = ppdev2->pdmwDev;
-    ppdev2->pdmwDev = pdevTmp.pdmwDev ;
+    ppdev2->pdmwDev = pdevTmp.pdmwDev;
+
+    /* Exchange state flags */
+    tmpStateFlags = ppdev->pGraphicsDevice->StateFlags;
+    ppdev->pGraphicsDevice->StateFlags = ppdev2->pGraphicsDevice->StateFlags;
+    ppdev2->pGraphicsDevice->StateFlags = tmpStateFlags;
 
     /* Notify each driver instance of its new HDEV association */
     ppdev->pfn.CompletePDEV(ppdev->dhpdev, (HDEV)ppdev);
@@ -471,6 +496,7 @@ EngpGetPDEV(
             if (!gppdevPrimary)
             {
                 gppdevPrimary = ppdev;
+                ppdev->pGraphicsDevice->StateFlags |= DISPLAY_DEVICE_PRIMARY_DEVICE;
             }
         }
     }
@@ -592,7 +618,7 @@ NtGdiGetDeviceCaps(
     pdc = DC_LockDc(hdc);
     if (!pdc)
     {
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        EngSetLastError(ERROR_INVALID_HANDLE);
         return 0;
     }
 
@@ -742,7 +768,7 @@ NtGdiGetDeviceCapsAll(
     pdc = DC_LockDc(hDC);
     if (!pdc)
     {
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        EngSetLastError(ERROR_INVALID_HANDLE);
         return FALSE;
     }