ddraw: Win64 printf format warning fixes.
[wine.git] / dlls / ddraw / main.c
blob8865fe586a75ef412bb9eeb05f67a813988c8676
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 /* DDraw list and critical section */
62 static struct list global_ddraw_list = LIST_INIT(global_ddraw_list);
64 static CRITICAL_SECTION ddraw_list_cs;
65 static CRITICAL_SECTION_DEBUG ddraw_list_cs_debug =
67 0, 0, &ddraw_list_cs,
68 { &ddraw_list_cs_debug.ProcessLocksList,
69 &ddraw_list_cs_debug.ProcessLocksList },
70 0, 0, { (DWORD_PTR)(__FILE__ ": ddraw_list_cs") }
72 static CRITICAL_SECTION ddraw_list_cs = { &ddraw_list_cs_debug, -1, 0, 0, 0, 0 };
74 /***********************************************************************
76 * Helper function for DirectDrawCreate and friends
77 * Creates a new DDraw interface with the given REFIID
79 * Interfaces that can be created:
80 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
81 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
82 * IDirect3D interfaces?)
84 * Arguments:
85 * guid: ID of the requested driver, NULL for the default driver.
86 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
87 * DD: Used to return the pointer to the created object
88 * UnkOuter: For aggregation, which is unsupported. Must be NULL
89 * iid: requested version ID.
91 * Returns:
92 * DD_OK if the Interface was created successfully
93 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
94 * E_OUTOFMEMORY if some allocation failed
96 ***********************************************************************/
97 static HRESULT
98 DDRAW_Create(GUID *guid,
99 void **DD,
100 IUnknown *UnkOuter,
101 REFIID iid)
103 IDirectDrawImpl *This = NULL;
104 HRESULT hr;
105 IWineD3D *wineD3D = NULL;
106 IWineD3DDevice *wineD3DDevice = NULL;
107 HDC hDC;
108 WINED3DDEVTYPE devicetype;
110 TRACE("(%s,%p,%p)\n", debugstr_guid(guid), DD, UnkOuter);
112 *DD = NULL;
114 /* We don't care about this guids. Well, there's no special guid anyway
115 * OK, we could
117 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
119 /* Use the reference device id. This doesn't actually change anything,
120 * WineD3D always uses OpenGL for D3D rendering. One could make it request
121 * indirect rendering
123 devicetype = WINED3DDEVTYPE_REF;
125 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
127 devicetype = WINED3DDEVTYPE_HAL;
129 else
131 devicetype = 0;
134 /* DDraw doesn't support aggregation, according to msdn */
135 if (UnkOuter != NULL)
136 return CLASS_E_NOAGGREGATION;
138 /* DirectDraw creation comes here */
139 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawImpl));
140 if(!This)
142 ERR("Out of memory when creating DirectDraw\n");
143 return E_OUTOFMEMORY;
146 /* The interfaces:
147 * IDirectDraw and IDirect3D are the same object,
148 * QueryInterface is used to get other interfaces.
150 ICOM_INIT_INTERFACE(This, IDirectDraw, IDirectDraw1_Vtbl);
151 ICOM_INIT_INTERFACE(This, IDirectDraw2, IDirectDraw2_Vtbl);
152 ICOM_INIT_INTERFACE(This, IDirectDraw4, IDirectDraw4_Vtbl);
153 ICOM_INIT_INTERFACE(This, IDirectDraw7, IDirectDraw7_Vtbl);
154 ICOM_INIT_INTERFACE(This, IDirect3D, IDirect3D1_Vtbl);
155 ICOM_INIT_INTERFACE(This, IDirect3D2, IDirect3D2_Vtbl);
156 ICOM_INIT_INTERFACE(This, IDirect3D3, IDirect3D3_Vtbl);
157 ICOM_INIT_INTERFACE(This, IDirect3D7, IDirect3D7_Vtbl);
159 /* See comments in IDirectDrawImpl_CreateNewSurface for a description
160 * of this member.
161 * Read from a registry key, should add a winecfg option later
163 This->ImplType = DefaultSurfaceType;
165 /* Get the current screen settings */
166 hDC = CreateDCA("DISPLAY", NULL, NULL, NULL);
167 This->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
168 DeleteDC(hDC);
169 This->orig_width = GetSystemMetrics(SM_CXSCREEN);
170 This->orig_height = GetSystemMetrics(SM_CYSCREEN);
172 if (hWineD3D == (HMODULE) -1)
174 hWineD3D = LoadLibraryA("wined3d");
175 if (hWineD3D)
176 pWineDirect3DCreate = (fnWineDirect3DCreate) GetProcAddress(hWineD3D, "WineDirect3DCreate");
179 if (!hWineD3D)
181 ERR("Couldn't load WineD3D - OpenGL libs not present?\n");
182 hr = DDERR_NODIRECTDRAWSUPPORT;
183 goto err_out;
186 /* Initialize WineD3D
188 * All Rendering (2D and 3D) is relayed to WineD3D,
189 * but DirectDraw specific management, like DDSURFACEDESC and DDPIXELFORMAT
190 * structure handling is handled in this lib.
192 wineD3D = pWineDirect3DCreate(0 /* SDKVersion */, 7 /* DXVersion */, (IUnknown *) This /* Parent */);
193 if(!wineD3D)
195 ERR("Failed to initialise WineD3D\n");
196 hr = E_OUTOFMEMORY;
197 goto err_out;
199 This->wineD3D = wineD3D;
200 TRACE("WineD3D created at %p\n", wineD3D);
202 /* Initialized member...
204 * It is set to false at creation time, and set to true in
205 * IDirectDraw7::Initialize. Its sole purpose is to return DD_OK on
206 * initialize only once
208 This->initialized = FALSE;
210 /* Initialize WineD3DDevice
212 * It is used for screen setup, surface and palette creation
213 * When a Direct3DDevice7 is created, the D3D capabilities of WineD3D are
214 * initialized
216 hr = IWineD3D_CreateDevice(wineD3D,
217 0 /*D3D_ADAPTER_DEFAULT*/,
218 devicetype,
219 NULL, /* FocusWindow, don't know yet */
220 0, /* BehaviorFlags */
221 &wineD3DDevice,
222 (IUnknown *) ICOM_INTERFACE(This, IDirectDraw7));
223 if(FAILED(hr))
225 ERR("Failed to create a wineD3DDevice, result = %x\n", hr);
226 goto err_out;
228 This->wineD3DDevice = wineD3DDevice;
229 TRACE("wineD3DDevice created at %p\n", This->wineD3DDevice);
231 /* Register the window class
233 * It is used to create a hidden window for D3D
234 * rendering, if the application didn't pass one.
235 * It can also be used for Creating a device window
236 * from SetCooperativeLevel
238 * The name: DDRAW_<address>. The classname is
239 * 32 bit long, so a 64 bit address will fit nicely
240 * (Will this be compiled for 64 bit anyway?)
243 sprintf(This->classname, "DDRAW_%p", This);
245 memset(&This->wnd_class, 0, sizeof(This->wnd_class));
246 This->wnd_class.style = CS_HREDRAW | CS_VREDRAW;
247 This->wnd_class.lpfnWndProc = DefWindowProcA;
248 This->wnd_class.cbClsExtra = 0;
249 This->wnd_class.cbWndExtra = 0;
250 This->wnd_class.hInstance = GetModuleHandleA(0);
251 This->wnd_class.hIcon = 0;
252 This->wnd_class.hCursor = 0;
253 This->wnd_class.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
254 This->wnd_class.lpszMenuName = NULL;
255 This->wnd_class.lpszClassName = This->classname;
256 if(!RegisterClassA(&This->wnd_class))
258 ERR("RegisterClassA failed!\n");
259 goto err_out;
262 /* Get the amount of video memory */
263 This->total_vidmem = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
265 /* Initialize the caps */
266 This->caps.dwSize = sizeof(This->caps);
267 /* do not report DDCAPS_OVERLAY and friends since we don't support overlays */
268 #define BLIT_CAPS (DDCAPS_BLT | DDCAPS_BLTCOLORFILL | DDCAPS_BLTDEPTHFILL \
269 | DDCAPS_BLTSTRETCH | DDCAPS_CANBLTSYSMEM | DDCAPS_CANCLIP \
270 | DDCAPS_CANCLIPSTRETCHED | DDCAPS_COLORKEY \
271 | DDCAPS_COLORKEYHWASSIST | DDCAPS_ALIGNBOUNDARYSRC )
272 #define CKEY_CAPS (DDCKEYCAPS_DESTBLT | DDCKEYCAPS_SRCBLT)
273 #define FX_CAPS (DDFXCAPS_BLTALPHA | DDFXCAPS_BLTMIRRORLEFTRIGHT \
274 | DDFXCAPS_BLTMIRRORUPDOWN | DDFXCAPS_BLTROTATION90 \
275 | DDFXCAPS_BLTSHRINKX | DDFXCAPS_BLTSHRINKXN \
276 | DDFXCAPS_BLTSHRINKY | DDFXCAPS_BLTSHRINKXN \
277 | DDFXCAPS_BLTSTRETCHX | DDFXCAPS_BLTSTRETCHXN \
278 | DDFXCAPS_BLTSTRETCHY | DDFXCAPS_BLTSTRETCHYN)
279 This->caps.dwCaps |= DDCAPS_GDI | DDCAPS_PALETTE | BLIT_CAPS;
281 This->caps.dwCaps2 |= DDCAPS2_CERTIFIED | DDCAPS2_NOPAGELOCKREQUIRED |
282 DDCAPS2_PRIMARYGAMMA | DDCAPS2_WIDESURFACES |
283 DDCAPS2_CANRENDERWINDOWED;
284 This->caps.dwCKeyCaps |= CKEY_CAPS;
285 This->caps.dwFXCaps |= FX_CAPS;
286 This->caps.dwPalCaps |= DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE;
287 This->caps.dwVidMemTotal = This->total_vidmem;
288 This->caps.dwVidMemFree = This->total_vidmem;
289 This->caps.dwSVBCaps |= BLIT_CAPS;
290 This->caps.dwSVBCKeyCaps |= CKEY_CAPS;
291 This->caps.dwSVBFXCaps |= FX_CAPS;
292 This->caps.dwVSBCaps |= BLIT_CAPS;
293 This->caps.dwVSBCKeyCaps |= CKEY_CAPS;
294 This->caps.dwVSBFXCaps |= FX_CAPS;
295 This->caps.dwSSBCaps |= BLIT_CAPS;
296 This->caps.dwSSBCKeyCaps |= CKEY_CAPS;
297 This->caps.dwSSBFXCaps |= FX_CAPS;
298 This->caps.ddsCaps.dwCaps |= DDSCAPS_ALPHA | DDSCAPS_BACKBUFFER |
299 DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER |
300 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_PALETTE |
301 DDSCAPS_PRIMARYSURFACE | DDSCAPS_SYSTEMMEMORY |
302 DDSCAPS_VIDEOMEMORY | DDSCAPS_VISIBLE;
303 /* Hacks for D3D code */
304 /* TODO: Check if WineD3D has 3D enabled
305 Need opengl surfaces or auto for 3D
307 if(This->ImplType == 0 || This->ImplType == SURFACE_OPENGL)
309 This->caps.dwCaps |= DDCAPS_3D;
310 This->caps.ddsCaps.dwCaps |= DDSCAPS_3DDEVICE | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE | DDSCAPS_ZBUFFER;
312 This->caps.ddsOldCaps.dwCaps = This->caps.ddsCaps.dwCaps;
314 #undef BLIT_CAPS
315 #undef CKEY_CAPS
316 #undef FX_CAPS
318 EnterCriticalSection(&ddraw_list_cs);
319 list_add_head(&global_ddraw_list, &This->ddraw_list_entry);
320 LeaveCriticalSection(&ddraw_list_cs);
322 /* Call QueryInterface to get the pointer to the requested interface. This also initializes
323 * The required refcount
325 hr = IDirectDraw7_QueryInterface( ICOM_INTERFACE(This, IDirectDraw7), iid, DD);
326 if(SUCCEEDED(hr)) return DD_OK;
328 err_out:
329 /* Let's hope we never need this ;) */
330 if(wineD3DDevice) IWineD3DDevice_Release(wineD3DDevice);
331 if(wineD3D) IWineD3D_Release(wineD3D);
332 HeapFree(GetProcessHeap(), 0, This);
333 return hr;
336 /***********************************************************************
337 * DirectDrawCreate (DDRAW.@)
339 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
340 * interfaces in theory
342 * Arguments, return values: See DDRAW_Create
344 ***********************************************************************/
345 HRESULT WINAPI
346 DirectDrawCreate(GUID *GUID,
347 IDirectDraw **DD,
348 IUnknown *UnkOuter)
350 TRACE("(%s,%p,%p)\n", debugstr_guid(GUID), DD, UnkOuter);
352 return DDRAW_Create(GUID, (void **) DD, UnkOuter, &IID_IDirectDraw);
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
365 DirectDrawCreateEx(GUID *GUID,
366 void **DD,
367 REFIID iid,
368 IUnknown *UnkOuter)
370 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(GUID), DD, debugstr_guid(iid), UnkOuter);
372 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
373 return DDERR_INVALIDPARAMS;
375 return DDRAW_Create(GUID, DD, UnkOuter, iid);
378 /***********************************************************************
379 * DirectDrawEnumerateA (DDRAW.@)
381 * Enumerates legacy ddraw drivers, ascii version. We only have one
382 * driver, which relays to WineD3D. If we were sufficiently cool,
383 * we could offer various interfaces, which use a different default surface
384 * implementation, but I think it's better to offer this choice in
385 * winecfg, because some apps use the default driver, so we would need
386 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
388 * Arguments:
389 * Callback: Callback function from the app
390 * Context: Argument to the call back.
392 * Returns:
393 * DD_OK on success
394 * E_INVALIDARG if the Callback caused a page fault
397 ***********************************************************************/
398 HRESULT WINAPI
399 DirectDrawEnumerateA(LPDDENUMCALLBACKA Callback,
400 void *Context)
402 BOOL stop = FALSE;
404 TRACE(" Enumerating default DirectDraw HAL interface\n");
405 /* We only have one driver */
406 __TRY
408 static CHAR driver_desc[] = "DirectDraw HAL",
409 driver_name[] = "display";
411 stop = !Callback(NULL, driver_desc, driver_name, Context);
413 __EXCEPT_PAGE_FAULT
415 return E_INVALIDARG;
417 __ENDTRY
419 TRACE(" End of enumeration\n");
420 return DD_OK;
423 /***********************************************************************
424 * DirectDrawEnumerateExA (DDRAW.@)
426 * Enumerates DirectDraw7 drivers, ascii version. See
427 * the comments above DirectDrawEnumerateA for more details.
429 * The Flag member is not supported right now.
431 ***********************************************************************/
432 HRESULT WINAPI
433 DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA Callback,
434 void *Context,
435 DWORD Flags)
437 BOOL stop = FALSE;
438 TRACE("Enumerating default DirectDraw HAL interface\n");
440 /* We only have one driver by now */
441 __TRY
443 static CHAR driver_desc[] = "DirectDraw HAL",
444 driver_name[] = "display";
446 /* QuickTime expects the description "DirectDraw HAL" */
447 stop = !Callback(NULL, driver_desc, driver_name, Context, 0);
449 __EXCEPT_PAGE_FAULT
451 return E_INVALIDARG;
453 __ENDTRY;
455 TRACE("End of enumeration\n");
456 return DD_OK;
459 /***********************************************************************
460 * DirectDrawEnumerateW (DDRAW.@)
462 * Enumerates legacy drivers, unicode version. See
463 * the comments above DirectDrawEnumerateA for more details.
465 * The Flag member is not supported right now.
467 ***********************************************************************/
469 /***********************************************************************
470 * DirectDrawEnumerateExW (DDRAW.@)
472 * Enumerates DirectDraw7 drivers, unicode version. See
473 * the comments above DirectDrawEnumerateA for more details.
475 * The Flag member is not supported right now.
477 ***********************************************************************/
479 /***********************************************************************
480 * Classfactory implementation.
481 ***********************************************************************/
483 /***********************************************************************
484 * CF_CreateDirectDraw
486 * DDraw 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_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
499 void **obj)
501 HRESULT hr;
503 TRACE("(%p,%s,%p)\n", UnkOuter, debugstr_guid(iid), obj);
505 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
506 return hr;
509 /***********************************************************************
510 * CF_CreateDirectDraw
512 * Clipper creation function for the class factory
514 * Params:
515 * UnkOuter: Set to NULL
516 * iid: ID of the wanted interface
517 * obj: Address to pass the interface pointer back
519 * Returns
520 * DD_OK / DDERR*, see DDRAW_Create
522 ***********************************************************************/
523 static HRESULT
524 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
525 void **obj)
527 HRESULT hr;
528 IDirectDrawClipper *Clip;
530 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
531 if (hr != DD_OK) return hr;
533 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
534 IDirectDrawClipper_Release(Clip);
535 return hr;
538 static const struct object_creation_info object_creation[] =
540 { &CLSID_DirectDraw, CF_CreateDirectDraw },
541 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
542 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
545 /*******************************************************************************
546 * IDirectDrawClassFactory::QueryInterface
548 * QueryInterface for the class factory
550 * PARAMS
551 * riid Reference to identifier of queried interface
552 * ppv Address to return the interface pointer at
554 * RETURNS
555 * Success: S_OK
556 * Failure: E_NOINTERFACE
558 *******************************************************************************/
559 static HRESULT WINAPI
560 IDirectDrawClassFactoryImpl_QueryInterface(IClassFactory *iface,
561 REFIID riid,
562 void **obj)
564 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
566 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obj);
568 if (IsEqualGUID(riid, &IID_IUnknown)
569 || IsEqualGUID(riid, &IID_IClassFactory))
571 IClassFactory_AddRef(iface);
572 *obj = This;
573 return S_OK;
576 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),obj);
577 return E_NOINTERFACE;
580 /*******************************************************************************
581 * IDirectDrawClassFactory::AddRef
583 * AddRef for the class factory
585 * RETURNS
586 * The new refcount
588 *******************************************************************************/
589 static ULONG WINAPI
590 IDirectDrawClassFactoryImpl_AddRef(IClassFactory *iface)
592 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
593 ULONG ref = InterlockedIncrement(&This->ref);
595 TRACE("(%p)->() incrementing from %d.\n", This, ref - 1);
597 return ref;
600 /*******************************************************************************
601 * IDirectDrawClassFactory::Release
603 * Release for the class factory. If the refcount falls to 0, the object
604 * is destroyed
606 * RETURNS
607 * The new refcount
609 *******************************************************************************/
610 static ULONG WINAPI
611 IDirectDrawClassFactoryImpl_Release(IClassFactory *iface)
613 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
614 ULONG ref = InterlockedDecrement(&This->ref);
615 TRACE("(%p)->() decrementing from %d.\n", This, ref+1);
617 if (ref == 0)
618 HeapFree(GetProcessHeap(), 0, This);
620 return ref;
624 /*******************************************************************************
625 * IDirectDrawClassFactory::CreateInstance
627 * What is this? Seems to create DirectDraw objects...
629 * Params
630 * The ususal things???
632 * RETURNS
633 * ???
635 *******************************************************************************/
636 static HRESULT WINAPI
637 IDirectDrawClassFactoryImpl_CreateInstance(IClassFactory *iface,
638 IUnknown *UnkOuter,
639 REFIID riid,
640 void **obj)
642 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
644 TRACE("(%p)->(%p,%s,%p)\n",This,UnkOuter,debugstr_guid(riid),obj);
646 return This->pfnCreateInstance(UnkOuter, riid, obj);
649 /*******************************************************************************
650 * IDirectDrawClassFactory::LockServer
652 * What is this?
654 * Params
655 * ???
657 * RETURNS
658 * S_OK, because it's a stub
660 *******************************************************************************/
661 static HRESULT WINAPI
662 IDirectDrawClassFactoryImpl_LockServer(IClassFactory *iface,BOOL dolock)
664 ICOM_THIS_FROM(IClassFactoryImpl, IClassFactory, iface);
665 FIXME("(%p)->(%d),stub!\n",This,dolock);
666 return S_OK;
669 /*******************************************************************************
670 * The class factory VTable
671 *******************************************************************************/
672 static const IClassFactoryVtbl IClassFactory_Vtbl =
674 IDirectDrawClassFactoryImpl_QueryInterface,
675 IDirectDrawClassFactoryImpl_AddRef,
676 IDirectDrawClassFactoryImpl_Release,
677 IDirectDrawClassFactoryImpl_CreateInstance,
678 IDirectDrawClassFactoryImpl_LockServer
681 /*******************************************************************************
682 * DllGetClassObject [DDRAW.@]
683 * Retrieves class object from a DLL object
685 * NOTES
686 * Docs say returns STDAPI
688 * PARAMS
689 * rclsid [I] CLSID for the class object
690 * riid [I] Reference to identifier of interface for class object
691 * ppv [O] Address of variable to receive interface pointer for riid
693 * RETURNS
694 * Success: S_OK
695 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
696 * E_UNEXPECTED
698 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
700 unsigned int i;
701 IClassFactoryImpl *factory;
703 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
705 if ( !IsEqualGUID( &IID_IClassFactory, riid )
706 && ! IsEqualGUID( &IID_IUnknown, riid) )
707 return E_NOINTERFACE;
709 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
711 if (IsEqualGUID(object_creation[i].clsid, rclsid))
712 break;
715 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
717 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
718 return CLASS_E_CLASSNOTAVAILABLE;
721 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
722 if (factory == NULL) return E_OUTOFMEMORY;
724 ICOM_INIT_INTERFACE(factory, IClassFactory, IClassFactory_Vtbl);
725 factory->ref = 1;
727 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
729 *ppv = ICOM_INTERFACE(factory, IClassFactory);
730 return S_OK;
734 /*******************************************************************************
735 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
737 * RETURNS
738 * Success: S_OK
739 * Failure: S_FALSE
741 HRESULT WINAPI DllCanUnloadNow(void)
743 FIXME("(void): stub\n");
744 return S_FALSE;
747 /*******************************************************************************
748 * DestroyCallback
750 * Callback function for the EnumSurfaces call in DllMain.
751 * Dumps some surface info and releases the surface
753 * Params:
754 * surf: The enumerated surface
755 * desc: it's description
756 * context: Pointer to the ddraw impl
758 * Returns:
759 * DDENUMRET_OK;
760 *******************************************************************************/
761 static HRESULT WINAPI
762 DestroyCallback(IDirectDrawSurface7 *surf,
763 DDSURFACEDESC2 *desc,
764 void *context)
766 IDirectDrawSurfaceImpl *Impl = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, surf);
767 IDirectDrawImpl *ddraw = (IDirectDrawImpl *) context;
768 ULONG ref;
770 ref = IDirectDrawSurface7_Release(surf); /* For the EnumSurfaces */
771 WARN("Surface %p has an reference count of %d\n", Impl, ref);
773 /* Skip surfaces which are attached somewhere or which are
774 * part of a complex compound. They will get released when destroying
775 * the root
777 if( (Impl->first_complex != Impl) || (Impl->first_attached != Impl) )
778 return DDENUMRET_OK;
779 /* Skip our depth stencil surface, it will be released with the render target */
780 if( Impl == ddraw->DepthStencilBuffer)
781 return DDENUMRET_OK;
783 /* Destroy the surface */
784 while(ref) ref = IDirectDrawSurface7_Release(surf);
786 return DDENUMRET_OK;
789 /***********************************************************************
790 * get_config_key
792 * Reads a config key from the registry. Taken from WineD3D
794 ***********************************************************************/
795 inline static DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
797 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
798 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
799 return ERROR_FILE_NOT_FOUND;
802 /***********************************************************************
803 * DllMain (DDRAW.0)
805 * Could be used to register DirectDraw drivers, if we have more than
806 * one. Also used to destroy any objects left at unload if the
807 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
809 ***********************************************************************/
810 BOOL WINAPI
811 DllMain(HINSTANCE hInstDLL,
812 DWORD Reason,
813 void *lpv)
815 TRACE("(%p,%x,%p)\n", hInstDLL, Reason, lpv);
816 if (Reason == DLL_PROCESS_ATTACH)
818 char buffer[MAX_PATH+10];
819 DWORD size = sizeof(buffer);
820 HKEY hkey = 0;
821 HKEY appkey = 0;
822 DWORD len;
824 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
825 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
827 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
828 if (len && len < MAX_PATH)
830 HKEY tmpkey;
831 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
832 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
834 char *p, *appname = buffer;
835 if ((p = strrchr( appname, '/' ))) appname = p + 1;
836 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
837 strcat( appname, "\\Direct3D" );
838 TRACE("appname = [%s]\n", appname);
839 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
840 RegCloseKey( tmpkey );
844 if ( 0 != hkey || 0 != appkey )
846 if ( !get_config_key( hkey, appkey, "DirectDrawRenderer", buffer, size) )
848 if (!strcmp(buffer,"gdi"))
850 TRACE("Defaulting to GDI surfaces\n");
851 DefaultSurfaceType = SURFACE_GDI;
853 else if (!strcmp(buffer,"opengl"))
855 TRACE("Defaulting to opengl surfaces\n");
856 DefaultSurfaceType = SURFACE_OPENGL;
858 else
860 ERR("Unknown default surface type. Supported are:\n gdi, opengl\n");
865 DisableThreadLibraryCalls(hInstDLL);
867 else if (Reason == DLL_PROCESS_DETACH)
869 if(!list_empty(&global_ddraw_list))
871 struct list *entry, *entry2;
872 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
874 /* We remove elemets from this loop */
875 LIST_FOR_EACH_SAFE(entry, entry2, &global_ddraw_list)
877 HRESULT hr;
878 DDSURFACEDESC2 desc;
879 int i;
880 IDirectDrawImpl *ddraw = LIST_ENTRY(entry, IDirectDrawImpl, ddraw_list_entry);
882 WARN("DDraw %p has a refcount of %d\n", ddraw, ddraw->ref7 + ddraw->ref4 + ddraw->ref2 + ddraw->ref1);
884 /* Add references to each interface to avoid freeing them unexpectadely */
885 IDirectDraw_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw));
886 IDirectDraw2_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw2));
887 IDirectDraw4_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw4));
888 IDirectDraw7_AddRef(ICOM_INTERFACE(ddraw, IDirectDraw7));
890 /* Does a D3D device exist? Destroy it
891 * TODO: Destroy all Vertex buffers, Lights, Materials
892 * and execture buffers too
894 if(ddraw->d3ddevice)
896 WARN("DDraw %p has d3ddevice %p attached\n", ddraw, ddraw->d3ddevice);
897 while(IDirect3DDevice7_Release(ICOM_INTERFACE(ddraw->d3ddevice, IDirect3DDevice7)));
900 /* Try to release the objects
901 * Do an EnumSurfaces to find any hanging surfaces
903 memset(&desc, 0, sizeof(desc));
904 desc.dwSize = sizeof(desc);
905 for(i = 0; i <= 1; i++)
907 hr = IDirectDraw7_EnumSurfaces(ICOM_INTERFACE(ddraw, IDirectDraw7),
908 DDENUMSURFACES_ALL,
909 &desc,
910 (void *) ddraw,
911 DestroyCallback);
912 if(hr != D3D_OK)
913 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw);
916 /* Check the surface count */
917 if(ddraw->surfaces > 0)
918 ERR("DDraw %p still has %d surfaces attached\n", ddraw, ddraw->surfaces);
920 /* Release all hanging references to destroy the objects. This
921 * restores the screen mode too
923 while(IDirectDraw_Release(ICOM_INTERFACE(ddraw, IDirectDraw)));
924 while(IDirectDraw2_Release(ICOM_INTERFACE(ddraw, IDirectDraw2)));
925 while(IDirectDraw4_Release(ICOM_INTERFACE(ddraw, IDirectDraw4)));
926 while(IDirectDraw7_Release(ICOM_INTERFACE(ddraw, IDirectDraw7)));
931 return TRUE;
934 void
935 remove_ddraw_object(IDirectDrawImpl *ddraw)
937 EnterCriticalSection(&ddraw_list_cs);
938 list_remove(&ddraw->ddraw_list_entry);
939 LeaveCriticalSection(&ddraw_list_cs);