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
28 #include "wine/port.h"
30 #define DDRAW_INIT_GUID
31 #include "ddraw_private.h"
34 #include "wine/exception.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
39 /* The configured default surface */
40 WINED3DSURFTYPE DefaultSurfaceType = SURFACE_UNKNOWN;
42 typeof(WineDirect3DCreateClipper) *pWineDirect3DCreateClipper DECLSPEC_HIDDEN;
43 typeof(WineDirect3DCreate) *pWineDirect3DCreate DECLSPEC_HIDDEN;
45 /* DDraw list and critical section */
46 static struct list global_ddraw_list = LIST_INIT(global_ddraw_list);
48 static CRITICAL_SECTION_DEBUG ddraw_cs_debug =
51 { &ddraw_cs_debug.ProcessLocksList,
52 &ddraw_cs_debug.ProcessLocksList },
53 0, 0, { (DWORD_PTR)(__FILE__ ": ddraw_cs") }
55 CRITICAL_SECTION ddraw_cs = { &ddraw_cs_debug, -1, 0, 0, 0, 0 };
57 static HINSTANCE instance;
59 /* value of ForceRefreshRate */
60 DWORD force_refresh_rate = 0;
62 /* Handle table functions */
63 BOOL ddraw_handle_table_init(struct ddraw_handle_table *t, UINT initial_size)
65 t->entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, initial_size * sizeof(*t->entries));
68 ERR("Failed to allocate handle table memory.\n");
71 t->free_entries = NULL;
72 t->table_size = initial_size;
78 void ddraw_handle_table_destroy(struct ddraw_handle_table *t)
80 HeapFree(GetProcessHeap(), 0, t->entries);
81 memset(t, 0, sizeof(*t));
84 DWORD ddraw_allocate_handle(struct ddraw_handle_table *t, void *object, enum ddraw_handle_type type)
86 struct ddraw_handle_entry *entry;
90 DWORD idx = t->free_entries - t->entries;
91 /* Use a free handle */
92 entry = t->free_entries;
93 if (entry->type != DDRAW_HANDLE_FREE)
95 ERR("Handle %#x (%p) is in the free list, but has type %#x.\n", idx, entry->object, entry->type);
96 return DDRAW_INVALID_HANDLE;
98 t->free_entries = entry->object;
99 entry->object = object;
105 if (!(t->entry_count < t->table_size))
108 UINT new_size = t->table_size + (t->table_size >> 1);
109 struct ddraw_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
110 0, t->entries, new_size * sizeof(*t->entries));
113 ERR("Failed to grow the handle table.\n");
114 return DDRAW_INVALID_HANDLE;
116 t->entries = new_entries;
117 t->table_size = new_size;
120 entry = &t->entries[t->entry_count];
121 entry->object = object;
124 return t->entry_count++;
127 void *ddraw_free_handle(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
129 struct ddraw_handle_entry *entry;
132 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
134 WARN("Invalid handle %#x passed.\n", handle);
138 entry = &t->entries[handle];
139 if (entry->type != type)
141 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
145 object = entry->object;
146 entry->object = t->free_entries;
147 entry->type = DDRAW_HANDLE_FREE;
148 t->free_entries = entry;
153 void *ddraw_get_object(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
155 struct ddraw_handle_entry *entry;
157 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
159 WARN("Invalid handle %#x passed.\n", handle);
163 entry = &t->entries[handle];
164 if (entry->type != type)
166 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
170 return entry->object;
174 * Helper Function for DDRAW_Create and DirectDrawCreateClipper for
175 * lazy loading of the Wine D3D driver.
182 BOOL LoadWineD3D(void)
184 static HMODULE hWineD3D = (HMODULE) -1;
185 if (hWineD3D == (HMODULE) -1)
187 hWineD3D = LoadLibraryA("wined3d");
190 pWineDirect3DCreate = (typeof(WineDirect3DCreate) *)GetProcAddress(hWineD3D, "WineDirect3DCreate");
191 pWineDirect3DCreateClipper = (typeof(WineDirect3DCreateClipper) *) GetProcAddress(hWineD3D, "WineDirect3DCreateClipper");
195 return hWineD3D != NULL;
198 /***********************************************************************
200 * Helper function for DirectDrawCreate and friends
201 * Creates a new DDraw interface with the given REFIID
203 * Interfaces that can be created:
204 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
205 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
206 * IDirect3D interfaces?)
209 * guid: ID of the requested driver, NULL for the default driver.
210 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
211 * DD: Used to return the pointer to the created object
212 * UnkOuter: For aggregation, which is unsupported. Must be NULL
213 * iid: requested version ID.
216 * DD_OK if the Interface was created successfully
217 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
218 * E_OUTOFMEMORY if some allocation failed
220 ***********************************************************************/
222 DDRAW_Create(const GUID *guid,
227 WINED3DDEVTYPE devicetype;
228 IDirectDrawImpl *This;
231 TRACE("driver_guid %s, ddraw %p, outer_unknown %p, interface_iid %s.\n",
232 debugstr_guid(guid), DD, UnkOuter, debugstr_guid(iid));
236 /* We don't care about this guids. Well, there's no special guid anyway
239 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
241 /* Use the reference device id. This doesn't actually change anything,
242 * WineD3D always uses OpenGL for D3D rendering. One could make it request
245 devicetype = WINED3DDEVTYPE_REF;
247 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
249 devicetype = WINED3DDEVTYPE_HAL;
256 /* DDraw doesn't support aggregation, according to msdn */
257 if (UnkOuter != NULL)
258 return CLASS_E_NOAGGREGATION;
260 /* DirectDraw creation comes here */
261 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawImpl));
264 ERR("Out of memory when creating DirectDraw\n");
265 return E_OUTOFMEMORY;
268 hr = ddraw_init(This, devicetype);
271 WARN("Failed to initialize ddraw object, hr %#x.\n", hr);
272 HeapFree(GetProcessHeap(), 0, This);
276 hr = IDirectDraw7_QueryInterface(&This->IDirectDraw7_iface, iid, DD);
277 IDirectDraw7_Release(&This->IDirectDraw7_iface);
278 if (SUCCEEDED(hr)) list_add_head(&global_ddraw_list, &This->ddraw_list_entry);
279 else WARN("Failed to query interface %s from ddraw object %p.\n", debugstr_guid(iid), This);
284 /***********************************************************************
285 * DirectDrawCreate (DDRAW.@)
287 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
288 * interfaces in theory
290 * Arguments, return values: See DDRAW_Create
292 ***********************************************************************/
293 HRESULT WINAPI DECLSPEC_HOTPATCH
294 DirectDrawCreate(GUID *GUID,
300 TRACE("driver_guid %s, ddraw %p, outer_unknown %p.\n",
301 debugstr_guid(GUID), DD, UnkOuter);
303 EnterCriticalSection(&ddraw_cs);
304 hr = DDRAW_Create(GUID, (void **) DD, UnkOuter, &IID_IDirectDraw);
305 LeaveCriticalSection(&ddraw_cs);
309 /***********************************************************************
310 * DirectDrawCreateEx (DDRAW.@)
312 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
313 * interfaces are requested.
315 * Arguments, return values: See DDRAW_Create
317 ***********************************************************************/
318 HRESULT WINAPI DECLSPEC_HOTPATCH
319 DirectDrawCreateEx(GUID *GUID,
326 TRACE("driver_guid %s, ddraw %p, interface_iid %s, outer_unknown %p.\n",
327 debugstr_guid(GUID), DD, debugstr_guid(iid), UnkOuter);
329 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
330 return DDERR_INVALIDPARAMS;
332 EnterCriticalSection(&ddraw_cs);
333 hr = DDRAW_Create(GUID, DD, UnkOuter, iid);
334 LeaveCriticalSection(&ddraw_cs);
338 /***********************************************************************
339 * DirectDrawEnumerateA (DDRAW.@)
341 * Enumerates legacy ddraw drivers, ascii version. We only have one
342 * driver, which relays to WineD3D. If we were sufficiently cool,
343 * we could offer various interfaces, which use a different default surface
344 * implementation, but I think it's better to offer this choice in
345 * winecfg, because some apps use the default driver, so we would need
346 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
349 * Callback: Callback function from the app
350 * Context: Argument to the call back.
354 * E_INVALIDARG if the Callback caused a page fault
357 ***********************************************************************/
358 HRESULT WINAPI DirectDrawEnumerateA(LPDDENUMCALLBACKA Callback, void *Context)
360 TRACE("callback %p, context %p.\n", Callback, Context);
362 TRACE(" Enumerating default DirectDraw HAL interface\n");
363 /* We only have one driver */
366 static CHAR driver_desc[] = "DirectDraw HAL",
367 driver_name[] = "display";
369 Callback(NULL, driver_desc, driver_name, Context);
373 return DDERR_INVALIDPARAMS;
377 TRACE(" End of enumeration\n");
381 /***********************************************************************
382 * DirectDrawEnumerateExA (DDRAW.@)
384 * Enumerates DirectDraw7 drivers, ascii version. See
385 * the comments above DirectDrawEnumerateA for more details.
387 * The Flag member is not supported right now.
389 ***********************************************************************/
390 HRESULT WINAPI DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA Callback, void *Context, DWORD Flags)
392 TRACE("callback %p, context %p, flags %#x.\n", Callback, Context, Flags);
394 if (Flags & ~(DDENUM_ATTACHEDSECONDARYDEVICES |
395 DDENUM_DETACHEDSECONDARYDEVICES |
396 DDENUM_NONDISPLAYDEVICES))
397 return DDERR_INVALIDPARAMS;
400 FIXME("flags 0x%08x not handled\n", Flags);
402 TRACE("Enumerating default DirectDraw HAL interface\n");
404 /* We only have one driver by now */
407 static CHAR driver_desc[] = "DirectDraw HAL",
408 driver_name[] = "display";
410 /* QuickTime expects the description "DirectDraw HAL" */
411 Callback(NULL, driver_desc, driver_name, Context, 0);
415 return DDERR_INVALIDPARAMS;
419 TRACE("End of enumeration\n");
423 /***********************************************************************
424 * DirectDrawEnumerateW (DDRAW.@)
426 * Enumerates legacy drivers, unicode version.
427 * This function is not implemented on Windows.
429 ***********************************************************************/
430 HRESULT WINAPI DirectDrawEnumerateW(LPDDENUMCALLBACKW callback, void *context)
432 TRACE("callback %p, context %p.\n", callback, context);
435 return DDERR_INVALIDPARAMS;
437 return DDERR_UNSUPPORTED;
440 /***********************************************************************
441 * DirectDrawEnumerateExW (DDRAW.@)
443 * Enumerates DirectDraw7 drivers, unicode version.
444 * This function is not implemented on Windows.
446 ***********************************************************************/
447 HRESULT WINAPI DirectDrawEnumerateExW(LPDDENUMCALLBACKEXW callback, void *context, DWORD flags)
449 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
451 return DDERR_UNSUPPORTED;
454 /***********************************************************************
455 * Classfactory implementation.
456 ***********************************************************************/
458 /***********************************************************************
459 * CF_CreateDirectDraw
461 * DDraw creation function for the class factory
464 * UnkOuter: Set to NULL
465 * iid: ID of the wanted interface
466 * obj: Address to pass the interface pointer back
469 * DD_OK / DDERR*, see DDRAW_Create
471 ***********************************************************************/
473 CF_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
478 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(iid), obj);
480 EnterCriticalSection(&ddraw_cs);
481 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
482 LeaveCriticalSection(&ddraw_cs);
486 /***********************************************************************
487 * CF_CreateDirectDraw
489 * Clipper creation function for the class factory
492 * UnkOuter: Set to NULL
493 * iid: ID of the wanted interface
494 * obj: Address to pass the interface pointer back
497 * DD_OK / DDERR*, see DDRAW_Create
499 ***********************************************************************/
501 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
505 IDirectDrawClipper *Clip;
507 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(riid), obj);
509 EnterCriticalSection(&ddraw_cs);
510 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
513 LeaveCriticalSection(&ddraw_cs);
517 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
518 IDirectDrawClipper_Release(Clip);
520 LeaveCriticalSection(&ddraw_cs);
524 static const struct object_creation_info object_creation[] =
526 { &CLSID_DirectDraw, CF_CreateDirectDraw },
527 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
528 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
532 /******************************************************************************
533 * DirectDraw ClassFactory implementation
534 ******************************************************************************/
537 IClassFactory IClassFactory_iface;
540 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID iid, LPVOID *ppObj);
543 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
545 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
548 /*******************************************************************************
549 * IDirectDrawClassFactory::QueryInterface
551 * QueryInterface for the class factory
554 * riid Reference to identifier of queried interface
555 * ppv Address to return the interface pointer at
559 * Failure: E_NOINTERFACE
561 *******************************************************************************/
562 static HRESULT WINAPI IDirectDrawClassFactoryImpl_QueryInterface(IClassFactory *iface, REFIID riid,
565 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
567 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
569 if (IsEqualGUID(riid, &IID_IUnknown)
570 || IsEqualGUID(riid, &IID_IClassFactory))
572 IClassFactory_AddRef(iface);
577 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),obj);
578 return E_NOINTERFACE;
581 /*******************************************************************************
582 * IDirectDrawClassFactory::AddRef
584 * AddRef for the class factory
589 *******************************************************************************/
590 static ULONG WINAPI IDirectDrawClassFactoryImpl_AddRef(IClassFactory *iface)
592 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
593 ULONG ref = InterlockedIncrement(&This->ref);
595 TRACE("%p increasing refcount to %u.\n", This, ref);
600 /*******************************************************************************
601 * IDirectDrawClassFactory::Release
603 * Release for the class factory. If the refcount falls to 0, the object
609 *******************************************************************************/
610 static ULONG WINAPI IDirectDrawClassFactoryImpl_Release(IClassFactory *iface)
612 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
613 ULONG ref = InterlockedDecrement(&This->ref);
615 TRACE("%p decreasing refcount to %u.\n", This, ref);
618 HeapFree(GetProcessHeap(), 0, This);
624 /*******************************************************************************
625 * IDirectDrawClassFactory::CreateInstance
627 * What is this? Seems to create DirectDraw objects...
630 * The usual things???
635 *******************************************************************************/
636 static HRESULT WINAPI IDirectDrawClassFactoryImpl_CreateInstance(IClassFactory *iface,
637 IUnknown *UnkOuter, REFIID riid, void **obj)
639 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
641 TRACE("iface %p, outer_unknown %p, riid %s, object %p.\n",
642 iface, UnkOuter, debugstr_guid(riid), obj);
644 return This->pfnCreateInstance(UnkOuter, riid, obj);
647 /*******************************************************************************
648 * IDirectDrawClassFactory::LockServer
656 * S_OK, because it's a stub
658 *******************************************************************************/
659 static HRESULT WINAPI IDirectDrawClassFactoryImpl_LockServer(IClassFactory *iface, BOOL dolock)
661 FIXME("iface %p, dolock %#x stub!\n", iface, dolock);
666 /*******************************************************************************
667 * The class factory VTable
668 *******************************************************************************/
669 static const IClassFactoryVtbl IClassFactory_Vtbl =
671 IDirectDrawClassFactoryImpl_QueryInterface,
672 IDirectDrawClassFactoryImpl_AddRef,
673 IDirectDrawClassFactoryImpl_Release,
674 IDirectDrawClassFactoryImpl_CreateInstance,
675 IDirectDrawClassFactoryImpl_LockServer
678 /*******************************************************************************
679 * DllGetClassObject [DDRAW.@]
680 * Retrieves class object from a DLL object
683 * Docs say returns STDAPI
686 * rclsid [I] CLSID for the class object
687 * riid [I] Reference to identifier of interface for class object
688 * ppv [O] Address of variable to receive interface pointer for riid
692 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
695 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
698 IClassFactoryImpl *factory;
700 TRACE("rclsid %s, riid %s, object %p.\n",
701 debugstr_guid(rclsid), debugstr_guid(riid), ppv);
703 if (!IsEqualGUID(&IID_IClassFactory, riid)
704 && !IsEqualGUID(&IID_IUnknown, riid))
705 return E_NOINTERFACE;
707 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
709 if (IsEqualGUID(object_creation[i].clsid, rclsid))
713 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
715 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
716 return CLASS_E_CLASSNOTAVAILABLE;
719 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
720 if (factory == NULL) return E_OUTOFMEMORY;
722 factory->IClassFactory_iface.lpVtbl = &IClassFactory_Vtbl;
725 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
732 /*******************************************************************************
733 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
739 HRESULT WINAPI DllCanUnloadNow(void)
747 /***********************************************************************
748 * DllRegisterServer (DDRAW.@)
750 HRESULT WINAPI DllRegisterServer(void)
752 return __wine_register_resources( instance, NULL );
755 /***********************************************************************
756 * DllUnregisterServer (DDRAW.@)
758 HRESULT WINAPI DllUnregisterServer(void)
760 return __wine_unregister_resources( instance, NULL );
763 /*******************************************************************************
766 * Callback function for the EnumSurfaces call in DllMain.
767 * Dumps some surface info and releases the surface
770 * surf: The enumerated surface
771 * desc: it's description
772 * context: Pointer to the ddraw impl
776 *******************************************************************************/
777 static HRESULT WINAPI
778 DestroyCallback(IDirectDrawSurface7 *surf,
779 DDSURFACEDESC2 *desc,
782 IDirectDrawSurfaceImpl *Impl = (IDirectDrawSurfaceImpl *)surf;
785 ref = IDirectDrawSurface7_Release(surf); /* For the EnumSurfaces */
786 WARN("Surface %p has an reference count of %d\n", Impl, ref);
788 /* Skip surfaces which are attached somewhere or which are
789 * part of a complex compound. They will get released when destroying
792 if( (!Impl->is_complex_root) || (Impl->first_attached != Impl) )
795 /* Destroy the surface */
796 while(ref) ref = IDirectDrawSurface7_Release(surf);
801 /***********************************************************************
804 * Reads a config key from the registry. Taken from WineD3D
806 ***********************************************************************/
807 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
809 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
810 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
811 return ERROR_FILE_NOT_FOUND;
814 /***********************************************************************
817 * Could be used to register DirectDraw drivers, if we have more than
818 * one. Also used to destroy any objects left at unload if the
819 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
821 ***********************************************************************/
823 DllMain(HINSTANCE hInstDLL,
827 TRACE("(%p,%x,%p)\n", hInstDLL, Reason, lpv);
828 if (Reason == DLL_PROCESS_ATTACH)
830 char buffer[MAX_PATH+10];
831 DWORD size = sizeof(buffer);
837 /* Register the window class. This is used to create a hidden window
838 * for D3D rendering, if the application didn't pass one. It can also
839 * be used for creating a device window from SetCooperativeLevel(). */
840 wc.style = CS_HREDRAW | CS_VREDRAW;
841 wc.lpfnWndProc = DefWindowProcA;
844 wc.hInstance = hInstDLL;
847 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
848 wc.lpszMenuName = NULL;
849 wc.lpszClassName = DDRAW_WINDOW_CLASS_NAME;
850 if (!RegisterClassA(&wc))
852 ERR("Failed to register ddraw window class, last error %#x.\n", GetLastError());
856 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
857 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
859 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
860 if (len && len < MAX_PATH)
863 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
864 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
866 char *p, *appname = buffer;
867 if ((p = strrchr( appname, '/' ))) appname = p + 1;
868 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
869 strcat( appname, "\\Direct3D" );
870 TRACE("appname = [%s]\n", appname);
871 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
872 RegCloseKey( tmpkey );
876 if ( 0 != hkey || 0 != appkey )
878 if ( !get_config_key( hkey, appkey, "DirectDrawRenderer", buffer, size) )
880 if (!strcmp(buffer,"gdi"))
882 TRACE("Defaulting to GDI surfaces\n");
883 DefaultSurfaceType = SURFACE_GDI;
885 else if (!strcmp(buffer,"opengl"))
887 TRACE("Defaulting to opengl surfaces\n");
888 DefaultSurfaceType = SURFACE_OPENGL;
892 ERR("Unknown default surface type. Supported are:\n gdi, opengl\n");
897 /* On Windows one can force the refresh rate that DirectDraw uses by
898 * setting an override value in dxdiag. This is documented in KB315614
899 * (main article), KB230002, and KB217348. By comparing registry dumps
900 * before and after setting the override, we see that the override value
901 * is stored in HKLM\Software\Microsoft\DirectDraw\ForceRefreshRate as a
902 * DWORD that represents the refresh rate to force. We use this
903 * registry entry to modify the behavior of SetDisplayMode so that Wine
904 * users can override the refresh rate in a Windows-compatible way.
906 * dxdiag will not accept a refresh rate lower than 40 or higher than
907 * 120 so this value should be within that range. It is, of course,
908 * possible for a user to set the registry entry value directly so that
909 * assumption might not hold.
911 * There is no current mechanism for setting this value through the Wine
912 * GUI. It would be most appropriate to set this value through a dxdiag
913 * clone, but it may be sufficient to use winecfg.
915 * TODO: Create a mechanism for setting this value through the Wine GUI.
917 if ( !RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectDraw", &hkey ) )
921 if (!RegQueryValueExA( hkey, "ForceRefreshRate", NULL, &type, (LPBYTE)&data, &size ) && type == REG_DWORD)
923 TRACE("ForceRefreshRate set; overriding refresh rate to %d Hz\n", data);
924 force_refresh_rate = data;
930 DisableThreadLibraryCalls(hInstDLL);
932 else if (Reason == DLL_PROCESS_DETACH)
934 if(!list_empty(&global_ddraw_list))
936 struct list *entry, *entry2;
937 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
939 /* We remove elements from this loop */
940 LIST_FOR_EACH_SAFE(entry, entry2, &global_ddraw_list)
945 IDirectDrawImpl *ddraw = LIST_ENTRY(entry, IDirectDrawImpl, ddraw_list_entry);
947 WARN("DDraw %p has a refcount of %d\n", ddraw, ddraw->ref7 + ddraw->ref4 + ddraw->ref3 + ddraw->ref2 + ddraw->ref1);
949 /* Add references to each interface to avoid freeing them unexpectedly */
950 IDirectDraw_AddRef(&ddraw->IDirectDraw_iface);
951 IDirectDraw2_AddRef(&ddraw->IDirectDraw2_iface);
952 IDirectDraw3_AddRef(&ddraw->IDirectDraw3_iface);
953 IDirectDraw4_AddRef(&ddraw->IDirectDraw4_iface);
954 IDirectDraw7_AddRef(&ddraw->IDirectDraw7_iface);
956 /* Does a D3D device exist? Destroy it
957 * TODO: Destroy all Vertex buffers, Lights, Materials
958 * and execute buffers too
962 WARN("DDraw %p has d3ddevice %p attached\n", ddraw, ddraw->d3ddevice);
963 while(IDirect3DDevice7_Release((IDirect3DDevice7 *)ddraw->d3ddevice));
966 /* Try to release the objects
967 * Do an EnumSurfaces to find any hanging surfaces
969 memset(&desc, 0, sizeof(desc));
970 desc.dwSize = sizeof(desc);
971 for(i = 0; i <= 1; i++)
973 hr = IDirectDraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, DDENUMSURFACES_ALL,
974 &desc, ddraw, DestroyCallback);
976 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw);
979 /* Check the surface count */
980 if(ddraw->surfaces > 0)
981 ERR("DDraw %p still has %d surfaces attached\n", ddraw, ddraw->surfaces);
983 /* Release all hanging references to destroy the objects. This
984 * restores the screen mode too
986 while(IDirectDraw_Release(&ddraw->IDirectDraw_iface));
987 while(IDirectDraw2_Release(&ddraw->IDirectDraw2_iface));
988 while(IDirectDraw3_Release(&ddraw->IDirectDraw3_iface));
989 while(IDirectDraw4_Release(&ddraw->IDirectDraw4_iface));
990 while(IDirectDraw7_Release(&ddraw->IDirectDraw7_iface));
994 /* Unregister the window class. */
995 UnregisterClassA(DDRAW_WINDOW_CLASS_NAME, hInstDLL);