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 DX_STUB_str("here\n");
37 This
= (LPDDRAWI_DIRECTDRAW_INT
)*pIface
;
39 /* fixme linking too second link when we shall not doing it */
40 if (IsBadReadPtr(This
,sizeof(LPDIRECTDRAW
)))
42 DX_STUB_str("1. no linking\n");
43 /* We do not have a DirectDraw interface, we need alloc it*/
44 LPDDRAWI_DIRECTDRAW_INT memThis
;
46 DX_STUB_str("here\n");
48 DxHeapMemAlloc(memThis
, sizeof(DDRAWI_DIRECTDRAW_INT
));
54 DX_STUB_str("DDERR_OUTOFMEMORY\n");
55 return DDERR_OUTOFMEMORY
;
61 DX_STUB_str("here\n");
63 /* Fixme release memory alloc if we fail */
64 DxHeapMemAlloc(This
->lpLcl
, sizeof(DDRAWI_DIRECTDRAW_INT
));
65 if (This
->lpLcl
== NULL
)
67 DX_STUB_str("DDERR_OUTOFMEMORY\n");
68 return DDERR_OUTOFMEMORY
;
71 DX_STUB_str("here\n");
75 DX_STUB_str("2.linking\n");
76 /* We got the DirectDraw interface alloc and we need create the link */
77 LPDDRAWI_DIRECTDRAW_INT newThis
;
79 DX_STUB_str("here\n");
81 /* step 1.Alloc the new DDRAWI_DIRECTDRAW_INT for the lnking */
82 DxHeapMemAlloc(newThis
, sizeof(DDRAWI_DIRECTDRAW_INT
));
85 DX_STUB_str("DDERR_OUTOFMEMORY\n");
86 return DDERR_OUTOFMEMORY
;
89 DX_STUB_str("here\n");
91 /* step 2 check if it not DDCREATE_HARDWAREONLY we got if so we fail */
92 if ((pGUID
) && (pGUID
!= (LPGUID
)DDCREATE_HARDWAREONLY
))
97 DX_STUB_str("DDERR_INVALIDDIRECTDRAWGUID\n");
98 return DDERR_INVALIDDIRECTDRAWGUID
;
102 DX_STUB_str("here\n");
104 /* step 3 do the link the old interface are store in the new one */
105 newThis
->lpLink
= This
;
107 /* step 4 we need create new local directdraw struct for the new linked interface */
108 DxHeapMemAlloc(newThis
->lpLcl
, sizeof(DDRAWI_DIRECTDRAW_LCL
));
109 if (newThis
->lpLcl
== NULL
)
112 DX_STUB_str("DDERR_OUTOFMEMORY\n");
113 return DDERR_OUTOFMEMORY
;
116 DX_STUB_str("here\n");
122 This
->lpLcl
->lpGbl
= &ddgbl
;
124 *pIface
= (LPDIRECTDRAW
)This
;
126 /* Get right interface we whant */
127 if (Main_DirectDraw_QueryInterface((LPDIRECTDRAW7
)This
, id
, (void**)&pIface
) == DD_OK
)
129 DX_STUB_str("Got iface\n");
131 if (StartDirectDraw((LPDIRECTDRAW
)This
, pGUID
, FALSE
) == DD_OK
);
134 RtlZeroMemory(&wnd_class, sizeof(wnd_class));
135 wnd_class.style = CS_HREDRAW | CS_VREDRAW;
136 wnd_class.lpfnWndProc = DefWindowProcW;
137 wnd_class.cbClsExtra = 0;
138 wnd_class.cbWndExtra = 0;
139 wnd_class.hInstance = GetModuleHandleW(0);
141 wnd_class.hCursor = 0;
142 wnd_class.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
143 wnd_class.lpszMenuName = NULL;
144 wnd_class.lpszClassName = classname;
145 if(!RegisterClassW(&wnd_class))
147 DX_STUB_str("DDERR_GENERIC");
148 return DDERR_GENERIC;
152 DX_STUB_str("DD_OK\n");
157 DX_STUB_str("DDERR_INVALIDPARAMS\n");
158 return DDERR_INVALIDPARAMS
;
163 StartDirectDraw(LPDIRECTDRAW iface
, LPGUID lpGuid
, BOOL reenable
)
165 LPDDRAWI_DIRECTDRAW_INT This
= (LPDDRAWI_DIRECTDRAW_INT
)iface
;
166 DWORD hal_ret
= DD_FALSE
;
167 DWORD hel_ret
= DD_FALSE
;
168 DWORD devicetypes
= 0;
174 * ddgbl.dwPDevice is not longer in use in windows 2000 and higher
175 * I am using it for device type
176 * devicetypes = 1 : both hal and hel are enable
177 * devicetypes = 2 : both hal are enable
178 * devicetypes = 3 : both hel are enable
179 * devicetypes = 4 :loading a guid drv from the register
182 if (reenable
== FALSE
)
184 if ((!IsBadReadPtr(This
->lpLink
,sizeof(LPDIRECTDRAW
))) && (This
->lpLink
== NULL
))
186 RtlZeroMemory(&ddgbl
, sizeof(DDRAWI_DIRECTDRAW_GBL
));
187 This
->lpLcl
->lpGbl
->dwRefCnt
++;
188 if (ddgbl
.lpDDCBtmp
== NULL
)
191 DxHeapMemAlloc( ddgbl
.lpDDCBtmp
, sizeof(DDHAL_CALLBACKS
));
192 if (ddgbl
.lpDDCBtmp
== NULL
)
194 DX_STUB_str("Out of memmory\n");
201 DX_STUB_str("here\n");
203 if (reenable
== FALSE
)
207 DX_STUB_str("lpGuid == NULL\n");
210 /* Create HDC for default, hal and hel driver */
211 This
->lpLcl
->hWnd
= (ULONG_PTR
) GetActiveWindow();
212 This
->lpLcl
->hDC
= (ULONG_PTR
) GetDC((HWND
)This
->lpLcl
->hWnd
);
214 /* cObsolete is undoc in msdn it being use in CreateDCA */
215 RtlCopyMemory(&ddgbl
.cObsolete
,&"DISPLAY",7);
216 RtlCopyMemory(&ddgbl
.cDriverName
,&"DISPLAY",7);
217 dwFlags
|= DDRAWI_DISPLAYDRV
| DDRAWI_GDIDRV
;
221 else if (lpGuid
== (LPGUID
) DDCREATE_HARDWAREONLY
)
224 /* Create HDC for default, hal driver */
225 This
->lpLcl
->hWnd
=(ULONG_PTR
) GetActiveWindow();
226 This
->lpLcl
->hDC
= (ULONG_PTR
) GetDC((HWND
)This
->lpLcl
->hWnd
);
228 /* cObsolete is undoc in msdn it being use in CreateDCA */
229 RtlCopyMemory(&ddgbl
.cObsolete
,&"DISPLAY",7);
230 RtlCopyMemory(&ddgbl
.cDriverName
,&"DISPLAY",7);
231 dwFlags
|= DDRAWI_DISPLAYDRV
| DDRAWI_GDIDRV
;
233 else if (lpGuid
== (LPGUID
) DDCREATE_EMULATIONONLY
)
237 /* Create HDC for default, hal and hel driver */
238 This
->lpLcl
->hWnd
= (ULONG_PTR
) GetActiveWindow();
239 This
->lpLcl
->hDC
= (ULONG_PTR
) GetDC((HWND
)This
->lpLcl
->hWnd
);
241 /* cObsolete is undoc in msdn it being use in CreateDCA */
242 RtlCopyMemory(&ddgbl
.cObsolete
,&"DISPLAY",7);
243 RtlCopyMemory(&ddgbl
.cDriverName
,&"DISPLAY",7);
245 dwFlags
|= DDRAWI_DISPLAYDRV
| DDRAWI_GDIDRV
;
249 /* FIXME : need getting driver from the GUID that have been pass in from
250 * the register. we do not support that yet
253 This
->lpLcl
->hDC
= (ULONG_PTR
) NULL
;
254 This
->lpLcl
->hWnd
= (ULONG_PTR
) GetActiveWindow();
257 if ( (HDC
)This
->lpLcl
->hDC
== NULL
)
259 DX_STUB_str("DDERR_OUTOFMEMORY\n");
260 return DDERR_OUTOFMEMORY
;
264 This
->lpLcl
->lpDDCB
= ddgbl
.lpDDCBtmp
;
266 /* Startup HEL and HAL */
267 This
->lpLcl
->lpDDCB
= This
->lpLcl
->lpGbl
->lpDDCBtmp
;
268 This
->lpLcl
->dwProcessId
= GetCurrentProcessId();
273 hal_ret
= StartDirectDrawHal(iface
, reenable
);
274 This
->lpLcl
->lpDDCB
->HELDD
.dwFlags
= 0;
278 hel_ret
= StartDirectDrawHel(iface
, reenable
);
279 This
->lpLcl
->lpDDCB
->HALDD
.dwFlags
= 0;
283 hal_ret
= StartDirectDrawHal(iface
, reenable
);
284 hel_ret
= StartDirectDrawHel(iface
, reenable
);
287 DX_STUB_str("return\n");
293 DX_STUB_str("DDERR_NODIRECTDRAWSUPPORT\n");
294 return DDERR_NODIRECTDRAWSUPPORT
;
296 dwFlags
|= DDRAWI_NOHARDWARE
;
301 dwFlags
|= DDRAWI_NOEMULATION
;
306 dwFlags
|= DDRAWI_EMULATIONINITIALIZED
;
309 /* Fill some basic info for Surface */
310 This
->lpLcl
->lpGbl
->dwFlags
= dwFlags
| DDRAWI_ATTACHEDTODESKTOP
;
311 This
->lpLcl
->lpDDCB
= This
->lpLcl
->lpGbl
->lpDDCBtmp
;
312 This
->lpLcl
->hDD
= This
->lpLcl
->lpGbl
->hDD
;
313 ddgbl
.hDD
= This
->lpLcl
->lpGbl
->hDD
;
315 DX_STUB_str("DD_OK\n");
320 StartDirectDrawHel(LPDIRECTDRAW iface
, BOOL reenable
)
322 LPDDRAWI_DIRECTDRAW_INT This
= (LPDDRAWI_DIRECTDRAW_INT
)iface
;
324 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.CanCreateSurface
= HelDdCanCreateSurface
;
325 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.CreateSurface
= HelDdCreateSurface
;
326 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.CreatePalette
= HelDdCreatePalette
;
327 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.DestroyDriver
= HelDdDestroyDriver
;
328 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.FlipToGDISurface
= HelDdFlipToGDISurface
;
329 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.GetScanLine
= HelDdGetScanLine
;
330 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.SetColorKey
= HelDdSetColorKey
;
331 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.SetExclusiveMode
= HelDdSetExclusiveMode
;
332 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.SetMode
= HelDdSetMode
;
333 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.WaitForVerticalBlank
= HelDdWaitForVerticalBlank
;
335 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.dwFlags
= DDHAL_CB32_CANCREATESURFACE
|
336 DDHAL_CB32_CREATESURFACE
|
337 DDHAL_CB32_CREATEPALETTE
|
338 DDHAL_CB32_DESTROYDRIVER
|
339 DDHAL_CB32_FLIPTOGDISURFACE
|
340 DDHAL_CB32_GETSCANLINE
|
341 DDHAL_CB32_SETCOLORKEY
|
342 DDHAL_CB32_SETEXCLUSIVEMODE
|
344 DDHAL_CB32_WAITFORVERTICALBLANK
;
346 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDD
.dwSize
= sizeof(This
->lpLcl
->lpDDCB
->HELDD
);
348 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.AddAttachedSurface
= HelDdSurfAddAttachedSurface
;
349 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Blt
= HelDdSurfBlt
;
350 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.DestroySurface
= HelDdSurfDestroySurface
;
351 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Flip
= HelDdSurfFlip
;
352 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.GetBltStatus
= HelDdSurfGetBltStatus
;
353 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.GetFlipStatus
= HelDdSurfGetFlipStatus
;
354 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Lock
= HelDdSurfLock
;
355 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.reserved4
= HelDdSurfreserved4
;
356 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetClipList
= HelDdSurfSetClipList
;
357 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetColorKey
= HelDdSurfSetColorKey
;
358 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetOverlayPosition
= HelDdSurfSetOverlayPosition
;
359 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.SetPalette
= HelDdSurfSetPalette
;
360 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.Unlock
= HelDdSurfUnlock
;
361 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.UpdateOverlay
= HelDdSurfUpdateOverlay
;
362 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.dwFlags
= DDHAL_SURFCB32_ADDATTACHEDSURFACE
|
364 DDHAL_SURFCB32_DESTROYSURFACE
|
365 DDHAL_SURFCB32_FLIP
|
366 DDHAL_SURFCB32_GETBLTSTATUS
|
367 DDHAL_SURFCB32_GETFLIPSTATUS
|
368 DDHAL_SURFCB32_LOCK
|
369 DDHAL_SURFCB32_RESERVED4
|
370 DDHAL_SURFCB32_SETCLIPLIST
|
371 DDHAL_SURFCB32_SETCOLORKEY
|
372 DDHAL_SURFCB32_SETOVERLAYPOSITION
|
373 DDHAL_SURFCB32_SETPALETTE
|
374 DDHAL_SURFCB32_UNLOCK
|
375 DDHAL_SURFCB32_UPDATEOVERLAY
;
377 This
->lpLcl
->lpGbl
->lpDDCBtmp
->HELDDSurface
.dwSize
= sizeof(This
->lpLcl
->lpDDCB
->HELDDSurface
);
380 This->lpLcl->lpDDCB->HELDDPalette.DestroyPalette = HelDdPalDestroyPalette;
381 This->lpLcl->lpDDCB->HELDDPalette.SetEntries = HelDdPalSetEntries;
382 This->lpLcl->lpDDCB->HELDDPalette.dwSize = sizeof(This->lpLcl->lpDDCB->HELDDPalette);
386 This->lpLcl->lpDDCB->HELDDExeBuf.CanCreateExecuteBuffer = HelDdExeCanCreateExecuteBuffer;
387 This->lpLcl->lpDDCB->HELDDExeBuf.CreateExecuteBuffer = HelDdExeCreateExecuteBuffer;
388 This->lpLcl->lpDDCB->HELDDExeBuf.DestroyExecuteBuffer = HelDdExeDestroyExecuteBuffer;
389 This->lpLcl->lpDDCB->HELDDExeBuf.LockExecuteBuffer = HelDdExeLockExecuteBuffer;
390 This->lpLcl->lpDDCB->HELDDExeBuf.UnlockExecuteBuffer = HelDdExeUnlockExecuteBuffer;
398 StartDirectDrawHal(LPDIRECTDRAW iface
, BOOL reenable
)
400 LPDWORD mpFourCC
= NULL
;
402 BOOL newmode
= FALSE
;
403 LPDDSURFACEDESC mpTextures
;
404 D3DHAL_CALLBACKS mD3dCallbacks
;
405 D3DHAL_GLOBALDRIVERDATA mD3dDriverData
;
406 DDHAL_DDEXEBUFCALLBACKS mD3dBufferCallbacks
;
407 LPDDRAWI_DIRECTDRAW_INT This
= (LPDDRAWI_DIRECTDRAW_INT
)iface
;
411 RtlZeroMemory(&mHALInfo
, sizeof(DDHALINFO
));
412 RtlZeroMemory(&mD3dCallbacks
, sizeof(D3DHAL_CALLBACKS
));
413 RtlZeroMemory(&mD3dDriverData
, sizeof(D3DHAL_GLOBALDRIVERDATA
));
414 RtlZeroMemory(&mD3dBufferCallbacks
, sizeof(DDHAL_DDEXEBUFCALLBACKS
));
416 if (reenable
== FALSE
)
418 if (ddgbl
.lpDDCBtmp
== NULL
)
420 DxHeapMemAlloc(ddgbl
.lpDDCBtmp
, sizeof(DDHAL_CALLBACKS
));
421 if ( ddgbl
.lpDDCBtmp
== NULL
)
429 RtlZeroMemory(ddgbl
.lpDDCBtmp
,sizeof(DDHAL_CALLBACKS
));
433 * Startup DX HAL step one of three
435 if (!DdCreateDirectDrawObject(This
->lpLcl
->lpGbl
, (HDC
)This
->lpLcl
->hDC
))
437 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
441 /* Some card disable the dx after it have been created so
442 * we are force reanble it
444 if (!DdReenableDirectDrawObject(This
->lpLcl
->lpGbl
, &newmode
))
446 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
450 if (!DdQueryDirectDrawObject(This
->lpLcl
->lpGbl
,
452 &ddgbl
.lpDDCBtmp
->HALDD
,
453 &ddgbl
.lpDDCBtmp
->HALDDSurface
,
454 &ddgbl
.lpDDCBtmp
->HALDDPalette
,
457 &mD3dBufferCallbacks
,
462 DxHeapMemFree(This
->lpLcl
->lpGbl
->lpModeInfo
);
463 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
464 // FIXME Close DX fristcall and second call
469 DX_STUB_str("Trying alloc FourCCC \n");
472 if (This
->lpLcl
->lpGbl
->lpdwFourCC
!= NULL
)
474 DxHeapMemFree(This
->lpLcl
->lpGbl
->lpdwFourCC
);
477 if (mHALInfo
.ddCaps
.dwNumFourCCCodes
> 0 )
479 DxHeapMemAlloc(mpFourCC
, sizeof(DWORD
) * 21);
481 mpFourCC
= (DWORD
*) DxHeapMemAlloc(sizeof(DWORD
) * (mHALInfo
.ddCaps
.dwNumFourCCCodes
+ 2));
483 if (mpFourCC
== NULL
)
485 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
486 // FIXME Close DX fristcall and second call
491 DX_STUB_str("End Trying alloc FourCCC\n");
497 /* Alloc mpTextures */
499 DX_STUB_str("1 Here\n");
501 if (This
->lpLcl
->lpGbl
->texture
!= NULL
)
503 DxHeapMemFree(This
->lpLcl
->lpGbl
->texture
;
507 if (mD3dDriverData
.dwNumTextureFormats
> 0)
509 mpTextures
= (DDSURFACEDESC
*) DxHeapMemAlloc(sizeof(DDSURFACEDESC
) * mD3dDriverData
.dwNumTextureFormats
);
510 if (mpTextures
== NULL
)
512 DxHeapMemFree(mpFourCC
);
513 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
514 // FIXME Close DX fristcall and second call
518 DX_STUB_str("2 Here\n");
525 /* Get all basic data from the driver */
526 if (!DdQueryDirectDrawObject(
529 &ddgbl
.lpDDCBtmp
->HALDD
,
530 &ddgbl
.lpDDCBtmp
->HALDDSurface
,
531 &ddgbl
.lpDDCBtmp
->HALDDPalette
,
534 &ddgbl
.lpDDCBtmp
->HALDDExeBuf
,
535 (DDSURFACEDESC
*)mpTextures
,
539 DxHeapMemFree(mpFourCC
);
540 DxHeapMemFree(mpTextures
);
541 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
542 // FIXME Close DX fristcall and second call
546 memcpy(&ddgbl
.vmiData
, &mHALInfo
.vmiData
,sizeof(VIDMEMINFO
));
549 memcpy(&ddgbl
.ddCaps
, &mHALInfo
.ddCaps
,sizeof(DDCORECAPS
));
551 This
->lpLcl
->lpGbl
->dwNumFourCC
= mHALInfo
.ddCaps
.dwNumFourCCCodes
;
552 This
->lpLcl
->lpGbl
->lpdwFourCC
= mpFourCC
;
553 This
->lpLcl
->lpGbl
->dwMonitorFrequency
= mHALInfo
.dwMonitorFrequency
;
554 This
->lpLcl
->lpGbl
->dwModeIndex
= mHALInfo
.dwModeIndex
;
555 This
->lpLcl
->lpGbl
->dwNumModes
= mHALInfo
.dwNumModes
;
556 This
->lpLcl
->lpGbl
->lpModeInfo
= mHALInfo
.lpModeInfo
;
558 DX_STUB_str("Here\n");
560 /* FIXME convert mpTextures to DDHALMODEINFO */
561 // DxHeapMemFree( mpTextures);
563 /* FIXME D3D setup mD3dCallbacks and mD3dDriverData */
564 DDHAL_GETDRIVERINFODATA DdGetDriverInfo
= { 0 };
565 DdGetDriverInfo
.dwSize
= sizeof (DDHAL_GETDRIVERINFODATA
);
566 DdGetDriverInfo
.guidInfo
= GUID_MiscellaneousCallbacks
;
568 DdGetDriverInfo
.lpvData
= (PVOID
)&ddgbl
.lpDDCBtmp
->HALDDMiscellaneous
;
570 DdGetDriverInfo
.dwExpectedSize
= sizeof (DDHAL_DDMISCELLANEOUSCALLBACKS
);
572 if(mHALInfo
.GetDriverInfo (&DdGetDriverInfo
) == DDHAL_DRIVER_NOTHANDLED
|| DdGetDriverInfo
.ddRVal
!= DD_OK
)
574 DxHeapMemFree(mpFourCC
);
575 DxHeapMemFree(mpTextures
);
576 DxHeapMemFree(ddgbl
.lpDDCBtmp
);
577 // FIXME Close DX fristcall and second call