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
;
26 Create_DirectDraw (LPGUID pGUID
,
31 LPDDRAWI_DIRECTDRAW_INT This
= (LPDDRAWI_DIRECTDRAW_INT
)*pIface
;
37 /* We do not have a DirectDraw interface, we need alloc it*/
38 LPDDRAWI_DIRECTDRAW_INT memThis
;
40 memThis
= DxHeapMemAlloc(sizeof(DDRAWI_DIRECTDRAW_INT
));
45 DxHeapMemFree(memThis
);
47 DX_STUB_str("DDERR_OUTOFMEMORY");
48 return DDERR_OUTOFMEMORY
;
53 /* We got the DirectDraw interface alloc and we need create the link */
54 LPDDRAWI_DIRECTDRAW_INT newThis
;
55 newThis
= DxHeapMemAlloc(sizeof(DDRAWI_DIRECTDRAW_INT
));
58 DX_STUB_str("DDERR_OUTOFMEMORY");
59 return DDERR_OUTOFMEMORY
;
62 /* we need check the GUID lpGUID what type it is */
63 if (pGUID
!= (LPGUID
)DDCREATE_HARDWAREONLY
)
68 DX_STUB_str("DDERR_INVALIDDIRECTDRAWGUID");
69 return DDERR_INVALIDDIRECTDRAWGUID
;
72 newThis
->lpLink
= This
;
76 /* Fixme release memory alloc if we fail */
77 This
->lpLcl
= DxHeapMemAlloc(sizeof(DDRAWI_DIRECTDRAW_INT
));
78 if (This
->lpLcl
== NULL
)
80 DX_STUB_str("DDERR_OUTOFMEMORY");
81 return DDERR_OUTOFMEMORY
;
83 This
->lpLcl
->lpGbl
= &ddgbl
;
85 *pIface
= (LPDIRECTDRAW
)This
;
87 /* Get right interface we whant */
88 if (Main_DirectDraw_QueryInterface((LPDIRECTDRAW7
)This
, id
, (void**)&pIface
) == DD_OK
)
90 DX_STUB_str("Got iface");
92 if (StartDirectDraw((LPDIRECTDRAW
*)This
, pGUID
, FALSE
) == DD_OK
);
97 RtlZeroMemory(&wnd_class, sizeof(wnd_class));
98 wnd_class.style = CS_HREDRAW | CS_VREDRAW;
99 wnd_class.lpfnWndProc = DefWindowProcW;
100 wnd_class.cbClsExtra = 0;
101 wnd_class.cbWndExtra = 0;
102 wnd_class.hInstance = GetModuleHandleW(0);
104 wnd_class.hCursor = 0;
105 wnd_class.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
106 wnd_class.lpszMenuName = NULL;
107 wnd_class.lpszClassName = classname;
108 if(!RegisterClassW(&wnd_class))
110 DX_STUB_str("DDERR_GENERIC");
111 return DDERR_GENERIC;
115 DX_STUB_str("DD_OK");
120 DX_STUB_str("DDERR_INVALIDPARAMS");
121 return DDERR_INVALIDPARAMS
;
126 StartDirectDraw(LPDIRECTDRAW
* iface
, LPGUID lpGuid
, BOOL reenable
)
128 LPDDRAWI_DIRECTDRAW_INT This
= (LPDDRAWI_DIRECTDRAW_INT
)iface
;
129 DWORD hal_ret
= DD_FALSE
;
130 DWORD hel_ret
= DD_FALSE
;
131 DWORD devicetypes
= 0;
138 * ddgbl.dwPDevice is not longer in use in windows 2000 and higher
139 * I am using it for device type
140 * devicetypes = 1 : both hal and hel are enable
141 * devicetypes = 2 : both hal are enable
142 * devicetypes = 3 : both hel are enable
143 * devicetypes = 4 :loading a guid drv from the register
148 if (reenable
== FALSE
)
150 if (This
->lpLink
== NULL
)
152 RtlZeroMemory(&ddgbl
, sizeof(DDRAWI_DIRECTDRAW_GBL
));
153 This
->lpLcl
->lpGbl
->dwRefCnt
++;
154 if (ddgbl
.lpDDCBtmp
== NULL
)
156 ddgbl
.lpDDCBtmp
= (LPDDHAL_CALLBACKS
) DxHeapMemAlloc(sizeof(DDHAL_CALLBACKS
));
157 if (ddgbl
.lpDDCBtmp
== NULL
)
159 DX_STUB_str("Out of memmory");
168 if (reenable
== FALSE
)
174 /* Create HDC for default, hal and hel driver */
175 This
->lpLcl
->hWnd
= (ULONG_PTR
) GetActiveWindow();
176 This
->lpLcl
->hDC
= (ULONG_PTR
) GetDC((HWND
)This
->lpLcl
->hWnd
);
178 /* cObsolete is undoc in msdn it being use in CreateDCA */
179 RtlCopyMemory(&ddgbl
.cObsolete
,&"DISPLAY",7);
180 RtlCopyMemory(&ddgbl
.cDriverName
,&"DISPLAY",7);
181 dwFlags
|= DDRAWI_DISPLAYDRV
| DDRAWI_GDIDRV
;
183 else if (lpGuid
== (LPGUID
) DDCREATE_HARDWAREONLY
)
186 /* Create HDC for default, hal driver */
187 This
->lpLcl
->hWnd
=(ULONG_PTR
) GetActiveWindow();
188 This
->lpLcl
->hDC
= (ULONG_PTR
) GetDC((HWND
)This
->lpLcl
->hWnd
);
190 /* cObsolete is undoc in msdn it being use in CreateDCA */
191 RtlCopyMemory(&ddgbl
.cObsolete
,&"DISPLAY",7);
192 RtlCopyMemory(&ddgbl
.cDriverName
,&"DISPLAY",7);
193 dwFlags
|= DDRAWI_DISPLAYDRV
| DDRAWI_GDIDRV
;
195 else if (lpGuid
== (LPGUID
) DDCREATE_EMULATIONONLY
)
199 /* Create HDC for default, hal and hel driver */
200 This
->lpLcl
->hWnd
= (ULONG_PTR
) GetActiveWindow();
201 This
->lpLcl
->hDC
= (ULONG_PTR
) GetDC((HWND
)This
->lpLcl
->hWnd
);
203 /* cObsolete is undoc in msdn it being use in CreateDCA */
204 RtlCopyMemory(&ddgbl
.cObsolete
,&"DISPLAY",7);
205 RtlCopyMemory(&ddgbl
.cDriverName
,&"DISPLAY",7);
207 dwFlags
|= DDRAWI_DISPLAYDRV
| DDRAWI_GDIDRV
;
211 /* FIXME : need getting driver from the GUID that have been pass in from
212 * the register. we do not support that yet
215 This
->lpLcl
->hDC
= (ULONG_PTR
) NULL
;
216 This
->lpLcl
->hWnd
= (ULONG_PTR
) GetActiveWindow();
219 if ( (HDC
)This
->lpLcl
->hDC
== NULL
)
221 DX_STUB_str("DDERR_OUTOFMEMORY");
222 return DDERR_OUTOFMEMORY
;
226 This
->lpLcl
->lpDDCB
= ddgbl
.lpDDCBtmp
;
228 /* Startup HEL and HAL */
229 This
->lpLcl
->lpDDCB
= This
->lpLcl
->lpGbl
->lpDDCBtmp
;
230 This
->lpLcl
->dwProcessId
= GetCurrentProcessId();
235 hal_ret
= StartDirectDrawHal(iface
, reenable
);
236 This
->lpLcl
->lpDDCB
->HELDD
.dwFlags
= 0;
240 hel_ret
= StartDirectDrawHel(iface
, reenable
);
241 This
->lpLcl
->lpDDCB
->HALDD
.dwFlags
= 0;
245 hal_ret
= StartDirectDrawHal(iface
, reenable
);
246 hel_ret
= StartDirectDrawHel(iface
, reenable
);
253 DX_STUB_str("DDERR_NODIRECTDRAWSUPPORT");
254 return DDERR_NODIRECTDRAWSUPPORT
;
256 dwFlags
|= DDRAWI_NOHARDWARE
;
261 dwFlags
|= DDRAWI_NOEMULATION
;
266 dwFlags
|= DDRAWI_EMULATIONINITIALIZED
;
269 /* Fill some basic info for Surface */
270 This
->lpLcl
->lpGbl
->dwFlags
= dwFlags
| DDRAWI_ATTACHEDTODESKTOP
;
271 This
->lpLcl
->lpDDCB
= This
->lpLcl
->lpGbl
->lpDDCBtmp
;
272 This
->lpLcl
->hDD
= This
->lpLcl
->lpGbl
->hDD
;
273 ddgbl
.hDD
= This
->lpLcl
->lpGbl
->hDD
;
275 DX_STUB_str("DD_OK");
280 StartDirectDrawHel(LPDIRECTDRAW
* iface
, BOOL reenable
)
282 LPDDRAWI_DIRECTDRAW_INT This
= (LPDDRAWI_DIRECTDRAW_INT
)iface
;
284 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.CanCreateSurface
= HelDdCanCreateSurface
;
285 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.CreateSurface
= HelDdCreateSurface
;
286 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.CreatePalette
= HelDdCreatePalette
;
287 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.DestroyDriver
= HelDdDestroyDriver
;
288 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.FlipToGDISurface
= HelDdFlipToGDISurface
;
289 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.GetScanLine
= HelDdGetScanLine
;
290 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.SetColorKey
= HelDdSetColorKey
;
291 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.SetExclusiveMode
= HelDdSetExclusiveMode
;
292 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.SetMode
= HelDdSetMode
;
293 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.WaitForVerticalBlank
= HelDdWaitForVerticalBlank
;
295 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.dwFlags
= DDHAL_CB32_CANCREATESURFACE
|
296 DDHAL_CB32_CREATESURFACE
|
297 DDHAL_CB32_CREATEPALETTE
|
298 DDHAL_CB32_DESTROYDRIVER
|
299 DDHAL_CB32_FLIPTOGDISURFACE
|
300 DDHAL_CB32_GETSCANLINE
|
301 DDHAL_CB32_SETCOLORKEY
|
302 DDHAL_CB32_SETEXCLUSIVEMODE
|
304 DDHAL_CB32_WAITFORVERTICALBLANK
;
306 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.dwSize
= sizeof(This
->lpLcl
->lpDDCB
->HELDD
);
308 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.AddAttachedSurface
= HelDdSurfAddAttachedSurface
;
309 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Blt
= HelDdSurfBlt
;
310 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.DestroySurface
= HelDdSurfDestroySurface
;
311 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Flip
= HelDdSurfFlip
;
312 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.GetBltStatus
= HelDdSurfGetBltStatus
;
313 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.GetFlipStatus
= HelDdSurfGetFlipStatus
;
314 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Lock
= HelDdSurfLock
;
315 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.reserved4
= HelDdSurfreserved4
;
316 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetClipList
= HelDdSurfSetClipList
;
317 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetColorKey
= HelDdSurfSetColorKey
;
318 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetOverlayPosition
= HelDdSurfSetOverlayPosition
;
319 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetPalette
= HelDdSurfSetPalette
;
320 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Unlock
= HelDdSurfUnlock
;
321 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.UpdateOverlay
= HelDdSurfUpdateOverlay
;
322 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.dwFlags
= DDHAL_SURFCB32_ADDATTACHEDSURFACE
|
324 DDHAL_SURFCB32_DESTROYSURFACE
|
325 DDHAL_SURFCB32_FLIP
|
326 DDHAL_SURFCB32_GETBLTSTATUS
|
327 DDHAL_SURFCB32_GETFLIPSTATUS
|
328 DDHAL_SURFCB32_LOCK
|
329 DDHAL_SURFCB32_RESERVED4
|
330 DDHAL_SURFCB32_SETCLIPLIST
|
331 DDHAL_SURFCB32_SETCOLORKEY
|
332 DDHAL_SURFCB32_SETOVERLAYPOSITION
|
333 DDHAL_SURFCB32_SETPALETTE
|
334 DDHAL_SURFCB32_UNLOCK
|
335 DDHAL_SURFCB32_UPDATEOVERLAY
;
337 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.dwSize
= sizeof(This
->lpLcl
->lpDDCB
->HELDDSurface
);
340 This->lpLcl->lpDDCB->HELDDPalette.DestroyPalette = HelDdPalDestroyPalette;
341 This->lpLcl->lpDDCB->HELDDPalette.SetEntries = HelDdPalSetEntries;
342 This->lpLcl->lpDDCB->HELDDPalette.dwSize = sizeof(This->lpLcl->lpDDCB->HELDDPalette);
346 This->lpLcl->lpDDCB->HELDDExeBuf.CanCreateExecuteBuffer = HelDdExeCanCreateExecuteBuffer;
347 This->lpLcl->lpDDCB->HELDDExeBuf.CreateExecuteBuffer = HelDdExeCreateExecuteBuffer;
348 This->lpLcl->lpDDCB->HELDDExeBuf.DestroyExecuteBuffer = HelDdExeDestroyExecuteBuffer;
349 This->lpLcl->lpDDCB->HELDDExeBuf.LockExecuteBuffer = HelDdExeLockExecuteBuffer;
350 This->lpLcl->lpDDCB->HELDDExeBuf.UnlockExecuteBuffer = HelDdExeUnlockExecuteBuffer;
358 StartDirectDrawHal(LPDIRECTDRAW
* iface
, BOOL reenable
)
364 BOOL newmode
= FALSE
;
365 LPDDSURFACEDESC mpTextures
;
366 D3DHAL_CALLBACKS mD3dCallbacks
;
367 D3DHAL_GLOBALDRIVERDATA mD3dDriverData
;
368 DDHAL_DDEXEBUFCALLBACKS mD3dBufferCallbacks
;
369 LPDDRAWI_DIRECTDRAW_INT This
= (LPDDRAWI_DIRECTDRAW_INT
)iface
;
371 RtlZeroMemory(&mHALInfo
, sizeof(DDHALINFO
));
372 RtlZeroMemory(&mD3dCallbacks
, sizeof(D3DHAL_CALLBACKS
));
373 RtlZeroMemory(&mD3dDriverData
, sizeof(D3DHAL_GLOBALDRIVERDATA
));
374 RtlZeroMemory(&mD3dBufferCallbacks
, sizeof(DDHAL_DDEXEBUFCALLBACKS
));
377 if (reenable
== FALSE
)
379 ddgbl
.lpDDCBtmp
= DxHeapMemAlloc(sizeof(DDHAL_CALLBACKS
));
380 if ( ddgbl
.lpDDCBtmp
== NULL
)
387 RtlZeroMemory(ddgbl
.lpDDCBtmp
,sizeof(DDHAL_CALLBACKS
));
391 * Startup DX HAL step one of three
393 if (!DdCreateDirectDrawObject(This
->lpLcl
->lpGbl
, (HDC
)This
->lpLcl
->hDC
))
395 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
401 /* Some card disable the dx after it have been created so
402 * we are force reanble it
404 if (!DdReenableDirectDrawObject(This
->lpLcl
->lpGbl
, &newmode
))
406 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
410 if (!DdQueryDirectDrawObject(This
->lpLcl
->lpGbl
,
412 &ddgbl
.lpDDCBtmp
->HALDD
,
413 &ddgbl
.lpDDCBtmp
->HALDDSurface
,
414 &ddgbl
.lpDDCBtmp
->HALDDPalette
,
417 &mD3dBufferCallbacks
,
422 DxHeapMemFree(This
->lpLcl
->lpGbl
->lpModeInfo
);
423 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
424 // FIXME Close DX fristcall and second call
430 if (mHALInfo
.ddCaps
.dwNumFourCCCodes
)
432 mpFourCC
= (DWORD
*) DxHeapMemAlloc(sizeof(DWORD
) * mHALInfo
.ddCaps
.dwNumFourCCCodes
);
433 if (mpFourCC
== NULL
)
435 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
436 // FIXME Close DX fristcall and second call
441 /* Alloc mpTextures */
444 if (mD3dDriverData
.dwNumTextureFormats
)
446 mpTextures
= (DDSURFACEDESC
*) DxHeapMemAlloc(sizeof(DDSURFACEDESC
) * mD3dDriverData
.dwNumTextureFormats
);
447 if (mpTextures
== NULL
)
449 DxHeapMemFree( mpFourCC
);
450 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
451 // FIXME Close DX fristcall and second call
457 /* Get all basic data from the driver */
458 if (!DdQueryDirectDrawObject(
461 &ddgbl
.lpDDCBtmp
->HALDD
,
462 &ddgbl
.lpDDCBtmp
->HALDDSurface
,
463 &ddgbl
.lpDDCBtmp
->HALDDPalette
,
466 &ddgbl
.lpDDCBtmp
->HALDDExeBuf
,
467 (DDSURFACEDESC
*)mpTextures
,
471 DxHeapMemFree( mpFourCC
);
472 DxHeapMemFree( mpTextures
);
473 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
474 // FIXME Close DX fristcall and second call
478 memcpy(&ddgbl
.vmiData
, &mHALInfo
.vmiData
,sizeof(VIDMEMINFO
));
479 memcpy(&ddgbl
.ddCaps
, &mHALInfo
.ddCaps
,sizeof(DDCORECAPS
));
481 This
->lpLcl
->lpGbl
->dwNumFourCC
= mHALInfo
.ddCaps
.dwNumFourCCCodes
;
482 This
->lpLcl
->lpGbl
->lpdwFourCC
= mpFourCC
;
484 This
->lpLcl
->lpGbl
->dwMonitorFrequency
= mHALInfo
.dwMonitorFrequency
;
485 This
->lpLcl
->lpGbl
->dwModeIndex
= mHALInfo
.dwModeIndex
;
486 This
->lpLcl
->lpGbl
->dwNumModes
= mHALInfo
.dwNumModes
;
487 This
->lpLcl
->lpGbl
->lpModeInfo
= mHALInfo
.lpModeInfo
;
488 This
->lpLcl
->lpGbl
->hInstance
= mHALInfo
.hInstance
;
489 This
->lpLcl
->lpGbl
->lp16DD
= This
->lpLcl
->lpGbl
;
491 /* FIXME convert mpTextures to DDHALMODEINFO */
492 DxHeapMemFree( mpTextures
);
494 /* FIXME D3D setup mD3dCallbacks and mD3dDriverData */