msvcp90: Return last index in string::find_last_not_of_cstr_substr if input is empty.
[wine.git] / dlls / ddraw / main.c
blobe613126b780c75b8f44dfb319ed3d690ad500fd6
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 /* 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));
66 if (!t->entries)
68 ERR("Failed to allocate handle table memory.\n");
69 return FALSE;
71 t->free_entries = NULL;
72 t->table_size = initial_size;
73 t->entry_count = 0;
75 return TRUE;
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;
88 if (t->free_entries)
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;
100 entry->type = type;
102 return idx;
105 if (!(t->entry_count < t->table_size))
107 /* Grow the table */
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));
111 if (!new_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;
122 entry->type = type;
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;
130 void *object;
132 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
134 WARN("Invalid handle %#x passed.\n", handle);
135 return NULL;
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);
142 return NULL;
145 object = entry->object;
146 entry->object = t->free_entries;
147 entry->type = DDRAW_HANDLE_FREE;
148 t->free_entries = entry;
150 return object;
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);
160 return NULL;
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);
167 return NULL;
170 return entry->object;
173 /***********************************************************************
175 * Helper function for DirectDrawCreate and friends
176 * Creates a new DDraw interface with the given REFIID
178 * Interfaces that can be created:
179 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
180 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
181 * IDirect3D interfaces?)
183 * Arguments:
184 * guid: ID of the requested driver, NULL for the default driver.
185 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
186 * DD: Used to return the pointer to the created object
187 * UnkOuter: For aggregation, which is unsupported. Must be NULL
188 * iid: requested version ID.
190 * Returns:
191 * DD_OK if the Interface was created successfully
192 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
193 * E_OUTOFMEMORY if some allocation failed
195 ***********************************************************************/
196 static HRESULT
197 DDRAW_Create(const GUID *guid,
198 void **DD,
199 IUnknown *UnkOuter,
200 REFIID iid)
202 enum wined3d_device_type device_type;
203 struct ddraw *ddraw;
204 HRESULT hr;
206 TRACE("driver_guid %s, ddraw %p, outer_unknown %p, interface_iid %s.\n",
207 debugstr_guid(guid), DD, UnkOuter, debugstr_guid(iid));
209 *DD = NULL;
211 /* We don't care about this guids. Well, there's no special guid anyway
212 * OK, we could
214 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
216 /* Use the reference device id. This doesn't actually change anything,
217 * WineD3D always uses OpenGL for D3D rendering. One could make it request
218 * indirect rendering
220 device_type = WINED3D_DEVICE_TYPE_REF;
222 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
224 device_type = WINED3D_DEVICE_TYPE_HAL;
226 else
228 device_type = 0;
231 /* DDraw doesn't support aggregation, according to msdn */
232 if (UnkOuter != NULL)
233 return CLASS_E_NOAGGREGATION;
235 /* DirectDraw creation comes here */
236 ddraw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw));
237 if (!ddraw)
239 ERR("Out of memory when creating DirectDraw\n");
240 return E_OUTOFMEMORY;
243 hr = ddraw_init(ddraw, device_type);
244 if (FAILED(hr))
246 WARN("Failed to initialize ddraw object, hr %#x.\n", hr);
247 HeapFree(GetProcessHeap(), 0, ddraw);
248 return hr;
251 hr = IDirectDraw7_QueryInterface(&ddraw->IDirectDraw7_iface, iid, DD);
252 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
253 if (SUCCEEDED(hr))
254 list_add_head(&global_ddraw_list, &ddraw->ddraw_list_entry);
255 else
256 WARN("Failed to query interface %s from ddraw object %p.\n", debugstr_guid(iid), ddraw);
258 return hr;
261 /***********************************************************************
262 * DirectDrawCreate (DDRAW.@)
264 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
265 * interfaces in theory
267 * Arguments, return values: See DDRAW_Create
269 ***********************************************************************/
270 HRESULT WINAPI DECLSPEC_HOTPATCH DirectDrawCreate(GUID *driver_guid, IDirectDraw **ddraw, IUnknown *outer)
272 HRESULT hr;
274 TRACE("driver_guid %s, ddraw %p, outer %p.\n",
275 debugstr_guid(driver_guid), ddraw, outer);
277 wined3d_mutex_lock();
278 hr = DDRAW_Create(driver_guid, (void **)ddraw, outer, &IID_IDirectDraw);
279 wined3d_mutex_unlock();
281 if (SUCCEEDED(hr))
283 if (FAILED(hr = IDirectDraw_Initialize(*ddraw, driver_guid)))
284 IDirectDraw_Release(*ddraw);
287 return hr;
290 /***********************************************************************
291 * DirectDrawCreateEx (DDRAW.@)
293 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
294 * interfaces are requested.
296 * Arguments, return values: See DDRAW_Create
298 ***********************************************************************/
299 HRESULT WINAPI DECLSPEC_HOTPATCH DirectDrawCreateEx(GUID *driver_guid,
300 void **ddraw, REFIID interface_iid, IUnknown *outer)
302 HRESULT hr;
304 TRACE("driver_guid %s, ddraw %p, interface_iid %s, outer %p.\n",
305 debugstr_guid(driver_guid), ddraw, debugstr_guid(interface_iid), outer);
307 if (!IsEqualGUID(interface_iid, &IID_IDirectDraw7))
308 return DDERR_INVALIDPARAMS;
310 wined3d_mutex_lock();
311 hr = DDRAW_Create(driver_guid, ddraw, outer, interface_iid);
312 wined3d_mutex_unlock();
314 if (SUCCEEDED(hr))
316 IDirectDraw7 *ddraw7 = *(IDirectDraw7 **)ddraw;
317 hr = IDirectDraw7_Initialize(ddraw7, driver_guid);
318 if (FAILED(hr))
319 IDirectDraw7_Release(ddraw7);
322 return hr;
325 /***********************************************************************
326 * DirectDrawEnumerateA (DDRAW.@)
328 * Enumerates legacy ddraw drivers, ascii version. We only have one
329 * driver, which relays to WineD3D. If we were sufficiently cool,
330 * we could offer various interfaces, which use a different default surface
331 * implementation, but I think it's better to offer this choice in
332 * winecfg, because some apps use the default driver, so we would need
333 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
335 * Arguments:
336 * Callback: Callback function from the app
337 * Context: Argument to the call back.
339 * Returns:
340 * DD_OK on success
341 * E_INVALIDARG if the Callback caused a page fault
344 ***********************************************************************/
345 HRESULT WINAPI DirectDrawEnumerateA(LPDDENUMCALLBACKA callback, void *context)
347 struct callback_info info;
349 TRACE("callback %p, context %p.\n", callback, context);
351 info.callback = callback;
352 info.context = context;
353 return DirectDrawEnumerateExA(enum_callback, &info, 0x0);
356 /***********************************************************************
357 * DirectDrawEnumerateExA (DDRAW.@)
359 * Enumerates DirectDraw7 drivers, ascii version. See
360 * the comments above DirectDrawEnumerateA for more details.
362 * The Flag member is not supported right now.
364 ***********************************************************************/
365 HRESULT WINAPI DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA callback, void *context, DWORD flags)
367 struct wined3d *wined3d;
369 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
371 if (flags & ~(DDENUM_ATTACHEDSECONDARYDEVICES |
372 DDENUM_DETACHEDSECONDARYDEVICES |
373 DDENUM_NONDISPLAYDEVICES))
374 return DDERR_INVALIDPARAMS;
376 if (flags)
377 FIXME("flags 0x%08x not handled\n", flags);
379 TRACE("Enumerating ddraw interfaces\n");
380 if (!(wined3d = wined3d_create(7, WINED3D_LEGACY_DEPTH_BIAS)))
382 if (!(wined3d = wined3d_create(7, WINED3D_LEGACY_DEPTH_BIAS | WINED3D_NO3D)))
384 WARN("Failed to create a wined3d object.\n");
385 return E_FAIL;
388 WARN("Created a wined3d object without 3D support.\n");
391 __TRY
393 /* QuickTime expects the description "DirectDraw HAL" */
394 static CHAR driver_desc[] = "DirectDraw HAL",
395 driver_name[] = "display";
396 struct wined3d_adapter_identifier adapter_id;
397 HRESULT hr = S_OK;
398 UINT adapter = 0;
399 BOOL cont_enum;
401 /* The Battle.net System Checker expects both a NULL device and a GUID-based device */
402 TRACE("Default interface: DirectDraw HAL\n");
403 cont_enum = callback(NULL, driver_desc, driver_name, context, 0);
404 for (adapter = 0; SUCCEEDED(hr) && cont_enum; adapter++)
406 char DriverName[512] = "";
408 /* The Battle.net System Checker expects the GetAdapterIdentifier DeviceName to match the
409 * Driver Name, so obtain the DeviceName and GUID from D3D. */
410 memset(&adapter_id, 0x0, sizeof(adapter_id));
411 adapter_id.device_name = DriverName;
412 adapter_id.device_name_size = sizeof(DriverName);
413 wined3d_mutex_lock();
414 hr = wined3d_get_adapter_identifier(wined3d, adapter, 0x0, &adapter_id);
415 wined3d_mutex_unlock();
416 if (SUCCEEDED(hr))
418 TRACE("Interface %d: %s\n", adapter, wine_dbgstr_guid(&adapter_id.device_identifier));
419 cont_enum = callback(&adapter_id.device_identifier, driver_desc,
420 adapter_id.device_name, context, 0);
424 __EXCEPT_PAGE_FAULT
426 wined3d_decref(wined3d);
427 return DDERR_INVALIDPARAMS;
429 __ENDTRY;
431 wined3d_decref(wined3d);
432 TRACE("End of enumeration\n");
433 return DD_OK;
436 /***********************************************************************
437 * DirectDrawEnumerateW (DDRAW.@)
439 * Enumerates legacy drivers, unicode version.
440 * This function is not implemented on Windows.
442 ***********************************************************************/
443 HRESULT WINAPI DirectDrawEnumerateW(LPDDENUMCALLBACKW callback, void *context)
445 TRACE("callback %p, context %p.\n", callback, context);
447 if (!callback)
448 return DDERR_INVALIDPARAMS;
449 else
450 return DDERR_UNSUPPORTED;
453 /***********************************************************************
454 * DirectDrawEnumerateExW (DDRAW.@)
456 * Enumerates DirectDraw7 drivers, unicode version.
457 * This function is not implemented on Windows.
459 ***********************************************************************/
460 HRESULT WINAPI DirectDrawEnumerateExW(LPDDENUMCALLBACKEXW callback, void *context, DWORD flags)
462 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
464 return DDERR_UNSUPPORTED;
467 /***********************************************************************
468 * Classfactory implementation.
469 ***********************************************************************/
471 /***********************************************************************
472 * CF_CreateDirectDraw
474 * DDraw creation function for the class factory
476 * Params:
477 * UnkOuter: Set to NULL
478 * iid: ID of the wanted interface
479 * obj: Address to pass the interface pointer back
481 * Returns
482 * DD_OK / DDERR*, see DDRAW_Create
484 ***********************************************************************/
485 static HRESULT
486 CF_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
487 void **obj)
489 HRESULT hr;
491 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(iid), obj);
493 wined3d_mutex_lock();
494 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
495 wined3d_mutex_unlock();
497 return hr;
500 /***********************************************************************
501 * CF_CreateDirectDraw
503 * Clipper creation function for the class factory
505 * Params:
506 * UnkOuter: Set to NULL
507 * iid: ID of the wanted interface
508 * obj: Address to pass the interface pointer back
510 * Returns
511 * DD_OK / DDERR*, see DDRAW_Create
513 ***********************************************************************/
514 static HRESULT
515 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
516 void **obj)
518 HRESULT hr;
519 IDirectDrawClipper *Clip;
521 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(riid), obj);
523 wined3d_mutex_lock();
524 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
525 if (hr != DD_OK)
527 wined3d_mutex_unlock();
528 return hr;
531 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
532 IDirectDrawClipper_Release(Clip);
534 wined3d_mutex_unlock();
536 return hr;
539 static const struct object_creation_info object_creation[] =
541 { &CLSID_DirectDraw, CF_CreateDirectDraw },
542 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
543 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
546 struct ddraw_class_factory
548 IClassFactory IClassFactory_iface;
550 LONG ref;
551 HRESULT (*pfnCreateInstance)(IUnknown *outer, REFIID iid, void **out);
554 static inline struct ddraw_class_factory *impl_from_IClassFactory(IClassFactory *iface)
556 return CONTAINING_RECORD(iface, struct ddraw_class_factory, IClassFactory_iface);
559 /*******************************************************************************
560 * IDirectDrawClassFactory::QueryInterface
562 * QueryInterface for the class factory
564 * PARAMS
565 * riid Reference to identifier of queried interface
566 * ppv Address to return the interface pointer at
568 * RETURNS
569 * Success: S_OK
570 * Failure: E_NOINTERFACE
572 *******************************************************************************/
573 static HRESULT WINAPI ddraw_class_factory_QueryInterface(IClassFactory *iface, REFIID riid, void **out)
575 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
577 if (IsEqualGUID(riid, &IID_IUnknown)
578 || IsEqualGUID(riid, &IID_IClassFactory))
580 IClassFactory_AddRef(iface);
581 *out = iface;
582 return S_OK;
585 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
587 return E_NOINTERFACE;
590 /*******************************************************************************
591 * IDirectDrawClassFactory::AddRef
593 * AddRef for the class factory
595 * RETURNS
596 * The new refcount
598 *******************************************************************************/
599 static ULONG WINAPI ddraw_class_factory_AddRef(IClassFactory *iface)
601 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
602 ULONG ref = InterlockedIncrement(&factory->ref);
604 TRACE("%p increasing refcount to %u.\n", factory, ref);
606 return ref;
609 /*******************************************************************************
610 * IDirectDrawClassFactory::Release
612 * Release for the class factory. If the refcount falls to 0, the object
613 * is destroyed
615 * RETURNS
616 * The new refcount
618 *******************************************************************************/
619 static ULONG WINAPI ddraw_class_factory_Release(IClassFactory *iface)
621 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
622 ULONG ref = InterlockedDecrement(&factory->ref);
624 TRACE("%p decreasing refcount to %u.\n", factory, ref);
626 if (!ref)
627 HeapFree(GetProcessHeap(), 0, factory);
629 return ref;
633 /*******************************************************************************
634 * IDirectDrawClassFactory::CreateInstance
636 * What is this? Seems to create DirectDraw objects...
638 * Params
639 * The usual things???
641 * RETURNS
642 * ???
644 *******************************************************************************/
645 static HRESULT WINAPI ddraw_class_factory_CreateInstance(IClassFactory *iface,
646 IUnknown *outer_unknown, REFIID riid, void **out)
648 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
650 TRACE("iface %p, outer_unknown %p, riid %s, out %p.\n",
651 iface, outer_unknown, debugstr_guid(riid), out);
653 return factory->pfnCreateInstance(outer_unknown, riid, out);
656 /*******************************************************************************
657 * IDirectDrawClassFactory::LockServer
659 * What is this?
661 * Params
662 * ???
664 * RETURNS
665 * S_OK, because it's a stub
667 *******************************************************************************/
668 static HRESULT WINAPI ddraw_class_factory_LockServer(IClassFactory *iface, BOOL dolock)
670 FIXME("iface %p, dolock %#x stub!\n", iface, dolock);
672 return S_OK;
675 /*******************************************************************************
676 * The class factory VTable
677 *******************************************************************************/
678 static const IClassFactoryVtbl IClassFactory_Vtbl =
680 ddraw_class_factory_QueryInterface,
681 ddraw_class_factory_AddRef,
682 ddraw_class_factory_Release,
683 ddraw_class_factory_CreateInstance,
684 ddraw_class_factory_LockServer
687 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **out)
689 struct ddraw_class_factory *factory;
690 unsigned int i;
692 TRACE("rclsid %s, riid %s, out %p.\n",
693 debugstr_guid(rclsid), debugstr_guid(riid), out);
695 if (!IsEqualGUID(&IID_IClassFactory, riid)
696 && !IsEqualGUID(&IID_IUnknown, riid))
697 return E_NOINTERFACE;
699 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
701 if (IsEqualGUID(object_creation[i].clsid, rclsid))
702 break;
705 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
707 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
708 return CLASS_E_CLASSNOTAVAILABLE;
711 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
712 if (factory == NULL) return E_OUTOFMEMORY;
714 factory->IClassFactory_iface.lpVtbl = &IClassFactory_Vtbl;
715 factory->ref = 1;
717 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
719 *out = factory;
720 return S_OK;
724 /*******************************************************************************
725 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
727 * RETURNS
728 * Success: S_OK
729 * Failure: S_FALSE
731 HRESULT WINAPI DllCanUnloadNow(void)
733 TRACE("\n");
735 return S_FALSE;
739 /***********************************************************************
740 * DllRegisterServer (DDRAW.@)
742 HRESULT WINAPI DllRegisterServer(void)
744 return __wine_register_resources( instance );
747 /***********************************************************************
748 * DllUnregisterServer (DDRAW.@)
750 HRESULT WINAPI DllUnregisterServer(void)
752 return __wine_unregister_resources( instance );
755 /*******************************************************************************
756 * DestroyCallback
758 * Callback function for the EnumSurfaces call in DllMain.
759 * Dumps some surface info and releases the surface
761 * Params:
762 * surf: The enumerated surface
763 * desc: it's description
764 * context: Pointer to the ddraw impl
766 * Returns:
767 * DDENUMRET_OK;
768 *******************************************************************************/
769 static HRESULT WINAPI
770 DestroyCallback(IDirectDrawSurface7 *surf,
771 DDSURFACEDESC2 *desc,
772 void *context)
774 struct ddraw_surface *Impl = impl_from_IDirectDrawSurface7(surf);
775 ULONG ref7, ref4, ref3, ref2, ref1, gamma_count, iface_count;
777 ref7 = IDirectDrawSurface7_Release(surf); /* For the EnumSurfaces */
778 ref4 = Impl->ref4;
779 ref3 = Impl->ref3;
780 ref2 = Impl->ref2;
781 ref1 = Impl->ref1;
782 gamma_count = Impl->gamma_count;
784 WARN("Surface %p has an reference counts of 7: %u 4: %u 3: %u 2: %u 1: %u gamma: %u\n",
785 Impl, ref7, ref4, ref3, ref2, ref1, gamma_count);
787 /* Skip surfaces which are attached somewhere or which are
788 * part of a complex compound. They will get released when destroying
789 * the root
791 if( (!Impl->is_complex_root) || (Impl->first_attached != Impl) )
792 return DDENUMRET_OK;
794 /* Destroy the surface */
795 iface_count = ddraw_surface_release_iface(Impl);
796 while (iface_count) iface_count = ddraw_surface_release_iface(Impl);
798 return DDENUMRET_OK;
801 /***********************************************************************
802 * DllMain (DDRAW.0)
804 * Could be used to register DirectDraw drivers, if we have more than
805 * one. Also used to destroy any objects left at unload if the
806 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
808 ***********************************************************************/
809 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
811 switch (reason)
813 case DLL_PROCESS_ATTACH:
815 static HMODULE ddraw_self;
816 HKEY hkey = 0;
817 WNDCLASSA wc;
819 /* Register the window class. This is used to create a hidden window
820 * for D3D rendering, if the application didn't pass one. It can also
821 * be used for creating a device window from SetCooperativeLevel(). */
822 wc.style = CS_HREDRAW | CS_VREDRAW;
823 wc.lpfnWndProc = DefWindowProcA;
824 wc.cbClsExtra = 0;
825 wc.cbWndExtra = 0;
826 wc.hInstance = inst;
827 wc.hIcon = 0;
828 wc.hCursor = 0;
829 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
830 wc.lpszMenuName = NULL;
831 wc.lpszClassName = DDRAW_WINDOW_CLASS_NAME;
832 if (!RegisterClassA(&wc))
834 ERR("Failed to register ddraw window class, last error %#x.\n", GetLastError());
835 return FALSE;
838 /* On Windows one can force the refresh rate that DirectDraw uses by
839 * setting an override value in dxdiag. This is documented in KB315614
840 * (main article), KB230002, and KB217348. By comparing registry dumps
841 * before and after setting the override, we see that the override value
842 * is stored in HKLM\Software\Microsoft\DirectDraw\ForceRefreshRate as a
843 * DWORD that represents the refresh rate to force. We use this
844 * registry entry to modify the behavior of SetDisplayMode so that Wine
845 * users can override the refresh rate in a Windows-compatible way.
847 * dxdiag will not accept a refresh rate lower than 40 or higher than
848 * 120 so this value should be within that range. It is, of course,
849 * possible for a user to set the registry entry value directly so that
850 * assumption might not hold.
852 * There is no current mechanism for setting this value through the Wine
853 * GUI. It would be most appropriate to set this value through a dxdiag
854 * clone, but it may be sufficient to use winecfg.
856 * TODO: Create a mechanism for setting this value through the Wine GUI.
858 if ( !RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectDraw", &hkey ) )
860 DWORD type, data, size;
862 size = sizeof(data);
863 if (!RegQueryValueExA(hkey, "ForceRefreshRate", NULL, &type, (BYTE *)&data, &size) && type == REG_DWORD)
865 TRACE("ForceRefreshRate set; overriding refresh rate to %d Hz\n", data);
866 force_refresh_rate = data;
868 RegCloseKey( hkey );
871 /* Prevent the ddraw module from being unloaded. When switching to
872 * exclusive mode, we replace the window proc of the ddraw window. If
873 * an application would unload ddraw from the WM_DESTROY handler for
874 * that window, it would return to unmapped memory and die. Apparently
875 * this is supposed to work on Windows. */
876 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
877 (const WCHAR *)&ddraw_self, &ddraw_self))
878 ERR("Failed to get own module handle.\n");
880 instance = inst;
881 DisableThreadLibraryCalls(inst);
882 break;
885 case DLL_PROCESS_DETACH:
886 if(!list_empty(&global_ddraw_list))
888 struct list *entry, *entry2;
889 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
891 /* We remove elements from this loop */
892 LIST_FOR_EACH_SAFE(entry, entry2, &global_ddraw_list)
894 struct ddraw *ddraw = LIST_ENTRY(entry, struct ddraw, ddraw_list_entry);
895 HRESULT hr;
896 DDSURFACEDESC2 desc;
897 int i;
899 WARN("DDraw %p has a refcount of %d\n", ddraw, ddraw->ref7 + ddraw->ref4 + ddraw->ref3 + ddraw->ref2 + ddraw->ref1);
901 /* Add references to each interface to avoid freeing them unexpectedly */
902 IDirectDraw_AddRef(&ddraw->IDirectDraw_iface);
903 IDirectDraw2_AddRef(&ddraw->IDirectDraw2_iface);
904 IDirectDraw4_AddRef(&ddraw->IDirectDraw4_iface);
905 IDirectDraw7_AddRef(&ddraw->IDirectDraw7_iface);
907 /* Does a D3D device exist? Destroy it
908 * TODO: Destroy all Vertex buffers, Lights, Materials
909 * and execute buffers too
911 if(ddraw->d3ddevice)
913 WARN("DDraw %p has d3ddevice %p attached\n", ddraw, ddraw->d3ddevice);
914 while(IDirect3DDevice7_Release(&ddraw->d3ddevice->IDirect3DDevice7_iface));
917 /* Destroy the swapchain after any 3D device. The 3D device
918 * cleanup code needs a swapchain. Specifically, it tries to
919 * set the current render target to the front buffer. */
920 if (ddraw->wined3d_swapchain)
921 ddraw_destroy_swapchain(ddraw);
923 /* Try to release the objects
924 * Do an EnumSurfaces to find any hanging surfaces
926 memset(&desc, 0, sizeof(desc));
927 desc.dwSize = sizeof(desc);
928 for(i = 0; i <= 1; i++)
930 hr = IDirectDraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, DDENUMSURFACES_ALL,
931 &desc, ddraw, DestroyCallback);
932 if(hr != D3D_OK)
933 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw);
936 if (!list_empty(&ddraw->surface_list))
937 ERR("DDraw %p still has surfaces attached.\n", ddraw);
939 /* Release all hanging references to destroy the objects. This
940 * restores the screen mode too
942 while(IDirectDraw_Release(&ddraw->IDirectDraw_iface));
943 while(IDirectDraw2_Release(&ddraw->IDirectDraw2_iface));
944 while(IDirectDraw4_Release(&ddraw->IDirectDraw4_iface));
945 while(IDirectDraw7_Release(&ddraw->IDirectDraw7_iface));
949 if (reserved) break;
950 UnregisterClassA(DDRAW_WINDOW_CLASS_NAME, inst);
953 return TRUE;