Changed Adam Sacarny's email, and -debugmsg to --debugmsg.
[wine/gsoc_dplay.git] / dlls / ddraw / main.c
blobeb1b6559f2fa5bae1203dd0e3e8519e998557818
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
25 #define GLPRIVATE_NO_REDEFINE
27 #include "config.h"
29 #include <assert.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <stdlib.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winnls.h"
37 #include "winerror.h"
38 #include "wingdi.h"
40 #include "ddraw.h"
41 #include "d3d.h"
43 /* This for all the enumeration and creation of D3D-related objects */
44 #include "ddraw_private.h"
45 #include "wine/debug.h"
46 #include "wine/library.h"
47 #include "wine/port.h"
49 #include "gl_private.h"
51 #undef GLPRIVATE_NO_REDEFINE
53 #define MAX_DDRAW_DRIVERS 3
54 static const ddraw_driver* DDRAW_drivers[MAX_DDRAW_DRIVERS];
55 static int DDRAW_num_drivers; /* = 0 */
56 static int DDRAW_default_driver;
58 void (*wine_tsx11_lock_ptr)(void) = NULL;
59 void (*wine_tsx11_unlock_ptr)(void) = NULL;
61 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
63 /**********************************************************************/
65 typedef struct {
66 LPVOID lpCallback;
67 LPVOID lpContext;
68 } DirectDrawEnumerateProcData;
70 BOOL opengl_initialized = 0;
72 #ifdef HAVE_OPENGL
74 #include "mesa_private.h"
76 static void *gl_handle = NULL;
78 #define GL_API_FUNCTION(f) typeof(f) * p##f;
79 #include "gl_api.h"
80 #undef GL_API_FUNCTION
82 #ifndef SONAME_LIBGL
83 #define SONAME_LIBGL "libGL.so"
84 #endif
86 static BOOL DDRAW_bind_to_opengl( void )
88 const char *glname = SONAME_LIBGL;
89 BOOL ret_value;
91 gl_handle = wine_dlopen(glname, RTLD_NOW, NULL, 0);
92 if (!gl_handle) {
93 WARN("Wine cannot find the OpenGL graphics library (%s).\n",glname);
94 return FALSE;
97 #define GL_API_FUNCTION(f) \
98 if((p##f = wine_dlsym(gl_handle, #f, NULL, 0)) == NULL) \
99 { \
100 WARN("Can't find symbol %s\n", #f); \
101 goto sym_not_found; \
103 #include "gl_api.h"
104 #undef GL_API_FUNCTION
106 /* And now calls the function to initialize the various fields for the rendering devices */
107 ret_value = d3ddevice_init_at_startup(gl_handle);
109 wine_dlclose(gl_handle, NULL, 0);
110 gl_handle = NULL;
111 return ret_value;
113 sym_not_found:
114 WARN("Wine cannot find certain functions that it needs inside the OpenGL\n"
115 "graphics library. To enable Wine to use OpenGL please upgrade\n"
116 "your OpenGL libraries\n");
117 wine_dlclose(gl_handle, NULL, 0);
118 gl_handle = NULL;
119 return FALSE;
122 #endif /* HAVE_OPENGL */
124 /***********************************************************************
125 * DirectDrawEnumerateExA (DDRAW.@)
127 HRESULT WINAPI DirectDrawEnumerateExA(
128 LPDDENUMCALLBACKEXA lpCallback, LPVOID lpContext, DWORD dwFlags)
130 int i;
131 TRACE("(%p,%p, %08lx)\n", lpCallback, lpContext, dwFlags);
133 if (TRACE_ON(ddraw)) {
134 TRACE(" Flags : ");
135 if (dwFlags & DDENUM_ATTACHEDSECONDARYDEVICES)
136 TRACE("DDENUM_ATTACHEDSECONDARYDEVICES ");
137 if (dwFlags & DDENUM_DETACHEDSECONDARYDEVICES)
138 TRACE("DDENUM_DETACHEDSECONDARYDEVICES ");
139 if (dwFlags & DDENUM_NONDISPLAYDEVICES)
140 TRACE("DDENUM_NONDISPLAYDEVICES ");
141 TRACE("\n");
144 for (i=0; i<DDRAW_num_drivers; i++)
146 TRACE("Enumerating %s/%s interface\n",
147 DDRAW_drivers[i]->info->szDriver,
148 DDRAW_drivers[i]->info->szDescription);
150 /* We have to pass NULL from the primary display device.
151 * RoadRage chapter 6's enumeration routine expects it. */
152 if (!lpCallback((DDRAW_default_driver == i) ? NULL
153 :(LPGUID)&DDRAW_drivers[i]->info->guidDeviceIdentifier,
154 (LPSTR)DDRAW_drivers[i]->info->szDescription,
155 (LPSTR)DDRAW_drivers[i]->info->szDriver,
156 lpContext, 0))
157 return DD_OK;
160 /* Unsupported flags */
161 if (dwFlags & DDENUM_NONDISPLAYDEVICES) {
162 FIXME("no non-display devices supported.\n");
164 if (dwFlags & DDENUM_DETACHEDSECONDARYDEVICES) {
165 FIXME("no detached secondary devices supported.\n");
168 return DD_OK;
171 /***********************************************************************
172 * DirectDrawEnumerateExW (DDRAW.@)
175 static BOOL CALLBACK DirectDrawEnumerateExProcW(
176 GUID *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName,
177 LPVOID lpContext, HMONITOR hm)
179 INT len;
180 BOOL bResult;
181 LPWSTR lpDriverDescriptionW, lpDriverNameW;
182 DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
184 len = MultiByteToWideChar( CP_ACP, 0, lpDriverDescription, -1, NULL, 0 );
185 lpDriverDescriptionW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
186 MultiByteToWideChar( CP_ACP, 0, lpDriverDescription, -1, lpDriverDescriptionW, len );
188 len = MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, NULL, 0 );
189 lpDriverNameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
190 MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, lpDriverNameW, len );
192 bResult = (*(LPDDENUMCALLBACKEXW *) pEPD->lpCallback)(lpGUID, lpDriverDescriptionW,
193 lpDriverNameW, pEPD->lpContext, hm);
195 HeapFree(GetProcessHeap(), 0, lpDriverDescriptionW);
196 HeapFree(GetProcessHeap(), 0, lpDriverNameW);
197 return bResult;
200 HRESULT WINAPI DirectDrawEnumerateExW(
201 LPDDENUMCALLBACKEXW lpCallback, LPVOID lpContext, DWORD dwFlags)
203 DirectDrawEnumerateProcData epd;
204 epd.lpCallback = (LPVOID) lpCallback;
205 epd.lpContext = lpContext;
207 return DirectDrawEnumerateExA(DirectDrawEnumerateExProcW, (LPVOID) &epd, 0);
210 /***********************************************************************
211 * DirectDrawEnumerateA (DDRAW.@)
214 static BOOL CALLBACK DirectDrawEnumerateProcA(
215 GUID *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName,
216 LPVOID lpContext, HMONITOR hm)
218 DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
220 return ((LPDDENUMCALLBACKA) pEPD->lpCallback)(
221 lpGUID, lpDriverDescription, lpDriverName, pEPD->lpContext);
224 HRESULT WINAPI DirectDrawEnumerateA(
225 LPDDENUMCALLBACKA lpCallback, LPVOID lpContext)
227 DirectDrawEnumerateProcData epd;
228 epd.lpCallback = (LPVOID) lpCallback;
229 epd.lpContext = lpContext;
231 return DirectDrawEnumerateExA(DirectDrawEnumerateProcA, (LPVOID) &epd, 0);
234 /***********************************************************************
235 * DirectDrawEnumerateW (DDRAW.@)
238 static BOOL WINAPI DirectDrawEnumerateProcW(
239 GUID *lpGUID, LPWSTR lpDriverDescription, LPWSTR lpDriverName,
240 LPVOID lpContext, HMONITOR hm)
242 DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
244 return ((LPDDENUMCALLBACKW) pEPD->lpCallback)(
245 lpGUID, lpDriverDescription, lpDriverName, pEPD->lpContext);
248 HRESULT WINAPI DirectDrawEnumerateW(
249 LPDDENUMCALLBACKW lpCallback, LPVOID lpContext)
251 DirectDrawEnumerateProcData epd;
252 epd.lpCallback = (LPVOID) lpCallback;
253 epd.lpContext = lpContext;
255 return DirectDrawEnumerateExW(DirectDrawEnumerateProcW, (LPVOID) &epd, 0);
258 /***********************************************************************
259 * DirectDrawCreate (DDRAW.@)
262 const ddraw_driver* DDRAW_FindDriver(const GUID* pGUID)
264 static const GUID zeroGUID; /* gets zero-inited */
266 TRACE("(%s)\n", pGUID ? debugstr_guid(pGUID) : "(null)");
268 if (DDRAW_num_drivers == 0) return NULL;
270 if (pGUID == (LPGUID)DDCREATE_EMULATIONONLY
271 || pGUID == (LPGUID)DDCREATE_HARDWAREONLY)
272 pGUID = NULL;
274 if (pGUID == NULL || memcmp(pGUID, &zeroGUID, sizeof(GUID)) == 0)
276 /* Use the default driver. */
277 return DDRAW_drivers[DDRAW_default_driver];
279 else
281 /* Look for a matching GUID. */
283 int i;
284 for (i=0; i < DDRAW_num_drivers; i++)
286 if (IsEqualGUID(pGUID,
287 &DDRAW_drivers[i]->info->guidDeviceIdentifier))
288 break;
291 if (i < DDRAW_num_drivers)
293 return DDRAW_drivers[i];
295 else
297 ERR("(%s): did not recognize requested GUID.\n",debugstr_guid(pGUID));
298 return NULL;
303 static HRESULT DDRAW_Create(
304 LPGUID lpGUID, LPVOID *lplpDD, LPUNKNOWN pUnkOuter, REFIID iid, BOOL ex
306 const ddraw_driver* driver;
307 LPDIRECTDRAW7 pDD;
308 HRESULT hr;
310 TRACE("(%s,%p,%p,%d)\n", debugstr_guid(lpGUID), lplpDD, pUnkOuter, ex);
312 if (DDRAW_num_drivers == 0)
314 WARN("no DirectDraw drivers registered\n");
315 return DDERR_INVALIDDIRECTDRAWGUID;
318 if (lpGUID == (LPGUID)DDCREATE_EMULATIONONLY
319 || lpGUID == (LPGUID)DDCREATE_HARDWAREONLY)
320 lpGUID = NULL;
322 if (pUnkOuter != NULL)
323 return DDERR_INVALIDPARAMS; /* CLASS_E_NOAGGREGATION? */
325 driver = DDRAW_FindDriver(lpGUID);
326 if (driver == NULL) return DDERR_INVALIDDIRECTDRAWGUID;
328 hr = driver->create(lpGUID, &pDD, pUnkOuter, ex);
329 if (FAILED(hr)) return hr;
331 hr = IDirectDraw7_QueryInterface(pDD, iid, lplpDD);
332 IDirectDraw7_Release(pDD);
333 return hr;
336 /***********************************************************************
337 * DirectDrawCreate (DDRAW.@)
339 * Only creates legacy IDirectDraw interfaces.
340 * Cannot create IDirectDraw7 interfaces.
341 * In theory.
343 HRESULT WINAPI DirectDrawCreate(
344 LPGUID lpGUID, LPDIRECTDRAW* lplpDD, LPUNKNOWN pUnkOuter
346 TRACE("(%s,%p,%p)\n", debugstr_guid(lpGUID), lplpDD, pUnkOuter);
347 return DDRAW_Create(lpGUID, (LPVOID*) lplpDD, pUnkOuter, &IID_IDirectDraw, FALSE);
350 /***********************************************************************
351 * DirectDrawCreateEx (DDRAW.@)
353 * Only creates new IDirectDraw7 interfaces.
354 * Supposed to fail if legacy interfaces are requested.
355 * In theory.
357 HRESULT WINAPI DirectDrawCreateEx(
358 LPGUID lpGUID, LPVOID* lplpDD, REFIID iid, LPUNKNOWN pUnkOuter
360 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(lpGUID), lplpDD, debugstr_guid(iid), pUnkOuter);
362 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
363 return DDERR_INVALIDPARAMS;
365 return DDRAW_Create(lpGUID, lplpDD, pUnkOuter, iid, TRUE);
368 extern HRESULT Uninit_DirectDraw_Create(const GUID*, LPDIRECTDRAW7*,
369 LPUNKNOWN, BOOL);
371 /* This is for the class factory. */
372 static HRESULT DDRAW_CreateDirectDraw(IUnknown* pUnkOuter, REFIID iid,
373 LPVOID* ppObj)
375 LPDIRECTDRAW7 pDD;
376 HRESULT hr;
377 BOOL ex;
379 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppObj);
381 /* This is a mighty hack :-) */
382 if (IsEqualGUID(iid, &IID_IDirectDraw7))
383 ex = TRUE;
384 else
385 ex = FALSE;
387 hr = Uninit_DirectDraw_Create(NULL, &pDD, pUnkOuter, ex);
388 if (FAILED(hr)) return hr;
390 hr = IDirectDraw7_QueryInterface(pDD, iid, ppObj);
391 IDirectDraw_Release(pDD);
392 return hr;
395 /******************************************************************************
396 * DirectDraw ClassFactory
398 typedef struct {
399 ICOM_VFIELD_MULTI(IClassFactory);
401 DWORD ref;
402 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID iid,
403 LPVOID *ppObj);
404 } IClassFactoryImpl;
406 struct object_creation_info
408 const CLSID *clsid;
409 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID riid,
410 LPVOID *ppObj);
413 /* There should be more, but these are the only ones listed in the header
414 * file. */
415 extern HRESULT DDRAW_CreateDirectDrawClipper(IUnknown *pUnkOuter, REFIID riid,
416 LPVOID *ppObj);
418 static const struct object_creation_info object_creation[] =
420 { &CLSID_DirectDraw, DDRAW_CreateDirectDraw },
421 { &CLSID_DirectDraw7, DDRAW_CreateDirectDraw },
422 { &CLSID_DirectDrawClipper, DDRAW_CreateDirectDrawClipper }
425 static HRESULT WINAPI
426 DDCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
428 ICOM_THIS(IClassFactoryImpl,iface);
430 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj);
432 if (IsEqualGUID(riid, &IID_IUnknown)
433 || IsEqualGUID(riid, &IID_IClassFactory))
435 IClassFactory_AddRef(iface);
436 *ppobj = This;
437 return S_OK;
440 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
441 return E_NOINTERFACE;
444 static ULONG WINAPI DDCF_AddRef(LPCLASSFACTORY iface) {
445 ICOM_THIS(IClassFactoryImpl,iface);
447 TRACE("(%p)->() incrementing from %ld.\n", This, This->ref);
449 return ++(This->ref);
452 static ULONG WINAPI DDCF_Release(LPCLASSFACTORY iface) {
453 ICOM_THIS(IClassFactoryImpl,iface);
455 TRACE("(%p)->() decrementing from %ld.\n", This, This->ref);
457 if (--This->ref == 0)
458 HeapFree(GetProcessHeap(), 0, This);
460 return This->ref;
464 static HRESULT WINAPI DDCF_CreateInstance(
465 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
467 ICOM_THIS(IClassFactoryImpl,iface);
469 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
471 return This->pfnCreateInstance(pOuter, riid, ppobj);
474 static HRESULT WINAPI DDCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
475 ICOM_THIS(IClassFactoryImpl,iface);
476 FIXME("(%p)->(%d),stub!\n",This,dolock);
477 return S_OK;
480 static ICOM_VTABLE(IClassFactory) DDCF_Vtbl =
482 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
483 DDCF_QueryInterface,
484 DDCF_AddRef,
485 DDCF_Release,
486 DDCF_CreateInstance,
487 DDCF_LockServer
490 /*******************************************************************************
491 * DllGetClassObject [DDRAW.@]
492 * Retrieves class object from a DLL object
494 * NOTES
495 * Docs say returns STDAPI
497 * PARAMS
498 * rclsid [I] CLSID for the class object
499 * riid [I] Reference to identifier of interface for class object
500 * ppv [O] Address of variable to receive interface pointer for riid
502 * RETURNS
503 * Success: S_OK
504 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
505 * E_UNEXPECTED
507 DWORD WINAPI DDRAW_DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID *ppv)
509 unsigned int i;
510 IClassFactoryImpl *factory;
512 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
514 if ( !IsEqualGUID( &IID_IClassFactory, riid )
515 && ! IsEqualGUID( &IID_IUnknown, riid) )
516 return E_NOINTERFACE;
518 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
520 if (IsEqualGUID(object_creation[i].clsid, rclsid))
521 break;
524 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
526 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
527 return CLASS_E_CLASSNOTAVAILABLE;
530 factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
531 if (factory == NULL) return E_OUTOFMEMORY;
533 ICOM_INIT_INTERFACE(factory, IClassFactory, DDCF_Vtbl);
534 factory->ref = 1;
536 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
538 *ppv = ICOM_INTERFACE(factory, IClassFactory);
539 return S_OK;
543 /*******************************************************************************
544 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
546 * RETURNS
547 * Success: S_OK
548 * Failure: S_FALSE
550 DWORD WINAPI DDRAW_DllCanUnloadNow(void) {
551 FIXME("(void): stub\n");
552 return S_FALSE;
555 /******************************************************************************
556 * Initialisation
559 /* Choose which driver is considered the primary display driver. It will
560 * be created when we get a NULL guid for the DirectDrawCreate(Ex). */
561 static int DDRAW_ChooseDefaultDriver(void)
563 int i;
564 int best = 0;
565 int best_score = 0;
567 assert(DDRAW_num_drivers > 0);
569 /* This algorithm is really stupid. */
570 for (i=0; i < DDRAW_num_drivers; i++)
572 if (DDRAW_drivers[i]->preference > best_score)
574 best_score = DDRAW_drivers[i]->preference;
575 best = i;
579 assert(best_score > 0);
581 return best;
584 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
586 /* If we were sufficiently cool, DDraw drivers would just be COM
587 * objects, registered with a particular component category. */
589 DDRAW_HAL_Init(hInstDLL, fdwReason, lpv);
590 DDRAW_User_Init(hInstDLL, fdwReason, lpv);
592 if (fdwReason == DLL_PROCESS_ATTACH)
594 HMODULE mod;
596 DisableThreadLibraryCalls(hInstDLL);
598 mod = GetModuleHandleA( "x11drv.dll" );
599 if (mod)
601 wine_tsx11_lock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
602 wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
604 #ifdef HAVE_OPENGL
605 opengl_initialized = DDRAW_bind_to_opengl();
606 #endif /* HAVE_OPENGL */
609 if (DDRAW_num_drivers > 0)
610 DDRAW_default_driver = DDRAW_ChooseDefaultDriver();
612 return TRUE;
615 /* Register a direct draw driver. This should be called from your init
616 * function. (That's why there is no locking: your init func is called from
617 * our DllInit, which is serialised.) */
618 void DDRAW_register_driver(const ddraw_driver *driver)
620 int i;
622 for (i = 0; i < DDRAW_num_drivers; i++)
624 if (DDRAW_drivers[i] == driver)
626 ERR("Driver reregistering %p\n", driver);
627 return;
631 if (DDRAW_num_drivers == sizeof(DDRAW_drivers)/sizeof(DDRAW_drivers[0]))
633 ERR("too many DDRAW drivers\n");
634 return;
637 DDRAW_drivers[DDRAW_num_drivers++] = driver;
640 /* This totally doesn't belong here. */
641 LONG DDRAW_width_bpp_to_pitch(DWORD width, DWORD bpp)
643 LONG pitch;
645 assert(bpp != 0); /* keeps happening... */
647 if (bpp == 15) bpp = 16;
648 pitch = width * (bpp / 8);
649 return pitch + (8 - (pitch % 8)) % 8;