MSVC fixes.
[wine/wine64.git] / dlls / ddraw / main.c
blob74e272bf4208a7e5ea3fb3f8afe4f8da87c6b78c
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 <string.h>
31 #include <stdlib.h>
33 #include "winnls.h"
34 #include "winerror.h"
36 #include "ddraw.h"
37 #include "d3d.h"
39 /* This for all the enumeration and creation of D3D-related objects */
40 #include "ddraw_private.h"
41 #include "wine/debug.h"
42 #include "wine/library.h"
43 #include "wine/port.h"
45 #include "gl_private.h"
47 #undef GLPRIVATE_NO_REDEFINE
49 #define MAX_DDRAW_DRIVERS 3
50 static const ddraw_driver* DDRAW_drivers[MAX_DDRAW_DRIVERS];
51 static int DDRAW_num_drivers; /* = 0 */
52 static int DDRAW_default_driver;
54 void (*wine_tsx11_lock_ptr)(void) = NULL;
55 void (*wine_tsx11_unlock_ptr)(void) = NULL;
57 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
59 /**********************************************************************/
61 typedef struct {
62 LPVOID lpCallback;
63 LPVOID lpContext;
64 } DirectDrawEnumerateProcData;
66 BOOL opengl_initialized = 0;
68 #ifdef HAVE_OPENGL
70 static void *gl_handle = NULL;
72 #define GL_API_FUNCTION(f) typeof(f) * p##f;
73 #include "gl_api.h"
74 #undef GL_API_FUNCTION
76 #ifndef SONAME_LIBGL
77 #define SONAME_LIBGL "libGL.so"
78 #endif
80 static BOOL DDRAW_bind_to_opengl( void )
82 char *glname = SONAME_LIBGL;
84 gl_handle = wine_dlopen(glname, RTLD_NOW, NULL, 0);
85 if (!gl_handle) {
86 WARN("Wine cannot find the OpenGL graphics library (%s).\n",glname);
87 return FALSE;
90 #define GL_API_FUNCTION(f) \
91 if((p##f = wine_dlsym(gl_handle, #f, NULL, 0)) == NULL) \
92 { \
93 WARN("Can't find symbol %s\n", #f); \
94 goto sym_not_found; \
96 #include "gl_api.h"
97 #undef GL_API_FUNCTION
99 return TRUE;
101 sym_not_found:
102 WARN("Wine cannot find certain functions that it needs inside the OpenGL\n"
103 "graphics library. To enable Wine to use OpenGL please upgrade\n"
104 "your OpenGL libraries\n");
105 wine_dlclose(gl_handle, NULL, 0);
106 gl_handle = NULL;
107 return FALSE;
110 #endif /* HAVE_OPENGL */
112 /***********************************************************************
113 * DirectDrawEnumerateExA (DDRAW.@)
115 HRESULT WINAPI DirectDrawEnumerateExA(
116 LPDDENUMCALLBACKEXA lpCallback, LPVOID lpContext, DWORD dwFlags)
118 int i;
119 TRACE("(%p,%p, %08lx)\n", lpCallback, lpContext, dwFlags);
121 if (TRACE_ON(ddraw)) {
122 TRACE(" Flags : ");
123 if (dwFlags & DDENUM_ATTACHEDSECONDARYDEVICES)
124 TRACE("DDENUM_ATTACHEDSECONDARYDEVICES ");
125 if (dwFlags & DDENUM_DETACHEDSECONDARYDEVICES)
126 TRACE("DDENUM_DETACHEDSECONDARYDEVICES ");
127 if (dwFlags & DDENUM_NONDISPLAYDEVICES)
128 TRACE("DDENUM_NONDISPLAYDEVICES ");
129 TRACE("\n");
132 for (i=0; i<DDRAW_num_drivers; i++)
134 TRACE("Enumerating %s/%s interface\n",
135 DDRAW_drivers[i]->info->szDriver,
136 DDRAW_drivers[i]->info->szDescription);
138 /* We have to pass NULL from the primary display device.
139 * RoadRage chapter 6's enumeration routine expects it. */
140 if (!lpCallback((DDRAW_default_driver == i) ? NULL
141 :(LPGUID)&DDRAW_drivers[i]->info->guidDeviceIdentifier,
142 (LPSTR)DDRAW_drivers[i]->info->szDescription,
143 (LPSTR)DDRAW_drivers[i]->info->szDriver,
144 lpContext, 0))
145 return DD_OK;
148 /* Unsupported flags */
149 if (dwFlags & DDENUM_NONDISPLAYDEVICES) {
150 FIXME("no non-display devices supported.\n");
152 if (dwFlags & DDENUM_DETACHEDSECONDARYDEVICES) {
153 FIXME("no detached secondary devices supported.\n");
156 return DD_OK;
159 /***********************************************************************
160 * DirectDrawEnumerateExW (DDRAW.@)
163 static BOOL CALLBACK DirectDrawEnumerateExProcW(
164 GUID *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName,
165 LPVOID lpContext, HMONITOR hm)
167 INT len;
168 BOOL bResult;
169 LPWSTR lpDriverDescriptionW, lpDriverNameW;
170 DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
172 len = MultiByteToWideChar( CP_ACP, 0, lpDriverDescription, -1, NULL, 0 );
173 lpDriverDescriptionW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
174 MultiByteToWideChar( CP_ACP, 0, lpDriverDescription, -1, lpDriverDescriptionW, len );
176 len = MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, NULL, 0 );
177 lpDriverNameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
178 MultiByteToWideChar( CP_ACP, 0, lpDriverName, -1, lpDriverNameW, len );
180 bResult = (*(LPDDENUMCALLBACKEXW *) pEPD->lpCallback)(lpGUID, lpDriverDescriptionW,
181 lpDriverNameW, pEPD->lpContext, hm);
183 HeapFree(GetProcessHeap(), 0, lpDriverDescriptionW);
184 HeapFree(GetProcessHeap(), 0, lpDriverNameW);
185 return bResult;
188 HRESULT WINAPI DirectDrawEnumerateExW(
189 LPDDENUMCALLBACKEXW lpCallback, LPVOID lpContext, DWORD dwFlags)
191 DirectDrawEnumerateProcData epd;
192 epd.lpCallback = (LPVOID) lpCallback;
193 epd.lpContext = lpContext;
195 return DirectDrawEnumerateExA(DirectDrawEnumerateExProcW, (LPVOID) &epd, 0);
198 /***********************************************************************
199 * DirectDrawEnumerateA (DDRAW.@)
202 static BOOL CALLBACK DirectDrawEnumerateProcA(
203 GUID *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName,
204 LPVOID lpContext, HMONITOR hm)
206 DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
208 return ((LPDDENUMCALLBACKA) pEPD->lpCallback)(
209 lpGUID, lpDriverDescription, lpDriverName, pEPD->lpContext);
212 HRESULT WINAPI DirectDrawEnumerateA(
213 LPDDENUMCALLBACKA lpCallback, LPVOID lpContext)
215 DirectDrawEnumerateProcData epd;
216 epd.lpCallback = (LPVOID) lpCallback;
217 epd.lpContext = lpContext;
219 return DirectDrawEnumerateExA(DirectDrawEnumerateProcA, (LPVOID) &epd, 0);
222 /***********************************************************************
223 * DirectDrawEnumerateW (DDRAW.@)
226 static BOOL WINAPI DirectDrawEnumerateProcW(
227 GUID *lpGUID, LPWSTR lpDriverDescription, LPWSTR lpDriverName,
228 LPVOID lpContext, HMONITOR hm)
230 DirectDrawEnumerateProcData *pEPD = (DirectDrawEnumerateProcData*)lpContext;
232 return ((LPDDENUMCALLBACKW) pEPD->lpCallback)(
233 lpGUID, lpDriverDescription, lpDriverName, pEPD->lpContext);
236 HRESULT WINAPI DirectDrawEnumerateW(
237 LPDDENUMCALLBACKW lpCallback, LPVOID lpContext)
239 DirectDrawEnumerateProcData epd;
240 epd.lpCallback = (LPVOID) lpCallback;
241 epd.lpContext = lpContext;
243 return DirectDrawEnumerateExW(DirectDrawEnumerateProcW, (LPVOID) &epd, 0);
246 /***********************************************************************
247 * DirectDrawCreate (DDRAW.@)
250 const ddraw_driver* DDRAW_FindDriver(const GUID* pGUID)
252 static const GUID zeroGUID; /* gets zero-inited */
254 TRACE("(%s)\n", pGUID ? debugstr_guid(pGUID) : "(null)");
256 if (DDRAW_num_drivers == 0) return NULL;
258 if (pGUID == (LPGUID)DDCREATE_EMULATIONONLY
259 || pGUID == (LPGUID)DDCREATE_HARDWAREONLY)
260 pGUID = NULL;
262 if (pGUID == NULL || memcmp(pGUID, &zeroGUID, sizeof(GUID)) == 0)
264 /* Use the default driver. */
265 return DDRAW_drivers[DDRAW_default_driver];
267 else
269 /* Look for a matching GUID. */
271 int i;
272 for (i=0; i < DDRAW_num_drivers; i++)
274 if (IsEqualGUID(pGUID,
275 &DDRAW_drivers[i]->info->guidDeviceIdentifier))
276 break;
279 if (i < DDRAW_num_drivers)
281 return DDRAW_drivers[i];
283 else
285 ERR("(%s): did not recognize requested GUID.\n",debugstr_guid(pGUID));
286 return NULL;
291 static HRESULT DDRAW_Create(
292 LPGUID lpGUID, LPVOID *lplpDD, LPUNKNOWN pUnkOuter, REFIID iid, BOOL ex
294 const ddraw_driver* driver;
295 LPDIRECTDRAW7 pDD;
296 HRESULT hr;
298 TRACE("(%s,%p,%p,%d)\n", debugstr_guid(lpGUID), lplpDD, pUnkOuter, ex);
300 if (DDRAW_num_drivers == 0)
302 WARN("no DirectDraw drivers registered\n");
303 return DDERR_INVALIDDIRECTDRAWGUID;
306 if (lpGUID == (LPGUID)DDCREATE_EMULATIONONLY
307 || lpGUID == (LPGUID)DDCREATE_HARDWAREONLY)
308 lpGUID = NULL;
310 if (pUnkOuter != NULL)
311 return DDERR_INVALIDPARAMS; /* CLASS_E_NOAGGREGATION? */
313 driver = DDRAW_FindDriver(lpGUID);
314 if (driver == NULL) return DDERR_INVALIDDIRECTDRAWGUID;
316 hr = driver->create(lpGUID, &pDD, pUnkOuter, ex);
317 if (FAILED(hr)) return hr;
319 hr = IDirectDraw7_QueryInterface(pDD, iid, lplpDD);
320 IDirectDraw7_Release(pDD);
321 return hr;
324 /***********************************************************************
325 * DirectDrawCreate (DDRAW.@)
327 * Only creates legacy IDirectDraw interfaces.
328 * Cannot create IDirectDraw7 interfaces.
329 * In theory.
331 HRESULT WINAPI DirectDrawCreate(
332 LPGUID lpGUID, LPDIRECTDRAW* lplpDD, LPUNKNOWN pUnkOuter
334 TRACE("(%s,%p,%p)\n", debugstr_guid(lpGUID), lplpDD, pUnkOuter);
335 return DDRAW_Create(lpGUID, (LPVOID*) lplpDD, pUnkOuter, &IID_IDirectDraw, FALSE);
338 /***********************************************************************
339 * DirectDrawCreateEx (DDRAW.@)
341 * Only creates new IDirectDraw7 interfaces.
342 * Supposed to fail if legacy interfaces are requested.
343 * In theory.
345 HRESULT WINAPI DirectDrawCreateEx(
346 LPGUID lpGUID, LPVOID* lplpDD, REFIID iid, LPUNKNOWN pUnkOuter
348 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(lpGUID), lplpDD, debugstr_guid(iid), pUnkOuter);
350 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
351 return DDERR_INVALIDPARAMS;
353 return DDRAW_Create(lpGUID, lplpDD, pUnkOuter, iid, TRUE);
356 extern HRESULT Uninit_DirectDraw_Create(const GUID*, LPDIRECTDRAW7*,
357 LPUNKNOWN, BOOL);
359 /* This is for the class factory. */
360 static HRESULT DDRAW_CreateDirectDraw(IUnknown* pUnkOuter, REFIID iid,
361 LPVOID* ppObj)
363 LPDIRECTDRAW7 pDD;
364 HRESULT hr;
365 BOOL ex;
367 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppObj);
369 /* This is a mighty hack :-) */
370 if (IsEqualGUID(iid, &IID_IDirectDraw7))
371 ex = TRUE;
372 else
373 ex = FALSE;
375 hr = Uninit_DirectDraw_Create(NULL, &pDD, pUnkOuter, ex);
376 if (FAILED(hr)) return hr;
378 hr = IDirectDraw7_QueryInterface(pDD, iid, ppObj);
379 IDirectDraw_Release(pDD);
380 return hr;
383 /******************************************************************************
384 * DirectDraw ClassFactory
386 typedef struct {
387 ICOM_VFIELD_MULTI(IClassFactory);
389 DWORD ref;
390 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID iid,
391 LPVOID *ppObj);
392 } IClassFactoryImpl;
394 struct object_creation_info
396 const CLSID *clsid;
397 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID riid,
398 LPVOID *ppObj);
401 /* There should be more, but these are the only ones listed in the header
402 * file. */
403 extern HRESULT DDRAW_CreateDirectDrawClipper(IUnknown *pUnkOuter, REFIID riid,
404 LPVOID *ppObj);
406 static const struct object_creation_info object_creation[] =
408 { &CLSID_DirectDraw, DDRAW_CreateDirectDraw },
409 { &CLSID_DirectDraw7, DDRAW_CreateDirectDraw },
410 { &CLSID_DirectDrawClipper, DDRAW_CreateDirectDrawClipper }
413 static HRESULT WINAPI
414 DDCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
416 ICOM_THIS(IClassFactoryImpl,iface);
418 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj);
420 if (IsEqualGUID(riid, &IID_IUnknown)
421 || IsEqualGUID(riid, &IID_IClassFactory))
423 IClassFactory_AddRef(iface);
424 *ppobj = This;
425 return S_OK;
428 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
429 return E_NOINTERFACE;
432 static ULONG WINAPI DDCF_AddRef(LPCLASSFACTORY iface) {
433 ICOM_THIS(IClassFactoryImpl,iface);
435 TRACE("(%p)->() incrementing from %ld.\n", This, This->ref);
437 return ++(This->ref);
440 static ULONG WINAPI DDCF_Release(LPCLASSFACTORY iface) {
441 ICOM_THIS(IClassFactoryImpl,iface);
443 TRACE("(%p)->() decrementing from %ld.\n", This, This->ref);
445 if (--This->ref == 0)
446 HeapFree(GetProcessHeap(), 0, This);
448 return This->ref;
452 static HRESULT WINAPI DDCF_CreateInstance(
453 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
455 ICOM_THIS(IClassFactoryImpl,iface);
457 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
459 return This->pfnCreateInstance(pOuter, riid, ppobj);
462 static HRESULT WINAPI DDCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
463 ICOM_THIS(IClassFactoryImpl,iface);
464 FIXME("(%p)->(%d),stub!\n",This,dolock);
465 return S_OK;
468 static ICOM_VTABLE(IClassFactory) DDCF_Vtbl =
470 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
471 DDCF_QueryInterface,
472 DDCF_AddRef,
473 DDCF_Release,
474 DDCF_CreateInstance,
475 DDCF_LockServer
478 /*******************************************************************************
479 * DllGetClassObject [DDRAW.@]
480 * Retrieves class object from a DLL object
482 * NOTES
483 * Docs say returns STDAPI
485 * PARAMS
486 * rclsid [I] CLSID for the class object
487 * riid [I] Reference to identifier of interface for class object
488 * ppv [O] Address of variable to receive interface pointer for riid
490 * RETURNS
491 * Success: S_OK
492 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
493 * E_UNEXPECTED
495 DWORD WINAPI DDRAW_DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID *ppv)
497 unsigned int i;
498 IClassFactoryImpl *factory;
500 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
502 if ( !IsEqualGUID( &IID_IClassFactory, riid )
503 && ! IsEqualGUID( &IID_IUnknown, riid) )
504 return E_NOINTERFACE;
506 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
508 if (IsEqualGUID(object_creation[i].clsid, rclsid))
509 break;
512 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
514 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
515 return CLASS_E_CLASSNOTAVAILABLE;
518 factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
519 if (factory == NULL) return E_OUTOFMEMORY;
521 ICOM_INIT_INTERFACE(factory, IClassFactory, DDCF_Vtbl);
522 factory->ref = 1;
524 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
526 *ppv = ICOM_INTERFACE(factory, IClassFactory);
527 return S_OK;
531 /*******************************************************************************
532 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
534 * RETURNS
535 * Success: S_OK
536 * Failure: S_FALSE
538 DWORD WINAPI DDRAW_DllCanUnloadNow(void) {
539 FIXME("(void): stub\n");
540 return S_FALSE;
543 /******************************************************************************
544 * Initialisation
547 /* Choose which driver is considered the primary display driver. It will
548 * be created when we get a NULL guid for the DirectDrawCreate(Ex). */
549 static int DDRAW_ChooseDefaultDriver(void)
551 int i;
552 int best = 0;
553 int best_score = 0;
555 assert(DDRAW_num_drivers > 0);
557 /* This algorithm is really stupid. */
558 for (i=0; i < DDRAW_num_drivers; i++)
560 if (DDRAW_drivers[i]->preference > best_score)
562 best_score = DDRAW_drivers[i]->preference;
563 best = i;
567 assert(best_score > 0);
569 return best;
572 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
574 /* If we were sufficiently cool, DDraw drivers would just be COM
575 * objects, registered with a particular component category. */
577 DDRAW_HAL_Init(hInstDLL, fdwReason, lpv);
578 DDRAW_User_Init(hInstDLL, fdwReason, lpv);
580 if (fdwReason == DLL_PROCESS_ATTACH)
582 HMODULE mod;
584 DisableThreadLibraryCalls(hInstDLL);
586 mod = GetModuleHandleA( "x11drv.dll" );
587 if (mod)
589 wine_tsx11_lock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
590 wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
592 #ifdef HAVE_OPENGL
593 opengl_initialized = DDRAW_bind_to_opengl();
594 #endif /* HAVE_OPENGL */
597 if (DDRAW_num_drivers > 0)
598 DDRAW_default_driver = DDRAW_ChooseDefaultDriver();
600 return TRUE;
603 /* Register a direct draw driver. This should be called from your init
604 * function. (That's why there is no locking: your init func is called from
605 * our DllInit, which is serialised.) */
606 void DDRAW_register_driver(const ddraw_driver *driver)
608 int i;
610 for (i = 0; i < DDRAW_num_drivers; i++)
612 if (DDRAW_drivers[i] == driver)
614 ERR("Driver reregistering %p\n", driver);
615 return;
619 if (DDRAW_num_drivers == sizeof(DDRAW_drivers)/sizeof(DDRAW_drivers[0]))
621 ERR("too many DDRAW drivers\n");
622 return;
625 DDRAW_drivers[DDRAW_num_drivers++] = driver;
628 /* This totally doesn't belong here. */
629 LONG DDRAW_width_bpp_to_pitch(DWORD width, DWORD bpp)
631 LONG pitch;
633 assert(bpp != 0); /* keeps happening... */
635 if (bpp == 15) bpp = 16;
636 pitch = width * (bpp / 8);
637 return pitch + (8 - (pitch % 8)) % 8;