[DDRAW]
[reactos.git] / reactos / 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 "ddraw_private.h"
28
29 #include <winreg.h>
30 #include <rpcproxy.h>
31
32 #include <wine/exception.h>
33
34 static struct list global_ddraw_list = LIST_INIT(global_ddraw_list);
35
36 static HINSTANCE instance;
37
38 /* value of ForceRefreshRate */
39 DWORD force_refresh_rate = 0;
40
41 /* Structure for converting DirectDrawEnumerateA to DirectDrawEnumerateExA */
42 struct callback_info
43 {
44 LPDDENUMCALLBACKA callback;
45 void *context;
46 };
47
48 /* Enumeration callback for converting DirectDrawEnumerateA to DirectDrawEnumerateExA */
49 static HRESULT CALLBACK enum_callback(GUID *guid, char *description, char *driver_name,
50 void *context, HMONITOR monitor)
51 {
52 const struct callback_info *info = context;
53
54 return info->callback(guid, description, driver_name, info->context);
55 }
56
57 /* Handle table functions */
58 BOOL ddraw_handle_table_init(struct ddraw_handle_table *t, UINT initial_size)
59 {
60 t->entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, initial_size * sizeof(*t->entries));
61 if (!t->entries)
62 {
63 ERR("Failed to allocate handle table memory.\n");
64 return FALSE;
65 }
66 t->free_entries = NULL;
67 t->table_size = initial_size;
68 t->entry_count = 0;
69
70 return TRUE;
71 }
72
73 void ddraw_handle_table_destroy(struct ddraw_handle_table *t)
74 {
75 HeapFree(GetProcessHeap(), 0, t->entries);
76 memset(t, 0, sizeof(*t));
77 }
78
79 DWORD ddraw_allocate_handle(struct ddraw_handle_table *t, void *object, enum ddraw_handle_type type)
80 {
81 struct ddraw_handle_entry *entry;
82
83 if (t->free_entries)
84 {
85 DWORD idx = t->free_entries - t->entries;
86 /* Use a free handle */
87 entry = t->free_entries;
88 if (entry->type != DDRAW_HANDLE_FREE)
89 {
90 ERR("Handle %#x (%p) is in the free list, but has type %#x.\n", idx, entry->object, entry->type);
91 return DDRAW_INVALID_HANDLE;
92 }
93 t->free_entries = entry->object;
94 entry->object = object;
95 entry->type = type;
96
97 return idx;
98 }
99
100 if (!(t->entry_count < t->table_size))
101 {
102 /* Grow the table */
103 UINT new_size = t->table_size + (t->table_size >> 1);
104 struct ddraw_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
105 0, t->entries, new_size * sizeof(*t->entries));
106 if (!new_entries)
107 {
108 ERR("Failed to grow the handle table.\n");
109 return DDRAW_INVALID_HANDLE;
110 }
111 t->entries = new_entries;
112 t->table_size = new_size;
113 }
114
115 entry = &t->entries[t->entry_count];
116 entry->object = object;
117 entry->type = type;
118
119 return t->entry_count++;
120 }
121
122 void *ddraw_free_handle(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
123 {
124 struct ddraw_handle_entry *entry;
125 void *object;
126
127 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
128 {
129 WARN("Invalid handle %#x passed.\n", handle);
130 return NULL;
131 }
132
133 entry = &t->entries[handle];
134 if (entry->type != type)
135 {
136 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
137 return NULL;
138 }
139
140 object = entry->object;
141 entry->object = t->free_entries;
142 entry->type = DDRAW_HANDLE_FREE;
143 t->free_entries = entry;
144
145 return object;
146 }
147
148 void *ddraw_get_object(struct ddraw_handle_table *t, DWORD handle, enum ddraw_handle_type type)
149 {
150 struct ddraw_handle_entry *entry;
151
152 if (handle == DDRAW_INVALID_HANDLE || handle >= t->entry_count)
153 {
154 WARN("Invalid handle %#x passed.\n", handle);
155 return NULL;
156 }
157
158 entry = &t->entries[handle];
159 if (entry->type != type)
160 {
161 WARN("Handle %#x (%p) is not of type %#x.\n", handle, entry->object, type);
162 return NULL;
163 }
164
165 return entry->object;
166 }
167
168 /***********************************************************************
169 *
170 * Helper function for DirectDrawCreate and friends
171 * Creates a new DDraw interface with the given REFIID
172 *
173 * Interfaces that can be created:
174 * IDirectDraw, IDirectDraw2, IDirectDraw4, IDirectDraw7
175 * IDirect3D, IDirect3D2, IDirect3D3, IDirect3D7. (Does Windows return
176 * IDirect3D interfaces?)
177 *
178 * Arguments:
179 * guid: ID of the requested driver, NULL for the default driver.
180 * The GUID can be queried with DirectDrawEnumerate(Ex)A/W
181 * DD: Used to return the pointer to the created object
182 * UnkOuter: For aggregation, which is unsupported. Must be NULL
183 * iid: requested version ID.
184 *
185 * Returns:
186 * DD_OK if the Interface was created successfully
187 * CLASS_E_NOAGGREGATION if UnkOuter is not NULL
188 * E_OUTOFMEMORY if some allocation failed
189 *
190 ***********************************************************************/
191 static HRESULT
192 DDRAW_Create(const GUID *guid,
193 void **DD,
194 IUnknown *UnkOuter,
195 REFIID iid)
196 {
197 enum wined3d_device_type device_type;
198 struct ddraw *ddraw;
199 HRESULT hr;
200
201 TRACE("driver_guid %s, ddraw %p, outer_unknown %p, interface_iid %s.\n",
202 debugstr_guid(guid), DD, UnkOuter, debugstr_guid(iid));
203
204 *DD = NULL;
205
206 /* We don't care about this guids. Well, there's no special guid anyway
207 * OK, we could
208 */
209 if (guid == (GUID *) DDCREATE_EMULATIONONLY)
210 {
211 /* Use the reference device id. This doesn't actually change anything,
212 * WineD3D always uses OpenGL for D3D rendering. One could make it request
213 * indirect rendering
214 */
215 device_type = WINED3D_DEVICE_TYPE_REF;
216 }
217 else if(guid == (GUID *) DDCREATE_HARDWAREONLY)
218 {
219 device_type = WINED3D_DEVICE_TYPE_HAL;
220 }
221 else
222 {
223 device_type = 0;
224 }
225
226 /* DDraw doesn't support aggregation, according to msdn */
227 if (UnkOuter != NULL)
228 return CLASS_E_NOAGGREGATION;
229
230 /* DirectDraw creation comes here */
231 ddraw = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ddraw));
232 if (!ddraw)
233 {
234 ERR("Out of memory when creating DirectDraw\n");
235 return E_OUTOFMEMORY;
236 }
237
238 hr = ddraw_init(ddraw, device_type);
239 if (FAILED(hr))
240 {
241 WARN("Failed to initialize ddraw object, hr %#x.\n", hr);
242 HeapFree(GetProcessHeap(), 0, ddraw);
243 return hr;
244 }
245
246 hr = IDirectDraw7_QueryInterface(&ddraw->IDirectDraw7_iface, iid, DD);
247 IDirectDraw7_Release(&ddraw->IDirectDraw7_iface);
248 if (SUCCEEDED(hr))
249 list_add_head(&global_ddraw_list, &ddraw->ddraw_list_entry);
250 else
251 WARN("Failed to query interface %s from ddraw object %p.\n", debugstr_guid(iid), ddraw);
252
253 return hr;
254 }
255
256 /***********************************************************************
257 * DirectDrawCreate (DDRAW.@)
258 *
259 * Creates legacy DirectDraw Interfaces. Can't create IDirectDraw7
260 * interfaces in theory
261 *
262 * Arguments, return values: See DDRAW_Create
263 *
264 ***********************************************************************/
265 HRESULT WINAPI DECLSPEC_HOTPATCH DirectDrawCreate(GUID *driver_guid, IDirectDraw **ddraw, IUnknown *outer)
266 {
267 HRESULT hr;
268
269 TRACE("driver_guid %s, ddraw %p, outer %p.\n",
270 debugstr_guid(driver_guid), ddraw, outer);
271
272 wined3d_mutex_lock();
273 hr = DDRAW_Create(driver_guid, (void **)ddraw, outer, &IID_IDirectDraw);
274 wined3d_mutex_unlock();
275
276 if (SUCCEEDED(hr))
277 {
278 if (FAILED(hr = IDirectDraw_Initialize(*ddraw, driver_guid)))
279 IDirectDraw_Release(*ddraw);
280 }
281
282 return hr;
283 }
284
285 /***********************************************************************
286 * DirectDrawCreateEx (DDRAW.@)
287 *
288 * Only creates new IDirectDraw7 interfaces, supposed to fail if legacy
289 * interfaces are requested.
290 *
291 * Arguments, return values: See DDRAW_Create
292 *
293 ***********************************************************************/
294 HRESULT WINAPI DECLSPEC_HOTPATCH
295 DirectDrawCreateEx(GUID *guid,
296 LPVOID *dd,
297 REFIID iid,
298 IUnknown *UnkOuter)
299 {
300 HRESULT hr;
301
302 TRACE("driver_guid %s, ddraw %p, interface_iid %s, outer_unknown %p.\n",
303 debugstr_guid(guid), dd, debugstr_guid(iid), UnkOuter);
304
305 if (!IsEqualGUID(iid, &IID_IDirectDraw7))
306 return DDERR_INVALIDPARAMS;
307
308 wined3d_mutex_lock();
309 hr = DDRAW_Create(guid, dd, UnkOuter, iid);
310 wined3d_mutex_unlock();
311
312 if (SUCCEEDED(hr))
313 {
314 IDirectDraw7 *ddraw7 = *(IDirectDraw7 **)dd;
315 hr = IDirectDraw7_Initialize(ddraw7, guid);
316 if (FAILED(hr))
317 IDirectDraw7_Release(ddraw7);
318 }
319
320 return hr;
321 }
322
323 /***********************************************************************
324 * DirectDrawEnumerateA (DDRAW.@)
325 *
326 * Enumerates legacy ddraw drivers, ascii version. We only have one
327 * driver, which relays to WineD3D. If we were sufficiently cool,
328 * we could offer various interfaces, which use a different default surface
329 * implementation, but I think it's better to offer this choice in
330 * winecfg, because some apps use the default driver, so we would need
331 * a winecfg option anyway, and there shouldn't be 2 ways to set one setting
332 *
333 * Arguments:
334 * Callback: Callback function from the app
335 * Context: Argument to the call back.
336 *
337 * Returns:
338 * DD_OK on success
339 * E_INVALIDARG if the Callback caused a page fault
340 *
341 *
342 ***********************************************************************/
343 HRESULT WINAPI DirectDrawEnumerateA(LPDDENUMCALLBACKA callback, void *context)
344 {
345 struct callback_info info;
346
347 TRACE("callback %p, context %p.\n", callback, context);
348
349 info.callback = callback;
350 info.context = context;
351 return DirectDrawEnumerateExA(enum_callback, &info, 0x0);
352 }
353
354 /***********************************************************************
355 * DirectDrawEnumerateExA (DDRAW.@)
356 *
357 * Enumerates DirectDraw7 drivers, ascii version. See
358 * the comments above DirectDrawEnumerateA for more details.
359 *
360 * The Flag member is not supported right now.
361 *
362 ***********************************************************************/
363 HRESULT WINAPI DirectDrawEnumerateExA(LPDDENUMCALLBACKEXA callback, void *context, DWORD flags)
364 {
365 struct wined3d *wined3d;
366
367 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
368
369 if (flags & ~(DDENUM_ATTACHEDSECONDARYDEVICES |
370 DDENUM_DETACHEDSECONDARYDEVICES |
371 DDENUM_NONDISPLAYDEVICES))
372 return DDERR_INVALIDPARAMS;
373
374 if (flags)
375 FIXME("flags 0x%08x not handled\n", flags);
376
377 TRACE("Enumerating ddraw interfaces\n");
378 if (!(wined3d = wined3d_create(7, WINED3D_LEGACY_DEPTH_BIAS)))
379 {
380 if (!(wined3d = wined3d_create(7, WINED3D_LEGACY_DEPTH_BIAS | WINED3D_NO3D)))
381 {
382 WARN("Failed to create a wined3d object.\n");
383 return E_FAIL;
384 }
385
386 WARN("Created a wined3d object without 3D support.\n");
387 }
388
389 __TRY
390 {
391 /* QuickTime expects the description "DirectDraw HAL" */
392 static CHAR driver_desc[] = "DirectDraw HAL",
393 driver_name[] = "display";
394 struct wined3d_adapter_identifier adapter_id;
395 HRESULT hr = S_OK;
396 UINT adapter = 0;
397 BOOL cont_enum;
398
399 /* The Battle.net System Checker expects both a NULL device and a GUID-based device */
400 TRACE("Default interface: DirectDraw HAL\n");
401 cont_enum = callback(NULL, driver_desc, driver_name, context, 0);
402 for (adapter = 0; SUCCEEDED(hr) && cont_enum; adapter++)
403 {
404 char DriverName[512] = "";
405
406 /* The Battle.net System Checker expects the GetAdapterIdentifier DeviceName to match the
407 * Driver Name, so obtain the DeviceName and GUID from D3D. */
408 memset(&adapter_id, 0x0, sizeof(adapter_id));
409 adapter_id.device_name = DriverName;
410 adapter_id.device_name_size = sizeof(DriverName);
411 wined3d_mutex_lock();
412 hr = wined3d_get_adapter_identifier(wined3d, adapter, 0x0, &adapter_id);
413 wined3d_mutex_unlock();
414 if (SUCCEEDED(hr))
415 {
416 TRACE("Interface %d: %s\n", adapter, wine_dbgstr_guid(&adapter_id.device_identifier));
417 cont_enum = callback(&adapter_id.device_identifier, driver_desc,
418 adapter_id.device_name, context, 0);
419 }
420 }
421 }
422 __EXCEPT_PAGE_FAULT
423 {
424 wined3d_decref(wined3d);
425 return DDERR_INVALIDPARAMS;
426 }
427 __ENDTRY;
428
429 wined3d_decref(wined3d);
430 TRACE("End of enumeration\n");
431 return DD_OK;
432 }
433
434 /***********************************************************************
435 * DirectDrawEnumerateW (DDRAW.@)
436 *
437 * Enumerates legacy drivers, unicode version.
438 * This function is not implemented on Windows.
439 *
440 ***********************************************************************/
441 HRESULT WINAPI DirectDrawEnumerateW(LPDDENUMCALLBACKW callback, void *context)
442 {
443 TRACE("callback %p, context %p.\n", callback, context);
444
445 if (!callback)
446 return DDERR_INVALIDPARAMS;
447 else
448 return DDERR_UNSUPPORTED;
449 }
450
451 /***********************************************************************
452 * DirectDrawEnumerateExW (DDRAW.@)
453 *
454 * Enumerates DirectDraw7 drivers, unicode version.
455 * This function is not implemented on Windows.
456 *
457 ***********************************************************************/
458 HRESULT WINAPI DirectDrawEnumerateExW(LPDDENUMCALLBACKEXW callback, void *context, DWORD flags)
459 {
460 TRACE("callback %p, context %p, flags %#x.\n", callback, context, flags);
461
462 return DDERR_UNSUPPORTED;
463 }
464
465 /***********************************************************************
466 * Classfactory implementation.
467 ***********************************************************************/
468
469 /***********************************************************************
470 * CF_CreateDirectDraw
471 *
472 * DDraw creation function for the class factory
473 *
474 * Params:
475 * UnkOuter: Set to NULL
476 * iid: ID of the wanted interface
477 * obj: Address to pass the interface pointer back
478 *
479 * Returns
480 * DD_OK / DDERR*, see DDRAW_Create
481 *
482 ***********************************************************************/
483 static HRESULT
484 CF_CreateDirectDraw(IUnknown* UnkOuter, REFIID iid,
485 void **obj)
486 {
487 HRESULT hr;
488
489 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(iid), obj);
490
491 wined3d_mutex_lock();
492 hr = DDRAW_Create(NULL, obj, UnkOuter, iid);
493 wined3d_mutex_unlock();
494
495 return hr;
496 }
497
498 /***********************************************************************
499 * CF_CreateDirectDraw
500 *
501 * Clipper creation function for the class factory
502 *
503 * Params:
504 * UnkOuter: Set to NULL
505 * iid: ID of the wanted interface
506 * obj: Address to pass the interface pointer back
507 *
508 * Returns
509 * DD_OK / DDERR*, see DDRAW_Create
510 *
511 ***********************************************************************/
512 static HRESULT
513 CF_CreateDirectDrawClipper(IUnknown* UnkOuter, REFIID riid,
514 void **obj)
515 {
516 HRESULT hr;
517 IDirectDrawClipper *Clip;
518
519 TRACE("outer_unknown %p, riid %s, object %p.\n", UnkOuter, debugstr_guid(riid), obj);
520
521 wined3d_mutex_lock();
522 hr = DirectDrawCreateClipper(0, &Clip, UnkOuter);
523 if (hr != DD_OK)
524 {
525 wined3d_mutex_unlock();
526 return hr;
527 }
528
529 hr = IDirectDrawClipper_QueryInterface(Clip, riid, obj);
530 IDirectDrawClipper_Release(Clip);
531
532 wined3d_mutex_unlock();
533
534 return hr;
535 }
536
537 static const struct object_creation_info object_creation[] =
538 {
539 { &CLSID_DirectDraw, CF_CreateDirectDraw },
540 { &CLSID_DirectDraw7, CF_CreateDirectDraw },
541 { &CLSID_DirectDrawClipper, CF_CreateDirectDrawClipper }
542 };
543
544 struct ddraw_class_factory
545 {
546 IClassFactory IClassFactory_iface;
547
548 LONG ref;
549 HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, REFIID iid, LPVOID *ppObj);
550 };
551
552 static inline struct ddraw_class_factory *impl_from_IClassFactory(IClassFactory *iface)
553 {
554 return CONTAINING_RECORD(iface, struct ddraw_class_factory, IClassFactory_iface);
555 }
556
557 /*******************************************************************************
558 * IDirectDrawClassFactory::QueryInterface
559 *
560 * QueryInterface for the class factory
561 *
562 * PARAMS
563 * riid Reference to identifier of queried interface
564 * ppv Address to return the interface pointer at
565 *
566 * RETURNS
567 * Success: S_OK
568 * Failure: E_NOINTERFACE
569 *
570 *******************************************************************************/
571 static HRESULT WINAPI ddraw_class_factory_QueryInterface(IClassFactory *iface, REFIID riid, void **out)
572 {
573 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
574
575 if (IsEqualGUID(riid, &IID_IUnknown)
576 || IsEqualGUID(riid, &IID_IClassFactory))
577 {
578 IClassFactory_AddRef(iface);
579 *out = iface;
580 return S_OK;
581 }
582
583 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
584
585 return E_NOINTERFACE;
586 }
587
588 /*******************************************************************************
589 * IDirectDrawClassFactory::AddRef
590 *
591 * AddRef for the class factory
592 *
593 * RETURNS
594 * The new refcount
595 *
596 *******************************************************************************/
597 static ULONG WINAPI ddraw_class_factory_AddRef(IClassFactory *iface)
598 {
599 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
600 ULONG ref = InterlockedIncrement(&factory->ref);
601
602 TRACE("%p increasing refcount to %u.\n", factory, ref);
603
604 return ref;
605 }
606
607 /*******************************************************************************
608 * IDirectDrawClassFactory::Release
609 *
610 * Release for the class factory. If the refcount falls to 0, the object
611 * is destroyed
612 *
613 * RETURNS
614 * The new refcount
615 *
616 *******************************************************************************/
617 static ULONG WINAPI ddraw_class_factory_Release(IClassFactory *iface)
618 {
619 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
620 ULONG ref = InterlockedDecrement(&factory->ref);
621
622 TRACE("%p decreasing refcount to %u.\n", factory, ref);
623
624 if (!ref)
625 HeapFree(GetProcessHeap(), 0, factory);
626
627 return ref;
628 }
629
630
631 /*******************************************************************************
632 * IDirectDrawClassFactory::CreateInstance
633 *
634 * What is this? Seems to create DirectDraw objects...
635 *
636 * Params
637 * The usual things???
638 *
639 * RETURNS
640 * ???
641 *
642 *******************************************************************************/
643 static HRESULT WINAPI ddraw_class_factory_CreateInstance(IClassFactory *iface,
644 IUnknown *outer_unknown, REFIID riid, void **out)
645 {
646 struct ddraw_class_factory *factory = impl_from_IClassFactory(iface);
647
648 TRACE("iface %p, outer_unknown %p, riid %s, out %p.\n",
649 iface, outer_unknown, debugstr_guid(riid), out);
650
651 return factory->pfnCreateInstance(outer_unknown, riid, out);
652 }
653
654 /*******************************************************************************
655 * IDirectDrawClassFactory::LockServer
656 *
657 * What is this?
658 *
659 * Params
660 * ???
661 *
662 * RETURNS
663 * S_OK, because it's a stub
664 *
665 *******************************************************************************/
666 static HRESULT WINAPI ddraw_class_factory_LockServer(IClassFactory *iface, BOOL dolock)
667 {
668 FIXME("iface %p, dolock %#x stub!\n", iface, dolock);
669
670 return S_OK;
671 }
672
673 /*******************************************************************************
674 * The class factory VTable
675 *******************************************************************************/
676 static const IClassFactoryVtbl IClassFactory_Vtbl =
677 {
678 ddraw_class_factory_QueryInterface,
679 ddraw_class_factory_AddRef,
680 ddraw_class_factory_Release,
681 ddraw_class_factory_CreateInstance,
682 ddraw_class_factory_LockServer
683 };
684
685 /*******************************************************************************
686 * DllGetClassObject [DDRAW.@]
687 * Retrieves class object from a DLL object
688 *
689 * NOTES
690 * Docs say returns STDAPI
691 *
692 * PARAMS
693 * rclsid [I] CLSID for the class object
694 * riid [I] Reference to identifier of interface for class object
695 * ppv [O] Address of variable to receive interface pointer for riid
696 *
697 * RETURNS
698 * Success: S_OK
699 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
700 * E_UNEXPECTED
701 */
702 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
703 {
704 struct ddraw_class_factory *factory;
705 unsigned int i;
706
707 TRACE("rclsid %s, riid %s, object %p.\n",
708 debugstr_guid(rclsid), debugstr_guid(riid), ppv);
709
710 if (!IsEqualGUID(&IID_IClassFactory, riid)
711 && !IsEqualGUID(&IID_IUnknown, riid))
712 return E_NOINTERFACE;
713
714 for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
715 {
716 if (IsEqualGUID(object_creation[i].clsid, rclsid))
717 break;
718 }
719
720 if (i == sizeof(object_creation)/sizeof(object_creation[0]))
721 {
722 FIXME("%s: no class found.\n", debugstr_guid(rclsid));
723 return CLASS_E_CLASSNOTAVAILABLE;
724 }
725
726 factory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*factory));
727 if (factory == NULL) return E_OUTOFMEMORY;
728
729 factory->IClassFactory_iface.lpVtbl = &IClassFactory_Vtbl;
730 factory->ref = 1;
731
732 factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
733
734 *ppv = factory;
735 return S_OK;
736 }
737
738
739 /*******************************************************************************
740 * DllCanUnloadNow [DDRAW.@] Determines whether the DLL is in use.
741 *
742 * RETURNS
743 * Success: S_OK
744 * Failure: S_FALSE
745 */
746 HRESULT WINAPI DllCanUnloadNow(void)
747 {
748 TRACE("\n");
749
750 return S_FALSE;
751 }
752
753
754 /***********************************************************************
755 * DllRegisterServer (DDRAW.@)
756 */
757 HRESULT WINAPI DllRegisterServer(void)
758 {
759 return __wine_register_resources( instance );
760 }
761
762 /***********************************************************************
763 * DllUnregisterServer (DDRAW.@)
764 */
765 HRESULT WINAPI DllUnregisterServer(void)
766 {
767 return __wine_unregister_resources( instance );
768 }
769
770 /*******************************************************************************
771 * DestroyCallback
772 *
773 * Callback function for the EnumSurfaces call in DllMain.
774 * Dumps some surface info and releases the surface
775 *
776 * Params:
777 * surf: The enumerated surface
778 * desc: it's description
779 * context: Pointer to the ddraw impl
780 *
781 * Returns:
782 * DDENUMRET_OK;
783 *******************************************************************************/
784 static HRESULT WINAPI
785 DestroyCallback(IDirectDrawSurface7 *surf,
786 DDSURFACEDESC2 *desc,
787 void *context)
788 {
789 struct ddraw_surface *Impl = impl_from_IDirectDrawSurface7(surf);
790 ULONG ref7, ref4, ref3, ref2, ref1, gamma_count, iface_count;
791
792 ref7 = IDirectDrawSurface7_Release(surf); /* For the EnumSurfaces */
793 ref4 = Impl->ref4;
794 ref3 = Impl->ref3;
795 ref2 = Impl->ref2;
796 ref1 = Impl->ref1;
797 gamma_count = Impl->gamma_count;
798
799 WARN("Surface %p has an reference counts of 7: %u 4: %u 3: %u 2: %u 1: %u gamma: %u\n",
800 Impl, ref7, ref4, ref3, ref2, ref1, gamma_count);
801
802 /* Skip surfaces which are attached somewhere or which are
803 * part of a complex compound. They will get released when destroying
804 * the root
805 */
806 if( (!Impl->is_complex_root) || (Impl->first_attached != Impl) )
807 return DDENUMRET_OK;
808
809 /* Destroy the surface */
810 iface_count = ddraw_surface_release_iface(Impl);
811 while (iface_count) iface_count = ddraw_surface_release_iface(Impl);
812
813 return DDENUMRET_OK;
814 }
815
816 /***********************************************************************
817 * get_config_key
818 *
819 * Reads a config key from the registry. Taken from WineD3D
820 *
821 ***********************************************************************/
822 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
823 {
824 if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
825 if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
826 return ERROR_FILE_NOT_FOUND;
827 }
828
829 /***********************************************************************
830 * DllMain (DDRAW.0)
831 *
832 * Could be used to register DirectDraw drivers, if we have more than
833 * one. Also used to destroy any objects left at unload if the
834 * app didn't release them properly(Gothic 2, Diablo 2, Moto racer, ...)
835 *
836 ***********************************************************************/
837 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD reason, LPVOID reserved)
838 {
839 TRACE("(%p,%x,%p)\n", hInstDLL, reason, reserved);
840 switch (reason)
841 {
842 case DLL_PROCESS_ATTACH:
843 {
844 static HMODULE ddraw_self;
845 HKEY hkey = 0;
846 WNDCLASSA wc;
847
848 /* Register the window class. This is used to create a hidden window
849 * for D3D rendering, if the application didn't pass one. It can also
850 * be used for creating a device window from SetCooperativeLevel(). */
851 wc.style = CS_HREDRAW | CS_VREDRAW;
852 wc.lpfnWndProc = DefWindowProcA;
853 wc.cbClsExtra = 0;
854 wc.cbWndExtra = 0;
855 wc.hInstance = hInstDLL;
856 wc.hIcon = 0;
857 wc.hCursor = 0;
858 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
859 wc.lpszMenuName = NULL;
860 wc.lpszClassName = DDRAW_WINDOW_CLASS_NAME;
861 if (!RegisterClassA(&wc))
862 {
863 ERR("Failed to register ddraw window class, last error %#x.\n", GetLastError());
864 return FALSE;
865 }
866
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.
875 *
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.
880 *
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.
884 *
885 * TODO: Create a mechanism for setting this value through the Wine GUI.
886 */
887 if ( !RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectDraw", &hkey ) )
888 {
889 DWORD type, data, size;
890
891 size = sizeof(data);
892 if (!RegQueryValueExA( hkey, "ForceRefreshRate", NULL, &type, (LPBYTE)&data, &size ) && type == REG_DWORD)
893 {
894 TRACE("ForceRefreshRate set; overriding refresh rate to %d Hz\n", data);
895 force_refresh_rate = data;
896 }
897 RegCloseKey( hkey );
898 }
899
900 /* Prevent the ddraw module from being unloaded. When switching to
901 * exclusive mode, we replace the window proc of the ddraw window. If
902 * an application would unload ddraw from the WM_DESTROY handler for
903 * that window, it would return to unmapped memory and die. Apparently
904 * this is supposed to work on Windows. We should probably use
905 * GET_MODULE_HANDLE_EX_FLAG_PIN for this, but that's not currently
906 * implemented. */
907 if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (const WCHAR *)&ddraw_self, &ddraw_self))
908 ERR("Failed to get own module handle.\n");
909
910 instance = hInstDLL;
911 DisableThreadLibraryCalls(hInstDLL);
912 break;
913 }
914
915 case DLL_PROCESS_DETACH:
916 if(!list_empty(&global_ddraw_list))
917 {
918 struct list *entry, *entry2;
919 WARN("There are still existing DirectDraw interfaces. Wine bug or buggy application?\n");
920
921 /* We remove elements from this loop */
922 LIST_FOR_EACH_SAFE(entry, entry2, &global_ddraw_list)
923 {
924 struct ddraw *ddraw = LIST_ENTRY(entry, struct ddraw, ddraw_list_entry);
925 HRESULT hr;
926 DDSURFACEDESC2 desc;
927 int i;
928
929 WARN("DDraw %p has a refcount of %d\n", ddraw, ddraw->ref7 + ddraw->ref4 + ddraw->ref3 + ddraw->ref2 + ddraw->ref1);
930
931 /* Add references to each interface to avoid freeing them unexpectedly */
932 IDirectDraw_AddRef(&ddraw->IDirectDraw_iface);
933 IDirectDraw2_AddRef(&ddraw->IDirectDraw2_iface);
934 IDirectDraw4_AddRef(&ddraw->IDirectDraw4_iface);
935 IDirectDraw7_AddRef(&ddraw->IDirectDraw7_iface);
936
937 /* Does a D3D device exist? Destroy it
938 * TODO: Destroy all Vertex buffers, Lights, Materials
939 * and execute buffers too
940 */
941 if(ddraw->d3ddevice)
942 {
943 WARN("DDraw %p has d3ddevice %p attached\n", ddraw, ddraw->d3ddevice);
944 while(IDirect3DDevice7_Release(&ddraw->d3ddevice->IDirect3DDevice7_iface));
945 }
946
947 /* Destroy the swapchain after any 3D device. The 3D device
948 * cleanup code needs a swapchain. Specifically, it tries to
949 * set the current render target to the front buffer. */
950 if (ddraw->wined3d_swapchain)
951 ddraw_destroy_swapchain(ddraw);
952
953 /* Try to release the objects
954 * Do an EnumSurfaces to find any hanging surfaces
955 */
956 memset(&desc, 0, sizeof(desc));
957 desc.dwSize = sizeof(desc);
958 for(i = 0; i <= 1; i++)
959 {
960 hr = IDirectDraw7_EnumSurfaces(&ddraw->IDirectDraw7_iface, DDENUMSURFACES_ALL,
961 &desc, ddraw, DestroyCallback);
962 if(hr != D3D_OK)
963 ERR("(%p) EnumSurfaces failed, prepare for trouble\n", ddraw);
964 }
965
966 if (!list_empty(&ddraw->surface_list))
967 ERR("DDraw %p still has surfaces attached.\n", ddraw);
968
969 /* Release all hanging references to destroy the objects. This
970 * restores the screen mode too
971 */
972 while(IDirectDraw_Release(&ddraw->IDirectDraw_iface));
973 while(IDirectDraw2_Release(&ddraw->IDirectDraw2_iface));
974 while(IDirectDraw4_Release(&ddraw->IDirectDraw4_iface));
975 while(IDirectDraw7_Release(&ddraw->IDirectDraw7_iface));
976 }
977 }
978
979 if (reserved) break;
980 UnregisterClassA(DDRAW_WINDOW_CLASS_NAME, hInstDLL);
981 }
982
983 return TRUE;
984 }