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)
16 DDRAWI_DIRECTDRAW_GBL ddgbl
;
17 DDRAWI_DDRAWSURFACE_GBL ddSurfGbl
;
24 Create_DirectDraw (LPGUID pGUID
, LPDIRECTDRAW
* pIface
,
27 LPDDRAWI_DIRECTDRAW_INT This
;
31 if ((IsBadReadPtr(pIface
,sizeof(LPDIRECTDRAW
))) ||
32 (IsBadWritePtr(pIface
,sizeof(LPDIRECTDRAW
))))
34 return DDERR_INVALIDPARAMS
;
37 This
= (LPDDRAWI_DIRECTDRAW_INT
)*pIface
;
40 /* fixme linking too second link when we shall not doing it */
41 if (IsBadReadPtr(This
,sizeof(LPDIRECTDRAW
)))
43 DX_STUB_str("1. no linking\n");
44 /* We do not have a DirectDraw interface, we need alloc it*/
45 LPDDRAWI_DIRECTDRAW_INT memThis
;
47 memThis
= DxHeapMemAlloc(sizeof(DDRAWI_DIRECTDRAW_INT
));
52 DxHeapMemFree(memThis
);
54 DX_STUB_str("DDERR_OUTOFMEMORY");
55 return DDERR_OUTOFMEMORY
;
58 /* Fixme release memory alloc if we fail */
59 This
->lpLcl
= DxHeapMemAlloc(sizeof(DDRAWI_DIRECTDRAW_INT
));
60 if (This
->lpLcl
== NULL
)
62 DX_STUB_str("DDERR_OUTOFMEMORY");
63 return DDERR_OUTOFMEMORY
;
68 DX_STUB_str("2.linking\n");
69 /* We got the DirectDraw interface alloc and we need create the link */
70 LPDDRAWI_DIRECTDRAW_INT newThis
;
72 /* step 1.Alloc the new DDRAWI_DIRECTDRAW_INT for the lnking */
73 newThis
= DxHeapMemAlloc(sizeof(DDRAWI_DIRECTDRAW_INT
));
76 DX_STUB_str("DDERR_OUTOFMEMORY\n");
77 return DDERR_OUTOFMEMORY
;
80 /* step 2 check if it not DDCREATE_HARDWAREONLY we got if so we fail */
81 if ((pGUID
) && (pGUID
!= (LPGUID
)DDCREATE_HARDWAREONLY
))
86 DX_STUB_str("DDERR_INVALIDDIRECTDRAWGUID\n");
87 return DDERR_INVALIDDIRECTDRAWGUID
;
91 /* step 3 do the link the old interface are store in the new one */
92 newThis
->lpLink
= This
;
94 /* step 4 we need create new local directdraw struct for the new linked interface */
95 newThis
->lpLcl
= DxHeapMemAlloc(sizeof(DDRAWI_DIRECTDRAW_LCL
));
96 if (newThis
->lpLcl
== NULL
)
99 DX_STUB_str("DDERR_OUTOFMEMORY");
100 return DDERR_OUTOFMEMORY
;
106 /* FIXME HACK linking does not working we need figout why */
107 LPDDRAWI_DIRECTDRAW_INT memThis
;
109 memThis
= DxHeapMemAlloc(sizeof(DDRAWI_DIRECTDRAW_INT
));
114 DxHeapMemFree(memThis
);
116 DX_STUB_str("DDERR_OUTOFMEMORY");
117 return DDERR_OUTOFMEMORY
;
120 /* Fixme release memory alloc if we fail */
121 This
->lpLcl
= DxHeapMemAlloc(sizeof(DDRAWI_DIRECTDRAW_INT
));
122 if (This
->lpLcl
== NULL
)
124 DX_STUB_str("DDERR_OUTOFMEMORY");
125 return DDERR_OUTOFMEMORY
;
129 This
->lpLcl
->lpGbl
= &ddgbl
;
131 *pIface
= (LPDIRECTDRAW
)This
;
133 /* Get right interface we whant */
134 if (Main_DirectDraw_QueryInterface((LPDIRECTDRAW7
)This
, id
, (void**)&pIface
) == DD_OK
)
136 DX_STUB_str("Got iface");
138 if (StartDirectDraw((LPDIRECTDRAW
)This
, pGUID
, FALSE
) == DD_OK
);
141 RtlZeroMemory(&wnd_class, sizeof(wnd_class));
142 wnd_class.style = CS_HREDRAW | CS_VREDRAW;
143 wnd_class.lpfnWndProc = DefWindowProcW;
144 wnd_class.cbClsExtra = 0;
145 wnd_class.cbWndExtra = 0;
146 wnd_class.hInstance = GetModuleHandleW(0);
148 wnd_class.hCursor = 0;
149 wnd_class.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
150 wnd_class.lpszMenuName = NULL;
151 wnd_class.lpszClassName = classname;
152 if(!RegisterClassW(&wnd_class))
154 DX_STUB_str("DDERR_GENERIC");
155 return DDERR_GENERIC;
159 DX_STUB_str("DD_OK");
164 DX_STUB_str("DDERR_INVALIDPARAMS");
165 return DDERR_INVALIDPARAMS
;
170 StartDirectDraw(LPDIRECTDRAW iface
, LPGUID lpGuid
, BOOL reenable
)
172 LPDDRAWI_DIRECTDRAW_INT This
= (LPDDRAWI_DIRECTDRAW_INT
)iface
;
173 DWORD hal_ret
= DD_FALSE
;
174 DWORD hel_ret
= DD_FALSE
;
175 DWORD devicetypes
= 0;
181 * ddgbl.dwPDevice is not longer in use in windows 2000 and higher
182 * I am using it for device type
183 * devicetypes = 1 : both hal and hel are enable
184 * devicetypes = 2 : both hal are enable
185 * devicetypes = 3 : both hel are enable
186 * devicetypes = 4 :loading a guid drv from the register
189 if (reenable
== FALSE
)
191 if (This
->lpLink
== NULL
)
193 RtlZeroMemory(&ddgbl
, sizeof(DDRAWI_DIRECTDRAW_GBL
));
194 This
->lpLcl
->lpGbl
->dwRefCnt
++;
195 if (ddgbl
.lpDDCBtmp
== NULL
)
197 ddgbl
.lpDDCBtmp
= (LPDDHAL_CALLBACKS
) DxHeapMemAlloc(sizeof(DDHAL_CALLBACKS
));
198 if (ddgbl
.lpDDCBtmp
== NULL
)
200 DX_STUB_str("Out of memmory\n");
207 DX_STUB_str("here\n");
209 if (reenable
== FALSE
)
213 DX_STUB_str("lpGuid == NULL\n");
216 /* Create HDC for default, hal and hel driver */
217 This
->lpLcl
->hWnd
= (ULONG_PTR
) GetActiveWindow();
218 This
->lpLcl
->hDC
= (ULONG_PTR
) GetDC((HWND
)This
->lpLcl
->hWnd
);
220 /* cObsolete is undoc in msdn it being use in CreateDCA */
221 RtlCopyMemory(&ddgbl
.cObsolete
,&"DISPLAY",7);
222 RtlCopyMemory(&ddgbl
.cDriverName
,&"DISPLAY",7);
223 dwFlags
|= DDRAWI_DISPLAYDRV
| DDRAWI_GDIDRV
;
227 else if (lpGuid
== (LPGUID
) DDCREATE_HARDWAREONLY
)
230 /* Create HDC for default, hal driver */
231 This
->lpLcl
->hWnd
=(ULONG_PTR
) GetActiveWindow();
232 This
->lpLcl
->hDC
= (ULONG_PTR
) GetDC((HWND
)This
->lpLcl
->hWnd
);
234 /* cObsolete is undoc in msdn it being use in CreateDCA */
235 RtlCopyMemory(&ddgbl
.cObsolete
,&"DISPLAY",7);
236 RtlCopyMemory(&ddgbl
.cDriverName
,&"DISPLAY",7);
237 dwFlags
|= DDRAWI_DISPLAYDRV
| DDRAWI_GDIDRV
;
239 else if (lpGuid
== (LPGUID
) DDCREATE_EMULATIONONLY
)
243 /* Create HDC for default, hal and hel driver */
244 This
->lpLcl
->hWnd
= (ULONG_PTR
) GetActiveWindow();
245 This
->lpLcl
->hDC
= (ULONG_PTR
) GetDC((HWND
)This
->lpLcl
->hWnd
);
247 /* cObsolete is undoc in msdn it being use in CreateDCA */
248 RtlCopyMemory(&ddgbl
.cObsolete
,&"DISPLAY",7);
249 RtlCopyMemory(&ddgbl
.cDriverName
,&"DISPLAY",7);
251 dwFlags
|= DDRAWI_DISPLAYDRV
| DDRAWI_GDIDRV
;
255 /* FIXME : need getting driver from the GUID that have been pass in from
256 * the register. we do not support that yet
259 This
->lpLcl
->hDC
= (ULONG_PTR
) NULL
;
260 This
->lpLcl
->hWnd
= (ULONG_PTR
) GetActiveWindow();
263 if ( (HDC
)This
->lpLcl
->hDC
== NULL
)
265 DX_STUB_str("DDERR_OUTOFMEMORY\n");
266 return DDERR_OUTOFMEMORY
;
270 This
->lpLcl
->lpDDCB
= ddgbl
.lpDDCBtmp
;
272 /* Startup HEL and HAL */
273 This
->lpLcl
->lpDDCB
= This
->lpLcl
->lpGbl
->lpDDCBtmp
;
274 This
->lpLcl
->dwProcessId
= GetCurrentProcessId();
279 hal_ret
= StartDirectDrawHal(iface
, reenable
);
280 This
->lpLcl
->lpDDCB
->HELDD
.dwFlags
= 0;
284 hel_ret
= StartDirectDrawHel(iface
, reenable
);
285 This
->lpLcl
->lpDDCB
->HALDD
.dwFlags
= 0;
289 hal_ret
= StartDirectDrawHal(iface
, reenable
);
290 hel_ret
= StartDirectDrawHel(iface
, reenable
);
293 DX_STUB_str("return\n");
299 DX_STUB_str("DDERR_NODIRECTDRAWSUPPORT\n");
300 return DDERR_NODIRECTDRAWSUPPORT
;
302 dwFlags
|= DDRAWI_NOHARDWARE
;
307 dwFlags
|= DDRAWI_NOEMULATION
;
312 dwFlags
|= DDRAWI_EMULATIONINITIALIZED
;
315 /* Fill some basic info for Surface */
316 This
->lpLcl
->lpGbl
->dwFlags
= dwFlags
| DDRAWI_ATTACHEDTODESKTOP
;
317 This
->lpLcl
->lpDDCB
= This
->lpLcl
->lpGbl
->lpDDCBtmp
;
318 This
->lpLcl
->hDD
= This
->lpLcl
->lpGbl
->hDD
;
319 ddgbl
.hDD
= This
->lpLcl
->lpGbl
->hDD
;
321 DX_STUB_str("DD_OK\n");
326 StartDirectDrawHel(LPDIRECTDRAW iface
, BOOL reenable
)
328 LPDDRAWI_DIRECTDRAW_INT This
= (LPDDRAWI_DIRECTDRAW_INT
)iface
;
330 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.CanCreateSurface
= HelDdCanCreateSurface
;
331 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.CreateSurface
= HelDdCreateSurface
;
332 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.CreatePalette
= HelDdCreatePalette
;
333 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.DestroyDriver
= HelDdDestroyDriver
;
334 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.FlipToGDISurface
= HelDdFlipToGDISurface
;
335 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.GetScanLine
= HelDdGetScanLine
;
336 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.SetColorKey
= HelDdSetColorKey
;
337 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.SetExclusiveMode
= HelDdSetExclusiveMode
;
338 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.SetMode
= HelDdSetMode
;
339 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.WaitForVerticalBlank
= HelDdWaitForVerticalBlank
;
341 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.dwFlags
= DDHAL_CB32_CANCREATESURFACE
|
342 DDHAL_CB32_CREATESURFACE
|
343 DDHAL_CB32_CREATEPALETTE
|
344 DDHAL_CB32_DESTROYDRIVER
|
345 DDHAL_CB32_FLIPTOGDISURFACE
|
346 DDHAL_CB32_GETSCANLINE
|
347 DDHAL_CB32_SETCOLORKEY
|
348 DDHAL_CB32_SETEXCLUSIVEMODE
|
350 DDHAL_CB32_WAITFORVERTICALBLANK
;
352 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.dwSize
= sizeof(This
->lpLcl
->lpDDCB
->HELDD
);
354 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.AddAttachedSurface
= HelDdSurfAddAttachedSurface
;
355 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Blt
= HelDdSurfBlt
;
356 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.DestroySurface
= HelDdSurfDestroySurface
;
357 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Flip
= HelDdSurfFlip
;
358 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.GetBltStatus
= HelDdSurfGetBltStatus
;
359 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.GetFlipStatus
= HelDdSurfGetFlipStatus
;
360 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Lock
= HelDdSurfLock
;
361 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.reserved4
= HelDdSurfreserved4
;
362 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetClipList
= HelDdSurfSetClipList
;
363 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetColorKey
= HelDdSurfSetColorKey
;
364 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetOverlayPosition
= HelDdSurfSetOverlayPosition
;
365 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetPalette
= HelDdSurfSetPalette
;
366 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Unlock
= HelDdSurfUnlock
;
367 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.UpdateOverlay
= HelDdSurfUpdateOverlay
;
368 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.dwFlags
= DDHAL_SURFCB32_ADDATTACHEDSURFACE
|
370 DDHAL_SURFCB32_DESTROYSURFACE
|
371 DDHAL_SURFCB32_FLIP
|
372 DDHAL_SURFCB32_GETBLTSTATUS
|
373 DDHAL_SURFCB32_GETFLIPSTATUS
|
374 DDHAL_SURFCB32_LOCK
|
375 DDHAL_SURFCB32_RESERVED4
|
376 DDHAL_SURFCB32_SETCLIPLIST
|
377 DDHAL_SURFCB32_SETCOLORKEY
|
378 DDHAL_SURFCB32_SETOVERLAYPOSITION
|
379 DDHAL_SURFCB32_SETPALETTE
|
380 DDHAL_SURFCB32_UNLOCK
|
381 DDHAL_SURFCB32_UPDATEOVERLAY
;
383 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.dwSize
= sizeof(This
->lpLcl
->lpDDCB
->HELDDSurface
);
386 This->lpLcl->lpDDCB->HELDDPalette.DestroyPalette = HelDdPalDestroyPalette;
387 This->lpLcl->lpDDCB->HELDDPalette.SetEntries = HelDdPalSetEntries;
388 This->lpLcl->lpDDCB->HELDDPalette.dwSize = sizeof(This->lpLcl->lpDDCB->HELDDPalette);
392 This->lpLcl->lpDDCB->HELDDExeBuf.CanCreateExecuteBuffer = HelDdExeCanCreateExecuteBuffer;
393 This->lpLcl->lpDDCB->HELDDExeBuf.CreateExecuteBuffer = HelDdExeCreateExecuteBuffer;
394 This->lpLcl->lpDDCB->HELDDExeBuf.DestroyExecuteBuffer = HelDdExeDestroyExecuteBuffer;
395 This->lpLcl->lpDDCB->HELDDExeBuf.LockExecuteBuffer = HelDdExeLockExecuteBuffer;
396 This->lpLcl->lpDDCB->HELDDExeBuf.UnlockExecuteBuffer = HelDdExeUnlockExecuteBuffer;
404 StartDirectDrawHal(LPDIRECTDRAW iface
, BOOL reenable
)
408 BOOL newmode
= FALSE
;
409 LPDDSURFACEDESC mpTextures
;
410 D3DHAL_CALLBACKS mD3dCallbacks
;
411 D3DHAL_GLOBALDRIVERDATA mD3dDriverData
;
412 DDHAL_DDEXEBUFCALLBACKS mD3dBufferCallbacks
;
413 LPDDRAWI_DIRECTDRAW_INT This
= (LPDDRAWI_DIRECTDRAW_INT
)iface
;
417 RtlZeroMemory(&mHALInfo
, sizeof(DDHALINFO
));
418 RtlZeroMemory(&mD3dCallbacks
, sizeof(D3DHAL_CALLBACKS
));
419 RtlZeroMemory(&mD3dDriverData
, sizeof(D3DHAL_GLOBALDRIVERDATA
));
420 RtlZeroMemory(&mD3dBufferCallbacks
, sizeof(DDHAL_DDEXEBUFCALLBACKS
));
422 if (reenable
== FALSE
)
424 if (ddgbl
.lpDDCBtmp
== NULL
)
426 ddgbl
.lpDDCBtmp
= DxHeapMemAlloc(sizeof(DDHAL_CALLBACKS
));
427 if ( ddgbl
.lpDDCBtmp
== NULL
)
435 RtlZeroMemory(ddgbl
.lpDDCBtmp
,sizeof(DDHAL_CALLBACKS
));
439 * Startup DX HAL step one of three
441 if (!DdCreateDirectDrawObject(This
->lpLcl
->lpGbl
, (HDC
)This
->lpLcl
->hDC
))
443 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
447 /* Some card disable the dx after it have been created so
448 * we are force reanble it
450 if (!DdReenableDirectDrawObject(This
->lpLcl
->lpGbl
, &newmode
))
452 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
456 if (!DdQueryDirectDrawObject(This
->lpLcl
->lpGbl
,
458 &ddgbl
.lpDDCBtmp
->HALDD
,
459 &ddgbl
.lpDDCBtmp
->HALDDSurface
,
460 &ddgbl
.lpDDCBtmp
->HALDDPalette
,
463 &mD3dBufferCallbacks
,
468 DxHeapMemFree(This
->lpLcl
->lpGbl
->lpModeInfo
);
469 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
470 // FIXME Close DX fristcall and second call
476 if (mHALInfo
.ddCaps
.dwNumFourCCCodes
> 0 )
478 mpFourCC
= (DWORD
*) DxHeapMemAlloc(sizeof(DWORD
) * mHALInfo
.ddCaps
.dwNumFourCCCodes
);
479 if (mpFourCC
== NULL
)
481 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
482 // FIXME Close DX fristcall and second call
486 DX_STUB_str("Here\n");
488 /* Alloc mpTextures */
491 if (mD3dDriverData
.dwNumTextureFormats
> 0)
493 mpTextures
= (DDSURFACEDESC
*) DxHeapMemAlloc(sizeof(DDSURFACEDESC
) * mD3dDriverData
.dwNumTextureFormats
);
494 if (mpTextures
== NULL
)
496 DxHeapMemFree(mpFourCC
);
497 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
498 // FIXME Close DX fristcall and second call
505 DX_STUB_str("Here\n");
507 /* Get all basic data from the driver */
508 if (!DdQueryDirectDrawObject(
511 &ddgbl
.lpDDCBtmp
->HALDD
,
512 &ddgbl
.lpDDCBtmp
->HALDDSurface
,
513 &ddgbl
.lpDDCBtmp
->HALDDPalette
,
516 &ddgbl
.lpDDCBtmp
->HALDDExeBuf
,
517 (DDSURFACEDESC
*)mpTextures
,
521 DxHeapMemFree(mpFourCC
);
522 DxHeapMemFree(mpTextures
);
523 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
524 // FIXME Close DX fristcall and second call
528 memcpy(&ddgbl
.vmiData
, &mHALInfo
.vmiData
,sizeof(VIDMEMINFO
));
531 memcpy(&ddgbl
.ddCaps
, &mHALInfo
.ddCaps
,sizeof(DDCORECAPS
));
533 This
->lpLcl
->lpGbl
->dwNumFourCC
= mHALInfo
.ddCaps
.dwNumFourCCCodes
;
534 This
->lpLcl
->lpGbl
->lpdwFourCC
= mpFourCC
;
536 This
->lpLcl
->lpGbl
->dwMonitorFrequency
= mHALInfo
.dwMonitorFrequency
;
537 This
->lpLcl
->lpGbl
->dwModeIndex
= mHALInfo
.dwModeIndex
;
538 This
->lpLcl
->lpGbl
->dwNumModes
= mHALInfo
.dwNumModes
;
539 This
->lpLcl
->lpGbl
->lpModeInfo
= mHALInfo
.lpModeInfo
;
541 DX_STUB_str("Here\n");
543 /* FIXME convert mpTextures to DDHALMODEINFO */
544 // DxHeapMemFree( mpTextures);
546 /* FIXME D3D setup mD3dCallbacks and mD3dDriverData */
547 DDHAL_GETDRIVERINFODATA DdGetDriverInfo
= { 0 };
548 DdGetDriverInfo
.dwSize
= sizeof (DDHAL_GETDRIVERINFODATA
);
549 DdGetDriverInfo
.guidInfo
= GUID_MiscellaneousCallbacks
;
550 DdGetDriverInfo
.lpvData
= (PVOID
)&ddgbl
.lpDDCBtmp
->cbDDMiscellaneousCallbacks
;
551 DdGetDriverInfo
.dwExpectedSize
= sizeof (DDHAL_DDMISCELLANEOUSCALLBACKS
);
552 if(mHALInfo
.GetDriverInfo (&DdGetDriverInfo
) == DDHAL_DRIVER_NOTHANDLED
|| DdGetDriverInfo
.ddRVal
!= DD_OK
)
554 DxHeapMemFree(mpFourCC
);
555 DxHeapMemFree(mpTextures
);
556 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
557 // FIXME Close DX fristcall and second call
561 DX_STUB_str("Return DD_OK\n");