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 DPRINTF("DDENUM_ATTACHEDSECONDARYDEVICES ");
71 if (dwFlags
& DDENUM_DETACHEDSECONDARYDEVICES
)
72 DPRINTF("DDENUM_DETACHEDSECONDARYDEVICES ");
73 if (dwFlags
& DDENUM_NONDISPLAYDEVICES
)
74 DPRINTF("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 if (DDRAW_num_drivers
== 0)
246 WARN("no DirectDraw drivers registered\n");
247 return DDERR_INVALIDDIRECTDRAWGUID
;
250 if (lpGUID
== (LPGUID
)DDCREATE_EMULATIONONLY
251 || lpGUID
== (LPGUID
)DDCREATE_HARDWAREONLY
)
254 TRACE("(%s,%p,%p)\n",debugstr_guid(lpGUID
),lplpDD
,pUnkOuter
);
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 return DDRAW_Create(lpGUID
,(LPVOID
*)lplpDD
,pUnkOuter
,&IID_IDirectDraw
,FALSE
);
283 /***********************************************************************
284 * DirectDrawCreateEx (DDRAW.@)
286 * Only creates new IDirectDraw7 interfaces.
287 * Supposed to fail if legacy interfaces are requested.
290 HRESULT WINAPI
DirectDrawCreateEx(
291 LPGUID lpGUID
, LPVOID
* lplpDD
, REFIID iid
, LPUNKNOWN pUnkOuter
293 if (!IsEqualGUID(iid
, &IID_IDirectDraw7
))
294 return DDERR_INVALIDPARAMS
;
296 return DDRAW_Create(lpGUID
, lplpDD
, pUnkOuter
, iid
, TRUE
);
299 extern HRESULT
Uninit_DirectDraw_Create(const GUID
*, LPDIRECTDRAW7
*,
302 /* This is for the class factory. */
303 static HRESULT
DDRAW_CreateDirectDraw(IUnknown
* pUnkOuter
, REFIID iid
,
309 hr
= Uninit_DirectDraw_Create(NULL
, &pDD
, pUnkOuter
, TRUE
); /* ex? */
310 if (FAILED(hr
)) return hr
;
312 hr
= IDirectDraw7_QueryInterface(pDD
, iid
, ppObj
);
313 IDirectDraw_Release(pDD
);
317 /******************************************************************************
318 * DirectDraw ClassFactory
321 ICOM_VFIELD_MULTI(IClassFactory
);
324 HRESULT (*pfnCreateInstance
)(IUnknown
*pUnkOuter
, REFIID iid
,
328 struct object_creation_info
331 HRESULT (*pfnCreateInstance
)(IUnknown
*pUnkOuter
, REFIID riid
,
335 /* There should be more, but these are the only ones listed in the header
337 extern HRESULT
DDRAW_CreateDirectDrawClipper(IUnknown
*pUnkOuter
, REFIID riid
,
340 static const struct object_creation_info object_creation
[] =
342 { &CLSID_DirectDraw
, DDRAW_CreateDirectDraw
},
343 { &CLSID_DirectDraw7
, DDRAW_CreateDirectDraw
},
344 { &CLSID_DirectDrawClipper
, DDRAW_CreateDirectDrawClipper
}
347 static HRESULT WINAPI
348 DDCF_QueryInterface(LPCLASSFACTORY iface
,REFIID riid
,LPVOID
*ppobj
)
350 ICOM_THIS(IClassFactoryImpl
,iface
);
352 if (IsEqualGUID(riid
, &IID_IUnknown
)
353 || IsEqualGUID(riid
, &IID_IClassFactory
))
355 IClassFactory_AddRef(iface
);
360 WARN("(%p)->(%s,%p),not found\n",This
,debugstr_guid(riid
),ppobj
);
361 return E_NOINTERFACE
;
364 static ULONG WINAPI
DDCF_AddRef(LPCLASSFACTORY iface
) {
365 ICOM_THIS(IClassFactoryImpl
,iface
);
366 return ++(This
->ref
);
369 static ULONG WINAPI
DDCF_Release(LPCLASSFACTORY iface
) {
370 ICOM_THIS(IClassFactoryImpl
,iface
);
372 ULONG ref
= --This
->ref
;
375 HeapFree(GetProcessHeap(), 0, This
);
381 static HRESULT WINAPI
DDCF_CreateInstance(
382 LPCLASSFACTORY iface
,LPUNKNOWN pOuter
,REFIID riid
,LPVOID
*ppobj
384 ICOM_THIS(IClassFactoryImpl
,iface
);
386 TRACE("(%p)->(%p,%s,%p)\n",This
,pOuter
,debugstr_guid(riid
),ppobj
);
388 return This
->pfnCreateInstance(pOuter
, riid
, ppobj
);
391 static HRESULT WINAPI
DDCF_LockServer(LPCLASSFACTORY iface
,BOOL dolock
) {
392 ICOM_THIS(IClassFactoryImpl
,iface
);
393 FIXME("(%p)->(%d),stub!\n",This
,dolock
);
397 static ICOM_VTABLE(IClassFactory
) DDCF_Vtbl
=
399 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
407 /*******************************************************************************
408 * DllGetClassObject [DDRAW.@]
409 * Retrieves class object from a DLL object
412 * Docs say returns STDAPI
415 * rclsid [I] CLSID for the class object
416 * riid [I] Reference to identifier of interface for class object
417 * ppv [O] Address of variable to receive interface pointer for riid
421 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
424 DWORD WINAPI
DDRAW_DllGetClassObject(REFCLSID rclsid
,REFIID riid
,LPVOID
*ppv
)
427 IClassFactoryImpl
*factory
;
429 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid
), debugstr_guid(riid
), ppv
);
431 if ( !IsEqualGUID( &IID_IClassFactory
, riid
)
432 && ! IsEqualGUID( &IID_IUnknown
, riid
) )
433 return E_NOINTERFACE
;
435 for (i
=0; i
< sizeof(object_creation
)/sizeof(object_creation
[0]); i
++)
437 if (IsEqualGUID(object_creation
[i
].clsid
, rclsid
))
441 if (i
== sizeof(object_creation
)/sizeof(object_creation
[0]))
443 FIXME("%s: no class found.\n", debugstr_guid(rclsid
));
444 return CLASS_E_CLASSNOTAVAILABLE
;
447 factory
= HeapAlloc(GetProcessHeap(), 0, sizeof(*factory
));
448 if (factory
== NULL
) return E_OUTOFMEMORY
;
450 ICOM_INIT_INTERFACE(factory
, IClassFactory
, DDCF_Vtbl
);
453 factory
->pfnCreateInstance
= object_creation
[i
].pfnCreateInstance
;
455 *ppv
= ICOM_INTERFACE(factory
, IClassFactory
);
460 /*******************************************************************************
461 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
467 DWORD WINAPI
DDRAW_DllCanUnloadNow(void) {
468 FIXME("(void): stub\n");
472 /******************************************************************************
476 /* Choose which driver is considered the primary display driver. It will
477 * be created when we get a NULL guid for the DirectDrawCreate(Ex). */
478 static int DDRAW_ChooseDefaultDriver(void)
484 assert(DDRAW_num_drivers
> 0);
486 /* This algorithm is really stupid. */
487 for (i
=0; i
< DDRAW_num_drivers
; i
++)
489 if (DDRAW_drivers
[i
]->preference
> best_score
)
491 best_score
= DDRAW_drivers
[i
]->preference
;
496 assert(best_score
> 0);
501 BOOL WINAPI
DDRAW_DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpv
)
503 /* If we were sufficiently cool, DDraw drivers would just be COM
504 * objects, registered with a particular component category. */
506 DDRAW_HAL_Init(hInstDLL
, fdwReason
, lpv
);
507 DDRAW_User_Init(hInstDLL
, fdwReason
, lpv
);
509 if (fdwReason
== DLL_PROCESS_ATTACH
)
511 HMODULE mod
= GetModuleHandleA( "x11drv.dll" );
514 wine_tsx11_lock_ptr
= (void *)GetProcAddress( mod
, "wine_tsx11_lock" );
515 wine_tsx11_unlock_ptr
= (void *)GetProcAddress( mod
, "wine_tsx11_unlock" );
519 if (DDRAW_num_drivers
> 0)
520 DDRAW_default_driver
= DDRAW_ChooseDefaultDriver();
525 /* Register a direct draw driver. This should be called from your init
526 * function. (That's why there is no locking: your init func is called from
527 * our DllInit, which is serialised.) */
528 void DDRAW_register_driver(const ddraw_driver
*driver
)
532 for (i
= 0; i
< DDRAW_num_drivers
; i
++)
534 if (DDRAW_drivers
[i
] == driver
)
536 ERR("Driver reregistering %p\n", driver
);
541 if (DDRAW_num_drivers
== sizeof(DDRAW_drivers
)/sizeof(DDRAW_drivers
[0]))
543 ERR("too many DDRAW drivers\n");
547 DDRAW_drivers
[DDRAW_num_drivers
++] = driver
;
550 /* This totally doesn't belong here. */
551 LONG
DDRAW_width_bpp_to_pitch(DWORD width
, DWORD bpp
)
555 assert(bpp
!= 0); /* keeps happening... */
557 if (bpp
== 15) bpp
= 16;
558 pitch
= width
* (bpp
/ 8);
559 return pitch
+ (8 - (pitch
% 8)) % 8;