ddraw: Set ddraw caps in wined3d.
[wine/gsoc_dplay.git] / dlls / ddraw / main.c
blob5f04feadb2ebf8f8f0458ba6115db01f8c20adfd
1 /* DirectDraw Base Functions
3 * Copyright 1997-1999 Marcus Meissner
4 * Copyright 1998 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2006 Stefan Dösinger
7 * Copyright 2008 Denver Gingerich
9 * This file contains the (internal) driver registration functions,
10 * driver enumeration APIs and DirectDraw creation functions.
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
29 #include "wine/debug.h"
31 #include <assert.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <stdlib.h>
36 #define COBJMACROS
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winerror.h"
41 #include "wingdi.h"
42 #include "wine/exception.h"
43 #include "winreg.h"
45 #include "ddraw.h"
46 #include "d3d.h"
48 #include "ddraw_private.h"
50 typedef IWineD3D* (WINAPI *fnWineDirect3DCreate)(UINT, UINT, IUnknown *);
52 static fnWineDirect3DCreate pWineDirect3DCreate;
54 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
56 /* The configured default surface */
57 WINED3DSURFTYPE DefaultSurfaceType = SURFACE_UNKNOWN;
59 /* DDraw list and critical section */
60 static struct list global_ddraw_list = LIST_INIT(global_ddraw_list);
62 static CRITICAL_SECTION_DEBUG ddraw_cs_debug =
64 0, 0, &ddraw_cs,
65 { &ddraw_cs_debug.ProcessLocksList,
66 &ddraw_cs_debug.ProcessLocksList },
67 0, 0, { (DWORD_PTR)(__FILE__ ": ddraw_cs") }
69 CRITICAL_SECTION ddraw_cs = { &ddraw_cs_debug, -1, 0, 0, 0, 0 };
71 /* value of ForceRefreshRate */
72 DWORD force_refresh_rate = 0;
75 * Helper Function for DDRAW_Create and DirectDrawCreateClipper for
76 * lazy loading of the Wine D3D driver.
78 * Returns
79 * TRUE on success
80 * FALSE on failure.
83 BOOL LoadWineD3D(void)
85 static HMODULE hWineD3D = (HMODULE) -1;
86 if (hWineD3D == (HMODULE) -1)
88 hWineD3D = LoadLibraryA("wined3d");
89 if (hWineD3D)
91 pWineDirect3DCreate = (fnWineDirect3DCreate) GetProcAddress(hWineD3D, "WineDirect3DCreate");
92 pWineDirect3DCreateClipper = (fnWineDirect3DCreateClipper) GetProcAddress(hWineD3D, "WineDirect3DCreateClipper");
93 return TRUE;
96 return hWineD3D != NULL;
99 /***********************************************************************
101 * Helper function for DirectDrawCreate and friends
102 * Creates a new DDraw interface with the given REFIID
104 * Interfaces that can be created:
105 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
106 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
107 * IDirect3D interfaces?)
109 * Arguments:
110 * guid: ID of the requested driver, NULL for the default driver.
111 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
112 * DD: Used to return the pointer to the created object
113 * UnkOuter: For aggregation, which is unsupported. Must be NULL
114 * iid: requested version ID.
116 * Returns:
117 * DD_OK if the Interface was created successfully
118 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
119 * E_OUTOFMEMORY if some allocation failed
121 ***********************************************************************/
122 static HRESULT
123 DDRAW_Create(const GUID *guid,
124 void **DD,
125 IUnknown *UnkOuter,
126 REFIID iid)
128 IDirectDrawImpl *This = NULL;
129 HRESULT hr;
130 IWineD3D *wineD3D = NULL;
131 IWineD3DDevice *wineD3DDevice = NULL;
132 HDC hDC;
133 WINED3DDEVTYPE devicetype;
135 TRACE("(%s,%p,%p)\n", debugstr_guid(guid), DD, UnkOuter);
137 *DD = NULL;
139 /* We don't care about this guids. Well, there's no special guid anyway
140 * OK, we could
142 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
144 /* Use the reference device id. This doesn't actually change anything,
145 * WineD3D always uses OpenGL for D3D rendering. One could make it request
146 * indirect rendering
148 devicetype = WINED3DDEVTYPE_REF;
150 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
152 devicetype = WINED3DDEVTYPE_HAL;
154 else
156 devicetype = 0;
159 /* DDraw doesn't support aggregation, according to msdn */
160 if (UnkOuter != NULL)
161 return CLASS_E_NOAGGREGATION;
163 /* DirectDraw creation comes here */
164 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawImpl));
165 if(!This)
167 ERR("Out of memory when creating DirectDraw\n");
168 return E_OUTOFMEMORY;
171 /* The interfaces:
172 * IDirectDraw and IDirect3D are the same object,
173 * QueryInterface is used to get other interfaces.
175 ICOM_INIT_INTERFACE(This, IDirectDraw, IDirectDraw1_Vtbl);
176 ICOM_INIT_INTERFACE(This, IDirectDraw2, IDirectDraw2_Vtbl);
177 ICOM_INIT_INTERFACE(This, IDirectDraw3, IDirectDraw3_Vtbl);
178 ICOM_INIT_INTERFACE(This, IDirectDraw4, IDirectDraw4_Vtbl);
179 ICOM_INIT_INTERFACE(This, IDirectDraw7, IDirectDraw7_Vtbl);
180 ICOM_INIT_INTERFACE(This, IDirect3D, IDirect3D1_Vtbl);
181 ICOM_INIT_INTERFACE(This, IDirect3D2, IDirect3D2_Vtbl);
182 ICOM_INIT_INTERFACE(This, IDirect3D3, IDirect3D3_Vtbl);
183 ICOM_INIT_INTERFACE(This, IDirect3D7, IDirect3D7_Vtbl);
185 /* See comments in IDirectDrawImpl_CreateNewSurface for a description
186 * of this member.
187 * Read from a registry key, should add a winecfg option later
189 This->ImplType = DefaultSurfaceType;
191 /* Get the current screen settings */
192 hDC = GetDC(0);
193 This->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
194 ReleaseDC(0, hDC);
195 This->orig_width = GetSystemMetrics(SM_CXSCREEN);
196 This->orig_height = GetSystemMetrics(SM_CYSCREEN);
198 if (!LoadWineD3D())
200 ERR("Couldn't load WineD3D - OpenGL libs not present?\n");
201 hr = DDERR_NODIRECTDRAWSUPPORT;
202 goto err_out;
205 /* Initialize WineD3D
207 * All Rendering (2D and 3D) is relayed to WineD3D,
208 * but DirectDraw specific management, like DDSURFACEDESC and DDPIXELFORMAT
209 * structure handling is handled in this lib.
211 wineD3D = pWineDirect3DCreate(0 /* SDKVersion */, 7 /* DXVersion */, (IUnknown *) This /* Parent */);
212 if(!wineD3D)
214 ERR("Failed to initialise WineD3D\n");
215 hr = E_OUTOFMEMORY;
216 goto err_out;
218 This->wineD3D = wineD3D;
219 TRACE("WineD3D created at %p\n", wineD3D);
221 /* Initialized member...
223 * It is set to false at creation time, and set to true in
224 * IDirectDraw7::Initialize. Its sole purpose is to return DD_OK on
225 * initialize only once
227 This->initialized = FALSE;
229 /* Initialize WineD3DDevice
231 * It is used for screen setup, surface and palette creation
232 * When a Direct3DDevice7 is created, the D3D capabilities of WineD3D are
233 * initialized
235 hr = IWineD3D_CreateDevice(wineD3D,
236 0 /*D3D_ADAPTER_DEFAULT*/,
237 devicetype,
238 NULL, /* FocusWindow, don't know yet */
239 0, /* BehaviorFlags */
240 &wineD3DDevice,
241 (IUnknown *) ICOM_INTERFACE(This, IDirectDraw7));
242 if(FAILED(hr))
244 ERR("Failed to create a wineD3DDevice, result = %x\n", hr);
245 goto err_out;
247 This->wineD3DDevice = wineD3DDevice;
248 TRACE("wineD3DDevice created at %p\n", This->wineD3DDevice);
250 /* Register the window class
252 * It is used to create a hidden window for D3D
253 * rendering, if the application didn't pass one.
254 * It can also be used for Creating a device window
255 * from SetCooperativeLevel
257 * The name: DDRAW_<address>. The classname is
258 * 32 bit long, so a 64 bit address will fit nicely
259 * (Will this be compiled for 64 bit anyway?)
262 sprintf(This->classname, "DDRAW_%p", This);
264 memset(&This->wnd_class, 0, sizeof(This->wnd_class));
265 This->wnd_class.style = CS_HREDRAW | CS_VREDRAW;
266 This->wnd_class.lpfnWndProc = DefWindowProcA;
267 This->wnd_class.cbClsExtra = 0;
268 This->wnd_class.cbWndExtra = 0;
269 This->wnd_class.hInstance = GetModuleHandleA(0);
270 This->wnd_class.hIcon = 0;
271 This->wnd_class.hCursor = 0;
272 This->wnd_class.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
273 This->wnd_class.lpszMenuName = NULL;
274 This->wnd_class.lpszClassName = This->classname;
275 if(!RegisterClassA(&This->wnd_class))
277 ERR("RegisterClassA failed!\n");
278 goto err_out;
281 /* Get the amount of video memory */
282 This->total_vidmem = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
284 list_init(&This->surface_list);
285 list_add_head(&global_ddraw_list, &This->ddraw_list_entry);
287 /* Call QueryInterface to get the pointer to the requested interface. This also initializes
288 * The required refcount
290 hr = IDirectDraw7_QueryInterface( ICOM_INTERFACE(This, IDirectDraw7), iid, DD);
291 if(SUCCEEDED(hr)) return DD_OK;
293 err_out:
294 /* Let's hope we never need this ;) */
295 if(wineD3DDevice) IWineD3DDevice_Release(wineD3DDevice);
296 if(wineD3D) IWineD3D_Release(wineD3D);
297 if(This) HeapFree(GetProcessHeap(), 0, This->decls);
298 HeapFree(GetProcessHeap(), 0, This);
299 return hr;
302 /***********************************************************************
303 * DirectDrawCreate (DDRAW.@)
305 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
306 * interfaces in theory
308 * Arguments, return values: See DDRAW_Create
310 ***********************************************************************/
311 HRESULT WINAPI
312 DirectDrawCreate(GUID *GUID,
313 LPDIRECTDRAW *DD,
314 IUnknown *UnkOuter)
316 HRESULT hr;
317 TRACE("(%s,%p,%p)\n", debugstr_guid(GUID), DD, UnkOuter);
319 EnterCriticalSection(&ddraw_cs);
320 hr = DDRAW_Create(GUID, (void **) DD, UnkOuter, &IID_IDirectDraw);
321 LeaveCriticalSection(&ddraw_cs);
322 return hr;
325 /***********************************************************************
326 * DirectDrawCreateEx (DDRAW.@)
328 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
329 * interfaces are requested.
331 * Arguments, return values: See DDRAW_Create
333 ***********************************************************************/
334 HRESULT WINAPI
335 DirectDrawCreateEx(GUID *GUID,
336 LPVOID *DD,
337 REFIID iid,
338 IUnknown *UnkOuter)
340 HRESULT hr;
341 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(GUID), DD, debugstr_guid(iid), UnkOuter);
343 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
344 return DDERR_INVALIDPARAMS;
346 EnterCriticalSection(&ddraw_cs);
347 hr = DDRAW_Create(GUID, DD, UnkOuter, iid);
348 LeaveCriticalSection(&ddraw_cs);
349 return hr;
352 /***********************************************************************
353 * DirectDrawEnumerateA (DDRAW.@)
355 * Enumerates legacy ddraw drivers, ascii version. We only have one
356 * driver, which relays to WineD3D. If we were sufficiently cool,
357 * we could offer various interfaces, which use a different default surface
358 * implementation, but I think it's better to offer this choice in
359 * winecfg, because some apps use the default driver, so we would need
360 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
362 * Arguments:
363 * Callback: Callback function from the app
364 * Context: Argument to the call back.
366 * Returns:
367 * DD_OK on success
368 * E_INVALIDARG if the Callback caused a page fault
371 ***********************************************************************/
372 HRESULT WINAPI
373 DirectDrawEnumerateA(LPDDENUMCALLBACKA Callback,
374 LPVOID Context)
376 BOOL stop = FALSE;
378 TRACE(" Enumerating default DirectDraw HAL interface\n");
379 /* We only have one driver */
380 __TRY
382 static CHAR driver_desc[] = "DirectDraw HAL",
383 driver_name[] = "display";
385 stop = !Callback(NULL, driver_desc, driver_name, Context);
387 __EXCEPT_PAGE_FAULT
389 return E_INVALIDARG;
391 __ENDTRY
393 TRACE(" End of enumeration\n");
394 return DD_OK;
397 /***********************************************************************
398 * DirectDrawEnumerateExA (DDRAW.@)
400 * Enumerates DirectDraw7 drivers, ascii version. See
401 * the comments above DirectDrawEnumerateA for more details.
403 * The Flag member is not supported right now.
405 ***********************************************************************/
406 HRESULT WINAPI
407 DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA Callback,
408 LPVOID Context,
409 DWORD Flags)
411 BOOL stop = FALSE;
412 TRACE("Enumerating default DirectDraw HAL interface\n");
414 /* We only have one driver by now */
415 __TRY
417 static CHAR driver_desc[] = "DirectDraw HAL",
418 driver_name[] = "display";
420 /* QuickTime expects the description "DirectDraw HAL" */
421 stop = !Callback(NULL, driver_desc, driver_name, Context, 0);
423 __EXCEPT_PAGE_FAULT
425 return E_INVALIDARG;
427 __ENDTRY;
429 TRACE("End of enumeration\n");
430 return DD_OK;
433 /***********************************************************************
434 * DirectDrawEnumerateW (DDRAW.@)
436 * Enumerates legacy drivers, unicode version. See
437 * the comments above DirectDrawEnumerateA for more details.
439 * The Flag member is not supported right now.
441 ***********************************************************************/
443 /***********************************************************************
444 * DirectDrawEnumerateExW (DDRAW.@)
446 * Enumerates DirectDraw7 drivers, unicode version. See
447 * the comments above DirectDrawEnumerateA for more details.
449 * The Flag member is not supported right now.
451 ***********************************************************************/
453 /***********************************************************************
454 * Classfactory implementation.
455 ***********************************************************************/
457 /***********************************************************************
458 * CF_CreateDirectDraw
460 * DDraw creation function for the class factory
462 * Params:
463 * UnkOuter: Set to NULL
464 * iid: ID of the wanted interface
465 * obj: Address to pass the interface pointer back
467 * Returns
468 * DD_OK / DDERR*, see DDRAW_Create
470 ***********************************************************************/
471 static HRESULT
472 CF_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
473 void **obj)
475 HRESULT hr;
477 TRACE("(%p,%s,%p)\n", UnkOuter, debugstr_guid(iid), obj);
479 EnterCriticalSection(&ddraw_cs);
480 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
481 LeaveCriticalSection(&ddraw_cs);
482 return hr;
485 /***********************************************************************
486 * CF_CreateDirectDraw
488 * Clipper creation function for the class factory
490 * Params:
491 * UnkOuter: Set to NULL
492 * iid: ID of the wanted interface
493 * obj: Address to pass the interface pointer back
495 * Returns
496 * DD_OK / DDERR*, see DDRAW_Create
498 ***********************************************************************/
499 static HRESULT
500 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
501 void **obj)
503 HRESULT hr;
504 IDirectDrawClipper *Clip;
506 EnterCriticalSection(&ddraw_cs);
507 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
508 if (hr != DD_OK)
510 LeaveCriticalSection(&ddraw_cs);
511 return hr;
514 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
515 IDirectDrawClipper_Release(Clip);
517 LeaveCriticalSection(&ddraw_cs);
518 return hr;
521 static const struct object_creation_info object_creation[] =
523 { &CLSID_DirectDraw, CF_CreateDirectDraw },
524 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
525 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
528 /*******************************************************************************
529 * IDirectDrawClassFactory::QueryInterface
531 * QueryInterface for the class factory
533 * PARAMS
534 * riid Reference to identifier of queried interface
535 * ppv Address to return the interface pointer at
537 * RETURNS
538 * Success: S_OK
539 * Failure: E_NOINTERFACE
541 *******************************************************************************/
542 static HRESULT WINAPI
543 IDirectDrawClassFactoryImpl_QueryInterface(IClassFactory *iface,
544 REFIID riid,
545 void **obj)
547 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
549 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obj);
551 if (IsEqualGUID(riid, &IID_IUnknown)
552 || IsEqualGUID(riid, &IID_IClassFactory))
554 IClassFactory_AddRef(iface);
555 *obj = This;
556 return S_OK;
559 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),obj);
560 return E_NOINTERFACE;
563 /*******************************************************************************
564 * IDirectDrawClassFactory::AddRef
566 * AddRef for the class factory
568 * RETURNS
569 * The new refcount
571 *******************************************************************************/
572 static ULONG WINAPI
573 IDirectDrawClassFactoryImpl_AddRef(IClassFactory *iface)
575 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
576 ULONG ref = InterlockedIncrement(&This->ref);
578 TRACE("(%p)->() incrementing from %d.\n", This, ref - 1);
580 return ref;
583 /*******************************************************************************
584 * IDirectDrawClassFactory::Release
586 * Release for the class factory. If the refcount falls to 0, the object
587 * is destroyed
589 * RETURNS
590 * The new refcount
592 *******************************************************************************/
593 static ULONG WINAPI
594 IDirectDrawClassFactoryImpl_Release(IClassFactory *iface)
596 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
597 ULONG ref = InterlockedDecrement(&This->ref);
598 TRACE("(%p)->() decrementing from %d.\n", This, ref+1);
600 if (ref == 0)
601 HeapFree(GetProcessHeap(), 0, This);
603 return ref;
607 /*******************************************************************************
608 * IDirectDrawClassFactory::CreateInstance
610 * What is this? Seems to create DirectDraw objects...
612 * Params
613 * The usual things???
615 * RETURNS
616 * ???
618 *******************************************************************************/
619 static HRESULT WINAPI
620 IDirectDrawClassFactoryImpl_CreateInstance(IClassFactory *iface,
621 IUnknown *UnkOuter,
622 REFIID riid,
623 void **obj)
625 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
627 TRACE("(%p)->(%p,%s,%p)\n",This,UnkOuter,debugstr_guid(riid),obj);
629 return This->pfnCreateInstance(UnkOuter, riid, obj);
632 /*******************************************************************************
633 * IDirectDrawClassFactory::LockServer
635 * What is this?
637 * Params
638 * ???
640 * RETURNS
641 * S_OK, because it's a stub
643 *******************************************************************************/
644 static HRESULT WINAPI
645 IDirectDrawClassFactoryImpl_LockServer(IClassFactory *iface,BOOL dolock)
647 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
648 FIXME("(%p)->(%d),stub!\n",This,dolock);
649 return S_OK;
652 /*******************************************************************************
653 * The class factory VTable
654 *******************************************************************************/
655 static const IClassFactoryVtbl IClassFactory_Vtbl =
657 IDirectDrawClassFactoryImpl_QueryInterface,
658 IDirectDrawClassFactoryImpl_AddRef,
659 IDirectDrawClassFactoryImpl_Release,
660 IDirectDrawClassFactoryImpl_CreateInstance,
661 IDirectDrawClassFactoryImpl_LockServer
664 /*******************************************************************************
665 * DllGetClassObject [DDRAW.@]
666 * Retrieves class object from a DLL object
668 * NOTES
669 * Docs say returns STDAPI
671 * PARAMS
672 * rclsid [I] CLSID for the class object
673 * riid [I] Reference to identifier of interface for class object
674 * ppv [O] Address of variable to receive interface pointer for riid
676 * RETURNS
677 * Success: S_OK
678 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
679 * E_UNEXPECTED
681 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
683 unsigned int i;
684 IClassFactoryImpl *factory;
686 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
688 if ( !IsEqualGUID( &IID_IClassFactory, riid )
689 && ! IsEqualGUID( &IID_IUnknown, riid) )
690 return E_NOINTERFACE;
692 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
694 if (IsEqualGUID(object_creation[i].clsid, rclsid))
695 break;
698 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
700 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
701 return CLASS_E_CLASSNOTAVAILABLE;
704 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
705 if (factory == NULL) return E_OUTOFMEMORY;
707 ICOM_INIT_INTERFACE(factory, IClassFactory, IClassFactory_Vtbl);
708 factory->ref = 1;
710 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
712 *ppv = ICOM_INTERFACE(factory, IClassFactory);
713 return S_OK;
717 /*******************************************************************************
718 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
720 * RETURNS
721 * Success: S_OK
722 * Failure: S_FALSE
724 HRESULT WINAPI DllCanUnloadNow(void)
726 HRESULT hr;
727 FIXME("(void): stub\n");
729 EnterCriticalSection(&ddraw_cs);
730 hr = S_FALSE;
731 LeaveCriticalSection(&ddraw_cs);
733 return hr;
736 /*******************************************************************************
737 * DestroyCallback
739 * Callback function for the EnumSurfaces call in DllMain.
740 * Dumps some surface info and releases the surface
742 * Params:
743 * surf: The enumerated surface
744 * desc: it's description
745 * context: Pointer to the ddraw impl
747 * Returns:
748 * DDENUMRET_OK;
749 *******************************************************************************/
750 static HRESULT WINAPI
751 DestroyCallback(IDirectDrawSurface7 *surf,
752 DDSURFACEDESC2 *desc,
753 void *context)
755 IDirectDrawSurfaceImpl *Impl = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surf);
756 IDirectDrawImpl *ddraw = (IDirectDrawImpl *) context;
757 ULONG ref;
759 ref = IDirectDrawSurface7_Release(surf); /* For the EnumSurfaces */
760 WARN("Surface %p has an reference count of %d\n", Impl, ref);
762 /* Skip surfaces which are attached somewhere or which are
763 * part of a complex compound. They will get released when destroying
764 * the root
766 if( (!Impl->is_complex_root) || (Impl->first_attached != Impl) )
767 return DDENUMRET_OK;
768 /* Skip our depth stencil surface, it will be released with the render target */
769 if( Impl == ddraw->DepthStencilBuffer)
770 return DDENUMRET_OK;
772 /* Destroy the surface */
773 while(ref) ref = IDirectDrawSurface7_Release(surf);
775 return DDENUMRET_OK;
778 /***********************************************************************
779 * get_config_key
781 * Reads a config key from the registry. Taken from WineD3D
783 ***********************************************************************/
784 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
786 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
787 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
788 return ERROR_FILE_NOT_FOUND;
791 /***********************************************************************
792 * DllMain (DDRAW.0)
794 * Could be used to register DirectDraw drivers, if we have more than
795 * one. Also used to destroy any objects left at unload if the
796 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
798 ***********************************************************************/
799 BOOL WINAPI
800 DllMain(HINSTANCE hInstDLL,
801 DWORD Reason,
802 LPVOID lpv)
804 TRACE("(%p,%x,%p)\n", hInstDLL, Reason, lpv);
805 if (Reason == DLL_PROCESS_ATTACH)
807 char buffer[MAX_PATH+10];
808 DWORD size = sizeof(buffer);
809 HKEY hkey = 0;
810 HKEY appkey = 0;
811 DWORD len;
813 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
814 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
816 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
817 if (len && len < MAX_PATH)
819 HKEY tmpkey;
820 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
821 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
823 char *p, *appname = buffer;
824 if ((p = strrchr( appname, '/' ))) appname = p + 1;
825 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
826 strcat( appname, "\\Direct3D" );
827 TRACE("appname = [%s]\n", appname);
828 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
829 RegCloseKey( tmpkey );
833 if ( 0 != hkey || 0 != appkey )
835 if ( !get_config_key( hkey, appkey, "DirectDrawRenderer", buffer, size) )
837 if (!strcmp(buffer,"gdi"))
839 TRACE("Defaulting to GDI surfaces\n");
840 DefaultSurfaceType = SURFACE_GDI;
842 else if (!strcmp(buffer,"opengl"))
844 TRACE("Defaulting to opengl surfaces\n");
845 DefaultSurfaceType = SURFACE_OPENGL;
847 else
849 ERR("Unknown default surface type. Supported are:\n gdi, opengl\n");
854 /* On Windows one can force the refresh rate that DirectDraw uses by
855 * setting an override value in dxdiag. This is documented in KB315614
856 * (main article), KB230002, and KB217348. By comparing registry dumps
857 * before and after setting the override, we see that the override value
858 * is stored in HKLM\Software\Microsoft\DirectDraw\ForceRefreshRate as a
859 * DWORD that represents the refresh rate to force. We use this
860 * registry entry to modify the behavior of SetDisplayMode so that Wine
861 * users can override the refresh rate in a Windows-compatible way.
863 * dxdiag will not accept a refresh rate lower than 40 or higher than
864 * 120 so this value should be within that range. It is, of course,
865 * possible for a user to set the registry entry value directly so that
866 * assumption might not hold.
868 * There is no current mechanism for setting this value through the Wine
869 * GUI. It would be most appropriate to set this value through a dxdiag
870 * clone, but it may be sufficient to use winecfg.
872 * TODO: Create a mechanism for setting this value through the Wine GUI.
874 if ( !RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectDraw", &hkey ) )
876 DWORD type, data;
877 size = sizeof(data);
878 if (!RegQueryValueExA( hkey, "ForceRefreshRate", NULL, &type, (LPBYTE)&data, &size ) && type == REG_DWORD)
880 TRACE("ForceRefreshRate set; overriding refresh rate to %d Hz\n", data);
881 force_refresh_rate = data;
883 RegCloseKey( hkey );
886 DisableThreadLibraryCalls(hInstDLL);
888 else if (Reason == DLL_PROCESS_DETACH)
890 if(!list_empty(&global_ddraw_list))
892 struct list *entry, *entry2;
893 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
895 /* We remove elements from this loop */
896 LIST_FOR_EACH_SAFE(entry, entry2, &global_ddraw_list)
898 HRESULT hr;
899 DDSURFACEDESC2 desc;
900 int i;
901 IDirectDrawImpl *ddraw = LIST_ENTRY(entry, IDirectDrawImpl, ddraw_list_entry);
903 WARN("DDraw %p has a refcount of %d\n", ddraw, ddraw->ref7 + ddraw->ref4 + ddraw->ref3 + ddraw->ref2 + ddraw->ref1);
905 /* Add references to each interface to avoid freeing them unexpectedly */
906 IDirectDraw_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw));
907 IDirectDraw2_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw2));
908 IDirectDraw3_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw3));
909 IDirectDraw4_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw4));
910 IDirectDraw7_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw7));
912 /* Does a D3D device exist? Destroy it
913 * TODO: Destroy all Vertex buffers, Lights, Materials
914 * and execute buffers too
916 if(ddraw->d3ddevice)
918 WARN("DDraw %p has d3ddevice %p attached\n", ddraw, ddraw->d3ddevice);
919 while(IDirect3DDevice7_Release(ICOM_INTERFACE(ddraw->d3ddevice, IDirect3DDevice7)));
922 /* Try to release the objects
923 * Do an EnumSurfaces to find any hanging surfaces
925 memset(&desc, 0, sizeof(desc));
926 desc.dwSize = sizeof(desc);
927 for(i = 0; i <= 1; i++)
929 hr = IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(ddraw, IDirectDraw7),
930 DDENUMSURFACES_ALL,
931 &desc,
932 (void *) ddraw,
933 DestroyCallback);
934 if(hr != D3D_OK)
935 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw);
938 /* Check the surface count */
939 if(ddraw->surfaces > 0)
940 ERR("DDraw %p still has %d surfaces attached\n", ddraw, ddraw->surfaces);
942 /* Release all hanging references to destroy the objects. This
943 * restores the screen mode too
945 while(IDirectDraw_Release(ICOM_INTERFACE(ddraw, IDirectDraw)));
946 while(IDirectDraw2_Release(ICOM_INTERFACE(ddraw, IDirectDraw2)));
947 while(IDirectDraw3_Release(ICOM_INTERFACE(ddraw, IDirectDraw3)));
948 while(IDirectDraw4_Release(ICOM_INTERFACE(ddraw, IDirectDraw4)));
949 while(IDirectDraw7_Release(ICOM_INTERFACE(ddraw, IDirectDraw7)));
954 return TRUE;