ddraw: Make the ddraw list a wine list.
[wine.git] / dlls / ddraw / main.c
blob9fbcf3551ddd56887f108bbcbce746295990129e
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
8 * This file contains the (internal) driver registration functions,
9 * driver enumeration APIs and DirectDraw creation functions.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "config.h"
27 #include "wine/port.h"
28 #include "wine/debug.h"
30 #include <assert.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include <stdlib.h>
35 #define COBJMACROS
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winnls.h"
40 #include "winerror.h"
41 #include "wingdi.h"
42 #include "wine/exception.h"
43 #include "excpt.h"
44 #include "winreg.h"
46 #include "ddraw.h"
47 #include "d3d.h"
49 #include "ddraw_private.h"
51 typedef IWineD3D* (WINAPI *fnWineDirect3DCreate)(UINT, UINT, IUnknown *);
53 static HMODULE hWineD3D = (HMODULE) -1;
54 static fnWineDirect3DCreate pWineDirect3DCreate;
56 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
58 /* The configured default surface */
59 WINED3DSURFTYPE DefaultSurfaceType = SURFACE_UNKNOWN;
61 static struct list global_ddraw_list = LIST_INIT(global_ddraw_list);
63 /***********************************************************************
65 * Helper function for DirectDrawCreate and friends
66 * Creates a new DDraw interface with the given REFIID
68 * Interfaces that can be created:
69 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
70 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
71 * IDirect3D interfaces?)
73 * Arguments:
74 * guid: ID of the requested driver, NULL for the default driver.
75 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
76 * DD: Used to return the pointer to the created object
77 * UnkOuter: For aggregation, which is unsupported. Must be NULL
78 * iid: requested version ID.
80 * Returns:
81 * DD_OK if the Interface was created successfully
82 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
83 * E_OUTOFMEMORY if some allocation failed
85 ***********************************************************************/
86 static HRESULT
87 DDRAW_Create(GUID *guid,
88 void **DD,
89 IUnknown *UnkOuter,
90 REFIID iid)
92 IDirectDrawImpl *This = NULL;
93 HRESULT hr;
94 IWineD3D *wineD3D = NULL;
95 IWineD3DDevice *wineD3DDevice = NULL;
96 HDC hDC;
97 WINED3DDEVTYPE devicetype;
99 TRACE("(%s,%p,%p)\n", debugstr_guid(guid), DD, UnkOuter);
101 *DD = NULL;
103 /* We don't care about this guids. Well, there's no special guid anyway
104 * OK, we could
106 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
108 /* Use the reference device id. This doesn't actually change anything,
109 * WineD3D always uses OpenGL for D3D rendering. One could make it request
110 * indirect rendering
112 devicetype = WINED3DDEVTYPE_REF;
114 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
116 devicetype = WINED3DDEVTYPE_HAL;
118 else
120 devicetype = 0;
123 /* DDraw doesn't support aggregation, according to msdn */
124 if (UnkOuter != NULL)
125 return CLASS_E_NOAGGREGATION;
127 /* DirectDraw creation comes here */
128 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawImpl));
129 if(!This)
131 ERR("Out of memory when creating DirectDraw\n");
132 return E_OUTOFMEMORY;
135 /* The interfaces:
136 * IDirectDraw and IDirect3D are the same object,
137 * QueryInterface is used to get other interfaces.
139 ICOM_INIT_INTERFACE(This, IDirectDraw, IDirectDraw1_Vtbl);
140 ICOM_INIT_INTERFACE(This, IDirectDraw2, IDirectDraw2_Vtbl);
141 ICOM_INIT_INTERFACE(This, IDirectDraw4, IDirectDraw4_Vtbl);
142 ICOM_INIT_INTERFACE(This, IDirectDraw7, IDirectDraw7_Vtbl);
143 ICOM_INIT_INTERFACE(This, IDirect3D, IDirect3D1_Vtbl);
144 ICOM_INIT_INTERFACE(This, IDirect3D2, IDirect3D2_Vtbl);
145 ICOM_INIT_INTERFACE(This, IDirect3D3, IDirect3D3_Vtbl);
146 ICOM_INIT_INTERFACE(This, IDirect3D7, IDirect3D7_Vtbl);
148 /* See comments in IDirectDrawImpl_CreateNewSurface for a description
149 * of this member.
150 * Read from a registry key, should add a winecfg option later
152 This->ImplType = DefaultSurfaceType;
154 /* Get the current screen settings */
155 hDC = CreateDCA("DISPLAY", NULL, NULL, NULL);
156 This->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
157 DeleteDC(hDC);
158 This->orig_width = GetSystemMetrics(SM_CXSCREEN);
159 This->orig_height = GetSystemMetrics(SM_CYSCREEN);
161 if (hWineD3D == (HMODULE) -1)
163 hWineD3D = LoadLibraryA("wined3d");
164 if (hWineD3D)
165 pWineDirect3DCreate = (fnWineDirect3DCreate) GetProcAddress(hWineD3D, "WineDirect3DCreate");
168 if (!hWineD3D)
170 ERR("Couldn't load WineD3D - OpenGL libs not present?\n");
171 hr = DDERR_NODIRECTDRAWSUPPORT;
172 goto err_out;
175 /* Initialize WineD3D
177 * All Rendering (2D and 3D) is relayed to WineD3D,
178 * but DirectDraw specific management, like DDSURFACEDESC and DDPIXELFORMAT
179 * structure handling is handled in this lib.
181 wineD3D = pWineDirect3DCreate(0 /* SDKVersion */, 7 /* DXVersion */, (IUnknown *) This /* Parent */);
182 if(!wineD3D)
184 ERR("Failed to initialise WineD3D\n");
185 hr = E_OUTOFMEMORY;
186 goto err_out;
188 This->wineD3D = wineD3D;
189 TRACE("WineD3D created at %p\n", wineD3D);
191 /* Initialized member...
193 * It is set to false at creation time, and set to true in
194 * IDirectDraw7::Initialize. Its sole purpose is to return DD_OK on
195 * initialize only once
197 This->initialized = FALSE;
199 /* Initialize WineD3DDevice
201 * It is used for screen setup, surface and palette creation
202 * When a Direct3DDevice7 is created, the D3D capabilities of WineD3D are
203 * initialized
205 hr = IWineD3D_CreateDevice(wineD3D,
206 0 /*D3D_ADAPTER_DEFAULT*/,
207 devicetype,
208 NULL, /* FocusWindow, don't know yet */
209 0, /* BehaviorFlags */
210 &wineD3DDevice,
211 (IUnknown *) ICOM_INTERFACE(This, IDirectDraw7));
212 if(FAILED(hr))
214 ERR("Failed to create a wineD3DDevice, result = %lx\n", hr);
215 goto err_out;
217 This->wineD3DDevice = wineD3DDevice;
218 TRACE("wineD3DDevice created at %p\n", This->wineD3DDevice);
220 /* Register the window class
222 * It is used to create a hidden window for D3D
223 * rendering, if the application didn't pass one.
224 * It can also be used for Creating a device window
225 * from SetCooperativeLevel
227 * The name: DDRAW_<address>. The classname is
228 * 32 bit long, so a 64 bit address will fit nicely
229 * (Will this be compiled for 64 bit anyway?)
232 sprintf(This->classname, "DDRAW_%p", This);
234 memset(&This->wnd_class, 0, sizeof(This->wnd_class));
235 This->wnd_class.style = CS_HREDRAW | CS_VREDRAW;
236 This->wnd_class.lpfnWndProc = DefWindowProcA;
237 This->wnd_class.cbClsExtra = 0;
238 This->wnd_class.cbWndExtra = 0;
239 This->wnd_class.hInstance = GetModuleHandleA(0);
240 This->wnd_class.hIcon = 0;
241 This->wnd_class.hCursor = 0;
242 This->wnd_class.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
243 This->wnd_class.lpszMenuName = NULL;
244 This->wnd_class.lpszClassName = This->classname;
245 if(!RegisterClassA(&This->wnd_class))
247 ERR("RegisterClassA failed!\n");
248 goto err_out;
251 /* Get the amount of video memory */
252 This->total_vidmem = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
254 /* Initialize the caps */
255 This->caps.dwSize = sizeof(This->caps);
256 /* do not report DDCAPS_OVERLAY and friends since we don't support overlays */
257 #define BLIT_CAPS (DDCAPS_BLT | DDCAPS_BLTCOLORFILL | DDCAPS_BLTDEPTHFILL \
258 | DDCAPS_BLTSTRETCH | DDCAPS_CANBLTSYSMEM | DDCAPS_CANCLIP \
259 | DDCAPS_CANCLIPSTRETCHED | DDCAPS_COLORKEY \
260 | DDCAPS_COLORKEYHWASSIST | DDCAPS_ALIGNBOUNDARYSRC )
261 #define CKEY_CAPS (DDCKEYCAPS_DESTBLT | DDCKEYCAPS_SRCBLT)
262 #define FX_CAPS (DDFXCAPS_BLTALPHA | DDFXCAPS_BLTMIRRORLEFTRIGHT \
263 | DDFXCAPS_BLTMIRRORUPDOWN | DDFXCAPS_BLTROTATION90 \
264 | DDFXCAPS_BLTSHRINKX | DDFXCAPS_BLTSHRINKXN \
265 | DDFXCAPS_BLTSHRINKY | DDFXCAPS_BLTSHRINKXN \
266 | DDFXCAPS_BLTSTRETCHX | DDFXCAPS_BLTSTRETCHXN \
267 | DDFXCAPS_BLTSTRETCHY | DDFXCAPS_BLTSTRETCHYN)
268 This->caps.dwCaps |= DDCAPS_GDI | DDCAPS_PALETTE | BLIT_CAPS;
270 This->caps.dwCaps2 |= DDCAPS2_CERTIFIED | DDCAPS2_NOPAGELOCKREQUIRED |
271 DDCAPS2_PRIMARYGAMMA | DDCAPS2_WIDESURFACES |
272 DDCAPS2_CANRENDERWINDOWED;
273 This->caps.dwCKeyCaps |= CKEY_CAPS;
274 This->caps.dwFXCaps |= FX_CAPS;
275 This->caps.dwPalCaps |= DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE;
276 This->caps.dwVidMemTotal = This->total_vidmem;
277 This->caps.dwVidMemFree = This->total_vidmem;
278 This->caps.dwSVBCaps |= BLIT_CAPS;
279 This->caps.dwSVBCKeyCaps |= CKEY_CAPS;
280 This->caps.dwSVBFXCaps |= FX_CAPS;
281 This->caps.dwVSBCaps |= BLIT_CAPS;
282 This->caps.dwVSBCKeyCaps |= CKEY_CAPS;
283 This->caps.dwVSBFXCaps |= FX_CAPS;
284 This->caps.dwSSBCaps |= BLIT_CAPS;
285 This->caps.dwSSBCKeyCaps |= CKEY_CAPS;
286 This->caps.dwSSBFXCaps |= FX_CAPS;
287 This->caps.ddsCaps.dwCaps |= DDSCAPS_ALPHA | DDSCAPS_BACKBUFFER |
288 DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER |
289 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_PALETTE |
290 DDSCAPS_PRIMARYSURFACE | DDSCAPS_SYSTEMMEMORY |
291 DDSCAPS_VIDEOMEMORY | DDSCAPS_VISIBLE;
292 /* Hacks for D3D code */
293 /* TODO: Check if WineD3D has 3D enabled
294 Need opengl surfaces or auto for 3D
296 if(This->ImplType == 0 || This->ImplType == SURFACE_OPENGL)
298 This->caps.dwCaps |= DDCAPS_3D;
299 This->caps.ddsCaps.dwCaps |= DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER;
301 This->caps.ddsOldCaps.dwCaps = This->caps.ddsCaps.dwCaps;
303 #undef BLIT_CAPS
304 #undef CKEY_CAPS
305 #undef FX_CAPS
307 list_add_head(&global_ddraw_list, &This->ddraw_list_entry);
309 /* Call QueryInterface to get the pointer to the requested interface. This also initializes
310 * The required refcount
312 hr = IDirectDraw7_QueryInterface( ICOM_INTERFACE(This, IDirectDraw7), iid, DD);
313 if(SUCCEEDED(hr)) return DD_OK;
315 err_out:
316 /* Let's hope we never need this ;) */
317 if(wineD3DDevice) IWineD3DDevice_Release(wineD3DDevice);
318 if(wineD3D) IWineD3D_Release(wineD3D);
319 HeapFree(GetProcessHeap(), 0, This);
320 return hr;
323 /***********************************************************************
324 * DirectDrawCreate (DDRAW.@)
326 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
327 * interfaces in theory
329 * Arguments, return values: See DDRAW_Create
331 ***********************************************************************/
332 HRESULT WINAPI
333 DirectDrawCreate(GUID *GUID,
334 IDirectDraw **DD,
335 IUnknown *UnkOuter)
337 TRACE("(%s,%p,%p)\n", debugstr_guid(GUID), DD, UnkOuter);
339 return DDRAW_Create(GUID, (void **) DD, UnkOuter, &IID_IDirectDraw);
342 /***********************************************************************
343 * DirectDrawCreateEx (DDRAW.@)
345 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
346 * interfaces are requested.
348 * Arguments, return values: See DDRAW_Create
350 ***********************************************************************/
351 HRESULT WINAPI
352 DirectDrawCreateEx(GUID *GUID,
353 void **DD,
354 REFIID iid,
355 IUnknown *UnkOuter)
357 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(GUID), DD, debugstr_guid(iid), UnkOuter);
359 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
360 return DDERR_INVALIDPARAMS;
362 return DDRAW_Create(GUID, DD, UnkOuter, iid);
365 /***********************************************************************
366 * DirectDrawEnumerateA (DDRAW.@)
368 * Enumerates legacy ddraw drivers, ascii version. We only have one
369 * driver, which relays to WineD3D. If we were sufficiently cool,
370 * we could offer various interfaces, which use a different default surface
371 * implementation, but I think it's better to offer this choice in
372 * winecfg, because some apps use the default driver, so we would need
373 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
375 * Arguments:
376 * Callback: Callback function from the app
377 * Context: Argument to the call back.
379 * Returns:
380 * DD_OK on success
381 * E_INVALIDARG if the Callback caused a page fault
384 ***********************************************************************/
385 HRESULT WINAPI
386 DirectDrawEnumerateA(LPDDENUMCALLBACKA Callback,
387 void *Context)
389 BOOL stop = FALSE;
391 TRACE(" Enumerating default DirectDraw HAL interface\n");
392 /* We only have one driver */
393 __TRY
395 static CHAR driver_desc[] = "DirectDraw HAL",
396 driver_name[] = "display";
398 stop = !Callback(NULL, driver_desc, driver_name, Context);
400 __EXCEPT_PAGE_FAULT
402 return E_INVALIDARG;
404 __ENDTRY
406 TRACE(" End of enumeration\n");
407 return DD_OK;
410 /***********************************************************************
411 * DirectDrawEnumerateExA (DDRAW.@)
413 * Enumerates DirectDraw7 drivers, ascii version. See
414 * the comments above DirectDrawEnumerateA for more details.
416 * The Flag member is not supported right now.
418 ***********************************************************************/
419 HRESULT WINAPI
420 DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA Callback,
421 void *Context,
422 DWORD Flags)
424 BOOL stop = FALSE;
425 TRACE("Enumerating default DirectDraw HAL interface\n");
427 /* We only have one driver by now */
428 __TRY
430 static CHAR driver_desc[] = "DirectDraw HAL",
431 driver_name[] = "display";
433 /* QuickTime expects the description "DirectDraw HAL" */
434 stop = !Callback(NULL, driver_desc, driver_name, Context, 0);
436 __EXCEPT_PAGE_FAULT
438 return E_INVALIDARG;
440 __ENDTRY;
442 TRACE("End of enumeration\n");
443 return DD_OK;
446 /***********************************************************************
447 * DirectDrawEnumerateW (DDRAW.@)
449 * Enumerates legacy drivers, unicode version. See
450 * the comments above DirectDrawEnumerateA for more details.
452 * The Flag member is not supported right now.
454 ***********************************************************************/
456 /***********************************************************************
457 * DirectDrawEnumerateExW (DDRAW.@)
459 * Enumerates DirectDraw7 drivers, unicode version. See
460 * the comments above DirectDrawEnumerateA for more details.
462 * The Flag member is not supported right now.
464 ***********************************************************************/
466 /***********************************************************************
467 * Classfactory implementation.
468 ***********************************************************************/
470 /***********************************************************************
471 * CF_CreateDirectDraw
473 * DDraw creation function for the class factory
475 * Params:
476 * UnkOuter: Set to NULL
477 * iid: ID of the wanted interface
478 * obj: Address to pass the interface pointer back
480 * Returns
481 * DD_OK / DDERR*, see DDRAW_Create
483 ***********************************************************************/
484 static HRESULT
485 CF_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
486 void **obj)
488 HRESULT hr;
490 TRACE("(%p,%s,%p)\n", UnkOuter, debugstr_guid(iid), obj);
492 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
493 return hr;
496 /***********************************************************************
497 * CF_CreateDirectDraw
499 * Clipper creation function for the class factory
501 * Params:
502 * UnkOuter: Set to NULL
503 * iid: ID of the wanted interface
504 * obj: Address to pass the interface pointer back
506 * Returns
507 * DD_OK / DDERR*, see DDRAW_Create
509 ***********************************************************************/
510 static HRESULT
511 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
512 void **obj)
514 HRESULT hr;
515 IDirectDrawClipper *Clip;
517 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
518 if (hr != DD_OK) return hr;
520 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
521 IDirectDrawClipper_Release(Clip);
522 return hr;
525 static const struct object_creation_info object_creation[] =
527 { &CLSID_DirectDraw, CF_CreateDirectDraw },
528 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
529 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
532 /*******************************************************************************
533 * IDirectDrawClassFactory::QueryInterface
535 * QueryInterface for the class factory
537 * PARAMS
538 * riid Reference to identifier of queried interface
539 * ppv Address to return the interface pointer at
541 * RETURNS
542 * Success: S_OK
543 * Failure: E_NOINTERFACE
545 *******************************************************************************/
546 static HRESULT WINAPI
547 IDirectDrawClassFactoryImpl_QueryInterface(IClassFactory *iface,
548 REFIID riid,
549 void **obj)
551 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
553 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obj);
555 if (IsEqualGUID(riid, &IID_IUnknown)
556 || IsEqualGUID(riid, &IID_IClassFactory))
558 IClassFactory_AddRef(iface);
559 *obj = This;
560 return S_OK;
563 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),obj);
564 return E_NOINTERFACE;
567 /*******************************************************************************
568 * IDirectDrawClassFactory::AddRef
570 * AddRef for the class factory
572 * RETURNS
573 * The new refcount
575 *******************************************************************************/
576 static ULONG WINAPI
577 IDirectDrawClassFactoryImpl_AddRef(IClassFactory *iface)
579 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
580 ULONG ref = InterlockedIncrement(&This->ref);
582 TRACE("(%p)->() incrementing from %ld.\n", This, ref - 1);
584 return ref;
587 /*******************************************************************************
588 * IDirectDrawClassFactory::Release
590 * Release for the class factory. If the refcount falls to 0, the object
591 * is destroyed
593 * RETURNS
594 * The new refcount
596 *******************************************************************************/
597 static ULONG WINAPI
598 IDirectDrawClassFactoryImpl_Release(IClassFactory *iface)
600 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
601 ULONG ref = InterlockedDecrement(&This->ref);
602 TRACE("(%p)->() decrementing from %ld.\n", This, ref+1);
604 if (ref == 0)
605 HeapFree(GetProcessHeap(), 0, This);
607 return ref;
611 /*******************************************************************************
612 * IDirectDrawClassFactory::CreateInstance
614 * What is this? Seems to create DirectDraw objects...
616 * Params
617 * The ususal things???
619 * RETURNS
620 * ???
622 *******************************************************************************/
623 static HRESULT WINAPI
624 IDirectDrawClassFactoryImpl_CreateInstance(IClassFactory *iface,
625 IUnknown *UnkOuter,
626 REFIID riid,
627 void **obj)
629 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
631 TRACE("(%p)->(%p,%s,%p)\n",This,UnkOuter,debugstr_guid(riid),obj);
633 return This->pfnCreateInstance(UnkOuter, riid, obj);
636 /*******************************************************************************
637 * IDirectDrawClassFactory::LockServer
639 * What is this?
641 * Params
642 * ???
644 * RETURNS
645 * S_OK, because it's a stub
647 *******************************************************************************/
648 static HRESULT WINAPI
649 IDirectDrawClassFactoryImpl_LockServer(IClassFactory *iface,BOOL dolock)
651 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
652 FIXME("(%p)->(%d),stub!\n",This,dolock);
653 return S_OK;
656 /*******************************************************************************
657 * The class factory VTable
658 *******************************************************************************/
659 static const IClassFactoryVtbl IClassFactory_Vtbl =
661 IDirectDrawClassFactoryImpl_QueryInterface,
662 IDirectDrawClassFactoryImpl_AddRef,
663 IDirectDrawClassFactoryImpl_Release,
664 IDirectDrawClassFactoryImpl_CreateInstance,
665 IDirectDrawClassFactoryImpl_LockServer
668 /*******************************************************************************
669 * DllGetClassObject [DDRAW.@]
670 * Retrieves class object from a DLL object
672 * NOTES
673 * Docs say returns STDAPI
675 * PARAMS
676 * rclsid [I] CLSID for the class object
677 * riid [I] Reference to identifier of interface for class object
678 * ppv [O] Address of variable to receive interface pointer for riid
680 * RETURNS
681 * Success: S_OK
682 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
683 * E_UNEXPECTED
685 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
687 unsigned int i;
688 IClassFactoryImpl *factory;
690 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
692 if ( !IsEqualGUID( &IID_IClassFactory, riid )
693 && ! IsEqualGUID( &IID_IUnknown, riid) )
694 return E_NOINTERFACE;
696 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
698 if (IsEqualGUID(object_creation[i].clsid, rclsid))
699 break;
702 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
704 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
705 return CLASS_E_CLASSNOTAVAILABLE;
708 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
709 if (factory == NULL) return E_OUTOFMEMORY;
711 ICOM_INIT_INTERFACE(factory, IClassFactory, IClassFactory_Vtbl);
712 factory->ref = 1;
714 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
716 *ppv = ICOM_INTERFACE(factory, IClassFactory);
717 return S_OK;
721 /*******************************************************************************
722 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
724 * RETURNS
725 * Success: S_OK
726 * Failure: S_FALSE
728 HRESULT WINAPI DllCanUnloadNow(void)
730 FIXME("(void): stub\n");
731 return S_FALSE;
734 /*******************************************************************************
735 * DestroyCallback
737 * Callback function for the EnumSurfaces call in DllMain.
738 * Dumps some surface info and releases the surface
740 * Params:
741 * surf: The enumerated surface
742 * desc: it's description
743 * context: Pointer to the ddraw impl
745 * Returns:
746 * DDENUMRET_OK;
747 *******************************************************************************/
748 static HRESULT WINAPI
749 DestroyCallback(IDirectDrawSurface7 *surf,
750 DDSURFACEDESC2 *desc,
751 void *context)
753 IDirectDrawSurfaceImpl *Impl = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surf);
754 IDirectDrawImpl *ddraw = (IDirectDrawImpl *) context;
755 ULONG ref;
757 ref = IDirectDrawSurface7_Release(surf); /* For the EnumSurfaces */
758 WARN("Surface %p has an reference count of %ld\n", Impl, ref);
760 /* Skip surfaces which are attached somewhere or which are
761 * part of a complex compound. They will get released when destroying
762 * the root
764 if( (Impl->first_complex != Impl) || (Impl->first_attached != Impl) )
765 return DDENUMRET_OK;
766 /* Skip our depth stencil surface, it will be released with the render target */
767 if( Impl == ddraw->DepthStencilBuffer)
768 return DDENUMRET_OK;
770 /* Destroy the surface */
771 while(ref) ref = IDirectDrawSurface7_Release(surf);
773 return DDENUMRET_OK;
776 /***********************************************************************
777 * get_config_key
779 * Reads a config key from the registry. Taken from WineD3D
781 ***********************************************************************/
782 inline static DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
784 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
785 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
786 return ERROR_FILE_NOT_FOUND;
789 /***********************************************************************
790 * DllMain (DDRAW.0)
792 * Could be used to register DirectDraw drivers, if we have more than
793 * one. Also used to destroy any objects left at unload if the
794 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
796 ***********************************************************************/
797 BOOL WINAPI
798 DllMain(HINSTANCE hInstDLL,
799 DWORD Reason,
800 void *lpv)
802 static LONG counter = 0;
804 TRACE("(%p,%lx,%p)\n", hInstDLL, Reason, lpv);
805 if (Reason == DLL_PROCESS_ATTACH)
807 char buffer[MAX_PATH+10];
808 DWORD size = sizeof(buffer);
809 HKEY hkey = 0;
810 HKEY appkey = 0;
811 DWORD len;
813 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
814 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
816 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
817 if (len && len < MAX_PATH)
819 HKEY tmpkey;
820 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
821 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
823 char *p, *appname = buffer;
824 if ((p = strrchr( appname, '/' ))) appname = p + 1;
825 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
826 strcat( appname, "\\Direct3D" );
827 TRACE("appname = [%s]\n", appname);
828 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
829 RegCloseKey( tmpkey );
833 if ( 0 != hkey || 0 != appkey )
835 if ( !get_config_key( hkey, appkey, "DirectDrawRenderer", buffer, size) )
837 if (!strcmp(buffer,"gdi"))
839 TRACE("Defaulting to GDI surfaces\n");
840 DefaultSurfaceType = SURFACE_GDI;
842 else if (!strcmp(buffer,"opengl"))
844 TRACE("Defaulting to opengl surfaces\n");
845 DefaultSurfaceType = SURFACE_OPENGL;
847 else
849 ERR("Unknown default surface type. Supported are:\n gdi, opengl\n");
854 DisableThreadLibraryCalls(hInstDLL);
855 TRACE("Attach counter: %ld\n", InterlockedIncrement(&counter));
857 else if (Reason == DLL_PROCESS_DETACH)
859 TRACE("Attach counter: %ld\n", InterlockedDecrement(&counter));
861 if(counter == 0)
863 if(!list_empty(&global_ddraw_list))
865 struct list *entry, *entry2;
866 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
868 /* We remove elemets from this loop */
869 LIST_FOR_EACH_SAFE(entry, entry2, &global_ddraw_list)
871 HRESULT hr;
872 DDSURFACEDESC2 desc;
873 int i;
874 IDirectDrawImpl *ddraw = LIST_ENTRY(entry, IDirectDrawImpl, ddraw_list_entry);
876 WARN("DDraw %p has a refcount of %ld\n", ddraw, ddraw->ref7 + ddraw->ref4 + ddraw->ref2 + ddraw->ref1);
878 /* Add references to each interface to avoid freeing them unexpectadely */
879 IDirectDraw_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw));
880 IDirectDraw2_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw2));
881 IDirectDraw4_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw4));
882 IDirectDraw7_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw7));
884 /* Does a D3D device exist? Destroy it
885 * TODO: Destroy all Vertex buffers, Lights, Materials
886 * and execture buffers too
888 if(ddraw->d3ddevice)
890 WARN("DDraw %p has d3ddevice %p attached\n", ddraw, ddraw->d3ddevice);
891 while(IDirect3DDevice7_Release(ICOM_INTERFACE(ddraw->d3ddevice, IDirect3DDevice7)));
894 /* Try to release the objects
895 * Do an EnumSurfaces to find any hanging surfaces
897 memset(&desc, 0, sizeof(desc));
898 desc.dwSize = sizeof(desc);
899 for(i = 0; i <= 1; i++)
901 hr = IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(ddraw, IDirectDraw7),
902 DDENUMSURFACES_ALL,
903 &desc,
904 (void *) ddraw,
905 DestroyCallback);
906 if(hr != D3D_OK)
907 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw);
910 /* Check the surface count */
911 if(ddraw->surfaces > 0)
912 ERR("DDraw %p still has %ld surfaces attached\n", ddraw, ddraw->surfaces);
914 /* Release all hanging references to destroy the objects. This
915 * restores the screen mode too
917 while(IDirectDraw_Release(ICOM_INTERFACE(ddraw, IDirectDraw)));
918 while(IDirectDraw2_Release(ICOM_INTERFACE(ddraw, IDirectDraw2)));
919 while(IDirectDraw4_Release(ICOM_INTERFACE(ddraw, IDirectDraw4)));
920 while(IDirectDraw7_Release(ICOM_INTERFACE(ddraw, IDirectDraw7)));
926 return TRUE;
929 void
930 remove_ddraw_object(IDirectDrawImpl *ddraw)
932 list_remove(&ddraw->ddraw_list_entry);