* Made it possible to include both d3d9types.h and ddrawgdi.h at the same time
[reactos.git] / reactos / dll / directx / ddraw / startup.c
index f9ab530..ef660ef 100644 (file)
@@ -9,7 +9,6 @@
  */
 
 #include "rosdraw.h"
-#include "ddrawgdi.h"
 
 DDRAWI_DIRECTDRAW_GBL ddgbl;
 DDRAWI_DDRAWSURFACE_GBL ddSurfGbl;
@@ -20,7 +19,7 @@ WNDCLASSW wnd_class;
 
 HRESULT WINAPI
 Create_DirectDraw (LPGUID pGUID, LPDIRECTDRAW* pIface,
-                   REFIID id, BOOL ex)
+                   REFIID id, BOOL reenable)
 {
     LPDDRAWI_DIRECTDRAW_INT This;
 
@@ -32,75 +31,56 @@ Create_DirectDraw (LPGUID pGUID, LPDIRECTDRAW* pIface,
         return DDERR_INVALIDPARAMS;
     }
 
-    DX_STUB_str("here\n");
-
     This = (LPDDRAWI_DIRECTDRAW_INT)*pIface;
 
     /* fixme linking too second link when we shall not doing it */
     if (IsBadReadPtr(This,sizeof(LPDIRECTDRAW)))
     {
-        DX_STUB_str("1. no linking\n");
         /* We do not have a DirectDraw interface, we need alloc it*/
         LPDDRAWI_DIRECTDRAW_INT memThis;
 
-        DX_STUB_str("here\n");
+        DX_STUB_str("1. no linking\n");
 
         DxHeapMemAlloc(memThis, sizeof(DDRAWI_DIRECTDRAW_INT));
-
-        DX_STUB_str("here\n")
-
         if (memThis == NULL)
         {
-            DX_STUB_str("DDERR_OUTOFMEMORY\n");
             return DDERR_OUTOFMEMORY;
         }
 
         This = memThis;
 
-
-        DX_STUB_str("here\n");
-
         /* Fixme release memory alloc if we fail */
-        DxHeapMemAlloc(This->lpLcl, sizeof(DDRAWI_DIRECTDRAW_INT));
+
+        DxHeapMemAlloc(This->lpLcl, sizeof(DDRAWI_DIRECTDRAW_LCL));
         if (This->lpLcl == NULL)
         {
-            DX_STUB_str("DDERR_OUTOFMEMORY\n");
             return DDERR_OUTOFMEMORY;
         }
-
-        DX_STUB_str("here\n");
     }
     else
     {
-        DX_STUB_str("2.linking\n");
         /* We got the DirectDraw interface alloc and we need create the link */
         LPDDRAWI_DIRECTDRAW_INT  newThis;
 
-        DX_STUB_str("here\n");
+        DX_STUB_str("2.linking\n");
 
         /* step 1.Alloc the new  DDRAWI_DIRECTDRAW_INT for the lnking */
         DxHeapMemAlloc(newThis, sizeof(DDRAWI_DIRECTDRAW_INT));
         if (newThis == NULL)
         {
-            DX_STUB_str("DDERR_OUTOFMEMORY\n");
             return DDERR_OUTOFMEMORY;
         }
 
-        DX_STUB_str("here\n");
-
         /* step 2 check if it not DDCREATE_HARDWAREONLY we got if so we fail */
         if ((pGUID) && (pGUID != (LPGUID)DDCREATE_HARDWAREONLY))
         {
             if (pGUID !=NULL)
             {
                 This = newThis;
-                DX_STUB_str("DDERR_INVALIDDIRECTDRAWGUID\n");
                 return DDERR_INVALIDDIRECTDRAWGUID;
             }
         }
 
-        DX_STUB_str("here\n");
-
         /* step 3 do the link the old interface are store in the new one */
         newThis->lpLink = This;
 
@@ -109,22 +89,50 @@ Create_DirectDraw (LPGUID pGUID, LPDIRECTDRAW* pIface,
         if (newThis->lpLcl == NULL)
         {
             This = newThis;
-            DX_STUB_str("DDERR_OUTOFMEMORY\n");
             return DDERR_OUTOFMEMORY;
         }
 
-        DX_STUB_str("here\n");
-
         This = newThis;
     }
 
-
     This->lpLcl->lpGbl = &ddgbl;
 
     *pIface = (LPDIRECTDRAW)This;
 
     /* Get right interface we whant */
-    if (Main_DirectDraw_QueryInterface((LPDIRECTDRAW7)This, id, (void**)&pIface) == DD_OK)
+
+    This->lpVtbl = 0;
+    if (IsEqualGUID(&IID_IDirectDraw7, id))
+    {
+        /* DirectDraw7 Vtable */
+        This->lpVtbl = &DirectDraw7_Vtable;
+        This->lpLcl->dwLocalFlags = This->lpLcl->dwLocalFlags + DDRAWILCL_DIRECTDRAW7;
+        *pIface = (LPDIRECTDRAW)&This->lpVtbl;
+        Main_DirectDraw_AddRef(This);
+    }
+    else if (IsEqualGUID(&IID_IDirectDraw4, id))
+    {
+        /* DirectDraw4 Vtable */
+        This->lpVtbl = &DirectDraw4_Vtable;
+        *pIface = (LPDIRECTDRAW)&This->lpVtbl;
+        Main_DirectDraw_AddRef(This);
+    }
+    else if (IsEqualGUID(&IID_IDirectDraw2, id))
+    {
+        /* DirectDraw2 Vtable */
+        This->lpVtbl = &DirectDraw2_Vtable;
+        *pIface = (LPDIRECTDRAW)&This->lpVtbl;
+        Main_DirectDraw_AddRef(This);
+    }
+    else if (IsEqualGUID(&IID_IDirectDraw, id))
+    {
+        /* DirectDraw Vtable */
+        This->lpVtbl = &DirectDraw_Vtable;
+        *pIface = (LPDIRECTDRAW)&This->lpVtbl;
+        Main_DirectDraw_AddRef(This);
+    }
+
+    if ( This->lpVtbl != 0)
     {
         DX_STUB_str("Got iface\n");
 
@@ -148,13 +156,11 @@ Create_DirectDraw (LPGUID pGUID, LPDIRECTDRAW* pIface,
                 return DDERR_GENERIC;
             }
             */
-
-            DX_STUB_str("DD_OK\n");
+            This->lpLcl->hDD = ddgbl.hDD;
             return DD_OK;
         }
     }
 
-    DX_STUB_str("DDERR_INVALIDPARAMS\n");
     return DDERR_INVALIDPARAMS;
 }
 
@@ -168,8 +174,10 @@ StartDirectDraw(LPDIRECTDRAW iface, LPGUID lpGuid, BOOL reenable)
     DWORD devicetypes = 0;
     DWORD dwFlags = 0;
 
+
     DX_WINDBG_trace();
 
+
     /*
      * ddgbl.dwPDevice  is not longer in use in windows 2000 and higher
      * I am using it for device type
@@ -179,6 +187,12 @@ StartDirectDraw(LPDIRECTDRAW iface, LPGUID lpGuid, BOOL reenable)
      * devicetypes = 4 :loading a guid drv from the register
      */
 
+    ddgbl.lpDriverHandle = &ddgbl;
+    ddgbl.hDDVxd = -1;
+
+
+
+
     if (reenable == FALSE)
     {
         if ((!IsBadReadPtr(This->lpLink,sizeof(LPDIRECTDRAW))) && (This->lpLink == NULL))
@@ -196,20 +210,27 @@ StartDirectDraw(LPDIRECTDRAW iface, LPGUID lpGuid, BOOL reenable)
                 }
             }
         }
-    }
 
-    DX_STUB_str("here\n");
+        DxHeapMemAlloc(ddgbl.lpModeInfo, sizeof(DDHALMODEINFO));
+        if (!ddgbl.lpModeInfo)
+        {
+            return DDERR_OUTOFMEMORY;
+        }
+
+    }
+    /* Windows handler are by set of SetCooperLevel
+     * so do not set it
+     */
 
     if (reenable == FALSE)
     {
         if (lpGuid == NULL)
         {
-            DX_STUB_str("lpGuid == NULL\n");
             devicetypes= 1;
 
             /* Create HDC for default, hal and hel driver */
-            This->lpLcl->hWnd = (ULONG_PTR) GetActiveWindow();
-            This->lpLcl->hDC = (ULONG_PTR) GetDC((HWND)This->lpLcl->hWnd);
+            // This->lpLcl->hWnd = (ULONG_PTR) GetActiveWindow();
+            This->lpLcl->hDC = (ULONG_PTR)CreateDCA("DISPLAY",NULL,NULL,NULL);
 
             /* cObsolete is undoc in msdn it being use in CreateDCA */
             RtlCopyMemory(&ddgbl.cObsolete,&"DISPLAY",7);
@@ -222,8 +243,8 @@ StartDirectDraw(LPDIRECTDRAW iface, LPGUID lpGuid, BOOL reenable)
         {
             devicetypes = 2;
             /* Create HDC for default, hal driver */
-            This->lpLcl->hWnd =(ULONG_PTR) GetActiveWindow();
-            This->lpLcl->hDC = (ULONG_PTR) GetDC((HWND)This->lpLcl->hWnd);
+            // This->lpLcl->hWnd =(ULONG_PTR) GetActiveWindow();
+            This->lpLcl->hDC = (ULONG_PTR)CreateDCA("DISPLAY",NULL,NULL,NULL);
 
             /* cObsolete is undoc in msdn it being use in CreateDCA */
             RtlCopyMemory(&ddgbl.cObsolete,&"DISPLAY",7);
@@ -235,8 +256,8 @@ StartDirectDraw(LPDIRECTDRAW iface, LPGUID lpGuid, BOOL reenable)
             devicetypes = 3;
 
             /* Create HDC for default, hal and hel driver */
-            This->lpLcl->hWnd = (ULONG_PTR) GetActiveWindow();
-            This->lpLcl->hDC = (ULONG_PTR) GetDC((HWND)This->lpLcl->hWnd);
+            //This->lpLcl->hWnd = (ULONG_PTR) GetActiveWindow();
+            This->lpLcl->hDC = (ULONG_PTR)CreateDCA("DISPLAY",NULL,NULL,NULL);
 
             /* cObsolete is undoc in msdn it being use in CreateDCA */
             RtlCopyMemory(&ddgbl.cObsolete,&"DISPLAY",7);
@@ -250,15 +271,17 @@ StartDirectDraw(LPDIRECTDRAW iface, LPGUID lpGuid, BOOL reenable)
              * the register. we do not support that yet
              */
              devicetypes = 4;
-             This->lpLcl->hDC = (ULONG_PTR) NULL ;
-             This->lpLcl->hWnd = (ULONG_PTR) GetActiveWindow();
+             //This->lpLcl->hDC = (ULONG_PTR) NULL ;
+             //This->lpLcl->hDC = (ULONG_PTR)CreateDCA("DISPLAY",NULL,NULL,NULL);
         }
 
+        /*
         if ( (HDC)This->lpLcl->hDC == NULL)
         {
             DX_STUB_str("DDERR_OUTOFMEMORY\n");
             return DDERR_OUTOFMEMORY ;
         }
+        */
     }
 
     This->lpLcl->lpDDCB = ddgbl.lpDDCBtmp;
@@ -284,8 +307,6 @@ StartDirectDraw(LPDIRECTDRAW iface, LPGUID lpGuid, BOOL reenable)
               hel_ret = StartDirectDrawHel(iface, reenable);
     }
 
-    DX_STUB_str("return\n");
-
     if (hal_ret!=DD_OK)
     {
         if (hel_ret!=DD_OK)
@@ -307,15 +328,35 @@ StartDirectDraw(LPDIRECTDRAW iface, LPGUID lpGuid, BOOL reenable)
     }
 
     /* Fill some basic info for Surface */
-    This->lpLcl->lpGbl->dwFlags = dwFlags | DDRAWI_ATTACHEDTODESKTOP;
+    This->lpLcl->lpGbl->dwFlags =  This->lpLcl->lpGbl->dwFlags | dwFlags | DDRAWI_ATTACHEDTODESKTOP;
     This->lpLcl->lpDDCB = This->lpLcl->lpGbl->lpDDCBtmp;
-    This->lpLcl->hDD = This->lpLcl->lpGbl->hDD;
-    ddgbl.hDD = This->lpLcl->lpGbl->hDD;
+    This->lpLcl->hDD = ddgbl.hDD;
+
+    ddgbl.rectDevice.top = 0;
+    ddgbl.rectDevice.left = 0;
+    ddgbl.rectDevice.right = ddgbl.vmiData.dwDisplayWidth;
+    ddgbl.rectDevice.bottom = ddgbl.vmiData.dwDisplayHeight;
+
+    ddgbl.rectDesktop.top = 0;
+    ddgbl.rectDesktop.left = 0;
+    ddgbl.rectDesktop.right = ddgbl.vmiData.dwDisplayWidth;
+    ddgbl.rectDesktop.bottom = ddgbl.vmiData.dwDisplayHeight;
+
+    ddgbl.dwMonitorFrequency = GetDeviceCaps(GetWindowDC(NULL),VREFRESH);
+    ddgbl.lpModeInfo->dwWidth      = ddgbl.vmiData.dwDisplayWidth;
+    ddgbl.lpModeInfo->dwHeight     = ddgbl.vmiData.dwDisplayHeight;
+    ddgbl.lpModeInfo->dwBPP        = ddgbl.vmiData.ddpfDisplay.dwRGBBitCount;
+    ddgbl.lpModeInfo->lPitch       = ddgbl.vmiData.lDisplayPitch;
+    ddgbl.lpModeInfo->wRefreshRate = ddgbl.dwMonitorFrequency;
+    ddgbl.lpModeInfo->dwRBitMask = ddgbl.vmiData.ddpfDisplay.dwRBitMask;
+    ddgbl.lpModeInfo->dwGBitMask = ddgbl.vmiData.ddpfDisplay.dwGBitMask;
+    ddgbl.lpModeInfo->dwBBitMask = ddgbl.vmiData.ddpfDisplay.dwBBitMask;
+    ddgbl.lpModeInfo->dwAlphaBitMask = ddgbl.vmiData.ddpfDisplay.dwRGBAlphaBitMask;
 
-    DX_STUB_str("DD_OK\n");
     return DD_OK;
 }
 
+
 HRESULT WINAPI
 StartDirectDrawHel(LPDIRECTDRAW iface, BOOL reenable)
 {
@@ -405,6 +446,7 @@ StartDirectDrawHal(LPDIRECTDRAW iface, BOOL reenable)
     D3DHAL_GLOBALDRIVERDATA mD3dDriverData;
     DDHAL_DDEXEBUFCALLBACKS mD3dBufferCallbacks;
     LPDDRAWI_DIRECTDRAW_INT This = (LPDDRAWI_DIRECTDRAW_INT)iface;
+    DDHAL_GETDRIVERINFODATA DdGetDriverInfo = { 0 };
 
     DX_WINDBG_trace();
 
@@ -461,13 +503,10 @@ StartDirectDrawHal(LPDIRECTDRAW iface, BOOL reenable)
     {
       DxHeapMemFree(This->lpLcl->lpGbl->lpModeInfo);
       DxHeapMemFree(ddgbl.lpDDCBtmp);
-      // FIXME Close DX fristcall and second call
+      // FIXME Close DX first and second call
       return DD_FALSE;
     }
 
-#if 0
-     DX_STUB_str("Trying alloc FourCCC \n");
-
     /* Alloc mpFourCC */
     if (This->lpLcl->lpGbl->lpdwFourCC != NULL)
     {
@@ -476,24 +515,17 @@ StartDirectDrawHal(LPDIRECTDRAW iface, BOOL reenable)
 
     if (mHALInfo.ddCaps.dwNumFourCCCodes > 0 )
     {
-        DxHeapMemAlloc(mpFourCC, sizeof(DWORD) * 21);
 
-        mpFourCC = (DWORD *) DxHeapMemAlloc(sizeof(DWORD) * (mHALInfo.ddCaps.dwNumFourCCCodes + 2));
+        DxHeapMemAlloc(mpFourCC, sizeof(DWORD) * (mHALInfo.ddCaps.dwNumFourCCCodes + 2));
 
         if (mpFourCC == NULL)
         {
             DxHeapMemFree(ddgbl.lpDDCBtmp);
-            // FIXME Close DX fristcall and second call
+            // FIXME Close DX first and second call
             return DD_FALSE;
         }
     }
 
-    DX_STUB_str("End Trying alloc FourCCC\n");
-#endif
-
-
-
-
     /* Alloc mpTextures */
 #if 0
     DX_STUB_str("1 Here\n");
@@ -511,7 +543,7 @@ StartDirectDrawHal(LPDIRECTDRAW iface, BOOL reenable)
         {
             DxHeapMemFree(mpFourCC);
             DxHeapMemFree(ddgbl.lpDDCBtmp);
-            // FIXME Close DX fristcall and second call
+            // FIXME Close DX first and second call
         }
     }
 
@@ -539,44 +571,82 @@ StartDirectDrawHal(LPDIRECTDRAW iface, BOOL reenable)
         DxHeapMemFree(mpFourCC);
         DxHeapMemFree(mpTextures);
         DxHeapMemFree(ddgbl.lpDDCBtmp);
-        // FIXME Close DX fristcall and second call
+        // FIXME Close DX first and second call
         return DD_FALSE;
     }
 
     memcpy(&ddgbl.vmiData, &mHALInfo.vmiData,sizeof(VIDMEMINFO));
-    
+
 
     memcpy(&ddgbl.ddCaps,  &mHALInfo.ddCaps,sizeof(DDCORECAPS));
 
     This->lpLcl->lpGbl->dwNumFourCC        = mHALInfo.ddCaps.dwNumFourCCCodes;
     This->lpLcl->lpGbl->lpdwFourCC         = mpFourCC;
-    This->lpLcl->lpGbl->dwMonitorFrequency = mHALInfo.dwMonitorFrequency;
+    // This->lpLcl->lpGbl->dwMonitorFrequency = mHALInfo.dwMonitorFrequency;     // 0
     This->lpLcl->lpGbl->dwModeIndex        = mHALInfo.dwModeIndex;
-    This->lpLcl->lpGbl->dwNumModes         = mHALInfo.dwNumModes;
-    This->lpLcl->lpGbl->lpModeInfo         = mHALInfo.lpModeInfo;
-
-    DX_STUB_str("Here\n");
+    // This->lpLcl->lpGbl->dwNumModes         = mHALInfo.dwNumModes;
+    // This->lpLcl->lpGbl->lpModeInfo         = mHALInfo.lpModeInfo;
 
     /* FIXME convert mpTextures to DDHALMODEINFO */
     // DxHeapMemFree( mpTextures);
 
     /* FIXME D3D setup mD3dCallbacks and mD3dDriverData */
-    DDHAL_GETDRIVERINFODATA DdGetDriverInfo = { 0 };
-    DdGetDriverInfo.dwSize = sizeof (DDHAL_GETDRIVERINFODATA);
-    DdGetDriverInfo.guidInfo = GUID_MiscellaneousCallbacks;
 
-    DdGetDriverInfo.lpvData = (PVOID)&ddgbl.lpDDCBtmp->HALDDMiscellaneous;
 
-    DdGetDriverInfo.dwExpectedSize = sizeof (DDHAL_DDMISCELLANEOUSCALLBACKS);
 
-    if(mHALInfo.GetDriverInfo (&DdGetDriverInfo) == DDHAL_DRIVER_NOTHANDLED || DdGetDriverInfo.ddRVal != DD_OK)
+
+    if (mHALInfo.dwFlags & DDHALINFO_GETDRIVERINFOSET)
     {
-        DxHeapMemFree(mpFourCC);
-        DxHeapMemFree(mpTextures);
-        DxHeapMemFree(ddgbl.lpDDCBtmp);
-        // FIXME Close DX fristcall and second call
-        return DD_FALSE;
+        DdGetDriverInfo.dwSize = sizeof (DDHAL_GETDRIVERINFODATA);
+        DdGetDriverInfo.guidInfo = GUID_MiscellaneousCallbacks;
+        DdGetDriverInfo.lpvData = (PVOID)&ddgbl.lpDDCBtmp->HALDDMiscellaneous;
+        DdGetDriverInfo.dwExpectedSize = sizeof (DDHAL_DDMISCELLANEOUSCALLBACKS);
+
+        if(mHALInfo.GetDriverInfo (&DdGetDriverInfo) == DDHAL_DRIVER_NOTHANDLED || DdGetDriverInfo.ddRVal != DD_OK)
+        {
+            DxHeapMemFree(mpFourCC);
+            DxHeapMemFree(mpTextures);
+            DxHeapMemFree(ddgbl.lpDDCBtmp);
+            // FIXME Close DX fristcall and second call
+            return DD_FALSE;
+        }
+
+        RtlZeroMemory(&DdGetDriverInfo, sizeof(DDHAL_GETDRIVERINFODATA));
+        DdGetDriverInfo.dwSize = sizeof (DDHAL_GETDRIVERINFODATA);
+        DdGetDriverInfo.guidInfo = GUID_Miscellaneous2Callbacks;
+
+        /* FIXME
+        DdGetDriverInfo.lpvData = (PVOID)&ddgbl.lpDDCBtmp->HALDDMiscellaneous;
+        DdGetDriverInfo.dwExpectedSize = sizeof (DDHAL_DDMISCELLANEOUS2CALLBACKS);
+
+        if(mHALInfo.GetDriverInfo (&DdGetDriverInfo) == DDHAL_DRIVER_NOTHANDLED || DdGetDriverInfo.ddRVal != DD_OK)
+        {
+            DxHeapMemFree(mpFourCC);
+            DxHeapMemFree(mpTextures);
+            DxHeapMemFree(ddgbl.lpDDCBtmp);
+            // FIXME Close DX fristcall and second call
+            return DD_FALSE;
+        }
+        DD_MISCELLANEOUS2CALLBACKS
+        {
+            DWORD                dwSize;
+            DWORD                dwFlags;
+            PDD_ALPHABLT         AlphaBlt;  // unsuse acoding msdn and always set to NULL
+            PDD_CREATESURFACEEX  CreateSurfaceEx;
+            PDD_GETDRIVERSTATE   GetDriverState;
+            PDD_DESTROYDDLOCAL   DestroyDDLocal;
+        }
+          DDHAL_MISC2CB32_CREATESURFACEEX
+          DDHAL_MISC2CB32_GETDRIVERSTATE
+          DDHAL_MISC2CB32_DESTROYDDLOCAL
+        */
     }
 
+    if (mHALInfo.dwFlags & DDHALINFO_GETDRIVERINFO2)
+    {
+        This->lpLcl->lpGbl->dwFlags = This->lpLcl->lpGbl->dwFlags | DDRAWI_DRIVERINFO2;
+    }
+
+
     return DD_OK;
 }