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