d3d8: Get rid of the format switching code in d3d8_device_CopyRects().
[wine.git] / dlls / ddraw / main.c
blob68bf3e30e4cdc398b25d1daa6d93ce941b7c75d5
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"
30 #define DDRAW_INIT_GUID
31 #include "ddraw_private.h"
32 #include "rpcproxy.h"
34 #include "wine/exception.h"
35 #include "winreg.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
39 static struct list global_ddraw_list = LIST_INIT(global_ddraw_list);
41 static HINSTANCE instance;
43 /* value of ForceRefreshRate */
44 DWORD force_refresh_rate = 0;
46 /* Structure for converting DirectDrawEnumerateA to DirectDrawEnumerateExA */
47 struct callback_info
49 LPDDENUMCALLBACKA callback;
50 void *context;
53 /* Enumeration callback for converting DirectDrawEnumerateA to DirectDrawEnumerateExA */
54 static HRESULT CALLBACK enum_callback(GUID *guid, char *description, char *driver_name,
55 void *context, HMONITOR monitor)
57 const struct callback_info *info = context;
59 return info->callback(guid, description, driver_name, info->context);
62 static void ddraw_enumerate_secondary_devices(struct wined3d *wined3d, LPDDENUMCALLBACKEXA callback,
63 void *context)
65 struct wined3d_adapter_identifier adapter_id;
66 BOOL cont_enum = TRUE;
67 HRESULT hr = S_OK;
68 UINT adapter = 0;
70 for (adapter = 0; SUCCEEDED(hr) && cont_enum; adapter++)
72 char DriverName[512] = "", DriverDescription[512] = "";
74 /* The Battle.net System Checker expects the GetAdapterIdentifier DeviceName to match the
75 * Driver Name, so obtain the DeviceName and GUID from D3D. */
76 memset(&adapter_id, 0x0, sizeof(adapter_id));
77 adapter_id.device_name = DriverName;
78 adapter_id.device_name_size = sizeof(DriverName);
79 adapter_id.description = DriverDescription;
80 adapter_id.description_size = sizeof(DriverDescription);
81 wined3d_mutex_lock();
82 hr = wined3d_get_adapter_identifier(wined3d, adapter, 0x0, &adapter_id);
83 wined3d_mutex_unlock();
84 if (SUCCEEDED(hr))
86 TRACE("Interface %d: %s\n", adapter, wine_dbgstr_guid(&adapter_id.device_identifier));
87 cont_enum = callback(&adapter_id.device_identifier, adapter_id.description,
88 adapter_id.device_name, context, wined3d_get_adapter_monitor(wined3d, adapter));
93 /* Handle table functions */
94 BOOL ddraw_handle_table_init(struct ddraw_handle_table *t, UINT initial_size)
96 t->entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, initial_size * sizeof(*t->entries));
97 if (!t->entries)
99 ERR("Failed to allocate handle table memory.\n");
100 return FALSE;
102 t->free_entries = NULL;
103 t->table_size = initial_size;
104 t->entry_count = 0;
106 return TRUE;
109 void ddraw_handle_table_destroy(struct ddraw_handle_table *t)
111 HeapFree(GetProcessHeap(), 0, t->entries);
112 memset(t, 0, sizeof(*t));
115 DWORD ddraw_allocate_handle(struct ddraw_handle_table *t, void *object, enum ddraw_handle_type type)
117 struct ddraw_handle_entry *entry;
119 if (t->free_entries)
121 DWORD idx = t->free_entries - t->entries;
122 /* Use a free handle */
123 entry = t->free_entries;
124 if (entry->type != DDRAW_HANDLE_FREE)
126 ERR("Handle %#x (%p) is in the free list, but has type %#x.\n", idx, entry->object, entry->type);
127 return DDRAW_INVALID_HANDLE;
129 t->free_entries = entry->object;
130 entry->object = object;
131 entry->type = type;
133 return idx;
136 if (!(t->entry_count < t->table_size))
138 /* Grow the table */
139 UINT new_size = t->table_size + (t->table_size >> 1);
140 struct ddraw_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
141 0, t->entries, new_size * sizeof(*t->entries));
142 if (!new_entries)
144 ERR("Failed to grow the handle table.\n");
145 return DDRAW_INVALID_HANDLE;
147 t->entries = new_entries;
148 t->table_size = new_size;
151 entry = &t->entries[t->entry_count];
152 entry->object = object;
153 entry->type = type;
155 return t->entry_count++;
158 void *ddraw_free_handle(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
160 struct ddraw_handle_entry *entry;
161 void *object;
163 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
165 WARN("Invalid handle %#x passed.\n", handle);
166 return NULL;
169 entry = &t->entries[handle];
170 if (entry->type != type)
172 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
173 return NULL;
176 object = entry->object;
177 entry->object = t->free_entries;
178 entry->type = DDRAW_HANDLE_FREE;
179 t->free_entries = entry;
181 return object;
184 void *ddraw_get_object(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
186 struct ddraw_handle_entry *entry;
188 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
190 WARN("Invalid handle %#x passed.\n", handle);
191 return NULL;
194 entry = &t->entries[handle];
195 if (entry->type != type)
197 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
198 return NULL;
201 return entry->object;
204 /***********************************************************************
206 * Helper function for DirectDrawCreate and friends
207 * Creates a new DDraw interface with the given REFIID
209 * Interfaces that can be created:
210 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
211 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
212 * IDirect3D interfaces?)
214 * Arguments:
215 * guid: ID of the requested driver, NULL for the default driver.
216 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
217 * DD: Used to return the pointer to the created object
218 * UnkOuter: For aggregation, which is unsupported. Must be NULL
219 * iid: requested version ID.
221 * Returns:
222 * DD_OK if the Interface was created successfully
223 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
224 * E_OUTOFMEMORY if some allocation failed
226 ***********************************************************************/
227 static HRESULT
228 DDRAW_Create(const GUID *guid,
229 void **DD,
230 IUnknown *UnkOuter,
231 REFIID iid)
233 enum wined3d_device_type device_type;
234 struct ddraw *ddraw;
235 HRESULT hr;
237 TRACE("driver_guid %s, ddraw %p, outer_unknown %p, interface_iid %s.\n",
238 debugstr_guid(guid), DD, UnkOuter, debugstr_guid(iid));
240 *DD = NULL;
242 /* We don't care about this guids. Well, there's no special guid anyway
243 * OK, we could
245 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
247 /* Use the reference device id. This doesn't actually change anything,
248 * WineD3D always uses OpenGL for D3D rendering. One could make it request
249 * indirect rendering
251 device_type = WINED3D_DEVICE_TYPE_REF;
253 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
255 device_type = WINED3D_DEVICE_TYPE_HAL;
257 else
259 device_type = 0;
262 /* DDraw doesn't support aggregation, according to msdn */
263 if (UnkOuter != NULL)
264 return CLASS_E_NOAGGREGATION;
266 /* DirectDraw creation comes here */
267 ddraw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw));
268 if (!ddraw)
270 ERR("Out of memory when creating DirectDraw\n");
271 return E_OUTOFMEMORY;
274 hr = ddraw_init(ddraw, device_type);
275 if (FAILED(hr))
277 WARN("Failed to initialize ddraw object, hr %#x.\n", hr);
278 HeapFree(GetProcessHeap(), 0, ddraw);
279 return hr;
282 hr = IDirectDraw7_QueryInterface(&ddraw->IDirectDraw7_iface, iid, DD);
283 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
284 if (SUCCEEDED(hr))
285 list_add_head(&global_ddraw_list, &ddraw->ddraw_list_entry);
286 else
287 WARN("Failed to query interface %s from ddraw object %p.\n", debugstr_guid(iid), ddraw);
289 return hr;
292 /***********************************************************************
293 * DirectDrawCreate (DDRAW.@)
295 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
296 * interfaces in theory
298 * Arguments, return values: See DDRAW_Create
300 ***********************************************************************/
301 HRESULT WINAPI DECLSPEC_HOTPATCH DirectDrawCreate(GUID *driver_guid, IDirectDraw **ddraw, IUnknown *outer)
303 HRESULT hr;
305 TRACE("driver_guid %s, ddraw %p, outer %p.\n",
306 debugstr_guid(driver_guid), ddraw, outer);
308 wined3d_mutex_lock();
309 hr = DDRAW_Create(driver_guid, (void **)ddraw, outer, &IID_IDirectDraw);
310 wined3d_mutex_unlock();
312 if (SUCCEEDED(hr))
314 if (FAILED(hr = IDirectDraw_Initialize(*ddraw, driver_guid)))
315 IDirectDraw_Release(*ddraw);
318 return hr;
321 /***********************************************************************
322 * DirectDrawCreateEx (DDRAW.@)
324 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
325 * interfaces are requested.
327 * Arguments, return values: See DDRAW_Create
329 ***********************************************************************/
330 HRESULT WINAPI DECLSPEC_HOTPATCH DirectDrawCreateEx(GUID *driver_guid,
331 void **ddraw, REFIID interface_iid, IUnknown *outer)
333 HRESULT hr;
335 TRACE("driver_guid %s, ddraw %p, interface_iid %s, outer %p.\n",
336 debugstr_guid(driver_guid), ddraw, debugstr_guid(interface_iid), outer);
338 if (!IsEqualGUID(interface_iid, &IID_IDirectDraw7))
339 return DDERR_INVALIDPARAMS;
341 wined3d_mutex_lock();
342 hr = DDRAW_Create(driver_guid, ddraw, outer, interface_iid);
343 wined3d_mutex_unlock();
345 if (SUCCEEDED(hr))
347 IDirectDraw7 *ddraw7 = *(IDirectDraw7 **)ddraw;
348 hr = IDirectDraw7_Initialize(ddraw7, driver_guid);
349 if (FAILED(hr))
350 IDirectDraw7_Release(ddraw7);
353 return hr;
356 /***********************************************************************
357 * DirectDrawEnumerateA (DDRAW.@)
359 * Enumerates legacy ddraw drivers, ascii version. We only have one
360 * driver, which relays to WineD3D. If we were sufficiently cool,
361 * we could offer various interfaces, which use a different default surface
362 * implementation, but I think it's better to offer this choice in
363 * winecfg, because some apps use the default driver, so we would need
364 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
366 * Arguments:
367 * Callback: Callback function from the app
368 * Context: Argument to the call back.
370 * Returns:
371 * DD_OK on success
372 * E_INVALIDARG if the Callback caused a page fault
375 ***********************************************************************/
376 HRESULT WINAPI DirectDrawEnumerateA(LPDDENUMCALLBACKA callback, void *context)
378 struct callback_info info;
380 TRACE("callback %p, context %p.\n", callback, context);
382 info.callback = callback;
383 info.context = context;
384 return DirectDrawEnumerateExA(enum_callback, &info, 0x0);
387 /***********************************************************************
388 * DirectDrawEnumerateExA (DDRAW.@)
390 * Enumerates DirectDraw7 drivers, ascii version. See
391 * the comments above DirectDrawEnumerateA for more details.
393 * The Flag member is not supported right now.
395 ***********************************************************************/
396 HRESULT WINAPI DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA callback, void *context, DWORD flags)
398 struct wined3d *wined3d;
400 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
402 if (flags & ~(DDENUM_ATTACHEDSECONDARYDEVICES |
403 DDENUM_DETACHEDSECONDARYDEVICES |
404 DDENUM_NONDISPLAYDEVICES))
405 return DDERR_INVALIDPARAMS;
407 if (flags & ~DDENUM_ATTACHEDSECONDARYDEVICES)
408 FIXME("flags 0x%08x not handled\n", flags & ~DDENUM_ATTACHEDSECONDARYDEVICES);
410 TRACE("Enumerating ddraw interfaces\n");
411 if (!(wined3d = wined3d_create(WINED3D_LEGACY_DEPTH_BIAS)))
413 if (!(wined3d = wined3d_create(WINED3D_LEGACY_DEPTH_BIAS | WINED3D_NO3D)))
415 WARN("Failed to create a wined3d object.\n");
416 return E_FAIL;
419 WARN("Created a wined3d object without 3D support.\n");
422 __TRY
424 /* QuickTime expects the description "DirectDraw HAL" */
425 static CHAR driver_desc[] = "DirectDraw HAL",
426 driver_name[] = "display";
427 BOOL cont_enum;
429 TRACE("Default interface: DirectDraw HAL\n");
430 cont_enum = callback(NULL, driver_desc, driver_name, context, 0);
432 /* The Battle.net System Checker expects both a NULL device and a GUID-based device */
433 if (cont_enum && (flags & ~DDENUM_ATTACHEDSECONDARYDEVICES))
434 ddraw_enumerate_secondary_devices(wined3d, callback, context);
436 __EXCEPT_PAGE_FAULT
438 wined3d_decref(wined3d);
439 return DDERR_INVALIDPARAMS;
441 __ENDTRY;
443 wined3d_decref(wined3d);
444 TRACE("End of enumeration\n");
445 return DD_OK;
448 /***********************************************************************
449 * DirectDrawEnumerateW (DDRAW.@)
451 * Enumerates legacy drivers, unicode version.
452 * This function is not implemented on Windows.
454 ***********************************************************************/
455 HRESULT WINAPI DirectDrawEnumerateW(LPDDENUMCALLBACKW callback, void *context)
457 TRACE("callback %p, context %p.\n", callback, context);
459 if (!callback)
460 return DDERR_INVALIDPARAMS;
461 else
462 return DDERR_UNSUPPORTED;
465 /***********************************************************************
466 * DirectDrawEnumerateExW (DDRAW.@)
468 * Enumerates DirectDraw7 drivers, unicode version.
469 * This function is not implemented on Windows.
471 ***********************************************************************/
472 HRESULT WINAPI DirectDrawEnumerateExW(LPDDENUMCALLBACKEXW callback, void *context, DWORD flags)
474 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
476 return DDERR_UNSUPPORTED;
479 /***********************************************************************
480 * Classfactory implementation.
481 ***********************************************************************/
483 /***********************************************************************
484 * CF_CreateDirectDraw
486 * DDraw creation function for the class factory
488 * Params:
489 * UnkOuter: Set to NULL
490 * iid: ID of the wanted interface
491 * obj: Address to pass the interface pointer back
493 * Returns
494 * DD_OK / DDERR*, see DDRAW_Create
496 ***********************************************************************/
497 static HRESULT
498 CF_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
499 void **obj)
501 HRESULT hr;
503 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(iid), obj);
505 wined3d_mutex_lock();
506 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
507 wined3d_mutex_unlock();
509 return hr;
512 /***********************************************************************
513 * CF_CreateDirectDraw
515 * Clipper creation function for the class factory
517 * Params:
518 * UnkOuter: Set to NULL
519 * iid: ID of the wanted interface
520 * obj: Address to pass the interface pointer back
522 * Returns
523 * DD_OK / DDERR*, see DDRAW_Create
525 ***********************************************************************/
526 static HRESULT
527 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
528 void **obj)
530 HRESULT hr;
531 IDirectDrawClipper *Clip;
533 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(riid), obj);
535 wined3d_mutex_lock();
536 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
537 if (hr != DD_OK)
539 wined3d_mutex_unlock();
540 return hr;
543 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
544 IDirectDrawClipper_Release(Clip);
546 wined3d_mutex_unlock();
548 return hr;
551 static const struct object_creation_info object_creation[] =
553 { &CLSID_DirectDraw, CF_CreateDirectDraw },
554 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
555 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
558 struct ddraw_class_factory
560 IClassFactory IClassFactory_iface;
562 LONG ref;
563 HRESULT (*pfnCreateInstance)(IUnknown *outer, REFIID iid, void **out);
566 static inline struct ddraw_class_factory *impl_from_IClassFactory(IClassFactory *iface)
568 return CONTAINING_RECORD(iface, struct ddraw_class_factory, IClassFactory_iface);
571 /*******************************************************************************
572 * IDirectDrawClassFactory::QueryInterface
574 * QueryInterface for the class factory
576 * PARAMS
577 * riid Reference to identifier of queried interface
578 * ppv Address to return the interface pointer at
580 * RETURNS
581 * Success: S_OK
582 * Failure: E_NOINTERFACE
584 *******************************************************************************/
585 static HRESULT WINAPI ddraw_class_factory_QueryInterface(IClassFactory *iface, REFIID riid, void **out)
587 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
589 if (IsEqualGUID(riid, &IID_IUnknown)
590 || IsEqualGUID(riid, &IID_IClassFactory))
592 IClassFactory_AddRef(iface);
593 *out = iface;
594 return S_OK;
597 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
599 return E_NOINTERFACE;
602 /*******************************************************************************
603 * IDirectDrawClassFactory::AddRef
605 * AddRef for the class factory
607 * RETURNS
608 * The new refcount
610 *******************************************************************************/
611 static ULONG WINAPI ddraw_class_factory_AddRef(IClassFactory *iface)
613 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
614 ULONG ref = InterlockedIncrement(&factory->ref);
616 TRACE("%p increasing refcount to %u.\n", factory, ref);
618 return ref;
621 /*******************************************************************************
622 * IDirectDrawClassFactory::Release
624 * Release for the class factory. If the refcount falls to 0, the object
625 * is destroyed
627 * RETURNS
628 * The new refcount
630 *******************************************************************************/
631 static ULONG WINAPI ddraw_class_factory_Release(IClassFactory *iface)
633 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
634 ULONG ref = InterlockedDecrement(&factory->ref);
636 TRACE("%p decreasing refcount to %u.\n", factory, ref);
638 if (!ref)
639 HeapFree(GetProcessHeap(), 0, factory);
641 return ref;
645 /*******************************************************************************
646 * IDirectDrawClassFactory::CreateInstance
648 * What is this? Seems to create DirectDraw objects...
650 * Params
651 * The usual things???
653 * RETURNS
654 * ???
656 *******************************************************************************/
657 static HRESULT WINAPI ddraw_class_factory_CreateInstance(IClassFactory *iface,
658 IUnknown *outer_unknown, REFIID riid, void **out)
660 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
662 TRACE("iface %p, outer_unknown %p, riid %s, out %p.\n",
663 iface, outer_unknown, debugstr_guid(riid), out);
665 return factory->pfnCreateInstance(outer_unknown, riid, out);
668 /*******************************************************************************
669 * IDirectDrawClassFactory::LockServer
671 * What is this?
673 * Params
674 * ???
676 * RETURNS
677 * S_OK, because it's a stub
679 *******************************************************************************/
680 static HRESULT WINAPI ddraw_class_factory_LockServer(IClassFactory *iface, BOOL dolock)
682 FIXME("iface %p, dolock %#x stub!\n", iface, dolock);
684 return S_OK;
687 /*******************************************************************************
688 * The class factory VTable
689 *******************************************************************************/
690 static const IClassFactoryVtbl IClassFactory_Vtbl =
692 ddraw_class_factory_QueryInterface,
693 ddraw_class_factory_AddRef,
694 ddraw_class_factory_Release,
695 ddraw_class_factory_CreateInstance,
696 ddraw_class_factory_LockServer
699 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **out)
701 struct ddraw_class_factory *factory;
702 unsigned int i;
704 TRACE("rclsid %s, riid %s, out %p.\n",
705 debugstr_guid(rclsid), debugstr_guid(riid), out);
707 if (!IsEqualGUID(&IID_IClassFactory, riid)
708 && !IsEqualGUID(&IID_IUnknown, riid))
709 return E_NOINTERFACE;
711 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
713 if (IsEqualGUID(object_creation[i].clsid, rclsid))
714 break;
717 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
719 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
720 return CLASS_E_CLASSNOTAVAILABLE;
723 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
724 if (factory == NULL) return E_OUTOFMEMORY;
726 factory->IClassFactory_iface.lpVtbl = &IClassFactory_Vtbl;
727 factory->ref = 1;
729 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
731 *out = factory;
732 return S_OK;
736 /*******************************************************************************
737 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
739 * RETURNS
740 * Success: S_OK
741 * Failure: S_FALSE
743 HRESULT WINAPI DllCanUnloadNow(void)
745 TRACE("\n");
747 return S_FALSE;
751 /***********************************************************************
752 * DllRegisterServer (DDRAW.@)
754 HRESULT WINAPI DllRegisterServer(void)
756 return __wine_register_resources( instance );
759 /***********************************************************************
760 * DllUnregisterServer (DDRAW.@)
762 HRESULT WINAPI DllUnregisterServer(void)
764 return __wine_unregister_resources( instance );
767 /*******************************************************************************
768 * DestroyCallback
770 * Callback function for the EnumSurfaces call in DllMain.
771 * Dumps some surface info and releases the surface
773 * Params:
774 * surf: The enumerated surface
775 * desc: it's description
776 * context: Pointer to the ddraw impl
778 * Returns:
779 * DDENUMRET_OK;
780 *******************************************************************************/
781 static HRESULT WINAPI
782 DestroyCallback(IDirectDrawSurface7 *surf,
783 DDSURFACEDESC2 *desc,
784 void *context)
786 struct ddraw_surface *Impl = impl_from_IDirectDrawSurface7(surf);
787 ULONG ref7, ref4, ref3, ref2, ref1, gamma_count, iface_count;
789 ref7 = IDirectDrawSurface7_Release(surf); /* For the EnumSurfaces */
790 ref4 = Impl->ref4;
791 ref3 = Impl->ref3;
792 ref2 = Impl->ref2;
793 ref1 = Impl->ref1;
794 gamma_count = Impl->gamma_count;
796 WARN("Surface %p has an reference counts of 7: %u 4: %u 3: %u 2: %u 1: %u gamma: %u\n",
797 Impl, ref7, ref4, ref3, ref2, ref1, gamma_count);
799 /* Skip surfaces which are attached somewhere or which are
800 * part of a complex compound. They will get released when destroying
801 * the root
803 if( (!Impl->is_complex_root) || (Impl->first_attached != Impl) )
804 return DDENUMRET_OK;
806 /* Destroy the surface */
807 iface_count = ddraw_surface_release_iface(Impl);
808 while (iface_count) iface_count = ddraw_surface_release_iface(Impl);
810 return DDENUMRET_OK;
813 /***********************************************************************
814 * DllMain (DDRAW.0)
816 * Could be used to register DirectDraw drivers, if we have more than
817 * one. Also used to destroy any objects left at unload if the
818 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
820 ***********************************************************************/
821 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
823 switch (reason)
825 case DLL_PROCESS_ATTACH:
827 static HMODULE ddraw_self;
828 HKEY hkey = 0;
829 WNDCLASSA wc;
831 /* Register the window class. This is used to create a hidden window
832 * for D3D rendering, if the application didn't pass one. It can also
833 * be used for creating a device window from SetCooperativeLevel(). */
834 wc.style = CS_HREDRAW | CS_VREDRAW;
835 wc.lpfnWndProc = DefWindowProcA;
836 wc.cbClsExtra = 0;
837 wc.cbWndExtra = 0;
838 wc.hInstance = inst;
839 wc.hIcon = 0;
840 wc.hCursor = 0;
841 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
842 wc.lpszMenuName = NULL;
843 wc.lpszClassName = DDRAW_WINDOW_CLASS_NAME;
844 if (!RegisterClassA(&wc))
846 ERR("Failed to register ddraw window class, last error %#x.\n", GetLastError());
847 return FALSE;
850 /* On Windows one can force the refresh rate that DirectDraw uses by
851 * setting an override value in dxdiag. This is documented in KB315614
852 * (main article), KB230002, and KB217348. By comparing registry dumps
853 * before and after setting the override, we see that the override value
854 * is stored in HKLM\Software\Microsoft\DirectDraw\ForceRefreshRate as a
855 * DWORD that represents the refresh rate to force. We use this
856 * registry entry to modify the behavior of SetDisplayMode so that Wine
857 * users can override the refresh rate in a Windows-compatible way.
859 * dxdiag will not accept a refresh rate lower than 40 or higher than
860 * 120 so this value should be within that range. It is, of course,
861 * possible for a user to set the registry entry value directly so that
862 * assumption might not hold.
864 * There is no current mechanism for setting this value through the Wine
865 * GUI. It would be most appropriate to set this value through a dxdiag
866 * clone, but it may be sufficient to use winecfg.
868 * TODO: Create a mechanism for setting this value through the Wine GUI.
870 if ( !RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectDraw", &hkey ) )
872 DWORD type, data, size;
874 size = sizeof(data);
875 if (!RegQueryValueExA(hkey, "ForceRefreshRate", NULL, &type, (BYTE *)&data, &size) && type == REG_DWORD)
877 TRACE("ForceRefreshRate set; overriding refresh rate to %d Hz\n", data);
878 force_refresh_rate = data;
880 RegCloseKey( hkey );
883 /* Prevent the ddraw module from being unloaded. When switching to
884 * exclusive mode, we replace the window proc of the ddraw window. If
885 * an application would unload ddraw from the WM_DESTROY handler for
886 * that window, it would return to unmapped memory and die. Apparently
887 * this is supposed to work on Windows. */
888 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
889 (const WCHAR *)&ddraw_self, &ddraw_self))
890 ERR("Failed to get own module handle.\n");
892 instance = inst;
893 DisableThreadLibraryCalls(inst);
894 break;
897 case DLL_PROCESS_DETACH:
898 if(!list_empty(&global_ddraw_list))
900 struct list *entry, *entry2;
901 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
903 /* We remove elements from this loop */
904 LIST_FOR_EACH_SAFE(entry, entry2, &global_ddraw_list)
906 struct ddraw *ddraw = LIST_ENTRY(entry, struct ddraw, ddraw_list_entry);
907 HRESULT hr;
908 DDSURFACEDESC2 desc;
909 int i;
911 WARN("DDraw %p has a refcount of %d\n", ddraw, ddraw->ref7 + ddraw->ref4 + ddraw->ref3 + ddraw->ref2 + ddraw->ref1);
913 /* Add references to each interface to avoid freeing them unexpectedly */
914 IDirectDraw_AddRef(&ddraw->IDirectDraw_iface);
915 IDirectDraw2_AddRef(&ddraw->IDirectDraw2_iface);
916 IDirectDraw4_AddRef(&ddraw->IDirectDraw4_iface);
917 IDirectDraw7_AddRef(&ddraw->IDirectDraw7_iface);
919 /* Does a D3D device exist? Destroy it
920 * TODO: Destroy all Vertex buffers, Lights, Materials
921 * and execute buffers too
923 if(ddraw->d3ddevice)
925 WARN("DDraw %p has d3ddevice %p attached\n", ddraw, ddraw->d3ddevice);
926 while(IDirect3DDevice7_Release(&ddraw->d3ddevice->IDirect3DDevice7_iface));
929 /* Destroy the swapchain after any 3D device. The 3D device
930 * cleanup code needs a swapchain. Specifically, it tries to
931 * set the current render target to the front buffer. */
932 if (ddraw->wined3d_swapchain)
933 ddraw_destroy_swapchain(ddraw);
935 /* Try to release the objects
936 * Do an EnumSurfaces to find any hanging surfaces
938 memset(&desc, 0, sizeof(desc));
939 desc.dwSize = sizeof(desc);
940 for(i = 0; i <= 1; i++)
942 hr = IDirectDraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, DDENUMSURFACES_ALL,
943 &desc, ddraw, DestroyCallback);
944 if(hr != D3D_OK)
945 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw);
948 if (!list_empty(&ddraw->surface_list))
949 ERR("DDraw %p still has surfaces attached.\n", ddraw);
951 /* Release all hanging references to destroy the objects. This
952 * restores the screen mode too
954 while(IDirectDraw_Release(&ddraw->IDirectDraw_iface));
955 while(IDirectDraw2_Release(&ddraw->IDirectDraw2_iface));
956 while(IDirectDraw4_Release(&ddraw->IDirectDraw4_iface));
957 while(IDirectDraw7_Release(&ddraw->IDirectDraw7_iface));
961 if (reserved) break;
962 UnregisterClassA(DDRAW_WINDOW_CLASS_NAME, inst);
965 return TRUE;