1 /* DirectDraw Base Functions
3 * Copyright 1997-1999 Marcus Meissner
4 * Copyright 1998 Lionel Ulmer (most of Direct3D stuff)
5 * Copyright 2000-2001 TransGaming Technologies Inc.
7 * This file contains the (internal) driver registration functions,
8 * driver enumeration APIs and DirectDraw creation functions.
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 /* This for all the enumeration and creation of D3D-related objects */
38 #include "ddraw_private.h"
39 #include "wine/debug.h"
41 #define MAX_DDRAW_DRIVERS 3
42 static const ddraw_driver
* DDRAW_drivers
[MAX_DDRAW_DRIVERS
];
43 static int DDRAW_num_drivers
; /* = 0 */
44 static int DDRAW_default_driver
;
46 WINE_DEFAULT_DEBUG_CHANNEL(ddraw
);
48 /**********************************************************************/
53 } DirectDrawEnumerateProcData
;
55 /***********************************************************************
56 * DirectDrawEnumerateExA (DDRAW.@)
58 HRESULT WINAPI
DirectDrawEnumerateExA(
59 LPDDENUMCALLBACKEXA lpCallback
, LPVOID lpContext
, DWORD dwFlags
)
62 TRACE("(%p,%p, %08lx)\n", lpCallback
, lpContext
, dwFlags
);
64 if (TRACE_ON(ddraw
)) {
66 if (dwFlags
& DDENUM_ATTACHEDSECONDARYDEVICES
)
67 DPRINTF("DDENUM_ATTACHEDSECONDARYDEVICES ");
68 if (dwFlags
& DDENUM_DETACHEDSECONDARYDEVICES
)
69 DPRINTF("DDENUM_DETACHEDSECONDARYDEVICES ");
70 if (dwFlags
& DDENUM_NONDISPLAYDEVICES
)
71 DPRINTF("DDENUM_NONDISPLAYDEVICES ");
75 for (i
=0; i
<DDRAW_num_drivers
; i
++)
77 TRACE("Enumerating %s/%s interface\n",
78 DDRAW_drivers
[i
]->info
->szDriver
,
79 DDRAW_drivers
[i
]->info
->szDescription
);
81 /* We have to pass NULL from the primary display device.
82 * RoadRage chapter 6's enumeration routine expects it. */
83 if (!lpCallback((DDRAW_default_driver
== i
) ? NULL
84 :(LPGUID
)&DDRAW_drivers
[i
]->info
->guidDeviceIdentifier
,
85 (LPSTR
)DDRAW_drivers
[i
]->info
->szDescription
,
86 (LPSTR
)DDRAW_drivers
[i
]->info
->szDriver
,
91 /* Unsupported flags */
92 if (dwFlags
& DDENUM_NONDISPLAYDEVICES
) {
93 FIXME("no non-display devices supported.\n");
95 if (dwFlags
& DDENUM_DETACHEDSECONDARYDEVICES
) {
96 FIXME("no detached secondary devices supported.\n");
102 /***********************************************************************
103 * DirectDrawEnumerateExW (DDRAW.@)
106 static BOOL CALLBACK
DirectDrawEnumerateExProcW(
107 GUID
*lpGUID
, LPSTR lpDriverDescription
, LPSTR lpDriverName
,
108 LPVOID lpContext
, HMONITOR hm
)
112 LPWSTR lpDriverDescriptionW
, lpDriverNameW
;
113 DirectDrawEnumerateProcData
*pEPD
= (DirectDrawEnumerateProcData
*)lpContext
;
115 len
= MultiByteToWideChar( CP_ACP
, 0, lpDriverDescription
, -1, NULL
, 0 );
116 lpDriverDescriptionW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
117 MultiByteToWideChar( CP_ACP
, 0, lpDriverDescription
, -1, lpDriverDescriptionW
, len
);
119 len
= MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, NULL
, 0 );
120 lpDriverNameW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
121 MultiByteToWideChar( CP_ACP
, 0, lpDriverName
, -1, lpDriverNameW
, len
);
123 bResult
= (*(LPDDENUMCALLBACKEXW
*) pEPD
->lpCallback
)(lpGUID
, lpDriverDescriptionW
,
124 lpDriverNameW
, pEPD
->lpContext
, hm
);
126 HeapFree(GetProcessHeap(), 0, lpDriverDescriptionW
);
127 HeapFree(GetProcessHeap(), 0, lpDriverNameW
);
131 HRESULT WINAPI
DirectDrawEnumerateExW(
132 LPDDENUMCALLBACKEXW lpCallback
, LPVOID lpContext
, DWORD dwFlags
)
134 DirectDrawEnumerateProcData epd
;
135 epd
.lpCallback
= (LPVOID
) lpCallback
;
136 epd
.lpContext
= lpContext
;
138 return DirectDrawEnumerateExA(DirectDrawEnumerateExProcW
, (LPVOID
) &epd
, 0);
141 /***********************************************************************
142 * DirectDrawEnumerateA (DDRAW.@)
145 static BOOL CALLBACK
DirectDrawEnumerateProcA(
146 GUID
*lpGUID
, LPSTR lpDriverDescription
, LPSTR lpDriverName
,
147 LPVOID lpContext
, HMONITOR hm
)
149 DirectDrawEnumerateProcData
*pEPD
= (DirectDrawEnumerateProcData
*)lpContext
;
151 return ((LPDDENUMCALLBACKA
) pEPD
->lpCallback
)(
152 lpGUID
, lpDriverDescription
, lpDriverName
, pEPD
->lpContext
);
155 HRESULT WINAPI
DirectDrawEnumerateA(
156 LPDDENUMCALLBACKA lpCallback
, LPVOID lpContext
)
158 DirectDrawEnumerateProcData epd
;
159 epd
.lpCallback
= (LPVOID
) lpCallback
;
160 epd
.lpContext
= lpContext
;
162 return DirectDrawEnumerateExA(DirectDrawEnumerateProcA
, (LPVOID
) &epd
, 0);
165 /***********************************************************************
166 * DirectDrawEnumerateW (DDRAW.@)
169 static BOOL WINAPI
DirectDrawEnumerateProcW(
170 GUID
*lpGUID
, LPWSTR lpDriverDescription
, LPWSTR lpDriverName
,
171 LPVOID lpContext
, HMONITOR hm
)
173 DirectDrawEnumerateProcData
*pEPD
= (DirectDrawEnumerateProcData
*)lpContext
;
175 return ((LPDDENUMCALLBACKW
) pEPD
->lpCallback
)(
176 lpGUID
, lpDriverDescription
, lpDriverName
, pEPD
->lpContext
);
179 HRESULT WINAPI
DirectDrawEnumerateW(
180 LPDDENUMCALLBACKW lpCallback
, LPVOID lpContext
)
182 DirectDrawEnumerateProcData epd
;
183 epd
.lpCallback
= (LPVOID
) lpCallback
;
184 epd
.lpContext
= lpContext
;
186 return DirectDrawEnumerateExW(DirectDrawEnumerateProcW
, (LPVOID
) &epd
, 0);
189 /***********************************************************************
190 * DirectDrawCreate (DDRAW.@)
193 const ddraw_driver
* DDRAW_FindDriver(const GUID
* pGUID
)
195 static const GUID zeroGUID
; /* gets zero-inited */
197 TRACE("(%s)\n", pGUID
? debugstr_guid(pGUID
) : "(null)");
199 if (DDRAW_num_drivers
== 0) return NULL
;
201 if (pGUID
== (LPGUID
)DDCREATE_EMULATIONONLY
202 || pGUID
== (LPGUID
)DDCREATE_HARDWAREONLY
)
205 if (pGUID
== NULL
|| memcmp(pGUID
, &zeroGUID
, sizeof(GUID
)) == 0)
207 /* Use the default driver. */
208 return DDRAW_drivers
[DDRAW_default_driver
];
212 /* Look for a matching GUID. */
215 for (i
=0; i
< DDRAW_num_drivers
; i
++)
217 if (IsEqualGUID(pGUID
,
218 &DDRAW_drivers
[i
]->info
->guidDeviceIdentifier
))
222 if (i
< DDRAW_num_drivers
)
224 return DDRAW_drivers
[i
];
228 ERR("(%s): did not recognize requested GUID.\n",debugstr_guid(pGUID
));
234 static HRESULT
DDRAW_Create(
235 LPGUID lpGUID
, LPVOID
*lplpDD
, LPUNKNOWN pUnkOuter
, REFIID iid
, BOOL ex
237 const ddraw_driver
* driver
;
241 if (DDRAW_num_drivers
== 0)
243 WARN("no DirectDraw drivers registered\n");
244 return DDERR_INVALIDDIRECTDRAWGUID
;
247 if (lpGUID
== (LPGUID
)DDCREATE_EMULATIONONLY
248 || lpGUID
== (LPGUID
)DDCREATE_HARDWAREONLY
)
251 TRACE("(%s,%p,%p)\n",debugstr_guid(lpGUID
),lplpDD
,pUnkOuter
);
253 if (pUnkOuter
!= NULL
)
254 return DDERR_INVALIDPARAMS
; /* CLASS_E_NOAGGREGATION? */
256 driver
= DDRAW_FindDriver(lpGUID
);
257 if (driver
== NULL
) return DDERR_INVALIDDIRECTDRAWGUID
;
259 hr
= driver
->create(lpGUID
, &pDD
, pUnkOuter
, ex
);
260 if (FAILED(hr
)) return hr
;
262 hr
= IDirectDraw7_QueryInterface(pDD
, iid
, lplpDD
);
263 IDirectDraw7_Release(pDD
);
267 /***********************************************************************
268 * DirectDrawCreate (DDRAW.@)
270 * Only creates legacy IDirectDraw interfaces.
271 * Cannot create IDirectDraw7 interfaces.
274 HRESULT WINAPI
DirectDrawCreate(
275 LPGUID lpGUID
, LPDIRECTDRAW
* lplpDD
, LPUNKNOWN pUnkOuter
277 return DDRAW_Create(lpGUID
,(LPVOID
*)lplpDD
,pUnkOuter
,&IID_IDirectDraw
,FALSE
);
280 /***********************************************************************
281 * DirectDrawCreateEx (DDRAW.@)
283 * Only creates new IDirectDraw7 interfaces.
284 * Supposed to fail if legacy interfaces are requested.
287 HRESULT WINAPI
DirectDrawCreateEx(
288 LPGUID lpGUID
, LPVOID
* lplpDD
, REFIID iid
, LPUNKNOWN pUnkOuter
290 if (!IsEqualGUID(iid
, &IID_IDirectDraw7
))
291 return DDERR_INVALIDPARAMS
;
293 return DDRAW_Create(lpGUID
, lplpDD
, pUnkOuter
, iid
, TRUE
);
296 extern HRESULT
Uninit_DirectDraw_Create(const GUID
*, LPDIRECTDRAW7
*,
299 /* This is for the class factory. */
300 static HRESULT
DDRAW_CreateDirectDraw(IUnknown
* pUnkOuter
, REFIID iid
,
306 hr
= Uninit_DirectDraw_Create(NULL
, &pDD
, pUnkOuter
, TRUE
); /* ex? */
307 if (FAILED(hr
)) return hr
;
309 hr
= IDirectDraw7_QueryInterface(pDD
, iid
, ppObj
);
310 IDirectDraw_Release(pDD
);
314 /******************************************************************************
315 * DirectDraw ClassFactory
318 ICOM_VFIELD_MULTI(IClassFactory
);
321 HRESULT (*pfnCreateInstance
)(IUnknown
*pUnkOuter
, REFIID iid
,
325 struct object_creation_info
328 HRESULT (*pfnCreateInstance
)(IUnknown
*pUnkOuter
, REFIID riid
,
332 /* There should be more, but these are the only ones listed in the header
334 extern HRESULT
DDRAW_CreateDirectDrawClipper(IUnknown
*pUnkOuter
, REFIID riid
,
337 static const struct object_creation_info object_creation
[] =
339 { &CLSID_DirectDraw
, DDRAW_CreateDirectDraw
},
340 { &CLSID_DirectDraw7
, DDRAW_CreateDirectDraw
},
341 { &CLSID_DirectDrawClipper
, DDRAW_CreateDirectDrawClipper
}
344 static HRESULT WINAPI
345 DDCF_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
,LPVOID
*ppobj
)
347 ICOM_THIS(IClassFactoryImpl
,iface
);
349 if (IsEqualGUID(riid
, &IID_IUnknown
)
350 || IsEqualGUID(riid
, &IID_IClassFactory
))
352 IClassFactory_AddRef(iface
);
357 WARN("(%p)->(%s,%p),not found\n",This
,debugstr_guid(riid
),ppobj
);
358 return E_NOINTERFACE
;
361 static ULONG WINAPI
DDCF_AddRef(LPCLASSFACTORY iface
) {
362 ICOM_THIS(IClassFactoryImpl
,iface
);
363 return ++(This
->ref
);
366 static ULONG WINAPI
DDCF_Release(LPCLASSFACTORY iface
) {
367 ICOM_THIS(IClassFactoryImpl
,iface
);
369 ULONG ref
= --This
->ref
;
372 HeapFree(GetProcessHeap(), 0, This
);
378 static HRESULT WINAPI
DDCF_CreateInstance(
379 LPCLASSFACTORY iface
,LPUNKNOWN pOuter
,REFIID riid
,LPVOID
*ppobj
381 ICOM_THIS(IClassFactoryImpl
,iface
);
383 TRACE("(%p)->(%p,%s,%p)\n",This
,pOuter
,debugstr_guid(riid
),ppobj
);
385 return This
->pfnCreateInstance(pOuter
, riid
, ppobj
);
388 static HRESULT WINAPI
DDCF_LockServer(LPCLASSFACTORY iface
,BOOL dolock
) {
389 ICOM_THIS(IClassFactoryImpl
,iface
);
390 FIXME("(%p)->(%d),stub!\n",This
,dolock
);
394 static ICOM_VTABLE(IClassFactory
) DDCF_Vtbl
=
396 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
404 /*******************************************************************************
405 * DllGetClassObject [DDRAW.@]
406 * Retrieves class object from a DLL object
409 * Docs say returns STDAPI
412 * rclsid [I] CLSID for the class object
413 * riid [I] Reference to identifier of interface for class object
414 * ppv [O] Address of variable to receive interface pointer for riid
418 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
421 DWORD WINAPI
DDRAW_DllGetClassObject(REFCLSID rclsid
,REFIID riid
,LPVOID
*ppv
)
424 IClassFactoryImpl
*factory
;
426 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
428 if ( !IsEqualGUID( &IID_IClassFactory
, riid
)
429 && ! IsEqualGUID( &IID_IUnknown
, riid
) )
430 return E_NOINTERFACE
;
432 for (i
=0; i
< sizeof(object_creation
)/sizeof(object_creation
[0]); i
++)
434 if (IsEqualGUID(object_creation
[i
].clsid
, rclsid
))
438 if (i
== sizeof(object_creation
)/sizeof(object_creation
[0]))
440 FIXME("%s: no class found.\n", debugstr_guid(rclsid
));
441 return CLASS_E_CLASSNOTAVAILABLE
;
444 factory
= HeapAlloc(GetProcessHeap(), 0, sizeof(*factory
));
445 if (factory
== NULL
) return E_OUTOFMEMORY
;
447 ICOM_INIT_INTERFACE(factory
, IClassFactory
, DDCF_Vtbl
);
450 factory
->pfnCreateInstance
= object_creation
[i
].pfnCreateInstance
;
452 *ppv
= ICOM_INTERFACE(factory
, IClassFactory
);
457 /*******************************************************************************
458 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
464 DWORD WINAPI
DDRAW_DllCanUnloadNow(void) {
465 FIXME("(void): stub\n");
469 /******************************************************************************
473 /* Choose which driver is considered the primary display driver. It will
474 * be created when we get a NULL guid for the DirectDrawCreate(Ex). */
475 static int DDRAW_ChooseDefaultDriver(void)
481 assert(DDRAW_num_drivers
> 0);
483 /* This algorithm is really stupid. */
484 for (i
=0; i
< DDRAW_num_drivers
; i
++)
486 if (DDRAW_drivers
[i
]->preference
> best_score
)
488 best_score
= DDRAW_drivers
[i
]->preference
;
493 assert(best_score
> 0);
498 BOOL WINAPI
DDRAW_DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpv
)
500 /* If we were sufficiently cool, DDraw drivers would just be COM
501 * objects, registered with a particular component category. */
503 DDRAW_HAL_Init(hInstDLL
, fdwReason
, lpv
);
504 DDRAW_User_Init(hInstDLL
, fdwReason
, lpv
);
506 if (DDRAW_num_drivers
> 0)
507 DDRAW_default_driver
= DDRAW_ChooseDefaultDriver();
512 /* Register a direct draw driver. This should be called from your init
513 * function. (That's why there is no locking: your init func is called from
514 * our DllInit, which is serialised.) */
515 void DDRAW_register_driver(const ddraw_driver
*driver
)
519 for (i
= 0; i
< DDRAW_num_drivers
; i
++)
521 if (DDRAW_drivers
[i
] == driver
)
523 ERR("Driver reregistering %p\n", driver
);
528 if (DDRAW_num_drivers
== sizeof(DDRAW_drivers
)/sizeof(DDRAW_drivers
[0]))
530 ERR("too many DDRAW drivers\n");
534 DDRAW_drivers
[DDRAW_num_drivers
++] = driver
;
537 /* This totally doesn't belong here. */
538 LONG
DDRAW_width_bpp_to_pitch(DWORD width
, DWORD bpp
)
542 assert(bpp
!= 0); /* keeps happening... */
544 if (bpp
== 15) bpp
= 16;
545 pitch
= width
* (bpp
/ 8);
546 return pitch
+ (8 - (pitch
% 8)) % 8;