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