d3drm: Make it possible to create frames with CreateObject().
[wine.git] / dlls / ddraw / main.c
blob863dc312d779e086b2bcf2d40073830f0cc37d73
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 BOOL 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 struct wined3d_output_desc output_desc;
64 BOOL cont_enum = TRUE;
65 HRESULT hr = S_OK;
66 UINT adapter = 0;
68 for (adapter = 0; SUCCEEDED(hr) && cont_enum; adapter++)
70 char DriverName[512] = "", DriverDescription[512] = "";
72 /* The Battle.net System Checker expects the GetAdapterIdentifier DeviceName to match the
73 * Driver Name, so obtain the DeviceName and GUID from D3D. */
74 memset(&adapter_id, 0x0, sizeof(adapter_id));
75 adapter_id.device_name = DriverName;
76 adapter_id.device_name_size = sizeof(DriverName);
77 adapter_id.description = DriverDescription;
78 adapter_id.description_size = sizeof(DriverDescription);
79 wined3d_mutex_lock();
80 if (SUCCEEDED(hr = wined3d_get_adapter_identifier(wined3d, adapter, 0x0, &adapter_id)))
81 hr = wined3d_get_output_desc(wined3d, adapter, &output_desc);
82 wined3d_mutex_unlock();
83 if (SUCCEEDED(hr))
85 TRACE("Interface %d: %s\n", adapter, wine_dbgstr_guid(&adapter_id.device_identifier));
86 cont_enum = callback(&adapter_id.device_identifier, adapter_id.description,
87 adapter_id.device_name, context, output_desc.monitor);
92 /* Handle table functions */
93 BOOL ddraw_handle_table_init(struct ddraw_handle_table *t, UINT initial_size)
95 t->entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, initial_size * sizeof(*t->entries));
96 if (!t->entries)
98 ERR("Failed to allocate handle table memory.\n");
99 return FALSE;
101 t->free_entries = NULL;
102 t->table_size = initial_size;
103 t->entry_count = 0;
105 return TRUE;
108 void ddraw_handle_table_destroy(struct ddraw_handle_table *t)
110 HeapFree(GetProcessHeap(), 0, t->entries);
111 memset(t, 0, sizeof(*t));
114 DWORD ddraw_allocate_handle(struct ddraw_handle_table *t, void *object, enum ddraw_handle_type type)
116 struct ddraw_handle_entry *entry;
118 if (t->free_entries)
120 DWORD idx = t->free_entries - t->entries;
121 /* Use a free handle */
122 entry = t->free_entries;
123 if (entry->type != DDRAW_HANDLE_FREE)
125 ERR("Handle %#x (%p) is in the free list, but has type %#x.\n", idx, entry->object, entry->type);
126 return DDRAW_INVALID_HANDLE;
128 t->free_entries = entry->object;
129 entry->object = object;
130 entry->type = type;
132 return idx;
135 if (!(t->entry_count < t->table_size))
137 /* Grow the table */
138 UINT new_size = t->table_size + (t->table_size >> 1);
139 struct ddraw_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
140 0, t->entries, new_size * sizeof(*t->entries));
141 if (!new_entries)
143 ERR("Failed to grow the handle table.\n");
144 return DDRAW_INVALID_HANDLE;
146 t->entries = new_entries;
147 t->table_size = new_size;
150 entry = &t->entries[t->entry_count];
151 entry->object = object;
152 entry->type = type;
154 return t->entry_count++;
157 void *ddraw_free_handle(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
159 struct ddraw_handle_entry *entry;
160 void *object;
162 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
164 WARN("Invalid handle %#x passed.\n", handle);
165 return NULL;
168 entry = &t->entries[handle];
169 if (entry->type != type)
171 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
172 return NULL;
175 object = entry->object;
176 entry->object = t->free_entries;
177 entry->type = DDRAW_HANDLE_FREE;
178 t->free_entries = entry;
180 return object;
183 void *ddraw_get_object(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
185 struct ddraw_handle_entry *entry;
187 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
189 WARN("Invalid handle %#x passed.\n", handle);
190 return NULL;
193 entry = &t->entries[handle];
194 if (entry->type != type)
196 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
197 return NULL;
200 return entry->object;
203 HRESULT WINAPI GetSurfaceFromDC(HDC dc, IDirectDrawSurface4 **surface, HDC *device_dc)
205 struct ddraw *ddraw;
207 TRACE("dc %p, surface %p, device_dc %p.\n", dc, surface, device_dc);
209 if (!surface)
210 return E_INVALIDARG;
212 if (!device_dc)
214 *surface = NULL;
216 return E_INVALIDARG;
219 wined3d_mutex_lock();
220 LIST_FOR_EACH_ENTRY(ddraw, &global_ddraw_list, struct ddraw, ddraw_list_entry)
222 if (FAILED(IDirectDraw4_GetSurfaceFromDC(&ddraw->IDirectDraw4_iface, dc, surface)))
223 continue;
225 *device_dc = NULL; /* FIXME */
226 wined3d_mutex_unlock();
227 return DD_OK;
229 wined3d_mutex_unlock();
231 *surface = NULL;
232 *device_dc = NULL;
234 return DDERR_NOTFOUND;
237 /***********************************************************************
239 * Helper function for DirectDrawCreate and friends
240 * Creates a new DDraw interface with the given REFIID
242 * Interfaces that can be created:
243 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
244 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
245 * IDirect3D interfaces?)
247 * Arguments:
248 * guid: ID of the requested driver, NULL for the default driver.
249 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
250 * DD: Used to return the pointer to the created object
251 * UnkOuter: For aggregation, which is unsupported. Must be NULL
252 * iid: requested version ID.
254 * Returns:
255 * DD_OK if the Interface was created successfully
256 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
257 * E_OUTOFMEMORY if some allocation failed
259 ***********************************************************************/
260 static HRESULT
261 DDRAW_Create(const GUID *guid,
262 void **DD,
263 IUnknown *UnkOuter,
264 REFIID iid)
266 enum wined3d_device_type device_type;
267 struct ddraw *ddraw;
268 HRESULT hr;
269 DWORD flags = 0;
271 TRACE("driver_guid %s, ddraw %p, outer_unknown %p, interface_iid %s.\n",
272 debugstr_guid(guid), DD, UnkOuter, debugstr_guid(iid));
274 *DD = NULL;
276 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
278 /* Use the reference device id. This doesn't actually change anything,
279 * WineD3D always uses OpenGL for D3D rendering. One could make it request
280 * indirect rendering
282 device_type = WINED3D_DEVICE_TYPE_REF;
284 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
286 device_type = WINED3D_DEVICE_TYPE_HAL;
288 else
290 device_type = 0;
293 /* DDraw doesn't support aggregation, according to msdn */
294 if (UnkOuter != NULL)
295 return CLASS_E_NOAGGREGATION;
297 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
298 flags = WINED3D_LEGACY_FFP_LIGHTING;
300 /* DirectDraw creation comes here */
301 ddraw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw));
302 if (!ddraw)
304 ERR("Out of memory when creating DirectDraw\n");
305 return E_OUTOFMEMORY;
308 hr = ddraw_init(ddraw, flags, device_type);
309 if (FAILED(hr))
311 WARN("Failed to initialize ddraw object, hr %#x.\n", hr);
312 HeapFree(GetProcessHeap(), 0, ddraw);
313 return hr;
316 hr = IDirectDraw7_QueryInterface(&ddraw->IDirectDraw7_iface, iid, DD);
317 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
318 if (SUCCEEDED(hr))
319 list_add_head(&global_ddraw_list, &ddraw->ddraw_list_entry);
320 else
321 WARN("Failed to query interface %s from ddraw object %p.\n", debugstr_guid(iid), ddraw);
323 return hr;
326 /***********************************************************************
327 * DirectDrawCreate (DDRAW.@)
329 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
330 * interfaces in theory
332 * Arguments, return values: See DDRAW_Create
334 ***********************************************************************/
335 HRESULT WINAPI DECLSPEC_HOTPATCH DirectDrawCreate(GUID *driver_guid, IDirectDraw **ddraw, IUnknown *outer)
337 HRESULT hr;
339 TRACE("driver_guid %s, ddraw %p, outer %p.\n",
340 debugstr_guid(driver_guid), ddraw, outer);
342 wined3d_mutex_lock();
343 hr = DDRAW_Create(driver_guid, (void **)ddraw, outer, &IID_IDirectDraw);
344 wined3d_mutex_unlock();
346 if (SUCCEEDED(hr))
348 if (FAILED(hr = IDirectDraw_Initialize(*ddraw, driver_guid)))
349 IDirectDraw_Release(*ddraw);
352 return hr;
355 /***********************************************************************
356 * DirectDrawCreateEx (DDRAW.@)
358 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
359 * interfaces are requested.
361 * Arguments, return values: See DDRAW_Create
363 ***********************************************************************/
364 HRESULT WINAPI DECLSPEC_HOTPATCH DirectDrawCreateEx(GUID *driver_guid,
365 void **ddraw, REFIID interface_iid, IUnknown *outer)
367 HRESULT hr;
369 TRACE("driver_guid %s, ddraw %p, interface_iid %s, outer %p.\n",
370 debugstr_guid(driver_guid), ddraw, debugstr_guid(interface_iid), outer);
372 if (!IsEqualGUID(interface_iid, &IID_IDirectDraw7))
373 return DDERR_INVALIDPARAMS;
375 wined3d_mutex_lock();
376 hr = DDRAW_Create(driver_guid, ddraw, outer, interface_iid);
377 wined3d_mutex_unlock();
379 if (SUCCEEDED(hr))
381 IDirectDraw7 *ddraw7 = *(IDirectDraw7 **)ddraw;
382 hr = IDirectDraw7_Initialize(ddraw7, driver_guid);
383 if (FAILED(hr))
384 IDirectDraw7_Release(ddraw7);
387 return hr;
390 /***********************************************************************
391 * DirectDrawEnumerateA (DDRAW.@)
393 * Enumerates legacy ddraw drivers, ascii version. We only have one
394 * driver, which relays to WineD3D. If we were sufficiently cool,
395 * we could offer various interfaces, which use a different default surface
396 * implementation, but I think it's better to offer this choice in
397 * winecfg, because some apps use the default driver, so we would need
398 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
400 * Arguments:
401 * Callback: Callback function from the app
402 * Context: Argument to the call back.
404 * Returns:
405 * DD_OK on success
406 * E_INVALIDARG if the Callback caused a page fault
409 ***********************************************************************/
410 HRESULT WINAPI DirectDrawEnumerateA(LPDDENUMCALLBACKA callback, void *context)
412 struct callback_info info;
414 TRACE("callback %p, context %p.\n", callback, context);
416 info.callback = callback;
417 info.context = context;
418 return DirectDrawEnumerateExA(enum_callback, &info, 0x0);
421 /***********************************************************************
422 * DirectDrawEnumerateExA (DDRAW.@)
424 * Enumerates DirectDraw7 drivers, ascii version. See
425 * the comments above DirectDrawEnumerateA for more details.
427 * The Flag member is not supported right now.
429 ***********************************************************************/
430 HRESULT WINAPI DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA callback, void *context, DWORD flags)
432 struct wined3d *wined3d;
434 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
436 if (flags & ~(DDENUM_ATTACHEDSECONDARYDEVICES |
437 DDENUM_DETACHEDSECONDARYDEVICES |
438 DDENUM_NONDISPLAYDEVICES))
439 return DDERR_INVALIDPARAMS;
441 if (flags & ~DDENUM_ATTACHEDSECONDARYDEVICES)
442 FIXME("flags 0x%08x not handled\n", flags & ~DDENUM_ATTACHEDSECONDARYDEVICES);
444 TRACE("Enumerating ddraw interfaces\n");
445 if (!(wined3d = wined3d_create(DDRAW_WINED3D_FLAGS)))
447 if (!(wined3d = wined3d_create(DDRAW_WINED3D_FLAGS | WINED3D_NO3D)))
449 WARN("Failed to create a wined3d object.\n");
450 return E_FAIL;
453 WARN("Created a wined3d object without 3D support.\n");
456 __TRY
458 /* QuickTime expects the description "DirectDraw HAL" */
459 static CHAR driver_desc[] = "DirectDraw HAL",
460 driver_name[] = "display";
461 BOOL cont_enum;
463 TRACE("Default interface: DirectDraw HAL\n");
464 cont_enum = callback(NULL, driver_desc, driver_name, context, 0);
466 /* The Battle.net System Checker expects both a NULL device and a GUID-based device */
467 if (cont_enum && (flags & DDENUM_ATTACHEDSECONDARYDEVICES))
468 ddraw_enumerate_secondary_devices(wined3d, callback, context);
470 __EXCEPT_PAGE_FAULT
472 wined3d_decref(wined3d);
473 return DDERR_INVALIDPARAMS;
475 __ENDTRY;
477 wined3d_decref(wined3d);
478 TRACE("End of enumeration\n");
479 return DD_OK;
482 /***********************************************************************
483 * DirectDrawEnumerateW (DDRAW.@)
485 * Enumerates legacy drivers, unicode version.
486 * This function is not implemented on Windows.
488 ***********************************************************************/
489 HRESULT WINAPI DirectDrawEnumerateW(LPDDENUMCALLBACKW callback, void *context)
491 TRACE("callback %p, context %p.\n", callback, context);
493 if (!callback)
494 return DDERR_INVALIDPARAMS;
495 else
496 return DDERR_UNSUPPORTED;
499 /***********************************************************************
500 * DirectDrawEnumerateExW (DDRAW.@)
502 * Enumerates DirectDraw7 drivers, unicode version.
503 * This function is not implemented on Windows.
505 ***********************************************************************/
506 HRESULT WINAPI DirectDrawEnumerateExW(LPDDENUMCALLBACKEXW callback, void *context, DWORD flags)
508 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
510 return DDERR_UNSUPPORTED;
513 /***********************************************************************
514 * Classfactory implementation.
515 ***********************************************************************/
517 /***********************************************************************
518 * CF_CreateDirectDraw
520 * DDraw creation function for the class factory
522 * Params:
523 * UnkOuter: Set to NULL
524 * iid: ID of the wanted interface
525 * obj: Address to pass the interface pointer back
527 * Returns
528 * DD_OK / DDERR*, see DDRAW_Create
530 ***********************************************************************/
531 static HRESULT
532 CF_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
533 void **obj)
535 HRESULT hr;
537 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(iid), obj);
539 wined3d_mutex_lock();
540 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
541 wined3d_mutex_unlock();
543 return hr;
546 /***********************************************************************
547 * CF_CreateDirectDraw
549 * Clipper creation function for the class factory
551 * Params:
552 * UnkOuter: Set to NULL
553 * iid: ID of the wanted interface
554 * obj: Address to pass the interface pointer back
556 * Returns
557 * DD_OK / DDERR*, see DDRAW_Create
559 ***********************************************************************/
560 static HRESULT
561 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
562 void **obj)
564 HRESULT hr;
565 IDirectDrawClipper *Clip;
567 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(riid), obj);
569 wined3d_mutex_lock();
570 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
571 if (hr != DD_OK)
573 wined3d_mutex_unlock();
574 return hr;
577 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
578 IDirectDrawClipper_Release(Clip);
580 wined3d_mutex_unlock();
582 return hr;
585 static const struct object_creation_info object_creation[] =
587 { &CLSID_DirectDraw, CF_CreateDirectDraw },
588 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
589 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
592 struct ddraw_class_factory
594 IClassFactory IClassFactory_iface;
596 LONG ref;
597 HRESULT (*pfnCreateInstance)(IUnknown *outer, REFIID iid, void **out);
600 static inline struct ddraw_class_factory *impl_from_IClassFactory(IClassFactory *iface)
602 return CONTAINING_RECORD(iface, struct ddraw_class_factory, IClassFactory_iface);
605 /*******************************************************************************
606 * IDirectDrawClassFactory::QueryInterface
608 * QueryInterface for the class factory
610 * PARAMS
611 * riid Reference to identifier of queried interface
612 * ppv Address to return the interface pointer at
614 * RETURNS
615 * Success: S_OK
616 * Failure: E_NOINTERFACE
618 *******************************************************************************/
619 static HRESULT WINAPI ddraw_class_factory_QueryInterface(IClassFactory *iface, REFIID riid, void **out)
621 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
623 if (IsEqualGUID(riid, &IID_IUnknown)
624 || IsEqualGUID(riid, &IID_IClassFactory))
626 IClassFactory_AddRef(iface);
627 *out = iface;
628 return S_OK;
631 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
633 return E_NOINTERFACE;
636 /*******************************************************************************
637 * IDirectDrawClassFactory::AddRef
639 * AddRef for the class factory
641 * RETURNS
642 * The new refcount
644 *******************************************************************************/
645 static ULONG WINAPI ddraw_class_factory_AddRef(IClassFactory *iface)
647 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
648 ULONG ref = InterlockedIncrement(&factory->ref);
650 TRACE("%p increasing refcount to %u.\n", factory, ref);
652 return ref;
655 /*******************************************************************************
656 * IDirectDrawClassFactory::Release
658 * Release for the class factory. If the refcount falls to 0, the object
659 * is destroyed
661 * RETURNS
662 * The new refcount
664 *******************************************************************************/
665 static ULONG WINAPI ddraw_class_factory_Release(IClassFactory *iface)
667 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
668 ULONG ref = InterlockedDecrement(&factory->ref);
670 TRACE("%p decreasing refcount to %u.\n", factory, ref);
672 if (!ref)
673 HeapFree(GetProcessHeap(), 0, factory);
675 return ref;
679 /*******************************************************************************
680 * IDirectDrawClassFactory::CreateInstance
682 * What is this? Seems to create DirectDraw objects...
684 * Params
685 * The usual things???
687 * RETURNS
688 * ???
690 *******************************************************************************/
691 static HRESULT WINAPI ddraw_class_factory_CreateInstance(IClassFactory *iface,
692 IUnknown *outer_unknown, REFIID riid, void **out)
694 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
696 TRACE("iface %p, outer_unknown %p, riid %s, out %p.\n",
697 iface, outer_unknown, debugstr_guid(riid), out);
699 return factory->pfnCreateInstance(outer_unknown, riid, out);
702 /*******************************************************************************
703 * IDirectDrawClassFactory::LockServer
705 * What is this?
707 * Params
708 * ???
710 * RETURNS
711 * S_OK, because it's a stub
713 *******************************************************************************/
714 static HRESULT WINAPI ddraw_class_factory_LockServer(IClassFactory *iface, BOOL dolock)
716 FIXME("iface %p, dolock %#x stub!\n", iface, dolock);
718 return S_OK;
721 /*******************************************************************************
722 * The class factory VTable
723 *******************************************************************************/
724 static const IClassFactoryVtbl IClassFactory_Vtbl =
726 ddraw_class_factory_QueryInterface,
727 ddraw_class_factory_AddRef,
728 ddraw_class_factory_Release,
729 ddraw_class_factory_CreateInstance,
730 ddraw_class_factory_LockServer
733 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **out)
735 struct ddraw_class_factory *factory;
736 unsigned int i;
738 TRACE("rclsid %s, riid %s, out %p.\n",
739 debugstr_guid(rclsid), debugstr_guid(riid), out);
741 if (!IsEqualGUID(&IID_IClassFactory, riid)
742 && !IsEqualGUID(&IID_IUnknown, riid))
743 return E_NOINTERFACE;
745 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
747 if (IsEqualGUID(object_creation[i].clsid, rclsid))
748 break;
751 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
753 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
754 return CLASS_E_CLASSNOTAVAILABLE;
757 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
758 if (factory == NULL) return E_OUTOFMEMORY;
760 factory->IClassFactory_iface.lpVtbl = &IClassFactory_Vtbl;
761 factory->ref = 1;
763 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
765 *out = factory;
766 return S_OK;
770 /*******************************************************************************
771 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
773 * RETURNS
774 * Success: S_OK
775 * Failure: S_FALSE
777 HRESULT WINAPI DllCanUnloadNow(void)
779 TRACE("\n");
781 return S_FALSE;
785 HRESULT WINAPI DllRegisterServer(void)
787 return __wine_register_resources( instance );
790 HRESULT WINAPI DllUnregisterServer(void)
792 return __wine_unregister_resources( instance );
795 /***********************************************************************
796 * DllMain (DDRAW.0)
798 * Could be used to register DirectDraw drivers, if we have more than
799 * one. Also used to destroy any objects left at unload if the
800 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
802 ***********************************************************************/
803 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
805 switch (reason)
807 case DLL_PROCESS_ATTACH:
809 static HMODULE ddraw_self;
810 HKEY hkey = 0;
811 WNDCLASSA wc;
813 /* Register the window class. This is used to create a hidden window
814 * for D3D rendering, if the application didn't pass one. It can also
815 * be used for creating a device window from SetCooperativeLevel(). */
816 wc.style = CS_HREDRAW | CS_VREDRAW;
817 wc.lpfnWndProc = DefWindowProcA;
818 wc.cbClsExtra = 0;
819 wc.cbWndExtra = 0;
820 wc.hInstance = inst;
821 wc.hIcon = 0;
822 wc.hCursor = 0;
823 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
824 wc.lpszMenuName = NULL;
825 wc.lpszClassName = DDRAW_WINDOW_CLASS_NAME;
826 if (!RegisterClassA(&wc))
828 ERR("Failed to register ddraw window class, last error %#x.\n", GetLastError());
829 return FALSE;
832 /* On Windows one can force the refresh rate that DirectDraw uses by
833 * setting an override value in dxdiag. This is documented in KB315614
834 * (main article), KB230002, and KB217348. By comparing registry dumps
835 * before and after setting the override, we see that the override value
836 * is stored in HKLM\Software\Microsoft\DirectDraw\ForceRefreshRate as a
837 * DWORD that represents the refresh rate to force. We use this
838 * registry entry to modify the behavior of SetDisplayMode so that Wine
839 * users can override the refresh rate in a Windows-compatible way.
841 * dxdiag will not accept a refresh rate lower than 40 or higher than
842 * 120 so this value should be within that range. It is, of course,
843 * possible for a user to set the registry entry value directly so that
844 * assumption might not hold.
846 * There is no current mechanism for setting this value through the Wine
847 * GUI. It would be most appropriate to set this value through a dxdiag
848 * clone, but it may be sufficient to use winecfg.
850 * TODO: Create a mechanism for setting this value through the Wine GUI.
852 if ( !RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectDraw", &hkey ) )
854 DWORD type, data, size;
856 size = sizeof(data);
857 if (!RegQueryValueExA(hkey, "ForceRefreshRate", NULL, &type, (BYTE *)&data, &size) && type == REG_DWORD)
859 TRACE("ForceRefreshRate set; overriding refresh rate to %d Hz\n", data);
860 force_refresh_rate = data;
862 RegCloseKey( hkey );
865 /* Prevent the ddraw module from being unloaded. When switching to
866 * exclusive mode, we replace the window proc of the ddraw window. If
867 * an application would unload ddraw from the WM_DESTROY handler for
868 * that window, it would return to unmapped memory and die. Apparently
869 * this is supposed to work on Windows. */
870 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
871 (const WCHAR *)&ddraw_self, &ddraw_self))
872 ERR("Failed to get own module handle.\n");
874 instance = inst;
875 DisableThreadLibraryCalls(inst);
876 break;
879 case DLL_PROCESS_DETACH:
880 if (WARN_ON(ddraw))
882 struct ddraw *ddraw;
884 LIST_FOR_EACH_ENTRY(ddraw, &global_ddraw_list, struct ddraw, ddraw_list_entry)
886 struct ddraw_surface *surface;
888 WARN("DirectDraw object %p has reference counts {%u, %u, %u, %u, %u}.\n",
889 ddraw, ddraw->ref7, ddraw->ref4, ddraw->ref3, ddraw->ref2, ddraw->ref1);
891 if (ddraw->d3ddevice)
892 WARN("DirectDraw object %p has Direct3D device %p attached.\n", ddraw, ddraw->d3ddevice);
894 LIST_FOR_EACH_ENTRY(surface, &ddraw->surface_list, struct ddraw_surface, surface_list_entry)
896 WARN("Surface %p has reference counts {%u, %u, %u, %u, %u, %u}.\n",
897 surface, surface->ref7, surface->ref4, surface->ref3,
898 surface->ref2, surface->ref1, surface->gamma_count);
903 if (reserved) break;
904 UnregisterClassA(DDRAW_WINDOW_CLASS_NAME, inst);
907 return TRUE;