1 /* $Id: main.c 21434 2006-04-01 19:12:56Z greatlrd $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: lib/ddraw/ddraw.c
6 * PURPOSE: DirectDraw Library
7 * PROGRAMMER: Magnus Olsen (greatlrd)
14 DDRAWI_DIRECTDRAW_GBL ddgbl
;
15 DDRAWI_DDRAWSURFACE_GBL ddSurfGbl
;
22 Create_DirectDraw (LPGUID pGUID
, LPDIRECTDRAW
* pIface
,
25 LPDDRAWI_DIRECTDRAW_INT This
;
29 if ((IsBadReadPtr(pIface
,sizeof(LPDIRECTDRAW
))) ||
30 (IsBadWritePtr(pIface
,sizeof(LPDIRECTDRAW
))))
32 return DDERR_INVALIDPARAMS
;
35 This
= (LPDDRAWI_DIRECTDRAW_INT
)*pIface
;
37 /* fixme linking too second link when we shall not doing it */
38 if (IsBadReadPtr(This
,sizeof(LPDIRECTDRAW
)))
40 DX_STUB_str("1. no linking\n");
42 /* We do not have a DirectDraw interface, we need alloc it*/
43 LPDDRAWI_DIRECTDRAW_INT memThis
;
45 DxHeapMemAlloc(memThis
, sizeof(DDRAWI_DIRECTDRAW_INT
));
48 return DDERR_OUTOFMEMORY
;
53 /* Fixme release memory alloc if we fail */
55 DxHeapMemAlloc(This
->lpLcl
, sizeof(DDRAWI_DIRECTDRAW_LCL
));
56 if (This
->lpLcl
== NULL
)
58 return DDERR_OUTOFMEMORY
;
63 DX_STUB_str("2.linking\n");
64 /* We got the DirectDraw interface alloc and we need create the link */
65 LPDDRAWI_DIRECTDRAW_INT newThis
;
67 /* step 1.Alloc the new DDRAWI_DIRECTDRAW_INT for the lnking */
68 DxHeapMemAlloc(newThis
, sizeof(DDRAWI_DIRECTDRAW_INT
));
71 return DDERR_OUTOFMEMORY
;
74 /* step 2 check if it not DDCREATE_HARDWAREONLY we got if so we fail */
75 if ((pGUID
) && (pGUID
!= (LPGUID
)DDCREATE_HARDWAREONLY
))
80 return DDERR_INVALIDDIRECTDRAWGUID
;
84 /* step 3 do the link the old interface are store in the new one */
85 newThis
->lpLink
= This
;
87 /* step 4 we need create new local directdraw struct for the new linked interface */
88 DxHeapMemAlloc(newThis
->lpLcl
, sizeof(DDRAWI_DIRECTDRAW_LCL
));
89 if (newThis
->lpLcl
== NULL
)
92 return DDERR_OUTOFMEMORY
;
98 This
->lpLcl
->lpGbl
= &ddgbl
;
100 *pIface
= (LPDIRECTDRAW
)This
;
102 /* Get right interface we whant */
103 if (Main_DirectDraw_QueryInterface((LPDIRECTDRAW7
)This
, id
, (void**)&pIface
) == DD_OK
)
105 DX_STUB_str("Got iface\n");
107 if (StartDirectDraw((LPDIRECTDRAW
)This
, pGUID
, FALSE
) == DD_OK
);
110 RtlZeroMemory(&wnd_class, sizeof(wnd_class));
111 wnd_class.style = CS_HREDRAW | CS_VREDRAW;
112 wnd_class.lpfnWndProc = DefWindowProcW;
113 wnd_class.cbClsExtra = 0;
114 wnd_class.cbWndExtra = 0;
115 wnd_class.hInstance = GetModuleHandleW(0);
117 wnd_class.hCursor = 0;
118 wnd_class.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
119 wnd_class.lpszMenuName = NULL;
120 wnd_class.lpszClassName = classname;
121 if(!RegisterClassW(&wnd_class))
123 DX_STUB_str("DDERR_GENERIC");
124 return DDERR_GENERIC;
127 This
->lpLcl
->hDD
= ddgbl
.hDD
;
132 return DDERR_INVALIDPARAMS
;
137 StartDirectDraw(LPDIRECTDRAW iface
, LPGUID lpGuid
, BOOL reenable
)
139 LPDDRAWI_DIRECTDRAW_INT This
= (LPDDRAWI_DIRECTDRAW_INT
)iface
;
140 DWORD hal_ret
= DD_FALSE
;
141 DWORD hel_ret
= DD_FALSE
;
142 DWORD devicetypes
= 0;
148 * ddgbl.dwPDevice is not longer in use in windows 2000 and higher
149 * I am using it for device type
150 * devicetypes = 1 : both hal and hel are enable
151 * devicetypes = 2 : both hal are enable
152 * devicetypes = 3 : both hel are enable
153 * devicetypes = 4 :loading a guid drv from the register
156 if (reenable
== FALSE
)
158 if ((!IsBadReadPtr(This
->lpLink
,sizeof(LPDIRECTDRAW
))) && (This
->lpLink
== NULL
))
160 RtlZeroMemory(&ddgbl
, sizeof(DDRAWI_DIRECTDRAW_GBL
));
161 This
->lpLcl
->lpGbl
->dwRefCnt
++;
162 if (ddgbl
.lpDDCBtmp
== NULL
)
165 DxHeapMemAlloc( ddgbl
.lpDDCBtmp
, sizeof(DDHAL_CALLBACKS
));
166 if (ddgbl
.lpDDCBtmp
== NULL
)
168 DX_STUB_str("Out of memmory\n");
175 DX_STUB_str("here\n");
177 if (reenable
== FALSE
)
181 DX_STUB_str("lpGuid == NULL\n");
184 /* Create HDC for default, hal and hel driver */
185 This
->lpLcl
->hWnd
= (ULONG_PTR
) GetActiveWindow();
186 This
->lpLcl
->hDC
= (ULONG_PTR
) GetDC((HWND
)This
->lpLcl
->hWnd
);
188 /* cObsolete is undoc in msdn it being use in CreateDCA */
189 RtlCopyMemory(&ddgbl
.cObsolete
,&"DISPLAY",7);
190 RtlCopyMemory(&ddgbl
.cDriverName
,&"DISPLAY",7);
191 dwFlags
|= DDRAWI_DISPLAYDRV
| DDRAWI_GDIDRV
;
195 else if (lpGuid
== (LPGUID
) DDCREATE_HARDWAREONLY
)
198 /* Create HDC for default, hal driver */
199 This
->lpLcl
->hWnd
=(ULONG_PTR
) GetActiveWindow();
200 This
->lpLcl
->hDC
= (ULONG_PTR
) GetDC((HWND
)This
->lpLcl
->hWnd
);
202 /* cObsolete is undoc in msdn it being use in CreateDCA */
203 RtlCopyMemory(&ddgbl
.cObsolete
,&"DISPLAY",7);
204 RtlCopyMemory(&ddgbl
.cDriverName
,&"DISPLAY",7);
205 dwFlags
|= DDRAWI_DISPLAYDRV
| DDRAWI_GDIDRV
;
207 else if (lpGuid
== (LPGUID
) DDCREATE_EMULATIONONLY
)
211 /* Create HDC for default, hal and hel driver */
212 This
->lpLcl
->hWnd
= (ULONG_PTR
) GetActiveWindow();
213 This
->lpLcl
->hDC
= (ULONG_PTR
) GetDC((HWND
)This
->lpLcl
->hWnd
);
215 /* cObsolete is undoc in msdn it being use in CreateDCA */
216 RtlCopyMemory(&ddgbl
.cObsolete
,&"DISPLAY",7);
217 RtlCopyMemory(&ddgbl
.cDriverName
,&"DISPLAY",7);
219 dwFlags
|= DDRAWI_DISPLAYDRV
| DDRAWI_GDIDRV
;
223 /* FIXME : need getting driver from the GUID that have been pass in from
224 * the register. we do not support that yet
227 This
->lpLcl
->hDC
= (ULONG_PTR
) NULL
;
228 This
->lpLcl
->hWnd
= (ULONG_PTR
) GetActiveWindow();
231 if ( (HDC
)This
->lpLcl
->hDC
== NULL
)
233 DX_STUB_str("DDERR_OUTOFMEMORY\n");
234 return DDERR_OUTOFMEMORY
;
238 This
->lpLcl
->lpDDCB
= ddgbl
.lpDDCBtmp
;
240 /* Startup HEL and HAL */
241 This
->lpLcl
->lpDDCB
= This
->lpLcl
->lpGbl
->lpDDCBtmp
;
242 This
->lpLcl
->dwProcessId
= GetCurrentProcessId();
247 hal_ret
= StartDirectDrawHal(iface
, reenable
);
248 This
->lpLcl
->lpDDCB
->HELDD
.dwFlags
= 0;
252 hel_ret
= StartDirectDrawHel(iface
, reenable
);
253 This
->lpLcl
->lpDDCB
->HALDD
.dwFlags
= 0;
257 hal_ret
= StartDirectDrawHal(iface
, reenable
);
258 hel_ret
= StartDirectDrawHel(iface
, reenable
);
261 DX_STUB_str("return\n");
267 DX_STUB_str("DDERR_NODIRECTDRAWSUPPORT\n");
268 return DDERR_NODIRECTDRAWSUPPORT
;
270 dwFlags
|= DDRAWI_NOHARDWARE
;
275 dwFlags
|= DDRAWI_NOEMULATION
;
280 dwFlags
|= DDRAWI_EMULATIONINITIALIZED
;
283 /* Fill some basic info for Surface */
284 This
->lpLcl
->lpGbl
->dwFlags
= dwFlags
| DDRAWI_ATTACHEDTODESKTOP
;
285 This
->lpLcl
->lpDDCB
= This
->lpLcl
->lpGbl
->lpDDCBtmp
;
286 This
->lpLcl
->hDD
= ddgbl
.hDD
;
288 DX_STUB_str("DD_OK\n");
293 StartDirectDrawHel(LPDIRECTDRAW iface
, BOOL reenable
)
295 LPDDRAWI_DIRECTDRAW_INT This
= (LPDDRAWI_DIRECTDRAW_INT
)iface
;
297 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.CanCreateSurface
= HelDdCanCreateSurface
;
298 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.CreateSurface
= HelDdCreateSurface
;
299 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.CreatePalette
= HelDdCreatePalette
;
300 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.DestroyDriver
= HelDdDestroyDriver
;
301 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.FlipToGDISurface
= HelDdFlipToGDISurface
;
302 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.GetScanLine
= HelDdGetScanLine
;
303 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.SetColorKey
= HelDdSetColorKey
;
304 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.SetExclusiveMode
= HelDdSetExclusiveMode
;
305 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.SetMode
= HelDdSetMode
;
306 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.WaitForVerticalBlank
= HelDdWaitForVerticalBlank
;
308 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.dwFlags
= DDHAL_CB32_CANCREATESURFACE
|
309 DDHAL_CB32_CREATESURFACE
|
310 DDHAL_CB32_CREATEPALETTE
|
311 DDHAL_CB32_DESTROYDRIVER
|
312 DDHAL_CB32_FLIPTOGDISURFACE
|
313 DDHAL_CB32_GETSCANLINE
|
314 DDHAL_CB32_SETCOLORKEY
|
315 DDHAL_CB32_SETEXCLUSIVEMODE
|
317 DDHAL_CB32_WAITFORVERTICALBLANK
;
319 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.dwSize
= sizeof(This
->lpLcl
->lpDDCB
->HELDD
);
321 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.AddAttachedSurface
= HelDdSurfAddAttachedSurface
;
322 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Blt
= HelDdSurfBlt
;
323 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.DestroySurface
= HelDdSurfDestroySurface
;
324 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Flip
= HelDdSurfFlip
;
325 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.GetBltStatus
= HelDdSurfGetBltStatus
;
326 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.GetFlipStatus
= HelDdSurfGetFlipStatus
;
327 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Lock
= HelDdSurfLock
;
328 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.reserved4
= HelDdSurfreserved4
;
329 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetClipList
= HelDdSurfSetClipList
;
330 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetColorKey
= HelDdSurfSetColorKey
;
331 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetOverlayPosition
= HelDdSurfSetOverlayPosition
;
332 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetPalette
= HelDdSurfSetPalette
;
333 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Unlock
= HelDdSurfUnlock
;
334 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.UpdateOverlay
= HelDdSurfUpdateOverlay
;
335 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.dwFlags
= DDHAL_SURFCB32_ADDATTACHEDSURFACE
|
337 DDHAL_SURFCB32_DESTROYSURFACE
|
338 DDHAL_SURFCB32_FLIP
|
339 DDHAL_SURFCB32_GETBLTSTATUS
|
340 DDHAL_SURFCB32_GETFLIPSTATUS
|
341 DDHAL_SURFCB32_LOCK
|
342 DDHAL_SURFCB32_RESERVED4
|
343 DDHAL_SURFCB32_SETCLIPLIST
|
344 DDHAL_SURFCB32_SETCOLORKEY
|
345 DDHAL_SURFCB32_SETOVERLAYPOSITION
|
346 DDHAL_SURFCB32_SETPALETTE
|
347 DDHAL_SURFCB32_UNLOCK
|
348 DDHAL_SURFCB32_UPDATEOVERLAY
;
350 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.dwSize
= sizeof(This
->lpLcl
->lpDDCB
->HELDDSurface
);
353 This->lpLcl->lpDDCB->HELDDPalette.DestroyPalette = HelDdPalDestroyPalette;
354 This->lpLcl->lpDDCB->HELDDPalette.SetEntries = HelDdPalSetEntries;
355 This->lpLcl->lpDDCB->HELDDPalette.dwSize = sizeof(This->lpLcl->lpDDCB->HELDDPalette);
359 This->lpLcl->lpDDCB->HELDDExeBuf.CanCreateExecuteBuffer = HelDdExeCanCreateExecuteBuffer;
360 This->lpLcl->lpDDCB->HELDDExeBuf.CreateExecuteBuffer = HelDdExeCreateExecuteBuffer;
361 This->lpLcl->lpDDCB->HELDDExeBuf.DestroyExecuteBuffer = HelDdExeDestroyExecuteBuffer;
362 This->lpLcl->lpDDCB->HELDDExeBuf.LockExecuteBuffer = HelDdExeLockExecuteBuffer;
363 This->lpLcl->lpDDCB->HELDDExeBuf.UnlockExecuteBuffer = HelDdExeUnlockExecuteBuffer;
371 StartDirectDrawHal(LPDIRECTDRAW iface
, BOOL reenable
)
373 LPDWORD mpFourCC
= NULL
;
375 BOOL newmode
= FALSE
;
376 LPDDSURFACEDESC mpTextures
;
377 D3DHAL_CALLBACKS mD3dCallbacks
;
378 D3DHAL_GLOBALDRIVERDATA mD3dDriverData
;
379 DDHAL_DDEXEBUFCALLBACKS mD3dBufferCallbacks
;
380 LPDDRAWI_DIRECTDRAW_INT This
= (LPDDRAWI_DIRECTDRAW_INT
)iface
;
384 RtlZeroMemory(&mHALInfo
, sizeof(DDHALINFO
));
385 RtlZeroMemory(&mD3dCallbacks
, sizeof(D3DHAL_CALLBACKS
));
386 RtlZeroMemory(&mD3dDriverData
, sizeof(D3DHAL_GLOBALDRIVERDATA
));
387 RtlZeroMemory(&mD3dBufferCallbacks
, sizeof(DDHAL_DDEXEBUFCALLBACKS
));
389 if (reenable
== FALSE
)
391 if (ddgbl
.lpDDCBtmp
== NULL
)
393 DxHeapMemAlloc(ddgbl
.lpDDCBtmp
, sizeof(DDHAL_CALLBACKS
));
394 if ( ddgbl
.lpDDCBtmp
== NULL
)
402 RtlZeroMemory(ddgbl
.lpDDCBtmp
,sizeof(DDHAL_CALLBACKS
));
406 * Startup DX HAL step one of three
408 if (!DdCreateDirectDrawObject(This
->lpLcl
->lpGbl
, (HDC
)This
->lpLcl
->hDC
))
410 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
414 /* Some card disable the dx after it have been created so
415 * we are force reanble it
417 if (!DdReenableDirectDrawObject(This
->lpLcl
->lpGbl
, &newmode
))
419 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
423 if (!DdQueryDirectDrawObject(This
->lpLcl
->lpGbl
,
425 &ddgbl
.lpDDCBtmp
->HALDD
,
426 &ddgbl
.lpDDCBtmp
->HALDDSurface
,
427 &ddgbl
.lpDDCBtmp
->HALDDPalette
,
430 &mD3dBufferCallbacks
,
435 DxHeapMemFree(This
->lpLcl
->lpGbl
->lpModeInfo
);
436 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
437 // FIXME Close DX fristcall and second call
441 DX_STUB_str("Trying alloc FourCCC \n");
444 if (This
->lpLcl
->lpGbl
->lpdwFourCC
!= NULL
)
446 DxHeapMemFree(This
->lpLcl
->lpGbl
->lpdwFourCC
);
449 if (mHALInfo
.ddCaps
.dwNumFourCCCodes
> 0 )
452 DxHeapMemAlloc(mpFourCC
, sizeof(DWORD
) * (mHALInfo
.ddCaps
.dwNumFourCCCodes
+ 2));
454 if (mpFourCC
== NULL
)
456 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
457 // FIXME Close DX fristcall and second call
462 DX_STUB_str("End Trying alloc FourCCC\n");
468 /* Alloc mpTextures */
470 DX_STUB_str("1 Here\n");
472 if (This
->lpLcl
->lpGbl
->texture
!= NULL
)
474 DxHeapMemFree(This
->lpLcl
->lpGbl
->texture
;
478 if (mD3dDriverData
.dwNumTextureFormats
> 0)
480 mpTextures
= (DDSURFACEDESC
*) DxHeapMemAlloc(sizeof(DDSURFACEDESC
) * mD3dDriverData
.dwNumTextureFormats
);
481 if (mpTextures
== NULL
)
483 DxHeapMemFree(mpFourCC
);
484 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
485 // FIXME Close DX fristcall and second call
489 DX_STUB_str("2 Here\n");
496 /* Get all basic data from the driver */
497 if (!DdQueryDirectDrawObject(
500 &ddgbl
.lpDDCBtmp
->HALDD
,
501 &ddgbl
.lpDDCBtmp
->HALDDSurface
,
502 &ddgbl
.lpDDCBtmp
->HALDDPalette
,
505 &ddgbl
.lpDDCBtmp
->HALDDExeBuf
,
506 (DDSURFACEDESC
*)mpTextures
,
510 DxHeapMemFree(mpFourCC
);
511 DxHeapMemFree(mpTextures
);
512 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
513 // FIXME Close DX fristcall and second call
517 memcpy(&ddgbl
.vmiData
, &mHALInfo
.vmiData
,sizeof(VIDMEMINFO
));
520 memcpy(&ddgbl
.ddCaps
, &mHALInfo
.ddCaps
,sizeof(DDCORECAPS
));
522 This
->lpLcl
->lpGbl
->dwNumFourCC
= mHALInfo
.ddCaps
.dwNumFourCCCodes
;
523 This
->lpLcl
->lpGbl
->lpdwFourCC
= mpFourCC
;
524 This
->lpLcl
->lpGbl
->dwMonitorFrequency
= mHALInfo
.dwMonitorFrequency
;
525 This
->lpLcl
->lpGbl
->dwModeIndex
= mHALInfo
.dwModeIndex
;
526 This
->lpLcl
->lpGbl
->dwNumModes
= mHALInfo
.dwNumModes
;
527 This
->lpLcl
->lpGbl
->lpModeInfo
= mHALInfo
.lpModeInfo
;
529 DX_STUB_str("Here\n");
531 /* FIXME convert mpTextures to DDHALMODEINFO */
532 // DxHeapMemFree( mpTextures);
534 /* FIXME D3D setup mD3dCallbacks and mD3dDriverData */
535 DDHAL_GETDRIVERINFODATA DdGetDriverInfo
= { 0 };
536 DdGetDriverInfo
.dwSize
= sizeof (DDHAL_GETDRIVERINFODATA
);
537 DdGetDriverInfo
.guidInfo
= GUID_MiscellaneousCallbacks
;
539 DdGetDriverInfo
.lpvData
= (PVOID
)&ddgbl
.lpDDCBtmp
->HALDDMiscellaneous
;
541 DdGetDriverInfo
.dwExpectedSize
= sizeof (DDHAL_DDMISCELLANEOUSCALLBACKS
);
543 if(mHALInfo
.GetDriverInfo (&DdGetDriverInfo
) == DDHAL_DRIVER_NOTHANDLED
|| DdGetDriverInfo
.ddRVal
!= DD_OK
)
545 DxHeapMemFree(mpFourCC
);
546 DxHeapMemFree(mpTextures
);
547 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
548 // FIXME Close DX fristcall and second call