wrc: Fix parsing of virtkey accelerators.
[wine/multimedia.git] / dlls / ddraw / main.c
blobb1456d846c4ed44ceb1b476fe577cfe46f324a47
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 /* The configured default surface */
40 enum wined3d_surface_type DefaultSurfaceType = WINED3D_SURFACE_TYPE_OPENGL;
42 static struct list global_ddraw_list = LIST_INIT(global_ddraw_list);
44 static HINSTANCE instance;
46 /* value of ForceRefreshRate */
47 DWORD force_refresh_rate = 0;
49 /* Handle table functions */
50 BOOL ddraw_handle_table_init(struct ddraw_handle_table *t, UINT initial_size)
52 t->entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, initial_size * sizeof(*t->entries));
53 if (!t->entries)
55 ERR("Failed to allocate handle table memory.\n");
56 return FALSE;
58 t->free_entries = NULL;
59 t->table_size = initial_size;
60 t->entry_count = 0;
62 return TRUE;
65 void ddraw_handle_table_destroy(struct ddraw_handle_table *t)
67 HeapFree(GetProcessHeap(), 0, t->entries);
68 memset(t, 0, sizeof(*t));
71 DWORD ddraw_allocate_handle(struct ddraw_handle_table *t, void *object, enum ddraw_handle_type type)
73 struct ddraw_handle_entry *entry;
75 if (t->free_entries)
77 DWORD idx = t->free_entries - t->entries;
78 /* Use a free handle */
79 entry = t->free_entries;
80 if (entry->type != DDRAW_HANDLE_FREE)
82 ERR("Handle %#x (%p) is in the free list, but has type %#x.\n", idx, entry->object, entry->type);
83 return DDRAW_INVALID_HANDLE;
85 t->free_entries = entry->object;
86 entry->object = object;
87 entry->type = type;
89 return idx;
92 if (!(t->entry_count < t->table_size))
94 /* Grow the table */
95 UINT new_size = t->table_size + (t->table_size >> 1);
96 struct ddraw_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
97 0, t->entries, new_size * sizeof(*t->entries));
98 if (!new_entries)
100 ERR("Failed to grow the handle table.\n");
101 return DDRAW_INVALID_HANDLE;
103 t->entries = new_entries;
104 t->table_size = new_size;
107 entry = &t->entries[t->entry_count];
108 entry->object = object;
109 entry->type = type;
111 return t->entry_count++;
114 void *ddraw_free_handle(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
116 struct ddraw_handle_entry *entry;
117 void *object;
119 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
121 WARN("Invalid handle %#x passed.\n", handle);
122 return NULL;
125 entry = &t->entries[handle];
126 if (entry->type != type)
128 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
129 return NULL;
132 object = entry->object;
133 entry->object = t->free_entries;
134 entry->type = DDRAW_HANDLE_FREE;
135 t->free_entries = entry;
137 return object;
140 void *ddraw_get_object(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
142 struct ddraw_handle_entry *entry;
144 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
146 WARN("Invalid handle %#x passed.\n", handle);
147 return NULL;
150 entry = &t->entries[handle];
151 if (entry->type != type)
153 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
154 return NULL;
157 return entry->object;
160 /***********************************************************************
162 * Helper function for DirectDrawCreate and friends
163 * Creates a new DDraw interface with the given REFIID
165 * Interfaces that can be created:
166 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
167 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
168 * IDirect3D interfaces?)
170 * Arguments:
171 * guid: ID of the requested driver, NULL for the default driver.
172 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
173 * DD: Used to return the pointer to the created object
174 * UnkOuter: For aggregation, which is unsupported. Must be NULL
175 * iid: requested version ID.
177 * Returns:
178 * DD_OK if the Interface was created successfully
179 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
180 * E_OUTOFMEMORY if some allocation failed
182 ***********************************************************************/
183 static HRESULT
184 DDRAW_Create(const GUID *guid,
185 void **DD,
186 IUnknown *UnkOuter,
187 REFIID iid)
189 enum wined3d_device_type device_type;
190 IDirectDrawImpl *This;
191 HRESULT hr;
193 TRACE("driver_guid %s, ddraw %p, outer_unknown %p, interface_iid %s.\n",
194 debugstr_guid(guid), DD, UnkOuter, debugstr_guid(iid));
196 *DD = NULL;
198 /* We don't care about this guids. Well, there's no special guid anyway
199 * OK, we could
201 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
203 /* Use the reference device id. This doesn't actually change anything,
204 * WineD3D always uses OpenGL for D3D rendering. One could make it request
205 * indirect rendering
207 device_type = WINED3D_DEVICE_TYPE_REF;
209 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
211 device_type = WINED3D_DEVICE_TYPE_HAL;
213 else
215 device_type = 0;
218 /* DDraw doesn't support aggregation, according to msdn */
219 if (UnkOuter != NULL)
220 return CLASS_E_NOAGGREGATION;
222 /* DirectDraw creation comes here */
223 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawImpl));
224 if(!This)
226 ERR("Out of memory when creating DirectDraw\n");
227 return E_OUTOFMEMORY;
230 hr = ddraw_init(This, device_type);
231 if (FAILED(hr))
233 WARN("Failed to initialize ddraw object, hr %#x.\n", hr);
234 HeapFree(GetProcessHeap(), 0, This);
235 return hr;
238 hr = IDirectDraw7_QueryInterface(&This->IDirectDraw7_iface, iid, DD);
239 IDirectDraw7_Release(&This->IDirectDraw7_iface);
240 if (SUCCEEDED(hr)) list_add_head(&global_ddraw_list, &This->ddraw_list_entry);
241 else WARN("Failed to query interface %s from ddraw object %p.\n", debugstr_guid(iid), This);
243 return hr;
246 /***********************************************************************
247 * DirectDrawCreate (DDRAW.@)
249 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
250 * interfaces in theory
252 * Arguments, return values: See DDRAW_Create
254 ***********************************************************************/
255 HRESULT WINAPI DECLSPEC_HOTPATCH
256 DirectDrawCreate(GUID *GUID,
257 LPDIRECTDRAW *DD,
258 IUnknown *UnkOuter)
260 HRESULT hr;
262 TRACE("driver_guid %s, ddraw %p, outer_unknown %p.\n",
263 debugstr_guid(GUID), DD, UnkOuter);
265 wined3d_mutex_lock();
266 hr = DDRAW_Create(GUID, (void **) DD, UnkOuter, &IID_IDirectDraw);
267 wined3d_mutex_unlock();
269 if (SUCCEEDED(hr))
271 hr = IDirectDraw_Initialize(*DD, GUID);
272 if (FAILED(hr))
273 IDirectDraw_Release(*DD);
276 return hr;
279 /***********************************************************************
280 * DirectDrawCreateEx (DDRAW.@)
282 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
283 * interfaces are requested.
285 * Arguments, return values: See DDRAW_Create
287 ***********************************************************************/
288 HRESULT WINAPI DECLSPEC_HOTPATCH
289 DirectDrawCreateEx(GUID *guid,
290 LPVOID *dd,
291 REFIID iid,
292 IUnknown *UnkOuter)
294 HRESULT hr;
296 TRACE("driver_guid %s, ddraw %p, interface_iid %s, outer_unknown %p.\n",
297 debugstr_guid(guid), dd, debugstr_guid(iid), UnkOuter);
299 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
300 return DDERR_INVALIDPARAMS;
302 wined3d_mutex_lock();
303 hr = DDRAW_Create(guid, dd, UnkOuter, iid);
304 wined3d_mutex_unlock();
306 if (SUCCEEDED(hr))
308 IDirectDraw7 *ddraw7 = *(IDirectDraw7 **)dd;
309 hr = IDirectDraw7_Initialize(ddraw7, guid);
310 if (FAILED(hr))
311 IDirectDraw7_Release(ddraw7);
314 return hr;
317 /***********************************************************************
318 * DirectDrawEnumerateA (DDRAW.@)
320 * Enumerates legacy ddraw drivers, ascii version. We only have one
321 * driver, which relays to WineD3D. If we were sufficiently cool,
322 * we could offer various interfaces, which use a different default surface
323 * implementation, but I think it's better to offer this choice in
324 * winecfg, because some apps use the default driver, so we would need
325 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
327 * Arguments:
328 * Callback: Callback function from the app
329 * Context: Argument to the call back.
331 * Returns:
332 * DD_OK on success
333 * E_INVALIDARG if the Callback caused a page fault
336 ***********************************************************************/
337 HRESULT WINAPI DirectDrawEnumerateA(LPDDENUMCALLBACKA Callback, void *Context)
339 TRACE("callback %p, context %p.\n", Callback, Context);
341 TRACE(" Enumerating default DirectDraw HAL interface\n");
342 /* We only have one driver */
343 __TRY
345 static CHAR driver_desc[] = "DirectDraw HAL",
346 driver_name[] = "display";
348 Callback(NULL, driver_desc, driver_name, Context);
350 __EXCEPT_PAGE_FAULT
352 return DDERR_INVALIDPARAMS;
354 __ENDTRY
356 TRACE(" End of enumeration\n");
357 return DD_OK;
360 /***********************************************************************
361 * DirectDrawEnumerateExA (DDRAW.@)
363 * Enumerates DirectDraw7 drivers, ascii version. See
364 * the comments above DirectDrawEnumerateA for more details.
366 * The Flag member is not supported right now.
368 ***********************************************************************/
369 HRESULT WINAPI DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA Callback, void *Context, DWORD Flags)
371 TRACE("callback %p, context %p, flags %#x.\n", Callback, Context, Flags);
373 if (Flags & ~(DDENUM_ATTACHEDSECONDARYDEVICES |
374 DDENUM_DETACHEDSECONDARYDEVICES |
375 DDENUM_NONDISPLAYDEVICES))
376 return DDERR_INVALIDPARAMS;
378 if (Flags)
379 FIXME("flags 0x%08x not handled\n", Flags);
381 TRACE("Enumerating default DirectDraw HAL interface\n");
383 /* We only have one driver by now */
384 __TRY
386 static CHAR driver_desc[] = "DirectDraw HAL",
387 driver_name[] = "display";
389 /* QuickTime expects the description "DirectDraw HAL" */
390 Callback(NULL, driver_desc, driver_name, Context, 0);
392 __EXCEPT_PAGE_FAULT
394 return DDERR_INVALIDPARAMS;
396 __ENDTRY;
398 TRACE("End of enumeration\n");
399 return DD_OK;
402 /***********************************************************************
403 * DirectDrawEnumerateW (DDRAW.@)
405 * Enumerates legacy drivers, unicode version.
406 * This function is not implemented on Windows.
408 ***********************************************************************/
409 HRESULT WINAPI DirectDrawEnumerateW(LPDDENUMCALLBACKW callback, void *context)
411 TRACE("callback %p, context %p.\n", callback, context);
413 if (!callback)
414 return DDERR_INVALIDPARAMS;
415 else
416 return DDERR_UNSUPPORTED;
419 /***********************************************************************
420 * DirectDrawEnumerateExW (DDRAW.@)
422 * Enumerates DirectDraw7 drivers, unicode version.
423 * This function is not implemented on Windows.
425 ***********************************************************************/
426 HRESULT WINAPI DirectDrawEnumerateExW(LPDDENUMCALLBACKEXW callback, void *context, DWORD flags)
428 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
430 return DDERR_UNSUPPORTED;
433 /***********************************************************************
434 * Classfactory implementation.
435 ***********************************************************************/
437 /***********************************************************************
438 * CF_CreateDirectDraw
440 * DDraw creation function for the class factory
442 * Params:
443 * UnkOuter: Set to NULL
444 * iid: ID of the wanted interface
445 * obj: Address to pass the interface pointer back
447 * Returns
448 * DD_OK / DDERR*, see DDRAW_Create
450 ***********************************************************************/
451 static HRESULT
452 CF_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
453 void **obj)
455 HRESULT hr;
457 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(iid), obj);
459 wined3d_mutex_lock();
460 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
461 wined3d_mutex_unlock();
463 return hr;
466 /***********************************************************************
467 * CF_CreateDirectDraw
469 * Clipper creation function for the class factory
471 * Params:
472 * UnkOuter: Set to NULL
473 * iid: ID of the wanted interface
474 * obj: Address to pass the interface pointer back
476 * Returns
477 * DD_OK / DDERR*, see DDRAW_Create
479 ***********************************************************************/
480 static HRESULT
481 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
482 void **obj)
484 HRESULT hr;
485 IDirectDrawClipper *Clip;
487 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(riid), obj);
489 wined3d_mutex_lock();
490 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
491 if (hr != DD_OK)
493 wined3d_mutex_unlock();
494 return hr;
497 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
498 IDirectDrawClipper_Release(Clip);
500 wined3d_mutex_unlock();
502 return hr;
505 static const struct object_creation_info object_creation[] =
507 { &CLSID_DirectDraw, CF_CreateDirectDraw },
508 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
509 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
513 /******************************************************************************
514 * DirectDraw ClassFactory implementation
515 ******************************************************************************/
516 typedef struct
518 IClassFactory IClassFactory_iface;
520 LONG ref;
521 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID iid, LPVOID *ppObj);
522 } IClassFactoryImpl;
524 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
526 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
529 /*******************************************************************************
530 * IDirectDrawClassFactory::QueryInterface
532 * QueryInterface for the class factory
534 * PARAMS
535 * riid Reference to identifier of queried interface
536 * ppv Address to return the interface pointer at
538 * RETURNS
539 * Success: S_OK
540 * Failure: E_NOINTERFACE
542 *******************************************************************************/
543 static HRESULT WINAPI IDirectDrawClassFactoryImpl_QueryInterface(IClassFactory *iface, REFIID riid,
544 void **obj)
546 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
548 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
550 if (IsEqualGUID(riid, &IID_IUnknown)
551 || IsEqualGUID(riid, &IID_IClassFactory))
553 IClassFactory_AddRef(iface);
554 *obj = This;
555 return S_OK;
558 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),obj);
559 return E_NOINTERFACE;
562 /*******************************************************************************
563 * IDirectDrawClassFactory::AddRef
565 * AddRef for the class factory
567 * RETURNS
568 * The new refcount
570 *******************************************************************************/
571 static ULONG WINAPI IDirectDrawClassFactoryImpl_AddRef(IClassFactory *iface)
573 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
574 ULONG ref = InterlockedIncrement(&This->ref);
576 TRACE("%p increasing refcount to %u.\n", This, ref);
578 return ref;
581 /*******************************************************************************
582 * IDirectDrawClassFactory::Release
584 * Release for the class factory. If the refcount falls to 0, the object
585 * is destroyed
587 * RETURNS
588 * The new refcount
590 *******************************************************************************/
591 static ULONG WINAPI IDirectDrawClassFactoryImpl_Release(IClassFactory *iface)
593 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
594 ULONG ref = InterlockedDecrement(&This->ref);
596 TRACE("%p decreasing refcount to %u.\n", This, ref);
598 if (ref == 0)
599 HeapFree(GetProcessHeap(), 0, This);
601 return ref;
605 /*******************************************************************************
606 * IDirectDrawClassFactory::CreateInstance
608 * What is this? Seems to create DirectDraw objects...
610 * Params
611 * The usual things???
613 * RETURNS
614 * ???
616 *******************************************************************************/
617 static HRESULT WINAPI IDirectDrawClassFactoryImpl_CreateInstance(IClassFactory *iface,
618 IUnknown *UnkOuter, REFIID riid, void **obj)
620 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
622 TRACE("iface %p, outer_unknown %p, riid %s, object %p.\n",
623 iface, UnkOuter, debugstr_guid(riid), obj);
625 return This->pfnCreateInstance(UnkOuter, riid, obj);
628 /*******************************************************************************
629 * IDirectDrawClassFactory::LockServer
631 * What is this?
633 * Params
634 * ???
636 * RETURNS
637 * S_OK, because it's a stub
639 *******************************************************************************/
640 static HRESULT WINAPI IDirectDrawClassFactoryImpl_LockServer(IClassFactory *iface, BOOL dolock)
642 FIXME("iface %p, dolock %#x stub!\n", iface, dolock);
644 return S_OK;
647 /*******************************************************************************
648 * The class factory VTable
649 *******************************************************************************/
650 static const IClassFactoryVtbl IClassFactory_Vtbl =
652 IDirectDrawClassFactoryImpl_QueryInterface,
653 IDirectDrawClassFactoryImpl_AddRef,
654 IDirectDrawClassFactoryImpl_Release,
655 IDirectDrawClassFactoryImpl_CreateInstance,
656 IDirectDrawClassFactoryImpl_LockServer
659 /*******************************************************************************
660 * DllGetClassObject [DDRAW.@]
661 * Retrieves class object from a DLL object
663 * NOTES
664 * Docs say returns STDAPI
666 * PARAMS
667 * rclsid [I] CLSID for the class object
668 * riid [I] Reference to identifier of interface for class object
669 * ppv [O] Address of variable to receive interface pointer for riid
671 * RETURNS
672 * Success: S_OK
673 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
674 * E_UNEXPECTED
676 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
678 unsigned int i;
679 IClassFactoryImpl *factory;
681 TRACE("rclsid %s, riid %s, object %p.\n",
682 debugstr_guid(rclsid), debugstr_guid(riid), ppv);
684 if (!IsEqualGUID(&IID_IClassFactory, riid)
685 && !IsEqualGUID(&IID_IUnknown, riid))
686 return E_NOINTERFACE;
688 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
690 if (IsEqualGUID(object_creation[i].clsid, rclsid))
691 break;
694 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
696 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
697 return CLASS_E_CLASSNOTAVAILABLE;
700 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
701 if (factory == NULL) return E_OUTOFMEMORY;
703 factory->IClassFactory_iface.lpVtbl = &IClassFactory_Vtbl;
704 factory->ref = 1;
706 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
708 *ppv = factory;
709 return S_OK;
713 /*******************************************************************************
714 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
716 * RETURNS
717 * Success: S_OK
718 * Failure: S_FALSE
720 HRESULT WINAPI DllCanUnloadNow(void)
722 TRACE("\n");
724 return S_FALSE;
728 /***********************************************************************
729 * DllRegisterServer (DDRAW.@)
731 HRESULT WINAPI DllRegisterServer(void)
733 return __wine_register_resources( instance );
736 /***********************************************************************
737 * DllUnregisterServer (DDRAW.@)
739 HRESULT WINAPI DllUnregisterServer(void)
741 return __wine_unregister_resources( instance );
744 /*******************************************************************************
745 * DestroyCallback
747 * Callback function for the EnumSurfaces call in DllMain.
748 * Dumps some surface info and releases the surface
750 * Params:
751 * surf: The enumerated surface
752 * desc: it's description
753 * context: Pointer to the ddraw impl
755 * Returns:
756 * DDENUMRET_OK;
757 *******************************************************************************/
758 static HRESULT WINAPI
759 DestroyCallback(IDirectDrawSurface7 *surf,
760 DDSURFACEDESC2 *desc,
761 void *context)
763 IDirectDrawSurfaceImpl *Impl = impl_from_IDirectDrawSurface7(surf);
764 ULONG ref7, ref4, ref3, ref2, ref1, gamma_count, iface_count;
766 ref7 = IDirectDrawSurface7_Release(surf); /* For the EnumSurfaces */
767 ref4 = Impl->ref4;
768 ref3 = Impl->ref3;
769 ref2 = Impl->ref2;
770 ref1 = Impl->ref1;
771 gamma_count = Impl->gamma_count;
773 WARN("Surface %p has an reference counts of 7: %u 4: %u 3: %u 2: %u 1: %u gamma: %u\n",
774 Impl, ref7, ref4, ref3, ref2, ref1, gamma_count);
776 /* Skip surfaces which are attached somewhere or which are
777 * part of a complex compound. They will get released when destroying
778 * the root
780 if( (!Impl->is_complex_root) || (Impl->first_attached != Impl) )
781 return DDENUMRET_OK;
783 /* Destroy the surface */
784 iface_count = ddraw_surface_release_iface(Impl);
785 while (iface_count) iface_count = ddraw_surface_release_iface(Impl);
787 return DDENUMRET_OK;
790 /***********************************************************************
791 * get_config_key
793 * Reads a config key from the registry. Taken from WineD3D
795 ***********************************************************************/
796 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
798 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
799 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
800 return ERROR_FILE_NOT_FOUND;
803 /***********************************************************************
804 * DllMain (DDRAW.0)
806 * Could be used to register DirectDraw drivers, if we have more than
807 * one. Also used to destroy any objects left at unload if the
808 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
810 ***********************************************************************/
811 BOOL WINAPI
812 DllMain(HINSTANCE hInstDLL,
813 DWORD Reason,
814 LPVOID lpv)
816 TRACE("(%p,%x,%p)\n", hInstDLL, Reason, lpv);
817 if (Reason == DLL_PROCESS_ATTACH)
819 char buffer[MAX_PATH+10];
820 DWORD size = sizeof(buffer);
821 HKEY hkey = 0;
822 HKEY appkey = 0;
823 WNDCLASSA wc;
824 DWORD len;
826 /* Register the window class. This is used to create a hidden window
827 * for D3D rendering, if the application didn't pass one. It can also
828 * be used for creating a device window from SetCooperativeLevel(). */
829 wc.style = CS_HREDRAW | CS_VREDRAW;
830 wc.lpfnWndProc = DefWindowProcA;
831 wc.cbClsExtra = 0;
832 wc.cbWndExtra = 0;
833 wc.hInstance = hInstDLL;
834 wc.hIcon = 0;
835 wc.hCursor = 0;
836 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
837 wc.lpszMenuName = NULL;
838 wc.lpszClassName = DDRAW_WINDOW_CLASS_NAME;
839 if (!RegisterClassA(&wc))
841 ERR("Failed to register ddraw window class, last error %#x.\n", GetLastError());
842 return FALSE;
845 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
846 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
848 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
849 if (len && len < MAX_PATH)
851 HKEY tmpkey;
852 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
853 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
855 char *p, *appname = buffer;
856 if ((p = strrchr( appname, '/' ))) appname = p + 1;
857 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
858 strcat( appname, "\\Direct3D" );
859 TRACE("appname = [%s]\n", appname);
860 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
861 RegCloseKey( tmpkey );
865 if ( 0 != hkey || 0 != appkey )
867 if ( !get_config_key( hkey, appkey, "DirectDrawRenderer", buffer, size) )
869 if (!strcmp(buffer,"gdi"))
871 TRACE("Defaulting to GDI surfaces\n");
872 DefaultSurfaceType = WINED3D_SURFACE_TYPE_GDI;
874 else if (!strcmp(buffer,"opengl"))
876 TRACE("Defaulting to opengl surfaces\n");
877 DefaultSurfaceType = WINED3D_SURFACE_TYPE_OPENGL;
879 else
881 ERR("Unknown default surface type. Supported are:\n gdi, opengl\n");
886 /* On Windows one can force the refresh rate that DirectDraw uses by
887 * setting an override value in dxdiag. This is documented in KB315614
888 * (main article), KB230002, and KB217348. By comparing registry dumps
889 * before and after setting the override, we see that the override value
890 * is stored in HKLM\Software\Microsoft\DirectDraw\ForceRefreshRate as a
891 * DWORD that represents the refresh rate to force. We use this
892 * registry entry to modify the behavior of SetDisplayMode so that Wine
893 * users can override the refresh rate in a Windows-compatible way.
895 * dxdiag will not accept a refresh rate lower than 40 or higher than
896 * 120 so this value should be within that range. It is, of course,
897 * possible for a user to set the registry entry value directly so that
898 * assumption might not hold.
900 * There is no current mechanism for setting this value through the Wine
901 * GUI. It would be most appropriate to set this value through a dxdiag
902 * clone, but it may be sufficient to use winecfg.
904 * TODO: Create a mechanism for setting this value through the Wine GUI.
906 if ( !RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectDraw", &hkey ) )
908 DWORD type, data;
909 size = sizeof(data);
910 if (!RegQueryValueExA( hkey, "ForceRefreshRate", NULL, &type, (LPBYTE)&data, &size ) && type == REG_DWORD)
912 TRACE("ForceRefreshRate set; overriding refresh rate to %d Hz\n", data);
913 force_refresh_rate = data;
915 RegCloseKey( hkey );
918 instance = hInstDLL;
919 DisableThreadLibraryCalls(hInstDLL);
921 else if (Reason == DLL_PROCESS_DETACH)
923 if(!list_empty(&global_ddraw_list))
925 struct list *entry, *entry2;
926 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
928 /* We remove elements from this loop */
929 LIST_FOR_EACH_SAFE(entry, entry2, &global_ddraw_list)
931 HRESULT hr;
932 DDSURFACEDESC2 desc;
933 int i;
934 IDirectDrawImpl *ddraw = LIST_ENTRY(entry, IDirectDrawImpl, ddraw_list_entry);
936 WARN("DDraw %p has a refcount of %d\n", ddraw, ddraw->ref7 + ddraw->ref4 + ddraw->ref3 + ddraw->ref2 + ddraw->ref1);
938 /* Add references to each interface to avoid freeing them unexpectedly */
939 IDirectDraw_AddRef(&ddraw->IDirectDraw_iface);
940 IDirectDraw2_AddRef(&ddraw->IDirectDraw2_iface);
941 IDirectDraw4_AddRef(&ddraw->IDirectDraw4_iface);
942 IDirectDraw7_AddRef(&ddraw->IDirectDraw7_iface);
944 /* Does a D3D device exist? Destroy it
945 * TODO: Destroy all Vertex buffers, Lights, Materials
946 * and execute buffers too
948 if(ddraw->d3ddevice)
950 WARN("DDraw %p has d3ddevice %p attached\n", ddraw, ddraw->d3ddevice);
951 while(IDirect3DDevice7_Release(&ddraw->d3ddevice->IDirect3DDevice7_iface));
954 /* Destroy the swapchain after any 3D device. The 3D device
955 * cleanup code needs a swapchain. Specifically, it tries to
956 * set the current render target to the front buffer. */
957 if (ddraw->wined3d_swapchain)
958 ddraw_destroy_swapchain(ddraw);
960 /* Try to release the objects
961 * Do an EnumSurfaces to find any hanging surfaces
963 memset(&desc, 0, sizeof(desc));
964 desc.dwSize = sizeof(desc);
965 for(i = 0; i <= 1; i++)
967 hr = IDirectDraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, DDENUMSURFACES_ALL,
968 &desc, ddraw, DestroyCallback);
969 if(hr != D3D_OK)
970 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw);
973 if (!list_empty(&ddraw->surface_list))
974 ERR("DDraw %p still has surfaces attached.\n", ddraw);
976 /* Release all hanging references to destroy the objects. This
977 * restores the screen mode too
979 while(IDirectDraw_Release(&ddraw->IDirectDraw_iface));
980 while(IDirectDraw2_Release(&ddraw->IDirectDraw2_iface));
981 while(IDirectDraw4_Release(&ddraw->IDirectDraw4_iface));
982 while(IDirectDraw7_Release(&ddraw->IDirectDraw7_iface));
986 /* Unregister the window class. */
987 UnregisterClassA(DDRAW_WINDOW_CLASS_NAME, hInstDLL);
990 return TRUE;