xcopy: Remove spaces before '\n's in the Korean translation.
[wine/hramrach.git] / dlls / ddraw / main.c
blob13729e247696de10f2726397e0ed5f50a1229aab
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"
33 #include "wine/exception.h"
34 #include "winreg.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
38 /* The configured default surface */
39 WINED3DSURFTYPE DefaultSurfaceType = SURFACE_UNKNOWN;
41 typeof(WineDirect3DCreateClipper) *pWineDirect3DCreateClipper DECLSPEC_HIDDEN;
42 typeof(WineDirect3DCreate) *pWineDirect3DCreate DECLSPEC_HIDDEN;
44 /* DDraw list and critical section */
45 static struct list global_ddraw_list = LIST_INIT(global_ddraw_list);
47 static CRITICAL_SECTION_DEBUG ddraw_cs_debug =
49 0, 0, &ddraw_cs,
50 { &ddraw_cs_debug.ProcessLocksList,
51 &ddraw_cs_debug.ProcessLocksList },
52 0, 0, { (DWORD_PTR)(__FILE__ ": ddraw_cs") }
54 CRITICAL_SECTION ddraw_cs = { &ddraw_cs_debug, -1, 0, 0, 0, 0 };
56 /* value of ForceRefreshRate */
57 DWORD force_refresh_rate = 0;
59 /* Handle table functions */
60 BOOL ddraw_handle_table_init(struct ddraw_handle_table *t, UINT initial_size)
62 t->entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, initial_size * sizeof(*t->entries));
63 if (!t->entries)
65 ERR("Failed to allocate handle table memory.\n");
66 return FALSE;
68 t->free_entries = NULL;
69 t->table_size = initial_size;
70 t->entry_count = 0;
72 return TRUE;
75 void ddraw_handle_table_destroy(struct ddraw_handle_table *t)
77 HeapFree(GetProcessHeap(), 0, t->entries);
78 memset(t, 0, sizeof(*t));
81 DWORD ddraw_allocate_handle(struct ddraw_handle_table *t, void *object, enum ddraw_handle_type type)
83 struct ddraw_handle_entry *entry;
85 if (t->free_entries)
87 DWORD idx = t->free_entries - t->entries;
88 /* Use a free handle */
89 entry = t->free_entries;
90 if (entry->type != DDRAW_HANDLE_FREE)
92 ERR("Handle %#x (%p) is in the free list, but has type %#x.\n", idx, entry->object, entry->type);
93 return DDRAW_INVALID_HANDLE;
95 t->free_entries = entry->object;
96 entry->object = object;
97 entry->type = type;
99 return idx;
102 if (!(t->entry_count < t->table_size))
104 /* Grow the table */
105 UINT new_size = t->table_size + (t->table_size >> 1);
106 struct ddraw_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
107 0, t->entries, new_size * sizeof(*t->entries));
108 if (!new_entries)
110 ERR("Failed to grow the handle table.\n");
111 return DDRAW_INVALID_HANDLE;
113 t->entries = new_entries;
114 t->table_size = new_size;
117 entry = &t->entries[t->entry_count];
118 entry->object = object;
119 entry->type = type;
121 return t->entry_count++;
124 void *ddraw_free_handle(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
126 struct ddraw_handle_entry *entry;
127 void *object;
129 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
131 WARN("Invalid handle %#x passed.\n", handle);
132 return NULL;
135 entry = &t->entries[handle];
136 if (entry->type != type)
138 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
139 return NULL;
142 object = entry->object;
143 entry->object = t->free_entries;
144 entry->type = DDRAW_HANDLE_FREE;
145 t->free_entries = entry;
147 return object;
150 void *ddraw_get_object(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
152 struct ddraw_handle_entry *entry;
154 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
156 WARN("Invalid handle %#x passed.\n", handle);
157 return NULL;
160 entry = &t->entries[handle];
161 if (entry->type != type)
163 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
164 return NULL;
167 return entry->object;
171 * Helper Function for DDRAW_Create and DirectDrawCreateClipper for
172 * lazy loading of the Wine D3D driver.
174 * Returns
175 * TRUE on success
176 * FALSE on failure.
179 BOOL LoadWineD3D(void)
181 static HMODULE hWineD3D = (HMODULE) -1;
182 if (hWineD3D == (HMODULE) -1)
184 hWineD3D = LoadLibraryA("wined3d");
185 if (hWineD3D)
187 pWineDirect3DCreate = (typeof(WineDirect3DCreate) *)GetProcAddress(hWineD3D, "WineDirect3DCreate");
188 pWineDirect3DCreateClipper = (typeof(WineDirect3DCreateClipper) *) GetProcAddress(hWineD3D, "WineDirect3DCreateClipper");
189 return TRUE;
192 return hWineD3D != NULL;
195 /***********************************************************************
197 * Helper function for DirectDrawCreate and friends
198 * Creates a new DDraw interface with the given REFIID
200 * Interfaces that can be created:
201 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
202 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
203 * IDirect3D interfaces?)
205 * Arguments:
206 * guid: ID of the requested driver, NULL for the default driver.
207 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
208 * DD: Used to return the pointer to the created object
209 * UnkOuter: For aggregation, which is unsupported. Must be NULL
210 * iid: requested version ID.
212 * Returns:
213 * DD_OK if the Interface was created successfully
214 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
215 * E_OUTOFMEMORY if some allocation failed
217 ***********************************************************************/
218 static HRESULT
219 DDRAW_Create(const GUID *guid,
220 void **DD,
221 IUnknown *UnkOuter,
222 REFIID iid)
224 WINED3DDEVTYPE devicetype;
225 IDirectDrawImpl *This;
226 HRESULT hr;
228 TRACE("driver_guid %s, ddraw %p, outer_unknown %p, interface_iid %s.\n",
229 debugstr_guid(guid), DD, UnkOuter, debugstr_guid(iid));
231 *DD = NULL;
233 /* We don't care about this guids. Well, there's no special guid anyway
234 * OK, we could
236 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
238 /* Use the reference device id. This doesn't actually change anything,
239 * WineD3D always uses OpenGL for D3D rendering. One could make it request
240 * indirect rendering
242 devicetype = WINED3DDEVTYPE_REF;
244 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
246 devicetype = WINED3DDEVTYPE_HAL;
248 else
250 devicetype = 0;
253 /* DDraw doesn't support aggregation, according to msdn */
254 if (UnkOuter != NULL)
255 return CLASS_E_NOAGGREGATION;
257 /* DirectDraw creation comes here */
258 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawImpl));
259 if(!This)
261 ERR("Out of memory when creating DirectDraw\n");
262 return E_OUTOFMEMORY;
265 hr = ddraw_init(This, devicetype);
266 if (FAILED(hr))
268 WARN("Failed to initialize ddraw object, hr %#x.\n", hr);
269 HeapFree(GetProcessHeap(), 0, This);
270 return hr;
273 hr = IDirectDraw7_QueryInterface((IDirectDraw7 *)This, iid, DD);
274 IDirectDraw7_Release((IDirectDraw7 *)This);
275 if (SUCCEEDED(hr)) list_add_head(&global_ddraw_list, &This->ddraw_list_entry);
276 else WARN("Failed to query interface %s from ddraw object %p.\n", debugstr_guid(iid), This);
278 return hr;
281 /***********************************************************************
282 * DirectDrawCreate (DDRAW.@)
284 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
285 * interfaces in theory
287 * Arguments, return values: See DDRAW_Create
289 ***********************************************************************/
290 HRESULT WINAPI DECLSPEC_HOTPATCH
291 DirectDrawCreate(GUID *GUID,
292 LPDIRECTDRAW *DD,
293 IUnknown *UnkOuter)
295 HRESULT hr;
297 TRACE("driver_guid %s, ddraw %p, outer_unknown %p.\n",
298 debugstr_guid(GUID), DD, UnkOuter);
300 EnterCriticalSection(&ddraw_cs);
301 hr = DDRAW_Create(GUID, (void **) DD, UnkOuter, &IID_IDirectDraw);
302 LeaveCriticalSection(&ddraw_cs);
303 return hr;
306 /***********************************************************************
307 * DirectDrawCreateEx (DDRAW.@)
309 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
310 * interfaces are requested.
312 * Arguments, return values: See DDRAW_Create
314 ***********************************************************************/
315 HRESULT WINAPI DECLSPEC_HOTPATCH
316 DirectDrawCreateEx(GUID *GUID,
317 LPVOID *DD,
318 REFIID iid,
319 IUnknown *UnkOuter)
321 HRESULT hr;
323 TRACE("driver_guid %s, ddraw %p, interface_iid %s, outer_unknown %p.\n",
324 debugstr_guid(GUID), DD, debugstr_guid(iid), UnkOuter);
326 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
327 return DDERR_INVALIDPARAMS;
329 EnterCriticalSection(&ddraw_cs);
330 hr = DDRAW_Create(GUID, DD, UnkOuter, iid);
331 LeaveCriticalSection(&ddraw_cs);
332 return hr;
335 /***********************************************************************
336 * DirectDrawEnumerateA (DDRAW.@)
338 * Enumerates legacy ddraw drivers, ascii version. We only have one
339 * driver, which relays to WineD3D. If we were sufficiently cool,
340 * we could offer various interfaces, which use a different default surface
341 * implementation, but I think it's better to offer this choice in
342 * winecfg, because some apps use the default driver, so we would need
343 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
345 * Arguments:
346 * Callback: Callback function from the app
347 * Context: Argument to the call back.
349 * Returns:
350 * DD_OK on success
351 * E_INVALIDARG if the Callback caused a page fault
354 ***********************************************************************/
355 HRESULT WINAPI DirectDrawEnumerateA(LPDDENUMCALLBACKA Callback, void *Context)
357 TRACE("callback %p, context %p.\n", Callback, Context);
359 TRACE(" Enumerating default DirectDraw HAL interface\n");
360 /* We only have one driver */
361 __TRY
363 static CHAR driver_desc[] = "DirectDraw HAL",
364 driver_name[] = "display";
366 Callback(NULL, driver_desc, driver_name, Context);
368 __EXCEPT_PAGE_FAULT
370 return DDERR_INVALIDPARAMS;
372 __ENDTRY
374 TRACE(" End of enumeration\n");
375 return DD_OK;
378 /***********************************************************************
379 * DirectDrawEnumerateExA (DDRAW.@)
381 * Enumerates DirectDraw7 drivers, ascii version. See
382 * the comments above DirectDrawEnumerateA for more details.
384 * The Flag member is not supported right now.
386 ***********************************************************************/
387 HRESULT WINAPI DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA Callback, void *Context, DWORD Flags)
389 TRACE("callback %p, context %p, flags %#x.\n", Callback, Context, Flags);
391 if (Flags & ~(DDENUM_ATTACHEDSECONDARYDEVICES |
392 DDENUM_DETACHEDSECONDARYDEVICES |
393 DDENUM_NONDISPLAYDEVICES))
394 return DDERR_INVALIDPARAMS;
396 if (Flags)
397 FIXME("flags 0x%08x not handled\n", Flags);
399 TRACE("Enumerating default DirectDraw HAL interface\n");
401 /* We only have one driver by now */
402 __TRY
404 static CHAR driver_desc[] = "DirectDraw HAL",
405 driver_name[] = "display";
407 /* QuickTime expects the description "DirectDraw HAL" */
408 Callback(NULL, driver_desc, driver_name, Context, 0);
410 __EXCEPT_PAGE_FAULT
412 return DDERR_INVALIDPARAMS;
414 __ENDTRY;
416 TRACE("End of enumeration\n");
417 return DD_OK;
420 /***********************************************************************
421 * DirectDrawEnumerateW (DDRAW.@)
423 * Enumerates legacy drivers, unicode version.
424 * This function is not implemented on Windows.
426 ***********************************************************************/
427 HRESULT WINAPI DirectDrawEnumerateW(LPDDENUMCALLBACKW callback, void *context)
429 TRACE("callback %p, context %p.\n", callback, context);
431 if (!callback)
432 return DDERR_INVALIDPARAMS;
433 else
434 return DDERR_UNSUPPORTED;
437 /***********************************************************************
438 * DirectDrawEnumerateExW (DDRAW.@)
440 * Enumerates DirectDraw7 drivers, unicode version.
441 * This function is not implemented on Windows.
443 ***********************************************************************/
444 HRESULT WINAPI DirectDrawEnumerateExW(LPDDENUMCALLBACKEXW callback, void *context, DWORD flags)
446 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
448 return DDERR_UNSUPPORTED;
451 /***********************************************************************
452 * Classfactory implementation.
453 ***********************************************************************/
455 /***********************************************************************
456 * CF_CreateDirectDraw
458 * DDraw creation function for the class factory
460 * Params:
461 * UnkOuter: Set to NULL
462 * iid: ID of the wanted interface
463 * obj: Address to pass the interface pointer back
465 * Returns
466 * DD_OK / DDERR*, see DDRAW_Create
468 ***********************************************************************/
469 static HRESULT
470 CF_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
471 void **obj)
473 HRESULT hr;
475 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(iid), obj);
477 EnterCriticalSection(&ddraw_cs);
478 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
479 LeaveCriticalSection(&ddraw_cs);
480 return hr;
483 /***********************************************************************
484 * CF_CreateDirectDraw
486 * Clipper creation function for the class factory
488 * Params:
489 * UnkOuter: Set to NULL
490 * iid: ID of the wanted interface
491 * obj: Address to pass the interface pointer back
493 * Returns
494 * DD_OK / DDERR*, see DDRAW_Create
496 ***********************************************************************/
497 static HRESULT
498 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
499 void **obj)
501 HRESULT hr;
502 IDirectDrawClipper *Clip;
504 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(riid), obj);
506 EnterCriticalSection(&ddraw_cs);
507 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
508 if (hr != DD_OK)
510 LeaveCriticalSection(&ddraw_cs);
511 return hr;
514 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
515 IDirectDrawClipper_Release(Clip);
517 LeaveCriticalSection(&ddraw_cs);
518 return hr;
521 static const struct object_creation_info object_creation[] =
523 { &CLSID_DirectDraw, CF_CreateDirectDraw },
524 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
525 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
528 /*******************************************************************************
529 * IDirectDrawClassFactory::QueryInterface
531 * QueryInterface for the class factory
533 * PARAMS
534 * riid Reference to identifier of queried interface
535 * ppv Address to return the interface pointer at
537 * RETURNS
538 * Success: S_OK
539 * Failure: E_NOINTERFACE
541 *******************************************************************************/
542 static HRESULT WINAPI
543 IDirectDrawClassFactoryImpl_QueryInterface(IClassFactory *iface,
544 REFIID riid,
545 void **obj)
547 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
549 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
551 if (IsEqualGUID(riid, &IID_IUnknown)
552 || IsEqualGUID(riid, &IID_IClassFactory))
554 IClassFactory_AddRef(iface);
555 *obj = This;
556 return S_OK;
559 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),obj);
560 return E_NOINTERFACE;
563 /*******************************************************************************
564 * IDirectDrawClassFactory::AddRef
566 * AddRef for the class factory
568 * RETURNS
569 * The new refcount
571 *******************************************************************************/
572 static ULONG WINAPI
573 IDirectDrawClassFactoryImpl_AddRef(IClassFactory *iface)
575 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
576 ULONG ref = InterlockedIncrement(&This->ref);
578 TRACE("%p increasing refcount to %u.\n", This, ref);
580 return ref;
583 /*******************************************************************************
584 * IDirectDrawClassFactory::Release
586 * Release for the class factory. If the refcount falls to 0, the object
587 * is destroyed
589 * RETURNS
590 * The new refcount
592 *******************************************************************************/
593 static ULONG WINAPI
594 IDirectDrawClassFactoryImpl_Release(IClassFactory *iface)
596 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
597 ULONG ref = InterlockedDecrement(&This->ref);
599 TRACE("%p decreasing refcount to %u.\n", This, ref);
601 if (ref == 0)
602 HeapFree(GetProcessHeap(), 0, This);
604 return ref;
608 /*******************************************************************************
609 * IDirectDrawClassFactory::CreateInstance
611 * What is this? Seems to create DirectDraw objects...
613 * Params
614 * The usual things???
616 * RETURNS
617 * ???
619 *******************************************************************************/
620 static HRESULT WINAPI
621 IDirectDrawClassFactoryImpl_CreateInstance(IClassFactory *iface,
622 IUnknown *UnkOuter,
623 REFIID riid,
624 void **obj)
626 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
628 TRACE("iface %p, outer_unknown %p, riid %s, object %p.\n",
629 iface, UnkOuter, debugstr_guid(riid), obj);
631 return This->pfnCreateInstance(UnkOuter, riid, obj);
634 /*******************************************************************************
635 * IDirectDrawClassFactory::LockServer
637 * What is this?
639 * Params
640 * ???
642 * RETURNS
643 * S_OK, because it's a stub
645 *******************************************************************************/
646 static HRESULT WINAPI IDirectDrawClassFactoryImpl_LockServer(IClassFactory *iface, BOOL dolock)
648 FIXME("iface %p, dolock %#x stub!\n", iface, dolock);
650 return S_OK;
653 /*******************************************************************************
654 * The class factory VTable
655 *******************************************************************************/
656 static const IClassFactoryVtbl IClassFactory_Vtbl =
658 IDirectDrawClassFactoryImpl_QueryInterface,
659 IDirectDrawClassFactoryImpl_AddRef,
660 IDirectDrawClassFactoryImpl_Release,
661 IDirectDrawClassFactoryImpl_CreateInstance,
662 IDirectDrawClassFactoryImpl_LockServer
665 /*******************************************************************************
666 * DllGetClassObject [DDRAW.@]
667 * Retrieves class object from a DLL object
669 * NOTES
670 * Docs say returns STDAPI
672 * PARAMS
673 * rclsid [I] CLSID for the class object
674 * riid [I] Reference to identifier of interface for class object
675 * ppv [O] Address of variable to receive interface pointer for riid
677 * RETURNS
678 * Success: S_OK
679 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
680 * E_UNEXPECTED
682 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
684 unsigned int i;
685 IClassFactoryImpl *factory;
687 TRACE("rclsid %s, riid %s, object %p.\n",
688 debugstr_guid(rclsid), debugstr_guid(riid), ppv);
690 if ( !IsEqualGUID( &IID_IClassFactory, riid )
691 && ! IsEqualGUID( &IID_IUnknown, riid) )
692 return E_NOINTERFACE;
694 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
696 if (IsEqualGUID(object_creation[i].clsid, rclsid))
697 break;
700 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
702 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
703 return CLASS_E_CLASSNOTAVAILABLE;
706 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
707 if (factory == NULL) return E_OUTOFMEMORY;
709 factory->lpVtbl = &IClassFactory_Vtbl;
710 factory->ref = 1;
712 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
714 *ppv = factory;
715 return S_OK;
719 /*******************************************************************************
720 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
722 * RETURNS
723 * Success: S_OK
724 * Failure: S_FALSE
726 HRESULT WINAPI DllCanUnloadNow(void)
728 TRACE("\n");
730 return S_FALSE;
733 /*******************************************************************************
734 * DestroyCallback
736 * Callback function for the EnumSurfaces call in DllMain.
737 * Dumps some surface info and releases the surface
739 * Params:
740 * surf: The enumerated surface
741 * desc: it's description
742 * context: Pointer to the ddraw impl
744 * Returns:
745 * DDENUMRET_OK;
746 *******************************************************************************/
747 static HRESULT WINAPI
748 DestroyCallback(IDirectDrawSurface7 *surf,
749 DDSURFACEDESC2 *desc,
750 void *context)
752 IDirectDrawSurfaceImpl *Impl = (IDirectDrawSurfaceImpl *)surf;
753 ULONG ref;
755 ref = IDirectDrawSurface7_Release(surf); /* For the EnumSurfaces */
756 WARN("Surface %p has an reference count of %d\n", Impl, ref);
758 /* Skip surfaces which are attached somewhere or which are
759 * part of a complex compound. They will get released when destroying
760 * the root
762 if( (!Impl->is_complex_root) || (Impl->first_attached != Impl) )
763 return DDENUMRET_OK;
765 /* Destroy the surface */
766 while(ref) ref = IDirectDrawSurface7_Release(surf);
768 return DDENUMRET_OK;
771 /***********************************************************************
772 * get_config_key
774 * Reads a config key from the registry. Taken from WineD3D
776 ***********************************************************************/
777 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
779 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
780 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
781 return ERROR_FILE_NOT_FOUND;
784 /***********************************************************************
785 * DllMain (DDRAW.0)
787 * Could be used to register DirectDraw drivers, if we have more than
788 * one. Also used to destroy any objects left at unload if the
789 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
791 ***********************************************************************/
792 BOOL WINAPI
793 DllMain(HINSTANCE hInstDLL,
794 DWORD Reason,
795 LPVOID lpv)
797 TRACE("(%p,%x,%p)\n", hInstDLL, Reason, lpv);
798 if (Reason == DLL_PROCESS_ATTACH)
800 char buffer[MAX_PATH+10];
801 DWORD size = sizeof(buffer);
802 HKEY hkey = 0;
803 HKEY appkey = 0;
804 WNDCLASSA wc;
805 DWORD len;
807 /* Register the window class. This is used to create a hidden window
808 * for D3D rendering, if the application didn't pass one. It can also
809 * be used for creating a device window from SetCooperativeLevel(). */
810 wc.style = CS_HREDRAW | CS_VREDRAW;
811 wc.lpfnWndProc = DefWindowProcA;
812 wc.cbClsExtra = 0;
813 wc.cbWndExtra = 0;
814 wc.hInstance = hInstDLL;
815 wc.hIcon = 0;
816 wc.hCursor = 0;
817 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
818 wc.lpszMenuName = NULL;
819 wc.lpszClassName = DDRAW_WINDOW_CLASS_NAME;
820 if (!RegisterClassA(&wc))
822 ERR("Failed to register ddraw window class, last error %#x.\n", GetLastError());
823 return FALSE;
826 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
827 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
829 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
830 if (len && len < MAX_PATH)
832 HKEY tmpkey;
833 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
834 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
836 char *p, *appname = buffer;
837 if ((p = strrchr( appname, '/' ))) appname = p + 1;
838 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
839 strcat( appname, "\\Direct3D" );
840 TRACE("appname = [%s]\n", appname);
841 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
842 RegCloseKey( tmpkey );
846 if ( 0 != hkey || 0 != appkey )
848 if ( !get_config_key( hkey, appkey, "DirectDrawRenderer", buffer, size) )
850 if (!strcmp(buffer,"gdi"))
852 TRACE("Defaulting to GDI surfaces\n");
853 DefaultSurfaceType = SURFACE_GDI;
855 else if (!strcmp(buffer,"opengl"))
857 TRACE("Defaulting to opengl surfaces\n");
858 DefaultSurfaceType = SURFACE_OPENGL;
860 else
862 ERR("Unknown default surface type. Supported are:\n gdi, opengl\n");
867 /* On Windows one can force the refresh rate that DirectDraw uses by
868 * setting an override value in dxdiag. This is documented in KB315614
869 * (main article), KB230002, and KB217348. By comparing registry dumps
870 * before and after setting the override, we see that the override value
871 * is stored in HKLM\Software\Microsoft\DirectDraw\ForceRefreshRate as a
872 * DWORD that represents the refresh rate to force. We use this
873 * registry entry to modify the behavior of SetDisplayMode so that Wine
874 * users can override the refresh rate in a Windows-compatible way.
876 * dxdiag will not accept a refresh rate lower than 40 or higher than
877 * 120 so this value should be within that range. It is, of course,
878 * possible for a user to set the registry entry value directly so that
879 * assumption might not hold.
881 * There is no current mechanism for setting this value through the Wine
882 * GUI. It would be most appropriate to set this value through a dxdiag
883 * clone, but it may be sufficient to use winecfg.
885 * TODO: Create a mechanism for setting this value through the Wine GUI.
887 if ( !RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectDraw", &hkey ) )
889 DWORD type, data;
890 size = sizeof(data);
891 if (!RegQueryValueExA( hkey, "ForceRefreshRate", NULL, &type, (LPBYTE)&data, &size ) && type == REG_DWORD)
893 TRACE("ForceRefreshRate set; overriding refresh rate to %d Hz\n", data);
894 force_refresh_rate = data;
896 RegCloseKey( hkey );
899 DisableThreadLibraryCalls(hInstDLL);
901 else if (Reason == DLL_PROCESS_DETACH)
903 if(!list_empty(&global_ddraw_list))
905 struct list *entry, *entry2;
906 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
908 /* We remove elements from this loop */
909 LIST_FOR_EACH_SAFE(entry, entry2, &global_ddraw_list)
911 HRESULT hr;
912 DDSURFACEDESC2 desc;
913 int i;
914 IDirectDrawImpl *ddraw = LIST_ENTRY(entry, IDirectDrawImpl, ddraw_list_entry);
916 WARN("DDraw %p has a refcount of %d\n", ddraw, ddraw->ref7 + ddraw->ref4 + ddraw->ref3 + ddraw->ref2 + ddraw->ref1);
918 /* Add references to each interface to avoid freeing them unexpectedly */
919 IDirectDraw_AddRef((IDirectDraw *)&ddraw->IDirectDraw_vtbl);
920 IDirectDraw2_AddRef((IDirectDraw2 *)&ddraw->IDirectDraw2_vtbl);
921 IDirectDraw3_AddRef((IDirectDraw3 *)&ddraw->IDirectDraw3_vtbl);
922 IDirectDraw4_AddRef((IDirectDraw4 *)&ddraw->IDirectDraw4_vtbl);
923 IDirectDraw7_AddRef((IDirectDraw7 *)ddraw);
925 /* Does a D3D device exist? Destroy it
926 * TODO: Destroy all Vertex buffers, Lights, Materials
927 * and execute buffers too
929 if(ddraw->d3ddevice)
931 WARN("DDraw %p has d3ddevice %p attached\n", ddraw, ddraw->d3ddevice);
932 while(IDirect3DDevice7_Release((IDirect3DDevice7 *)ddraw->d3ddevice));
935 /* Try to release the objects
936 * Do an EnumSurfaces to find any hanging surfaces
938 memset(&desc, 0, sizeof(desc));
939 desc.dwSize = sizeof(desc);
940 for(i = 0; i <= 1; i++)
942 hr = IDirectDraw7_EnumSurfaces((IDirectDraw7 *)ddraw,
943 DDENUMSURFACES_ALL, &desc, ddraw, DestroyCallback);
944 if(hr != D3D_OK)
945 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw);
948 /* Check the surface count */
949 if(ddraw->surfaces > 0)
950 ERR("DDraw %p still has %d surfaces attached\n", ddraw, ddraw->surfaces);
952 /* Release all hanging references to destroy the objects. This
953 * restores the screen mode too
955 while(IDirectDraw_Release((IDirectDraw *)&ddraw->IDirectDraw_vtbl));
956 while(IDirectDraw2_Release((IDirectDraw2 *)&ddraw->IDirectDraw2_vtbl));
957 while(IDirectDraw3_Release((IDirectDraw3 *)&ddraw->IDirectDraw3_vtbl));
958 while(IDirectDraw4_Release((IDirectDraw4 *)&ddraw->IDirectDraw4_vtbl));
959 while(IDirectDraw7_Release((IDirectDraw7 *)ddraw));
963 /* Unregister the window class. */
964 UnregisterClassA(DDRAW_WINDOW_CLASS_NAME, hInstDLL);
967 return TRUE;