msvcrt: Import cbrt implementation from musl.
[wine.git] / dlls / ddraw / main.c
blobb75676a69414e6872fea3195ae3c5c56396f4920
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 #define DDRAW_INIT_GUID
25 #include "ddraw_private.h"
26 #include "rpcproxy.h"
28 #include "wine/exception.h"
29 #include "winreg.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
33 static struct list global_ddraw_list = LIST_INIT(global_ddraw_list);
35 static HINSTANCE instance;
37 /* value of ForceRefreshRate */
38 DWORD force_refresh_rate = 0;
40 /* Structure for converting DirectDrawEnumerateA to DirectDrawEnumerateExA */
41 struct callback_info
43 LPDDENUMCALLBACKA callback;
44 void *context;
47 /* Enumeration callback for converting DirectDrawEnumerateA to DirectDrawEnumerateExA */
48 static BOOL CALLBACK enum_callback(GUID *guid, char *description, char *driver_name,
49 void *context, HMONITOR monitor)
51 const struct callback_info *info = context;
53 return info->callback(guid, description, driver_name, info->context);
56 static void ddraw_enumerate_secondary_devices(struct wined3d *wined3d, LPDDENUMCALLBACKEXA callback,
57 void *context)
59 struct wined3d_adapter_identifier adapter_id;
60 struct wined3d_adapter *wined3d_adapter;
61 struct wined3d_output_desc output_desc;
62 struct wined3d_output *wined3d_output;
63 unsigned int interface_count = 0;
64 unsigned int adapter_idx = 0;
65 unsigned int output_idx;
66 BOOL cont_enum = TRUE;
67 HRESULT hr;
69 while (cont_enum && (wined3d_adapter = wined3d_get_adapter(wined3d, adapter_idx)))
71 char device_name[512] = "", description[512] = "";
73 /* The Battle.net System Checker expects the GetAdapterIdentifier DeviceName to match the
74 * Driver Name, so obtain the DeviceName and GUID from D3D. */
75 memset(&adapter_id, 0x0, sizeof(adapter_id));
76 adapter_id.description = description;
77 adapter_id.description_size = sizeof(description);
79 wined3d_mutex_lock();
80 if (FAILED(hr = wined3d_adapter_get_identifier(wined3d_adapter, 0x0, &adapter_id)))
82 WARN("Failed to get adapter identifier, hr %#x.\n", hr);
83 wined3d_mutex_unlock();
84 break;
86 wined3d_mutex_unlock();
88 for (output_idx = 0; cont_enum && (wined3d_output = wined3d_adapter_get_output(
89 wined3d_adapter, output_idx)); ++output_idx)
91 wined3d_mutex_lock();
92 if (FAILED(hr = wined3d_output_get_desc(wined3d_output, &output_desc)))
94 WARN("Failed to get output description, hr %#x.\n", hr);
95 wined3d_mutex_unlock();
96 break;
98 wined3d_mutex_unlock();
100 TRACE("Interface %u: %s\n", interface_count++,
101 wine_dbgstr_guid(&adapter_id.device_identifier));
102 WideCharToMultiByte(CP_ACP, 0, output_desc.device_name, -1, device_name,
103 sizeof(device_name), NULL, NULL);
104 cont_enum = callback(&adapter_id.device_identifier, adapter_id.description,
105 device_name, context, output_desc.monitor);
108 if (FAILED(hr))
109 break;
111 ++adapter_idx;
115 /* Handle table functions */
116 BOOL ddraw_handle_table_init(struct ddraw_handle_table *t, UINT initial_size)
118 if (!(t->entries = heap_alloc_zero(initial_size * sizeof(*t->entries))))
120 ERR("Failed to allocate handle table memory.\n");
121 return FALSE;
123 t->free_entries = NULL;
124 t->table_size = initial_size;
125 t->entry_count = 0;
127 return TRUE;
130 void ddraw_handle_table_destroy(struct ddraw_handle_table *t)
132 heap_free(t->entries);
133 memset(t, 0, sizeof(*t));
136 DWORD ddraw_allocate_handle(struct ddraw_handle_table *t, void *object, enum ddraw_handle_type type)
138 struct ddraw_handle_entry *entry;
140 if (t->free_entries)
142 DWORD idx = t->free_entries - t->entries;
143 /* Use a free handle */
144 entry = t->free_entries;
145 if (entry->type != DDRAW_HANDLE_FREE)
147 ERR("Handle %#x (%p) is in the free list, but has type %#x.\n", idx, entry->object, entry->type);
148 return DDRAW_INVALID_HANDLE;
150 t->free_entries = entry->object;
151 entry->object = object;
152 entry->type = type;
154 return idx;
157 if (!(t->entry_count < t->table_size))
159 /* Grow the table */
160 UINT new_size = t->table_size + (t->table_size >> 1);
161 struct ddraw_handle_entry *new_entries;
163 if (!(new_entries = heap_realloc(t->entries, new_size * sizeof(*t->entries))))
165 ERR("Failed to grow the handle table.\n");
166 return DDRAW_INVALID_HANDLE;
168 t->entries = new_entries;
169 t->table_size = new_size;
172 entry = &t->entries[t->entry_count];
173 entry->object = object;
174 entry->type = type;
176 return t->entry_count++;
179 void *ddraw_free_handle(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
181 struct ddraw_handle_entry *entry;
182 void *object;
184 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
186 WARN("Invalid handle %#x passed.\n", handle);
187 return NULL;
190 entry = &t->entries[handle];
191 if (entry->type != type)
193 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
194 return NULL;
197 object = entry->object;
198 entry->object = t->free_entries;
199 entry->type = DDRAW_HANDLE_FREE;
200 t->free_entries = entry;
202 return object;
205 void *ddraw_get_object(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
207 struct ddraw_handle_entry *entry;
209 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
211 WARN("Invalid handle %#x passed.\n", handle);
212 return NULL;
215 entry = &t->entries[handle];
216 if (entry->type != type)
218 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
219 return NULL;
222 return entry->object;
225 HRESULT WINAPI GetSurfaceFromDC(HDC dc, IDirectDrawSurface4 **surface, HDC *device_dc)
227 struct ddraw *ddraw;
229 TRACE("dc %p, surface %p, device_dc %p.\n", dc, surface, device_dc);
231 if (!surface)
232 return E_INVALIDARG;
234 if (!device_dc)
236 *surface = NULL;
238 return E_INVALIDARG;
241 wined3d_mutex_lock();
242 LIST_FOR_EACH_ENTRY(ddraw, &global_ddraw_list, struct ddraw, ddraw_list_entry)
244 if (FAILED(IDirectDraw4_GetSurfaceFromDC(&ddraw->IDirectDraw4_iface, dc, surface)))
245 continue;
247 *device_dc = NULL; /* FIXME */
248 wined3d_mutex_unlock();
249 return DD_OK;
251 wined3d_mutex_unlock();
253 *surface = NULL;
254 *device_dc = NULL;
256 return DDERR_NOTFOUND;
259 /***********************************************************************
261 * Helper function for DirectDrawCreate and friends
262 * Creates a new DDraw interface with the given REFIID
264 * Interfaces that can be created:
265 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
266 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
267 * IDirect3D interfaces?)
269 * Arguments:
270 * guid: ID of the requested driver, NULL for the default driver.
271 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
272 * DD: Used to return the pointer to the created object
273 * UnkOuter: For aggregation, which is unsupported. Must be NULL
274 * iid: requested version ID.
276 * Returns:
277 * DD_OK if the Interface was created successfully
278 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
279 * E_OUTOFMEMORY if some allocation failed
281 ***********************************************************************/
282 static HRESULT DDRAW_Create(const GUID *guid, void **out, IUnknown *outer_unknown, REFIID iid)
284 enum wined3d_device_type device_type;
285 struct ddraw *ddraw;
286 DWORD flags = 0;
287 HRESULT hr;
289 TRACE("driver_guid %s, ddraw %p, outer_unknown %p, interface_iid %s.\n",
290 debugstr_guid(guid), out, outer_unknown, debugstr_guid(iid));
292 *out = NULL;
294 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
296 device_type = WINED3D_DEVICE_TYPE_REF;
298 else if (guid == (GUID *) DDCREATE_HARDWAREONLY)
300 device_type = WINED3D_DEVICE_TYPE_HAL;
302 else
304 device_type = WINED3D_DEVICE_TYPE_HAL;
307 /* DDraw doesn't support aggregation, according to msdn */
308 if (outer_unknown != NULL)
309 return CLASS_E_NOAGGREGATION;
311 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
312 flags = WINED3D_LEGACY_FFP_LIGHTING;
314 if (!(ddraw = heap_alloc_zero(sizeof(*ddraw))))
316 ERR("Out of memory when creating DirectDraw.\n");
317 return E_OUTOFMEMORY;
320 if (FAILED(hr = ddraw_init(ddraw, flags, device_type)))
322 WARN("Failed to initialize ddraw object, hr %#x.\n", hr);
323 heap_free(ddraw);
324 return hr;
327 hr = IDirectDraw7_QueryInterface(&ddraw->IDirectDraw7_iface, iid, out);
328 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
329 if (SUCCEEDED(hr))
330 list_add_head(&global_ddraw_list, &ddraw->ddraw_list_entry);
331 else
332 WARN("Failed to query interface %s from ddraw object %p.\n", debugstr_guid(iid), ddraw);
334 return hr;
337 /***********************************************************************
338 * DirectDrawCreate (DDRAW.@)
340 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
341 * interfaces in theory
343 * Arguments, return values: See DDRAW_Create
345 ***********************************************************************/
346 HRESULT WINAPI DECLSPEC_HOTPATCH DirectDrawCreate(GUID *driver_guid, IDirectDraw **ddraw, IUnknown *outer)
348 HRESULT hr;
350 TRACE("driver_guid %s, ddraw %p, outer %p.\n",
351 debugstr_guid(driver_guid), ddraw, outer);
353 wined3d_mutex_lock();
354 hr = DDRAW_Create(driver_guid, (void **)ddraw, outer, &IID_IDirectDraw);
355 wined3d_mutex_unlock();
357 if (SUCCEEDED(hr))
359 if (FAILED(hr = IDirectDraw_Initialize(*ddraw, driver_guid)))
360 IDirectDraw_Release(*ddraw);
363 return hr;
366 /***********************************************************************
367 * DirectDrawCreateEx (DDRAW.@)
369 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
370 * interfaces are requested.
372 * Arguments, return values: See DDRAW_Create
374 ***********************************************************************/
375 HRESULT WINAPI DECLSPEC_HOTPATCH DirectDrawCreateEx(GUID *driver_guid,
376 void **ddraw, REFIID interface_iid, IUnknown *outer)
378 HRESULT hr;
380 TRACE("driver_guid %s, ddraw %p, interface_iid %s, outer %p.\n",
381 debugstr_guid(driver_guid), ddraw, debugstr_guid(interface_iid), outer);
383 if (!IsEqualGUID(interface_iid, &IID_IDirectDraw7))
384 return DDERR_INVALIDPARAMS;
386 wined3d_mutex_lock();
387 hr = DDRAW_Create(driver_guid, ddraw, outer, interface_iid);
388 wined3d_mutex_unlock();
390 if (SUCCEEDED(hr))
392 IDirectDraw7 *ddraw7 = *(IDirectDraw7 **)ddraw;
393 hr = IDirectDraw7_Initialize(ddraw7, driver_guid);
394 if (FAILED(hr))
395 IDirectDraw7_Release(ddraw7);
398 return hr;
401 /***********************************************************************
402 * DirectDrawEnumerateA (DDRAW.@)
404 * Enumerates legacy ddraw drivers, ascii version. We only have one
405 * driver, which relays to WineD3D. If we were sufficiently cool,
406 * we could offer various interfaces, which use a different default surface
407 * implementation, but I think it's better to offer this choice in
408 * winecfg, because some apps use the default driver, so we would need
409 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
411 * Arguments:
412 * Callback: Callback function from the app
413 * Context: Argument to the call back.
415 * Returns:
416 * DD_OK on success
417 * E_INVALIDARG if the Callback caused a page fault
420 ***********************************************************************/
421 HRESULT WINAPI DirectDrawEnumerateA(LPDDENUMCALLBACKA callback, void *context)
423 struct callback_info info;
425 TRACE("callback %p, context %p.\n", callback, context);
427 info.callback = callback;
428 info.context = context;
429 return DirectDrawEnumerateExA(enum_callback, &info, 0x0);
432 /***********************************************************************
433 * DirectDrawEnumerateExA (DDRAW.@)
435 * Enumerates DirectDraw7 drivers, ascii version. See
436 * the comments above DirectDrawEnumerateA for more details.
438 * The Flag member is not supported right now.
440 ***********************************************************************/
441 HRESULT WINAPI DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA callback, void *context, DWORD flags)
443 struct wined3d *wined3d;
445 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
447 if (flags & ~(DDENUM_ATTACHEDSECONDARYDEVICES |
448 DDENUM_DETACHEDSECONDARYDEVICES |
449 DDENUM_NONDISPLAYDEVICES))
450 return DDERR_INVALIDPARAMS;
452 if (flags & ~DDENUM_ATTACHEDSECONDARYDEVICES)
453 FIXME("flags 0x%08x not handled\n", flags & ~DDENUM_ATTACHEDSECONDARYDEVICES);
455 TRACE("Enumerating ddraw interfaces\n");
456 if (!(wined3d = wined3d_create(DDRAW_WINED3D_FLAGS)))
458 if (!(wined3d = wined3d_create(DDRAW_WINED3D_FLAGS | WINED3D_NO3D)))
460 WARN("Failed to create a wined3d object.\n");
461 return E_FAIL;
464 WARN("Created a wined3d object without 3D support.\n");
467 __TRY
469 /* QuickTime expects the description "DirectDraw HAL" */
470 static CHAR driver_desc[] = "DirectDraw HAL",
471 driver_name[] = "display";
472 BOOL cont_enum;
474 TRACE("Default interface: DirectDraw HAL\n");
475 cont_enum = callback(NULL, driver_desc, driver_name, context, 0);
477 /* The Battle.net System Checker expects both a NULL device and a GUID-based device */
478 if (cont_enum && (flags & DDENUM_ATTACHEDSECONDARYDEVICES))
479 ddraw_enumerate_secondary_devices(wined3d, callback, context);
481 __EXCEPT_PAGE_FAULT
483 wined3d_decref(wined3d);
484 return DDERR_INVALIDPARAMS;
486 __ENDTRY;
488 wined3d_decref(wined3d);
489 TRACE("End of enumeration\n");
490 return DD_OK;
493 /***********************************************************************
494 * DirectDrawEnumerateW (DDRAW.@)
496 * Enumerates legacy drivers, unicode version.
497 * This function is not implemented on Windows.
499 ***********************************************************************/
500 HRESULT WINAPI DirectDrawEnumerateW(LPDDENUMCALLBACKW callback, void *context)
502 TRACE("callback %p, context %p.\n", callback, context);
504 if (!callback)
505 return DDERR_INVALIDPARAMS;
506 else
507 return DDERR_UNSUPPORTED;
510 /***********************************************************************
511 * DirectDrawEnumerateExW (DDRAW.@)
513 * Enumerates DirectDraw7 drivers, unicode version.
514 * This function is not implemented on Windows.
516 ***********************************************************************/
517 HRESULT WINAPI DirectDrawEnumerateExW(LPDDENUMCALLBACKEXW callback, void *context, DWORD flags)
519 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
521 return DDERR_UNSUPPORTED;
524 /***********************************************************************
525 * Classfactory implementation.
526 ***********************************************************************/
528 /***********************************************************************
529 * CF_CreateDirectDraw
531 * DDraw creation function for the class factory
533 * Params:
534 * UnkOuter: Set to NULL
535 * iid: ID of the wanted interface
536 * obj: Address to pass the interface pointer back
538 * Returns
539 * DD_OK / DDERR*, see DDRAW_Create
541 ***********************************************************************/
542 static HRESULT
543 CF_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
544 void **obj)
546 HRESULT hr;
548 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(iid), obj);
550 wined3d_mutex_lock();
551 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
552 wined3d_mutex_unlock();
554 return hr;
557 /***********************************************************************
558 * CF_CreateDirectDraw
560 * Clipper creation function for the class factory
562 * Params:
563 * UnkOuter: Set to NULL
564 * iid: ID of the wanted interface
565 * obj: Address to pass the interface pointer back
567 * Returns
568 * DD_OK / DDERR*, see DDRAW_Create
570 ***********************************************************************/
571 static HRESULT
572 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
573 void **obj)
575 HRESULT hr;
576 IDirectDrawClipper *Clip;
578 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(riid), obj);
580 wined3d_mutex_lock();
581 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
582 if (hr != DD_OK)
584 wined3d_mutex_unlock();
585 return hr;
588 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
589 IDirectDrawClipper_Release(Clip);
591 wined3d_mutex_unlock();
593 return hr;
596 static const struct object_creation_info object_creation[] =
598 { &CLSID_DirectDraw, CF_CreateDirectDraw },
599 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
600 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
603 struct ddraw_class_factory
605 IClassFactory IClassFactory_iface;
607 LONG ref;
608 HRESULT (*pfnCreateInstance)(IUnknown *outer, REFIID iid, void **out);
611 static inline struct ddraw_class_factory *impl_from_IClassFactory(IClassFactory *iface)
613 return CONTAINING_RECORD(iface, struct ddraw_class_factory, IClassFactory_iface);
616 /*******************************************************************************
617 * IDirectDrawClassFactory::QueryInterface
619 * QueryInterface for the class factory
621 * PARAMS
622 * riid Reference to identifier of queried interface
623 * ppv Address to return the interface pointer at
625 * RETURNS
626 * Success: S_OK
627 * Failure: E_NOINTERFACE
629 *******************************************************************************/
630 static HRESULT WINAPI ddraw_class_factory_QueryInterface(IClassFactory *iface, REFIID riid, void **out)
632 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
634 if (IsEqualGUID(riid, &IID_IUnknown)
635 || IsEqualGUID(riid, &IID_IClassFactory))
637 IClassFactory_AddRef(iface);
638 *out = iface;
639 return S_OK;
642 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
644 return E_NOINTERFACE;
647 /*******************************************************************************
648 * IDirectDrawClassFactory::AddRef
650 * AddRef for the class factory
652 * RETURNS
653 * The new refcount
655 *******************************************************************************/
656 static ULONG WINAPI ddraw_class_factory_AddRef(IClassFactory *iface)
658 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
659 ULONG ref = InterlockedIncrement(&factory->ref);
661 TRACE("%p increasing refcount to %u.\n", factory, ref);
663 return ref;
666 /*******************************************************************************
667 * IDirectDrawClassFactory::Release
669 * Release for the class factory. If the refcount falls to 0, the object
670 * is destroyed
672 * RETURNS
673 * The new refcount
675 *******************************************************************************/
676 static ULONG WINAPI ddraw_class_factory_Release(IClassFactory *iface)
678 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
679 ULONG ref = InterlockedDecrement(&factory->ref);
681 TRACE("%p decreasing refcount to %u.\n", factory, ref);
683 if (!ref)
684 heap_free(factory);
686 return ref;
690 /*******************************************************************************
691 * IDirectDrawClassFactory::CreateInstance
693 * What is this? Seems to create DirectDraw objects...
695 * Params
696 * The usual things???
698 * RETURNS
699 * ???
701 *******************************************************************************/
702 static HRESULT WINAPI ddraw_class_factory_CreateInstance(IClassFactory *iface,
703 IUnknown *outer_unknown, REFIID riid, void **out)
705 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
707 TRACE("iface %p, outer_unknown %p, riid %s, out %p.\n",
708 iface, outer_unknown, debugstr_guid(riid), out);
710 return factory->pfnCreateInstance(outer_unknown, riid, out);
713 /*******************************************************************************
714 * IDirectDrawClassFactory::LockServer
716 * What is this?
718 * Params
719 * ???
721 * RETURNS
722 * S_OK, because it's a stub
724 *******************************************************************************/
725 static HRESULT WINAPI ddraw_class_factory_LockServer(IClassFactory *iface, BOOL dolock)
727 FIXME("iface %p, dolock %#x stub!\n", iface, dolock);
729 return S_OK;
732 /*******************************************************************************
733 * The class factory VTable
734 *******************************************************************************/
735 static const IClassFactoryVtbl IClassFactory_Vtbl =
737 ddraw_class_factory_QueryInterface,
738 ddraw_class_factory_AddRef,
739 ddraw_class_factory_Release,
740 ddraw_class_factory_CreateInstance,
741 ddraw_class_factory_LockServer
744 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **out)
746 struct ddraw_class_factory *factory;
747 unsigned int i;
749 TRACE("rclsid %s, riid %s, out %p.\n",
750 debugstr_guid(rclsid), debugstr_guid(riid), out);
752 if (!IsEqualGUID(&IID_IClassFactory, riid)
753 && !IsEqualGUID(&IID_IUnknown, riid))
754 return E_NOINTERFACE;
756 for (i=0; i < ARRAY_SIZE(object_creation); i++)
758 if (IsEqualGUID(object_creation[i].clsid, rclsid))
759 break;
762 if (i == ARRAY_SIZE(object_creation))
764 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
765 return CLASS_E_CLASSNOTAVAILABLE;
768 if (!(factory = heap_alloc_zero(sizeof(*factory))))
769 return E_OUTOFMEMORY;
771 factory->IClassFactory_iface.lpVtbl = &IClassFactory_Vtbl;
772 factory->ref = 1;
774 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
776 *out = factory;
777 return S_OK;
781 /***********************************************************************
782 * DllMain (DDRAW.0)
784 * Could be used to register DirectDraw drivers, if we have more than
785 * one. Also used to destroy any objects left at unload if the
786 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
788 ***********************************************************************/
789 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
791 switch (reason)
793 case DLL_PROCESS_ATTACH:
795 static HMODULE ddraw_self;
796 HKEY hkey = 0;
797 WNDCLASSA wc;
799 /* Register the window class. This is used to create a hidden window
800 * for D3D rendering, if the application didn't pass one. It can also
801 * be used for creating a device window from SetCooperativeLevel(). */
802 wc.style = CS_HREDRAW | CS_VREDRAW;
803 wc.lpfnWndProc = DefWindowProcA;
804 wc.cbClsExtra = 0;
805 wc.cbWndExtra = 0;
806 wc.hInstance = inst;
807 wc.hIcon = 0;
808 wc.hCursor = 0;
809 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
810 wc.lpszMenuName = NULL;
811 wc.lpszClassName = DDRAW_WINDOW_CLASS_NAME;
812 if (!RegisterClassA(&wc))
814 ERR("Failed to register ddraw window class, last error %#x.\n", GetLastError());
815 return FALSE;
818 /* On Windows one can force the refresh rate that DirectDraw uses by
819 * setting an override value in dxdiag. This is documented in KB315614
820 * (main article), KB230002, and KB217348. By comparing registry dumps
821 * before and after setting the override, we see that the override value
822 * is stored in HKLM\Software\Microsoft\DirectDraw\ForceRefreshRate as a
823 * DWORD that represents the refresh rate to force. We use this
824 * registry entry to modify the behavior of SetDisplayMode so that Wine
825 * users can override the refresh rate in a Windows-compatible way.
827 * dxdiag will not accept a refresh rate lower than 40 or higher than
828 * 120 so this value should be within that range. It is, of course,
829 * possible for a user to set the registry entry value directly so that
830 * assumption might not hold.
832 * There is no current mechanism for setting this value through the Wine
833 * GUI. It would be most appropriate to set this value through a dxdiag
834 * clone, but it may be sufficient to use winecfg.
836 * TODO: Create a mechanism for setting this value through the Wine GUI.
838 if ( !RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectDraw", &hkey ) )
840 DWORD type, data, size;
842 size = sizeof(data);
843 if (!RegQueryValueExA(hkey, "ForceRefreshRate", NULL, &type, (BYTE *)&data, &size) && type == REG_DWORD)
845 TRACE("ForceRefreshRate set; overriding refresh rate to %d Hz\n", data);
846 force_refresh_rate = data;
848 RegCloseKey( hkey );
851 /* Prevent the ddraw module from being unloaded. When switching to
852 * exclusive mode, we replace the window proc of the ddraw window. If
853 * an application would unload ddraw from the WM_DESTROY handler for
854 * that window, it would return to unmapped memory and die. Apparently
855 * this is supposed to work on Windows. */
856 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
857 (const WCHAR *)&ddraw_self, &ddraw_self))
858 ERR("Failed to get own module handle.\n");
860 instance = inst;
861 DisableThreadLibraryCalls(inst);
862 break;
865 case DLL_PROCESS_DETACH:
866 if (WARN_ON(ddraw))
868 struct ddraw *ddraw;
870 LIST_FOR_EACH_ENTRY(ddraw, &global_ddraw_list, struct ddraw, ddraw_list_entry)
872 struct ddraw_surface *surface;
874 WARN("DirectDraw object %p has reference counts {%u, %u, %u, %u, %u}.\n",
875 ddraw, ddraw->ref7, ddraw->ref4, ddraw->ref3, ddraw->ref2, ddraw->ref1);
877 if (ddraw->d3ddevice)
878 WARN("DirectDraw object %p has Direct3D device %p attached.\n", ddraw, ddraw->d3ddevice);
880 LIST_FOR_EACH_ENTRY(surface, &ddraw->surface_list, struct ddraw_surface, surface_list_entry)
882 WARN("Surface %p has reference counts {%u, %u, %u, %u, %u, %u}.\n",
883 surface, surface->ref7, surface->ref4, surface->ref3,
884 surface->ref2, surface->ref1, surface->gamma_count);
889 if (reserved) break;
890 UnregisterClassA(DDRAW_WINDOW_CLASS_NAME, inst);
893 return TRUE;