* Made it possible to include both d3d9types.h and ddrawgdi.h at the same time
[reactos.git] / reactos / dll / win32 / gdi32 / misc / gdientry.c
index f7bf389..84e7c19 100644 (file)
@@ -15,6 +15,7 @@
 #include <ddrawint.h>
 #include <ddrawgdi.h>
 #include <ntgdi.h>
+#include <d3dhal.h>
 
 /* DATA **********************************************************************/
 
@@ -72,7 +73,7 @@ DdDestroySurface(LPDDHAL_DESTROYSURFACEDATA pDestroySurface)
         {
             RealDestroy = FALSE;
         }
-        
+
         /* Call win32k */
         Return = NtGdiDdDestroySurface((HANDLE)pSurfaceLocal->hDDSurface,
                                        RealDestroy);
@@ -86,20 +87,20 @@ WINAPI
 DdFlip(LPDDHAL_FLIPDATA Flip)
 {
     /* Call win32k */
-       
+
     return NtGdiDdFlip( (HANDLE)Flip->lpSurfCurr->hDDSurface,
                         (HANDLE)Flip->lpSurfTarg->hDDSurface,
-   /* FIXME  the two last should be current left handler */ 
+   /* FIXME  the two last should be current left handler */
                                                (HANDLE)Flip->lpSurfCurr->hDDSurface,
                         (HANDLE)Flip->lpSurfTarg->hDDSurface,
                         (PDD_FLIPDATA)Flip);
 }
 
-WORD
+DWORD
 WINAPI
 DdLock(LPDDHAL_LOCKDATA Lock)
 {
-    /* Call win32k */  
+    /* Call win32k */
     return NtGdiDdLock((HANDLE)Lock->lpDDSurface->hDDSurface,
                         (PDD_LOCKDATA)Lock,
                                                (HANDLE)Lock->lpDDSurface->hDC);
@@ -209,16 +210,20 @@ DdCreateSurface(LPDDHAL_CREATESURFACEDATA pCreateSurface)
     DD_SURFACE_LOCAL DdSurfaceLocal;
     DD_SURFACE_MORE DdSurfaceMore;
     DD_SURFACE_GLOBAL DdSurfaceGlobal;
+
     HANDLE hPrevSurface, hSurface;
-    DD_SURFACE_LOCAL* pDdSurfaceLocal;
-    DD_SURFACE_MORE* pDdSurfaceMore;
-    DD_SURFACE_GLOBAL* pDdSurfaceGlobal;
-    LPDDRAWI_DDRAWSURFACE_LCL pSurfaceLocal;
-    //LPDDRAWI_DDRAWSURFACE_MORE pSurfaceMore;
-    LPDDRAWI_DDRAWSURFACE_GBL pSurfaceGlobal;
+
+
+    PDD_SURFACE_LOCAL pDdSurfaceLocal = NULL;
+    PDD_SURFACE_MORE pDdSurfaceMore = NULL;
+    PDD_SURFACE_GLOBAL pDdSurfaceGlobal = NULL;
+
+    PDD_SURFACE_LOCAL ptmpDdSurfaceLocal = NULL;
+    PDD_SURFACE_MORE ptmpDdSurfaceMore = NULL;
+    PDD_SURFACE_GLOBAL ptmpDdSurfaceGlobal = NULL;
     PHANDLE phSurface = NULL, puhSurface = NULL;
     ULONG i;
-    LPDDSURFACEDESC pSurfaceDesc;
+    LPDDSURFACEDESC pSurfaceDesc = NULL;
 
     /* Check how many surfaces there are */
     if (SurfaceCount != 1)
@@ -234,74 +239,149 @@ DdCreateSurface(LPDDHAL_CREATESURFACEDATA pCreateSurface)
         pDdSurfaceGlobal = &DdSurfaceGlobal;
         phSurface = &hPrevSurface;
         puhSurface = &hSurface;
-        
+
         /* Clear the structures */
         RtlZeroMemory(&DdSurfaceLocal, sizeof(DdSurfaceLocal));
         RtlZeroMemory(&DdSurfaceGlobal, sizeof(DdSurfaceGlobal));
-        RtlZeroMemory(&DdSurfaceMore, sizeof(DdSurfaceMore));  
+        RtlZeroMemory(&DdSurfaceMore, sizeof(DdSurfaceMore));
     }
 
-    /* Loop for each surface */
-    for (i = 0; i < pCreateSurface->dwSCnt; i++)
+    /* check if we got a surface or not */
+    if (SurfaceCount!=0)
     {
-        /* Get data */
-        pSurfaceLocal = pCreateSurface->lplpSList[i];
-        pSurfaceGlobal = pSurfaceLocal->lpGbl;
+        /* Loop for each surface */
+        ptmpDdSurfaceGlobal = pDdSurfaceGlobal;
+        ptmpDdSurfaceLocal = pDdSurfaceLocal;
+        ptmpDdSurfaceMore = pDdSurfaceMore;
         pSurfaceDesc = pCreateSurface->lpDDSurfaceDesc;
 
-        /* Check if it has pixel data */
-        if (pSurfaceDesc->dwFlags & DDRAWISURF_HASPIXELFORMAT)
-        {
-            /* Use its pixel data */
-            DdSurfaceGlobal.ddpfSurface = pSurfaceDesc->ddpfPixelFormat;
-            DdSurfaceGlobal.ddpfSurface.dwSize = sizeof(DDPIXELFORMAT);
-        }
-        else
+        for (i = 0; i < SurfaceCount; i++)
         {
-            /* Use the one from the global surface */
-            DdSurfaceGlobal.ddpfSurface = pSurfaceGlobal->lpDD->vmiData.ddpfDisplay;
+            LPDDRAWI_DDRAWSURFACE_LCL lcl = pCreateSurface->lplpSList[i];
+            LPDDRAWI_DDRAWSURFACE_GBL gpl = pCreateSurface->lplpSList[i]->lpGbl;
+
+            phSurface[i] = (HANDLE)lcl->hDDSurface;
+            ptmpDdSurfaceLocal->ddsCaps.dwCaps = lcl->ddsCaps.dwCaps;
+
+            ptmpDdSurfaceLocal->dwFlags = (ptmpDdSurfaceLocal->dwFlags &
+                                          (0xB0000000 | DDRAWISURF_INMASTERSPRITELIST |
+                                           DDRAWISURF_HELCB | DDRAWISURF_FRONTBUFFER |
+                                           DDRAWISURF_BACKBUFFER | DDRAWISURF_INVALID |
+                                           DDRAWISURF_DCIBUSY | DDRAWISURF_DCILOCK)) |
+                                          (lcl->dwFlags & DDRAWISURF_DRIVERMANAGED);
+
+            ptmpDdSurfaceGlobal->wWidth = gpl->wWidth;
+            ptmpDdSurfaceGlobal->wHeight = gpl->wHeight;
+            ptmpDdSurfaceGlobal->lPitch = gpl->lPitch;
+            ptmpDdSurfaceGlobal->fpVidMem = gpl->fpVidMem;
+            ptmpDdSurfaceGlobal->dwBlockSizeX = gpl->dwBlockSizeX;
+            ptmpDdSurfaceGlobal->dwBlockSizeY = gpl->dwBlockSizeY;
+
+            if (lcl->dwFlags & DDRAWISURF_HASPIXELFORMAT)
+            {
+                RtlCopyMemory( &ptmpDdSurfaceGlobal->ddpfSurface ,
+                               &gpl->ddpfSurface,
+                               sizeof(DDPIXELFORMAT));
+
+                ptmpDdSurfaceGlobal->ddpfSurface.dwSize = sizeof(DDPIXELFORMAT);
+            }
+            else
+            {
+                RtlCopyMemory( &ptmpDdSurfaceGlobal->ddpfSurface ,
+                               &gpl->lpDD->vmiData.ddpfDisplay,
+                               sizeof(DDPIXELFORMAT));
+            }
+
+            if (lcl->lpSurfMore)
+            {
+                ptmpDdSurfaceMore->ddsCapsEx.dwCaps2 = lcl->lpSurfMore->ddsCapsEx.dwCaps2;
+                ptmpDdSurfaceMore->ddsCapsEx.dwCaps3 = lcl->lpSurfMore->ddsCapsEx.dwCaps3;
+                ptmpDdSurfaceMore->ddsCapsEx.dwCaps4 = lcl->lpSurfMore->ddsCapsEx.dwCaps4;
+                ptmpDdSurfaceMore->dwSurfaceHandle = (DWORD) pCreateSurface->lplpSList[i]->dbnOverlayNode.object_int;
+            }
+
+            /* FIXME count to next SurfaceCount for
+               ptmpDdSurfaceGlobal = pDdSurfaceGlobal;
+               ptmpDdSurfaceLocal = pDdSurfaceLocal;
+               ptmpDdSurfaceMore = pDdSurfaceMore;
+
+               we only support one surface create at moment
+             */
         }
+    }
 
-        /* Convert data */
-        DdSurfaceGlobal.wWidth = pSurfaceGlobal->wWidth;
-        DdSurfaceGlobal.wHeight = pSurfaceGlobal->wHeight;
-        DdSurfaceGlobal.lPitch = pSurfaceGlobal->lPitch;
-        DdSurfaceGlobal.fpVidMem = pSurfaceGlobal->fpVidMem;
-        DdSurfaceGlobal.dwBlockSizeX = pSurfaceGlobal->dwBlockSizeX;
-        DdSurfaceGlobal.dwBlockSizeY = pSurfaceGlobal->dwBlockSizeY;
-        // DdSurfaceGlobal.ddsCaps = pSurfaceLocal->ddsCaps | 0xBF0000;
-
-        /* FIXME: Ddscapsex stuff missing */
-
-        /* Call win32k now */
-        pCreateSurface->ddRVal = E_FAIL;
-               
-        Return = NtGdiDdCreateSurface(GetDdHandle(pCreateSurface->lpDD->hDD),
-                                     (HANDLE *)phSurface,
+    /* Call win32k now */
+    pCreateSurface->ddRVal = DDERR_GENERIC;
+
+    Return = NtGdiDdCreateSurface(GetDdHandle(pCreateSurface->lpDD->hDD),
+                                    (HANDLE *)phSurface,
                                      pSurfaceDesc,
-                                     &DdSurfaceGlobal,
-                                     &DdSurfaceLocal,
-                                     &DdSurfaceMore,
+                                     pDdSurfaceGlobal,
+                                     pDdSurfaceLocal,
+                                     pDdSurfaceMore,
                                      (PDD_CREATESURFACEDATA)pCreateSurface,
                                      puhSurface);
-          
-          
-        /* FIXME: Ddscapsex stuff missing */
-        
-        /* Convert the data back */
-        pSurfaceGlobal->lPitch = DdSurfaceGlobal.lPitch;
-        pSurfaceGlobal->fpVidMem = DdSurfaceGlobal.fpVidMem;
-        pSurfaceGlobal->dwBlockSizeX = DdSurfaceGlobal.dwBlockSizeX;
-        pSurfaceGlobal->dwBlockSizeY = DdSurfaceGlobal.dwBlockSizeY;
-        pCreateSurface->lplpSList[i]->hDDSurface = (DWORD) hSurface;
-
-        /* FIXME: Ddscapsex stuff missing */
+
+    if (SurfaceCount == 0)
+    {
+        pCreateSurface->ddRVal = DDERR_GENERIC;
+    }
+    else
+    {
+        ptmpDdSurfaceMore = pDdSurfaceMore;
+        ptmpDdSurfaceGlobal = pDdSurfaceGlobal;
+        ptmpDdSurfaceLocal = pDdSurfaceLocal;
+
+        for (i=0;i<SurfaceCount;i++)
+        {
+            LPDDRAWI_DDRAWSURFACE_LCL lcl = pCreateSurface->lplpSList[i];
+            LPDDRAWI_DDRAWSURFACE_GBL gpl = pCreateSurface->lplpSList[i]->lpGbl;
+
+            gpl->lPitch = ptmpDdSurfaceGlobal->lPitch;
+            gpl->fpVidMem = ptmpDdSurfaceGlobal->fpVidMem;
+            gpl->dwBlockSizeX = ptmpDdSurfaceGlobal->dwBlockSizeX;
+            gpl->dwBlockSizeY = ptmpDdSurfaceGlobal->dwBlockSizeY;
+
+            if (lcl->dwFlags & DDRAWISURF_HASPIXELFORMAT)
+            {
+                RtlCopyMemory( &gpl->ddpfSurface, &ptmpDdSurfaceGlobal->ddpfSurface , sizeof(DDPIXELFORMAT));
+            }
+
+            if (pCreateSurface->ddRVal != DD_OK)
+            {
+                gpl->fpVidMem = 0;
+                if (lcl->hDDSurface)
+                {
+                    NtGdiDdDeleteSurfaceObject( (HANDLE)lcl->hDDSurface);
+                }
+                lcl->hDDSurface = 0;
+            }
+            else
+            {
+
+                lcl->hDDSurface = (ULONG_PTR) puhSurface[i];
+            }
+
+            lcl->ddsCaps.dwCaps = ptmpDdSurfaceLocal->ddsCaps.dwCaps;
+            if (lcl->lpSurfMore)
+            {
+                lcl->lpSurfMore->ddsCapsEx.dwCaps2 = ptmpDdSurfaceMore->ddsCapsEx.dwCaps2;
+                lcl->lpSurfMore->ddsCapsEx.dwCaps3 = ptmpDdSurfaceMore->ddsCapsEx.dwCaps3;
+                lcl->lpSurfMore->ddsCapsEx.dwCaps4 = ptmpDdSurfaceMore->ddsCapsEx.dwCaps4;
+            }
+            /* FIXME count to next SurfaceCount for
+               ptmpDdSurfaceGlobal = pDdSurfaceGlobal;
+               ptmpDdSurfaceLocal = pDdSurfaceLocal;
+               ptmpDdSurfaceMore = pDdSurfaceMore;
+               we only support one surface create at moment
+             */
+        }
     }
-    
+
     /* Check if we have to free all our local allocations */
     if (SurfaceCount > 1)
     {
-     /* FIXME: */
+        /* FIXME: */
     }
 
     /* Return */
@@ -327,9 +407,10 @@ DdGetScanLine(LPDDHAL_GETSCANLINEDATA pGetScanLine)
 }
 
 /* PRIVATE FUNCTIONS *********************************************************/
+
 BOOL
 WINAPI
-bDDCreateSurface(LPDDRAWI_DDRAWSURFACE_LCL pSurface, 
+bDDCreateSurface(LPDDRAWI_DDRAWSURFACE_LCL pSurface,
                  BOOL bComplete)
 {
     DD_SURFACE_LOCAL SurfaceLocal;
@@ -359,7 +440,7 @@ bDDCreateSurface(LPDDRAWI_DDRAWSURFACE_LCL pSurface,
 
     /* Check if we have a pixel format */
     if (pSurface->dwFlags & DDSD_PIXELFORMAT)
-    {  
+    {
         /* Use global one */
         SurfaceGlobal.ddpfSurface = pSurface->lpGbl->lpDD->vmiData.ddpfDisplay;
         SurfaceGlobal.ddpfSurface.dwSize = sizeof(DDPIXELFORMAT);
@@ -388,13 +469,13 @@ bDDCreateSurface(LPDDRAWI_DDRAWSURFACE_LCL pSurface,
 /*
  * @implemented
  *
- * GDIEntry 1 
+ * GDIEntry 1
  */
-BOOL 
-WINAPI 
+BOOL
+WINAPI
 DdCreateDirectDrawObject(LPDDRAWI_DIRECTDRAW_GBL pDirectDrawGlobal,
                          HDC hdc)
-{  
+{
     BOOL Return = FALSE;
 
     /* Check if the global hDC (hdc == 0) is being used */
@@ -404,13 +485,13 @@ DdCreateDirectDrawObject(LPDDRAWI_DIRECTDRAW_GBL pDirectDrawGlobal,
         if (!ghDirectDraw)
         {
             /* Create the DC */
-            if ((hdc = CreateDC(L"Display", NULL, NULL, NULL)))
+            if ((hdc = CreateDCW(L"Display", NULL, NULL, NULL)))
             {
                 /* Create the DDraw Object */
                 ghDirectDraw = NtGdiDdCreateDirectDrawObject(hdc);
 
-                /* Delete our DC */                
-                               NtGdiDeleteObjectApp(hdc);
+                /* Delete our DC */
+                NtGdiDeleteObjectApp(hdc);
             }
         }
 
@@ -428,8 +509,8 @@ DdCreateDirectDrawObject(LPDDRAWI_DIRECTDRAW_GBL pDirectDrawGlobal,
     else
     {
         /* Using the per-process object, so create it */
-    pDirectDrawGlobal->hDD = (ULONG_PTR)NtGdiDdCreateDirectDrawObject(hdc); 
-    
+         pDirectDrawGlobal->hDD = (ULONG_PTR)NtGdiDdCreateDirectDrawObject(hdc);
+
         /* Set the return value */
         Return = pDirectDrawGlobal->hDD ? TRUE : FALSE;
     }
@@ -438,24 +519,13 @@ DdCreateDirectDrawObject(LPDDRAWI_DIRECTDRAW_GBL pDirectDrawGlobal,
     return Return;
 }
 
-
-
-
-static LPDDRAWI_DIRECTDRAW_GBL pDirectDrawGlobalInternal;
-static ULONG RemberDdQueryDisplaySettingsUniquenessID = 0;
-
-BOOL
-intDDCreateSurface ( LPDDRAWI_DDRAWSURFACE_LCL pSurface, 
-                                    BOOL bComplete);
-
-
-
-
 /*
- * @unimplemented
+ * @implemented
+ *
+ * GDIEntry 2
  */
 BOOL
-STDCALL
+WINAPI
 DdQueryDirectDrawObject(LPDDRAWI_DIRECTDRAW_GBL pDirectDrawGlobal,
                         LPDDHALINFO pHalInfo,
                         LPDDHAL_DDCALLBACKS pDDCallbacks,
@@ -467,51 +537,245 @@ DdQueryDirectDrawObject(LPDDRAWI_DIRECTDRAW_GBL pDirectDrawGlobal,
                         LPDDSURFACEDESC pD3dTextureFormats,
                         LPDWORD pdwFourCC,
                         LPVIDMEM pvmList)
-{
-    BOOL bStatus = FALSE;
-       DD_HALINFO DDHalInfo;
-       LPVOID pCallBackFlags[3];
-       DWORD NumHeaps;
-       DWORD NumFourCC; 
-
-       DDHalInfo.dwSize = sizeof(DD_HALINFO);
-
-       pCallBackFlags[0] = pDDCallbacks;
-    pCallBackFlags[1] = pDDSurfaceCallbacks;
-       pCallBackFlags[2] = pDDPaletteCallbacks;
-       
-       bStatus = NtGdiDdQueryDirectDrawObject(
-                     (HANDLE)pDirectDrawGlobal->hDD,
-                         (DD_HALINFO *)&DDHalInfo,
-                         (DWORD *)pCallBackFlags,
-                         (LPD3DNTHAL_CALLBACKS)pD3dCallbacks,
-              (LPD3DNTHAL_GLOBALDRIVERDATA)pD3dDriverData,
-                         (PDD_D3DBUFCALLBACKS)pD3dBufferCallbacks,
-                         (LPDDSURFACEDESC)pD3dTextureFormats,
-                         (DWORD *)&NumHeaps,
-                         (VIDEOMEMORY *)pvmList,
-                         (DWORD *)&NumFourCC,
-              (DWORD *)pdwFourCC);
-
-       
-       //SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-       return bStatus;
-}
+    {
+    PVIDEOMEMORY VidMemList = NULL;
+    DD_HALINFO HalInfo;
+    D3DNTHAL_CALLBACKS D3dCallbacks;
+    D3DNTHAL_GLOBALDRIVERDATA D3dDriverData;
+    DD_D3DBUFCALLBACKS D3dBufferCallbacks;
+    DWORD CallbackFlags[3];
+    DWORD dwNumHeaps=0, FourCCs=0;
+    DWORD Flags;
+
+    /* Clear the structures */
+    RtlZeroMemory(&HalInfo, sizeof(DD_HALINFO));
+    RtlZeroMemory(&D3dCallbacks, sizeof(D3DNTHAL_CALLBACKS));
+    RtlZeroMemory(&D3dDriverData, sizeof(D3DNTHAL_GLOBALDRIVERDATA));
+    RtlZeroMemory(&D3dBufferCallbacks, sizeof(DD_D3DBUFCALLBACKS));
+    RtlZeroMemory(CallbackFlags, sizeof(DWORD)*3);
+
+    pvmList = NULL;
+
+    /* Do the query */
+    if (!NtGdiDdQueryDirectDrawObject(GetDdHandle(pDirectDrawGlobal->hDD),
+                                      &HalInfo,
+                                      CallbackFlags,
+                                      &D3dCallbacks,
+                                      &D3dDriverData,
+                                      &D3dBufferCallbacks,
+                                      pD3dTextureFormats,
+                                      &dwNumHeaps,
+                                      VidMemList,
+                                      &FourCCs,
+                                      pdwFourCC))
+    {
+        /* We failed, free the memory and return */
+        return FALSE;
+    }
 
-/*
- * @unimplemented
- */
-HBITMAP
-STDCALL
-DdCreateDIBSection(HDC hdc,
-                   CONST BITMAPINFO *pbmi,
-                   UINT iUsage,
-                   VOID **ppvBits,
-                   HANDLE hSectionApp,
-                   DWORD dwOffset)
-{
-       SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-       return 0;
+    /* Clear the incoming pointer */
+    RtlZeroMemory(pHalInfo, sizeof(DDHALINFO));
+
+    /* Convert all the data */
+    pHalInfo->dwSize = sizeof(DDHALINFO);
+    pHalInfo->lpDDCallbacks = pDDCallbacks;
+    pHalInfo->lpDDSurfaceCallbacks = pDDSurfaceCallbacks;
+    pHalInfo->lpDDPaletteCallbacks = pDDPaletteCallbacks;
+
+    /* Check for NT5+ D3D Data */
+    if (D3dCallbacks.dwSize && D3dDriverData.dwSize)
+    {
+        /* Write these down */
+        pHalInfo->lpD3DGlobalDriverData = (ULONG_PTR)pD3dDriverData;
+        pHalInfo->lpD3DHALCallbacks = (ULONG_PTR)pD3dCallbacks;
+
+        /* Check for Buffer Callbacks */
+        if (D3dBufferCallbacks.dwSize)
+        {
+            /* Write this one too */
+            pHalInfo->lpDDExeBufCallbacks = pD3dBufferCallbacks;
+        }
+    }
+
+    /* Continue converting the rest */
+    pHalInfo->vmiData.dwFlags = HalInfo.vmiData.dwFlags;
+    pHalInfo->vmiData.dwDisplayWidth = HalInfo.vmiData.dwDisplayWidth;
+    pHalInfo->vmiData.dwDisplayHeight = HalInfo.vmiData.dwDisplayHeight;
+    pHalInfo->vmiData.lDisplayPitch = HalInfo.vmiData.lDisplayPitch;
+    pHalInfo->vmiData.fpPrimary = 0;
+
+    RtlCopyMemory( &pHalInfo->vmiData.ddpfDisplay,
+                   &HalInfo.vmiData.ddpfDisplay,
+                   sizeof(DDPIXELFORMAT));
+
+    pHalInfo->vmiData.dwOffscreenAlign = HalInfo.vmiData.dwOffscreenAlign;
+    pHalInfo->vmiData.dwOverlayAlign = HalInfo.vmiData.dwOverlayAlign;
+    pHalInfo->vmiData.dwTextureAlign = HalInfo.vmiData.dwTextureAlign;
+    pHalInfo->vmiData.dwZBufferAlign = HalInfo.vmiData.dwZBufferAlign;
+    pHalInfo->vmiData.dwAlphaAlign = HalInfo.vmiData.dwAlphaAlign;
+    pHalInfo->vmiData.dwNumHeaps = 0;
+    pHalInfo->vmiData.pvmList = pvmList;
+
+    RtlCopyMemory( &pHalInfo->ddCaps, &HalInfo.ddCaps,sizeof(DDCORECAPS ));
+
+    pHalInfo->ddCaps.dwNumFourCCCodes = FourCCs;
+    pHalInfo->lpdwFourCC = pdwFourCC;
+    pHalInfo->ddCaps.dwRops[6] = 0x1000;
+
+    /* FIXME implement DdGetDriverInfo */
+    //  pHalInfo->dwFlags = HalInfo.dwFlags | DDHALINFO_GETDRIVERINFOSET;
+    //  pHalInfo->GetDriverInfo = DdGetDriverInfo;
+
+    /* Now check if we got any DD callbacks */
+    if (pDDCallbacks)
+    {
+        /* Zero the structure */
+        RtlZeroMemory(pDDCallbacks, sizeof(DDHAL_DDCALLBACKS));
+
+        /* Set the flags for this structure */
+        Flags = CallbackFlags[0];
+
+        /* Write the header */
+        pDDCallbacks->dwSize = sizeof(DDHAL_DDCALLBACKS);
+        pDDCallbacks->dwFlags = Flags;
+
+        /* Now write the pointers, if applicable */
+        if (Flags & DDHAL_CB32_CREATESURFACE)
+        {
+            pDDCallbacks->CreateSurface = DdCreateSurface;
+        }
+        if (Flags & DDHAL_CB32_WAITFORVERTICALBLANK)
+        {
+            pDDCallbacks->WaitForVerticalBlank = DdWaitForVerticalBlank;
+        }
+        if (Flags & DDHAL_CB32_CANCREATESURFACE)
+        {
+            pDDCallbacks->CanCreateSurface = DdCanCreateSurface;
+        }
+        if (Flags & DDHAL_CB32_GETSCANLINE)
+        {
+            pDDCallbacks->GetScanLine = DdGetScanLine;
+        }
+    }
+
+    /* Check for DD Surface Callbacks */
+    if (pDDSurfaceCallbacks)
+    {
+        /* Zero the structures */
+        RtlZeroMemory(pDDSurfaceCallbacks, sizeof(DDHAL_DDSURFACECALLBACKS));
+
+        /* Set the flags for this one */
+        Flags = CallbackFlags[1];
+
+        /* Write the header, note that some functions are always exposed */
+        pDDSurfaceCallbacks->dwSize  = sizeof(DDHAL_DDSURFACECALLBACKS);
+
+        pDDSurfaceCallbacks->dwFlags = Flags;
+        /*
+        pDDSurfaceCallBacks->dwFlags = (DDHAL_SURFCB32_LOCK |
+                                        DDHAL_SURFCB32_UNLOCK |
+                                        DDHAL_SURFCB32_SETCOLORKEY |
+                                        DDHAL_SURFCB32_DESTROYSURFACE) | Flags;
+        */
+
+        /* Write the always-on functions */
+        pDDSurfaceCallbacks->Lock = DdLock;
+        pDDSurfaceCallbacks->Unlock = DdUnlock;
+        pDDSurfaceCallbacks->SetColorKey = DdSetColorKey;
+        pDDSurfaceCallbacks->DestroySurface = DdDestroySurface;
+
+        /* Write the optional ones */
+        if (Flags & DDHAL_SURFCB32_FLIP)
+        {
+            pDDSurfaceCallbacks->Flip = DdFlip;
+        }
+        if (Flags & DDHAL_SURFCB32_BLT)
+        {
+            pDDSurfaceCallbacks->Blt = DdBlt;
+        }
+        if (Flags & DDHAL_SURFCB32_GETBLTSTATUS)
+        {
+            pDDSurfaceCallbacks->GetBltStatus = DdGetBltStatus;
+        }
+        if (Flags & DDHAL_SURFCB32_GETFLIPSTATUS)
+        {
+            pDDSurfaceCallbacks->GetFlipStatus = DdGetFlipStatus;
+        }
+        if (Flags & DDHAL_SURFCB32_UPDATEOVERLAY)
+        {
+            pDDSurfaceCallbacks->UpdateOverlay = DdUpdateOverlay;
+        }
+        if (Flags & DDHAL_SURFCB32_SETOVERLAYPOSITION)
+        {
+            pDDSurfaceCallbacks->SetOverlayPosition = DdSetOverlayPosition;
+        }
+        if (Flags & DDHAL_SURFCB32_ADDATTACHEDSURFACE)
+        {
+            pDDSurfaceCallbacks->AddAttachedSurface = DdAddAttachedSurface;
+        }
+    }
+
+    /* Check for DD Palette Callbacks */
+    if (pDDPaletteCallbacks)
+    {
+        /* Zero the struct */
+        RtlZeroMemory(pDDPaletteCallbacks, sizeof(DDHAL_DDPALETTECALLBACKS));
+
+        /* Get the flags for this one */
+        Flags = CallbackFlags[2];
+
+        /* Write the header */
+        pDDPaletteCallbacks->dwSize  = sizeof(DDHAL_DDPALETTECALLBACKS);
+        pDDPaletteCallbacks->dwFlags = Flags;
+    }
+
+    /* Check for D3D Callbacks */
+    if (pD3dCallbacks)
+    {
+        /* Zero the struct */
+        RtlZeroMemory(pD3dCallbacks, sizeof(D3DHAL_CALLBACKS));
+
+        /* Check if we have one */
+        if (D3dCallbacks.dwSize)
+        {
+            /* Write the header */
+            pD3dCallbacks->dwSize  = sizeof(D3DHAL_CALLBACKS);
+
+            /* Now check for each callback */
+            if (D3dCallbacks.ContextCreate)
+            {
+                /* FIXME
+                 pD3dCallbacks->ContextCreate = D3dContextCreate;
+                 */
+            }
+            if (D3dCallbacks.ContextDestroy)
+            {
+                pD3dCallbacks->ContextDestroy = (LPD3DHAL_CONTEXTDESTROYCB) NtGdiD3dContextDestroy;
+            }
+            if (D3dCallbacks.ContextDestroyAll)
+            {
+                /* FIXME
+                pD3dCallbacks->ContextDestroyAll = (LPD3DHAL_CONTEXTDESTROYALLCB) NtGdiD3dContextDestroyAll;
+                */
+            }
+        }
+    }
+
+    /* Check for D3D Driver Data */
+    if (pD3dDriverData)
+    {
+        /* Copy the struct */
+        RtlMoveMemory(pD3dDriverData,
+                      &D3dDriverData,
+                      sizeof(D3DHAL_GLOBALDRIVERDATA));
+
+        /* Write the pointer to the texture formats */
+        pD3dDriverData->lpTextureFormats = pD3dTextureFormats;
+    }
+
+    /* FIXME: Check for D3D Buffer Callbacks */
+
+  return TRUE;
 }
 
 /*
@@ -519,30 +783,41 @@ DdCreateDIBSection(HDC hdc,
  *
  * GDIEntry 3
  */
-BOOL 
-STDCALL 
+BOOL
+WINAPI
 DdDeleteDirectDrawObject(LPDDRAWI_DIRECTDRAW_GBL pDirectDrawGlobal)
 {
-  BOOL status;                                               
-  /* if pDirectDrawGlobal->hDD == NULL and pDirectDrawGlobalInternal->hDD == NULL
-     return false */
+    BOOL Return = FALSE;
 
-  if (!pDirectDrawGlobal->hDD)
-  {
-     if (!pDirectDrawGlobalInternal->hDD)
-     {
-       return FALSE;
-     }
-    return NtGdiDdDeleteDirectDrawObject((HANDLE)pDirectDrawGlobalInternal->hDD); 
-  }
-  
-  status = NtGdiDdDeleteDirectDrawObject((HANDLE)pDirectDrawGlobal->hDD);      
-  if ((status == TRUE) && (pDirectDrawGlobalInternal->hDD != 0))
-  {
-     pDirectDrawGlobalInternal->hDD = 0;        
-  }
-     
-  return status;       
+    /* If this is the global object */
+    if(pDirectDrawGlobal->hDD)
+    {
+        /* Free it */
+        Return = NtGdiDdDeleteDirectDrawObject((HANDLE)pDirectDrawGlobal->hDD);
+        if (Return == TRUE)
+        {
+            pDirectDrawGlobal->hDD = 0;
+        }
+    }
+    else if (ghDirectDraw)
+    {
+        /* Always success here */
+        Return = TRUE;
+
+        /* Make sure this is the last instance */
+        if (!(--gcDirectDraw))
+        {
+            /* Delete the object */
+            Return = NtGdiDdDeleteDirectDrawObject(ghDirectDraw);
+            if (Return == TRUE)
+            {
+                ghDirectDraw = 0;
+            }
+        }
+    }
+
+    /* Return */
+    return Return;
 }
 
 /*
@@ -550,29 +825,35 @@ DdDeleteDirectDrawObject(LPDDRAWI_DIRECTDRAW_GBL pDirectDrawGlobal)
  *
  * GDIEntry 4
  */
-BOOL 
-STDCALL 
+BOOL
+WINAPI
 DdCreateSurfaceObject( LPDDRAWI_DDRAWSURFACE_LCL pSurfaceLocal,
                        BOOL bPrimarySurface)
 {
-       return intDDCreateSurface(pSurfaceLocal,1);     
+    return bDDCreateSurface(pSurfaceLocal, TRUE);
 }
 
+
 /*
  * @implemented
  *
  * GDIEntry 5
  */
-BOOL 
-STDCALL 
+BOOL
+WINAPI
 DdDeleteSurfaceObject(LPDDRAWI_DDRAWSURFACE_LCL pSurfaceLocal)
 {
-  if (!pSurfaceLocal->hDDSurface)
-  {
-    return FALSE;
-  }
+    BOOL Return = FALSE;
 
-  return NtGdiDdDeleteSurfaceObject((HANDLE)pSurfaceLocal->hDDSurface);
+    /* Make sure there is one */
+    if (pSurfaceLocal->hDDSurface)
+    {
+        /* Delete it */
+        Return = NtGdiDdDeleteSurfaceObject((HANDLE)pSurfaceLocal->hDDSurface);
+        pSurfaceLocal->hDDSurface = 0;
+    }
+
+    return Return;
 }
 
 /*
@@ -580,12 +861,13 @@ DdDeleteSurfaceObject(LPDDRAWI_DDRAWSURFACE_LCL pSurfaceLocal)
  *
  * GDIEntry 6
  */
-BOOL 
-STDCALL 
-DdResetVisrgn(LPDDRAWI_DDRAWSURFACE_LCL pSurfaceLocal, 
+BOOL
+WINAPI
+DdResetVisrgn(LPDDRAWI_DDRAWSURFACE_LCL pSurfaceLocal,
               HWND hWnd)
 {
- return NtGdiDdResetVisrgn((HANDLE) pSurfaceLocal->hDDSurface, hWnd);
+    /* Call win32k directly */
+    return NtGdiDdResetVisrgn((HANDLE) pSurfaceLocal->hDDSurface, hWnd);
 }
 
 /*
@@ -593,12 +875,13 @@ DdResetVisrgn(LPDDRAWI_DDRAWSURFACE_LCL pSurfaceLocal,
  *
  * GDIEntry 7
  */
-HDC STDCALL DdGetDC( 
-LPDDRAWI_DDRAWSURFACE_LCL pSurfaceLocal,
-LPPALETTEENTRY pColorTable
-)
+HDC
+WINAPI
+DdGetDC(LPDDRAWI_DDRAWSURFACE_LCL pSurfaceLocal,
+        LPPALETTEENTRY pColorTable)
 {
-       return NtGdiDdGetDC(pColorTable, (HANDLE) pSurfaceLocal->hDDSurface);
+    /* Call win32k directly */
+    return NtGdiDdGetDC(pColorTable, (HANDLE) pSurfaceLocal->hDDSurface);
 }
 
 /*
@@ -606,67 +889,78 @@ LPPALETTEENTRY pColorTable
  *
  * GDIEntry 8
  */
-BOOL STDCALL DdReleaseDC( 
-LPDDRAWI_DDRAWSURFACE_LCL pSurfaceLocal
-)
+BOOL
+WINAPI
+DdReleaseDC(LPDDRAWI_DDRAWSURFACE_LCL pSurfaceLocal)
 {
- return NtGdiDdReleaseDC((HANDLE) pSurfaceLocal->hDDSurface);
+    /* Call win32k directly */
+    return NtGdiDdReleaseDC((HANDLE) pSurfaceLocal->hDDSurface);
 }
 
-
+/*
+ * @unimplemented
+ * GDIEntry 9
+ */
+HBITMAP
+STDCALL
+DdCreateDIBSection(HDC hdc,
+                   CONST BITMAPINFO *pbmi,
+                   UINT iUsage,
+                   VOID **ppvBits,
+                   HANDLE hSectionApp,
+                   DWORD dwOffset)
+{
+    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
+    return 0;
+}
 
 /*
  * @implemented
  *
  * GDIEntry 10
  */
-BOOL 
-STDCALL 
+BOOL
+WINAPI
 DdReenableDirectDrawObject(LPDDRAWI_DIRECTDRAW_GBL pDirectDrawGlobal,
                            BOOL *pbNewMode)
 {
- if (!pDirectDrawGlobal->hDD)
-  {
-     if (!pDirectDrawGlobalInternal->hDD)
-     {
-       return FALSE;
-     }
-    return NtGdiDdReenableDirectDrawObject((HANDLE)pDirectDrawGlobalInternal->hDD, pbNewMode); 
-  }
+    /* Call win32k directly */
+    return NtGdiDdReenableDirectDrawObject(GetDdHandle(pDirectDrawGlobal->hDD),
+                                           pbNewMode);
+}
 
-  return NtGdiDdReenableDirectDrawObject((HANDLE)pDirectDrawGlobal->hDD, pbNewMode);   
-} 
 
 /*
  * @implemented
  *
  * GDIEntry 11
  */
-BOOL 
-STDCALL 
+BOOL
+STDCALL
 DdAttachSurface( LPDDRAWI_DDRAWSURFACE_LCL pSurfaceFrom,
                  LPDDRAWI_DDRAWSURFACE_LCL pSurfaceTo)
 {
+    /* Create Surface if it does not exits one */
+    if (!pSurfaceFrom->hDDSurface)
+    {
+        if (!bDDCreateSurface(pSurfaceFrom, FALSE))
+        {
+            return FALSE;
+        }
+    }
 
- /* Create Surface if it does not exits one */
- if (pSurfaceFrom->hDDSurface)
- {    
-       if (!intDDCreateSurface(pSurfaceFrom,FALSE))
-       {
-         return FALSE;
-       }
- }
-
- /* Create Surface if it does not exits one */
- if (pSurfaceTo->hDDSurface)
- {    
-       if (!intDDCreateSurface(pSurfaceTo,FALSE))
-       {
-         return FALSE;
-       }
- }
-
- return NtGdiDdAttachSurface( (HANDLE) pSurfaceFrom->hDDSurface, (HANDLE) pSurfaceTo->hDDSurface);
+    /* Create Surface if it does not exits one */
+    if (!pSurfaceTo->hDDSurface)
+    {
+        if (!bDDCreateSurface(pSurfaceTo, FALSE))
+        {
+            return FALSE;
+        }
+    }
+
+    /* Call win32k */
+    return NtGdiDdAttachSurface((HANDLE)pSurfaceFrom->hDDSurface,
+                                (HANDLE)pSurfaceTo->hDDSurface);
 }
 
 /*
@@ -674,12 +968,14 @@ DdAttachSurface( LPDDRAWI_DDRAWSURFACE_LCL pSurfaceFrom,
  *
  * GDIEntry 12
  */
-VOID 
-STDCALL 
+VOID
+STDCALL
 DdUnattachSurface(LPDDRAWI_DDRAWSURFACE_LCL pSurface,
                   LPDDRAWI_DDRAWSURFACE_LCL pSurfaceAttached)
 {
-  NtGdiDdUnattachSurface((HANDLE) pSurface->hDDSurface, (HANDLE) pSurfaceAttached->hDDSurface);        
+    /* Call win32k */
+    NtGdiDdUnattachSurface((HANDLE)pSurface->hDDSurface,
+                           (HANDLE)pSurfaceAttached->hDDSurface);
 }
 
 /*
@@ -688,10 +984,10 @@ DdUnattachSurface(LPDDRAWI_DDRAWSURFACE_LCL pSurface,
  * GDIEntry 13
  */
 ULONG
-STDCALL 
+STDCALL
 DdQueryDisplaySettingsUniqueness()
 {
- return RemberDdQueryDisplaySettingsUniquenessID;
+ return GdiSharedHandleTable->flDeviceUniq;
 }
 
 /*
@@ -699,28 +995,25 @@ DdQueryDisplaySettingsUniqueness()
  *
  * GDIEntry 14
  */
-HANDLE 
-STDCALL 
+HANDLE
+WINAPI
 DdGetDxHandle(LPDDRAWI_DIRECTDRAW_LCL pDDraw,
               LPDDRAWI_DDRAWSURFACE_LCL pSurface,
               BOOL bRelease)
 {
- if (pSurface) 
- {                              
-   return ((HANDLE) NtGdiDdGetDxHandle(NULL, (HANDLE)pSurface->hDDSurface, bRelease));    
- }
+    HANDLE hDD = NULL;
+    HANDLE hSurface = (HANDLE)pSurface->hDDSurface;
 
if (!pDDraw->lpGbl->hDD)
-  {
-     if (!pDirectDrawGlobalInternal->hDD)
-     {
-       return FALSE;
+    /* Check if we already have a surface */
   if (!pSurface)
+    {
+        /* We don't have one, use the DirectDraw Object handle instead */
+        hSurface = NULL;
+        hDD = GetDdHandle(pDDraw->lpGbl->hDD);
      }
-   return ((HANDLE) NtGdiDdGetDxHandle( (HANDLE) pDirectDrawGlobalInternal->hDD, (HANDLE) pSurface->hDDSurface, bRelease));
-  }
 
-  return ((HANDLE) NtGdiDdGetDxHandle((HANDLE)pDDraw->lpGbl->hDD, (HANDLE) pSurface->hDDSurface, bRelease));
+    /* Call the API */
+    return (HANDLE)NtGdiDdGetDxHandle(hDD, hSurface, bRelease);
 }
 
 /*
@@ -728,100 +1021,19 @@ DdGetDxHandle(LPDDRAWI_DIRECTDRAW_LCL pDDraw,
  *
  * GDIEntry 15
  */
-BOOL STDCALL DdSetGammaRamp( 
-LPDDRAWI_DIRECTDRAW_LCL pDDraw,
-HDC hdc,
-LPVOID lpGammaRamp
-)
+BOOL
+WINAPI
+DdSetGammaRamp(LPDDRAWI_DIRECTDRAW_LCL pDDraw,
+               HDC hdc,
+               LPVOID lpGammaRamp)
 {
-       if (!pDDraw->lpGbl->hDD)
-  {
-     if (!pDirectDrawGlobalInternal->hDD)
-     {
-       return FALSE;
-     }
-    return NtGdiDdSetGammaRamp((HANDLE)pDirectDrawGlobalInternal->hDD,hdc,lpGammaRamp);
-  }
-
-  return NtGdiDdSetGammaRamp((HANDLE)pDDraw->lpGbl->hDD,hdc,lpGammaRamp);
+    /* Call win32k directly */
+    return NtGdiDdSetGammaRamp(GetDdHandle(pDDraw->lpGbl->hDD),
+                               hdc,
+                               lpGammaRamp);
 }
 
-/*
- * @implemented
- *
- * GDIEntry 16
- */
-DWORD STDCALL DdSwapTextureHandles( 
-LPDDRAWI_DIRECTDRAW_LCL pDDraw,
-LPDDRAWI_DDRAWSURFACE_LCL pDDSLcl1,
-LPDDRAWI_DDRAWSURFACE_LCL pDDSLcl2
-)
-{  
-       return TRUE;
-}
 
 
-/* interal create surface */
-BOOL
-intDDCreateSurface ( LPDDRAWI_DDRAWSURFACE_LCL pSurface, 
-                                    BOOL bComplete)
-{
-  DD_SURFACE_LOCAL SurfaceLocal;
-  DD_SURFACE_GLOBAL SurfaceGlobal;
-  DD_SURFACE_MORE SurfaceMore;
-
-  /* Zero struct */
-  RtlZeroMemory(&SurfaceLocal, sizeof(DD_SURFACE_LOCAL));
-  RtlZeroMemory(&SurfaceGlobal, sizeof(DD_SURFACE_GLOBAL));
-  RtlZeroMemory(&SurfaceMore, sizeof(DD_SURFACE_MORE));
-
-  /* Set up SurfaceLocal struct */
-  SurfaceLocal.ddsCaps.dwCaps = pSurface->ddsCaps.dwCaps;
-  SurfaceLocal.dwFlags = pSurface->dwFlags;
-
-  /* Set up SurfaceMore struct */
-  /* copy  pSurface->ddckCKDestBlt and pSurface->ddckCKSrcBlt to SurfaceMore.ddsCapsEx */  
-  memcpy(&SurfaceMore.ddsCapsEx, &pSurface->ddckCKDestBlt, sizeof(DDSCAPSEX));   
-  SurfaceMore.dwSurfaceHandle =  (DWORD) pSurface->dbnOverlayNode.object_int->lpVtbl; 
-
-
-  /* Set up SurfaceGlobal struct */
-  SurfaceGlobal.fpVidMem = pSurface->lpGbl->fpVidMem;
-  SurfaceGlobal.dwLinearSize = pSurface->lpGbl->dwLinearSize;
-  SurfaceGlobal.wHeight = pSurface->lpGbl->wHeight;
-  SurfaceGlobal.wWidth = pSurface->lpGbl->wWidth;
-
-  /* check which memory type should be use */
-  if ((pSurface->dwFlags & DDRAWISURFGBL_LOCKVRAMSTYLE) == DDRAWISURFGBL_LOCKVRAMSTYLE)
-  {    
-         memcpy(&SurfaceGlobal.ddpfSurface,&pSurface->lpGbl->lpDD->vmiData.ddpfDisplay, sizeof(DDPIXELFORMAT));
-  }
-  else
-  {
-         memcpy(&SurfaceGlobal.ddpfSurface,&pSurface->lpGbl->ddpfSurface, sizeof(DDPIXELFORMAT));
-  }
 
-  /* Determer if Gdi32 chace of directdraw handler or not */
-  if (pSurface->lpGbl->lpDD->hDD)
-  {
-     pSurface->hDDSurface = ((DWORD) NtGdiDdCreateSurfaceObject( (HANDLE) pSurface->lpGbl->lpDD->hDD,
-                                                       (HANDLE) pSurface->hDDSurface, &SurfaceLocal, 
-                                                                                                   &SurfaceMore, &SurfaceGlobal, bComplete));
-  }
-  else
-  {
-     pSurface->hDDSurface = ((DWORD) NtGdiDdCreateSurfaceObject( (HANDLE) pDirectDrawGlobalInternal->hDD,
-                                                       (HANDLE) pSurface->hDDSurface, &SurfaceLocal, 
-                                                                                                   &SurfaceMore, 
-                                                                                                       &SurfaceGlobal, 
-                                                                                                       bComplete));
-  }
-
-  /* return status */
-  if (pSurface->hDDSurface) 
-  {
-    return TRUE;
-  }
 
-  return FALSE;
-}