079800d9a2ba7ef7907cd929a7a61468904893f8
[reactos.git] / dll / directx / wine / ddraw / main.c
1 /* DirectDraw Base Functions
2 *
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
8 *
9 * This file contains the (internal) driver registration functions,
10 * driver enumeration APIs and DirectDraw creation functions.
11 *
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.
16 *
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.
21 *
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
25 */
26
27 #include "config.h"
28 #include "wine/port.h"
29 #include "wine/debug.h"
30
31 #include <assert.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <stdlib.h>
35
36 #define COBJMACROS
37
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winerror.h"
41 #include "wingdi.h"
42 #include "wine/exception.h"
43 #include "winreg.h"
44
45 #include "ddraw.h"
46 #include "d3d.h"
47
48 #define DDRAW_INIT_GUID
49 #include "ddraw_private.h"
50
51 static typeof(WineDirect3DCreate) *pWineDirect3DCreate;
52
53 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
54
55 /* The configured default surface */
56 WINED3DSURFTYPE DefaultSurfaceType = SURFACE_UNKNOWN;
57
58 /* DDraw list and critical section */
59 static struct list global_ddraw_list = LIST_INIT(global_ddraw_list);
60
61 static CRITICAL_SECTION_DEBUG ddraw_cs_debug =
62 {
63 0, 0, &ddraw_cs,
64 { &ddraw_cs_debug.ProcessLocksList,
65 &ddraw_cs_debug.ProcessLocksList },
66 0, 0, { (DWORD_PTR)(__FILE__ ": ddraw_cs") }
67 };
68 CRITICAL_SECTION ddraw_cs = { &ddraw_cs_debug, -1, 0, 0, 0, 0 };
69
70 /* value of ForceRefreshRate */
71 DWORD force_refresh_rate = 0;
72
73 /*
74 * Helper Function for DDRAW_Create and DirectDrawCreateClipper for
75 * lazy loading of the Wine D3D driver.
76 *
77 * Returns
78 * TRUE on success
79 * FALSE on failure.
80 */
81
82 BOOL LoadWineD3D(void)
83 {
84 static HMODULE hWineD3D = (HMODULE) -1;
85 if (hWineD3D == (HMODULE) -1)
86 {
87 hWineD3D = LoadLibraryA("wined3d");
88 if (hWineD3D)
89 {
90 pWineDirect3DCreate = (typeof(WineDirect3DCreate) *)GetProcAddress(hWineD3D, "WineDirect3DCreate");
91 pWineDirect3DCreateClipper = (typeof(WineDirect3DCreateClipper) *) GetProcAddress(hWineD3D, "WineDirect3DCreateClipper");
92 return TRUE;
93 }
94 }
95 return hWineD3D != NULL;
96 }
97
98 /***********************************************************************
99 *
100 * Helper function for DirectDrawCreate and friends
101 * Creates a new DDraw interface with the given REFIID
102 *
103 * Interfaces that can be created:
104 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
105 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
106 * IDirect3D interfaces?)
107 *
108 * Arguments:
109 * guid: ID of the requested driver, NULL for the default driver.
110 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
111 * DD: Used to return the pointer to the created object
112 * UnkOuter: For aggregation, which is unsupported. Must be NULL
113 * iid: requested version ID.
114 *
115 * Returns:
116 * DD_OK if the Interface was created successfully
117 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
118 * E_OUTOFMEMORY if some allocation failed
119 *
120 ***********************************************************************/
121 static HRESULT
122 DDRAW_Create(const GUID *guid,
123 void **DD,
124 IUnknown *UnkOuter,
125 REFIID iid)
126 {
127 IDirectDrawImpl *This = NULL;
128 HRESULT hr;
129 IWineD3D *wineD3D = NULL;
130 IWineD3DDevice *wineD3DDevice = NULL;
131 HDC hDC;
132 WINED3DDEVTYPE devicetype;
133
134 TRACE("(%s,%p,%p)\n", debugstr_guid(guid), DD, UnkOuter);
135
136 *DD = NULL;
137
138 /* We don't care about this guids. Well, there's no special guid anyway
139 * OK, we could
140 */
141 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
142 {
143 /* Use the reference device id. This doesn't actually change anything,
144 * WineD3D always uses OpenGL for D3D rendering. One could make it request
145 * indirect rendering
146 */
147 devicetype = WINED3DDEVTYPE_REF;
148 }
149 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
150 {
151 devicetype = WINED3DDEVTYPE_HAL;
152 }
153 else
154 {
155 devicetype = 0;
156 }
157
158 /* DDraw doesn't support aggregation, according to msdn */
159 if (UnkOuter != NULL)
160 return CLASS_E_NOAGGREGATION;
161
162 /* DirectDraw creation comes here */
163 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectDrawImpl));
164 if(!This)
165 {
166 ERR("Out of memory when creating DirectDraw\n");
167 return E_OUTOFMEMORY;
168 }
169
170 /* The interfaces:
171 * IDirectDraw and IDirect3D are the same object,
172 * QueryInterface is used to get other interfaces.
173 */
174 This->lpVtbl = &IDirectDraw7_Vtbl;
175 This->IDirectDraw_vtbl = &IDirectDraw1_Vtbl;
176 This->IDirectDraw2_vtbl = &IDirectDraw2_Vtbl;
177 This->IDirectDraw4_vtbl = &IDirectDraw4_Vtbl;
178 This->IDirect3D_vtbl = &IDirect3D1_Vtbl;
179 This->IDirect3D2_vtbl = &IDirect3D2_Vtbl;
180 This->IDirect3D3_vtbl = &IDirect3D3_Vtbl;
181 This->IDirect3D7_vtbl = &IDirect3D7_Vtbl;
182 This->device_parent_vtbl = &ddraw_wined3d_device_parent_vtbl;
183
184 /* See comments in IDirectDrawImpl_CreateNewSurface for a description
185 * of this member.
186 * Read from a registry key, should add a winecfg option later
187 */
188 This->ImplType = DefaultSurfaceType;
189
190 /* Get the current screen settings */
191 hDC = GetDC(0);
192 This->orig_bpp = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
193 ReleaseDC(0, hDC);
194 This->orig_width = GetSystemMetrics(SM_CXSCREEN);
195 This->orig_height = GetSystemMetrics(SM_CYSCREEN);
196
197 if (!LoadWineD3D())
198 {
199 ERR("Couldn't load WineD3D - OpenGL libs not present?\n");
200 hr = DDERR_NODIRECTDRAWSUPPORT;
201 goto err_out;
202 }
203
204 /* Initialize WineD3D
205 *
206 * All Rendering (2D and 3D) is relayed to WineD3D,
207 * but DirectDraw specific management, like DDSURFACEDESC and DDPIXELFORMAT
208 * structure handling is handled in this lib.
209 */
210 wineD3D = pWineDirect3DCreate(7 /* DXVersion */, (IUnknown *) This /* Parent */);
211 if(!wineD3D)
212 {
213 ERR("Failed to initialise WineD3D\n");
214 hr = E_OUTOFMEMORY;
215 goto err_out;
216 }
217 This->wineD3D = wineD3D;
218 TRACE("WineD3D created at %p\n", wineD3D);
219
220 /* Initialized member...
221 *
222 * It is set to false at creation time, and set to true in
223 * IDirectDraw7::Initialize. Its sole purpose is to return DD_OK on
224 * initialize only once
225 */
226 This->initialized = FALSE;
227
228 /* Initialize WineD3DDevice
229 *
230 * It is used for screen setup, surface and palette creation
231 * When a Direct3DDevice7 is created, the D3D capabilities of WineD3D are
232 * initialized
233 */
234 hr = IWineD3D_CreateDevice(wineD3D, 0 /* D3D_ADAPTER_DEFAULT */, devicetype, NULL /* FocusWindow, don't know yet */,
235 0 /* BehaviorFlags */, (IUnknown *)This, (IWineD3DDeviceParent *)&This->device_parent_vtbl, &wineD3DDevice);
236 if(FAILED(hr))
237 {
238 ERR("Failed to create a wineD3DDevice, result = %x\n", hr);
239 goto err_out;
240 }
241 This->wineD3DDevice = wineD3DDevice;
242 TRACE("wineD3DDevice created at %p\n", This->wineD3DDevice);
243
244 /* Register the window class
245 *
246 * It is used to create a hidden window for D3D
247 * rendering, if the application didn't pass one.
248 * It can also be used for Creating a device window
249 * from SetCooperativeLevel
250 *
251 * The name: DDRAW_<address>. The classname is
252 * 32 bit long, so a 64 bit address will fit nicely
253 * (Will this be compiled for 64 bit anyway?)
254 *
255 */
256 sprintf(This->classname, "DDRAW_%p", This);
257
258 memset(&This->wnd_class, 0, sizeof(This->wnd_class));
259 This->wnd_class.style = CS_HREDRAW | CS_VREDRAW;
260 This->wnd_class.lpfnWndProc = DefWindowProcA;
261 This->wnd_class.cbClsExtra = 0;
262 This->wnd_class.cbWndExtra = 0;
263 This->wnd_class.hInstance = GetModuleHandleA(0);
264 This->wnd_class.hIcon = 0;
265 This->wnd_class.hCursor = 0;
266 This->wnd_class.hbrBackground = GetStockObject(BLACK_BRUSH);
267 This->wnd_class.lpszMenuName = NULL;
268 This->wnd_class.lpszClassName = This->classname;
269 if(!RegisterClassA(&This->wnd_class))
270 {
271 ERR("RegisterClassA failed!\n");
272 goto err_out;
273 }
274
275 /* Get the amount of video memory */
276 This->total_vidmem = IWineD3DDevice_GetAvailableTextureMem(This->wineD3DDevice);
277
278 list_init(&This->surface_list);
279 list_add_head(&global_ddraw_list, &This->ddraw_list_entry);
280
281 /* Call QueryInterface to get the pointer to the requested interface. This also initializes
282 * The required refcount
283 */
284 hr = IDirectDraw7_QueryInterface((IDirectDraw7 *)This, iid, DD);
285 if(SUCCEEDED(hr)) return DD_OK;
286
287 err_out:
288 /* Let's hope we never need this ;) */
289 if(wineD3DDevice) IWineD3DDevice_Release(wineD3DDevice);
290 if(wineD3D) IWineD3D_Release(wineD3D);
291 if(This) HeapFree(GetProcessHeap(), 0, This->decls);
292 HeapFree(GetProcessHeap(), 0, This);
293 return hr;
294 }
295
296 /***********************************************************************
297 * DirectDrawCreate (DDRAW.@)
298 *
299 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
300 * interfaces in theory
301 *
302 * Arguments, return values: See DDRAW_Create
303 *
304 ***********************************************************************/
305 HRESULT WINAPI
306 DirectDrawCreate(GUID *GUID,
307 LPDIRECTDRAW *DD,
308 IUnknown *UnkOuter)
309 {
310 HRESULT hr;
311 TRACE("(%s,%p,%p)\n", debugstr_guid(GUID), DD, UnkOuter);
312
313 EnterCriticalSection(&ddraw_cs);
314 hr = DDRAW_Create(GUID, (void **) DD, UnkOuter, &IID_IDirectDraw);
315 LeaveCriticalSection(&ddraw_cs);
316 return hr;
317 }
318
319 /***********************************************************************
320 * DirectDrawCreateEx (DDRAW.@)
321 *
322 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
323 * interfaces are requested.
324 *
325 * Arguments, return values: See DDRAW_Create
326 *
327 ***********************************************************************/
328 HRESULT WINAPI
329 DirectDrawCreateEx(GUID *GUID,
330 LPVOID *DD,
331 REFIID iid,
332 IUnknown *UnkOuter)
333 {
334 HRESULT hr;
335 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(GUID), DD, debugstr_guid(iid), UnkOuter);
336
337 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
338 return DDERR_INVALIDPARAMS;
339
340 EnterCriticalSection(&ddraw_cs);
341 hr = DDRAW_Create(GUID, DD, UnkOuter, iid);
342 LeaveCriticalSection(&ddraw_cs);
343 return hr;
344 }
345
346 /***********************************************************************
347 * DirectDrawEnumerateA (DDRAW.@)
348 *
349 * Enumerates legacy ddraw drivers, ascii version. We only have one
350 * driver, which relays to WineD3D. If we were sufficiently cool,
351 * we could offer various interfaces, which use a different default surface
352 * implementation, but I think it's better to offer this choice in
353 * winecfg, because some apps use the default driver, so we would need
354 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
355 *
356 * Arguments:
357 * Callback: Callback function from the app
358 * Context: Argument to the call back.
359 *
360 * Returns:
361 * DD_OK on success
362 * E_INVALIDARG if the Callback caused a page fault
363 *
364 *
365 ***********************************************************************/
366 HRESULT WINAPI
367 DirectDrawEnumerateA(LPDDENUMCALLBACKA Callback,
368 LPVOID Context)
369 {
370 TRACE("(%p, %p)\n", Callback, Context);
371
372 TRACE(" Enumerating default DirectDraw HAL interface\n");
373 /* We only have one driver */
374 __TRY
375 {
376 static CHAR driver_desc[] = "DirectDraw HAL",
377 driver_name[] = "display";
378
379 Callback(NULL, driver_desc, driver_name, Context);
380 }
381 __EXCEPT_PAGE_FAULT
382 {
383 return DDERR_INVALIDPARAMS;
384 }
385 __ENDTRY
386
387 TRACE(" End of enumeration\n");
388 return DD_OK;
389 }
390
391 /***********************************************************************
392 * DirectDrawEnumerateExA (DDRAW.@)
393 *
394 * Enumerates DirectDraw7 drivers, ascii version. See
395 * the comments above DirectDrawEnumerateA for more details.
396 *
397 * The Flag member is not supported right now.
398 *
399 ***********************************************************************/
400 HRESULT WINAPI
401 DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA Callback,
402 LPVOID Context,
403 DWORD Flags)
404 {
405 TRACE("(%p, %p, 0x%08x)\n", Callback, Context, Flags);
406
407 if (Flags & ~(DDENUM_ATTACHEDSECONDARYDEVICES |
408 DDENUM_DETACHEDSECONDARYDEVICES |
409 DDENUM_NONDISPLAYDEVICES))
410 return DDERR_INVALIDPARAMS;
411
412 if (Flags)
413 FIXME("flags 0x%08x not handled\n", Flags);
414
415 TRACE("Enumerating default DirectDraw HAL interface\n");
416
417 /* We only have one driver by now */
418 __TRY
419 {
420 static CHAR driver_desc[] = "DirectDraw HAL",
421 driver_name[] = "display";
422
423 /* QuickTime expects the description "DirectDraw HAL" */
424 Callback(NULL, driver_desc, driver_name, Context, 0);
425 }
426 __EXCEPT_PAGE_FAULT
427 {
428 return DDERR_INVALIDPARAMS;
429 }
430 __ENDTRY;
431
432 TRACE("End of enumeration\n");
433 return DD_OK;
434 }
435
436 /***********************************************************************
437 * DirectDrawEnumerateW (DDRAW.@)
438 *
439 * Enumerates legacy drivers, unicode version.
440 * This function is not implemented on Windows.
441 *
442 ***********************************************************************/
443 HRESULT WINAPI
444 DirectDrawEnumerateW(LPDDENUMCALLBACKW Callback,
445 LPVOID Context)
446 {
447 TRACE("(%p, %p)\n", Callback, Context);
448
449 if (!Callback)
450 return DDERR_INVALIDPARAMS;
451 else
452 return DDERR_UNSUPPORTED;
453 }
454
455 /***********************************************************************
456 * DirectDrawEnumerateExW (DDRAW.@)
457 *
458 * Enumerates DirectDraw7 drivers, unicode version.
459 * This function is not implemented on Windows.
460 *
461 ***********************************************************************/
462 HRESULT WINAPI
463 DirectDrawEnumerateExW(LPDDENUMCALLBACKEXW Callback,
464 LPVOID Context,
465 DWORD Flags)
466 {
467 TRACE("(%p, %p, 0x%x)\n", Callback, Context, Flags);
468
469 return DDERR_UNSUPPORTED;
470 }
471
472 /***********************************************************************
473 * Classfactory implementation.
474 ***********************************************************************/
475
476 /***********************************************************************
477 * CF_CreateDirectDraw
478 *
479 * DDraw creation function for the class factory
480 *
481 * Params:
482 * UnkOuter: Set to NULL
483 * iid: ID of the wanted interface
484 * obj: Address to pass the interface pointer back
485 *
486 * Returns
487 * DD_OK / DDERR*, see DDRAW_Create
488 *
489 ***********************************************************************/
490 static HRESULT
491 CF_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
492 void **obj)
493 {
494 HRESULT hr;
495
496 TRACE("(%p,%s,%p)\n", UnkOuter, debugstr_guid(iid), obj);
497
498 EnterCriticalSection(&ddraw_cs);
499 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
500 LeaveCriticalSection(&ddraw_cs);
501 return hr;
502 }
503
504 /***********************************************************************
505 * CF_CreateDirectDraw
506 *
507 * Clipper creation function for the class factory
508 *
509 * Params:
510 * UnkOuter: Set to NULL
511 * iid: ID of the wanted interface
512 * obj: Address to pass the interface pointer back
513 *
514 * Returns
515 * DD_OK / DDERR*, see DDRAW_Create
516 *
517 ***********************************************************************/
518 static HRESULT
519 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
520 void **obj)
521 {
522 HRESULT hr;
523 IDirectDrawClipper *Clip;
524
525 EnterCriticalSection(&ddraw_cs);
526 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
527 if (hr != DD_OK)
528 {
529 LeaveCriticalSection(&ddraw_cs);
530 return hr;
531 }
532
533 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
534 IDirectDrawClipper_Release(Clip);
535
536 LeaveCriticalSection(&ddraw_cs);
537 return hr;
538 }
539
540 static const struct object_creation_info object_creation[] =
541 {
542 { &CLSID_DirectDraw, CF_CreateDirectDraw },
543 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
544 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
545 };
546
547 /*******************************************************************************
548 * IDirectDrawClassFactory::QueryInterface
549 *
550 * QueryInterface for the class factory
551 *
552 * PARAMS
553 * riid Reference to identifier of queried interface
554 * ppv Address to return the interface pointer at
555 *
556 * RETURNS
557 * Success: S_OK
558 * Failure: E_NOINTERFACE
559 *
560 *******************************************************************************/
561 static HRESULT WINAPI
562 IDirectDrawClassFactoryImpl_QueryInterface(IClassFactory *iface,
563 REFIID riid,
564 void **obj)
565 {
566 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
567
568 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obj);
569
570 if (IsEqualGUID(riid, &IID_IUnknown)
571 || IsEqualGUID(riid, &IID_IClassFactory))
572 {
573 IClassFactory_AddRef(iface);
574 *obj = This;
575 return S_OK;
576 }
577
578 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),obj);
579 return E_NOINTERFACE;
580 }
581
582 /*******************************************************************************
583 * IDirectDrawClassFactory::AddRef
584 *
585 * AddRef for the class factory
586 *
587 * RETURNS
588 * The new refcount
589 *
590 *******************************************************************************/
591 static ULONG WINAPI
592 IDirectDrawClassFactoryImpl_AddRef(IClassFactory *iface)
593 {
594 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
595 ULONG ref = InterlockedIncrement(&This->ref);
596
597 TRACE("(%p)->() incrementing from %d.\n", This, ref - 1);
598
599 return ref;
600 }
601
602 /*******************************************************************************
603 * IDirectDrawClassFactory::Release
604 *
605 * Release for the class factory. If the refcount falls to 0, the object
606 * is destroyed
607 *
608 * RETURNS
609 * The new refcount
610 *
611 *******************************************************************************/
612 static ULONG WINAPI
613 IDirectDrawClassFactoryImpl_Release(IClassFactory *iface)
614 {
615 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
616 ULONG ref = InterlockedDecrement(&This->ref);
617 TRACE("(%p)->() decrementing from %d.\n", This, ref+1);
618
619 if (ref == 0)
620 HeapFree(GetProcessHeap(), 0, This);
621
622 return ref;
623 }
624
625
626 /*******************************************************************************
627 * IDirectDrawClassFactory::CreateInstance
628 *
629 * What is this? Seems to create DirectDraw objects...
630 *
631 * Params
632 * The usual things???
633 *
634 * RETURNS
635 * ???
636 *
637 *******************************************************************************/
638 static HRESULT WINAPI
639 IDirectDrawClassFactoryImpl_CreateInstance(IClassFactory *iface,
640 IUnknown *UnkOuter,
641 REFIID riid,
642 void **obj)
643 {
644 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
645
646 TRACE("(%p)->(%p,%s,%p)\n",This,UnkOuter,debugstr_guid(riid),obj);
647
648 return This->pfnCreateInstance(UnkOuter, riid, obj);
649 }
650
651 /*******************************************************************************
652 * IDirectDrawClassFactory::LockServer
653 *
654 * What is this?
655 *
656 * Params
657 * ???
658 *
659 * RETURNS
660 * S_OK, because it's a stub
661 *
662 *******************************************************************************/
663 static HRESULT WINAPI
664 IDirectDrawClassFactoryImpl_LockServer(IClassFactory *iface,BOOL dolock)
665 {
666 IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
667 FIXME("(%p)->(%d),stub!\n",This,dolock);
668 return S_OK;
669 }
670
671 /*******************************************************************************
672 * The class factory VTable
673 *******************************************************************************/
674 static const IClassFactoryVtbl IClassFactory_Vtbl =
675 {
676 IDirectDrawClassFactoryImpl_QueryInterface,
677 IDirectDrawClassFactoryImpl_AddRef,
678 IDirectDrawClassFactoryImpl_Release,
679 IDirectDrawClassFactoryImpl_CreateInstance,
680 IDirectDrawClassFactoryImpl_LockServer
681 };
682
683 /*******************************************************************************
684 * DllGetClassObject [DDRAW.@]
685 * Retrieves class object from a DLL object
686 *
687 * NOTES
688 * Docs say returns STDAPI
689 *
690 * PARAMS
691 * rclsid [I] CLSID for the class object
692 * riid [I] Reference to identifier of interface for class object
693 * ppv [O] Address of variable to receive interface pointer for riid
694 *
695 * RETURNS
696 * Success: S_OK
697 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
698 * E_UNEXPECTED
699 */
700 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
701 {
702 unsigned int i;
703 IClassFactoryImpl *factory;
704
705 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
706
707 if ( !IsEqualGUID( &IID_IClassFactory, riid )
708 && ! IsEqualGUID( &IID_IUnknown, riid) )
709 return E_NOINTERFACE;
710
711 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
712 {
713 if (IsEqualGUID(object_creation[i].clsid, rclsid))
714 break;
715 }
716
717 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
718 {
719 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
720 return CLASS_E_CLASSNOTAVAILABLE;
721 }
722
723 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
724 if (factory == NULL) return E_OUTOFMEMORY;
725
726 factory->lpVtbl = &IClassFactory_Vtbl;
727 factory->ref = 1;
728
729 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
730
731 *ppv = factory;
732 return S_OK;
733 }
734
735
736 /*******************************************************************************
737 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
738 *
739 * RETURNS
740 * Success: S_OK
741 * Failure: S_FALSE
742 */
743 HRESULT WINAPI DllCanUnloadNow(void)
744 {
745 HRESULT hr;
746 FIXME("(void): stub\n");
747
748 EnterCriticalSection(&ddraw_cs);
749 hr = S_FALSE;
750 LeaveCriticalSection(&ddraw_cs);
751
752 return hr;
753 }
754
755 /*******************************************************************************
756 * DestroyCallback
757 *
758 * Callback function for the EnumSurfaces call in DllMain.
759 * Dumps some surface info and releases the surface
760 *
761 * Params:
762 * surf: The enumerated surface
763 * desc: it's description
764 * context: Pointer to the ddraw impl
765 *
766 * Returns:
767 * DDENUMRET_OK;
768 *******************************************************************************/
769 static HRESULT WINAPI
770 DestroyCallback(IDirectDrawSurface7 *surf,
771 DDSURFACEDESC2 *desc,
772 void *context)
773 {
774 IDirectDrawSurfaceImpl *Impl = (IDirectDrawSurfaceImpl *)surf;
775 ULONG ref;
776
777 ref = IDirectDrawSurface7_Release(surf); /* For the EnumSurfaces */
778 WARN("Surface %p has an reference count of %d\n", Impl, ref);
779
780 /* Skip surfaces which are attached somewhere or which are
781 * part of a complex compound. They will get released when destroying
782 * the root
783 */
784 if( (!Impl->is_complex_root) || (Impl->first_attached != Impl) )
785 return DDENUMRET_OK;
786
787 /* Destroy the surface */
788 while(ref) ref = IDirectDrawSurface7_Release(surf);
789
790 return DDENUMRET_OK;
791 }
792
793 /***********************************************************************
794 * get_config_key
795 *
796 * Reads a config key from the registry. Taken from WineD3D
797 *
798 ***********************************************************************/
799 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
800 {
801 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
802 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
803 return ERROR_FILE_NOT_FOUND;
804 }
805
806 /***********************************************************************
807 * DllMain (DDRAW.0)
808 *
809 * Could be used to register DirectDraw drivers, if we have more than
810 * one. Also used to destroy any objects left at unload if the
811 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
812 *
813 ***********************************************************************/
814 BOOL WINAPI
815 DllMain(HINSTANCE hInstDLL,
816 DWORD Reason,
817 LPVOID lpv)
818 {
819 TRACE("(%p,%x,%p)\n", hInstDLL, Reason, lpv);
820 if (Reason == DLL_PROCESS_ATTACH)
821 {
822 char buffer[MAX_PATH+10];
823 DWORD size = sizeof(buffer);
824 HKEY hkey = 0;
825 HKEY appkey = 0;
826 DWORD len;
827
828 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
829 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
830
831 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
832 if (len && len < MAX_PATH)
833 {
834 HKEY tmpkey;
835 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
836 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
837 {
838 char *p, *appname = buffer;
839 if ((p = strrchr( appname, '/' ))) appname = p + 1;
840 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
841 strcat( appname, "\\Direct3D" );
842 TRACE("appname = [%s]\n", appname);
843 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
844 RegCloseKey( tmpkey );
845 }
846 }
847
848 if ( 0 != hkey || 0 != appkey )
849 {
850 if ( !get_config_key( hkey, appkey, "DirectDrawRenderer", buffer, size) )
851 {
852 if (!strcmp(buffer,"gdi"))
853 {
854 TRACE("Defaulting to GDI surfaces\n");
855 DefaultSurfaceType = SURFACE_GDI;
856 }
857 else if (!strcmp(buffer,"opengl"))
858 {
859 TRACE("Defaulting to opengl surfaces\n");
860 DefaultSurfaceType = SURFACE_OPENGL;
861 }
862 else
863 {
864 ERR("Unknown default surface type. Supported are:\n gdi, opengl\n");
865 }
866 }
867 }
868
869 /* On Windows one can force the refresh rate that DirectDraw uses by
870 * setting an override value in dxdiag. This is documented in KB315614
871 * (main article), KB230002, and KB217348. By comparing registry dumps
872 * before and after setting the override, we see that the override value
873 * is stored in HKLM\Software\Microsoft\DirectDraw\ForceRefreshRate as a
874 * DWORD that represents the refresh rate to force. We use this
875 * registry entry to modify the behavior of SetDisplayMode so that Wine
876 * users can override the refresh rate in a Windows-compatible way.
877 *
878 * dxdiag will not accept a refresh rate lower than 40 or higher than
879 * 120 so this value should be within that range. It is, of course,
880 * possible for a user to set the registry entry value directly so that
881 * assumption might not hold.
882 *
883 * There is no current mechanism for setting this value through the Wine
884 * GUI. It would be most appropriate to set this value through a dxdiag
885 * clone, but it may be sufficient to use winecfg.
886 *
887 * TODO: Create a mechanism for setting this value through the Wine GUI.
888 */
889 if ( !RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectDraw", &hkey ) )
890 {
891 DWORD type, data;
892 size = sizeof(data);
893 if (!RegQueryValueExA( hkey, "ForceRefreshRate", NULL, &type, (LPBYTE)&data, &size ) && type == REG_DWORD)
894 {
895 TRACE("ForceRefreshRate set; overriding refresh rate to %d Hz\n", data);
896 force_refresh_rate = data;
897 }
898 RegCloseKey( hkey );
899 }
900
901 DisableThreadLibraryCalls(hInstDLL);
902 }
903 else if (Reason == DLL_PROCESS_DETACH)
904 {
905 if(!list_empty(&global_ddraw_list))
906 {
907 struct list *entry, *entry2;
908 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
909
910 /* We remove elements from this loop */
911 LIST_FOR_EACH_SAFE(entry, entry2, &global_ddraw_list)
912 {
913 HRESULT hr;
914 DDSURFACEDESC2 desc;
915 int i;
916 IDirectDrawImpl *ddraw = LIST_ENTRY(entry, IDirectDrawImpl, ddraw_list_entry);
917
918 WARN("DDraw %p has a refcount of %d\n", ddraw, ddraw->ref7 + ddraw->ref4 + ddraw->ref3 + ddraw->ref2 + ddraw->ref1);
919
920 /* Add references to each interface to avoid freeing them unexpectedly */
921 IDirectDraw_AddRef((IDirectDraw *)&ddraw->IDirectDraw_vtbl);
922 IDirectDraw2_AddRef((IDirectDraw2 *)&ddraw->IDirectDraw2_vtbl);
923 IDirectDraw4_AddRef((IDirectDraw4 *)&ddraw->IDirectDraw4_vtbl);
924 IDirectDraw7_AddRef((IDirectDraw7 *)ddraw);
925
926 /* Does a D3D device exist? Destroy it
927 * TODO: Destroy all Vertex buffers, Lights, Materials
928 * and execute buffers too
929 */
930 if(ddraw->d3ddevice)
931 {
932 WARN("DDraw %p has d3ddevice %p attached\n", ddraw, ddraw->d3ddevice);
933 while(IDirect3DDevice7_Release((IDirect3DDevice7 *)ddraw->d3ddevice));
934 }
935
936 /* Try to release the objects
937 * Do an EnumSurfaces to find any hanging surfaces
938 */
939 memset(&desc, 0, sizeof(desc));
940 desc.dwSize = sizeof(desc);
941 for(i = 0; i <= 1; i++)
942 {
943 hr = IDirectDraw7_EnumSurfaces((IDirectDraw7 *)ddraw,
944 DDENUMSURFACES_ALL, &desc, ddraw, DestroyCallback);
945 if(hr != D3D_OK)
946 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw);
947 }
948
949 /* Check the surface count */
950 if(ddraw->surfaces > 0)
951 ERR("DDraw %p still has %d surfaces attached\n", ddraw, ddraw->surfaces);
952
953 /* Release all hanging references to destroy the objects. This
954 * restores the screen mode too
955 */
956 while(IDirectDraw_Release((IDirectDraw *)&ddraw->IDirectDraw_vtbl));
957 while(IDirectDraw2_Release((IDirectDraw2 *)&ddraw->IDirectDraw2_vtbl));
958 while(IDirectDraw4_Release((IDirectDraw4 *)&ddraw->IDirectDraw4_vtbl));
959 while(IDirectDraw7_Release((IDirectDraw7 *)ddraw));
960 }
961 }
962 }
963
964 return TRUE;
965 }