ntdll: Implement support for AT_ROUND_TO_PAGE flag in NtMapViewOfSection.
[wine.git] / dlls / ddraw / main.c
blob8631827cd5d32381185e4717a5340966e73ae1af
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 library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wine/port.h"
27 #define DDRAW_INIT_GUID
28 #include "ddraw_private.h"
29 #include "rpcproxy.h"
31 #include "wine/exception.h"
32 #include "winreg.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
36 static struct list global_ddraw_list = LIST_INIT(global_ddraw_list);
38 static HINSTANCE instance;
40 /* value of ForceRefreshRate */
41 DWORD force_refresh_rate = 0;
43 /* Structure for converting DirectDrawEnumerateA to DirectDrawEnumerateExA */
44 struct callback_info
46 LPDDENUMCALLBACKA callback;
47 void *context;
50 /* Enumeration callback for converting DirectDrawEnumerateA to DirectDrawEnumerateExA */
51 static HRESULT CALLBACK enum_callback(GUID *guid, char *description, char *driver_name,
52 void *context, HMONITOR monitor)
54 const struct callback_info *info = context;
56 return info->callback(guid, description, driver_name, info->context);
59 static void ddraw_enumerate_secondary_devices(struct wined3d *wined3d, LPDDENUMCALLBACKEXA callback,
60 void *context)
62 struct wined3d_adapter_identifier adapter_id;
63 BOOL cont_enum = TRUE;
64 HRESULT hr = S_OK;
65 UINT adapter = 0;
67 for (adapter = 0; SUCCEEDED(hr) && cont_enum; adapter++)
69 char DriverName[512] = "", DriverDescription[512] = "";
71 /* The Battle.net System Checker expects the GetAdapterIdentifier DeviceName to match the
72 * Driver Name, so obtain the DeviceName and GUID from D3D. */
73 memset(&adapter_id, 0x0, sizeof(adapter_id));
74 adapter_id.device_name = DriverName;
75 adapter_id.device_name_size = sizeof(DriverName);
76 adapter_id.description = DriverDescription;
77 adapter_id.description_size = sizeof(DriverDescription);
78 wined3d_mutex_lock();
79 hr = wined3d_get_adapter_identifier(wined3d, adapter, 0x0, &adapter_id);
80 wined3d_mutex_unlock();
81 if (SUCCEEDED(hr))
83 TRACE("Interface %d: %s\n", adapter, wine_dbgstr_guid(&adapter_id.device_identifier));
84 cont_enum = callback(&adapter_id.device_identifier, adapter_id.description,
85 adapter_id.device_name, context, wined3d_get_adapter_monitor(wined3d, adapter));
90 /* Handle table functions */
91 BOOL ddraw_handle_table_init(struct ddraw_handle_table *t, UINT initial_size)
93 t->entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, initial_size * sizeof(*t->entries));
94 if (!t->entries)
96 ERR("Failed to allocate handle table memory.\n");
97 return FALSE;
99 t->free_entries = NULL;
100 t->table_size = initial_size;
101 t->entry_count = 0;
103 return TRUE;
106 void ddraw_handle_table_destroy(struct ddraw_handle_table *t)
108 HeapFree(GetProcessHeap(), 0, t->entries);
109 memset(t, 0, sizeof(*t));
112 DWORD ddraw_allocate_handle(struct ddraw_handle_table *t, void *object, enum ddraw_handle_type type)
114 struct ddraw_handle_entry *entry;
116 if (t->free_entries)
118 DWORD idx = t->free_entries - t->entries;
119 /* Use a free handle */
120 entry = t->free_entries;
121 if (entry->type != DDRAW_HANDLE_FREE)
123 ERR("Handle %#x (%p) is in the free list, but has type %#x.\n", idx, entry->object, entry->type);
124 return DDRAW_INVALID_HANDLE;
126 t->free_entries = entry->object;
127 entry->object = object;
128 entry->type = type;
130 return idx;
133 if (!(t->entry_count < t->table_size))
135 /* Grow the table */
136 UINT new_size = t->table_size + (t->table_size >> 1);
137 struct ddraw_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
138 0, t->entries, new_size * sizeof(*t->entries));
139 if (!new_entries)
141 ERR("Failed to grow the handle table.\n");
142 return DDRAW_INVALID_HANDLE;
144 t->entries = new_entries;
145 t->table_size = new_size;
148 entry = &t->entries[t->entry_count];
149 entry->object = object;
150 entry->type = type;
152 return t->entry_count++;
155 void *ddraw_free_handle(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
157 struct ddraw_handle_entry *entry;
158 void *object;
160 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
162 WARN("Invalid handle %#x passed.\n", handle);
163 return NULL;
166 entry = &t->entries[handle];
167 if (entry->type != type)
169 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
170 return NULL;
173 object = entry->object;
174 entry->object = t->free_entries;
175 entry->type = DDRAW_HANDLE_FREE;
176 t->free_entries = entry;
178 return object;
181 void *ddraw_get_object(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
183 struct ddraw_handle_entry *entry;
185 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
187 WARN("Invalid handle %#x passed.\n", handle);
188 return NULL;
191 entry = &t->entries[handle];
192 if (entry->type != type)
194 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
195 return NULL;
198 return entry->object;
201 /***********************************************************************
203 * Helper function for DirectDrawCreate and friends
204 * Creates a new DDraw interface with the given REFIID
206 * Interfaces that can be created:
207 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
208 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
209 * IDirect3D interfaces?)
211 * Arguments:
212 * guid: ID of the requested driver, NULL for the default driver.
213 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
214 * DD: Used to return the pointer to the created object
215 * UnkOuter: For aggregation, which is unsupported. Must be NULL
216 * iid: requested version ID.
218 * Returns:
219 * DD_OK if the Interface was created successfully
220 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
221 * E_OUTOFMEMORY if some allocation failed
223 ***********************************************************************/
224 static HRESULT
225 DDRAW_Create(const GUID *guid,
226 void **DD,
227 IUnknown *UnkOuter,
228 REFIID iid)
230 enum wined3d_device_type device_type;
231 struct ddraw *ddraw;
232 HRESULT hr;
233 DWORD flags = 0;
235 TRACE("driver_guid %s, ddraw %p, outer_unknown %p, interface_iid %s.\n",
236 debugstr_guid(guid), DD, UnkOuter, debugstr_guid(iid));
238 *DD = NULL;
240 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
242 /* Use the reference device id. This doesn't actually change anything,
243 * WineD3D always uses OpenGL for D3D rendering. One could make it request
244 * indirect rendering
246 device_type = WINED3D_DEVICE_TYPE_REF;
248 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
250 device_type = WINED3D_DEVICE_TYPE_HAL;
252 else
254 device_type = 0;
257 /* DDraw doesn't support aggregation, according to msdn */
258 if (UnkOuter != NULL)
259 return CLASS_E_NOAGGREGATION;
261 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
262 flags = WINED3D_LEGACY_FFP_LIGHTING;
264 /* DirectDraw creation comes here */
265 ddraw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw));
266 if (!ddraw)
268 ERR("Out of memory when creating DirectDraw\n");
269 return E_OUTOFMEMORY;
272 hr = ddraw_init(ddraw, flags, device_type);
273 if (FAILED(hr))
275 WARN("Failed to initialize ddraw object, hr %#x.\n", hr);
276 HeapFree(GetProcessHeap(), 0, ddraw);
277 return hr;
280 hr = IDirectDraw7_QueryInterface(&ddraw->IDirectDraw7_iface, iid, DD);
281 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
282 if (SUCCEEDED(hr))
283 list_add_head(&global_ddraw_list, &ddraw->ddraw_list_entry);
284 else
285 WARN("Failed to query interface %s from ddraw object %p.\n", debugstr_guid(iid), ddraw);
287 return hr;
290 /***********************************************************************
291 * DirectDrawCreate (DDRAW.@)
293 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
294 * interfaces in theory
296 * Arguments, return values: See DDRAW_Create
298 ***********************************************************************/
299 HRESULT WINAPI DECLSPEC_HOTPATCH DirectDrawCreate(GUID *driver_guid, IDirectDraw **ddraw, IUnknown *outer)
301 HRESULT hr;
303 TRACE("driver_guid %s, ddraw %p, outer %p.\n",
304 debugstr_guid(driver_guid), ddraw, outer);
306 wined3d_mutex_lock();
307 hr = DDRAW_Create(driver_guid, (void **)ddraw, outer, &IID_IDirectDraw);
308 wined3d_mutex_unlock();
310 if (SUCCEEDED(hr))
312 if (FAILED(hr = IDirectDraw_Initialize(*ddraw, driver_guid)))
313 IDirectDraw_Release(*ddraw);
316 return hr;
319 /***********************************************************************
320 * DirectDrawCreateEx (DDRAW.@)
322 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
323 * interfaces are requested.
325 * Arguments, return values: See DDRAW_Create
327 ***********************************************************************/
328 HRESULT WINAPI DECLSPEC_HOTPATCH DirectDrawCreateEx(GUID *driver_guid,
329 void **ddraw, REFIID interface_iid, IUnknown *outer)
331 HRESULT hr;
333 TRACE("driver_guid %s, ddraw %p, interface_iid %s, outer %p.\n",
334 debugstr_guid(driver_guid), ddraw, debugstr_guid(interface_iid), outer);
336 if (!IsEqualGUID(interface_iid, &IID_IDirectDraw7))
337 return DDERR_INVALIDPARAMS;
339 wined3d_mutex_lock();
340 hr = DDRAW_Create(driver_guid, ddraw, outer, interface_iid);
341 wined3d_mutex_unlock();
343 if (SUCCEEDED(hr))
345 IDirectDraw7 *ddraw7 = *(IDirectDraw7 **)ddraw;
346 hr = IDirectDraw7_Initialize(ddraw7, driver_guid);
347 if (FAILED(hr))
348 IDirectDraw7_Release(ddraw7);
351 return hr;
354 /***********************************************************************
355 * DirectDrawEnumerateA (DDRAW.@)
357 * Enumerates legacy ddraw drivers, ascii version. We only have one
358 * driver, which relays to WineD3D. If we were sufficiently cool,
359 * we could offer various interfaces, which use a different default surface
360 * implementation, but I think it's better to offer this choice in
361 * winecfg, because some apps use the default driver, so we would need
362 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
364 * Arguments:
365 * Callback: Callback function from the app
366 * Context: Argument to the call back.
368 * Returns:
369 * DD_OK on success
370 * E_INVALIDARG if the Callback caused a page fault
373 ***********************************************************************/
374 HRESULT WINAPI DirectDrawEnumerateA(LPDDENUMCALLBACKA callback, void *context)
376 struct callback_info info;
378 TRACE("callback %p, context %p.\n", callback, context);
380 info.callback = callback;
381 info.context = context;
382 return DirectDrawEnumerateExA(enum_callback, &info, 0x0);
385 /***********************************************************************
386 * DirectDrawEnumerateExA (DDRAW.@)
388 * Enumerates DirectDraw7 drivers, ascii version. See
389 * the comments above DirectDrawEnumerateA for more details.
391 * The Flag member is not supported right now.
393 ***********************************************************************/
394 HRESULT WINAPI DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA callback, void *context, DWORD flags)
396 struct wined3d *wined3d;
398 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
400 if (flags & ~(DDENUM_ATTACHEDSECONDARYDEVICES |
401 DDENUM_DETACHEDSECONDARYDEVICES |
402 DDENUM_NONDISPLAYDEVICES))
403 return DDERR_INVALIDPARAMS;
405 if (flags & ~DDENUM_ATTACHEDSECONDARYDEVICES)
406 FIXME("flags 0x%08x not handled\n", flags & ~DDENUM_ATTACHEDSECONDARYDEVICES);
408 TRACE("Enumerating ddraw interfaces\n");
409 if (!(wined3d = wined3d_create(DDRAW_WINED3D_FLAGS)))
411 if (!(wined3d = wined3d_create(DDRAW_WINED3D_FLAGS | WINED3D_NO3D)))
413 WARN("Failed to create a wined3d object.\n");
414 return E_FAIL;
417 WARN("Created a wined3d object without 3D support.\n");
420 __TRY
422 /* QuickTime expects the description "DirectDraw HAL" */
423 static CHAR driver_desc[] = "DirectDraw HAL",
424 driver_name[] = "display";
425 BOOL cont_enum;
427 TRACE("Default interface: DirectDraw HAL\n");
428 cont_enum = callback(NULL, driver_desc, driver_name, context, 0);
430 /* The Battle.net System Checker expects both a NULL device and a GUID-based device */
431 if (cont_enum && (flags & DDENUM_ATTACHEDSECONDARYDEVICES))
432 ddraw_enumerate_secondary_devices(wined3d, callback, context);
434 __EXCEPT_PAGE_FAULT
436 wined3d_decref(wined3d);
437 return DDERR_INVALIDPARAMS;
439 __ENDTRY;
441 wined3d_decref(wined3d);
442 TRACE("End of enumeration\n");
443 return DD_OK;
446 /***********************************************************************
447 * DirectDrawEnumerateW (DDRAW.@)
449 * Enumerates legacy drivers, unicode version.
450 * This function is not implemented on Windows.
452 ***********************************************************************/
453 HRESULT WINAPI DirectDrawEnumerateW(LPDDENUMCALLBACKW callback, void *context)
455 TRACE("callback %p, context %p.\n", callback, context);
457 if (!callback)
458 return DDERR_INVALIDPARAMS;
459 else
460 return DDERR_UNSUPPORTED;
463 /***********************************************************************
464 * DirectDrawEnumerateExW (DDRAW.@)
466 * Enumerates DirectDraw7 drivers, unicode version.
467 * This function is not implemented on Windows.
469 ***********************************************************************/
470 HRESULT WINAPI DirectDrawEnumerateExW(LPDDENUMCALLBACKEXW callback, void *context, DWORD flags)
472 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
474 return DDERR_UNSUPPORTED;
477 /***********************************************************************
478 * Classfactory implementation.
479 ***********************************************************************/
481 /***********************************************************************
482 * CF_CreateDirectDraw
484 * DDraw creation function for the class factory
486 * Params:
487 * UnkOuter: Set to NULL
488 * iid: ID of the wanted interface
489 * obj: Address to pass the interface pointer back
491 * Returns
492 * DD_OK / DDERR*, see DDRAW_Create
494 ***********************************************************************/
495 static HRESULT
496 CF_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
497 void **obj)
499 HRESULT hr;
501 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(iid), obj);
503 wined3d_mutex_lock();
504 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
505 wined3d_mutex_unlock();
507 return hr;
510 /***********************************************************************
511 * CF_CreateDirectDraw
513 * Clipper creation function for the class factory
515 * Params:
516 * UnkOuter: Set to NULL
517 * iid: ID of the wanted interface
518 * obj: Address to pass the interface pointer back
520 * Returns
521 * DD_OK / DDERR*, see DDRAW_Create
523 ***********************************************************************/
524 static HRESULT
525 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
526 void **obj)
528 HRESULT hr;
529 IDirectDrawClipper *Clip;
531 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(riid), obj);
533 wined3d_mutex_lock();
534 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
535 if (hr != DD_OK)
537 wined3d_mutex_unlock();
538 return hr;
541 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
542 IDirectDrawClipper_Release(Clip);
544 wined3d_mutex_unlock();
546 return hr;
549 static const struct object_creation_info object_creation[] =
551 { &CLSID_DirectDraw, CF_CreateDirectDraw },
552 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
553 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
556 struct ddraw_class_factory
558 IClassFactory IClassFactory_iface;
560 LONG ref;
561 HRESULT (*pfnCreateInstance)(IUnknown *outer, REFIID iid, void **out);
564 static inline struct ddraw_class_factory *impl_from_IClassFactory(IClassFactory *iface)
566 return CONTAINING_RECORD(iface, struct ddraw_class_factory, IClassFactory_iface);
569 /*******************************************************************************
570 * IDirectDrawClassFactory::QueryInterface
572 * QueryInterface for the class factory
574 * PARAMS
575 * riid Reference to identifier of queried interface
576 * ppv Address to return the interface pointer at
578 * RETURNS
579 * Success: S_OK
580 * Failure: E_NOINTERFACE
582 *******************************************************************************/
583 static HRESULT WINAPI ddraw_class_factory_QueryInterface(IClassFactory *iface, REFIID riid, void **out)
585 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
587 if (IsEqualGUID(riid, &IID_IUnknown)
588 || IsEqualGUID(riid, &IID_IClassFactory))
590 IClassFactory_AddRef(iface);
591 *out = iface;
592 return S_OK;
595 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
597 return E_NOINTERFACE;
600 /*******************************************************************************
601 * IDirectDrawClassFactory::AddRef
603 * AddRef for the class factory
605 * RETURNS
606 * The new refcount
608 *******************************************************************************/
609 static ULONG WINAPI ddraw_class_factory_AddRef(IClassFactory *iface)
611 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
612 ULONG ref = InterlockedIncrement(&factory->ref);
614 TRACE("%p increasing refcount to %u.\n", factory, ref);
616 return ref;
619 /*******************************************************************************
620 * IDirectDrawClassFactory::Release
622 * Release for the class factory. If the refcount falls to 0, the object
623 * is destroyed
625 * RETURNS
626 * The new refcount
628 *******************************************************************************/
629 static ULONG WINAPI ddraw_class_factory_Release(IClassFactory *iface)
631 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
632 ULONG ref = InterlockedDecrement(&factory->ref);
634 TRACE("%p decreasing refcount to %u.\n", factory, ref);
636 if (!ref)
637 HeapFree(GetProcessHeap(), 0, factory);
639 return ref;
643 /*******************************************************************************
644 * IDirectDrawClassFactory::CreateInstance
646 * What is this? Seems to create DirectDraw objects...
648 * Params
649 * The usual things???
651 * RETURNS
652 * ???
654 *******************************************************************************/
655 static HRESULT WINAPI ddraw_class_factory_CreateInstance(IClassFactory *iface,
656 IUnknown *outer_unknown, REFIID riid, void **out)
658 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
660 TRACE("iface %p, outer_unknown %p, riid %s, out %p.\n",
661 iface, outer_unknown, debugstr_guid(riid), out);
663 return factory->pfnCreateInstance(outer_unknown, riid, out);
666 /*******************************************************************************
667 * IDirectDrawClassFactory::LockServer
669 * What is this?
671 * Params
672 * ???
674 * RETURNS
675 * S_OK, because it's a stub
677 *******************************************************************************/
678 static HRESULT WINAPI ddraw_class_factory_LockServer(IClassFactory *iface, BOOL dolock)
680 FIXME("iface %p, dolock %#x stub!\n", iface, dolock);
682 return S_OK;
685 /*******************************************************************************
686 * The class factory VTable
687 *******************************************************************************/
688 static const IClassFactoryVtbl IClassFactory_Vtbl =
690 ddraw_class_factory_QueryInterface,
691 ddraw_class_factory_AddRef,
692 ddraw_class_factory_Release,
693 ddraw_class_factory_CreateInstance,
694 ddraw_class_factory_LockServer
697 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **out)
699 struct ddraw_class_factory *factory;
700 unsigned int i;
702 TRACE("rclsid %s, riid %s, out %p.\n",
703 debugstr_guid(rclsid), debugstr_guid(riid), out);
705 if (!IsEqualGUID(&IID_IClassFactory, riid)
706 && !IsEqualGUID(&IID_IUnknown, riid))
707 return E_NOINTERFACE;
709 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
711 if (IsEqualGUID(object_creation[i].clsid, rclsid))
712 break;
715 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
717 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
718 return CLASS_E_CLASSNOTAVAILABLE;
721 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
722 if (factory == NULL) return E_OUTOFMEMORY;
724 factory->IClassFactory_iface.lpVtbl = &IClassFactory_Vtbl;
725 factory->ref = 1;
727 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
729 *out = factory;
730 return S_OK;
734 /*******************************************************************************
735 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
737 * RETURNS
738 * Success: S_OK
739 * Failure: S_FALSE
741 HRESULT WINAPI DllCanUnloadNow(void)
743 TRACE("\n");
745 return S_FALSE;
749 /***********************************************************************
750 * DllRegisterServer (DDRAW.@)
752 HRESULT WINAPI DllRegisterServer(void)
754 return __wine_register_resources( instance );
757 /***********************************************************************
758 * DllUnregisterServer (DDRAW.@)
760 HRESULT WINAPI DllUnregisterServer(void)
762 return __wine_unregister_resources( instance );
765 /*******************************************************************************
766 * DestroyCallback
768 * Callback function for the EnumSurfaces call in DllMain.
769 * Dumps some surface info and releases the surface
771 * Params:
772 * surf: The enumerated surface
773 * desc: it's description
774 * context: Pointer to the ddraw impl
776 * Returns:
777 * DDENUMRET_OK;
778 *******************************************************************************/
779 static HRESULT WINAPI
780 DestroyCallback(IDirectDrawSurface7 *surf,
781 DDSURFACEDESC2 *desc,
782 void *context)
784 struct ddraw_surface *Impl = impl_from_IDirectDrawSurface7(surf);
785 ULONG ref7, ref4, ref3, ref2, ref1, gamma_count, iface_count;
787 ref7 = IDirectDrawSurface7_Release(surf); /* For the EnumSurfaces */
788 ref4 = Impl->ref4;
789 ref3 = Impl->ref3;
790 ref2 = Impl->ref2;
791 ref1 = Impl->ref1;
792 gamma_count = Impl->gamma_count;
794 WARN("Surface %p has an reference counts of 7: %u 4: %u 3: %u 2: %u 1: %u gamma: %u\n",
795 Impl, ref7, ref4, ref3, ref2, ref1, gamma_count);
797 /* Skip surfaces which are attached somewhere or which are
798 * part of a complex compound. They will get released when destroying
799 * the root
801 if( (!Impl->is_complex_root) || (Impl->first_attached != Impl) )
802 return DDENUMRET_OK;
804 /* Destroy the surface */
805 iface_count = ddraw_surface_release_iface(Impl);
806 while (iface_count) iface_count = ddraw_surface_release_iface(Impl);
808 return DDENUMRET_OK;
811 /***********************************************************************
812 * DllMain (DDRAW.0)
814 * Could be used to register DirectDraw drivers, if we have more than
815 * one. Also used to destroy any objects left at unload if the
816 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
818 ***********************************************************************/
819 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
821 switch (reason)
823 case DLL_PROCESS_ATTACH:
825 static HMODULE ddraw_self;
826 HKEY hkey = 0;
827 WNDCLASSA wc;
829 /* Register the window class. This is used to create a hidden window
830 * for D3D rendering, if the application didn't pass one. It can also
831 * be used for creating a device window from SetCooperativeLevel(). */
832 wc.style = CS_HREDRAW | CS_VREDRAW;
833 wc.lpfnWndProc = DefWindowProcA;
834 wc.cbClsExtra = 0;
835 wc.cbWndExtra = 0;
836 wc.hInstance = inst;
837 wc.hIcon = 0;
838 wc.hCursor = 0;
839 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
840 wc.lpszMenuName = NULL;
841 wc.lpszClassName = DDRAW_WINDOW_CLASS_NAME;
842 if (!RegisterClassA(&wc))
844 ERR("Failed to register ddraw window class, last error %#x.\n", GetLastError());
845 return FALSE;
848 /* On Windows one can force the refresh rate that DirectDraw uses by
849 * setting an override value in dxdiag. This is documented in KB315614
850 * (main article), KB230002, and KB217348. By comparing registry dumps
851 * before and after setting the override, we see that the override value
852 * is stored in HKLM\Software\Microsoft\DirectDraw\ForceRefreshRate as a
853 * DWORD that represents the refresh rate to force. We use this
854 * registry entry to modify the behavior of SetDisplayMode so that Wine
855 * users can override the refresh rate in a Windows-compatible way.
857 * dxdiag will not accept a refresh rate lower than 40 or higher than
858 * 120 so this value should be within that range. It is, of course,
859 * possible for a user to set the registry entry value directly so that
860 * assumption might not hold.
862 * There is no current mechanism for setting this value through the Wine
863 * GUI. It would be most appropriate to set this value through a dxdiag
864 * clone, but it may be sufficient to use winecfg.
866 * TODO: Create a mechanism for setting this value through the Wine GUI.
868 if ( !RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectDraw", &hkey ) )
870 DWORD type, data, size;
872 size = sizeof(data);
873 if (!RegQueryValueExA(hkey, "ForceRefreshRate", NULL, &type, (BYTE *)&data, &size) && type == REG_DWORD)
875 TRACE("ForceRefreshRate set; overriding refresh rate to %d Hz\n", data);
876 force_refresh_rate = data;
878 RegCloseKey( hkey );
881 /* Prevent the ddraw module from being unloaded. When switching to
882 * exclusive mode, we replace the window proc of the ddraw window. If
883 * an application would unload ddraw from the WM_DESTROY handler for
884 * that window, it would return to unmapped memory and die. Apparently
885 * this is supposed to work on Windows. */
886 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
887 (const WCHAR *)&ddraw_self, &ddraw_self))
888 ERR("Failed to get own module handle.\n");
890 instance = inst;
891 DisableThreadLibraryCalls(inst);
892 break;
895 case DLL_PROCESS_DETACH:
896 if(!list_empty(&global_ddraw_list))
898 struct list *entry, *entry2;
899 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
901 /* We remove elements from this loop */
902 LIST_FOR_EACH_SAFE(entry, entry2, &global_ddraw_list)
904 struct ddraw *ddraw = LIST_ENTRY(entry, struct ddraw, ddraw_list_entry);
905 HRESULT hr;
906 DDSURFACEDESC2 desc;
907 int i;
909 WARN("DDraw %p has a refcount of %d\n", ddraw, ddraw->ref7 + ddraw->ref4 + ddraw->ref3 + ddraw->ref2 + ddraw->ref1);
911 /* Add references to each interface to avoid freeing them unexpectedly */
912 IDirectDraw_AddRef(&ddraw->IDirectDraw_iface);
913 IDirectDraw2_AddRef(&ddraw->IDirectDraw2_iface);
914 IDirectDraw4_AddRef(&ddraw->IDirectDraw4_iface);
915 IDirectDraw7_AddRef(&ddraw->IDirectDraw7_iface);
917 /* Does a D3D device exist? Destroy it
918 * TODO: Destroy all Vertex buffers, Lights, Materials
919 * and execute buffers too
921 if(ddraw->d3ddevice)
923 WARN("DDraw %p has d3ddevice %p attached\n", ddraw, ddraw->d3ddevice);
924 while(IDirect3DDevice7_Release(&ddraw->d3ddevice->IDirect3DDevice7_iface));
927 /* Destroy the swapchain after any 3D device. The 3D device
928 * cleanup code needs a swapchain. Specifically, it tries to
929 * set the current render target to the front buffer. */
930 if (ddraw->wined3d_swapchain)
931 ddraw_destroy_swapchain(ddraw);
933 /* Try to release the objects
934 * Do an EnumSurfaces to find any hanging surfaces
936 memset(&desc, 0, sizeof(desc));
937 desc.dwSize = sizeof(desc);
938 for(i = 0; i <= 1; i++)
940 hr = IDirectDraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, DDENUMSURFACES_ALL,
941 &desc, ddraw, DestroyCallback);
942 if(hr != D3D_OK)
943 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw);
946 if (!list_empty(&ddraw->surface_list))
947 ERR("DDraw %p still has surfaces attached.\n", ddraw);
949 /* Release all hanging references to destroy the objects. This
950 * restores the screen mode too
952 while(IDirectDraw_Release(&ddraw->IDirectDraw_iface));
953 while(IDirectDraw2_Release(&ddraw->IDirectDraw2_iface));
954 while(IDirectDraw4_Release(&ddraw->IDirectDraw4_iface));
955 while(IDirectDraw7_Release(&ddraw->IDirectDraw7_iface));
959 if (reserved) break;
960 UnregisterClassA(DDRAW_WINDOW_CLASS_NAME, inst);
963 return TRUE;