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 void (*wine_tsx11_lock_ptr)(void) = NULL;
47 void (*wine_tsx11_unlock_ptr)(void) = NULL;
49 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
51 /**********************************************************************/
56 } DirectDrawEnumerateProcData;
58 /***********************************************************************
59 * DirectDrawEnumerateExA (DDRAW.@)
61 HRESULT WINAPI DirectDrawEnumerateExA(
62 LPDDENUMCALLBACKEXA lpCallback, LPVOID lpContext, DWORD dwFlags)
65 TRACE("(%p,%p, %08lx)\n", lpCallback, lpContext, dwFlags);
67 if (TRACE_ON(ddraw)) {
69 if (dwFlags & DDENUM_ATTACHEDSECONDARYDEVICES)
70 TRACE("DDENUM_ATTACHEDSECONDARYDEVICES ");
71 if (dwFlags & DDENUM_DETACHEDSECONDARYDEVICES)
72 TRACE("DDENUM_DETACHEDSECONDARYDEVICES ");
73 if (dwFlags & DDENUM_NONDISPLAYDEVICES)
74 TRACE("DDENUM_NONDISPLAYDEVICES ");
78 for (i=0; i<DDRAW_num_drivers; i++)
80 TRACE("Enumerating %s/%s interface\n",
81 DDRAW_drivers[i]->info->szDriver,
82 DDRAW_drivers[i]->info->szDescription);
84 /* We have to pass NULL from the primary display device.
85 * RoadRage chapter 6's enumeration routine expects it. */
86 if (!lpCallback((DDRAW_default_driver == i) ? NULL
87 :(LPGUID)&DDRAW_drivers[i]->info->guidDeviceIdentifier,
88 (LPSTR)DDRAW_drivers[i]->info->szDescription,
89 (LPSTR)DDRAW_drivers[i]->info->szDriver,
94 /* Unsupported flags */
95 if (dwFlags & DDENUM_NONDISPLAYDEVICES) {
96 FIXME("no non-display devices supported.\n");
98 if (dwFlags & DDENUM_DETACHEDSECONDARYDEVICES) {
99 FIXME("no detached secondary devices supported.\n");
105 /***********************************************************************
106 * DirectDrawEnumerateExW (DDRAW.@)
109 static BOOL CALLBACK DirectDrawEnumerateExProcW(
110 GUID *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName,
111 LPVOID lpContext, HMONITOR hm)
115 LPWSTR lpDriverDescriptionW, lpDriverNameW;
116 DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
118 len = MultiByteToWideChar( CP_ACP, 0, lpDriverDescription, -1, NULL, 0 );
119 lpDriverDescriptionW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
120 MultiByteToWideChar( CP_ACP, 0, lpDriverDescription, -1, lpDriverDescriptionW, len );
122 len = MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, NULL, 0 );
123 lpDriverNameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
124 MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, lpDriverNameW, len );
126 bResult = (*(LPDDENUMCALLBACKEXW *) pEPD->lpCallback)(lpGUID, lpDriverDescriptionW,
127 lpDriverNameW, pEPD->lpContext, hm);
129 HeapFree(GetProcessHeap(), 0, lpDriverDescriptionW);
130 HeapFree(GetProcessHeap(), 0, lpDriverNameW);
134 HRESULT WINAPI DirectDrawEnumerateExW(
135 LPDDENUMCALLBACKEXW lpCallback, LPVOID lpContext, DWORD dwFlags)
137 DirectDrawEnumerateProcData epd;
138 epd.lpCallback = (LPVOID) lpCallback;
139 epd.lpContext = lpContext;
141 return DirectDrawEnumerateExA(DirectDrawEnumerateExProcW, (LPVOID) &epd, 0);
144 /***********************************************************************
145 * DirectDrawEnumerateA (DDRAW.@)
148 static BOOL CALLBACK DirectDrawEnumerateProcA(
149 GUID *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName,
150 LPVOID lpContext, HMONITOR hm)
152 DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
154 return ((LPDDENUMCALLBACKA) pEPD->lpCallback)(
155 lpGUID, lpDriverDescription, lpDriverName, pEPD->lpContext);
158 HRESULT WINAPI DirectDrawEnumerateA(
159 LPDDENUMCALLBACKA lpCallback, LPVOID lpContext)
161 DirectDrawEnumerateProcData epd;
162 epd.lpCallback = (LPVOID) lpCallback;
163 epd.lpContext = lpContext;
165 return DirectDrawEnumerateExA(DirectDrawEnumerateProcA, (LPVOID) &epd, 0);
168 /***********************************************************************
169 * DirectDrawEnumerateW (DDRAW.@)
172 static BOOL WINAPI DirectDrawEnumerateProcW(
173 GUID *lpGUID, LPWSTR lpDriverDescription, LPWSTR lpDriverName,
174 LPVOID lpContext, HMONITOR hm)
176 DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
178 return ((LPDDENUMCALLBACKW) pEPD->lpCallback)(
179 lpGUID, lpDriverDescription, lpDriverName, pEPD->lpContext);
182 HRESULT WINAPI DirectDrawEnumerateW(
183 LPDDENUMCALLBACKW lpCallback, LPVOID lpContext)
185 DirectDrawEnumerateProcData epd;
186 epd.lpCallback = (LPVOID) lpCallback;
187 epd.lpContext = lpContext;
189 return DirectDrawEnumerateExW(DirectDrawEnumerateProcW, (LPVOID) &epd, 0);
192 /***********************************************************************
193 * DirectDrawCreate (DDRAW.@)
196 const ddraw_driver* DDRAW_FindDriver(const GUID* pGUID)
198 static const GUID zeroGUID; /* gets zero-inited */
200 TRACE("(%s)\n", pGUID ? debugstr_guid(pGUID) : "(null)");
202 if (DDRAW_num_drivers == 0) return NULL;
204 if (pGUID == (LPGUID)DDCREATE_EMULATIONONLY
205 || pGUID == (LPGUID)DDCREATE_HARDWAREONLY)
208 if (pGUID == NULL || memcmp(pGUID, &zeroGUID, sizeof(GUID)) == 0)
210 /* Use the default driver. */
211 return DDRAW_drivers[DDRAW_default_driver];
215 /* Look for a matching GUID. */
218 for (i=0; i < DDRAW_num_drivers; i++)
220 if (IsEqualGUID(pGUID,
221 &DDRAW_drivers[i]->info->guidDeviceIdentifier))
225 if (i < DDRAW_num_drivers)
227 return DDRAW_drivers[i];
231 ERR("(%s): did not recognize requested GUID.\n",debugstr_guid(pGUID));
237 static HRESULT DDRAW_Create(
238 LPGUID lpGUID, LPVOID *lplpDD, LPUNKNOWN pUnkOuter, REFIID iid, BOOL ex
240 const ddraw_driver* driver;
244 TRACE("(%s,%p,%p,%d)\n", debugstr_guid(lpGUID), lplpDD, pUnkOuter, ex);
246 if (DDRAW_num_drivers == 0)
248 WARN("no DirectDraw drivers registered\n");
249 return DDERR_INVALIDDIRECTDRAWGUID;
252 if (lpGUID == (LPGUID)DDCREATE_EMULATIONONLY
253 || lpGUID == (LPGUID)DDCREATE_HARDWAREONLY)
256 if (pUnkOuter != NULL)
257 return DDERR_INVALIDPARAMS; /* CLASS_E_NOAGGREGATION? */
259 driver = DDRAW_FindDriver(lpGUID);
260 if (driver == NULL) return DDERR_INVALIDDIRECTDRAWGUID;
262 hr = driver->create(lpGUID, &pDD, pUnkOuter, ex);
263 if (FAILED(hr)) return hr;
265 hr = IDirectDraw7_QueryInterface(pDD, iid, lplpDD);
266 IDirectDraw7_Release(pDD);
270 /***********************************************************************
271 * DirectDrawCreate (DDRAW.@)
273 * Only creates legacy IDirectDraw interfaces.
274 * Cannot create IDirectDraw7 interfaces.
277 HRESULT WINAPI DirectDrawCreate(
278 LPGUID lpGUID, LPDIRECTDRAW* lplpDD, LPUNKNOWN pUnkOuter
280 TRACE("(%s,%p,%p)\n", debugstr_guid(lpGUID), lplpDD, pUnkOuter);
281 return DDRAW_Create(lpGUID, (LPVOID*) lplpDD, pUnkOuter, &IID_IDirectDraw, FALSE);
284 /***********************************************************************
285 * DirectDrawCreateEx (DDRAW.@)
287 * Only creates new IDirectDraw7 interfaces.
288 * Supposed to fail if legacy interfaces are requested.
291 HRESULT WINAPI DirectDrawCreateEx(
292 LPGUID lpGUID, LPVOID* lplpDD, REFIID iid, LPUNKNOWN pUnkOuter
294 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(lpGUID), lplpDD, debugstr_guid(iid), pUnkOuter);
296 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
297 return DDERR_INVALIDPARAMS;
299 return DDRAW_Create(lpGUID, lplpDD, pUnkOuter, iid, TRUE);
302 extern HRESULT Uninit_DirectDraw_Create(const GUID*, LPDIRECTDRAW7*,
305 /* This is for the class factory. */
306 static HRESULT DDRAW_CreateDirectDraw(IUnknown* pUnkOuter, REFIID iid,
313 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppObj);
315 /* This is a mighty hack :-) */
316 if (IsEqualGUID(iid, &IID_IDirectDraw7))
321 hr = Uninit_DirectDraw_Create(NULL, &pDD, pUnkOuter, ex);
322 if (FAILED(hr)) return hr;
324 hr = IDirectDraw7_QueryInterface(pDD, iid, ppObj);
325 IDirectDraw_Release(pDD);
329 /******************************************************************************
330 * DirectDraw ClassFactory
333 ICOM_VFIELD_MULTI(IClassFactory);
336 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID iid,
340 struct object_creation_info
343 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID riid,
347 /* There should be more, but these are the only ones listed in the header
349 extern HRESULT DDRAW_CreateDirectDrawClipper(IUnknown *pUnkOuter, REFIID riid,
352 static const struct object_creation_info object_creation[] =
354 { &CLSID_DirectDraw, DDRAW_CreateDirectDraw },
355 { &CLSID_DirectDraw7, DDRAW_CreateDirectDraw },
356 { &CLSID_DirectDrawClipper, DDRAW_CreateDirectDrawClipper }
359 static HRESULT WINAPI
360 DDCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
362 ICOM_THIS(IClassFactoryImpl,iface);
364 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj);
366 if (IsEqualGUID(riid, &IID_IUnknown)
367 || IsEqualGUID(riid, &IID_IClassFactory))
369 IClassFactory_AddRef(iface);
374 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
375 return E_NOINTERFACE;
378 static ULONG WINAPI DDCF_AddRef(LPCLASSFACTORY iface) {
379 ICOM_THIS(IClassFactoryImpl,iface);
381 TRACE("(%p)->() incrementing from %ld.\n", This, This->ref);
383 return ++(This->ref);
386 static ULONG WINAPI DDCF_Release(LPCLASSFACTORY iface) {
387 ICOM_THIS(IClassFactoryImpl,iface);
389 TRACE("(%p)->() decrementing from %ld.\n", This, This->ref);
391 if (--This->ref == 0)
392 HeapFree(GetProcessHeap(), 0, This);
398 static HRESULT WINAPI DDCF_CreateInstance(
399 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
401 ICOM_THIS(IClassFactoryImpl,iface);
403 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
405 return This->pfnCreateInstance(pOuter, riid, ppobj);
408 static HRESULT WINAPI DDCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
409 ICOM_THIS(IClassFactoryImpl,iface);
410 FIXME("(%p)->(%d),stub!\n",This,dolock);
414 static ICOM_VTABLE(IClassFactory) DDCF_Vtbl =
416 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
424 /*******************************************************************************
425 * DllGetClassObject [DDRAW.@]
426 * Retrieves class object from a DLL object
429 * Docs say returns STDAPI
432 * rclsid [I] CLSID for the class object
433 * riid [I] Reference to identifier of interface for class object
434 * ppv [O] Address of variable to receive interface pointer for riid
438 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
441 DWORD WINAPI DDRAW_DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID *ppv)
444 IClassFactoryImpl *factory;
446 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
448 if ( !IsEqualGUID( &IID_IClassFactory, riid )
449 && ! IsEqualGUID( &IID_IUnknown, riid) )
450 return E_NOINTERFACE;
452 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
454 if (IsEqualGUID(object_creation[i].clsid, rclsid))
458 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
460 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
461 return CLASS_E_CLASSNOTAVAILABLE;
464 factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
465 if (factory == NULL) return E_OUTOFMEMORY;
467 ICOM_INIT_INTERFACE(factory, IClassFactory, DDCF_Vtbl);
470 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
472 *ppv = ICOM_INTERFACE(factory, IClassFactory);
477 /*******************************************************************************
478 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
484 DWORD WINAPI DDRAW_DllCanUnloadNow(void) {
485 FIXME("(void): stub\n");
489 /******************************************************************************
493 /* Choose which driver is considered the primary display driver. It will
494 * be created when we get a NULL guid for the DirectDrawCreate(Ex). */
495 static int DDRAW_ChooseDefaultDriver(void)
501 assert(DDRAW_num_drivers > 0);
503 /* This algorithm is really stupid. */
504 for (i=0; i < DDRAW_num_drivers; i++)
506 if (DDRAW_drivers[i]->preference > best_score)
508 best_score = DDRAW_drivers[i]->preference;
513 assert(best_score > 0);
518 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
520 /* If we were sufficiently cool, DDraw drivers would just be COM
521 * objects, registered with a particular component category. */
523 DDRAW_HAL_Init(hInstDLL, fdwReason, lpv);
524 DDRAW_User_Init(hInstDLL, fdwReason, lpv);
526 if (fdwReason == DLL_PROCESS_ATTACH)
528 HMODULE mod = GetModuleHandleA( "x11drv.dll" );
531 wine_tsx11_lock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
532 wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
536 if (DDRAW_num_drivers > 0)
537 DDRAW_default_driver = DDRAW_ChooseDefaultDriver();
542 /* Register a direct draw driver. This should be called from your init
543 * function. (That's why there is no locking: your init func is called from
544 * our DllInit, which is serialised.) */
545 void DDRAW_register_driver(const ddraw_driver *driver)
549 for (i = 0; i < DDRAW_num_drivers; i++)
551 if (DDRAW_drivers[i] == driver)
553 ERR("Driver reregistering %p\n", driver);
558 if (DDRAW_num_drivers == sizeof(DDRAW_drivers)/sizeof(DDRAW_drivers[0]))
560 ERR("too many DDRAW drivers\n");
564 DDRAW_drivers[DDRAW_num_drivers++] = driver;
567 /* This totally doesn't belong here. */
568 LONG DDRAW_width_bpp_to_pitch(DWORD width, DWORD bpp)
572 assert(bpp != 0); /* keeps happening... */
574 if (bpp == 15) bpp = 16;
575 pitch = width * (bpp / 8);
576 return pitch + (8 - (pitch % 8)) % 8;